KerMor  0.9
Model order reduction for nonlinear dynamical systems and nonlinear approximation
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
FileDataCollection.m
Go to the documentation of this file.
1 namespace data{
2 
3 
4 /* (Autoinserted by mtoc++)
5  * This source code has been filtered by the mtoc++ executable,
6  * which generates code that can be processed by the doxygen documentation tool.
7  *
8  * On the other hand, it can neither be interpreted by MATLAB, nor can it be compiled with a C++ compiler.
9  * Except for the comments, the function bodies of your M-file functions are untouched.
10  * Consequently, the FILTER_SOURCE_FILES doxygen switch (default in our Doxyfile.template) will produce
11  * attached source files that are highly readable by humans.
12  *
13  * Additionally, links in the doxygen generated documentation to the source code of functions and class members refer to
14  * the correct locations in the source code browser.
15  * However, the line numbers most likely do not correspond to the line numbers in the original MATLAB source files.
16  */
17 
19  :public data.FileData {
37  private:
38 
39  hm;
47  public:
48 
49  KeepFiles = false;
59  public:
60 
61  FileDataCollection(data_dir) {
62  if ~usejava(" jvm ")
63  error(" FileDataCollection cannot be used as java is not enabled. ");
64  end
65  if nargin < 1
66  data_dir = fullfile(KerMor.App.TempDirectory,...
67  sprintf(" temp_fdc_%s ",...
69  end
70  this = this@data.FileData(data_dir);
71  this.hm= java.util.HashMap;
72  this.clear;
73  }
84  function delete() {
85  if (~this.KeepFiles && ~this.isSaved) || this.hm.size == 0
86  this.clear;
87  end
88  /* Superclass delete removes the folder if empty. */
89  delete@data.FileData(this);
90  }
100  function structdata = getData(rowvec<double> keydata,varargin) {
101  key = Utils.getHash(keydata);
102  data = [];
103  if this.hm.containsKey(key)
104  try
105  data = load(this.getfile(this.hm.get(key)), varargin[:]);
106  catch ME
107  warning(" KerMor:FileDataCollection ",...
108  " Error loading a file from directory %s: %s ",...
109  this.DataDirectory,ME.message);
110  data = [];
111  end
112  else
113  /* "Backup" function. In case some data is stored and then the
114  * FileDataCollection is not saved, the existing trajectory files are recognized
115  * and loaded. */
116  file = fullfile(this.DataDirectory,[key " .mat "]);
117  if exist(file," file ") == 2
118  data = load(file,varargin[:]);
119  this.hm.put(key,[key " .mat "]);
120  end
121  end
122  }
137  function n = getCollectionSize() {
138  n = this.hm.size;
139  }
140 
141 
142  function structdata = getDataNr(nr,varargin) {
143  if nr > this.hm.size || nr < 1
144  error(" Invalid data position number: %d ",nr);
145  end
146  keys = this.hm.keySet.toArray(java_array(" java.lang.String ",1));
147  data = load(this.getfile(this.hm.get(keys(nr))), varargin[:]);
148  }
162  function res = hasData(keydata) {
163  key = Utils.getHash(keydata);
164  /* Check in hash first */
165  res = this.hm.containsKey(key);
166  /* Lazy check for file existence if not in hashmap but on disk */
167  res = res || exist(fullfile(this.DataDirectory,[key " .mat "])," file ") == 2;
168  }
169 
170 
171  function addData(keydata,data) {
172  key = Utils.getHash(keydata);
173  file = [key " .mat "];
174  this.hm.put(key,file);
175 
176  file = fullfile(this.DataDirectory,file);
177  try
178  fn = fieldnames(data);
179  save(file," -struct "," data ",fn[:]);
180  catch ME
181  this.hm.remove(key);
182  rethrow(ME);
183  end
184  }
185 
186 
187  function clear() {
188  ks = this.hm.values.iterator;
189  while ks.hasNext
190  try
191  file = this.getfile(ks.next);
192  catch ME
193  warning(" KerMor:data:FileDataCollection ",...
194  " Could not delete file '%s': %s\nPlease remove manually. ",...
195  file,ME.message);
196  end
197  delete(file);
198  end
199  this.hm.clear;
200  }
201 
202 
203  protected: /* ( Static ) */
204 
205  static function this = loadobj(this,initfrom) {
206  created = false;
207  if ~isa(this, " data.FileDataCollection ")
208  initfrom = this;
209  this = data.FileDataCollection(initfrom.DataDirectory);
210  created = true;
211  end
212  if nargin == 2 || created
213  this.hm= initfrom.hm;
214  this = loadobj@data.FileData(this, initfrom);
215  else
216  this = loadobj@data.FileData(this);
217  end
218  }
219 
220 
221  public: /* ( Static ) */
222 
223  static function res = test_FileDataCollection() {
224  res = true;
225  thedir = fullfile(KerMor.App.TempDirectory," testFDC ");
226  c = data.FileDataCollection(thedir);
227  num = 10;
228  keys = rand(num,1);
229 
230  for k=1:num
231  s.field= rand(200,300);
232  c.addData(keys(k),s);
233  end
234  clear c;
235 
236  if exist(thedir," file ") ~= 0
237  res = false;
238  end
239 
240  c = data.FileDataCollection(thedir);
241  for k=1:num
242  s.field= rand(500,500);
243  c.addData(keys(k),s);
244  end
245  save(fullfile(thedir," tmpsave.mat ")," c ");
246  clear c;
247  if exist(thedir," file ") ~= 7
248  res = false;
249  end
250  rmdir(thedir," s ");
251  }
261 };
262 }
263 
function res = hasData(keydata)
Collection of generally useful functions.
Definition: Utils.m:17
static function res = test_FileDataCollection()
Quickly tests if the directory is persisted(deleted) when (not) saved to a mat file.
static function gen = generateID()
Generates a new unique ID.
Definition: IDGenerator.m:90
function file = getfile(file)
Definition: FileData.m:162
static function this = loadobj(this, initfrom)
function struct data = getDataNr(nr, varargin)
Retrieves data from the nr-st collection element.
FileDataCollection(data_dir)
Creates a new FileDataCollection in a file folder.
A variable number of input arguments.
KeepFiles
Tells the FileDataCollection to keep any stored files if the instance is deleted but has not been pre...
fieldnames
Returns a cell array of string containing the names of public properties. See the MATLAB fieldnames f...
function n = getCollectionSize()
FileData: Base class for access of files stored in a specific folder in the local file system...
Definition: FileData.m:18
static function h = getHash(vec)
Returns a hash code for the given vector.
Definition: Utils.m:493
FileDataCollection: Basic class for storing data given a hashable key value.
char DataDirectory
The root folder where the FileData's files are saved.
Definition: FileData.m:56
function addData(keydata, data)
logical isSaved
This flag indicates that this FileData instance has been stored to disk via the save method somewhere...
Definition: FileData.m:72
Global configuration class for all KerMor run-time settings.
Definition: KerMor.m:17
Generates unique IDs.
Definition: IDGenerator.m:17
function struct data = getData(rowvec< double > keydata, varargin)
methods(Access=protected) Retrieves data from the collection element of a given key.
static function KerMor theinstance = App()
The singleton KerMor instance.
Definition: KerMor.m:910