Skip to content
Snippets Groups Projects
Commit f7c2c71e authored by David Hammer's avatar David Hammer
Browse files

Merge branch 'improve-integrated-intensity-addon' into 'master'

Improve integrated intensity addon

See merge request !58
parents 4d477e94 61257760
No related branches found
No related tags found
1 merge request!58Improve integrated intensity addon
import numpy as np
from karabo.bound import NDARRAY_ELEMENT
from karabo.bound import DOUBLE_ELEMENT, NDARRAY_ELEMENT, NODE_ELEMENT
from .base_addon import BaseCorrectionAddon
......@@ -13,18 +13,73 @@ def maybe_get(a):
class IntegratedIntensityAddon(BaseCorrectionAddon):
_node_name = "integratedIntensity"
def __init__(self, config):
global cupy
import cupy
self._vmin = config["valueMin"]
self._vmax = config["valueMax"]
def reconfigure(self, changed_config):
if changed_config.has("valueMin"):
self._vmin = changed_config["valueMin"]
if changed_config.has("valueMax"):
self._vmax = changed_config["valueMax"]
@staticmethod
def extend_output_schema(schema):
# note: sort of assumes XTDF hash structure
(
NODE_ELEMENT(schema)
.key("integratedIntensity")
.commit(),
NDARRAY_ELEMENT(schema)
.key("integratedIntensity.mean")
.dtype("DOUBLE")
.commit(),
NDARRAY_ELEMENT(schema)
.key("image.integratedIntensity")
.key("integratedIntensity.variance")
.dtype("DOUBLE")
.commit(),
NDARRAY_ELEMENT(schema)
.key("integratedIntensity.count")
.dtype("UINT64")
.commit()
)
def post_correction(self, processed_data, cell_table, pulse_table, output_hash):
# Numpy should handle CuPy dispatch for us
output_hash["image.integratedIntensity"] = maybe_get(
np.nansum(processed_data, axis=(1, 2))
@staticmethod
def extend_device_schema(schema, prefix):
(
DOUBLE_ELEMENT(schema)
.key(f"{prefix}.valueMin")
.tags("managed")
.assignmentOptional()
.defaultValue(-10.0)
.reconfigurable()
.commit(),
DOUBLE_ELEMENT(schema)
.key(f"{prefix}.valueMax")
.tags("managed")
.assignmentOptional()
.defaultValue(20000.0)
.reconfigurable()
.commit()
)
def post_correction(self, data, cell_table, pulse_table, output_hash):
# Numpy should handle CuPy dispatch for us
mask = np.isfinite(data) & (self._vmin < data) & (data < self._vmax)
count = np.sum(mask, axis=(1, 2))
tmp = mask * data
mu = np.sum(tmp, axis=(1, 2)) / count
var = np.sum(tmp * data, axis=(1, 2)) / count - mu * mu
output_hash["integratedIntensity.mean"] = maybe_get(mu)
output_hash["integratedIntensity.variance"] = maybe_get(var)
output_hash["integratedIntensity.count"] = maybe_get(count)
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment