ParaMonte MATLAB 3.0.0
Parallel Monte Carlo and Machine Learning Library
See the latest version documentation.
FileContentsRestartDRAM.m
Go to the documentation of this file.
1%> \brief
2%> This is the base class for generating objects
3%> that contain the contents of a restart file
4%> generated by a ParaMonte sampler.<br>
5%>
6%> \details
7%> This class is meant to be primarily internally
8%> used by the ParaMonte MATLAB library samplers.<br>
9%>
10%> \warning
11%> The attributes of this class are considered
12%> internal implementation of the ParaMonte library.<br>
13%> As such, the interface of this class may change over time.<br>
14%>
15%> \brief
16%> See the documentation of the class constructor.<br>
17%>
18%> \note
19%> See below for information on the attributes (properties).<br>
20%>
21%> \note
22%> See below for information on the methods.<br>
23%>
25%>
26%> \author
27%> \JoshuaOsborne, May 21 2024, 3:26 AM, University of Texas at Arlington<br>
28%> \FatemehBagheri, May 20 2024, 1:25 PM, NASA Goddard Space Flight Center (GSFC), Washington, D.C.<br>
29%> \AmirShahmoradi, May 16 2016, 9:03 AM, Oden Institute for Computational Engineering and Sciences (ICES), UT Austin<br>
30classdef FileContentsRestartDRAM < pm.sampling.FileContentsRestart
31
32 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
33
34 properties(Access = public)
35 %>
36 %> ``meanAcceptanceRateSinceStart``
37 %>
38 %> The real-valued MATLAB array of rank ``1`` of
39 %> shape ``(1:self.count)`` containing the set of
40 %> average MCMC acceptance rates of the sampler proposal
41 %> distribution over the course of the simulation.<br>
42 %>
43 meanAcceptanceRateSinceStart = [];
44 %>
45 %> ``proposalAdaptiveScaleSq``
46 %>
47 %> The real-valued MATLAB array of rank ``1`` of
48 %> shape ``(1:self.count)`` containing the set of
49 %> adaptive squared scale factors of the sampler proposal
50 %> distribution over the course of the simulation.<br>
51 %>
52 proposalAdaptiveScaleSq = [];
53 %>
54 %> ``proposalCor``
55 %>
56 %> The real-valued MATLAB array of rank ``3`` of
57 %> shape ``(self.ndim, self.ndim, self.count)`` containing
58 %> the set of correlation matrices of the proposal distribution
59 %> of the sampler, representing the evolution of the proposal
60 %> correlation matrix over the course of the simulation.<br>
61 %>
62 proposalCor = [];
63 %>
64 %> ``proposalCov``
65 %>
66 %> The real-valued MATLAB array of rank ``3`` of
67 %> shape ``(1:self.ndim, 1:self.ndim, 1:self.count)`` containing
68 %> the set of covariance matrices of the proposal distribution
69 %> of the sampler, representing the evolution of the proposal
70 %> covariance matrix over the course of the simulation.<br>
71 %>
72 proposalCov = [];
73 %>
74 %> ``proposalCovLogVol``
75 %>
76 %> The real-valued MATLAB array of rank ``1`` of
77 %> shape ``(1:self.count)`` containing the set of
78 %> ``log(determinant(covmat))`` of the proposal distribution
79 %> representing the evolution of mean of proposal distribution
80 %> over the course of the simulation.<br>
81 %>
82 proposalCovLogVol = [];
83 %>
84 %> ``proposalMean``
85 %>
86 %> The real-valued MATLAB array of rank ``2`` of
87 %> shape ``(1:self.ndim, 1:self.count)`` containing
88 %> the set of mean vectors of the proposal distribution
89 %> of the sampler, representing the evolution of the proposal
90 %> mean over the course of the simulation.<br>
91 %>
92 proposalMean = [];
93 %>
94 %> ``uniqueStateVisitCount``
95 %>
96 %> The scalar MATLAB integer containing the number of
97 %> states uniquely visited within the domain of the
98 %> objective function up to the stage specified
99 %> within the specified restart file.<br>
100 %>
101 uniqueStateVisitCount = [];
102 %>
103 %> ``vis``
104 %>
105 %> The scalar MATLAB ``struct`` containing the set of
106 %> predefined visualizations for the restart data.<br>
107 %>
108 vis = [];
109 end
111 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
112
113 methods(Access = public)
114
115 %> \brief
116 %> Return a scalar object of class [pm.sampling.FileContentsRestartDRAM](@ref FileContentsRestartDRAM).<br>
117 %>
118 %> \details
119 %> This is the constructor of the class [pm.sampling.FileContentsRestartDRAM](@ref FileContentsRestartDRAM).<br>
120 %>
121 %> \param[in] file : The input scalar MATLAB string containing the path to an external report file.<br>
122 %> \param[in] silent : See the corresponding argument of [pm.sampling.FileContentsRestart](@ref FileContentsRestart) class.<br>
123 %> (**optional**. The default is set by [pm.sampling.FileContentsRestart](@ref FileContentsRestart).)
124 %>
125 %> \return
126 %> ``self`` : The output scalar object of class [pm.sampling.FileContentsRestartDRAM](@ref FileContentsRestartDRAM).<br>
127 %>
128 %> \interface{FileContentsRestartDRAM}
129 %> \code{.m}
130 %>
131 %> contents = pm.sampling.FileContentsRestartDRAM(file)
132 %> contents = pm.sampling.FileContentsRestartDRAM(file, [])
133 %> contents = pm.sampling.FileContentsRestartDRAM(file, silent)
134 %> contents = pm.sampling.FileContentsRestartDRAM(file, [], [])
135 %> contents = pm.sampling.FileContentsRestartDRAM(file, silent, [])
136 %> contents = pm.sampling.FileContentsRestartDRAM(file, silent, method)
137 %>
138 %> \endcode
139 %>
140 %> \example{FileContentsRestartDRAM}
141 %> \include{lineno} example/sampling/FileContentsRestartDRAM/main.m
143 %> <br><br>
144 %> \image html example/sampling/FileContentsRestartDRAM/FileContentsRestartDRAM.proposalCor.ellipse.12.png width=700
145 %> <br><br>
146 %> \image html example/sampling/FileContentsRestartDRAM/FileContentsRestartDRAM.proposalCov.ellipse.12.png width=700
147 %> <br><br>
148 %> \image html example/sampling/FileContentsRestartDRAM/FileContentsRestartDRAM.proposalCor.ellipse3.12.png width=700
149 %> <br><br>
150 %> \image html example/sampling/FileContentsRestartDRAM/FileContentsRestartDRAM.proposalCov.ellipse3.12.png width=700
151 %> <br><br>
152 %> \image html example/sampling/FileContentsRestartDRAM/FileContentsRestartDRAM.proposalCovLogVol.line.png width=700
153 %> <br><br>
154 %> \image html example/sampling/FileContentsRestartDRAM/FileContentsRestartDRAM.meanAcceptanceRateSinceStart.line.png width=700
155 %>
156 %> \final{FileContentsRestartDRAM}
157 %>
158 %> \author
159 %> \JoshuaOsborne, May 21 2024, 3:31 AM, University of Texas at Arlington<br>
160 %> \FatemehBagheri, May 20 2024, 1:25 PM, NASA Goddard Space Flight Center (GSFC), Washington, D.C.<br>
161 %> \AmirShahmoradi, May 16 2016, 9:03 AM, Oden Institute for Computational Engineering and Sciences (ICES), UT Austin<br>
162 function self = FileContentsRestartDRAM(file, silent)
163
164 if nargin < 2
165 silent = [];
166 end
167
168 self = self@pm.sampling.FileContentsRestart(file, silent, "ParaDRAM");
169
170 %%%%
171 %%%% Find the update count in the file.
172 %%%%
174 self.count = count(self.contents, 'uniqueStateVisitCount');
175
176 %%%%
177 %%%% Parse the restart file contents.
178 %%%%
179
180 cholupp = zeros(self.ndim, self.ndim);
181 self.meanAcceptanceRateSinceStart = zeros(self.count, 1);
182 self.proposalAdaptiveScaleSq = zeros(self.count, 1);
183 self.proposalCovLogVol = zeros(self.count, 1);
184 self.proposalMean = zeros(self.ndim, self.count);
185 self.proposalCov = zeros(self.ndim, self.ndim, self.count);
186 self.proposalCor = zeros(self.ndim, self.ndim, self.count);
187 self.uniqueStateVisitCount = zeros(self.count, 1);
188 skip = 10 + self.ndim * (self.ndim + 3) / 2;
189 icount = 0;
190 istart = 1;
191
192 while istart < length(self.lineList) - skip
193
194 if ~strcmp(self.lineList{self.ilast + istart}, self.lineList{self.ilast + istart + 2})
195 icount = icount + 1;
196 if ~self.silent && mod(icount, 10) == 0
197 self.spinner.spin(icount / self.count);
198 end
199 self.meanAcceptanceRateSinceStart (icount) = str2double(self.lineList(self.ilast + istart + 1));
200 self.uniqueStateVisitCount (icount) = str2double(self.lineList(self.ilast + istart + 3));
201 self.proposalAdaptiveScaleSq (icount) = str2double(self.lineList(self.ilast + istart + 5));
202 self.proposalCovLogVol (icount) = str2double(self.lineList(self.ilast + istart + 7));
203 istart = istart + 9;
204 iend = istart + self.ndim;
205 self.proposalMean(1 : self.ndim, icount) = str2double(self.lineList(self.ilast + istart : self.ilast + iend - 1));
206 for idim = 1 : self.ndim % covmat
207 istart = iend + 1;
208 iend = iend + idim;
209 cholupp(1 : idim, idim) = str2double(self.lineList(self.ilast + istart : self.ilast + iend)); % This is the upper Cholesky.
210 end
211 self.proposalCov(:, :, icount) = cholupp' * cholupp;
212 istart = iend + 1;
213 else
214 istart = istart + 2;
215 end
216
217 end
218
219 %%%%
220 %%%% Ensure the intrinsic ``corrcov`` is installed.
221 %%%%
222
223 try
224
225 for icount = 1 : size(self.proposalCov, 3)
226 self.proposalCor(:, :, icount) = corrcov(squeeze(self.proposalCov(:, :, icount)));
227 end
228
229 catch me
230
231 self.proposalCor = self.proposalCov;
232 warning ( newline ...
233 + string(me.identifier) + " : " + string(me.message) + newline ...
234 + "The component ``proposalCor`` will be set to the same values as the component ``proposalCov``." + newline ...
235 + newline ...
236 );
237
238 end
239
240 if ~self.silent
241 self.spinner.spin(1);
242 self.checkpoint([]);
243 end
244
245 %%%%
246 %%%% Add the restart visualizations.
247 %%%%
248
249 try
250
251 self.vis = struct();
252 self.vis.cascade = struct();
253 silent_kws = {"silent", self.silent};
254
255 self.checkpoint("adding restart data visualization tools for proposal evolution...");
256
257 dims = self.ndim * (self.ndim - 1) / 2;
258 dimx = zeros(dims, 1);
259 dimy = zeros(dims, 1);
260 counter = 1;
261 for ix = 1 : self.ndim - 1
262 for iy = 2 : self.ndim
263 dimx(counter) = ix;
264 dimy(counter) = iy;
265 counter = counter + 1;
266 end
267 end
268
269 for field = ["proposalCor", "proposalCov"]
270 self.vis.(field) = struct();
271 self.vis.(field).cascade = struct();
272 self.vis.(field).cascade.ellipse = pm.vis.CascadeEllipse( @()self.(field) ...
273 , @()self.proposalMean ...
274 , [] ... color data
275 , "dimx", dimx, "dimy", dimy ...
276 , "names", self.domainAxisName ...
277 , silent_kws{:} ...
278 );
279 self.vis.(field).cascade.ellipse3 = pm.vis.CascadeEllipse3 ( @()self.(field) ...
280 , @()self.proposalMean ...
281 , @()transpose(self.uniqueStateVisitCount) ...
282 , [] ... color data
283 , "axes", {"zscale", "log"} ...
284 , "dimx", dimx, "dimy", dimy ...
285 , "names", self.domainAxisName ...
286 , "zlabel", {"txt", "Unique State Visit Count"} ...
287 , silent_kws{:} ...
288 );
289 end
290
291 self.checkpoint("adding restart data visualization tools for other proposal properties...");
292
293 colnames = [ "uniqueStateVisitCount" ...
294 , "meanAcceptanceRateSinceStart" ...
295 , "proposalAdaptiveScaleSq" ...
296 , "proposalCovLogVol" ...
297 ];
298 %fullnames = [ "Unique State Visit Count" ...
299 % , "Mean Acceptance Rate Since Start" ...
300 % , "Proposal Adaptive Scale-Squared" ...
301 % , "Proposal Covriance Log-Volume" ...
302 % ];
303 cdf = array2table( [ self.(colnames(1))(:) ...
304 , self.(colnames(2))(:) ...
305 , self.(colnames(3))(:) ...
306 , self.(colnames(4))(:) ...
307 ]);
308 cdf.Properties.VariableNames = colnames;
309 self.vis.cascade.line = pm.vis.CascadeLine ( cdf ...
310 , "colx", colnames(1) ...
311 , "coly", colnames(2 : end) ...
312 , "axes", {"xscale", "log"} ...
313 , "colormap", {"enabled", false} ...
314 ..., "xlabel", {"txt", fullnames(1)} ...
315 ..., "ylabel", {"txt", fullnames(2 : end)} ...
316 , "plot", {"linewidth", 3} ...
317 , silent_kws{:} ...
318 );
319
320 self.checkpoint([]);
321
322 catch me
323
324 warning ( newline ...
325 + "Failed to create the visualizations for the restart data." + newline ...
326 + "Here is the error message:" + newline ...
327 + newline ...
328 + string(me.identifier) + newline + string(me.message) + newline ...
329 + newline ...
330 );
331
332 end
333
334 end % constructor
335
336 end % methods(Access = public)
337
338 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
339
340 methods(Access = public, Hidden)
341
342 function warn(self)
343 if ~self.silent
344 warning ( newline ...
345 + "The structure of the file:" + newline ...
346 + newline ...
347 + pm.io.tab + self.file + newline ...
348 + newline ...
349 + "does not match a DRAM-class restart file." + newline ...
350 + "The contents of the file may have been compromised." + newline ...
351 + "Verify the integrity of the file contents before attempting a reread." + newline ...
352 );
353 end
354 end
355
356 end % methods(Access = Hidden)
357
358 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
359
360end % classdef
This is the base class for generating objects that contain the contents of a restart file generated b...
function FileContentsRestartDRAM(in file, in silent)
Return a scalar object of class pm.sampling.FileContentsRestartDRAM.
function warn(in self)
This is the base class for generating objects that contain the contents of a restart file generated b...