ParaMonte MATLAB 3.0.0
Parallel Monte Carlo and Machine Learning Library
See the latest version documentation.
readSample.m
Go to the documentation of this file.
1%> \brief
2%> Return a list of objects of superclass [pm.sampling.FileContentsSample](@ref FileContentsSample)
3%> containing the contents of a (set of) ParaMonte simulation output sample 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 sample 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 sample 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 sample 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 ``"_sample.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%>
32%> \return
33%> ``sampleList`` : The output MATLAB cell array of objects of superclass [pm.sampling.FileContentsSample](@ref FileContentsSample),
34%> each of which corresponds to the contents of a unique ParaMonte sampler sample file.<br>
35%> <ol>
36%> <li> If the input argument ``sampler`` is of type [pm.sampling.Sampler](@ref Sampler),
37%> then the output array elements are of type [pm.sampling.FileContentsSample](@ref FileContentsSample).<br>
38%> <li> If the input argument ``sampler`` is of type [pm.sampling.Paradram](@ref Paradram),
39%> then the output array elements are of type [pm.sampling.FileContentsSample](@ref FileContentsSample).<br>
40%> </ol>
41%>
42%> \interface{readSample}
43%> \code{.m}
44%>
45%> sampleList = pm.sampling.readSample();
46%> sampleList = pm.sampling.readSample([]);
47%> sampleList = pm.sampling.readSample([], []);
48%> sampleList = pm.sampling.readSample(pattern);
49%> sampleList = pm.sampling.readSample([], sep);
50%> sampleList = pm.sampling.readSample(pattern, sep);
51%>
52%> \endcode
53%>
54%> \note
55%> See the documentation of the subclasses of [pm.sampling.Sampler](@ref Sampler)
56%> (e.g., [pm.sampling.Paradram](@ref Paradram)) for example usage in action.<br>
57%>
58%> \example{readSample}
59%> \code{.m}
60%>
61%> sampler = pm.sampling.Sampler();
62%>
63%> sampleList = pm.sampling.readSample([], "./out/test_run_");
64%> sampleList = pm.sampling.readSample(sampler, "./out/test_run_");
65%>
66%> sampler.spec.outputFileName = "./out/test_run_";
67%>
68%> sampleList = pm.sampling.readSample(sampler, []);
69%> sampleList = pm.sampling.readSample(sampler);
70%>
71%> sampler.spec.outputSeparator = ",";
72%> pm.sampling.readSample(sampler, "./out/test_run_");
73%> pm.sampling.readSample(sampler, "./out/test_run_", []);
74%> sampleList = pm.sampling.readSample(sampler, "./out/test_run_", ",");
75%>
76%> \endcode
77%>
78%> \final{readSample}
79%>
80%> \author
81%> \AmirShahmoradi, Saturday Nov 2 2024, 11:11 PM, Dallas, TX.<br>
82%> \FatemehBagheri, May 20 2024, 1:25 PM, NASA Goddard Space Flight Center (GSFC), Washington, D.C.<br>
83function sampleList = readSample(sampler, pattern, sep)
84
85 if nargin < 3
86 if 0 < pm.array.len(sampler.spec.outputSeparator)
87 sep = string(sampler.spec.outputSeparator);
88 else
89 sep = [];
90 end
91 end
92
93 if nargin < 2
94 if 0 < pm.array.len(sampler.spec.outputFileName)
95 pattern = string(sampler.spec.outputFileName);
96 else
97 pattern = [];
98 end
99 end
100
101 if nargin < 1
102 sampler = [];
103 end
104 if isempty(sampler)
105 sampler = pm.sampling.Sampler();
106 end
107
108 pathList = sampler.findfile("sample", pattern);
109 sampleList = cell(numel(pathList), 1);
110
111 for ifile = numel(pathList) : -1 : 1
112
113 if ~sampler.silent
114 disp("processing file: """ + pathList(ifile) + """");
115 end
116
117 if isequal(class(sampler), "pm.sampling.Sampler")
118 sampleList{ifile} = pm.sampling.FileContentsSample(pathList(ifile), sampler.silent, sep);
119 elseif isequal(class(sampler), "pm.sampling.Paradram")
120 sampleList{ifile} = pm.sampling.FileContentsSampleDRAM(pathList(ifile), sampler.silent, sep);
121 else
122 help("pm.sampling.readSample")
123 disp("class(sampler)");
124 disp( class(sampler) );
125 error ( newline ...
126 + "Invalid input ``sampler`` object type." + newline ...
127 + "See the documentation displayed above for more " + newline ...
128 + "information on possible values for ``sampler`` argument." + newline ...
129 + newline ...
130 );
131 end
132
133 end
134
135end
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 sample/chain file genera...
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 readSample(in sampler, in pattern, in sep)
Return a list of objects of superclass pm.sampling.FileContentsSample containing the contents of a (s...
function which(in vendor)
Return the a MATLAB string containing the path to the first mpiexec executable binary found in system...