Skip to content

Commit

Permalink
Merge pull request #345 from ISISNeutronMuon/improved-labels
Browse files Browse the repository at this point in the history
Improved labels, corrected text output
  • Loading branch information
ChiCheng45 authored Mar 8, 2024
2 parents 39ce5cc + 59517af commit 905aed1
Show file tree
Hide file tree
Showing 98 changed files with 350 additions and 172 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -102,4 +102,4 @@ def get_information(self):
except KeyError:
print(f"No VALUE in {self._name}")

return "Input file: %r" % self["value"]
return "Input file: %r\n" % self["value"]
Original file line number Diff line number Diff line change
Expand Up @@ -79,4 +79,4 @@ def get_information(self) -> str:
if "value" not in self:
return "Not configured yet\n"

return str(self["value"])
return str(self["value"]) + "\n"
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ def get_information(self):
info.append("Number of selected atoms:%d" % self["selection_length"])
info.append("Selected elements:%s" % self["unique_names"])

return "\n".join(info)
return "\n".join(info) + "\n"

def get_selector(self) -> Selector:
traj_config = self._configurable[self._dependencies["trajectory"]]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,6 @@ def get_information(self):
return "Not configured yet"

if self["value"] is None:
return "No atoms selected for transmutation"
return "No atoms selected for transmutation\n"

return "Number of transmutated atoms:%d\n" % self._nTransmutatedAtoms
Original file line number Diff line number Diff line change
Expand Up @@ -114,4 +114,4 @@ def get_information(self):
"Number of selected %d-tuplets:%d" % (self._nAtoms, self["n_values"])
)

return "\n".join(info)
return "\n".join(info) + "\n"
Original file line number Diff line number Diff line change
Expand Up @@ -78,4 +78,4 @@ def get_information(self):
:rtype: str
"""

return "Axis vector:%s" % self["value"]
return "Axis vector:%s\n" % self["value"]
Original file line number Diff line number Diff line change
Expand Up @@ -81,4 +81,4 @@ def get_information(self):
:rtype: str
"""

return "Basis vector:%s" % self["value"]
return "Basis vector:%s\n" % self["value"]
Original file line number Diff line number Diff line change
Expand Up @@ -71,4 +71,4 @@ def get_information(self):
:rtype: str
"""

return "Value: %r" % self["value"]
return "Value: %r\n" % self["value"]
Original file line number Diff line number Diff line change
Expand Up @@ -124,4 +124,4 @@ def get_information(self):
:rtype: str
"""

return "Value: %r" % self["value"]
return "Value: %r\n" % self["value"]
Original file line number Diff line number Diff line change
Expand Up @@ -103,4 +103,4 @@ def get_information(self):
for v in variables:
info.append("\t-{}".format(v))

return "\n".join(info)
return "\n".join(info) + "\n"
Original file line number Diff line number Diff line change
Expand Up @@ -52,4 +52,4 @@ def get_information(self):
:rtype: str
"""

return "Input directory: %r" % self["value"]
return "Input directory: %r\n" % self["value"]
Original file line number Diff line number Diff line change
Expand Up @@ -82,4 +82,4 @@ def get_information(self):
:rtype: str
"""

return "Input file: %r" % self["value"]
return "Input file: %r\n" % self["value"]
Original file line number Diff line number Diff line change
Expand Up @@ -112,4 +112,4 @@ def get_information(self):

info = "\n".join(info)

return info
return info + "\n"
Original file line number Diff line number Diff line change
Expand Up @@ -133,4 +133,4 @@ def get_information(self):
:rtype: str
"""

return "Value: %r" % self["value"]
return "Value: %r\n" % self["value"]
Original file line number Diff line number Diff line change
Expand Up @@ -83,4 +83,4 @@ def get_information(self):
:rtype: str
"""

return "McStas command line options: %s" % self["value"]
return "McStas command line options: %s\n" % self["value"]
Original file line number Diff line number Diff line change
Expand Up @@ -114,4 +114,4 @@ def get_information(self):
:rtype: str
"""

return "McStas command line parameters:%s" % self["value"]
return "McStas command line parameters:%s\n" % self["value"]
Original file line number Diff line number Diff line change
Expand Up @@ -102,4 +102,4 @@ def nChoices(self):
return self._nChoices

def get_information(self):
return "Selected items: %r" % self["choices"]
return "Selected items: %r\n" % self["choices"]
Original file line number Diff line number Diff line change
Expand Up @@ -71,4 +71,4 @@ def get_information(self):
:rtype: str
"""

return "Output directory: %r" % self["value"]
return "Output directory: %r\n" % self["value"]
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class OutputFilesConfigurator(IConfigurator):
Once configured, this configurator will provide a list of files built by joining the given output directory, the
basename and the extensions corresponding to the input file formats.
For analysis, MDANSE currently supports only the HDF and ASCII formats. To define a new output file format
For analysis, MDANSE currently supports only the HDF and Text formats. To define a new output file format
for an analysis, you must inherit from MDANSE.Framework.Formats.IFormat.IFormat interface.
"""

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ def get_information(self):
:rtype: str
"""

