def _inliers_outliers(self, sample=None, inliers=True): """Inliers or outliers cloud drawing. :param sample: Sample of size (n_samples, n_dims). :type sample: :class:`openturns.Sample` :param bool inliers: Whether to draw inliers or outliers. :return: OpenTURNS Cloud or Pair object if :attr:`dim` > 2. :rtype: :class:`openturns.Cloud` or :class:`openturns.Pairs` """ if inliers: idx = self.computeOutlierIndices(False) legend = "Inliers at alpha=%.4f" % (self.outlierAlpha) marker_color = 'blue' else: idx = self.computeOutlierIndices() legend = "Outliers at alpha=%.4f" % (self.outlierAlpha) marker_color = 'red' if sample is None: sample = np.array(self.sample) else: sample = np.asarray(sample) sample = sample[idx, :] if sample.size == 0: return if sample.shape[1] == 2: cloud = ot.Cloud(sample, marker_color, self.data_marker, legend) else: cloud = ot.Pairs(sample, '', self.sample.getDescription(), marker_color, self.data_marker) return cloud
def pairs(data, filename): print(" Draw pairs") print(" Distribution") g = ot.Graph() pairs_data = ot.Pairs(data) pairs_data.setPointStyle('dot') g.add(pairs_data) view = otv.View(g, (800, 800), square_axes=True) view.save(filename) print("Saving figure in {}".format(filename)) view.close() print(" Copula") g = ot.Graph() pairs_data = ot.Pairs((data.rank() + 0.5) / data.getSize()) pairs_data.setPointStyle('dot') g.add(pairs_data) view = otv.View(g, (800, 800), square_axes=True) view.save(filename) print("Saving figure in {}".format(filename)) view.close()
def drawDimensionReduction(self): """Pairdraw of the principal components. :return: OpenTURNS Graph object. :rtype: :class:`openturns.Graph` """ graph = ot.Graph('Reduced Space', '', '', True, 'topright') cloud = ot.Pairs(self.principalComponents) cloud.setLabels(self.principalComponents.getDescription()) graph.add(cloud) return graph
def pairs(data, filename): ''' Allows to plot the data for each pair of component random variable''' print(" Draw pairs") print(" Distribution") g = ot.Graph() pairs_data = ot.Pairs(data) pairs_data.setPointStyle('dot') g.add(pairs_data) view = otv.View(g, (800, 800), square_axes=True) view.save(filename) print("Saving figure in {}".format(filename)) view.close() print(" Copula") g = ot.Graph() pairs_data = ot.Pairs((data.rank() + 0.5) / data.getSize()) pairs_data.setPointStyle('dot') g.add(pairs_data) view = otv.View(g, (800, 800), square_axes=True) view.save( filename.parent.joinpath(filename.stem + '_copula' + filename.suffix)) print("Saving figure in {}".format( filename.parent.joinpath(filename.stem + '_copula' + filename.suffix))) view.close()
import openturns as ot from openturns.viewer import View # Factorial d = ot.Factorial([1.5, 2.5, 3.5], [1, 2, 3]) s = d.generate() s.setDescription(["X1", "X2", "X3"]) g = ot.Graph() g.setTitle("Factorial experiment") g.setGridColor("black") p = ot.Pairs(s) g.add(p) View(g)
# Create a Funky distribution corr = ot.CorrelationMatrix(3) corr[0, 1] = 0.2 corr[1, 2] = -0.3 copula = ot.NormalCopula(corr) x1 = ot.Normal(-1., 1) x2 = ot.Normal(2, 1) x3 = ot.Normal(-2, 1) x_funk = ot.ComposedDistribution([x1, x2, x3], copula) # Create a Punk distribution x1 = ot.Normal(1., 1) x2 = ot.Normal(-2, 1) x3 = ot.Normal(3, 1) x_punk = ot.ComposedDistribution([x1, x2, x3], copula) # Merge the distributions distribution = ot.Mixture([x_funk, x_punk], [0.5, 1.]) # Sample from the mixture n = 500 sample = distribution.getSample(n) myGraph = ot.Graph('Sample n=%d' % (n), ' ', ' ', True, '') myPairs = ot.Pairs(sample, 'Pairs', sample.getDescription(), 'blue', 'bullet') myGraph.add(myPairs) View(myGraph) sample.exportToCSVFile("gauss-mixture-3D.csv")
# view.save('curve8.png') view.show() # Pairs dim = 5 meanPoint = ot.NumericalPoint(dim, 0.0) sigma = ot.NumericalPoint(dim, 1.0) R = ot.CorrelationMatrix(dim) for i in range(dim): meanPoint[i] = (i + 1) * dim distribution = ot.Normal(meanPoint, sigma, R) size = 1000 sample = distribution.getSample(size) graph = ot.Graph('Pairs', ' ', ' ', True, 'topright') labels = list(['x' + str(i) for i in range(dim)]) myPairs = ot.Pairs(sample, 'Pairs example', labels, 'green', 'bullet') graph.add(myPairs) # graph.draw('curve9.png') view = View(graph) # view.save('curve9.png') view.show(block=False) # Convergence graph curve aCollection = [] aCollection.append(ot.LogNormal(300., 30., 0., ot.LogNormal.MUSIGMA)) aCollection.append(ot.Normal(75e3, 5e3)) myDistribution = ot.ComposedDistribution(aCollection) vect = ot.RandomVector(myDistribution) LimitState = ot.NumericalMathFunction(('R', 'F'), ('G', ), ('R-F/(_pi*100.0)', )) G = ot.RandomVector(LimitState, vect)
# Create a Funky distribution corr = ot.CorrelationMatrix(3) corr[0, 1] = 0.2 corr[1, 2] = -0.3 copula = ot.NormalCopula(corr) x1 = ot.Normal(-1.0, 1) x2 = ot.Normal(2, 1) x3 = ot.Normal(-2, 1) x_funk = ot.ComposedDistribution([x1, x2, x3], copula) # Create a Punk distribution x1 = ot.Normal(1.0, 1) x2 = ot.Normal(-2, 1) x3 = ot.Normal(3, 1) x_punk = ot.ComposedDistribution([x1, x2, x3], copula) # Merge the distributions distribution = ot.Mixture([x_funk, x_punk], [0.5, 1.0]) # Sample from the mixture n = 500 sample = distribution.getSample(n) myGraph = ot.Graph("Sample n=%d" % (n), " ", " ", True, "") myPairs = ot.Pairs(sample, "Pairs", sample.getDescription(), "blue", "bullet") myGraph.add(myPairs) View(myGraph) sample.exportToCSVFile("gauss-mixture-3D.csv")
sampleLearn.exportToCSVFile("samplelearn.csv", ",") sample.exportToCSVFile("sample.csv", ",") print("cbn sample=", sample) logL = 0.0 pdfSample = cbn.computePDF(sample) pdfSample.exportToCSVFile("pdfSample.csv", ",") for i in range(size): logL += m.log(pdfSample(i, 0)) logL /= size print("log-l=", logL) copula = ot.NormalCopulaFactory().buildAsNormalCopula(sampleLearn) print("copula=", copula) print("log-l=", copula.computeLogPDF(sample).computeMean()[0]) gr = ot.Graph() pairs = ot.Pairs(sample) pairs.setPointStyle("dot") gr.add(pairs) import openturns.viewer as otv view = otv.View(gr, (800, 820), square_axes=True) view.save("pairs.png") view.close() gr = ot.Graph() pairs = ot.Pairs(copula.getSample(size)) pairs.setPointStyle("dot") gr.add(pairs) view = otv.View(gr, (800, 820), square_axes=True) view.save("pairsCopula.png") view.close()
def functional_scatter_matrix(x, y, operator, color="blue", q_levels=None, values=None, colors=None): def _highlight_plot(view, x_array, y_array, operator, y_values, colors): # Find corresponding indices if colors is None or len(colors) != len(y_values): from matplotlib.colors import cnames lcolors = list(cnames.keys()) i = ot.RandomGenerator.IntegerGenerate(len(y_values), len(lcolors)) colors = [lcolors[_] for _ in i] dim = len(x_array[0]) for k, val in enumerate(y_values): I = np.array([operator(_[0], val) for _ in y_array]) indices = np.where(I)[0] if len(indices) > 0: for i in range(dim): x = x_array[:, i] z = y_array[:, 0] index_i_i = 1 + i * dim + i view._ax[index_i_i].plot(x[indices], z[indices], '.', color=colors[k]) for j in range(i + 1, dim): y = x_array[:, j] index_i_j = 1 + i * dim + j index_j_i = 1 + j * dim + i view._ax[index_i_j].plot(x[indices], y[indices], '.', color=colors[k]) view._ax[index_j_i].plot(y[indices], x[indices], '.', color=colors[k]) return view # Functional scatter matrix X = ot.NumericalSample(x) dim = X.getDimension() X.setDescription([""] * dim) Y = ot.NumericalSample(y) assert (len(X) == len(Y)) assert Y.getDimension() == 1 if q_levels is not None and values is not None: raise ValueError("Expected q_Levels or values or no one") # Start the graphs p = ot.Pairs(X) p.setColor(color) g = ot.Graph() g.add(p) view = View(g) for i in range(dim): index_i_i = 1 + i * dim + i view._ax[index_i_i] = view._fig.add_subplot(dim, dim, index_i_i) view._ax[index_i_i].plot(X[:, i], Y, '.', color=color) # Now we get the view if q_levels is not None: # check q_level type if type(q_levels) is float: values = list(Y.computeQuantile(q_levels)) else: values = [Y.computeQuantile(q)[0] for q in q_levels] return _highlight_plot(view, np.array(X), np.array(Y), operator, values, colors) elif values is not None: # check values if type(values) is float: values = [values] else: values = list(values) return _highlight_plot(view, np.array(X), np.array(Y), operator, values, colors) else: return view
isStationary = False graph = cov.draw(0, 0, tmin, tmax, 128, isStationary, asCorrelation) View(graph) # Sample of coefficients Xi sampleKsi = KLResult.project(outputSample) # Chaque marginale est reconstruite par noyau gaussien # False, 0, False: pas de binning (non aggergation des donnees dnas des segments), nbre de bins, pas d'effet de bord nbmodes = sampleKsi.getDimension() xi_marges = [ot.KernelSmoothing(ot.Normal(), False, 0, False).build(sampleKsi.getMarginal(i)) for i in range(nbmodes)] # graphes des pdf marginales for i in range(len(xi_marges)): monHisto = ot.HistogramFactory().build(sampleKsi.getMarginal(i)).drawPDF() monHisto.setColors(["blue"]) graph.add(monHisto) graph = xi_marges[i].drawPDF() graph.setTitle(r"Distribution of $\xi$" + str(i)) graph.setXTitle(r"$\xi$" + str(i)) graph.setYTitle("PDF") graph.add(monHisto) graph.setLegends(["KS","Histogram"]) View(graph) # graphe des pairs dans l'espace des rangs pairs = ot.Pairs(sampleKsi.rank()) pairs.setPointStyle("bullet") View(pairs)