Skip to content
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

feat: Import from project - Improved IO Management #508

Merged
merged 80 commits into from
May 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
80 commits
Select commit Hold shift + click to select a range
eb3e895
Store output in data
rhinoella May 10, 2024
c538162
Create separate parser classes
rhinoella May 14, 2024
2f6f6a3
Create dedicated iO handling
rhinoella May 14, 2024
beefca0
Update functions
rhinoella May 15, 2024
af58312
Continue refactoring
rhinoella May 15, 2024
576fc22
Apply breaking changes
rhinoella May 16, 2024
6ce452d
Fix checks
rhinoella May 17, 2024
6c89dde
Fix YAML imports
rhinoella May 20, 2024
e8bc324
Fix instrument parsing
rhinoella May 20, 2024
317d618
Fix export, update test project
rhinoella May 20, 2024
739410b
Merge branch 'develop' of https://github.com/disorderedmaterials/GudP…
rhinoella May 20, 2024
8604ee3
Lint, delete gudpy_yaml.py
rhinoella May 20, 2024
ba9e8bc
Small bug fixes
rhinoella May 20, 2024
5f9209f
Lint: remove unused import
rhinoella May 20, 2024
39aacf8
Test path fis
rhinoella May 20, 2024
8594fd6
Text updating
rhinoella May 20, 2024
904ec39
Syntax fix
rhinoella May 20, 2024
a0ad5e5
Remove unused import, allow tests without lint
rhinoella May 20, 2024
912970b
update test
rhinoella May 20, 2024
df2b130
Revert error message
rhinoella May 20, 2024
3f3d212
Add output folder attr to tests
rhinoella May 20, 2024
a8c3854
Update tests
rhinoella May 20, 2024
e1a141d
Add writeGudrunFileTo method
rhinoella May 21, 2024
234eeeb
Switch to correct parser
rhinoella May 21, 2024
2ed178a
Add decorator
rhinoella May 21, 2024
9b6b813
Bugfixes
rhinoella May 21, 2024
62b1819
Correctly identify sample files
rhinoella May 22, 2024
e0a3c92
Fix output data paths
rhinoella May 22, 2024
30acd32
Fix iteration output handling
rhinoella May 22, 2024
450357d
Fix order of datafile initialisation
rhinoella May 22, 2024
ba91c2b
Change exception assertions
rhinoella May 22, 2024
9305a28
Fix assertions
rhinoella May 23, 2024
a9af7fa
Assertions
rhinoella May 23, 2024
05914eb
Remove elements from dict
rhinoella May 23, 2024
6ae7b7b
Fix config setting
rhinoella May 23, 2024
0d8e261
Remove data files from assertion
rhinoella May 23, 2024
cfec70a
Remove print statement
rhinoella May 23, 2024
892f90a
Pop the dict
rhinoella May 23, 2024
428884a
Apply rebase
rhinoella May 28, 2024
4dd11fd
Create separate parser classes
rhinoella May 14, 2024
02026f1
Create dedicated iO handling
rhinoella May 14, 2024
c369c02
Update functions
rhinoella May 15, 2024
5da1786
Continue refactoring
rhinoella May 15, 2024
a2be7cb
Apply breaking changes
rhinoella May 16, 2024
ad8fddc
Fix checks
rhinoella May 17, 2024
6ebff8f
Fix YAML imports
rhinoella May 20, 2024
27bcd7c
Fix instrument parsing
rhinoella May 20, 2024
3c5ca04
Fix export, update test project
rhinoella May 20, 2024
4a9ae78
Lint, delete gudpy_yaml.py
rhinoella May 20, 2024
a4cb2c6
Small bug fixes
rhinoella May 20, 2024
0ce46c1
Lint: remove unused import
rhinoella May 20, 2024
e3f1737
Test path fis
rhinoella May 20, 2024
44a8478
Text updating
rhinoella May 20, 2024
58b44bf
Syntax fix
rhinoella May 20, 2024
6bc281e
Remove unused import, allow tests without lint
rhinoella May 20, 2024
bdfc567
update test
rhinoella May 20, 2024
38dbb0c
Revert error message
rhinoella May 20, 2024
351d83a
Add output folder attr to tests
rhinoella May 20, 2024
20eb90d
Update tests
rhinoella May 20, 2024
92d5662
Add writeGudrunFileTo method
rhinoella May 21, 2024
723acaf
Switch to correct parser
rhinoella May 21, 2024
dd47a3e
Add decorator
rhinoella May 21, 2024
ced04ff
Bugfixes
rhinoella May 21, 2024
8c2683a
Correctly identify sample files
rhinoella May 22, 2024
18cfc09
Fix output data paths
rhinoella May 22, 2024
7d01100
Fix iteration output handling
rhinoella May 22, 2024
c5eef91
Fix order of datafile initialisation
rhinoella May 22, 2024
603a654
Change exception assertions
rhinoella May 22, 2024
e8364b2
Fix assertions
rhinoella May 23, 2024
4d57566
Assertions
rhinoella May 23, 2024
9ad5c68
Remove elements from dict
rhinoella May 23, 2024
f421d38
Fix config setting
rhinoella May 23, 2024
3252d20
Remove data files from assertion
rhinoella May 23, 2024
41864e0
Remove print statement
rhinoella May 23, 2024
6e672ed
Pop the dict
rhinoella May 23, 2024
b8cec99
Merge branch 'inelasticity-iterator' of https://github.com/disordered…
rhinoella May 28, 2024
e5501a9
Fix purge file path
rhinoella May 28, 2024
acbf1ad
Change directory sourcing
rhinoella May 28, 2024
e44a75b
Fix path separator for windows
rhinoella May 28, 2024
9e78414
Fix windows
rhinoella May 28, 2024
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
1 change: 0 additions & 1 deletion .github/workflows/pr.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ jobs:
uses: "./.github/workflows/lint"