info = "Output file: %s" % self["file"]
info = "Output file: %s\n" % self["file"]

return info

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,6 @@ def get_information(self):
:rtype: str
"""

info = "Sum of partial charges = %8.3f" % sum(self["charges"].values())
info = "Sum of partial charges = %8.3f\n" % sum(self["charges"].values())

return info
Original file line number Diff line number Diff line change
Expand Up @@ -58,4 +58,4 @@ def get_information(self):
:rtype: str
"""

return "Python object: %r" % self["value"]
return "Python object: %r\n" % self["value"]
Original file line number Diff line number Diff line change
Expand Up @@ -211,8 +211,8 @@ def get_information(self):
info = "%d values from %s to %s" % (self["number"], self["first"], self["last"])

if self._includeLast:
info += "last value included"
info += " last value included"
else:
info += "last value excluded"
info += " last value excluded"

return info
return info + "\n"
Original file line number Diff line number Diff line change
Expand Up @@ -27,21 +27,21 @@ class RunningModeConfigurator(IConfigurator):
"""
This configurator allows to choose the mode used to run the calculation.
MDANSE currently support monoprocessor or multiprocessor (SMP) running modes. In the laster case, you have to
MDANSE currently support single-core or multicore (SMP) running modes. In the laster case, you have to
specify the number of slots used for running the analysis.
"""

availablesModes = ["monoprocessor", "threadpool", "multiprocessor"]
availablesModes = ["single-core", "threadpool", "multicore"]

_default = ("monoprocessor", 1)
_default = ("single-core", 1)

def configure(self, value):
"""
Configure the running mode.
:param value: the running mode specification. It can be *'monoprocessor'* or a 2-tuple whose first element \
must be *'multiprocessor'* and 2nd element the number of slots allocated for running the analysis.
:type value: *'monoprocessor'* or 2-tuple
:param value: the running mode specification. It can be *'single-core'* or a 2-tuple whose first element \
must be *'multicore'* and 2nd element the number of slots allocated for running the analysis.
:type value: *'single-core'* or 2-tuple
"""

if isinstance(value, str):
Expand All @@ -53,13 +53,13 @@ def configure(self, value):
self.error_status = f"{mode} is not a valid running mode."
return

if mode == "monoprocessor":
if mode == "single-core":
slots = 1

else:
slots = int(value[1])

if mode == "multiprocessor":
if mode == "multicore":
maxSlots = multiprocessing.cpu_count()
if slots > maxSlots:
self.error_status = "invalid number of allocated slots."
Expand All @@ -82,4 +82,4 @@ def get_information(self):
:rtype: str
"""

return "Run in %s mode (%d slots)" % (self["mode"], self["slots"])
return "Run in %s mode (%d slots)\n" % (self["mode"], self["slots"])
Original file line number Diff line number Diff line change
Expand Up @@ -77,4 +77,4 @@ def get_information(self):
:rtype: str
"""

return "Selected item: %r" % self["value"]
return "Selected item: %r\n" % self["value"]
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ def get_information(self):
:rtype: str
"""

info = "Output file: %s" % self["file"]
info = "Output file: %s\n" % self["file"]

return info

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,4 +103,4 @@ def get_information(self):
:rtype: str
"""

return "Value: %r" % self["value"]
return "Value: %r\n" % self["value"]
Original file line number Diff line number Diff line change
Expand Up @@ -56,4 +56,4 @@ def get_information(self):
:rtype: str
"""

return "Selected variable: %r" % self["value"]
return "Selected variable: %r\n" % self["value"]
Original file line number Diff line number Diff line change
Expand Up @@ -141,4 +141,4 @@ def get_information(self):
:rtype: str
"""

