示例#1
0
    def liftAsProcessSample(self, coefficients):
        '''Function to lift a sample of coefficients into a collections of
        process samples and points.

        Parameters
        ----------
        coefficients : ot.Sample
            sample of values, follwing a centered normal law in general

        Returns
        -------
        processes : list
            ordered list of samples of scalars (ot.Sample) and field samples (ot.ProcessSample)
        '''
        assert isinstance(coefficients, (ot.Sample, ot.SampleImplementation))
        print('Lifting as process sample')
        jumpDim = 0
        processes = []
        for i in range(self.__field_distribution_count__):
            if self.__isProcess__[i] :
                if not self.__liftWithMean__:
                    processes.append(self.__KL_lifting__[i](coefficients[:, jumpDim : jumpDim + self.__mode_count__[i]]))
                else :
                    processSample = self.__KL_lifting__[i](coefficients[:, jumpDim : jumpDim + self.__mode_count__[i]])
                    addConstant2Iterable(processSample, self.__means__[i])
                    processes.append(processSample)
            else :
                if not self.__liftWithMean__:
                    processSample = ot.ProcessSample(ot.Mesh(), 0, 1)
                    val_sample = self.__KL_lifting__[i](coefficients[:, jumpDim : jumpDim + self.__mode_count__[i]])
                    for j, value in enumerate(val_sample):
                        field = ot.Field(ot.Mesh(),1)
                        field.setValueAtIndex(0,value)
                        processSample.add(field)
                    processes.append(processSample)
                else :
                    processSample = ot.ProcessSample(ot.Mesh(), 0, 1)
                    val_sample = self.__KL_lifting__[i](coefficients[:, jumpDim : jumpDim + self.__mode_count__[i]])
                    mean = self.__means__[i]
                    for j, value in enumerate(val_sample):
                        field = ot.Field(ot.Mesh(),1)
                        field.setValueAtIndex(0,[value[0]+mean]) # adding mean
                        processSample.add(field)
                    processes.append(processSample)
            jumpDim += self.__mode_count__[i]
        return processes
def readProcessSample(fname):
    """
    Return a ProcessSample from a text file.
    Assume the mesh is regular [0,1].
    """
    # Dataset
    data = np.loadtxt(fname)

    # Create the mesh
    n_nodes = data.shape[1]
    mesher = ot.IntervalMesher([n_nodes - 1])
    Interval = ot.Interval([0.0], [1.0])
    mesh = mesher.build(Interval)

    # Create the ProcessSample from the data
    n_fields = data.shape[0]
    dim_fields = 1
    processSample = ot.ProcessSample(mesh, n_fields, dim_fields)
    for i in range(n_fields):
        trajectory = ot.Sample([[x] for x in data[i, :]])
        processSample[i] = ot.Field(mesh, trajectory)
    return processSample
示例#3
0
        Y = Xs * ([2.0] * Xs.getDimension())
        return Y

    def isActingPointwise(self):
        return True


F = FUNC()
print('in_dim=' + str(F.getInputDimension()) + ' out_dim=' +
      str(F.getOutputDimension()) + ' spatial_dim=' +
      str(F.getInputMesh().getDimension()))

X = ot.Field(mesh, ot.Normal(2).getSample(11))
print(F(X.getValues()))

Xsample = ot.ProcessSample(5, X)
print(F(Xsample))

# Instance creation
myFunc = ot.FieldFunction(F)

# Copy constructor
newFunc = ot.FieldFunction(myFunc)

print(('myFunc input dimension= ' + str(myFunc.getInputDimension())))
print(('myFunc output dimension= ' + str(myFunc.getOutputDimension())))

print(myFunc(X.getValues()))

print(myFunc(Xsample))
# size of timeGrid
size = 6
dimension = 1
sample = ot.Sample(size, dimension)
for i in range(size):
    for j in range(dimension):
        sample[i, j] = i + j + 1

