3 Handles caching of star polymer results.
6 def __init__(self, run, writePath = None):
11 The run in question, which must be an instance of `Run`.
13 If not `None`, a path used to write data to.
18 if not isinstance(run, Run):
27 Returns the post-processing path for the given run.
29 @param[in] useWritePath
30 Set to `True` to use return a designated path to write data.
33 if useWritePath
and self.
_writePath is not None:
36 return self.
_run.getPath() +
"/StarPolymers/post-processing"
39 def storeData(self, filename, data, metadata):
41 Stores the given data and metadata in the given file name, relative to
42 the post-processing path.
49 if not os.path.exists(postProcessingPath):
51 os.makedirs(postProcessingPath)
53 filepath = postProcessingPath +
"/" + filename
55 with open(filepath +
".metadata",
"w")
as f:
57 yaml.safe_dump(metadata, f)
60 if isinstance(data, collections.OrderedDict):
63 raise Exception(
"Don't know how to treat `data` type")
66 def hasData(self, filename, metadata):
68 Returns whether there is cached data under `filename`, with metadata
69 matching the given `metadata` dictionary.
72 The name of the cached data to query.
74 The expected metadata.
82 if not os.path.isfile(filepath +
".metadata"):
84 if not os.path.isfile(filepath):
87 storedMetadata = yaml.safe_load(open(filepath +
".metadata"))
89 if not storedMetadata == metadata:
96 self, filename, metadata, keyInterpreter, valueInterpreter):
98 Returns the cached data under `filename`, with metadata matching the
99 given `metadata`, as an instance of `collections.OrderedDict`.
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.
107 Throws if `not self.hasData(filename, metadata)`.
110 The name of the cached data to query.
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
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
123 if not self.
hasData(filename, metadata):
129 ret = collections.OrderedDict()
130 with open(filepath,
"r")
as f:
132 key, value = line.split(
None, 1)
133 key = keyInterpreter(key)
134 value = valueInterpreter(value)
141 def _storeDataOrderedDict(self, data, stream):
143 Stores the given data in the given stream.
146 The data, in the form of a `collections.OrderedDict`.
148 The stream to write the data to.
152 assert isinstance(data, collections.OrderedDict)
154 for key, value
in data.items():
155 line = str(key) +
"\t" + str(value) +
"\n"