return "Value: %r" % self["value"]
return "Value: %r\n" % self["value"]
Original file line number Diff line number Diff line change
Expand Up @@ -92,4 +92,4 @@ def get_information(self):
:rtype: str
"""

return "selected weight: %s" % self["property"]
return "selected weight: %s\n" % self["property"]
2 changes: 1 addition & 1 deletion MDANSE/Src/MDANSE/Framework/Formats/IFormat.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class IFormat(metaclass=SubclassFactory):
"""
This is the base class for writing MDANSE output data. In MDANSE, the output of an analysis can be written in different file format.
Currently, MDANSE supports HDF5 and ASCII output file formats. To introduce a new file output file format, just create a new concrete
Currently, MDANSE supports HDF5 and Text output file formats. To introduce a new file output file format, just create a new concrete
subclass of IFormat and overload the "write" class method as defined in IFormat base class which will actually write the output variables,
and redefine the "type", "extension" and "extensions" class attributes.
"""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
#
# MDANSE: Molecular Dynamics Analysis for Neutron Scattering Experiments
#
# @file Src/Framework/Formats/ASCIIFormat.py
# @brief Implements module/class/test ASCIIFormat
# @file Src/Framework/Formats/TextFormat.py
# @brief Implements module/class/test TextFormat
#
# @homepage https://www.isis.stfc.ac.uk/Pages/MDANSEproject.aspx
# @license GNU General Public License v3 or higher (see LICENSE)
Expand All @@ -16,16 +16,23 @@
import os
import io
import tarfile
import codecs
import time

import numpy as np


from MDANSE.Framework.Formats.IFormat import IFormat


class ASCIIFormat(IFormat):
def length_stringio(input: "io.BytesIO") -> int:
result = input.getbuffer().nbytes
return result


class TextFormat(IFormat):
"""
This class handles the writing of output variables in ASCII format. Each output variable is written into separate ASCII files which are further
This class handles the writing of output variables in Text format. Each output variable is written into separate Text files which are further
added to a single archive file.
"""

Expand All @@ -36,11 +43,11 @@ class ASCIIFormat(IFormat):
@classmethod
def write(cls, filename, data, header=""):
"""
Write a set of output variables into a set of ASCII files.
Write a set of output variables into a set of Text files.
Each output variable will be output in a separate ASCII file. All the ASCII files will be compressed into a tar file.
Each output variable will be output in a separate Text file. All the Text files will be compressed into a tar file.
:param filename: the path to the output archive file that will contain the ASCII files written for each output variable.
:param filename: the path to the output archive file that will contain the Text files written for each output variable.
:type filename: str
:param data: the data to be written out.
:type data: dict of Framework.OutputVariables.IOutputVariable
Expand All @@ -49,29 +56,34 @@ def write(cls, filename, data, header=""):
"""

filename = os.path.splitext(filename)[0]
filename = "%s_ascii.tar" % filename
filename = "%s_text.tar" % filename

tf = tarfile.open(filename, "w")

if header:
real_buffer = io.BytesIO()
tempStr = codecs.getwriter("utf-8")(real_buffer)
for line in header:
tempStr.write(str(line))
tempStr.write("\n\n")
real_buffer.seek(0)
info = tarfile.TarInfo(name="jobinfo.txt")
info.size = length_stringio(real_buffer)
info.mtime = time.time()
tf.addfile(tarinfo=info, fileobj=real_buffer)

for var in list(data.values()):
tempStr = io.StringIO()
real_buffer = io.BytesIO()
tempStr = codecs.getwriter("utf-8")(real_buffer)
tempStr.write(var.info())
tempStr.write("\n\n")
cls.write_data(tempStr, var, data)
tempStr.seek(0)
real_buffer.seek(0)

info = tarfile.TarInfo(name="%s%s" % (var.varname, cls.extensions[0]))
info.size = tempStr.len
tf.addfile(tarinfo=info, fileobj=tempStr)

if header:
tempStr = io.StringIO()
tempStr.write(header)
tempStr.write("\n\n")
tempStr.seek(0)
info = tarfile.TarInfo(name="jobinfo.txt")
info.size = tempStr.len
tf.addfile(tarinfo=info, fileobj=tempStr)
info.size = length_stringio(real_buffer)
info.mtime = time.time()
tf.addfile(tarinfo=info, fileobj=real_buffer)

tf.close()

Expand All @@ -91,9 +103,7 @@ def write_data(cls, fileobject, data, allData):
"""

if data.ndim > 2:
fileobject.write(
"Can not write ASCII output for data of dimensionality > 2"
)
fileobject.write("Can not write Text output for data of dimensionality > 2")

elif data.ndim == 2:
xData, yData = data.axis.split("|")
Expand Down
2 changes: 1 addition & 1 deletion MDANSE/Src/MDANSE/Framework/Jobs/AngularCorrelation.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ class AngularCorrelation(IJob):
)
settings["output_files"] = (
"OutputFilesConfigurator",
{"formats": ["MDAFormat", "ASCIIFormat"]},
{"formats": ["MDAFormat", "TextFormat"]},
)
settings["running_mode"] = ("RunningModeConfigurator", {})

Expand Down
2 changes: 1 addition & 1 deletion MDANSE/Src/MDANSE/Framework/Jobs/AreaPerMolecule.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ class AreaPerMolecule(IJob):
)
settings["output_files"] = (
"OutputFilesConfigurator",
{"formats": ["MDAFormat", "ASCIIFormat"]},
{"formats": ["MDAFormat", "TextFormat"]},
)
settings["running_mode"] = ("RunningModeConfigurator", {})

Expand Down
2 changes: 1 addition & 1 deletion MDANSE/Src/MDANSE/Framework/Jobs/CoordinationNumber.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ class CoordinationNumber(DistanceHistogram):
)
settings["output_files"] = (
"OutputFilesConfigurator",
{"formats": ["MDAFormat", "ASCIIFormat"]},
{"formats": ["MDAFormat", "TextFormat"]},
)
settings["running_mode"] = ("RunningModeConfigurator", {})

Expand Down
Loading

0 comments on commit 905aed1

Please sign in to comment.