ParaMonte MATLAB 3.0.0
Parallel Monte Carlo and Machine Learning Library
See the latest version documentation.
readChain.m
Go to the documentation of this file.
1%> \brief
2%> Return a list of objects of superclass [pm.sampling.FileContentsChain](@ref FileContentsChain)
3%> containing the contents of a (set of) ParaMonte simulation output chain file(s) whose paths match
4%> the specified input ``pattern`` or the simulation specification ``sampler.spec.outputFileName``.<br>
5%>
6%> \warning
7%> This function is to be only used for post-processing of the output chain file(s) of an already finished simulation.<br>
8%> Although possible, this method is NOT meant to be called by all processes in MPI-parallel simulations.<br>
9%>
10%> \param[in] sampler : The input object of superclass [pm.sampling.Sampler](@ref Sampler)
11%> whose type and properties determine the type of the output object(s).<br>
12%> (**optional**. If empty, it is set to [pm.sampling.Sampler()](@ref Sampler).)
13%> \param[in] pattern : The input scalar MATLAB string containing the pattern matching
14%> the desired chain file(s) whose contents is to be read.<br>
15%> The specified ``pattern`` only needs to partially identify
16%> the name of the simulation to which the chain file belongs.<br>
17%> For example, specifying ``"./mydir/mysim"`` as input will
18%> lead to a search for file(s) beginning with "mysim" and
19%> ending with ``"_chain.txt"`` inside the directory ``"./mydir/"``.<br>
20%> If there are multiple files matching in the input ``pattern``,
21%> then all such files will be read and returned as elements of a list.<br>
22%> If the specified pattern is a valid existing URL, the file will be
23%> downloaded as a temporary file to the local system, its contents
24%> shall be parsed and the file will be subsequently removed.<br>
25%> If the input ``pattern`` is empty, then the method will search
26%> for any possible candidate files with the appropriate suffix
27%> in the current working directory.<br>
28%> (**optional**. If empty, it is set to ``sampler.spec.outputFileName`` or if empty, it is set to ``"./"``.)
29%> \param[in] sep : The input MATLAB string containing the field separator used in the file(s).<br>
30%> (**optional**. If empty, it is set to ``sampler.spec.outputSeparator`` or if empty, it is automatically inferred.)
31%> \param[in] varargin : Any set of arguments that must be directly passed to the relevant file contents parser class.<br>
32%> (**optional**. default is empty, corresponding to no extra input arguments.)
33%>
34%> \return
35%> ``chainList`` : The output MATLAB cell array of objects of superclass [pm.sampling.FileContentsChain](@ref FileContentsChain),
36%> each of which corresponds to the contents of a unique ParaMonte sampler chain file.<br>
37%> <ol>
38%> <li> If the input argument ``sampler`` is of type [pm.sampling.Sampler](@ref Sampler),
39%> then the output array elements are of type [pm.sampling.FileContentsChain](@ref FileContentsChain).<br>
40%> <li> If the input argument ``sampler`` is of type [pm.sampling.Paradram](@ref Paradram),
41%> then the output array elements are of type [pm.sampling.FileContentsChainDRAM](@ref FileContentsChainDRAM).<br>
42%> </ol>
43%>
44%> \interface{readChain}
45%> \code{.m}
46%>
47%> chainList = pm.sampling.readChain();
48%> chainList = pm.sampling.readChain([]);
49%> chainList = pm.sampling.readChain([], []);
50%> chainList = pm.sampling.readChain([], [], []);
51%> chainList = pm.sampling.readChain([], pattern);
52%> chainList = pm.sampling.readChain([], [], sep);
53%> chainList = pm.sampling.readChain([], pattern, sep);
54%> chainList = pm.sampling.readChain(sampler, pattern, sep, varargin);
55%>
56%> \endcode
57%>
58%> \note
59%> See the documentation of the ParaMonte MATLAB samplers
60%> (e.g., [pm.sampling.Paradram](@ref Paradram)) for example usage.<br>
61%>
62%> \example{readChain}
63%> \code{.m}
64%>
65%> sampler = pm.sampling.Sampler();
66%>
67%> chainList = pm.sampling.readChain([], "./out/test_run_");
68%> chainList = pm.sampling.readChain(sampler, "./out/test_run_");
69%>
70%> sampler.spec.outputFileName = "./out/test_run_";
71%>
72%> chainList = pm.sampling.readChain(sampler, [], ",");
73%> chainList = pm.sampling.readChain(sampler);
74%>
75%> chainList = pm.sampling.readChain("./out/test_run_", ",");
76%>
77%> sampler.spec.outputSeparator = ",";
78%>
79%> chainList = pm.sampling.readChain(sampler, "./out/test_run_");
80%>
81%> sampler.spec.outputFileName = "./out/test_run_";
82%> sampler.spec.outputSeparator = ",";
83%>
84%> chainList = pm.sampling.readChain(sampler);
85%>
86%> \endcode
87%>
88%> \final{readChain}
89%>
90%> \author
91%> \AmirShahmoradi, Saturday Nov 2 2024, 10:32 PM, Dallas, TX.<br>
92%> \FatemehBagheri, May 20 2024, 1:25 PM, NASA Goddard Space Flight Center (GSFC), Washington, D.C.<br>
93function chainList = readChain(sampler, pattern, sep, varargin)
94
95 if nargin < 3
96 if 0 < pm.array.len(sampler.spec.outputSeparator)
97 sep = string(sampler.spec.outputSeparator);
98 else
99 sep = [];
100 end
101 end
102
103 if nargin < 2
104 if 0 < pm.array.len(sampler.spec.outputFileName)
105 pattern = string(sampler.spec.outputFileName);
106 else
107 pattern = [];
108 end
109 end
110
111 if nargin < 1
112 sampler = [];
113 end
114 if isempty(sampler)
115 sampler = pm.sampling.Sampler();
116 end
117
118 pathList = sampler.findfile("chain", pattern);
119 chainList = cell(numel(pathList), 1);
120
121 for ifile = numel(pathList) : -1 : 1
122
123 if ~sampler.silent
124 disp("processing file: """ + pathList(ifile) + """");
125 end
126
127 if isequal(class(sampler), "pm.sampling.Sampler")
128 chainList{ifile} = pm.sampling.FileContentsChain(pathList(ifile), sampler.silent, sep, varargin{:});
129 elseif isequal(class(sampler), "pm.sampling.Paradram")
130 chainList{ifile} = pm.sampling.FileContentsChainDRAM(pathList(ifile), sampler.silent, sep, varargin{:});
131 else
132 help("pm.sampling.readChain")
133 disp("class(sampler)");
134 disp( class(sampler) );
135 error ( newline ...
136 + "Invalid input ``sampler`` object type." + newline ...
137 + "See the documentation displayed above for more " + newline ...
138 + "information on possible values for ``sampler`` argument." + newline ...
139 + newline ...
140 );
141 end
142
143 end
144
145end
function name(in vendor)
Return the MPI library name as used in naming the ParaMonte MATLAB shared library directories.
function list()
Return a list of MATLAB strings containing the names of OS platforms supported by the ParaMonte MATLA...
This is the base class for generating objects that contain the contents of a chain file generated by ...
This is the base class for generating objects that contain the contents of a chain file generated by ...
This is the ParaDRAM class for generating instances of serial and parallel Delayed-Rejection Adaptive...
Definition: Paradram.m:18
This is the base class for the ParaMonte sampler routines.
Definition: Sampler.m:21
function parallel()
Return a scalar MATLAB logical that is true if and only if the current installation of MATLAB contain...
function readChain(in sampler, in pattern, in sep, in varargin)
Return a list of objects of superclass pm.sampling.FileContentsChain containing the contents of a (se...
function which(in vendor)
Return the a MATLAB string containing the path to the first mpiexec executable binary found in system...