Beispiel #1
0
    def _reportWrongPlotName(self, plotNames):

        err.abort(
            msg="The input argument plotNames must be a string representing" +
            newline +
            "the name of a plot belonging to the AutoCorr class or," +
            newline + "a list of such plot names. You have entered: " +
            plotNames + newline + "Possible plots are: " + newline + newline +
            newline.join(self._plotTypeList) + newline + newline +
            "Here is the help for the ``reset()`` method: " + newline +
            newline + self._resetPlot.__doc__,
            marginTop=1,
            marginBot=1,
            methodName=self._methodName)
Beispiel #2
0
    def _resetPlot(self, resetType="soft", plotNames="all"):
        """

        Reset the properties of the plot to the original default settings.
        Use this method when you change many attributes of the plot and
        you want to clean up and go back to the default settings.

            **Parameters**

                resetType (optional)

                    An optional string with possible value of ``"hard"``.
                    If provided, the plot object will be regenerated from scratch.
                    This includes reading the original data frame again and resetting
                    everything. If not provided, then only the plot settings will be
                    reset without reseting the dataFrame.

                plotNames (optional)

                    An optional string value or list of string values representing 
                    the names of plots to reset. If no value is provided, 
                    then all plots will be reset.

            **Returns**

                None

            **Example**

                .. code-block:: python

                    reset("hard")                    # regenerate all plots from scratch
                    reset("hard","line")             # regenerate line plot from scratch
                    reset("hard",["line","scatter"]) # regenerate line and scatter plots

        """

        if isinstance(plotNames, str):
            plotTypeLower = plotNames.lower()
            if plotTypeLower == "all":
                requestedPlotTypeList = self._plotTypeList
            elif plotNames in self._plotTypeList:
                requestedPlotTypeList = [plotNames]
            else:
                requestedPlotTypeList = None  # non-essential, only to silence scrutinizer.
                self._reportWrongPlotName(plotNames)
        elif isinstance(plotNames, list):
            for plotName in plotNames:
                if plotName not in self._plotTypeList:
                    self._reportWrongPlotName(plotName)
        else:
            self._reportWrongPlotName("a none-string none-list object.")

        resetTypeIsHard = None
        if isinstance(resetType, str):
            resetTypeIsHard = resetType.lower() == "hard"
        else:
            err.abort(
                msg="The input argument resetType must be a string representing"
                + newline +
                "the type of the reset to be performed on the plots." +
                newline +
                "A list of possible plots includes: \"hard\", \"soft\"" +
                newline + "Here is the help for the ``reset()`` method: " +
                newline + newline + self._resetPlot.__doc__,
                marginTop=1,
                marginBot=1,
                methodName=self._methodName)

        ############################################################################################################################
        #### reset plots
        ############################################################################################################################

        for requestedPlotType in requestedPlotTypeList:

            plotObject = None
            requestedPlotTypeLower = requestedPlotType.lower()

            isLine = "line" in requestedPlotTypeLower
            isScatter = "scatter" in requestedPlotTypeLower

            isLineScatterPlot = isLine or isScatter

            if not resetTypeIsHard:
                plotComponent = getattr(self, "plot")
                plotObject = getattr(plotComponent, requestedPlotType)
                plotObject._reset()

            ########################################################################################################################
            #### reset line / scatter
            ########################################################################################################################

            if isLineScatterPlot:

                if resetTypeIsHard:
                    plotObject = LineScatterPlot(
                        plotType=requestedPlotType,
                        dataFrame=self.df,
                        methodName=self._methodName,
                        reportEnabled=self._reportEnabled,
                        resetPlot=self._resetPlot)

                plotObject.xcolumns = "Lag"
                plotObject.ycolumns = self.df.columns[2:]

                plotObject._xlabel = "AutoCorrelation Lag"
                plotObject._ylabel = "AutoCorrelation Function (ACF) Value"

                plotObject.ccolumns = None

                plotObject.colorbar.kws.extend = "neither"
                plotObject.colorbar.kws.orientation = "vertical"
                plotObject.colorbar.kws.spacing = "uniform"

                plotObject._xlimit = [1, None]
                plotObject._ylimit = [None, 1]
                plotObject._xscale = "log"

                plotObject.legend.enabled = True

                if isLine:
                    if isScatter:
                        plotObject.lineCollection.enabled = False
                        plotObject.plot.enabled = True
                        plotObject.plot.kws.alpha = 0.2
                        plotObject.plot.kws.color = "grey"
                        plotObject.plot.kws.linewidth = 0.75
                    else:
                        plotObject.plot.enabled = True
                        plotObject.plot.kws.linewidth = 1
                        plotObject.lineCollection.enabled = False

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

            if plotObject is not None:
                setattr(self.plot, requestedPlotType, plotObject)
