:mod:`paramonte._ParaMonteSampler` ================================== .. py:module:: paramonte._ParaMonteSampler Module Contents --------------- Classes ~~~~~~~ .. autoapisummary:: paramonte._ParaMonteSampler.ParaMonteSampler .. data:: Struct .. data:: newline .. py:class:: ParaMonteSampler(methodName: str) This is the **ParaMonteSampler** base class for the ParaMonte sampler routines. This class is NOT meant to be directly accessed or called by the user of the ParaMonte library. However, its children, such as the ParaDRAM sampler class will be directly accessible to the public. **Parameters** methodName A string representing the name of the ParaMonte sampler that is to be instantiated. **Attributes** buildMode optional string argument with the default value "release". possible choices are: "debug" to be used for identifying sources of bug and causes of code crash. "release" to be used in all other normal scenarios for maximum runtime efficiency. mpiEnabled optional logical (boolean) indicator which is ``False`` by default. If it is set to ``True``, it will cause the ParaMonte simulation to run in parallel on the requested number of processors. See the class documentation guidelines in the above for information on how to run a simulation in parallel. reportEnabled optional logical (boolean) indicator which is ``True`` by default. If it is set to ``True``, it will cause extensive guidelines to be printed on the standard output as the simulation or post-processing continues with hints on the next possible steps that could be taken in the process. If you do not need such help and information set this variable to ``False`` to silence all output messages. inputFile optional string input representing the path to an external input namelist of simulation specifications. USE THIS OPTIONAL ARGUMENT WITH CAUTION AND ONLY IF YOU KNOW WHAT YOU ARE DOING. **WARNING** Specifying an input file will cause the sampler to ignore all other simulation specifications set by the user via sampler instance's `spec`-component attributes. spec A Python structure containing all simulation specifications. All simulation attributes are by default set to appropriate values at runtime. To override the default simulation specifications, set the ``spec`` attributes to some desired values of your choice. If you need help on any of the simulation specifications, try the supplied ``helpme()`` function in this component. If you wish to reset some specifications to the default values, simply set them to ``None``. **Methods** See below for information on the methods. **Returns** Object of class ParaMonteSampler. .. method:: _runSampler(self, ndim: int, getLogFuncRaw: tp.Callable[[int, tp.List[float]], float], inputFile: tp.Optional[str] = None) Run ParaMonte sampler and return nothing. This method is identical to the ``runSampler()`` method, except that the input ``point`` parameter to the user-provided input objective function ``getLogFuncRaw(ndim,point)`` is a C-style raw pointer. This requires the user to guarantee that ``point`` will be always used with array bounds in their implementation of the objective function. The use of ``_runSampler()`` in place of ``runSampler()`` might lead to a slight performance gain in the simulations, that is often negligible. **Example serial usage** Copy and paste the following code enclosed between the two comment lines in your python/ipython/jupyter session (ensure the indentations of the pasted lines comply with Python rules): .. code-block:: python :linenos: ################################## import paramonte as pm import numpy as np def getLogFuncRaw(ndim,point): # return the log of the standard multivariate # Normal density function with ndim dimensions return -0.5 * np.sum( np.double( point[0:ndim] )**2 ) pmpd = pm.ParaDRAM() pmpd._runSampler( ndim = 4 # length of point , getLogFuncRaw = getLogFuncRaw # the objective function ) ################################## where, ndim represents the number of dimensions of the domain of the user's objective function ``getLogFuncRaw(ndim, point)`` and, getLogFuncRaw(ndim, point) represents the user's objective function to be sampled, where, ndim is a 32-bit integer, representing the number of dimensions of the domain of the user-provided objective function. point is a C-style array-pointer of length ``ndim`` and type float64. Note that the bounds of ``point`` must be always specified wherever it is used within the objective function. On output, it must return the natural logarithm of the objective function. **Parameters** All input parameters have the same meaning as the parameters of ``runSampler()``. The only difference is in the input parameters to the objective function ``getLogFuncRaw``. **Returns** None .. method:: _getInputFile(self, inputFile) .. method:: _setFileToRead(self, file, fileType, fileSuffix) .. method:: _setDelimiterToRead(self, delimiter, fileType, fileSuffix) .. method:: _readTabular(self, file: str, fileType: str, delimiter: str, parseContents: bool, renabled: bool) Read the contents of the file(s) whose path is given by the input argument ``file``. This function is not to be directly accessible to and callable by the users of the ParaMonte library. **Parameters** file A string representing the path to the tabular file with the default value of ``None``. The path only needs to uniquely identify the simulation to which the tabular file belongs. For example, specifying ``"./mydir/mysim"`` as input will lead to a search for a file that begins with ``"mysim"`` and ends with the tabular file name's prefix, such as, ``"_sample.txt"``, inside the directory ``"./mydir/"``. If there are multiple files with such name, then all of them will be read and returned as a list. The path can be also a world wide web address. If this input argument is not provided by the user, the value of the object attribute ``outputFileName`` will be used instead. At least one of the two mentioned routes must provide the path to the tabular file otherwise, this method will break by calling ``sys.exit()``. fileType A string containing the type of the file to be parsed. Current options include but are not limited to: ``sample``, ``chain``, ``markovChain``, ``progress`` delimiter An input string representing the delimiter used in the output tabular file. If it is not provided as input argument, the value of the corresponding object attribute outputDelimiter will be used instead. If none of the two are available, the default comma delimiter "," will be assumed and used. parseContents If set to True, the contents of the file will be parsed and stored in a component of the object named ``contents``. The default value is ``True``. renabled If set to ``False``, the contents of the file(s) will be stored as a list in a (new) component of the object with a name that ends with the prefix ``List``. Otherwise, ``None`` will be the return value of the method. If set to ``True``, the reverse will done. The default value is ``False``. **Returns** List A Python list of ``TabularFileContents`` objects, each of which corresponds to the contents of a unique restart file. The contents of each object is dependent on the type of the file that has been parsed. .. method:: readSample(self, file: tp.Optional[str] = None, delimiter: tp.Optional[str] = None, parseContents: tp.Optional[bool] = True, renabled: tp.Optional[bool] = False) Return a list of the contents of a set of ParaDRAM output sample files whose names contain the user-provided input file. This method is to be only used for postprocessing of the output sample file(s) of an already finished ParaDRAM simulation. It is not meant to be called by all processes in parallel mode, although it is possible. **Parameters** file (optional) A string representing the path to the output file with the default value of ``None``. The path only needs to uniquely identify the simulation to which the output file belongs. For example, specifying ``"./mydir/mysim"`` as input will lead to a search for a file that begins with ``"mysim"`` and ends with ``"_sample.txt"`` inside the directory ``"./mydir/"``. If there are multiple files with such name, then all of them will be read and returned as a list. The path can be also a world wide web address. If this input argument is not provided by the user, the value of the object attribute outputFileName will be used instead. At least one of the two mentioned routes must provide the path to the output file otherwise, this method will break by calling ``sys.exit()``. delimiter (optional) Optional input string representing the delimiter used in the output output file. If it is not provided as input argument, the value of the corresponding output object's attribute ``outputDelimiter`` will be used instead. If none of the two are available, the default comma delimiter ``","`` will be assumed and used. parseContents (optional) If set to True, the contents of the file will be parsed and stored in a component of the object named ``contents``. The default value is ``True``. renabled (optional) If set to ``False``, the contents of the file(s) will be stored as a list in a (new) component of the ParaDRAM object named ``sampleList`` and ``None`` will be the return value of the method. If set to ``True``, the reverse will done. The default value is ``False``. **Returns** sampleList (optional) A Python list of ``TabularFileContents`` objects, each of which corresponds to the contents of a unique restart file. Each object has the following components: file The full absolute path to the output file. delimiter The delimiter used in the output file. ndim The number of dimensions of the domain of the objective function from which the output has been drawn. count The number of sampled points in the output file. plot A structure containing the graphics tools for the visualization of the contents of the file. df The contents of the output file in the form of a pandas-library DataFrame (hence called ``df``). contents corresponding to each column in the progress file, a property with the same name as the column header is also created for the object which contains the data stored in that column of the progress file. These properties are all stored in the attribute ``contents``. If ``renabled = True``, the list of objects will be returned as the return value of the method. Otherwise, the list will be stored in a component of the ParaDRAM object named ``sampleList``. .. method:: readChain(self, file: tp.Optional[str] = None, delimiter: tp.Optional[str] = None, parseContents: tp.Optional[bool] = True, renabled: tp.Optional[bool] = False) Return a list of the contents of a set of ParaDRAM output chain files whose names begin the user-provided input file. This method is to be only used for postprocessing of the output chain file(s) of an already finished ParaDRAM simulation. It is not meant to be called by all processes in parallel mode, although it is possible. **Parameters** file (optional) A string representing the path to the output file with the default value of ``None``. The path only needs to uniquely identify the simulation to which the output file belongs. For example, specifying ``"./mydir/mysim"`` as input will lead to a search for a file that begins with ``"mysim"`` and ends with ``"_chain.txt"`` inside the directory ``"./mydir/"``. If there are multiple files with such name, then all of them will be read and returned as a list. The path can be also a world wide web address. If this input argument is not provided by the user, the value of the object attribute outputFileName will be used instead. At least one of the two mentioned routes must provide the path to the output file otherwise, this method will break by calling ``sys.exit()``. delimiter (optional) Optional input string representing the delimiter used in the output output file. If it is not provided as input argument, the value of the corresponding output object's attribute ``outputDelimiter`` will be used instead. If none of the two are available, the default comma delimiter ``","`` will be assumed and used. parseContents (optional) If set to True, the contents of the file will be parsed and stored in a component of the object named ``contents``. The default value is ``True``. renabled (optional) If set to ``False``, the contents of the file(s) will be stored as a list in a (new) component of the ParaDRAM object named ``chainList`` and ``None`` will be the return value of the method. If set to ``True``, the reverse will done. The default value is ``False``. **Returns** chainList (optional) A Python list of ``TabularFileContents`` objects, each of which corresponds to the contents of a unique restart file. Each object has the following components: file The full absolute path to the output file. delimiter The delimiter used in the output file. ndim The number of dimensions of the domain of the objective function from which the output has been drawn. count The number of sampled points in the output file. plot A structure containing the graphics tools for the visualization of the contents of the file. df The contents of the output file in the form of a pandas-library DataFrame (hence called ``df``). contents corresponding to each column in the progress file, a property with the same name as the column header is also created for the object which contains the data stored in that column of the progress file. These properties are all stored in the attribute ``contents``. If ``renabled = True``, the list of objects will be returned as the return value of the method. Otherwise, the list will be stored in a component of the ParaDRAM object named ``sampleList``. .. method:: readProgress(self, file: tp.Optional[str] = None, delimiter: tp.Optional[str] = None, parseContents: tp.Optional[bool] = True, renabled: tp.Optional[bool] = False) Return a list of the contents of a set of ParaMonte output progress files whose names begin the user-provided input file. This method is to be only used for postprocessing of the output progress file(s) of an already finished ParaMonte simulation. It is not meant to be called by all processes in parallel mode, although it is possible. **Parameters** file (optional) A string representing the path to the output file with the default value of ``None``. The path only needs to uniquely identify the simulation to which the output file belongs. For example, specifying ``"./mydir/mysim"`` as input will lead to a search for a file that begins with ``"mysim"`` and ends with ``"_progress.txt"`` inside the directory ``"./mydir/"``. If there are multiple files with such name, then all of them will be read and returned as a list. The path can be also a world wide web address. If this input argument is not provided by the user, the value of the object attribute outputFileName will be used instead. At least one of the two mentioned routes must provide the path to the progress file otherwise, this method will break by calling ``sys.exit()``. delimiter (optional) Optional input string representing the delimiter used in the output progress file. If it is not provided as input argument, the value of the corresponding object attribute outputDelimiter will be used instead. If none of the two are available, the default comma delimiter ``","`` will be assumed and used. parseContents (optional) If set to True, the contents of the file will be parsed and stored in a component of the object named ``contents``. The default value is ``True``. renabled (optional) If set to False, the contents of the file(s) will be stored as a list in a (new) component of the sampler object named ``progressList`` and ``None`` will be the return value of the method. If set to ``True``, the reverse will be done. The default value is ``False``. **Returns** A list of objects, each of which has the following properties: file The full absolute path to the file. delimiter The delimiter used in the file. ncol The number of columns of the file. plot A structure containing the graphics tools for the visualization of the contents of the file. df the contents of the progress file in the form of a pandas-library DataFrame (hence called ``df``). contents corresponding to each column in the progress file, a property with the same name as the column header is also created for the object which contains the data stored in that column of the progress file. These properties are all stored in the attribute ``contents``. If ``renabled = True``, the list of objects will be returned as the return value of the method. Otherwise, the list will be stored in a component of the sampler object named ``progressList``. .. method:: readRestart(self, file: tp.Optional[str] = None, renabled: tp.Optional[bool] = False) Return a list of the contents of a set of the simulation(s) output restart files whose names begin the user-provided input file prefix, or as specified by the input simulation specification ``SAMPLER.spec.outputFileName``, where SAMPLER can be an instance of any one of the ParaMonte's sampler classes, such as ``ParaDRAM()``. **NOTE** Only restart output files in **ASCII format** can be read via this method. The binary restart files are NOT meant to be parsed via this method. To request for ASCII restart output files in simulations, set the input simulation specification .. code-block:: python SAMPLER.spec.restartFileFormat = "ascii", where ``SAMPLER`` can be an instance of any one of the ParaMonte's sampler classes, such as ``ParaDRAM()``. **WARNING** Avoid using this routine for very large long simulations. Reading the full restart file of a large-scale simulation problem can be extremely memory-intensive. **WARNING** This method is to be only used for post-processing of the output restart file(s) of an already finished simulation. It is NOT meant to be called by all processes in parallel mode, although it is possible. **Parameters** file (optional) A string representing the path to the restart file with the default value of []. The path only needs to uniquely identify the name of the simulation to which the restart file belongs. For example, specifying ``"./mydir/mysim"`` as input will lead to a search for a file that begins with ``"mysim"`` and ends with ``"_restart.txt"`` inside the directory ``"./mydir/"``. If there are multiple files with such name, then all of them will be read and returned as a list. If this input argument is not provided by the user, the value of the object's ``spec`` attribute ``outputFileName`` will be used instead. **WARNING** At least one of the two mentioned routes must provide the path to the restart file. Otherwise, this method will abort the program. Example usage: .. code-block:: python pmpd.readRestart("./out/test_run_") or, .. code-block:: python pmpd.spec.outputFileName = "./out/test_run_" pmpd.readRestart() Both of the above examples are equivalent. The latter is recommended as it is less confusing. renabled (optional) If set to ``False``, the contents of the file(s) will be stored as a list in a (new) component of the sampler object named ``restartList`` and ``None`` will be the return value of the method. If set to ``True``, the reverse will done. The default value is ``False``. **Returns** restartList (optional) A Python list of ``RestartFileContents`` objects, each of which corresponds to the contents of a unique restart file. Each object has the following components: file The full absolute path to the restart file. ndim The number of dimensions of the domain of the objective function for which the restart file was generated. count The number of restart writes to the file. plot A structure containing the graphics tools for the visualization of the contents of the file. df The contents of the restart file in the form of a ``panda``'s dataframe (``df`` stands for **DataFrame**). contents A structure whose components contain the information retrieved about each of the entities in the file. propNameList A list of entities names parsed from the restart file. If no output argument is provided, a ``restartList`` property will be added to the parent sampler-object to which the method ``readRestart()`` belongs. .. method:: readReport(self, file: tp.Optional[str] = None, renabled: tp.Optional[bool] = False) Return a list of the contents of a set of the simulation(s) output report files whose names begin the user-provided input file prefix, or as specified by the input simulation specification ``SAMPLER.spec.outputFileName``, where SAMPLER can be an instance of any one of the ParaMonte's sampler classes, such as ``ParaDRAM()``. **NOTE** This method is to be only used for post-processing of the output report file(s) of an already finished simulation. It is NOT meant to be called by all processes in parallel mode, although it is possible. **Parameters** file (optional) A string representing the path to the report file with the default value of []. The path only needs to uniquely identify the name of the simulation to which the report file belongs. For example, specifying ``"./mydir/mysim"`` as input will lead to a search for a file that begins with ``"mysim"`` and ends with ``"_report.txt"`` inside the directory ``"./mydir/"``. If there are multiple files with such name, then all of them will be read and returned as a list. If this input argument is not provided by the user, the value of the object's ``spec`` attribute ``outputFileName`` will be used instead. **WARNING** At least one of the two mentioned routes must provide the path to the report file. Otherwise, this method will abort the program. Example usage: .. code-block:: python pmpd.readReport("./out/test_run_") or, .. code-block:: python pmpd.spec.outputFileName = "./out/test_run_" pmpd.readReport() Both of the above examples are equivalent. The latter is recommended as it is less confusing. renabled (optional) If set to ``False``, the contents of the file(s) will be stored as a list in a (new) component of the object with a name that ends with the prefix ``List``. Otherwise, ``None`` will be the return value of the method. If set to ``True``, the reverse will done. The default value is ``False``. **Returns** reportList (optional) A Python list of ``ReportFileContents`` objects, each of which corresponds to the contents of a unique report file. Each object may have a dynamic list of the different sections of the output report file Each object may have the following components: file The full absolute path to the report file. contents The contents of the file in its entirely as a string. If no output argument is provided, a ``reportList`` property will be added to the parent sampler object to which the method ``readReport()`` belongs. .. method:: helpme(self, topic: tp.Optional[str] = '') Prints help on the input object. **Parameters** topic A string value that is the name of a component of the current sample object for which help is needed. For example: Example usage: .. code-block:: python pm.helpme("helpme") **Returns** None