This repository has been archived by the owner on Mar 4, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 12
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 #20 from cta-sst-1m/protobuf_to_namedtuple
Work in progress: Protobuf to namedtuple
- Loading branch information
Showing
30 changed files
with
644 additions
and
291 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
File renamed without changes.
File renamed without changes.
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 @@ | ||
0.44.3 |
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,15 @@ | ||
from pkg_resources import resource_string | ||
from .digicam import File as DigicamFile | ||
from .simple import File as SimpleFile | ||
from .any_array_to_numpy import any_array_to_numpy | ||
from .simple import make_namedtuple | ||
|
||
__version__ = resource_string('protozfits', 'VERSION').decode().strip() | ||
|
||
|
||
__all__ = [ | ||
'DigicamFile', | ||
'SimpleFile', | ||
'make_namedtuple', | ||
'any_array_to_numpy', | ||
] |
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,33 @@ | ||
import numpy as np | ||
|
||
|
||
def any_array_to_numpy(any_array): | ||
any_array_type_to_numpy_type = { | ||
1: np.int8, | ||
2: np.uint8, | ||
3: np.int16, | ||
4: np.uint16, | ||
5: np.int32, | ||
6: np.uint32, | ||
7: np.int64, | ||
8: np.uint64, | ||
9: np.float, | ||
10: np.double, | ||
} | ||
if any_array.type == 0: | ||
if any_array.data: | ||
raise Exception("any_array has no type", any_array) | ||
else: | ||
return np.array([]) | ||
if any_array.type == 11: | ||
print(any_array) | ||
raise Exception( | ||
"I have no idea if the boolean representation of" | ||
" the anyarray is the same as the numpy one", | ||
any_array | ||
) | ||
|
||
return np.frombuffer( | ||
any_array.data, | ||
any_array_type_to_numpy_type[any_array.type] | ||
) |
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,121 @@ | ||
from os.path import isfile | ||
import warnings | ||
import numpy as np | ||
from astropy.utils.decorators import lazyproperty | ||
|
||
from . import rawzfitsreader | ||
from google.protobuf.pyext.cpp_message import GeneratedProtocolMessageType | ||
from .patch_ids import PATCH_ID_INPUT_SORT_IDS, PATCH_ID_OUTPUT_SORT_IDS | ||
from .any_array_to_numpy import any_array_to_numpy | ||
from .L0_pb2 import CameraEvent | ||
|
||
|
||
class File: | ||
|
||
def __init__(self, fname): | ||
if not isfile(fname): | ||
raise FileNotFoundError(fname) | ||
self.fname = fname | ||
rawzfitsreader.open(self.fname + ":Events") | ||
self.numrows = rawzfitsreader.getNumRows() | ||
self.run_id = 0 | ||
|
||
def __iter__(self): | ||
return self | ||
|
||
def __next__(self): | ||
event = CameraEvent() | ||
try: | ||
event.ParseFromString(rawzfitsreader.readEvent()) | ||
return Event(event, self.run_id) | ||
except EOFError: | ||
raise StopIteration | ||
|
||
def list_tables(self): | ||
return rawzfitsreader.listAllTables(self.fname) | ||
|
||
def rewind_table(self): | ||
# Rewind the current reader. Go to the beginning of the table. | ||
rawzfitsreader.rewindTable() | ||
|
||
|
||
class Event: | ||
_sort_ids = None | ||
|
||
def __init__(self, event, run_id): | ||
if type(type(event)) is GeneratedProtocolMessageType: | ||
trafo = any_array_to_numpy | ||
else: | ||
trafo = no_trafo | ||
|
||
self.run_id = run_id | ||
self._event = event | ||
|
||
self.pixel_ids = trafo( | ||
self._event.hiGain.waveforms.pixelsIndices) | ||
if Event._sort_ids is None: | ||
Event._sort_ids = np.argsort(self.pixel_ids) | ||
self.n_pixels = len(self.pixel_ids) | ||
self._samples = ( | ||
trafo(self._event.hiGain.waveforms.samples) | ||
).reshape(self.n_pixels, -1) | ||
self.baseline = self.unsorted_baseline[Event._sort_ids] | ||
self.telescope_id = self._event.telescopeID | ||
self.event_number = self._event.eventNumber | ||
self.central_event_gps_time = ( | ||
self._event.trig.timeSec * 1E9 + self._event.trig.timeNanoSec | ||
) | ||
self.local_time = ( | ||
self._event.local_time_sec * 1E9 + self._event.local_time_nanosec | ||
) | ||
self.event_number_array = self._event.arrayEvtNum | ||
self.camera_event_type = self._event.event_type | ||
self.array_event_type = self._event.eventType | ||
self.num_gains = self._event.num_gains | ||
self.num_channels = self._event.head.numGainChannels | ||
self.num_samples = self._samples.shape[1] | ||
self.pixel_flags = trafo( | ||
self._event.pixels_flags)[Event._sort_ids] | ||
self.adc_samples = self._samples[Event._sort_ids] | ||
self.trigger_output_patch7 = _prepare_trigger_output( | ||
trafo(self._event.trigger_output_patch7)) | ||
self.trigger_output_patch19 = _prepare_trigger_output( | ||
trafo(self._event.trigger_output_patch19)) | ||
self.trigger_input_traces = _prepare_trigger_input( | ||
trafo(self._event.trigger_input_traces)) | ||
|
||
@lazyproperty | ||
def unsorted_baseline(self): | ||
try: | ||
return any_array_to_numpy(self._event.hiGain.waveforms.baselines) | ||
except ValueError: | ||
warnings.warn(( | ||
"Could not read `hiGain.waveforms.baselines` for event:{0}" | ||
"of run_id:{1}".format(self.event_number, self.run_id) | ||
)) | ||
return np.ones(len(self.pixel_ids)) * np.nan | ||
|
||
|
||
def _prepare_trigger_input(_a): | ||
A, B = 3, 192 | ||
cut = 144 | ||
_a = _a.reshape(-1, A) | ||
_a = _a.reshape(-1, A, B) | ||
_a = _a[..., :cut] | ||
_a = _a.reshape(_a.shape[0], -1) | ||
_a = _a.T | ||
_a = _a[PATCH_ID_INPUT_SORT_IDS] | ||
return _a | ||
|
||
|
||
def _prepare_trigger_output(_a): | ||
A, B, C = 3, 18, 8 | ||
|
||
_a = np.unpackbits(_a.reshape(-1, A, B, 1), axis=-1) | ||
_a = _a[..., ::-1] | ||
_a = _a.reshape(-1, A*B*C).T | ||
return _a[PATCH_ID_OUTPUT_SORT_IDS] | ||
|
||
|
||
def no_trafo(value): | ||
return value |
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Oops, something went wrong.