<!--
This file is generated by a tool. Do not edit directly.
For open-source contributions the docs will be updated automatically.
-->

*Last updated: 2023-03-16.*

<div itemscope itemtype="http://developers.google.com/ReferenceObject">
<meta itemprop="name" content="tf_quant_finance.experimental.io.ArrayDictWriter" />
<meta itemprop="path" content="Stable" />
<meta itemprop="property" content="__enter__"/>
<meta itemprop="property" content="__exit__"/>
<meta itemprop="property" content="__init__"/>
<meta itemprop="property" content="close"/>
<meta itemprop="property" content="flush"/>
<meta itemprop="property" content="write"/>
</div>

# tf_quant_finance.experimental.io.ArrayDictWriter

<!-- Insert buttons and diff -->

<table class="tfo-notebook-buttons tfo-api" align="left">
</table>

<a target="_blank" href="https://github.com/paolodelia99/tf-quant-finance/blob/main/tf_quant_finance/experimental/io.py">View source</a>



Writer to write dictionaries of numpy arrays in binary format.

```python
tf_quant_finance.experimental.io.ArrayDictWriter(
    path
)
```



<!-- Placeholder for "Used in" -->

Writes dictionaries with string keys and numpy array values as records into
a [tfrecords](https://www.tensorflow.org/tutorials/load_data/tfrecord) file.
The usage of tfrecords should be treated as an implementation detail. To
read the file, use the `ArrayDictReader` class.

#### Notes and Limitations:



1. Any values which are not numpy arrays will be first converted to
  an array before serializing.
2. 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
```python
  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

<h3 id="__enter__"><code>__enter__</code></h3>

<a target="_blank" href="https://github.com/paolodelia99/tf-quant-finance/blob/main/tf_quant_finance/experimental/io.py">View source</a>

```python
__enter__()
```




<h3 id="__exit__"><code>__exit__</code></h3>

<a target="_blank" href="https://github.com/paolodelia99/tf-quant-finance/blob/main/tf_quant_finance/experimental/io.py">View source</a>

```python
__exit__(
    unused_type, unused_value, unused_traceback
)
```

Exits a `with` block and closes the file.


<h3 id="close"><code>close</code></h3>

<a target="_blank" href="https://github.com/paolodelia99/tf-quant-finance/blob/main/tf_quant_finance/experimental/io.py">View source</a>

```python
close()
```

Close the file.


<h3 id="flush"><code>flush</code></h3>

<a target="_blank" href="https://github.com/paolodelia99/tf-quant-finance/blob/main/tf_quant_finance/experimental/io.py">View source</a>

```python
flush()
```

Flushes the file.


<h3 id="write"><code>write</code></h3>

<a target="_blank" href="https://github.com/paolodelia99/tf-quant-finance/blob/main/tf_quant_finance/experimental/io.py">View source</a>

```python
write(
    array_dict
)
```

Writes a dictionary of arrays to the file.


#### Args:


* <b>`array_dict`</b>: A record to write. Should be a dictionary with string keys and
  numpy array values.



