From de08ab6ccbd541574291c52f2982a1322e0acbce Mon Sep 17 00:00:00 2001 From: apetri Date: Wed, 20 Apr 2016 15:18:53 -0400 Subject: [PATCH] added random and invert methods to Ensemble --- lenstools/statistics/ensemble.py | 36 ++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/lenstools/statistics/ensemble.py b/lenstools/statistics/ensemble.py index e006495..b70f4c6 100644 --- a/lenstools/statistics/ensemble.py +++ b/lenstools/statistics/ensemble.py @@ -370,6 +370,28 @@ def save(self,filename,format=None,**kwargs): else: format(self,filename,**kwargs) + ############################################################# + ######Construct a random ensemble for testing purposes####### + ############################################################# + + @classmethod + def random(cls,nobs=10,columns=list("abc")): + + """ + Construct a random ensemble for testing purposes, sampling from a univariate normal distribution in each column + + :param nobs: number of observations (rows) + :type nobs: int. + + :param columns: columns of the ensemble + :type columns: list. + + :rtype: :py:class:`Ensemble` + + """ + + return cls(np.random.randn(nobs,len(columns)),columns=columns) + ################################################# #############Construct from meshgrid############# @@ -827,6 +849,20 @@ def shuffle(self,seed=None): ##################################################################################################################### + def invert(self): + + """ + If the Ensemble is a square matrix, compute its inverse + + :rtype: :py:class:`Ensemble` + + """ + + if self.shape[0]!=self.shape[1]: + raise ValueError("This is not a square matrix!") + + return self.__class__(np.linalg.inv(self.values),index=self.index,columns=self.columns) + #################################### #############Visualization########## ####################################