# TimeGrid
timeGrid = ot.RegularGrid(0.0, 1.0 / (size - 1), size)

# TimeSeries
timeSeries = ot.TimeSeries(timeGrid, sample)

# We create an empty ProcessSample with default constructor
psample0 = ot.ProcessSample()
psample0.setName("PSample0")
print("Default constructor")
print("psample0=", psample0)

# We create an empty ProcessSample with timeGrid, size and dimension
# arguments
psample1 = ot.ProcessSample(timeGrid, 4, dimension)
psample1.setName("PSample1")
print("Constructor based on size, dimension and timeGrid")
print("psample1=", psample1)

# change the first component using operator []
psample1[0] = timeSeries
print("changing psample1[0] with []")
print("psample1[0]=", (psample1[0]))
示例#5
0
def test_ProcessHighDensityRegionAlgorithm(mock_show):
    ot.RandomGenerator.SetSeed(0)
    numberOfPointsForSampling = 500
    ot.ResourceMap.Set('Distribution-MinimumVolumeLevelSetBySampling', 'true')
    ot.ResourceMap.Set('Distribution-MinimumVolumeLevelSetSamplingSize',
                       str(numberOfPointsForSampling))

    # Dataset
    fname = os.path.join(os.path.dirname(__file__), 'data', 'npfda-elnino.dat')
    data = np.loadtxt(fname)

    # Create the mesh
    n_nodes = data.shape[1]
    mesher = ot.IntervalMesher([n_nodes - 1])
    Interval = ot.Interval([0.0], [1.0])
    mesh = mesher.build(Interval)

    # Create the ProcessSample from the data
    n_fields = data.shape[0]
    dim_fields = 1
    sample = ot.ProcessSample(mesh, n_fields, dim_fields)
    for i in range(n_fields):
        trajectory = ot.Sample(data[i, :], 1)
        sample[i] = ot.Field(mesh, trajectory)

    # Compute HDRPlot
    hdr = ProcessHighDensityRegionAlgorithm(sample)
    hdr.setContoursAlpha([0.8, 0.5])
    hdr.setOutlierAlpha(0.8)
    hdr.run()
    hdr.summary()
    hdr.dimensionReductionSummary()

    # Plot ACP
    graph = hdr.drawDimensionReduction()
    View(graph)
    plt.show(graph)

    # Plot Density
    fig, axs, graphs = hdr.drawDensity()
    plt.show()

    # Plot outlier trajectories
    graph = hdr.drawOutlierTrajectories(drawInliers=True, discreteMean=True)
    View(graph)
    plt.show()

    graph = hdr.drawOutlierTrajectories(bounds=False)
    View(graph)
    plt.show()

    outlier_indices = hdr.computeOutlierIndices()
    expected_outlier_indices = [3, 7, 22, 32, 33, 41, 47]
    assert_equal(outlier_indices, expected_outlier_indices)

    # Check data
    assert_equal(hdr.getNumberOfTrajectories(), 54)
    assert_equal(hdr.getNumberOfVertices(), 12)
    assert_equal(hdr.getNumberOfComponents(), 2)
    assert_array_almost_equal(hdr.getPartOfExplainedVariance(), 0.86569783, 4)
    assert_array_almost_equal(hdr.getExplainedVarianceRatio(),
                              [0.60759627, 0.25810156], 4)

    # Check higher dimension
    hdr = ProcessHighDensityRegionAlgorithm(sample, numberOfComponents=3)
    hdr.setOutlierAlpha(0.6)
    hdr.run()

    fig, axs, graphs = hdr.drawDensity()
    plt.show()

    fig, axs, graphs = hdr.drawDensity(drawData=True)
    plt.show()
    def _convert_exec_sample_ot(self, output):
        """Converts the output of the batch function passed to the class into
        a basic openturns object, and makes some checks on the dimensions.

        Note
        ----
        If the checks fail, the output can still be found under self.__output_backup__
        """
        print(
            '''Using the batch evaluation function. Assumes that the outputs are in the
same order than for the single evaluation function. This one should only
return ProcessSamples, Samples, Lists or numpy arrays.''')
        outputList = []
        if len(output) != len(self._outputDescription):
            self.__nOutputs__ = len(output)
            self.setOutputDescription(
                ot.Description.BuildDefault(self.__nOutputs__, 'Y_'))
        for i, element in enumerate(output):
            if isinstance(element, (ot.Sample, ot.ProcessSample)):
                element.setName(self._outputDescription[i])
                outputList.append(element)
                print(
                    'Element {} of the output tuple returns elements of type {} of dimension {}'
                    .format(i, element.__class__.__name__,
                            element.getDimension()))
            elif isinstance(element, (Sequence, Iterable)):
                print(
                    'Element is iterable, assumes that first dimension is size of sample'
                )
                intermElem = CustomList(element)
                intermElem.recurse2list()
                shape = intermElem.shape
                dtype = intermElem.dtype
                print('Shape is {} and dtype is {}'.format(shape, dtype))
                sampleSize = shape[0]
                subSample = [
                    CustomList(intermElem[j]) for j in range(sampleSize)
                ]
                assert dtype is not None, 'If None the list is not homogenous'
                if isinstance(
                        dtype(),
                    (Complex, Integral, Real, Rational, Number, str)):
                    if len(shape) >= 2:
                        print(
                            'Element {} of the output tuple returns process samples of dimension {}'
                            .format(i,
                                    len(shape) - 1))
                        mesh = self._buildMesh(self._getGridShape(shape[1:]))
                        subSample = [
                            subSample[j].flatten() for j in range(sampleSize)
                        ]
                        procsample = ot.ProcessSample(mesh, 0, len(shape) - 1)
                        for j in range(sampleSize):
                            procsample.add(
                                ot.Field(mesh,
                                         [[elem]
                                          for elem in subSample[j].data]))
                        procsample.setName(self._outputDescription[i])
                        outputList.append(procsample)
                    elif len(shape) == 1:
                        print(
                            'Element {} of the output tuple returns samples of dimension {}'
                            .format(i, 1))
                        element = ot.Sample([[dat] for dat in intermElem.data])
                        element.setName(self._outputDescription[i])
                        outputList.append(element)
                else:
                    print('Do not use non-numerical dtypes in your objects')
                    print('Wrong dtype is: ', dtype.__name__)
            elif isinstance(element, ot.Point):
                print(
                    'Element {} of the output tuple returns samples of dimension 1'
                    .format(i,
                            type(element).__name__))
                element = ot.Sample([[element[j]]
                                     for j in range(len(element))])
                element.setName(self._outputDescription[i])
                outputList.append(element)
            elif isinstance(element, ot.Field):
                print(
                    'ONLY _exec_sample FUNCTION MUST RETURN ot.Sample OR ot.ProcessSample OBJECTS!!'
                )
                raise TypeError
            else:
                print('Element is {} of type {}'.format(
                    element, element.__class__.__name__))
                raise NotImplementedError
        return outputList
    def draw(self,
             drawInliers=False,
             drawOutliers=True,
             discreteMean=False,
             bounds=True):
        """
        Plot outlier trajectories based on HDR.

        Parameters
        ----------
        drawInliers : bool
            If True, plots the inlier curves.
        drawOutliers : bool
            If True, draw the outliers curves.
        discreteMean : bool
            If False, the central curve is the curve in the process sample
            which has highest density.
            If True, the central curve is the mean of the process sample.
        bounds : bool
            If True, plots the bounds of the confidence interval.
            These bounds are made of the mininimum and maximum at
            each time.

        Returns
        -------
        graph : ot.Graph
            The plot of outlier trajectories.
        """
        outlierAlpha = self.getOutlierAlpha()
        graph = ot.Graph(
            r"Outliers at $\alpha$=%.2f" % (outlierAlpha),
            "",
            "",
            True,
            "topright",
        )

        # Get the mesh
        mesh = self.processSample.getMesh()
        t = np.ravel(mesh.getVertices())

        # Plot outlier trajectories
        outlier_indices = self.computeIndices()
        if len(outlier_indices) > 0:
            outlier_process_sample = ot.ProcessSample(mesh,
                                                      len(outlier_indices), 1)
            index = 0
            for i in outlier_indices:
                outlier_process_sample[index] = self.processSample[i]
                index += 1
            if drawOutliers:
                outlier_graph = outlier_process_sample.drawMarginal(0)
                outlier_graph.setColors([self.outlier_color])
                graph.add(outlier_graph)

        # Plot inlier trajectories
        inlier_indices = self.computeIndices(False)
        if len(inlier_indices):
            inlier_process_sample = ot.ProcessSample(mesh, len(inlier_indices),
                                                     1)
            index = 0
            for i in inlier_indices:
                inlier_process_sample[index] = self.processSample[i]
                index += 1
            if drawInliers:
                inlier_graph = inlier_process_sample.drawMarginal(0)
                inlier_graph.setColors([self.inlier_color])
                graph.add(inlier_graph)

        # Plot inlier bounds
        def fill_between(lower, upper, legend, color):
            """Draw a shaded area between two curves."""
            disc = len(lower)
            poly_data = [[lower[i], lower[i + 1], upper[i + 1], upper[i]]
                         for i in range(disc - 1)]
            polygon = [
                ot.Polygon(poly_data[i], color, color) for i in range(disc - 1)
            ]
            bounds_poly = ot.PolygonArray(polygon)
            bounds_poly.setLegend(legend)
            return bounds_poly

        if bounds:
            inlier_min = inlier_process_sample.computeQuantilePerComponent(0.0)
            inlier_max = inlier_process_sample.computeQuantilePerComponent(1.0)
            min_values = inlier_min.getValues()
            max_values = inlier_max.getValues()
            nbVertices = mesh.getVerticesNumber()
            lower_bound = [[t[i], min_values[i, 0]] for i in range(nbVertices)]
            upper_bound = [[t[i], max_values[i, 0]] for i in range(nbVertices)]

            outlierAlpha = np.max(self.alphaLevels)
            bounds = fill_between(
                lower_bound,
                upper_bound,
                r"Conf. interval at $\alpha$=%.2f" % (outlierAlpha),
                self.confidence_band_color,
            )
            graph.add(bounds)

        # Plot central curve
        if discreteMean:
            central_field = self.processSample.computeMean()
        else:
            mode_index = self.getMode()
            central_field = self.processSample[mode_index]

        curve = ot.Curve(t[:, None], central_field, "Central curve")
        curve.setColor(self.central_color)
        graph.add(curve)

        return graph