Test:
needs: Lint
strategy:
fail-fast: false
matrix:
Expand Down
3 changes: 2 additions & 1 deletion gudpy/core/container.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ def __init__(self, config=None):
"""
self.name = ""
self.periodNumber = 1
self.dataFiles = DataFiles([], "CONTAINER")
self.dataFiles = DataFiles([], "CONTAINER", True)
self.composition = Composition("CONTAINER")
self.geometry = Geometry.SameAsBeam
self.upstreamThickness = 0.1
Expand All @@ -97,6 +97,7 @@ def __init__(self, config=None):
self.grBroadening = 0.
self.powerForBroadening = 0.0
self.stepSize = 0.0
self.outputFolder = ""

self.yamlignore = {
"runAsSample",
Expand Down
93 changes: 84 additions & 9 deletions gudpy/core/data_files.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,56 @@
import os

from core import config
from core import utils
from core.gud_file import GudFile


class DataFile:
def __init__(self, filename, isSampleDataFile: bool = False):
self.filename = filename
self.name = utils.replace_unwanted_chars(
os.path.splitext(filename)[0]
)
self.ext = os.path.splitext(filename)[1]
self.outputFolder = ""
self._outputs = {}
self.isSampleDataFile = isSampleDataFile

self.yamlignore = {
"str",
"yamlignore"
}

def addOutput(self, path):
ext = os.path.splitext(path)[1]
self._outputs[ext] = path

def outputs(self, ext):
return self._outputs[ext]

def __str__(self):
return self.filename

@property
def gudFile(self):
if not self.isSampleDataFile:
return None
gudPath = self._outputs.get(".gud", None)
if not gudPath:
return None
return GudFile(gudPath)

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

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


class DataFiles:
Expand All @@ -17,7 +69,7 @@ class DataFiles:
-------
"""

def __init__(self, dataFiles, name):
def __init__(self, dataFiles, name, isSampleDataFile=False):
"""
Constructs all the necessary attributes for the DataFiles object.

Expand All @@ -28,14 +80,28 @@ def __init__(self, dataFiles, name):
name : str
Name of the parent of the data files, e.g. Sample Background
"""
self.dataFiles = dataFiles

self._dataFiles: DataFile = []
self.isSampleDataFile = isSampleDataFile
self.setFiles(dataFiles)

self.name = name

self.yamlignore = {
"str",
"yamlignore"
}

self.outputFolders = {}
self._outputs = {}

def setFiles(self, dataFilenames):
self._dataFiles.clear()
for dataFile in dataFilenames:
if not dataFile:
continue
self._dataFiles.append(DataFile(dataFile, self.isSampleDataFile))

def __str__(self):
"""
Returns the string representation of the DataFiles object.
Expand All @@ -50,11 +116,20 @@ def __str__(self):
String representation of DataFiles.
"""
self.str = [
df + config.spc10 + self.name + " data files"
for df in self.dataFiles
df.filename + config.spc10 + self.name + " data files"
for df in self._dataFiles
]
return """\n""".join(self.str)

@property
def dataFilenames(self):
dfNames = [df.filename for df in self._dataFiles]
return dfNames

@property
def dataFiles(self):
return self._dataFiles

def __len__(self):
"""
Returns the length of the dataFiles list member.
Expand All @@ -68,15 +143,15 @@ def __len__(self):
int
Number of data files,
"""
return len(self.dataFiles)
return len(self._dataFiles)

def __getitem__(self, n):
return self.dataFiles[n]
return self._dataFiles[n]

def __setitem__(self, n, item):
if n >= len(self):
self.dataFiles.extend(n+1)
self.dataFiles[n] = item
self._dataFiles.extend(n+1)
self._dataFiles[n] = DataFile(item)

def __iter__(self):
return iter(self.dataFiles)
return iter(self._dataFiles)
27 changes: 11 additions & 16 deletions gudpy/core/file_library.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import os
from zipfile import ZipFile, ZIP_DEFLATED
from pathlib import Path

from core import utils
from core.enums import CrossSectionSource
from core.io.gudpy_io import GudPyIO


class GudPyFileLibrary:
Expand Down Expand Up @@ -64,8 +64,8 @@ def __init__(self, gudrunFile):
}

self.dataFiles = [
*gudrunFile.normalisation.dataFiles.dataFiles,
*gudrunFile.normalisation.dataFilesBg.dataFiles,
*gudrunFile.normalisation.dataFiles.dataFilenames,
*gudrunFile.normalisation.dataFilesBg.dataFilenames,
]

# If NXS files are being used
Expand All @@ -90,15 +90,15 @@ def __init__(self, gudrunFile):
# that file too.

for sampleBackground in gudrunFile.sampleBackgrounds:
self.dataFiles.extend(sampleBackground.dataFiles.dataFiles)
self.dataFiles.extend(sampleBackground.dataFiles.dataFilenames)

for sample in sampleBackground.samples:
self.dataFiles.extend(sample.dataFiles.dataFiles)
self.dataFiles.extend(sample.dataFiles.dataFilenames)
if sample.totalCrossSectionSource == CrossSectionSource.FILE:
self.files[sample.name] = sample.crossSectionFilename

for container in sample.containers:
self.dataFiles.extend(container.dataFiles.dataFiles)
self.dataFiles.extend(container.dataFiles.dataFilenames)
if container.totalCrossSectionSource == (
CrossSectionSource.FILE
):
Expand Down Expand Up @@ -166,21 +166,16 @@ def checkFilesExist(self):
def exportMintData(
self,
samples,
exportTo="",
renameDataFiles=False,
exportTo=None,
includeParams=False,
):
if not exportTo:
exportTo = os.path.join(
self.gudrunFile.projectDir,
Path(self.gudrunFile.path()).stem + ".zip",
)
with ZipFile(exportTo, "w", ZIP_DEFLATED) as zipFile:
for sample in samples:
if len(sample.dataFiles.dataFiles):
path = os.path.join(
self.gudrunFile.projectDir,
sample.dataFiles.dataFiles[0].replace(
sample.dataFiles.dataFilenames[0].replace(
self.gudrunFile.instrument.dataFileType, "mint01"
),
)
Expand All @@ -199,10 +194,10 @@ def exportMintData(
self.gudrunFile.projectDir,
safeSampleName + ".sample",
)
sample.sampleFile = path

if not os.path.exists(path):
sample.write_out(
self.gudrunFile.projectDir
)
GudPyIO.writeObject(sample, path)
zipFile.write(path, arcname=os.path.basename(path))

return zipFile.filename
26 changes: 0 additions & 26 deletions gudpy/core/gud_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,13 +71,6 @@ class GudFile:
Contents of the .gud file.
output : str
Output for use in the GUI.
Methods
-------
parse():
Parses the GudFile from path, assigning values
to each of the attributes.
write_out(overwrite=False)
Writes out the string representation of the GudFile to a file.
"""

def __init__(self, path):
Expand Down Expand Up @@ -350,22 +343,3 @@ def __str__(self):
f'{self.suggestedTweakFactor}\n'

)

def write_out(self, path):
"""
Writes out the string representation of the GudFile.
If 'overwrite' is True, then the initial file is overwritten.
Otherwise, it is written to 'gudpy_{initial filename}.gud'.

Parameters
----------
overwrite : bool, optional
Overwrite the initial file? (default is False).

Returns
-------
None
"""
f = open(path, "w", encoding="utf-8")
f.write(str(self))
f.close()
Loading
Loading