slippy.contact.OutputReader¶
- class slippy.contact.OutputReader(file_name)[source]¶
A class for reading and querying output files (.sdb) and array files (.sar)
- Parameters
file_name (str) – The path to the .sdb file with or without the extension, there should be a corresponding .sar file with the same name in the same directory
- fields¶
A set of all the fields which appear in the output database
- Type
set
- time_points¶
A list of the all the time points in the output file
- Type
list
See also
OutputSaver,OutputRequestNotes
All arrays in the output will be lazily read from the array file. These will not be read until the data from the array is requested. However, the lazy array objects are written so that numpy functions can use them as if they are arrays and they can be indexed as if they are arrays. Indexing the array will read the entire array into memory. For many uses this is good enough, however sometimes it is necessary to explicitly convert the lazy array into a numpy array using numpy.asarray(lazy_array).
Examples
Making an output file to test:
>>> import slippy.contact as c >>> with c.OutputSaver('test') as output_s: >>> output_s.write({'time':0,'a':5, 'b':np.array([1,2,3,4]), 'c':np.array([1,2,3,4])}) >>> output_s.write({'time':1,'a':10, 'c':np.array([1,2,3,4])}) >>> output_s.write({'time':2,'a':15, 'b':np.array([1,2,3,4]), }) >>> output_s.write({'time':3,'a':20, 'b':np.array([1,2,3,4]), 'c':np.array([1,2,3,4])})
The file can then be read in using an output reader:
>>> output = c.OutputReader('test')
We can find the time points and fields saved:
>>> output.time_points [0, 1, 2, 3] >>> output.fields {'a', 'b', 'c', 'time'}
The output reader can be indexed by a time point or a field:
>>> output[1] {'time': 1, 'a': 10, 'c': array, shape:(4,), dtype:int32} >>> output['a'] {0: 5, 1: 10, 2: 15, 3: 20}
The result of this index is a dictionary of the results from a single time point or all the results from a field, indexing again will give the result for a specific field at a specific time point.
>>> output['a'][1] == output[1]['a'] True
For plotting etc. all of the values can be accessed:
>>> all_times = list(output['time'].values())
You can also query the results data base directly using tinydb, note that this will not work with array items.
>>> from tinydb import Query >>> result = Query() >>> output.search(result.a == 10) [{'time': 1, 'a': 10, 'c': array, shape:(4,), dtype:int32}]
The output reader can also be use to iterate through the results from each time point:
>>> for result_from_time_point in output: >>> print(result_from_time_point['time']) 0 1 2 3
Methods
__init__(file_name)search(query)Wraps TinyDB search method replacing array codes in database with lazy arrays