Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add fuse base plugins to reduce duplicate code #112

Merged
merged 20 commits into from
Feb 1, 2024
Merged
Show file tree
Hide file tree
Changes from 15 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
87 changes: 87 additions & 0 deletions fuse/plugin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
import strax
import straxen
import numpy as np
import logging

logging.basicConfig(handlers=[logging.StreamHandler()])

class FuseBasePlugin(strax.Plugin):

"""Base plugin for fuse plugins"""

#Forbid rechunking
rechunk_on_save = False

#Lets wait 10 minutes for the plugin to finish
input_timeout = 600

#Config options
debug = straxen.URLConfig(
default=False, type=bool, track=False,
help='Show debug informations',
)

deterministic_seed = straxen.URLConfig(
default=True, type=bool,
help='Set the random seed from lineage and run_id, or pull the seed from the OS.',
)

def setup(self):
dachengx marked this conversation as resolved.
Show resolved Hide resolved
super().setup()
dachengx marked this conversation as resolved.
Show resolved Hide resolved

log = logging.getLogger(f"{self.__class__.__name__}")

if self.debug:
log.setLevel('DEBUG')
log.debug(f"Running {self.__class__.__name__} version {self.__version__} in debug mode")
else:
log.setLevel('INFO')

if self.deterministic_seed:
hash_string = strax.deterministic_hash((self.run_id, self.lineage))
seed = int(hash_string.encode().hex(), 16)
self.rng = np.random.default_rng(seed = seed)
log.debug(f"Generating random numbers from seed {seed}")
else:
self.rng = np.random.default_rng()
log.debug(f"Generating random numbers with seed pulled from OS")

class FuseBaseDownChunkingPlugin(strax.DownChunkingPlugin):
dachengx marked this conversation as resolved.
Show resolved Hide resolved

"""Base plugin for fuse DownChunkingPlugins"""

#Forbid rechunking
rechunk_on_save = False

#Lets wait 10 minutes for the plugin to finish
input_timeout = 600

#Config options
debug = straxen.URLConfig(
default=False, type=bool, track=False,
help='Show debug informations',
)

deterministic_seed = straxen.URLConfig(
default=True, type=bool,
help='Set the random seed from lineage and run_id, or pull the seed from the OS.',
)

def setup(self):

log = logging.getLogger(f"{self.__class__.__name__}")

if self.debug:
log.setLevel('DEBUG')
log.debug(f"Running {self.__class__.__name__} version {self.__version__} in debug mode")
else:
log.setLevel('INFO')

if self.deterministic_seed:
hash_string = strax.deterministic_hash((self.run_id, self.lineage))
seed = int(hash_string.encode().hex(), 16)
self.rng = np.random.default_rng(seed = seed)
log.debug(f"Generating random numbers from seed {seed}")
else:
self.rng = np.random.default_rng()
log.debug(f"Generating random numbers with seed pulled from OS")
36 changes: 4 additions & 32 deletions fuse/plugins/detector_physics/csv_input.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,16 @@
import pandas as pd
import numpy as np

from ...common import dynamic_chunking, FUSE_PLUGIN_TIMEOUT
from ...common import dynamic_chunking
from ...plugin import FuseBasePlugin

export, __all__ = strax.exporter()

logging.basicConfig(handlers=[logging.StreamHandler()])
log = logging.getLogger('fuse.detector_physics.csv_input')

