Exemple #1
0
    def get(self):
        """

        .. py:method:: get(self)

        Computes the correlation matrix of the selected columns 
        of the input dataframe to the object's constructor.

            **Parameters**

                None

            **Returns**

                None. However, this method causes side-effects by manipulating 
                the existing attributes of the object.

        """

        if hasattr(self, "method"):
            self._isCorMat = True
            self._matrixType = "correlation"
            if self.method not in ["pearson", "kendall", "spearman"]:
                raise Exception(
                    "The requested correlation type must be one of the following string values,\n"
                    + "    pearson  : standard correlation coefficient\n" +
                    "    kendall  : Kendall Tau correlation coefficient\n" +
                    "    spearman : Spearman rank correlation.")
        else:
            self._isCorMat = False
            self._matrixType = "covariance"

        # check columns presence

        if self.columns is None:
            colnames = self._dfref().columns
            colindex = range(len(colnames))
        elif all(isinstance(element, str) for element in self.columns):
            colnames = self.columns
            colindex = dfutils.nam2num(self._dfref().columns, self.columns)
        elif all(isinstance(element, int) for element in self.columns):
            colindex = self.columns
            colnames = self._dfref().columns[colindex]
        else:
            raise Exception(
                "The input argument 'columns' must be a list whose elements are all\n"
                +
                "    1.   string-valued, each representing the name of the column from\n"
                +
                "         the input dataframe to the object's constructor, to be included\n"
                + "         in the " + self._matrixType +
                " matrix construction, or,\n" +
                "    2.   integer-valued, each representing the index of the column from\n"
                +
                "         the input dataframe to the object's constructor, to be included\n"
                + "         in the " + self._matrixType +
                " matrix construction.")

        # check rows presence

        if self.rows is None:
            rowindex = range(len(self._dfref().index))
        else:
            rowindex = self.rows

        # construct the matrix dataframe

        if self._isCorMat:
            self.df = self._dfref().iloc[rowindex,
                                         colindex].corr(method=self.method)
        else:
            self.df = self._dfref().iloc[rowindex, colindex].cov()

        # specify columns/index names

        self.df.columns = colnames
        self.df.index = colnames

        #!DEC$ ifdef PMVIS_ENABLED

        # add heatmap plot

        heatmap_kws = {}
        if self._isCorMat:

            annotPrecision = 2
            heatmap_kws["cbar_kws"] = {
                "label": self.method.capitalize() + "'s Correlation Strength",
                "orientation": "vertical",
                "ticks": _np.linspace(-1, 1, 9)
            }

            heatmap_kws["vmin"] = -1
            heatmap_kws["vmax"] = +1
            heatmap_kws["center"] = 0

        else:

            annotPrecision = None
            heatmap_kws["cbar_kws"] = {
                "label": "Covariance Strength",
                "orientation": "vertical"
            }

        from _HeatMapPlot import HeatMapPlot

        self.plot = _Struct()
        self.plot.heatmap = HeatMapPlot(dataFrame=self.df,
                                        heatmap_kws=heatmap_kws,
                                        annotPrecision=annotPrecision)
Exemple #2
0
    def get(self, reself: tp.Optional[bool] = False, **kwargs):
        """

        Compute the correlation / covariance matrix of the selected 
        columns of the input dataframe to the object's constructor.

            **Parameters**

                reself

                    A logical variable. If ``True``, an instance of 
                    the object will be returned  to the calling routine 
                    upon exit. The default value is ``False``.

            **Returns**

                The object self if ``reself = True`` otherwise, ``None``.

                **NOTE**

                This method causes side-effects by manipulating
                the existing attributes of the object.

        """

        for key in kwargs.keys():
            if hasattr(self, key):
                setattr(self, key, kwargs[key])
            elif key == "dataFrame":
                setattr(self, "_dfref", wref.ref(kwargs[key]))
            else:
                raise Exception("Unrecognized input '" + key +
                                "' class attribute detected." + newline +
                                self._getDocString())

        if hasattr(self, "method"):
            self._isCorMat = True
            self._matrixType = "correlation"
            if self.method not in ["pearson", "kendall", "spearman"]:
                raise Exception(
                    newline +
                    "The requested correlation type must be one of the following string values,\n"
                    + "    pearson  : standard correlation coefficient\n" +
                    "    kendall  : Kendall Tau correlation coefficient\n" +
                    "    spearman : Spearman rank correlation.")
        else:
            self._isCorMat = False
            self._matrixType = "covariance"

        ############################################################################################################################
        #### check columns presence
        ############################################################################################################################

        if self.columns is None:
            colnames = self._dfref().columns
            colindex = range(len(colnames))
        elif all(isinstance(element, str) for element in self.columns):
            colnames = self.columns
            colindex = dfutils.nam2num(self._dfref().columns, self.columns)
        elif all(isinstance(element, int) for element in self.columns):
            colindex = self.columns
            colnames = self._dfref().columns[colindex]
        else:
            raise Exception(
                newline +
                "The input argument 'columns' must be a list whose elements are all\n"
                +
                "    1.   string-valued, each representing the name of the column from\n"
                +
                "         the input dataframe to the object's constructor, to be included\n"
                + "         in the " + self._matrixType +
                " matrix construction, or,\n" +
                "    2.   integer-valued, each representing the index of the column from\n"
                +
                "         the input dataframe to the object's constructor, to be included\n"
                + "         in the " + self._matrixType +
                " matrix construction.")

        ############################################################################################################################
        #### check rows presence. This must be checked here, because it depends on the integrity of the in input dataFrame.
        ############################################################################################################################

        if self.rows is None: self.rows = range(len(self._dfref().index))

        ############################################################################################################################
        #### construct the matrix dataframe
        ############################################################################################################################

        if self._isCorMat:
            self.df = self._dfref().iloc[self.rows,
                                         colindex].corr(method=self.method)
        else:
            self.df = self._dfref().iloc[self.rows, colindex].cov()

        ############################################################################################################################
        #### specify columns/index names
        ############################################################################################################################

        self.df.columns = colnames
        self.df.index = colnames

        ############################################################################################################################
        #### graphics
        ############################################################################################################################

        self._plotTypeList = ["heatmap"]

        self._progress.note(msg="adding the " + self._matrixType +
                            " graphics tools... ",
                            end=newline,
                            pre=True)
        self.plot = Struct()
        self._resetPlot(resetType="hard")

        self.plot.reset = self._resetPlot

        ############################################################################################################################

        if reself: return self
