Skip to content

Commit

Permalink
feat: normalization based on select region (e.g. glass peak)
Browse files Browse the repository at this point in the history
  • Loading branch information
maffettone committed Apr 4, 2024
1 parent 10f9593 commit 7e6d0fc
Showing 1 changed file with 42 additions and 0 deletions.
42 changes: 42 additions & 0 deletions pdf_agents/agents.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import uuid
from abc import ABC
from logging import getLogger
from turtle import back
from typing import Dict, List, Optional, Sequence, Tuple, Union

import nslsii.kafka_utils
Expand Down Expand Up @@ -29,6 +30,7 @@ def __init__(
data_key: str = "chi_I",
roi_key: str = "chi_Q",
roi: Optional[Tuple] = None,
norm_region: Optional[Tuple] = None,
offline=False,
**kwargs,
):
Expand All @@ -39,6 +41,7 @@ def __init__(
self._data_key = data_key
self._roi_key = roi_key
self._roi = roi
self._norm_region = norm_region
# Attributes pulled in from Redis
self._exposure = float(self._rkvs.get("PDF:desired_exposure_time").decode("utf-8"))
self._sample_number = int(self._rkvs.get("PDF:xpdacq:sample_number").decode("utf-8"))
Expand Down Expand Up @@ -90,6 +93,35 @@ def unpack_run(self, run) -> Tuple[Union[float, ArrayLike], Union[float, ArrayLi
y = run.primary.data[self.data_key].read().flatten()
if self.background is not None:
y = y - self.background[1]

if self.norm_region is not None:
ordinate = np.array(run.primary.data[self.roi_key]).flatten()
idx_min = (
np.where(ordinate < self.norm_region[0])[0][-1]
if len(np.where(ordinate < self.norm_region[0])[0])
else None
)
idx_max = (
np.where(ordinate > self.norm_region[1])[0][-1]
if len(np.where(ordinate > self.norm_region[1])[0])
else None
)
bkg_idx_min = (
np.where(self.background[0] < self.norm_region[0])[0][-1]
if len(np.where(self.background[0] < self.norm_region[0])[0])
else None
)
bkg_idx_max = (
np.where(self.background[0] > self.norm_region[1])[0][-1]
if len(np.where(self.background[0] > self.norm_region[1])[0])
else None
)
scale_factor = np.sum(self.background[1][bkg_idx_min:bkg_idx_max]) / np.sum(y[idx_min:idx_max])
else:
scale_factor = 1

y = y * scale_factor

if self.roi is not None:
ordinate = np.array(run.primary.data[self.roi_key]).flatten()
idx_min = np.where(ordinate < self.roi[0])[0][-1] if len(np.where(ordinate < self.roi[0])[0]) else None
Expand All @@ -115,6 +147,7 @@ def server_registrations(self) -> None:
self._register_property("roi_key")
self._register_property("roi")
self._register_property("background")
self._register_property("norm_region")
return super().server_registrations()

@property
Expand Down Expand Up @@ -217,6 +250,15 @@ def roi(self, value: Tuple[float, float]):
self._roi = value
self.close_and_restart(clear_tell_cache=True)

@property
def norm_region(self):
return self._norm_region

@norm_region.setter
def norm_region(self, value: Tuple[float, float]):
self._norm_region = value
self.close_and_restart(clear_tell_cache=True)

@staticmethod
def get_beamline_objects() -> dict:
beamline_tla = "pdf"
Expand Down

0 comments on commit 7e6d0fc

Please sign in to comment.