def delFile(file, desc="the requested file", methodName=""): try: import os os.remove(file) except: err.warn( msg = newline + "Failed to delete " + desc + " from the local disk. File may be protected. You can try manually deleting it: " + newline + newline + " " + file + newline + newline , methodName = methodName , marginTop = 1 , marginBot = 1 ) return None
except: pass cmd = None if platform.isWin32: cmd = "systeminfo > " + platform.systemInfoFilePath if platform.isLinux: cmd = "uname -a >> " + platform.systemInfoFilePath + "; lscpu >> " + platform.systemInfoFilePath if platform.isMacOS: cmd = "uname -a >> " + platform.systemInfoFilePath + "; sysctl -a | grep machdep.cpu >> " + platform.systemInfoFilePath if cmd is not None: err = os.system(cmd) if err != 0: platform.systemInfoFilePath = None warn(msg="Failed to get the system information. skipping...", methodName=names.paramonte, marginTop=1, marginBot=1) #################################################################################################################################### website = Struct() website.home = Struct() website.home.url = "https://www.cdslab.org/paramonte" website.home.install = Struct() website.home.install._url = website.home.url + "/notes/installation" # installation Linux website.home.overview = Struct() website.home.overview._url = website.home.url + "/notes/overview"
def get(self, reself: tp.Optional[bool] = False, **kwargs): """ Compute the autocorrelations of the selected columns of the input dataframe to the object's constructor. **Parameters** reself A logical variable. When ``True``, an instance of the object will be returned to the calling routine upon exit. The default value is ``False``. **Returns** The object 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()) ############################################################################################################################ #### check columns presence ############################################################################################################################ colnames, colindex = dfutils.getColNamesIndex(self._dfref().columns, self.columns) ############################################################################################################################ #### 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)) #rownames = self._dfref().index[self.rows] ############################################################################################################################ #### compute the autocorrelations ############################################################################################################################ self._nvar = len(colnames) self._nlag = len(self.rows) acf = np.zeros((self._nvar, self._nlag)) try: from scipy.signal import correlate for cnt, ivar in enumerate(colindex): xdata = self._dfref().iloc[ self.rows, ivar].values.flatten() - np.mean( self._dfref().iloc[self.rows, ivar].values.flatten()) acf[cnt] = correlate( xdata, xdata, mode="full")[self._nlag - 1:2 * self._nlag] acf[cnt] = acf[cnt] / acf[cnt, 0] except: if self._reportEnabled: err.warn( msg= "Failed to compute the Autocorrelation function of the input dataFrame." + newline + "This could likely be due to an error in importing the scipy Python library." + newline + "Please make sure you have the scipy library properly installed on your system." + newline + "You can do so by typing the following command on your Anaconda3 or Bash command prompt:" + newline + newline + " pip install --user --upgrade scipy", methodName=self._methodName, marginTop=1, marginBot=1) 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]) ############################################################################################################################ #### graphics ############################################################################################################################ self._plotTypeList = ["line", "scatter", "lineScatter"] self._progress.note(msg="adding the autocrrelation graphics tools... ", end=newline, pre=True) self.plot = Struct() self._resetPlot(resetType="hard") self.plot.reset = self._resetPlot ############################################################################################################################ if reself: return self
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
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
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