Skip to content

Ewm7122 implement artificial normalization algo #460

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

Merged
merged 9 commits into from
Sep 27, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
2 changes: 2 additions & 0 deletions .github/workflows/actions.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ jobs:
- name: Set up MicroMamaba
uses: mamba-org/setup-micromamba@v1
with:
micromamba-version: '1.5.10-0'
environment-file: environment.yml
condarc: |
channels:
Expand Down Expand Up @@ -67,6 +68,7 @@ jobs:
- name: Set up MicroMamaba
uses: mamba-org/setup-micromamba@v1
with:
micromamba-version: '1.5.10-0'
environment-file: environment.yml
condarc: |
channels:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
from pydantic import BaseModel

from snapred.meta.Config import Config


class ArtificialNormalizationIngredients(BaseModel):
"""
Class to hold ingredients for the creation of artificial normalization data.
"""

peakWindowClippingSize: int = Config["constants.ArtificialNormalization.peakWindowClippingSize"]
smoothingParameter: float
decreaseParameter: bool = True
lss: bool = True
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
import json

import numpy as np
from mantid.api import (
AlgorithmFactory,
MatrixWorkspaceProperty,
PropertyMode,
PythonAlgorithm,
WorkspaceUnitValidator,
)
from mantid.kernel import Direction

from snapred.backend.log.logger import snapredLogger
from snapred.backend.recipe.algorithm.MantidSnapper import MantidSnapper

logger = snapredLogger.getLogger(__name__)


class CreateArtificialNormalizationAlgo(PythonAlgorithm):
def category(self):
return "SNAPRed Data Processing"

def PyInit(self):
self.declareProperty(
MatrixWorkspaceProperty(
"InputWorkspace",
"",
Direction.Input,
PropertyMode.Mandatory,
validator=WorkspaceUnitValidator("dSpacing"),
),
doc="Workspace that contains calibrated and focused diffraction data.",
)
self.declareProperty(
MatrixWorkspaceProperty(
"OutputWorkspace",
"",
Direction.Output,
PropertyMode.Mandatory,
validator=WorkspaceUnitValidator("dSpacing"),
),
doc="Workspace that contains artificial normalization.",
)
self.declareProperty(
"Ingredients",
defaultValue="",
direction=Direction.Input,
)
self.setRethrows(True)
self.mantidSnapper = MantidSnapper(self, __name__)

def chopInredients(self, ingredientsStr: str):
ingredientsDict = json.loads(ingredientsStr)
self.peakWindowClippingSize = ingredientsDict["peakWindowClippingSize"]
self.smoothingParameter = ingredientsDict["smoothingParameter"]
self.decreaseParameter = ingredientsDict["decreaseParameter"]
self.LSS = ingredientsDict["lss"]

def unbagGroceries(self):
self.inputWorkspaceName = self.getPropertyValue("InputWorkspace")
self.outputWorkspaceName = self.getPropertyValue("OutputWorkspace")

def peakClip(self, data, winSize: int, decrese: bool, LLS: bool, smoothing: float):
# Clipping peaks from the data with optional smoothing and transformations
startData = np.copy(data)
window = winSize
if smoothing > 0:
data = self.smooth(data, smoothing)

if LLS:
data = self.LLSTransformation(data)

temp = data.copy()
scan = list(range(window + 1, 0, -1)) if decrese else list(range(1, window + 1))

for w in scan:
for i in range(len(temp)):
if i < w or i > (len(temp) - w - 1):
continue
winArray = temp[i - w : i + w + 1].copy()
winArrayReversed = winArray[::-1]
average = (winArray + winArrayReversed) / 2
temp[i] = np.min(average[: int(len(average) / 2)])

if LLS:
temp = self.InvLLSTransformation(temp)

index = np.where((startData - temp) == min(startData - temp))[0][0]
output = temp * (startData[index] / temp[index])
return output

def smooth(self, data, order):
# Applies smoothing to the data
sm = np.zeros(len(data))
factor = order / 2 + 1
for i in range(len(data)):
temp = 0
ave = 0
for r in range(max(0, i - int(order / 2)), min(i + int(order / 2), len(data) - 1) + 1):
temp += (factor - abs(r - i)) * data[r]
ave += factor - abs(r - i)
sm[i] = temp / ave
return sm

def LLSTransformation(self, input): # noqa: A002
# Applies LLS transformation to emphasize weaker peaks
return np.log(np.log((input + 1) ** 0.5 + 1) + 1)

def InvLLSTransformation(self, input): # noqa: A002
# Reverts LLS transformation
return (np.exp(np.exp(input) - 1) - 1) ** 2 - 1

def PyExec(self):
# Main execution method for the algorithm
self.unbagGroceries()
ingredients = self.getProperty("Ingredients").value
self.chopInredients(ingredients)
self.mantidSnapper.CloneWorkspace(
"Cloning input workspace...",
InputWorkspace=self.inputWorkspaceName,
OutputWorkspace=self.outputWorkspaceName,
)
self.mantidSnapper.executeQueue()
self.inputWorkspace = self.mantidSnapper.mtd[self.inputWorkspaceName]
self.outputWorkspace = self.mantidSnapper.mtd[self.outputWorkspaceName]

# Apply peak clipping to each histogram in the workspace
for i in range(self.outputWorkspace.getNumberHistograms()):
dataY = self.outputWorkspace.readY(i)
clippedData = self.peakClip(
data=dataY,
winSize=self.peakWindowClippingSize,
decrese=self.decreaseParameter,
LLS=self.LSS,
smoothing=self.smoothingParameter,
)
self.outputWorkspace.setY(i, clippedData)

# Set the output workspace property
self.setProperty("OutputWorkspace", self.outputWorkspaceName)


# Register algorithm with Mantid
AlgorithmFactory.subscribe(CreateArtificialNormalizationAlgo)
3 changes: 3 additions & 0 deletions src/snapred/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,9 @@ constants:
crystalDMin: 0.4
crystalDMax: 100.0

ArtificialNormalization:
peakWindowClippingSize: 10

CalibrationReduction:
tofMin: 2000
tofMax: 14500
Expand Down
Loading