@export
class ChunkCsvInput(strax.Plugin):
class ChunkCsvInput(FuseBasePlugin):
"""
Plugin which reads a CSV file containing instructions for the detector physics simulation
and returns the data in chunks
Expand All @@ -24,13 +25,8 @@ class ChunkCsvInput(strax.Plugin):
provides = "microphysics_summary"
data_kind = "interactions_in_roi"

#Forbid rechunking
rechunk_on_save = False

save_when = strax.SaveWhen.TARGET

input_timeout = FUSE_PLUGIN_TIMEOUT

source_done = False

dtype = [('x', np.float32),
Expand All @@ -48,11 +44,6 @@ class ChunkCsvInput(strax.Plugin):
dtype = dtype + strax.time_fields

#Config options
debug = straxen.URLConfig(
default=False, type=bool, track=False,
help='Show debug informations',
)

input_file = straxen.URLConfig(
track=False,
infer_type=False,
Expand All @@ -76,27 +67,8 @@ class ChunkCsvInput(strax.Plugin):
help='n_interactions_per_chunk',
)

deterministic_seed = straxen.URLConfig(
default=True, type=bool,
help='Set the random seed from lineage and run_id, or pull the seed from the OS.',
)

def setup(self):

if self.debug:
log.setLevel('DEBUG')
log.debug(f"Running ChunkCsvInput version {self.__version__} in debug mode")
else:
log.setLevel('INFO')

if self.deterministic_seed:
hash_string = strax.deterministic_hash((self.run_id, self.lineage))
seed = int(hash_string.encode().hex(), 16)
self.rng = np.random.default_rng(seed = seed)
log.debug(f"Generating random numbers from seed {seed}")
else:
self.rng = np.random.default_rng()
log.debug(f"Generating random numbers with seed pulled from OS")
super().setup()
dachengx marked this conversation as resolved.
Show resolved Hide resolved

self.file_reader = csv_file_loader(
input_file = self.input_file,
Expand Down
36 changes: 4 additions & 32 deletions fuse/plugins/detector_physics/electron_drift.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@
import straxen
import logging

from ...common import FUSE_PLUGIN_TIMEOUT
from ...plugin import FuseBasePlugin

export, __all__ = strax.exporter()

logging.basicConfig(handlers=[logging.StreamHandler()])
log = logging.getLogger('fuse.detector_physics.electron_drift')

@export
class ElectronDrift(strax.Plugin):
class ElectronDrift(FuseBasePlugin):

__version__ = "0.1.4"

Expand All @@ -27,20 +27,11 @@ class ElectronDrift(strax.Plugin):
('z_obs', np.float32),
]
dtype = dtype + strax.time_fields

#Forbid rechunking
rechunk_on_save = False

save_when = strax.SaveWhen.ALWAYS

input_timeout = FUSE_PLUGIN_TIMEOUT

#Config options
debug = straxen.URLConfig(
default=False, type=bool,track=False,
help='Show debug informations',
)


drift_velocity_liquid = straxen.URLConfig(
default = "take://resource://"
"SIMULATION_CONFIG_FILE.json?&fmt=json"
Expand Down Expand Up @@ -131,28 +122,9 @@ class ElectronDrift(strax.Plugin):
cache=True,
help='fdc_map',
)

deterministic_seed = straxen.URLConfig(
default=True, type=bool,
help='Set the random seed from lineage and run_id, or pull the seed from the OS.',
)

def setup(self):

if self.debug:
log.setLevel('DEBUG')
log.debug(f"Running ElectronDrift version {self.__version__} in debug mode")
else:
log.setLevel('INFO')

if self.deterministic_seed:
hash_string = strax.deterministic_hash((self.run_id, self.lineage))
seed = int(hash_string.encode().hex(), 16)
self.rng = np.random.default_rng(seed = seed)
log.debug(f"Generating random numbers from seed {seed}")
else:
self.rng = np.random.default_rng()
log.debug(f"Generating random numbers with seed pulled from OS")
super().setup()

#Can i do this scaling in the url config?
if self.field_distortion_model == "inverse_fdc":
Expand Down
35 changes: 3 additions & 32 deletions fuse/plugins/detector_physics/electron_extraction.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,40 +4,30 @@
import os
import logging

from ...common import FUSE_PLUGIN_TIMEOUT
from ...plugin import FuseBasePlugin

export, __all__ = strax.exporter()

logging.basicConfig(handlers=[logging.StreamHandler()])
log = logging.getLogger('fuse.detector_physics.electron_extraction')

@export
class ElectronExtraction(strax.Plugin):
class ElectronExtraction(FuseBasePlugin):

__version__ = "0.1.3"

depends_on = ("microphysics_summary", "drifted_electrons")
provides = "extracted_electrons"
data_kind = "interactions_in_roi"

#Forbid rechunking
rechunk_on_save = False

save_when = strax.SaveWhen.ALWAYS

input_timeout = FUSE_PLUGIN_TIMEOUT

dtype = [('n_electron_extracted', np.int32),
]

dtype = dtype + strax.time_fields

#Config options
debug = straxen.URLConfig(
default=False, type=bool,track=False,
help='Show debug informations',
)

s2_secondary_sc_gain_mc = straxen.URLConfig(
default = "take://resource://"
"SIMULATION_CONFIG_FILE.json?&fmt=json"
Expand Down Expand Up @@ -100,28 +90,9 @@ class ElectronExtraction(strax.Plugin):
cache=True,
help='Map of the single electron gain',
)

deterministic_seed = straxen.URLConfig(
default=True, type=bool,
help='Set the random seed from lineage and run_id, or pull the seed from the OS.',
)

def setup(self):

if self.debug:
log.setLevel('DEBUG')
log.debug(f"Running ElectronExtraction version {self.__version__} in debug mode")
else:
log.setLevel('INFO')

if self.deterministic_seed:
hash_string = strax.deterministic_hash((self.run_id, self.lineage))
seed = int(hash_string.encode().hex(), 16)
self.rng = np.random.default_rng(seed = seed)
log.debug(f"Generating random numbers from seed {seed}")
else:
self.rng = np.random.default_rng()
log.debug(f"Generating random numbers with seed pulled from OS")
super().setup()

def compute(self, interactions_in_roi):

Expand Down
35 changes: 3 additions & 32 deletions fuse/plugins/detector_physics/electron_timing.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,28 +3,23 @@
import straxen
import logging

from ...common import FUSE_PLUGIN_TIMEOUT
from ...plugin import FuseBasePlugin

export, __all__ = strax.exporter()

logging.basicConfig(handlers=[logging.StreamHandler()])
log = logging.getLogger('fuse.detector_physics.electron_timing')

@export
class ElectronTiming(strax.Plugin):
class ElectronTiming(FuseBasePlugin):

__version__ = "0.1.1"

depends_on = ("drifted_electrons", "extracted_electrons")
provides = "electron_time"
data_kind = "individual_electrons"

#Forbid rechunking
rechunk_on_save = False

save_when = strax.SaveWhen.TARGET

input_timeout = FUSE_PLUGIN_TIMEOUT

data_kind = "individual_electrons"

Expand All @@ -35,11 +30,6 @@ class ElectronTiming(strax.Plugin):
dtype = dtype + strax.time_fields

#Config options
debug = straxen.URLConfig(
default=False, type=bool,track=False,
help='Show debug informations',
)

electron_trapping_time = straxen.URLConfig(
default = "take://resource://"
"SIMULATION_CONFIG_FILE.json?&fmt=json"
Expand All @@ -48,28 +38,9 @@ class ElectronTiming(strax.Plugin):
cache=True,
help='Time scale electrons are trapped at the liquid gas interface',
)

deterministic_seed = straxen.URLConfig(
default=True, type=bool,
help='Set the random seed from lineage and run_id, or pull the seed from the OS.',
)

def setup(self):

if self.debug:
log.setLevel('DEBUG')
log.debug(f"Running ElectronTiming version {self.__version__} in debug mode")
else:
log.setLevel('INFO')

if self.deterministic_seed:
hash_string = strax.deterministic_hash((self.run_id, self.lineage))
seed = int(hash_string.encode().hex(), 16)
self.rng = np.random.default_rng(seed = seed)
log.debug(f"Generating random numbers from seed {seed}")
else:
self.rng = np.random.default_rng()
log.debug(f"Generating random numbers with seed pulled from OS")
super().setup()

def compute(self, interactions_in_roi):

Expand Down
Loading
Loading