Skip to content

Commit

Permalink
Finalise optimisation
Browse files Browse the repository at this point in the history
  • Loading branch information
rhinoella committed Jun 28, 2024
1 parent 6dd9fe7 commit 4839629
Show file tree
Hide file tree
Showing 11 changed files with 214 additions and 130 deletions.
12 changes: 0 additions & 12 deletions gudpy/core/data_files.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,10 @@ def gudFile(self):

@property
def mintFile(self):
if not self.isSampleDataFile:
return None
return self._outputs.get(".mint01", None)

@property
def mdcsFile(self):
if not self.isSampleDataFile:
return None
mdcsFile = self._outputs.get(".mdcs01", None)
if mdcsFile:
return mdcsFile
Expand All @@ -72,26 +68,18 @@ def mdcsFile(self):

@property
def msubwFile(self):
if not self.isSampleDataFile:
return None
return self._outputs.get(".msubw01", None)

@property
def mcdsFile(self):
if not self.isSampleDataFile:
return None
return self._outputs.get(".mcds01", None)

@property
def mdorFile(self):
if not self.isSampleDataFile:
return None
return self._outputs.get(".mdor01", None)

@property
def mgorFile(self):
if not self.isSampleDataFile:
return None
return self._outputs.get(".mgor01", None)


Expand Down
4 changes: 2 additions & 2 deletions gudpy/core/gudpy.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ def __init__(

self.purge: Purge = None
self.gudrun: Gudrun = None
self.runModes: RunModes = None
self.runModes: RunModes = RunModes()
self.gudrunIterator: GudrunIterator = None
self.batchProcessor: BatchProcessing = None
self.optimiser = None
Expand Down Expand Up @@ -713,7 +713,7 @@ def convertContainersToSample(self, gudrunFile: GudrunFile):
containersAsSamples.append(
container.convertToSample()
)
newGudrunFile.sampleBackground.samples = containersAsSamples
sampleBackground.samples = containersAsSamples
return newGudrunFile

def partition(self, gudrunFile):
Expand Down
3 changes: 3 additions & 0 deletions gudpy/core/gudrun_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,9 @@ def samples(self):
s for sb in self.sampleBackgrounds
for s in sb.samples]

def containers(self):
return [c for s in self.samples() for c in s.containers]

def checkNormDataFiles(self):
return (len(self.normalisation.dataFiles)
and len(self.normalisation.dataFilesBg))
Expand Down
1 change: 0 additions & 1 deletion gudpy/core/iterators.py
Original file line number Diff line number Diff line change
Expand Up @@ -489,7 +489,6 @@ def setSelfScatteringFiles(self, scale, gudrunFile):
if self.iterationCount == 0:
return
for sample in gudrunFile.runSamples():
print(sample.name)
if len(sample.dataFiles):
if scale == Scales.Q:
targetFile = sample.dataFiles[0].msubwFile
Expand Down
31 changes: 23 additions & 8 deletions gudpy/core/optimise.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ def __init__(

self.mseRec = {}

for sample in self.gudrunIterator.gudrunFile.samples():
for sample in self.gudrunFile.samples():
sample.runThisSample = False
if sample.name in [s.name for s in samples]:
sample.runThisSample = True
Expand All @@ -42,13 +42,17 @@ def __init__(
self.mse[sample.name]["initial"] = copy.deepcopy(sample)
self.results[sample.name] = {}

@property
def gudrunFile(self):
return self.gudrunIterator.gudrunFile

def optimise(self):
projectDir = gudpy_io.GudPyIO.projectDir
gudpy_io.GudPyIO.projectDir = os.path.join(projectDir, "Optimise")

for gudrun in self.gudrunIterator.gudrunObjects:
exitcode, error = self.gudrunIterator.singleIteration(
self.gudrunIterator.gudrunFile, gudrun, self.purge)
self.gudrunFile, gudrun, self.purge)
self.exitcode = exitcode
if exitcode: # An exit code != 0 indicates failure
self.error = error
Expand All @@ -62,7 +66,7 @@ def optimise(self):
self.results[sample.name]["bestSample"] = (
self.mse[sample.name]["initial"]
)
self.results[sample.name]["bestIt"] = "initial"
self.results[sample.name]["bestIt"] = "None"
for iteration, sample in self.mse[sample.name].items():
if (sample.mse()
< self.results[sample.name]["bestSample"].mse()):
Expand Down Expand Up @@ -98,10 +102,10 @@ def __init__(
self.nCalls = nIters
self.nIters = nIters * len(samples)
self.purge = purge
self.gudrunFile = gudrunFile
self.gudrunFile = copy.deepcopy(gudrunFile)
self.samples = []
self.sample = None
self.nCurrent = 0
self.nCurrent = 1
self.mse = None
self.result = None

Expand All @@ -117,7 +121,7 @@ def __init__(
self.gudrun = gudpy.Gudrun()
self.gudrunObjects = []
self.gudrunQueue = []
for _ in range(nIters + len(self.samples)):
for _ in range(nIters * len(self.samples)):
self.gudrunQueue.append(gudpy.Gudrun())

self.simulation = data.NpDataSet(
Expand Down Expand Up @@ -166,6 +170,7 @@ def tweakExponent(self, exponents):
return error

def optimiseSample(self):
self.mse = None
for sample in self.gudrunFile.runSamples():
if sample.name != self.sample.name:
sample.runThisSample = False
Expand All @@ -190,17 +195,20 @@ def optimiseSample(self):
)
self.result = result
mse = round(result.fun, 5)
self.mse = mse
optExponents = [result.x[0:2] + [0.0],
result.x[2:] + [0.0]]

gudpy_io.GudPyIO.projectDir = projectFolder

if initError < mse:
optExponents = initExp
self.mse = initError

return optExponents

def optimise(self):
projectFolder = gudpy_io.GudPyIO.projectDir
try:
for sample in self.samples:
self.sample = sample
Expand All @@ -210,9 +218,16 @@ def optimise(self):
sample.dataFiles[0].mintFile, self.limit)
optExponents = self.optimiseSample()
self.sample.exponentialValues = optExponents
self.nCurrent = 0
self.nCurrent = 0
self.nCurrent = 1
self.nCurrent = "Final Run"
for sample in self.gudrunFile.samples():
sample.runThisSample = True
outputPath = os.path.join(projectFolder, "Optimise", "Exponents")
gudpy_io.GudPyIO.projectDir = outputPath
self.gudrun.gudrun(gudrunFile=self.gudrunFile, purge=self.purge)
gudpy_io.GudPyIO.projectDir = projectFolder
except exc.GudrunException as e:
gudpy_io.GudPyIO.projectDir = projectFolder
self.error = e.args
return (1, e)
return (0, "")
Expand Down
11 changes: 6 additions & 5 deletions gudpy/core/output_file_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -272,13 +272,14 @@ def _createSampleDir(self, dest: str):

# Create container folders within sample folder
for container in sample.containers:
name = (utils.replace_unwanted_chars(container.name)
if container.name != "CONTAINER"
else "Container")
for dataFile in container.dataFiles:
self._copyOutputs(
self._copyOutputsByExt(
dataFile,
samplePath,
(utils.replace_unwanted_chars(container.name)
if container.name != "CONTAINER"
else "Container")
os.path.join(samplePath, name),
name
)

def _createAddOutDir(self, dest: str, exclude: list[str] = []):
Expand Down
17 changes: 13 additions & 4 deletions gudpy/core/sample.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import os

from core.utils import bjoin, numifyBool
from core.data_files import DataFiles
from core.composition import Composition
Expand Down Expand Up @@ -174,11 +176,18 @@ def referenceData(self):
return data.NpDataSet(self._referenceDataFile)

def mse(self, limit=None):
if (not self.dataFiles or not self.dataFiles[0].mintFile
or not self.referenceDataFile):
if not self.dataFiles or not self.dataFiles[0].mintFile:
return None
if not os.path.exists(self.dataFiles[0].mintFile):
return None
if (not self.referenceDataFile
or not os.path.exists(self.referenceDataFile)):
return None
try:
refData = data.NpDataSet(self.referenceDataFile, lim=limit)
expData = data.NpDataSet(self.dataFiles[0].mintFile, lim=limit)
except TypeError:
return None
refData = data.NpDataSet(self.referenceDataFile, lim=limit)
expData = data.NpDataSet(self.dataFiles[0].mintFile, lim=limit)
return data.meanSquaredError(refData, expData)

def pathName(self):
Expand Down
28 changes: 10 additions & 18 deletions gudpy/gui/widgets/core/control.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
import sys
import traceback
import typing as typ
import copy

from PySide6 import QtCore, QtWidgets

Expand Down Expand Up @@ -561,8 +560,6 @@ def gudrunFinished(self, exitcode):
return

if exitcode != 0:
print(self.workerThread.gudrunIterator.gudrunFile.runSamples(
)[0].dataFiles[0]._outputs)
self.mainWidget.sendError(
f"Process failed with the following output: "
f"\n{self.workerThread.error}"
Expand All @@ -585,20 +582,17 @@ def gudrunFinished(self, exitcode):

if self.workerThread == self.gudpy.optimiser:
self.mainWidget.optimiseTab.setResults(
self.gudpy.optimiser.gudrunIterator.gudrunFile.samples(),
self.gudpy.optimiser.gudrunFile.samples(),
self.gudpy.optimiser
)
(self.mainWidget.optimiseTab.exponentialGroupBox
.exportDataButton.setEnabled(True))
(self.mainWidget.optimiseTab.exponentialGroupBox
.setParamsButton.setEnabled(True))

self.mainWidget.outputSlots.setOutput(
self.workerThread.output, "Gudrun", self.gudrunFile)
self.mainWidget.updateWidgets(
gudrunFile=self.gudrunFile,
)
self.gudpy.io.save(self.gudrunFile)
if not self.workerThread == self.gudpy.optimiser:
self.mainWidget.updateWidgets(
gudrunFile=self.gudrunFile,
)
self.gudpy.io.save(self.gudrunFile)
self.workerThread = None

def compositionIterationFinished(self, exitcode):
Expand Down Expand Up @@ -654,18 +648,16 @@ def runBatchProcessing(self):
self.startProcess()

def runOptimiseExponents(self):
gudrunFileCopy = copy.deepcopy(self.gudrunFile)

if not self.prepareRun():
return

samples = []
for sw in self.mainWidget.optimiseTab.sampleWidgets:
for sw in self.mainWidget.optimiseTab.sampleWidgets.values():
if sw.runSample:
samples.append(sw.sample)

self.gudpy.optimiser = worker.OptimiseExponentsWorker(
gudrunFile=gudrunFileCopy,
gudrunFile=self.gudrunFile,
samples=samples,
purge=self.gudpy.purge,
limit=(self.mainWidget.optimiseTab
Expand All @@ -675,7 +667,7 @@ def runOptimiseExponents(self):
)

self.mainWidget.optimiseTab.setSamples(
self.gudpy.optimiser.gudrunFile.runSamples()
self.gudpy.gudrunFile.runSamples()
)

self.connectProcessSignals(
Expand All @@ -698,7 +690,7 @@ def runOptimiseInelasticity(self):
return

samples = []
for sw in self.mainWidget.optimiseTab.sampleWidgets:
for sw in self.mainWidget.optimiseTab.sampleWidgets.values():
if sw.runSample:
samples.append(sw.sample)

Expand Down
18 changes: 18 additions & 0 deletions gudpy/gui/widgets/core/main_window.py
Original file line number Diff line number Diff line change
Expand Up @@ -371,6 +371,21 @@ def updateWidgets(self, gudrunFile):
self.normalisationSlots.setNormalisation(self.gudrunFile.normalisation)
self.optimiseTab.setSamples(self.gudrunFile.runSamples())

if not self.allPlots:
allTopChart = GudPyChart()
samples = [*gudrunFile.samples(), *gudrunFile.containers()]
allTopChart.addSamples(samples)
allTopChart.plot(
self.ui.allPlotComboBox.itemData(
self.ui.allPlotComboBox.currentIndex()
)
)
allBottomChart = GudPyChart()
allBottomChart.addSamples(samples)
self.allPlots = [allTopChart, allBottomChart]
self.ui.allSampleTopPlot.setChart(allTopChart)
self.ui.allSampleBottomPlot.setChart(allBottomChart)

if len(self.gudrunFile.sampleBackgrounds):
self.sampleBackgroundSlots.setSampleBackground(
self.gudrunFile.sampleBackgrounds[0]
Expand Down Expand Up @@ -577,6 +592,8 @@ def updateSamples(self):
*self.ui.objectTree.getContainers(),
]
for sample in samples:
if sample in self.results.keys():
continue
topChart = GudPyChart()
topChart.addSample(sample)
bottomChart = GudPyChart()
Expand All @@ -600,6 +617,7 @@ def updateSamples(self):
topChart, bottomChart, gf if gf else None]

def updateAllSamples(self):
return
samples = [
*self.ui.objectTree.getSamples(),
*self.ui.objectTree.getContainers(),
Expand Down
Loading

0 comments on commit 4839629

Please sign in to comment.