Skip to content

Commit

Permalink
Merge pull request #79 from iguinn/main
Browse files Browse the repository at this point in the history
Change unit conversion to work with inverse units (e.g. frequencies)
  • Loading branch information
iguinn authored Jul 22, 2024
2 parents 5c118b0 + 641e1cb commit 30babe7
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 8 deletions.
18 changes: 10 additions & 8 deletions src/dspeed/processing_chain.py
Original file line number Diff line number Diff line change
Expand Up @@ -1465,15 +1465,21 @@ def __init__(
if isinstance(param, (Quantity, Unit)):
if ureg.is_compatible_with(ureg.dimensionless, param):
param = param.to(ureg.dimensionless).magnitude
elif not isinstance(
grid, CoordinateGrid
) or not ureg.is_compatible_with(grid.period.u, param):
elif not isinstance(grid, CoordinateGrid):
raise ProcessingChainError(
f"could not find valid conversion for {param}; "
f"CoordinateGrid is {grid}"
)
else:
param = (param / grid.period).to(ureg.dimensionless).magnitude
# This lets us convert powers of unit
pi = ureg.pi_theorem({0: grid.period, 1: param})
if not pi:
raise ProcessingChainError(
f"could not find valid conversion for {param}; "
f"CoordinateGrid is {grid}"
)
param = param * grid.period ** (pi[0][0] / pi[0][1])
param = param.to(ureg.dimensionless).magnitude
if np.issubdtype(dtype, np.integer):
param = dtype.type(np.round(param))
else:
Expand Down Expand Up @@ -2128,10 +2134,6 @@ def build_processing_chain(
# parse the arguments list for prereqs, if not included explicitly
if "prereqs" not in node:
prereqs = []
if "args" in node:
args = node["args"]
else:
args = [node["function"]]

for arg in args:
if not isinstance(arg, str):
Expand Down
2 changes: 2 additions & 0 deletions src/dspeed/processors/inject_ringing.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ def inject_damped_oscillation(
time constant of decay
omega:
angular frequency of oscillation
phase:
phase shift of oscillation
frac
fraction of amplitude in injected pole
w_out
Expand Down
33 changes: 33 additions & 0 deletions tests/test_processing_chain.py
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,39 @@ def test_processor_variable_array_output(spms_raw_tbl):
proc_chain.execute(0, 1)


def test_proc_chain_unit_conversion(spms_raw_tbl):
dsp_config = {
"outputs": ["a_unitless", "a_ns", "a_us", "a_ghz"],
"processors": {
"a_unitless": {
"function": "fixed_time_pickoff",
"module": "dspeed.processors",
"args": ["waveform", 100, "'n'", "a_unitless"],
},
"a_ns": {
"function": "fixed_time_pickoff",
"module": "dspeed.processors",
"args": ["waveform", "1600*ns", "'n'", "a_ns"],
},
"a_us": {
"function": "fixed_time_pickoff",
"module": "dspeed.processors",
"args": ["waveform", "1.6*us", "'n'", "a_us"],
},
"a_ghz": { # note this doesn't really make sense, but I want to test if it will convert inverse units
"function": "fixed_time_pickoff",
"module": "dspeed.processors",
"args": ["waveform", "6.25*GHz", "'n'", "a_ghz"],
},
},
}
proc_chain, _, lh5_out = build_processing_chain(spms_raw_tbl, dsp_config)
proc_chain.execute(0, 1)
assert lh5_out["a_unitless"][0] == lh5_out["a_ns"][0]
assert lh5_out["a_unitless"][0] == lh5_out["a_us"][0]
assert lh5_out["a_unitless"][0] == lh5_out["a_ghz"][0]


# Test that timing variables can be converted between multiple coordinate
# grids correctly. Also tests slicing with a stride. Pickoff a time from
# a windowed wf and a down-sampled waveform; they should be the same
Expand Down

0 comments on commit 30babe7

Please sign in to comment.