Beispiel #3
0
    def _resetPlot(self, resetType="soft", plotNames="all"):
        """

        Reset the properties of the plot to the original default settings.
        Use this method when you change many attributes of the plot and
        you want to clean up and go back to the default settings.

            **Parameters**

                resetType (optional)

                    An optional string with possible value of ``"hard"``.
                    If provided, the plot object will be regenerated from scratch.
                    This includes reading the original data frame again and resetting
                    everything. If not provided, then only the plot settings will be
                    reset without reseting the dataFrame.

                plotNames (optional)

                    An optional string value or list of string values representing 
                    the names of plots to reset. If no value is provided, 
                    then all plots will be reset.

            **Returns**

                None

            **Example**

                .. code-block:: python

                    reset("hard")                    # regenerate all plots from scratch
                    reset("hard","heatmap")          # regenerate heatmap plot from scratch

        """

        if isinstance(plotNames, str):
            plotTypeLower = plotNames.lower()
            if plotTypeLower == "all":
                requestedPlotTypeList = self._plotTypeList
            elif plotNames in self._plotTypeList:
                requestedPlotTypeList = [plotNames]
            else:
                self._reportWrongPlotName(plotNames)
        elif isinstance(plotNames, list):
            for plotName in plotNames:
                if plotName not in self._plotTypeList:
                    self._reportWrongPlotName(plotName)
        else:
            self._reportWrongPlotName("a none-string none-list object.")

        resetTypeIsHard = None
        if isinstance(resetType, str):
            resetTypeIsHard = resetType.lower() == "hard"
        else:
            err.abort(
                msg="The input argument resetType must be a string representing"
                + newline +
                "the type of the reset to be performed on the plots." +
                newline +
                "A list of possible plots includes: \"hard\", \"soft\"" +
                newline + "Here is the help for the ``reset()`` method: " +
                newline + newline + self._resetPlot.__doc__,
                marginTop=1,
                marginBot=1,
                methodName=self._methodName)

        ############################################################################################################################
        #### reset plots
        ############################################################################################################################

        for requestedPlotType in requestedPlotTypeList:

            plotObject = None
            requestedPlotTypeLower = requestedPlotType.lower()

            isHeatmap = "heatmap" in requestedPlotTypeLower

            if not resetTypeIsHard:
                plotComponent = getattr(self, "plot")
                plotObject = getattr(plotComponent, requestedPlotType)
                plotObject._reset()

            ########################################################################################################################
            #### reset heatmap
            ########################################################################################################################

            if isHeatmap:

                if resetTypeIsHard:
                    plotObject = HeatMapPlot(plotType=requestedPlotType,
                                             dataFrame=self.df,
                                             methodName=self._methodName,
                                             reportEnabled=self._reportEnabled,
                                             resetPlot=self._resetPlot)

                if self._isCorMat:

                    plotObject.annotPrecision = 2
                    plotObject.heatmap.kws.cbar_kws = {
                        "label":
                        self.method.capitalize() + "'s Correlation Strength",
                        "orientation": "vertical",
                        "ticks": np.linspace(-1, 1, 9)
                    }

                    plotObject.heatmap.kws.vmin = -1
                    plotObject.heatmap.kws.vmax = +1
                    plotObject.heatmap.kws.center = 0

                else:

                    plotObject.annotPrecision = None
                    plotObject.heatmap.kws.cbar_kws = {
                        "label": "Covariance Strength",
                        "orientation": "vertical"
                    }

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

            if plotObject is not None:
                setattr(self.plot, requestedPlotType, plotObject)
Beispiel #4
0
def getFileList(file, fileType, methodName, _mpiDisabled):

    suffix = "_" + fileType + ".txt"
    if _os.path.isfile(
            file):  # check if the input path is a full path to a file
        FileList = [file]
        pattern = file
        if suffix != file[-len(suffix):]:
            _msg.warn(msg="The name of the input file: \n\n" + "    " + file +
                      "\n\n" + "does not end with the expected suffix '" +
                      suffix + "' for a " + fileType + " file type.\n",
                      methodName=methodName,
                      marginTop=1,
                      marginBot=1)
    elif _os.path.isdir(file):  # ensure the input path is not a directory
        _msg.abort(
            msg="file='" + file + "' cannot point to a directory.\n" +
            "Provide a string as the value of file that points to a unique " +
            fileType + " file or\n" +
            "to the unique name (including path) of the simulation name shared among its output files.\n",
            methodName=methodName,
            marginTop=1,
            marginBot=1)
    else:

        # search for files matching the input pattern

        import glob
        if file[-1:] == "*":
            pattern = file
        else:
            pattern = file + "*"  # + suffix

        _ = glob.glob(pattern)
        FileList = []
        for filename in _:
            if suffix in filename: FileList.append(filename)
        if len(FileList) == 0:
            _msg.abort(
                msg="Failed to detect any " + fileType +
                " files with the requested pattern: \n\n" + "    " + pattern +
                "\n\n" +
                "Provide a string, as the value of the input argument 'file', that either \n\n"
                + "    - points to one or more " + fileType +
                " files, or, \n" +
                "    - represents the unique name of a ParaMonte simulation. \n"
                +
                "      This unique-name is the common prefix in the names of \n"
                + "      the output files of a ParaMonte simulation.",
                methodName=methodName,
                marginTop=1,
                marginBot=1)
        else:
            pattern += suffix

    if _mpiDisabled:
        _msg.note(msg=str(len(FileList)) +
                  ' files detected matching the pattern: "' + pattern + '"',
                  methodName=methodName,
                  marginTop=0,
                  marginBot=0)
    return FileList
Beispiel #5
0
def getFileList(file, fileSuffix, methodName, reportEnabled = True):

    FileList = []
    iswebfile = False
    fullSuffix = "_" + fileSuffix + ".txt"

    if os.path.isfile(file):

        # check if the input path is a full path to a file

        FileList.append(file)
        pattern = file
        if fullSuffix != file[-len(fullSuffix):]:
            err.warn( msg   = "The name of the input file: \n\n"
                            + "    " + file + "\n\n"
                            + "does not end with the expected suffix '" + fullSuffix + "' for a " + fileSuffix + " file type.\n"
                    , methodName = methodName
                    , marginTop = 1
                    , marginBot = 1
                    )

    else:

        import glob

        pattern = "" # not really needed, but just in case...
        for i in [1,2]:

            if i==1:

                #### first search for files matching the input pattern, then for directory

                pattern = file if "*" in file else file + "*" # + fullSuffix

            elif i==2:

                #### then search for file as directory

                if os.path.isdir(file):

                    pattern = os.path.join(file, "*" + fullSuffix)

                    if reportEnabled:
                        err.warn( msg   = "file='" + file + "' points to a directory.\n"
                                        + "Now searching inside the folder for a " + fileSuffix + " file..."
                                , methodName = methodName
                                , marginTop = 1
                                , marginBot = 1
                                )

            #### now search for pattern

            for filePath in glob.glob(pattern):
                if filePath.endswith(fullSuffix):
                    FileList.append(filePath)

            if len(FileList)>0: break

        if not pattern.endswith(fullSuffix): pattern += fullSuffix

        if len(FileList)==0:

            #### one last try, search the web

            try:
                import urllib.request
                #filePath = getRandomFilePrefix(prefix = methodName + "_" + fileSuffix)
                localFilePath, headers = urllib.request.urlretrieve(url = file)
                FileList.append(localFilePath)
                iswebfile = True
            except:
                err.abort   ( msg   = "Failed to detect any " + fileSuffix + " files with the requested pattern: \n\n"
                                    + "    " + pattern + "\n\n"
                                    + "Provide a string, as the value of the input argument ``file``, that either \n\n"
                                    + "    - points to one or more " + fileSuffix + " files, or, \n"
                                    + "    - represents the unique name of a ParaMonte simulation. \n"
                                    + "      This unique-name is the common prefix in the names of \n"
                                    + "      the output files of a ParaMonte simulation.\n\n"
                                    + "Most importantly, ensure the requested file is in ASCII format.\n"
                                    + "The binary-format chain or restart output files cannot be parsed.\n"
                                    + "You can request ASCII-format output files by setting the\n"
                                    + "appropriate simulation specifications of the " + methodName + " sampler,\n\n"
                                    + "    spec.restartFileFormat = \"ascii\"\n"
                                    + "    spec.chainFileFormat = \"ascii\""
                            , methodName = methodName
                            , marginTop = 1
                            , marginBot = 1
                            )

        elif reportEnabled:

            err.note( msg = str(len(FileList)) + ' files detected matching the pattern: "' + pattern + '"'
                    , methodName = methodName
                    , marginTop = 1
                    , marginBot = 1
                    )

    return FileList, iswebfile
