From e7eb425b8f4b24006186019317b58f41282a6420 Mon Sep 17 00:00:00 2001 From: zineb aly <aly.zineb.az@gmail.com> Date: Sun, 17 Nov 2019 20:19:08 +0100 Subject: [PATCH] adapted AanetReader format to pep8 --- km3io/aanet.py | 62 ++++++---- notebooks/AanetReader_tutorial.ipynb | 162 +++++++++------------------ 2 files changed, 90 insertions(+), 134 deletions(-) diff --git a/km3io/aanet.py b/km3io/aanet.py index 6a3b161..247ecbd 100644 --- a/km3io/aanet.py +++ b/km3io/aanet.py @@ -1,15 +1,16 @@ import uproot + class AanetReader: """Reader for one Aanet ROOT file""" def __init__(self, file_path): """ AanetReader class is a Aanet ROOT file wrapper - + Parameters ---------- file_path : path-like object - Path to the file of interest. It can be a str or any python path-like object that points - to the file of ineterst. + Path to the file of interest. It can be a str or any python + path-like object that points to the file of ineterst. """ self.file_path = file_path self.data = uproot.open(self.file_path)['E'] @@ -18,52 +19,65 @@ class AanetReader: self._hits_keys = None self._tracks_keys = None - def __getitem__(self, key): - """reads data stored in the branch of interest in an event tree. - + """reads data stored in the branch of interest in an event 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>`. - + 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 ------ KeyEroor - Some branches in an Aanet file structure are "fake branches" and do not contain data. Therefore, - the keys corresponding to these fake branches are not read. + Some branches in an Aanet file structure are "fake branches" and do + not contain data. Therefore, the keys corresponding to these fake + branches are not read. """ if key not in self.keys() and not isinstance(key, int): raise KeyError(f"'{key}' is not a valid key or is a fake branch.") return self.lazy_data[key] def __len__(self): - return len(self.lazy_data) def __repr__(self): - return '\n'.join([f"Number of events: {self.__len__()}", - "Events keys are:\n\t" + '\n\t'.join(self.events_keys), - "Hits keys are:\n\t" + '\n\t'.join(self.hits_keys), - "Tracks keys are:\n\t" + '\n\t'.join(self.tracks_keys)]) + return '\n'.join([ + f"Number of events: {self.__len__()}", + "Events keys are:\n\t" + '\n\t'.join(self.events_keys), + "Hits keys are:\n\t" + '\n\t'.join(self.hits_keys), + "Tracks keys are:\n\t" + '\n\t'.join(self.tracks_keys) + ]) def keys(self): - return self.events_keys + self.hits_keys + self.tracks_keys + """constructs a list of all valid keys to be read from an Aanet event file. + + Returns + ------- + list + list of all valid keys. + """ + return self.events_keys + self.hits_keys + self.tracks_keys @property def events_keys(self): if self._events_keys is None: - fake_branches = ['Evt', 'AAObject', 'TObject','t'] + fake_branches = ['Evt', 'AAObject', 'TObject', 't'] t_baskets = ['t.fSec', 't.fNanoSec'] - self._events_keys = [key.decode('utf-8') for key in self.data['Evt'].keys() if key.decode('utf-8') not in fake_branches] + t_baskets + self._events_keys = [ + key.decode('utf-8') for key in self.data['Evt'].keys() + if key.decode('utf-8') not in fake_branches + ] + t_baskets return self._events_keys @property @@ -77,5 +91,7 @@ class AanetReader: def tracks_keys(self): if self._tracks_keys is None: tracks_tree = self.data['Evt']['trks'] - self._tracks_keys = [key.decode('utf8') for key in tracks_tree.keys()] + self._tracks_keys = [ + key.decode('utf8') for key in tracks_tree.keys() + ] return self._tracks_keys diff --git a/notebooks/AanetReader_tutorial.ipynb b/notebooks/AanetReader_tutorial.ipynb index 5f4e757..4f79101 100644 --- a/notebooks/AanetReader_tutorial.ipynb +++ b/notebooks/AanetReader_tutorial.ipynb @@ -140,7 +140,7 @@ { "data": { "text/plain": [ - "<Table [<Row 0> <Row 1> <Row 2> ... <Row 7> <Row 8> <Row 9>] at 0x7f57ac134810>" + "<Table [<Row 0> <Row 1> <Row 2> ... <Row 7> <Row 8> <Row 9>] at 0x7f9011f02b50>" ] }, "execution_count": 5, @@ -156,7 +156,7 @@ }, { "cell_type": "code", - "execution_count": 11, + "execution_count": 6, "metadata": {}, "outputs": [ { @@ -165,7 +165,7 @@ "5971" ] }, - "execution_count": 11, + "execution_count": 6, "metadata": {}, "output_type": "execute_result" } @@ -177,7 +177,7 @@ }, { "cell_type": "code", - "execution_count": 12, + "execution_count": 7, "metadata": {}, "outputs": [ { @@ -186,7 +186,7 @@ "60" ] }, - "execution_count": 12, + "execution_count": 7, "metadata": {}, "output_type": "execute_result" } @@ -198,7 +198,7 @@ }, { "cell_type": "code", - "execution_count": 13, + "execution_count": 8, "metadata": {}, "outputs": [ { @@ -207,7 +207,7 @@ "56" ] }, - "execution_count": 13, + "execution_count": 8, "metadata": {}, "output_type": "execute_result" } @@ -219,7 +219,7 @@ }, { "cell_type": "code", - "execution_count": 7, + "execution_count": 9, "metadata": {}, "outputs": [ { @@ -248,16 +248,16 @@ }, { "cell_type": "code", - "execution_count": 15, + "execution_count": 10, "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 0x7f57997ee990>" + "<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 0x7f9011eb3b10>" ] }, - "execution_count": 15, + "execution_count": 10, "metadata": {}, "output_type": "execute_result" } @@ -270,7 +270,7 @@ }, { "cell_type": "code", - "execution_count": 16, + "execution_count": 11, "metadata": {}, "outputs": [ { @@ -289,7 +289,7 @@ " dtype=int32)" ] }, - "execution_count": 16, + "execution_count": 11, "metadata": {}, "output_type": "execute_result" } @@ -302,7 +302,7 @@ }, { "cell_type": "code", - "execution_count": 17, + "execution_count": 12, "metadata": {}, "outputs": [ { @@ -311,7 +311,7 @@ "60" ] }, - "execution_count": 17, + "execution_count": 12, "metadata": {}, "output_type": "execute_result" } @@ -325,7 +325,7 @@ }, { "cell_type": "code", - "execution_count": 20, + "execution_count": 13, "metadata": {}, "outputs": [ { @@ -334,7 +334,7 @@ "806455814" ] }, - "execution_count": 20, + "execution_count": 13, "metadata": {}, "output_type": "execute_result" } @@ -353,16 +353,16 @@ }, { "cell_type": "code", - "execution_count": 24, + "execution_count": 14, "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 0x7f57997f8d90>" + "<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 0x7f9011ebdf10>" ] }, - "execution_count": 24, + "execution_count": 14, "metadata": {}, "output_type": "execute_result" } @@ -374,7 +374,7 @@ }, { "cell_type": "code", - "execution_count": 25, + "execution_count": 15, "metadata": {}, "outputs": [ { @@ -394,7 +394,7 @@ " -0.97094183])" ] }, - "execution_count": 25, + "execution_count": 15, "metadata": {}, "output_type": "execute_result" } @@ -407,107 +407,47 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 16, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "56" + ] + }, + "execution_count": 16, + "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.hits_reader('hits.dom_id')[5]\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.read_tracks('trks.dir.z')[5])" + "len(reader['trks.dir.z'][5])" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 17, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "-0.6024604933159441" + ] + }, + "execution_count": 17, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "# one can access the first trks.dir.z from event 5 using \n", - "reader.read_tracks('trks.dir.z')[5][0]" + "reader['trks.dir.z'][5][0]" ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# once again, the user is reminded to always use the tracks keys stored in Aanet\n", - "# event file\n", - "try:\n", - " reader.read_tracks('whatever')\n", - "except KeyError as e:\n", - " print(e)" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "reader['trks.dir.z']" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "import numpy as np\n", - "np.array(reader[0])" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [] } ], "metadata": { -- GitLab