Commit 680e0a6d8fe7525482151df18a3fd1d7bc7bdd16
1 parent
ccd27bcd
Fix rare numpy formatting in xyz files
Closing xyz files resets the numpy string function. If this occurs between two calls to write_sample(), this could screw up formatting. To prevent this, we set the string function at every call. This may incur in a slight overhead, but at least it is safe as long as we stay single thread.
Showing
1 changed file
with
18 additions
and
21 deletions
atooms/trajectory/xyz.py
... | ... | @@ -117,7 +117,6 @@ class TrajectoryXYZ(TrajectoryBase): |
117 | 117 | # Internal variables |
118 | 118 | self._cell = None |
119 | 119 | self._fields_float = True |
120 | - self._done_format_setup = False | |
121 | 120 | self._shortcuts = {'pos': 'position', |
122 | 121 | 'x': 'position[0]', |
123 | 122 | 'y': 'position[1]', |
... | ... | @@ -184,26 +183,24 @@ class TrajectoryXYZ(TrajectoryBase): |
184 | 183 | self.__cache_fields = copy(self.fields) |
185 | 184 | |
186 | 185 | def _setup_format(self): |
187 | - if not self._done_format_setup: | |
188 | - self._done_format_setup = True | |
189 | - # %g allows to format both float and int but it's 2x slower. | |
190 | - # This switch is for performance | |
191 | - if self._fields_float: | |
192 | - _fmt = '%.' + str(self.precision) + 'f' | |
193 | - else: | |
194 | - _fmt = '%g' | |
195 | - | |
196 | - def array_fmt(arr): | |
197 | - """Remove commas and [] from numpy array repr.""" | |
198 | - # Passing a scalar will trigger an error (gotcha: even | |
199 | - # when casting numpy array to list, the elements remain of | |
200 | - # numpy type and this function gets called! (4% slowdown) | |
201 | - try: | |
202 | - return ' '.join([_fmt % x for x in arr]) | |
203 | - except: | |
204 | - return _fmt % arr | |
205 | - # Note: numpy.array2string is MUCH slower | |
206 | - numpy.set_string_function(array_fmt, repr=False) | |
186 | + # %g allows to format both float and int but it's 2x slower. | |
187 | + # This switch is for performance | |
188 | + if self._fields_float: | |
189 | + _fmt = '%.' + str(self.precision) + 'f' | |
190 | + else: | |
191 | + _fmt = '%g' | |
192 | + | |
193 | + def array_fmt(arr): | |
194 | + """Remove commas and [] from numpy array repr.""" | |
195 | + # Passing a scalar will trigger an error (gotcha: even | |
196 | + # when casting numpy array to list, the elements remain of | |
197 | + # numpy type and this function gets called! (4% slowdown) | |
198 | + try: | |
199 | + return ' '.join([_fmt % x for x in arr]) | |
200 | + except: | |
201 | + return _fmt % arr | |
202 | + # Note: numpy.array2string is MUCH slower | |
203 | + numpy.set_string_function(array_fmt, repr=False) | |
207 | 204 | |
208 | 205 | def _setup_index(self): |
209 | 206 | """Sample indexing via tell / seek""" | ... | ... |