still don't like the reader
dneise opened this issue · 1 comments
Hey,
I was still not really happy with the reader. I came up with a different interface, see
https://github.com/mackaiver/lst_calibration/tree/dneise_io_File
1.) dragonboard.io only contains one public called: "File" directly added to the dragonboard namespace. So one can do: from dragonboard import File
2.) The dragonboard.File
is both iterable and can be sliced like a list. So one can do
import dragonboard as dbd
my_file = dbd.File("Ped444706_1.dat")
for each_event in my_file:
# show iteration, but don't do anything with it :-)
break
some_event = next(my_file)
print(some_event.header.event_counter)
print(some_event.header.stop_cells["low"][3])
print(some_event.data["low"][3])
Or
import dragonboard as dbd
my_file = dbd.File("Ped444706_1.dat")
some_event = my_file[15]
print(some_event.header.event_counter)
print(some_event.header.stop_cells["low"][3])
print(some_event.data["low"][3])
Or
import dragonboard as dbd
my_file = dbd.File("Ped444706_1.dat")
the_last_ten_events = my_file[-10:]
for an_event in the_last_ten_events:
print(an_event.header.event_counter)
Or even backwards if you like:
import dragonboard as dbd
my_file = dbd.File("Ped444706_1.dat")
my_file_backwards_as_a_list = my_file[::-1]
for event in my_file_backwards_as_a_list:
print(event.header.event_counter)
3.) I don't like the fact, that event.data
does not immediately reveal its interface, as in
import dragonboard as dbd
my_file = dbd.File("Ped444706_1.dat")
first_event = my_file[0]
print(first_event.data.shape) # ---> (8,)
print(first_event.data.dtype) # ---> dtype([('low', '>i2', (1024,)), ('high', '>i2', (1024,))])
print(first_event.data[3]["low"][14:17]) # ---> [160 102 138]
I'd like it better, if one would clearly see, there are 8 channels, 2 gains and 1024 slices, on a single view.
So you can tell the dragon.File
to not return a structured array with return_structured_array=False
.
import dragonboard as dbd
my_file = dbd.File("Ped444706_1.dat", return_structured_array=False)
first_event = my_file[0]
print(first_event.data.shape) # ---> (8, 2, 1024)
print(first_event.data.dtype) # ---> ">i2"
print(first_event.data[3, 1, 14:17]) # ---> [160 102 138]
Both versions, perform equally fast on my machine. But some operations, are easier to achieve, when using the plain 3d-numpy-array, like:
import dragonboard as dbd
my_file = dbd.File("Ped444706_1.dat", return_structured_array=False)
first_event = my_file[0]
print("what is the maximum in each channel", first_event.data.max(axis=2))
#array([[ 367, 302],
# [ 389, 304],
# [ 323, 256],
# [ 298, 253],
# [ 339, 263],
# [ 363, 265],
# [ 346, 319],
# [2143, 2168]], dtype=int16)
print("where is the maximum?", first_event.data.argmax(axis=2)))
#array([[428, 431],
# [432, 880],
# [432, 428],
# [430, 430],
# [430, 427],
# [883, 850],
# [432, 491],
# [428, 945]])
What do you think?
4.) In case one is interested in having all events of a file, in a 4d-numpy-array one can also do:
import dragonboard as dbd
my_file = dbd.File("Ped444706_1.dat", return_structured_array=False)
all_event_data = np.array([e.data for e in my_file[:]])
print(all_event_data.shape) # --> (1000, 8, 2, 1024)
and so on and so on....
What do you guys think?
how up to date is this issue? is the reader still unsatisfactory for anybody?