Skip to content

Commit af4d667

Browse files
authored
Merge pull request #67 from vmarandon/guess_stream_number
Add an automatic detection of the number of stream
2 parents 6f6dd62 + 31f990f commit af4d667

2 files changed

Lines changed: 32 additions & 1 deletion

File tree

src/ctapipe_io_nectarcam/__init__.py

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1319,7 +1319,7 @@ class BlockNectarCAMEventSource:
13191319
13201320
"""
13211321

1322-
def __init__(self, block_size=4, allowed_blocks=None, **kwargs):
1322+
def __init__(self, block_size=None, allowed_blocks=None, **kwargs):
13231323
self._arguments = kwargs # blocks
13241324
self._file_names = None
13251325
self._block_file_names = list()
@@ -1366,6 +1366,8 @@ def __init__(self, block_size=4, allowed_blocks=None, **kwargs):
13661366
self.show_empty_stats = bool(kwargs["show_empty_stats"])
13671367
del self._arguments["show_empty_stats"]
13681368

1369+
if self.block_size is None:
1370+
self.block_size = self.guess_block_size_from_file(self._file_names[0])
13691371
self._create_blocks()
13701372
self._switch_block()
13711373

@@ -1392,6 +1394,26 @@ def call_wrapper(*args, **kwargs):
13921394
else:
13931395
return attr_val
13941396

1397+
@staticmethod
1398+
def guess_block_size_from_file(filename):
1399+
# try to open the file
1400+
# read the first events
1401+
# look at the separation between ids
1402+
try:
1403+
with File(str(filename)) as f:
1404+
ids = list()
1405+
for e in f.Events:
1406+
ids.append(e.event_id)
1407+
if len(ids) > 100:
1408+
break
1409+
ids = np.array(ids, dtype=int)
1410+
block_size = int(np.rint(np.median(np.array(ids[1:] - ids[:-1]))))
1411+
# print(f"{self.block_size = }")
1412+
except Exception:
1413+
print("Can't guess properly block size !")
1414+
block_size = 4
1415+
return block_size
1416+
13951417
def _rewind(self):
13961418
self._current_block = None
13971419
self._switch_block()

src/ctapipe_io_nectarcam/tests/test_nectarcameventsource.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -253,3 +253,12 @@ def test_r1_waveforms():
253253
for event in inputfile_reader:
254254
for telid in event.trigger.tels_with_trigger:
255255
assert event.r1.tel[telid].waveform.shape == waveform_shape
256+
257+
258+
def test_blockdetection():
259+
from ctapipe_io_nectarcam import BlockNectarCAMEventSource
260+
261+
assert (
262+
BlockNectarCAMEventSource.guess_block_size_from_file(EXAMPLE_FILE_PATH_V6) == 4
263+
)
264+
assert BlockNectarCAMEventSource.guess_block_size_from_file(EXAMPLE_FILE_PATH) == 2

0 commit comments

Comments
 (0)