Last updated: 2023-03-16.
tf_quant_finance.experimental.io.ArrayDictWriter#
Writer to write dictionaries of numpy arrays in binary format.
tf_quant_finance.experimental.io.ArrayDictWriter(
path
)
Writes dictionaries with string keys and numpy array values as records into
a tfrecords file.
The usage of tfrecords should be treated as an implementation detail. To
read the file, use the ArrayDictReader class.
Notes and Limitations:#
Any values which are not numpy arrays will be first converted to an array before serializing.
Serializing strings or arrays of strings is complicated because numpy doesn’t support variable length strings. By default, a python string converted to a numpy array will be converted to a fixed size dtype (of the form ‘Un’ where n is the size of the largest string). During serialization this information is lost and the deserialization produces an object array with bytes elements. These need to be manually converted back to a unicode string using `object.astype(‘U?’) where ? is the length of the largest string in the array.
Example#
options_data = {
'instrument_type': 'EuropeanOption',
'strikes': np.array([1.0, 2.0, 3.0], dtype=np.float64),
'is_call': np.array([True, True, False]),
'expiries': np.array([0.4, 1.3, 2.3], dtype=np.float64)
}
barriers_data = {
'instrument_type': 'BarrierOption',
'strikes': np.array([1.0, 2.0, 3.0], dtype=np.float64),
'is_call': np.array([True, True, False]),
'expiries': np.array([0.4, 1.3, 2.3], dtype=np.float64),
'barrier': np.array([1.4, 2.5, 2.5], dtype=np.float64),
'is_knockout': np.array([True, True, False])
}
with ArrayDictWriter('datafile.bin') as writer:
writer.write(options_data)
writer.write(barriers_data)
Methods#
__enter__
__enter__()
__exit__
__exit__(
unused_type, unused_value, unused_traceback
)
Exits a with block and closes the file.
close
close()
Close the file.
flush
flush()
Flushes the file.
write
write(
array_dict
)
Writes a dictionary of arrays to the file.
Args:#
array_dict: A record to write. Should be a dictionary with string keys and numpy array values.