OpenMPCD
Cache.py
1 class Cache:
2  """
3  Handles caching of star polymer results.
4  """
5 
6  def __init__(self, run, writePath = None):
7  """
8  The constructor.
9 
10  @param[in] run
11  The run in question, which must be an instance of `Run`.
12  @param[in] writePath
13  If not `None`, a path used to write data to.
14  """
15 
16  from ..Run import Run
17 
18  if not isinstance(run, Run):
19  raise TypeError()
20 
21  self._run = run
22  self._writePath = writePath
23 
24 
25  def getPostProcessingPath(self, useWritePath = False):
26  """
27  Returns the post-processing path for the given run.
28 
29  @param[in] useWritePath
30  Set to `True` to use return a designated path to write data.
31  """
32 
33  if useWritePath and self._writePath is not None:
34  return self._writePath
35 
36  return self._run.getPath() + "/StarPolymers/post-processing"
37 
38 
39  def storeData(self, filename, data, metadata):
40  """
41  Stores the given data and metadata in the given file name, relative to
42  the post-processing path.
43  """
44 
45  import os.path
46 
47  postProcessingPath = self.getPostProcessingPath(True)
48 
49  if not os.path.exists(postProcessingPath):
50  import os
51  os.makedirs(postProcessingPath)
52 
53  filepath = postProcessingPath + "/" + filename
54 
55  with open(filepath + ".metadata", "w") as f:
56  import yaml
57  yaml.safe_dump(metadata, f)
58 
59  import collections
60  if isinstance(data, collections.OrderedDict):
61  self._storeDataOrderedDict(data, open(filepath, "w"))
62  else:
63  raise Exception("Don't know how to treat `data` type")
64 
65 
66  def hasData(self, filename, metadata):
67  """
68  Returns whether there is cached data under `filename`, with metadata
69  matching the given `metadata` dictionary.
70 
71  @param[in] filename
72  The name of the cached data to query.
73  @param[in] metadata
74  The expected metadata.
75  """
76 
77  import os.path
78  import yaml
79 
80  filepath = self.getPostProcessingPath() + "/" + filename
81 
82  if not os.path.isfile(filepath + ".metadata"):
83  return False
84  if not os.path.isfile(filepath):
85  return False
86 
87  storedMetadata = yaml.safe_load(open(filepath + ".metadata"))
88 
89  if not storedMetadata == metadata:
90  return False
91 
92  return True
93 
94 
96  self, filename, metadata, keyInterpreter, valueInterpreter):
97  """
98  Returns the cached data under `filename`, with metadata matching the
99  given `metadata`, as an instance of `collections.OrderedDict`.
100 
101  Each line of the stored file will be split at its first whitespace; the
102  part of the line left of that whitespace will be fed to `keyInterpreter`
103  to serve as that line's dictionary key, while the remainder of that line
104  will be fed to `valueInterpreter` to generate that key's value.
105 
106  @throw RuntimeError
107  Throws if `not self.hasData(filename, metadata)`.
108 
109  @param[in] filename
110  The name of the cached data to query.
111  @param[in] metadata
112  The expected metadata.
113  @param[in] keyInterpreter
114  A function that will be called on each data line's key, the
115  result of which will be used as the returned dictionary's
116  key for that line.
117  @param[in] valueInterpreter
118  A function that will be called on each data line's value, the
119  result of which will be used as the returned dictionary's
120  value for that line.
121  """
122 
123  if not self.hasData(filename, metadata):
124  raise RuntimeError()
125 
126  filepath = self.getPostProcessingPath() + "/" + filename
127 
128  import collections
129  ret = collections.OrderedDict()
130  with open(filepath, "r") as f:
131  for line in f:
132  key, value = line.split(None, 1)
133  key = keyInterpreter(key)
134  value = valueInterpreter(value)
135  ret[key] = value
136 
137  return ret
138 
139 
140 
141  def _storeDataOrderedDict(self, data, stream):
142  """
143  Stores the given data in the given stream.
144 
145  @param[in] data
146  The data, in the form of a `collections.OrderedDict`.
147  @param[in] stream
148  The stream to write the data to.
149  """
150 
151  import collections
152  assert isinstance(data, collections.OrderedDict)
153 
154  for key, value in data.items():
155  line = str(key) + "\t" + str(value) + "\n"
156  stream.write(line)
MPCDAnalysis.StarPolymersAnalysis.Cache.Cache.getPostProcessingPath
def getPostProcessingPath(self, useWritePath=False)
Definition: Cache.py:34
MPCDAnalysis.StarPolymersAnalysis.Cache.Cache._storeDataOrderedDict
def _storeDataOrderedDict(self, data, stream)
Definition: Cache.py:156
MPCDAnalysis.StarPolymersAnalysis.Cache.Cache.hasData
def hasData(self, filename, metadata)
Definition: Cache.py:80
MPCDAnalysis.StarPolymersAnalysis.Cache.Cache._run
_run
Definition: Cache.py:23
MPCDAnalysis.StarPolymersAnalysis.Cache.Cache.__init__
def __init__(self, run, writePath=None)
Definition: Cache.py:16
MPCDAnalysis.StarPolymersAnalysis.Cache.Cache._writePath
_writePath
Definition: Cache.py:24
MPCDAnalysis.StarPolymersAnalysis.Cache.Cache.storeData
def storeData(self, filename, data, metadata)
Definition: Cache.py:47
MPCDAnalysis.StarPolymersAnalysis.Cache.Cache.getDataOrderedDict
def getDataOrderedDict(self, filename, metadata, keyInterpreter, valueInterpreter)
Definition: Cache.py:126