示例#8
0
mesh = ot.RegularGrid(0.0, 1.0, 4)
values = ot.Sample([[0.5], [1.5], [1.0], [-0.5]])
field = ot.Field(mesh, values)
evaluation = ot.P1LagrangeEvaluation(field)
x = [2.3]
y = evaluation(x)
print(y)
ott.assert_almost_equal(y, [0.55])

# Learning sample on meshD
mesher = ot.IntervalMesher([7, 7])
lowerbound = [-1.0, -1.0]
upperBound = [1, 1]
interval = ot.Interval(lowerbound, upperBound)
meshD = mesher.build(interval)
sample = ot.ProcessSample(meshD, 10, 1)
field = ot.Field(meshD, 1)
for k in range(sample.getSize()):
    field.setValues(ot.Normal().getSample(64))
    sample[k] = field
lagrange = ot.P1LagrangeEvaluation(sample)
# New mesh
mesh = ot.Mesh(
    ot.MonteCarloExperiment(ot.ComposedDistribution([ot.Uniform(-1, 1)] * 2),
                            200).generate())

point = mesh.getVertices()[0]
y = lagrange(point)
print(y)
index = lagrange.getEnclosingSimplexAlgorithm().query(point)
print(index)
def test_ProcessHighDensityRegionAlgorithm(mock_show):
    ot.RandomGenerator.SetSeed(0)
    numberOfPointsForSampling = 500
    ot.ResourceMap.Set('Distribution-MinimumVolumeLevelSetBySampling', 'true')
    ot.ResourceMap.Set('Distribution-MinimumVolumeLevelSetSamplingSize',
                       str(numberOfPointsForSampling))
    #
    fname = os.path.join(os.path.dirname(__file__), 'data', 'npfda-elnino.dat')
    dataR = np.loadtxt(fname)

    # Create the mesh
    numberOfNodes = dataR.shape[1]
    myMesher = ot.IntervalMesher([numberOfNodes - 1])
    myInterval = ot.Interval([0.0], [1.0])
    myMesh = myMesher.build(myInterval)

    # Create the ProcessSample from the data
    numberOfFields = dataR.shape[0]
    dimensionOfFields = 1
    myps = ot.ProcessSample(myMesh, numberOfFields, dimensionOfFields)
    for i in range(numberOfFields):
        thisTrajectory = ot.Sample(dataR[i, :], 1)
        myps[i] = ot.Field(myMesh, thisTrajectory)

    # Compute HDRPlot
    myhdrplot = ProcessHighDensityRegionAlgorithm(myps)
    myhdrplot.setContoursAlpha([0.8, 0.5])
    myhdrplot.setOutlierAlpha(0.8)
    myhdrplot.run()
    myhdrplot.summary()
    myhdrplot.dimensionReductionSummary()

    # Plot ACP
    myhdrplot.plotDimensionReduction()
    plt.show()

    # Plot Density
    plotData = True
    plotOutliers = True
    myhdrplot.plotDensity(plotData, plotOutliers)
    plt.show()

    # Plot trajectories
    myhdrplot.plotTrajectories()
    plt.show()

    # Plot outlier trajectories
    myhdrplot.plotOutlierTrajectories()
    plt.show()

    outlierIndices = myhdrplot.computeOutlierIndices()
    expected_outlierIndices = [3, 7, 22, 32, 33, 41, 47]
    assert_equal(outlierIndices, expected_outlierIndices)

    # Check data
    assert_equal(myhdrplot.getNumberOfTrajectories(), 54)
    assert_equal(myhdrplot.getNumberOfVertices(), 12)
    assert_equal(myhdrplot.getNumberOfComponents(), 2)
    assert_array_almost_equal(myhdrplot.getPartOfExplainedVariance(),
                              0.86569783, 4)
    assert_array_almost_equal(myhdrplot.getExplainedVarianceRatio(),
                              [0.60759627, 0.25810156], 4)
# First, define a regular 2-d mesh
discretization = [10, 5]
mesher = ot.IntervalMesher(discretization)
lowerBound = [0.0, 0.0]
upperBound = [2.0, 1.0]
interval = ot.Interval(lowerBound, upperBound)
mesh = mesher.build(interval)
mesh = ot.RegularGrid(0.0, 0.01, 100)
graph = mesh.draw()
view = viewer.View(graph)

# %%
# Allocate a process sample from a field
field = ot.Field()
sampleSize = 10
processSample = ot.ProcessSample(sampleSize, field)
#field.draw()

# %%
# Create a process sample as realizations of a process
amplitude = [1.0]
scale = [0.2] * 1
myCovModel = ot.ExponentialModel(scale, amplitude)
myProcess = ot.GaussianProcess(myCovModel, mesh)
processSample = myProcess.getSample(10)
#processSample

# %%
# draw the sample, without interpolation
graph = processSample.drawMarginal(0, False)
view = viewer.View(graph)