Commit 1e469036 authored by Remy Moll's avatar Remy Moll
Browse files

Useful data models

parent 83c82705
This source diff could not be displayed because it is stored in LFS. You can view the blob instead.
from os import device_encoding
import numpy as np
from scipy import signal
import decode
DECODER = decode.ArrayLoader("data/")
class Channel:
def __init__(self, number, timings, voltage, nbins) -> None:
self.number = number
self.timings = timings
self.voltage = voltage
self.rebin(nbins=nbins)
self.peaks = self.find_peaks()
self.matches_criteria = self.determine_if_usable()
@property
def peak_index(self):
return self.peaks[0]
# TODO Check if the first value is the most accurate or if the index of maximum voltage is better
@property
def peak_time(self):
return self.timings[self.peak_index]
@property
def is_usable(self):
return self.matches_criteria
def rebin(self, nbins=3) -> None:
"""Changes self.timings and self.voltage and sort of deflates them"""
for i in range(nbins):
self.timings = (self.timings[:-1:2] + self.timings[1::2])/2
self.voltage = (self.voltage[:-1:2] + self.voltage[1::2])/2
def find_peaks(self):
peak_indices, _ = signal.find_peaks(np.abs(self.voltage), height=0.1)#, width=5)
return peak_indices
def determine_if_usable(self):
if np.min(self.voltage) >= 2 * np.mean(self.voltage):
# the peak (negative) is not at least twice as strong as the (negative average)
return False
if len(self.peaks) == 0:
return False
if len(self.peaks) > 10:
return False
return True
class Event:
def __init__(self, event_data, nbins, min_good_channels) -> None:
self.min_good_channels = min_good_channels
self.channels = []
for i in range(event_data.shape[0]): # no. of channels:
self.channels.append(Channel(i, event_data[i,:,0], event_data[i,:,1], nbins))
del event_data
self.n_channels = len(self.channels)
@property
def is_usable(self):
good = sum([ch.is_usable for ch in self.channels])
return good >= self.min_good_channels
class Measurement:
def __init__(self, cathode_voltage, n_events, delay, nbins, min_good_channels) -> None:
self.cathode_voltage = cathode_voltage
self.delay = delay
self.events = []
loaded = DECODER.get_data(self.cathode_voltage, events=n_events, delay=self.delay)
for i in range(loaded.shape[0]): # no. of events:
self.events.append(Event(loaded[i,...], nbins, min_good_channels))
del loaded
self.n_events = len(self.events)
def filter_events(self):
keep = [e for e in self.events if e.is_usable]
self.events = keep
self.n_events = len(keep)
def get_peak_timings(self):
"""Returns an array [n_channels x n_events] with the computed time of each maximum"""
timings = np.zeros((self.events[0].n_channels, self.n_events))
for i,e in enumerate(self.events):
for j,ch in enumerate(e.channels):
if ch.is_usable:
timings[j,i] = ch.peak_time
else:
timings[j,i] = -1
return timings
\ No newline at end of file
This diff is collapsed.
This diff is collapsed.
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment