Skip to content

Commit

Permalink
Merge pull request #1 from ggggggggg/ebit_cal
Browse files Browse the repository at this point in the history
Ebit cal
  • Loading branch information
ggggggggg authored Jul 25, 2024
2 parents 803e759 + 740e5cc commit 99af165
Show file tree
Hide file tree
Showing 11 changed files with 698 additions and 469 deletions.
39 changes: 39 additions & 0 deletions moss/blah.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import polars as pl
import pylab as plt
import numpy as np
import marimo as mo
import moss

import mass
import os
massroot = os.path.split(os.path.split(mass.__file__)[0])[0]

noise_folder = os.path.join(massroot,"tests","ljh_files","20230626","0000")
pulse_folder = os.path.join(massroot,"tests","ljh_files","20230626","0001")
data = moss.Channels.from_ljh_folder(
pulse_folder=pulse_folder, noise_folder=noise_folder
)
data2 = data.transform_channels(
lambda channel: channel.summarize_pulses()
.with_good_expr_pretrig_mean_and_postpeak_deriv()
.rough_cal(
["MnKAlpha", "MnKBeta", "CuKAlpha", "CuKBeta", "PdLAlpha", "PdLBeta"],
uncalibrated_col="peak_value",
calibrated_col="energy_peak_value",
ph_smoothing_fwhm=50,
)
.filter5lag(f_3db=10e3)
.rough_cal(
["MnKAlpha", "MnKBeta", "CuKAlpha", "CuKBeta", "PdLAlpha", "PdLBeta"],
uncalibrated_col="5lagy",
calibrated_col="energy_5lagy",
ph_smoothing_fwhm=50,
)
.driftcorrect()
.rough_cal(
["MnKAlpha", "MnKBeta", "CuKAlpha", "CuKBeta", "PdLAlpha", "PdLBeta"],
uncalibrated_col="5lagy_dc",
calibrated_col="energy_5lagy_dc",
ph_smoothing_fwhm=50,
)
)
14 changes: 14 additions & 0 deletions moss/broken_notebook.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import marimo

__generated_with = "0.7.11"
app = marimo.App(width="medium")


@app.cell
def __():
raise Exception()
return


if __name__ == "__main__":
app.run()
17 changes: 9 additions & 8 deletions moss/channel.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,8 @@ def plot_hist(self, col, bin_edges, axis=None):

def good_series(self, col, use_expr):
return moss.good_series(self.df, col, self.good_expr, use_expr)

def rough_cal_gain(
def rough_gain_cal(
self, line_names, uncalibrated_col, calibrated_col, ph_smoothing_fwhm,
use_expr=True
):
Expand All @@ -74,26 +74,26 @@ def rough_cal_gain(
gain = opt_assignments / energies_out
gain_pfit = np.polynomial.Polynomial.fit(opt_assignments, gain, deg=2)

def rough_cal_f(uncalibrated):
def ph2energy(uncalibrated):
calibrated = uncalibrated / gain_pfit(uncalibrated)
return calibrated

predicted_energies = rough_cal_f(np.array(opt_assignments))
predicted_energies = ph2energy(np.array(opt_assignments))
# energy_residuals = predicted_energies - energies_out
# if any(np.abs(energy_residuals) > max_residual_ev):
# raise Exception(f"too large residuals: {energy_residuals=} eV")
step = moss.RoughCalibrationGainStep(
[uncalibrated_col],
calibrated_col,
[calibrated_col],
self.good_expr,
rough_cal_f,
use_expr=use_expr,
line_names=name_e,
line_energies=energies_out,
predicted_energies=predicted_energies,
gain_pfit = gain_pfit
ph2energy = ph2energy
)
return self.with_step(step)

def rough_cal(
self, line_names, uncalibrated_col, calibrated_col, ph_smoothing_fwhm,
use_expr=True
Expand Down Expand Up @@ -336,6 +336,7 @@ def multifit_spline_cal(
previous_cal_step = self.steps[previous_cal_step_index]
rough_energy_col = previous_cal_step.output[0]
uncalibrated_col = previous_cal_step.inputs[0]

fits_with_results = multifit.fit_ch(self, col=rough_energy_col)
multifit_df = fits_with_results.results_params_as_df()
peaks_in_energy_rough_cal = multifit_df["peak_ph"].to_numpy()
Expand Down
102 changes: 24 additions & 78 deletions moss/example_marimo.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import marimo

__generated_with = "0.7.9"
__generated_with = "0.7.11"
app = marimo.App(width="medium", app_title="MOSS intro")


Expand All @@ -24,7 +24,8 @@ def __():
import pylab as plt
import numpy as np
import marimo as mo
return mo, np, pl, plt
import pulsedata
return mo, np, pl, plt, pulsedata


@app.cell
Expand All @@ -33,69 +34,6 @@ def __():
return moss,


@app.cell(disabled=True)
def __(mo):
file_browser = mo.ui.file_browser(
initial_path=r"C:\Users\oneilg\Desktop\python\src\mass\tests\ljh_files",
multiple=True,
label="pick microcal data directory",
selection_mode="directory",
)
mo.md(
f"# File Picker\npick microcal data directory\nthe top line should be the folder that contains all the folders you want to search for data\n{file_browser}"
)
return file_browser,


@app.cell
def __(massroot, mo, moss, pl):
_root_path = massroot
_extensions = ["ljh"]
_folders = moss.ljhutil.find_folders_with_extension(_root_path, _extensions)
_df = pl.DataFrame({"folder": _folders})
pulse_table = mo.ui.table(_df, label="puck pulse files")
noise_table = mo.ui.table(_df, label="pick noise file", selection="single")
start_button = mo.ui.run_button(label="submit file choices")
mo.md(
f"#File Picker\nMarimo suports very easy reactive UI. You should read the docs. The you can can try it out here if you want. You can edit the line defining `_root_path` to point to a folder with LJH files, and the above ui element will update. Then pick one noise file and one pulse file and the rest of the notebook will update. Though the calibration will likely break since the actual lines chosen will be wrong. So maybe come back here later.\npick files for noise \n{noise_table}\n#pick files for pulses \n{pulse_table}\nthen click {start_button}"
)
return noise_table, pulse_table, start_button


@app.cell
def __(mo):
mo.md(
"""
# file picker overide
below here we manually define some default ljh files for this notebook to work on, but if you use the file picker it will override the default values.
"""
)
return


@app.cell
def __():
import mass
import os
massroot = os.path.split(os.path.split(mass.__file__)[0])[0]
massroot
return mass, massroot, os


@app.cell
def __(massroot, noise_table, os, pulse_table):
if len(noise_table.value) == 1 and len(pulse_table.value) >= 1:
noise_folder = noise_table.value["folder"][0]
pulse_folders = pulse_table.value["folder"]
pulse_folder = pulse_folders[0]
else:
noise_folder = os.path.join(massroot,"tests","ljh_files","20230626","0000")
pulse_folder = os.path.join(massroot,"tests","ljh_files","20230626","0001")


return noise_folder, pulse_folder, pulse_folders


@app.cell
def __(mo):
mo.md(
Expand All @@ -108,9 +46,10 @@ def __(mo):


@app.cell
def __(moss, noise_folder, pulse_folder):
def __(moss, pulsedata):
_p = pulsedata.pulse_noise_ljh_pairs["20230626"]
data = moss.Channels.from_ljh_folder(
pulse_folder=pulse_folder, noise_folder=noise_folder
pulse_folder=_p.pulse_folder, noise_folder=_p.noise_folder
)
data
return data,
Expand Down Expand Up @@ -228,7 +167,7 @@ def __(result):

@app.cell
def __(mo):
mo.md(r"# plot a noise spectrum")
mo.md(r"""# plot a noise spectrum""")
return


Expand Down Expand Up @@ -292,7 +231,7 @@ def __(ch, mo, steps):

@app.cell
def __(mo):
mo.md("# make sure the results are the same!")
mo.md("""# make sure the results are the same!""")
return


Expand All @@ -306,6 +245,7 @@ def __(ch2, data2):
def __(ch2):
# to help you remember not to mutate, everything is as immutable as python will allow! assignment throws!
from dataclasses import FrozenInstanceError

try:
ch2.df = 4
except FrozenInstanceError:
Expand Down Expand Up @@ -333,7 +273,7 @@ def __(data, data2, mo, np):

@app.cell
def __(mo):
mo.md("# step plots")
mo.md("""# step plots""")
return


Expand Down Expand Up @@ -492,7 +432,11 @@ def __(multifit_with_results):

@app.cell
def __(data3, multifit):
data4= data3.transform_channels(lambda ch: ch.multifit_spline_cal(multifit, previous_cal_step_index=5, calibrated_col="energy2_5lagy_dc"))
data4 = data3.transform_channels(
lambda ch: ch.multifit_spline_cal(
multifit, previous_cal_step_index=5, calibrated_col="energy2_5lagy_dc"
)
)
return data4,


Expand All @@ -505,7 +449,7 @@ def __(data4, mo, plt):

@app.cell
def __(data4):
steps_dict = {ch_num:ch.steps for ch_num, ch in data4.channels.items()}
steps_dict = {ch_num: ch.steps for ch_num, ch in data4.channels.items()}
return steps_dict,


Expand All @@ -517,7 +461,9 @@ def __(moss, steps_dict):

@app.cell
def __(data4):
data4.dfg().select("timestamp", "energy2_5lagy_dc", "state_label", "ch_num").write_parquet("example_result.parquet")
data4.dfg().select(
"timestamp", "energy2_5lagy_dc", "state_label", "ch_num"
).write_parquet("example_result.parquet")
return


Expand All @@ -536,35 +482,35 @@ def __(mo):
def __(ch):
# here we concatenate two channels and check that the length has double
ch_concat = ch.concat_ch(ch)
assert 2*len(ch.df) == len(ch_concat.df)
assert 2 * len(ch.df) == len(ch_concat.df)
return ch_concat,


@app.cell
def __(data4):
# here we concatenate two `Channels` objects and check that the length of the resulting dfg (remember, this is the df of good pulses) has doubled
data_concat = data4.concat_data(data4)
assert 2*len(data4.dfg())==len(data_concat.dfg())
assert 2 * len(data4.dfg()) == len(data_concat.dfg())
return data_concat,


@app.cell
def __(mo):
mo.md(r"# final coadded spectrum")
mo.md(r"""# final coadded spectrum""")
return


@app.cell
def __(data4, mo, plt):
_result = data4.linefit("MnKAlpha",col="energy2_5lagy_dc")
_result = data4.linefit("MnKAlpha", col="energy2_5lagy_dc")
_result.plotm()
mo.mpl.interactive(plt.gcf())
return


@app.cell
def __(data2):
ch6=data2.channels[4102]
ch6 = data2.channels[4102]
return ch6,


Expand Down
Loading

0 comments on commit 99af165

Please sign in to comment.