From e93a51e1b7f345edfee779569b27ccb951ba790f Mon Sep 17 00:00:00 2001 From: Tamas Gal <tgal@km3net.de> Date: Wed, 4 Mar 2020 14:03:45 +0100 Subject: [PATCH] Remove obselete Reader class --- km3io/offline.py | 65 ----- notebooks/Reader_tutorial.ipynb | 440 -------------------------------- tests/test_offline.py | 68 +---- 3 files changed, 1 insertion(+), 572 deletions(-) delete mode 100644 notebooks/Reader_tutorial.ipynb diff --git a/km3io/offline.py b/km3io/offline.py index e9c4ead..862ffc9 100644 --- a/km3io/offline.py +++ b/km3io/offline.py @@ -211,71 +211,6 @@ class OfflineKeys: return km3io.definitions.trigger.data -class Reader: - """Reader for one offline ROOT file""" - def __init__(self, file_path): - """ Reader class is an offline ROOT file reader. This class is a - "very" low level I/O. - - Parameters - ---------- - file_path : str or file-like object - The file of interest. - """ - self._tree = uproot.open(file_path)[MAIN_TREE_NAME] - self._data = self._tree.lazyarrays( - basketcache=uproot.cache.ThreadSafeArrayCache(BASKET_CACHE_SIZE)) - self._keys = None - - def __getitem__(self, key): - """reads data stored in the branch of interest in an Evt tree. - - Parameters - ---------- - key : str - name of the branch of interest in event data. - - Returns - ------- - lazyarray - Lazyarray of all data stored in the branch of interest. A lazyarray - is an array-like object that reads data on demand. Here, only the - first and last chunks of data are read in memory, and not all data - in the array. The output can be used with all `Numpy's universal - functions <https://docs.scipy.org/doc/numpy/reference/ufuncs.html>` - . - - Raises - ------ - KeyError - Some branches in an offline file structure are "fake branches" and - do not contain data. Therefore, the keys corresponding to these - fake branches are not read. - """ - keys = self.keys.valid_keys - if key not in keys and not isinstance(key, int): - raise KeyError( - "'{}' is not a valid key or is a fake branch.".format(key)) - return self._data[key] - - def __len__(self): - return len(self._data) - - def __repr__(self): - return "<{}: {} entries>".format(self.__class__.__name__, len(self)) - - @cached_property - def keys(self): - """wrapper for all keys in an offline file. - - Returns - ------- - Class - OfflineKeys. - """ - return OfflineKeys(self._tree) - - class OfflineReader: """reader for offline ROOT files""" def __init__(self, file_path=None): diff --git a/notebooks/Reader_tutorial.ipynb b/notebooks/Reader_tutorial.ipynb deleted file mode 100644 index 808e545..0000000 --- a/notebooks/Reader_tutorial.ipynb +++ /dev/null @@ -1,440 +0,0 @@ -{ - "cells": [ - { - "cell_type": "code", - "execution_count": 1, - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "['/home/zineb/km3net/km3net/km3io/notebooks', '/home/zineb/miniconda3/envs/km3pipe/lib/python37.zip', '/home/zineb/miniconda3/envs/km3pipe/lib/python3.7', '/home/zineb/miniconda3/envs/km3pipe/lib/python3.7/lib-dynload', '', '/home/zineb/miniconda3/envs/km3pipe/lib/python3.7/site-packages', '/home/zineb/km3net/km3net/km3io', '/home/zineb/miniconda3/envs/km3pipe/lib/python3.7/site-packages/IPython/extensions', '/home/zineb/.ipython', '/home/zineb/km3net/km3net/km3io']\n" - ] - } - ], - "source": [ - "# Add file to current python path\n", - "from pathlib import Path\n", - "import sys\n", - "sys.path.append(str(Path.cwd().parent))\n", - "Path.cwd()\n", - "print(sys.path)" - ] - }, - { - "cell_type": "code", - "execution_count": 2, - "metadata": {}, - "outputs": [], - "source": [ - "from km3io.aanet import Reader" - ] - }, - { - "cell_type": "code", - "execution_count": 3, - "metadata": {}, - "outputs": [], - "source": [ - "# test samples directory - aanet test file\n", - "files_path = Path.cwd().parent / 'tests/samples' \n", - "aanet_file = files_path / 'aanet_v2.0.0.root'" - ] - }, - { - "cell_type": "code", - "execution_count": 4, - "metadata": { - "scrolled": true - }, - "outputs": [ - { - "data": { - "text/plain": [ - "<Reader: 10 entries>" - ] - }, - "execution_count": 4, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "reader = Reader(aanet_file)\n", - "reader" - ] - }, - { - "cell_type": "code", - "execution_count": 5, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "['trks.fUniqueID',\n", - " 'trks.fBits',\n", - " 'trks.id',\n", - " 'trks.pos.x',\n", - " 'trks.pos.y',\n", - " 'trks.pos.z',\n", - " 'trks.dir.x',\n", - " 'trks.dir.y',\n", - " 'trks.dir.z',\n", - " 'trks.t',\n", - " 'trks.E',\n", - " 'trks.len',\n", - " 'trks.lik',\n", - " 'trks.type',\n", - " 'trks.rec_type',\n", - " 'trks.rec_stages',\n", - " 'trks.status',\n", - " 'trks.mother_id',\n", - " 'trks.fitinf',\n", - " 'trks.hit_ids',\n", - " 'trks.error_matrix',\n", - " 'trks.comment']" - ] - }, - "execution_count": 5, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "reader.keys.tracks_keys" - ] - }, - { - "cell_type": "code", - "execution_count": 6, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "<Table [<Row 0> <Row 1> <Row 2> ... <Row 7> <Row 8> <Row 9>] at 0x7fed2d98a750>" - ] - }, - "execution_count": 6, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "# big lazyarray with ALL file data!\n", - "lazy_data = reader._data\n", - "lazy_data" - ] - }, - { - "cell_type": "code", - "execution_count": 7, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "<ChunkedArray [5971 5971 5971 ... 5971 5971 5971] at 0x7fed2d91f9d0>" - ] - }, - "execution_count": 7, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "# getting the run_id for a specific event (event 5 for example)\n", - "reader['run_id']" - ] - }, - { - "cell_type": "code", - "execution_count": 8, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "60" - ] - }, - "execution_count": 8, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "# one can check how many hits are in event 5\n", - "reader[5]['hits']" - ] - }, - { - "cell_type": "code", - "execution_count": 9, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "56" - ] - }, - "execution_count": 9, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "# one can also check how many tracks are in event 5\n", - "reader[5]['trks']" - ] - }, - { - "cell_type": "code", - "execution_count": 10, - "metadata": {}, - "outputs": [], - "source": [ - "# # the user is reminded to always specify the \"correct\" event/hits/tracks \n", - "# # key in the Aanet event file\n", - "# try:\n", - "# reader['whatever']\n", - "# except KeyError as e:\n", - "# print(e)" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "# Now let's explore in more details the hits:" - ] - }, - { - "cell_type": "code", - "execution_count": 11, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "<ChunkedArray [[806451572 806451572 806451572 ... 809544061 809544061 809544061] [806451572 806451572 806451572 ... 809524432 809526097 809544061] [806451572 806451572 806451572 ... 809544061 809544061 809544061] ... [806451572 806455814 806465101 ... 809526097 809544058 809544061] [806455814 806455814 806455814 ... 809544061 809544061 809544061] [806455814 806455814 806455814 ... 809544058 809544058 809544061]] at 0x7fed2d8af450>" - ] - }, - "execution_count": 11, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "# reading all data from a specific branch in hits data: for example \n", - "# 'hits.dom_id'\n", - "reader['hits.dom_id']" - ] - }, - { - "cell_type": "code", - "execution_count": 12, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "array([806455814, 806487219, 806487219, 806487219, 806487226, 808432835,\n", - " 808432835, 808432835, 808432835, 808432835, 808432835, 808432835,\n", - " 808451904, 808451904, 808451907, 808451907, 808469129, 808469129,\n", - " 808469129, 808493910, 808949744, 808949744, 808951460, 808951460,\n", - " 808956908, 808961655, 808964908, 808969848, 808969857, 808972593,\n", - " 808972593, 808972598, 808972598, 808972698, 808972698, 808974758,\n", - " 808974811, 808976377, 808981510, 808981523, 808981812, 808982005,\n", - " 808982005, 808982018, 808982077, 808982077, 808982547, 809007627,\n", - " 809521500, 809521500, 809521500, 809524432, 809526097, 809526097,\n", - " 809526097, 809526097, 809526097, 809526097, 809526097, 809544058],\n", - " dtype=int32)" - ] - }, - "execution_count": 12, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "# the user can access hits.dom_id data for a specific event (for example \n", - "# event 5)\n", - "reader['hits.dom_id'][5]" - ] - }, - { - "cell_type": "code", - "execution_count": 13, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "60" - ] - }, - "execution_count": 13, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "# We previsouly checked (using reader[5]['hits']) that event\n", - "# 5 has 60 hits, now we can see that reader['hits.dom_id'][5]\n", - "# has exaclty 60 dom ids as well! \n", - "len(reader['hits.dom_id'][5])" - ] - }, - { - "cell_type": "code", - "execution_count": 14, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "806455814" - ] - }, - "execution_count": 14, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "# one can access a dom id of the first hit \n", - "reader['hits.dom_id'][5][0]" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "# Now let's explore in more details the tracks:" - ] - }, - { - "cell_type": "code", - "execution_count": 15, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "<ChunkedArray [[-0.872885221293917 -0.872885221293917 -0.872885221293917 ... -0.6631226836266504 -0.5680647731737454 -0.5680647731737454] [-0.8351996698137462 -0.8351996698137462 -0.8351996698137462 ... -0.7485107718446855 -0.8229838871876581 -0.239315690284641] [-0.989148723802379 -0.989148723802379 -0.989148723802379 ... -0.9350162572437829 -0.88545604390297 -0.88545604390297] ... [-0.5704611045902105 -0.5704611045902105 -0.5704611045902105 ... -0.9350162572437829 -0.4647231989130516 -0.4647231989130516] [-0.9779941383490359 -0.9779941383490359 -0.9779941383490359 ... -0.88545604390297 -0.88545604390297 -0.8229838871876581] [-0.7396916780974963 -0.7396916780974963 -0.7396916780974963 ... -0.6631226836266504 -0.7485107718446855 -0.7485107718446855]] at 0x7fed2d8b8850>" - ] - }, - "execution_count": 15, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "# reading all data from a specific branch in tracks data:\n", - "reader['trks.dir.z']" - ] - }, - { - "cell_type": "code", - "execution_count": 16, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "array([-0.60246049, -0.60246049, -0.60246049, -0.51420541, -0.5475772 ,\n", - " -0.5772408 , -0.56068238, -0.64907684, -0.67781799, -0.66565114,\n", - " -0.63014839, -0.64566464, -0.62691012, -0.58465493, -0.59287533,\n", - " -0.63655091, -0.63771247, -0.73446841, -0.7456636 , -0.70941246,\n", - " -0.66312268, -0.66312268, -0.56806477, -0.56806477, -0.66312268,\n", - " -0.66312268, -0.74851077, -0.74851077, -0.66312268, -0.74851077,\n", - " -0.56806477, -0.74851077, -0.66312268, -0.74851077, -0.56806477,\n", - " -0.66312268, -0.56806477, -0.66312268, -0.56806477, -0.56806477,\n", - " -0.66312268, -0.74851077, -0.66312268, -0.93501626, -0.56806477,\n", - " -0.74851077, -0.66312268, -0.56806477, -0.82298389, -0.74851077,\n", - " -0.66312268, -0.56806477, -0.82298389, -0.56806477, -0.66312268,\n", - " -0.97094183])" - ] - }, - "execution_count": 16, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "# the user can access trks.dir.z data for a specific event (for example \n", - "# event 5)\n", - "reader['trks.dir.z'][5]" - ] - }, - { - "cell_type": "code", - "execution_count": 17, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "56" - ] - }, - "execution_count": 17, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "# We previsouly checked (using reader[5]['trks']) that event\n", - "# 5 has 56 tracks, now we can see that reader['trks.dir.z'][5]\n", - "# has exaclty 56 values of trks.dir.z as well! \n", - "len(reader['trks.dir.z'][5])" - ] - }, - { - "cell_type": "code", - "execution_count": 18, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "-0.6024604933159441" - ] - }, - "execution_count": 18, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "# one can access the first trks.dir.z from event 5 using \n", - "reader['trks.dir.z'][5][0]" - ] - } - ], - "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.4" - } - }, - "nbformat": 4, - "nbformat_minor": 4 -} diff --git a/tests/test_offline.py b/tests/test_offline.py index 0fa6eff..60ae196 100644 --- a/tests/test_offline.py +++ b/tests/test_offline.py @@ -2,7 +2,7 @@ import unittest import numpy as np from pathlib import Path -from km3io.offline import Reader, OfflineEvents, OfflineHits, OfflineTracks +from km3io.offline import OfflineEvents, OfflineHits, OfflineTracks from km3io import OfflineReader SAMPLES_DIR = Path(__file__).parent / 'samples' @@ -42,72 +42,6 @@ class TestOfflineKeys(unittest.TestCase): self.assertEqual(len(self.keys.fit_keys), 18) -class TestReader(unittest.TestCase): - def setUp(self): - self.r = Reader(OFFLINE_FILE) - self.lengths = {0: 176, 1: 125, -1: 105} - self.total_item_count = 1434 - - def test_reading_dom_id(self): - dom_ids = self.r["hits.dom_id"] - - for event_id, length in self.lengths.items(): - self.assertEqual(length, len(dom_ids[event_id])) - - self.assertEqual(self.total_item_count, sum(dom_ids.count())) - - self.assertListEqual([806451572, 806451572, 806451572], - list(dom_ids[0][:3])) - - def test_reading_channel_id(self): - channel_ids = self.r["hits.channel_id"] - - for event_id, length in self.lengths.items(): - self.assertEqual(length, len(channel_ids[event_id])) - - self.assertEqual(self.total_item_count, sum(channel_ids.count())) - - self.assertListEqual([8, 9, 14], list(channel_ids[0][:3])) - - # channel IDs are always between [0, 30] - self.assertTrue(all(c >= 0 for c in channel_ids.min())) - self.assertTrue(all(c < 31 for c in channel_ids.max())) - - def test_reading_times(self): - ts = self.r["hits.t"] - - for event_id, length in self.lengths.items(): - self.assertEqual(length, len(ts[event_id])) - - self.assertEqual(self.total_item_count, sum(ts.count())) - - self.assertListEqual([70104010.0, 70104016.0, 70104192.0], - list(ts[0][:3])) - - def test_reading_keys(self): - # there are 106 "valid" keys in an offline file - self.assertEqual(len(self.r.keys.valid_keys), 106) - - # there are 20 hits keys - self.assertEqual(len(self.r.keys.hits_keys), 20) - self.assertEqual(len(self.r.keys.mc_hits_keys), 20) - - # there are 22 tracks keys - self.assertEqual(len(self.r.keys.tracks_keys), 22) - self.assertEqual(len(self.r.keys.mc_tracks_keys), 22) - - def test_raising_KeyError(self): - # non valid keys must raise a KeyError - with self.assertRaises(KeyError): - self.r['whatever'] - - def test_number_events(self): - Nevents = len(self.r) - - # check that there are 10 events - self.assertEqual(Nevents, 10) - - class TestOfflineReader(unittest.TestCase): def setUp(self): self.r = OfflineReader(OFFLINE_FILE) -- GitLab