From 5cd1b6aa9c1397943f17d17020708eb198e20677 Mon Sep 17 00:00:00 2001 From: Jared Swift <87013792+jswift-stfc@users.noreply.github.com> Date: Mon, 11 Apr 2022 21:12:44 +0100 Subject: [PATCH] Test iterators (#332) * tests: iterate by thickness. * tests: test composition iterator. --- src/gudrun_classes/composition_iterator.py | 22 +++--- tests/test_gudpy_workflows.py | 87 +++++++++++++++++++++- 2 files changed, 96 insertions(+), 13 deletions(-) diff --git a/src/gudrun_classes/composition_iterator.py b/src/gudrun_classes/composition_iterator.py index 9aa6d7e88..16866bea0 100644 --- a/src/gudrun_classes/composition_iterator.py +++ b/src/gudrun_classes/composition_iterator.py @@ -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 @@ -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 = [ @@ -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 ) ) @@ -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 ( @@ -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 ) ) @@ -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. diff --git a/tests/test_gudpy_workflows.py b/tests/test_gudpy_workflows.py index f7419c4b6..412bcf85a 100644 --- a/tests/test_gudpy_workflows.py +++ b/tests/test_gudpy_workflows.py @@ -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 @@ -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):