Exemple #3
0
    def get(self):
        """

        .. py:method:: get(self)

        Computes the autocorrelations of the selected columns 
        of the input dataframe to the object's constructor.

            **Parameters**

                None

            **Returns**

                None. However, this method causes side-effects by manipulating 
                the existing attributes of the object.

        """

        # check columns presence

        if self.columns is None:
            colnames = self._dfref().columns
            colindex = range(len(colnames))
        elif all(isinstance(element, str) for element in self.columns):
            colnames = self.columns
            colindex = dfutils.nam2num( self._dfref().columns , self.columns )
        elif all(isinstance(element,int) for element in self.columns):
            colindex = self.columns
            colnames = self._dfref().columns[colindex]
        else:
            raise Exception ( "The input argument 'columns' must be a list whose elements are all\n"
                            + "    1.   string-valued, each representing the name of the column from\n"
                            + "         the input dataframe to the object's constructor, to be included\n"
                            + "         in the computation of autocorrelations, or,\n"
                            + "    2.   integer-valued, each representing the index of the column from\n"
                            + "         the input dataframe to the object's constructor, to be included\n"
                            + "         in the computation of autocorrelations."
                            )

        # check rows presence

        if self.rows is None:
            rowindex = range(len(self._dfref().index))
        else:
            rowindex = self.rows

        # compute the autocorrelations

        nvar = len(colnames)
        nlag = len(rowindex)
        acf = _np.zeros((nvar,nlag))

        from scipy.signal import correlate as _ccor
        for icnt, ivar in enumerate(colindex):
            xdata = self._dfref().iloc[rowindex,ivar].values.flatten() - _np.mean(self._dfref().iloc[rowindex,ivar].values.flatten())
            acf[icnt] = _ccor   ( xdata
                                , xdata
                                , mode = "full"
                                )[nlag-1:2*nlag]
            acf[icnt] = acf[icnt] / acf[icnt,0]
        self.df = _pd.DataFrame(_np.transpose(acf))

        # specify columns/index names

        colnames = [ "ACF_"+colnames[i] for i in range(len(colnames)) ]
        self.df.columns = colnames

        # add SampleLogFunc to df for plot coloring, if exists

        ccolumns = ()
        if "SampleLogFunc" in self._dfref().columns:
            ccolumns = "SampleLogFunc"
            self.df.insert  ( loc = 0
                            , column = ccolumns
                            , value = self._dfref()[[ccolumns]].values.flatten()
                            , allow_duplicates = True
                            )

        # add lags to df

        self.df.insert  ( loc = 0
                        , column = "Lag"
                        , value = [ i for i in self.df.index ]
                        )

#!DEC$ ifdef PMVIS_ENABLED

        #####################
        #### add plots
        #####################

        self.plot = _Struct()

        # add LinePlot

        from _LinePlot import LinePlot

        self.plot.line = LinePlot   ( dataFrame = self.df
                                    , xcolumns = "Lag"
                                    , ycolumns = colnames
                                    , ccolumns = None #ccolumns
                                    , lc_kws =  {
                                                #"linewidth":0.75,
                                                #"cmap":"viridis",
                                                "cmap":"autumn",
                                                #"alpha":0.5,
                                                }
                                    , colorbar_kws =    {
                                                        "extend":"neither",
                                                        "orientation":"vertical",
                                                        #"spacing":"uniform",
                                                        }
                                    #, legend_kws = None
                                    )

        # add ScatterPlot

        from _ScatterPlot import ScatterPlot

        self.plot.scatter = ScatterPlot ( dataFrame = self.df
                                        , xcolumns = "Lag"
                                        , ycolumns = colnames
                                        , ccolumns = None #ccolumns
                                        #, scatter_kws = {}
                                        , colorbar_kws =    {
                                                            "extend":"neither",
                                                            "orientation":"vertical",
                                                            #"spacing":"uniform",
                                                            }
                                        #, legend_kws = None
                                        )

    ################################################################################################################################

#!DEC$ endif