-
Notifications
You must be signed in to change notification settings - Fork 270
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #978 from cta-observatory/allow_enums_in_table_wri…
…ter_and_reader * allow enums in containers and support in tableio
- Loading branch information
Showing
4 changed files
with
312 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
212 changes: 212 additions & 0 deletions
212
docs/examples/containers_with_enums_and_table_writer.ipynb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,212 @@ | ||
{ | ||
"cells": [ | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"# containers_with_enums_and_table_writer\n" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"## Create some example Containers" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"import enum\n", | ||
"from ctapipe.io import HDF5TableWriter\n", | ||
"from ctapipe.core import Container, Field\n", | ||
"from astropy import units as u\n", | ||
"import numpy as np" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"class WithEnum(Container):\n", | ||
" \n", | ||
" # this class could also be defined in global namespace \n", | ||
" # outside this container, but this looks a bit tidier.\n", | ||
" # both variants work however\n", | ||
" class EventType(enum.Enum):\n", | ||
" pedestal = 1\n", | ||
" physics = 2\n", | ||
" calibration = 3\n", | ||
" \n", | ||
" event_type = Field(\n", | ||
" EventType.calibration, \n", | ||
" f'type of event, one of: {list(EventType.__members__.keys())}'\n", | ||
" )" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"let's also make a dummy stream (generator) that will create a series of these containers" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"def create_stream(n_event):\n", | ||
" data = WithEnum()\n", | ||
" for i in range(n_event):\n", | ||
" data.event_type = WithEnum.EventType(i % 3 + 1)\n", | ||
" yield data" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"for data in create_stream(3):\n", | ||
" for key, val in data.items():\n", | ||
" print('{}: {}, type : {}'.format(key, val, type(val)))" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"## Writing the Data" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"with HDF5TableWriter('container.h5', group_name='data') as h5_table:\n", | ||
" for data in create_stream(10):\n", | ||
" h5_table.write('table', data)" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"!ls container.h5" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"## Reading the Data" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"import pandas as pd\n", | ||
"\n", | ||
"data = pd.read_hdf('container.h5', key='/data/table')\n", | ||
"data.head()" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"#### Reading with PyTables" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"import tables\n", | ||
"h5 = tables.open_file('container.h5')\n", | ||
"table = h5.root['data']['table']\n", | ||
"table" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"table.attrs" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"from ctapipe.io import HDF5TableReader\n", | ||
"\n", | ||
"def read(mode):\n", | ||
" \n", | ||
" print('reading mode {}'.format(mode))\n", | ||
"\n", | ||
" with HDF5TableReader('container.h5', mode=mode) as h5_table:\n", | ||
"\n", | ||
" for group_name in ['data/']:\n", | ||
"\n", | ||
" group_name = '/{}table'.format(group_name)\n", | ||
" print(group_name)\n", | ||
"\n", | ||
" for data in h5_table.read(group_name, WithEnum()):\n", | ||
"\n", | ||
" print(data.as_dict())" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"read('r')" | ||
] | ||
} | ||
], | ||
"metadata": { | ||
"kernelspec": { | ||
"display_name": "Python 3", | ||
"language": "python", | ||
"name": "python3" | ||
}, | ||
"language_info": { | ||
"codemirror_mode": { | ||
"name": "ipython", | ||
"version": 3 | ||
}, | ||
"file_extension": ".py", | ||
"mimetype": "text/x-python", | ||
"name": "python", | ||
"nbconvert_exporter": "python", | ||
"pygments_lexer": "ipython3", | ||
"version": "3.7.2" | ||
} | ||
}, | ||
"nbformat": 4, | ||
"nbformat_minor": 2 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters