Skip to content

Commit

Permalink
Test iterators (#332)
Browse files Browse the repository at this point in the history
* tests: iterate by thickness.

* tests: test composition iterator.
  • Loading branch information
jswift-stfc authored Apr 11, 2022
1 parent 525fd2b commit 5cd1b6a
Show file tree
Hide file tree
Showing 2 changed files with 96 additions and 13 deletions.
22 changes: 10 additions & 12 deletions src/gudrun_classes/composition_iterator.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ class CompositionIterator():
Performs n iterations using cost function f, args and bounds.
"""
def __init__(self, gudrunFile):
self.gudrunFile = deepcopy(gudrunFile)
self.gudrunFile = gudrunFile
self.components = []
self.ratio = 0

Expand Down Expand Up @@ -133,8 +133,7 @@ def setComponents(self, components, ratio=1):
Target Sample Background.
"""
def processSingleComponent(self, x, sampleBackground):
gudrunFile = deepcopy(self.gudrunFile)
gudrunFile.sampleBackgrounds = [sampleBackground]
self.gudrunFile.sampleBackgrounds = [sampleBackground]

x = abs(x)
weightedComponents = [
Expand All @@ -148,16 +147,16 @@ def processSingleComponent(self, x, sampleBackground):
component.ratio = x

sampleBackground.samples[0].composition.translate()
gudrunFile.process()
self.gudrunFile.process()

time.sleep(1)
gudPath = sampleBackground.samples[0].dataFiles.dataFiles[0].replace(
gudrunFile.instrument.dataFileType,
self.gudrunFile.instrument.dataFileType,
"gud"
)
gudFile = GudFile(
os.path.join(
gudrunFile.instrument.GudrunInputFileDir, gudPath
self.gudrunFile.instrument.GudrunInputFileDir, gudPath
)
)

Expand All @@ -179,8 +178,7 @@ def processSingleComponent(self, x, sampleBackground):
Sum of molecules of both components.
"""
def processTwoComponents(self, x, sampleBackground, totalMolecules):
gudrunFile = deepcopy(self.gudrunFile)
gudrunFile.sampleBackgrounds = [sampleBackground]
self.gudrunFile.sampleBackgrounds = [sampleBackground]
x = abs(x)
wcA = wcB = None
for weightedComponent in (
Expand All @@ -196,16 +194,16 @@ def processTwoComponents(self, x, sampleBackground, totalMolecules):
wcB.ratio = abs(totalMolecules - x)

sampleBackground.samples[0].composition.translate()
gudrunFile.process()
self.gudrunFile.process()

time.sleep(1)
gudPath = sampleBackground.samples[0].dataFiles.dataFiles[0].replace(
gudrunFile.instrument.dataFileType,
self.gudrunFile.instrument.dataFileType,
"gud"
)
gudFile = GudFile(
os.path.join(
gudrunFile.instrument.GudrunInputFileDir, gudPath
self.gudrunFile.instrument.GudrunInputFileDir, gudPath
)
)

Expand All @@ -232,7 +230,7 @@ def processTwoComponents(self, x, sampleBackground, totalMolecules):
rtol : float
Relative tolerance
"""
def iterate(self, n=10, rtol=10):
def iterate(self, n=10, rtol=10.):
if not self.components or not self.ratio:
return None
# Only include samples that are marked for analysis.
Expand Down
87 changes: 86 additions & 1 deletion tests/test_gudpy_workflows.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
from copy import deepcopy
import os
from shutil import copyfile
from unittest import TestCase
import re
import math

from src.gudrun_classes.composition import Composition, Component
from src.gudrun_classes.gudrun_file import GudrunFile
from src.gudrun_classes.thickness_iterator import ThicknessIterator
from src.gudrun_classes.density_iterator import DensityIterator
from src.gudrun_classes.tweak_factor_iterator import TweakFactorIterator
from src.gudrun_classes.composition_iterator import CompositionIterator
from src.gudrun_classes.gud_file import GudFile
from src.gudrun_classes.wavelength_subtraction_iterator import (
WavelengthSubtractionIterator
Expand Down Expand Up @@ -281,6 +283,89 @@ def testGudPyIterateByDensity(self):
dcsLevelPercentage = float(dcsLevelPercentage.replace('%', ''))
self.assertAlmostEqual(dcsLevelPercentage, 100.0, 0)

def testIterateByThickness(self):

self.g.purge()
thicknessIterator = ThicknessIterator(self.g)
thicknessIterator.iterate(5)

gfPath = self.g.sampleBackgrounds[0].samples[0].dataFiles.dataFiles[0]
gfPath = gfPath.replace(self.g.instrument.dataFileType, 'gud')
gf1 = GudFile(
os.path.join(
self.g.instrument.GudrunInputFileDir, gfPath
)
)
dcsLevelPercentage = re.findall(r'\d*[.]?\d*%', gf1.result)[0]
dcsLevelPercentage = float(dcsLevelPercentage.replace('%', ''))
self.assertAlmostEqual(dcsLevelPercentage, 100.0, 0)

gfPath = self.g.sampleBackgrounds[0].samples[1].dataFiles.dataFiles[0]
gfPath = gfPath.replace(self.g.instrument.dataFileType, 'gud')
gf2 = GudFile(
os.path.join(
self.g.instrument.GudrunInputFileDir, gfPath
)
)
dcsLevelPercentage = re.findall(r'\d*[.]?\d*%', gf2.result)[0]
dcsLevelPercentage = float(dcsLevelPercentage.replace('%', ''))
self.assertAlmostEqual(dcsLevelPercentage, 100.0, 0)

gfPath = self.g.sampleBackgrounds[0].samples[2].dataFiles.dataFiles[0]
gfPath = gfPath.replace(self.g.instrument.dataFileType, 'gud')
gf3 = GudFile(
os.path.join(
self.g.instrument.GudrunInputFileDir, gfPath
)
)
dcsLevelPercentage = re.findall(r'\d*[.]?\d*%', gf3.result)[0]
dcsLevelPercentage = float(dcsLevelPercentage.replace('%', ''))
self.assertAlmostEqual(dcsLevelPercentage, 100.0, 0)

gfPath = self.g.sampleBackgrounds[0].samples[3].dataFiles.dataFiles[0]
gfPath = gfPath.replace(self.g.instrument.dataFileType, 'gud')
gf4 = GudFile(
os.path.join(
self.g.instrument.GudrunInputFileDir, gfPath
)
)
dcsLevelPercentage = re.findall(r'\d*[.]?\d*%', gf4.result)[0]
dcsLevelPercentage = float(dcsLevelPercentage.replace('%', ''))
self.assertAlmostEqual(dcsLevelPercentage, 100.0, 0)

def testIterateByComposition(self):

g = deepcopy(self.g)
g.purge()

g.sampleBackgrounds[0].samples[0].runThisSample = False
g.sampleBackgrounds[0].samples[2].runThisSample = False
g.sampleBackgrounds[0].samples[3].runThisSample = False
sample = g.sampleBackgrounds[0].samples[1]

composition = Composition("Sample")

h2 = Component("H[2]")
h2.parse()
o = Component("O")
o.parse()

composition.addComponent(h2, 1)
composition.addComponent(o, 1)

sample.composition = composition

compositionIterator = CompositionIterator(g)
compositionIterator.setComponent(h2, 1)
compositionIterator.iterate(10, 3)
self.assertAlmostEqual(
sample.composition.weightedComponents[0].ratio, 2, 1
)

self.assertEqual(
sample.composition.weightedComponents[1].ratio, 1
)

def testGudPyIterateBySubtractingWavelength(self):

for i in range(1, 4):
Expand Down

0 comments on commit 5cd1b6a

Please sign in to comment.