Beispiel #6
0
def getFileList(file, fileSuffix, methodName, reportEnabled):

    fullSuffix = "_" + fileSuffix + ".txt"

    if os.path.isfile(file):

        # check if the input path is a full path to a file

        FileList = [file]
        pattern = file
        if fullSuffix != file[-len(fullSuffix):]:
            err.warn( msg   = "The name of the input file: \n\n"
                            + "    " + file + "\n\n"
                            + "does not end with the expected suffix '" + fullSuffix + "' for a " + fileSuffix + " file type.\n"
                    , methodName = methodName
                    , marginTop = 1
                    , marginBot = 1
                    )

    else:

        if os.path.isdir(file):

            # ensure the input path is not a directory

            err.warn( msg   = "file='" + file + "' points to a directory.\n"
                            + "Now searching inside the folder for a " + fileSuffix + " file..."
                    , methodName = methodName
                    , marginTop = 1
                    , marginBot = 1
                    )
            pattern = os.path.join(file, "*" + fullSuffix)

        else:

            # search for files matching the input pattern

            if "*" in file: # file[-1:]=="*":
                pattern = file
            else:
                pattern = file + "*" # + fullSuffix

        import glob
        _ = glob.glob(pattern)

        FileList = []
        for filePath in _:
            if filePath.endswith(fullSuffix):
                FileList.append(filePath)

        if not pattern.endswith(fullSuffix): pattern += fullSuffix

        if len(FileList)==0:
            err.abort   ( msg   = "Failed to detect any " + fileSuffix + " files with the requested pattern: \n\n"
                                + "    " + pattern + "\n\n"
                                + "Provide a string, as the value of the input argument ``file``, that either \n\n"
                                + "    - points to one or more " + fileSuffix + " files, or, \n"
                                + "    - represents the unique name of a ParaMonte simulation. \n"
                                + "      This unique-name is the common prefix in the names of \n"
                                + "      the output files of a ParaMonte simulation.\n\n"
                                + "Most importantly, ensure the requested file is in ASCII format.\n"
                                + "The binary-format chain or restart output files cannot be parsed.\n"
                                + "You can request ASCII-format output files by setting the\n"
                                + "appropriate simulation specifications of the " + methodName + " sampler,\n\n"
                                + "    spec.restartFileFormat = \"ascii\"\n"
                                + "    spec.chainFileFormat = \"ascii\""
                        , methodName = methodName
                        , marginTop = 1
                        , marginBot = 1
                        )
        elif reportEnabled:
            err.note( msg = str(len(FileList)) + ' files detected matching the pattern: "' + pattern + '"'
                    , methodName = methodName
                    , marginTop = 1
                    , marginBot = 1
                    )

    return FileList