Skip to content

Commit

Permalink
cli debugged
Browse files Browse the repository at this point in the history
  • Loading branch information
stkroe committed Jul 30, 2024
1 parent 8930c36 commit ecf90a2
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 39 deletions.
21 changes: 14 additions & 7 deletions PQAnalysis/analysis/thermal_expansion/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@
linear or volumetric thermal expansion coefficient analysis.
"""

import numpy as np

from PQAnalysis.io import BoxFileReader
from PQAnalysis.traj import MDEngineFormat
from PQAnalysis.type_checking import runtime_type_checking


from .thermal_expansion import ThermalExpansion
from .thermal_expansion_input_file_reader import ThermalExpansionInputFileReader
from .thermal_expansion_output_file_writer import ThermalExpansionDataWriter
Expand Down Expand Up @@ -44,14 +44,21 @@ def thermal_expansion(input_file: str, md_format: MDEngineFormat | str = MDEngin
input_reader = ThermalExpansionInputFileReader(input_file)
input_reader.read()

temperature_points = input_reader.temperature_points
if input_reader.unit is None:
unit = "Angstrom"
else:
unit = input_reader.unit

temperature_points = np.array(input_reader.temperature_points)

box_data = []
for box_file in input_reader.box_files:
for i, box_file in enumerate(input_reader.box_files):
print(f"Reading box file: {
box_file} at temperature: {temperature_points[i]} K")
box_reader = BoxFileReader(
filename=box_file,
engine_format=md_format,
unit=input_reader.unit
unit=unit
)
box = box_reader.read()
box_data.append(box)
Expand All @@ -69,12 +76,12 @@ def thermal_expansion(input_file: str, md_format: MDEngineFormat | str = MDEngin

log_writer.write_before_run(_thermal_expansion)

box_avg_data, box_std_data, thermal_expansion_data = _thermal_expansion.run()
boxes_avg_data, boxes_std_data, thermal_expansion_data = _thermal_expansion.run()

data_writer.write(
temperature_points=temperature_points,
box_avg_data=box_avg_data,
box_std_data=box_std_data,
boxes_avg_data=boxes_avg_data,
boxes_std_data=boxes_std_data,
thermal_expansion_data=thermal_expansion_data
)
log_writer.write_after_run(_thermal_expansion)
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ class ThermalExpansionInputFileReader(Reader):

#: List[str]: The optional keys of the input file
optional_keys = required_keys + [
Reader.unit_key,
Reader.log_file_key,
]

Expand Down Expand Up @@ -73,8 +74,6 @@ def read(self):
- A list of files. Each file contains the box dimensions:
:math:`a`, :math:`b`, :math:`c`, :math:`\\alpha`, :math:`\\beta`, :math:`\\gamma`
and corroponds to a temperature point.
* - {Reader.unit_key}
- The unit of the box dimensions. Default is "Å".
* - {Reader.out_file_key}
- The output file to write Box dimension
averages and standard deviations, linear and volumetric thermal expansion coefficients to.
Expand All @@ -84,6 +83,8 @@ def read(self):
* - Key
- Value
* - {Reader.unit_key}
- The unit of the box dimensions. Default is Å.
* - {Reader.log_file_key}
- The log file to write the log information to.
Note
Expand All @@ -92,6 +93,7 @@ def read(self):
They are optional in the input file, but they might be required for
the analysis. This means that if an optional keyword is specified
other keywords might be required.
- :code:`{Reader.log_file_key}` is optional for the analysis.
(for more information see
:py:class:`~PQAnalysis.io.input_file_reader.pq_analysis.thermal_expansion.thermal_expansion.ThermalExpansion`).
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ def __init__(self, filename: str) -> None:
def write(
self,
temperature_points: Np1DNumberArray,
boxes_avgg_data: Np2DNumberArray,
boxes_avg_data: Np2DNumberArray,
boxes_std_data: Np2DNumberArray,
thermal_expansion_data: Np1DNumberArray,

Expand All @@ -48,7 +48,7 @@ def write(
----------
temperature_points : Np1DNumberArray
the temperature points
boxes_avgg_data : Np2DNumberArray
boxes_avg_data : Np2DNumberArray
the average box data output from the Box._initialize_run() method
boxes_std_data : Np2DNumberArray
the standard deviation box data output from the Box._initialize_run() method
Expand All @@ -61,26 +61,24 @@ def write(
angstrom = '\u212B'.encode('utf-8')
self.file.write(
f"T / K"
f"a_avg / {angstrom} a_std / {angstrom}"
f"b_avg / {angstrom} b_std / {angstrom}"
f"c_avg / {angstrom} c_std / {angstrom}"
f"volume / {angstrom}³ volume_std / {angstrom}³"
f"thermal_expansion_a / MK^-1 thermal_expansion_b / MK^-1"
f"thermal_expansion_c / MK^-1 volumetric expansion / MK^-1\n"
f"a_avg in {angstrom} a_std in {angstrom}"
f"b_avg in {angstrom} b_std in {angstrom}"
f"c_avg in {angstrom} c_std in {angstrom}"
f"volume in {angstrom}³ volume_std in {angstrom}³"
f"thermal_expansion_a in (M/K) thermal_expansion_b in (M/K)"
f"thermal_expansion_c in (M/K) volumetric expansion in (M/K)\n"
)
for i, temperature_point in enumerate(temperature_points):
self.file.write(
f"{temperature_point}"
f"{boxes_avgg_data[0][i]} {boxes_std_data[0][i]}"
f"{boxes_avgg_data[1][i]} {boxes_std_data[1][i]}"
f"{boxes_avgg_data[2][i]} {boxes_std_data[2][i]}"
f"{boxes_avgg_data[3][i]} {boxes_std_data[3][i]}"
print(
f"{temperature_point} "
f"{boxes_avg_data[0][i]} {boxes_std_data[0][i]} "
f"{boxes_avg_data[1][i]} {boxes_std_data[1][i]} "
f"{boxes_avg_data[2][i]} {boxes_std_data[2][i]} "
f"{boxes_avg_data[3][i]} {boxes_std_data[3][i]} "
f"{thermal_expansion_data_mega[0]} {
thermal_expansion_data_mega[1]}"
thermal_expansion_data_mega[1]} "
f"{thermal_expansion_data_mega[2]} {
thermal_expansion_data_mega[3]}\n"
)

thermal_expansion_data_mega[3]}", file=self.file)
super().close()


Expand Down Expand Up @@ -135,7 +133,7 @@ def write_before_run(self, thermal_expansion: ThermalExpansion) -> None:
print(f" Temperature step size: {
thermal_expansion.temperature_step_size}", file=self.file)
print(f" Number of box files: {
len(thermal_expansion.box)}", file=self.file)
len(thermal_expansion.boxes)}", file=self.file)

print(file=self.file)

Expand Down
10 changes: 8 additions & 2 deletions PQAnalysis/io/box_file_reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,17 @@
import logging
import numpy as np


from PQAnalysis.physical_data.box import Box
from PQAnalysis.utils import instance_function_count_decorator
from PQAnalysis.utils.custom_logging import setup_logger
from PQAnalysis import __package_name__
from PQAnalysis.type_checking import runtime_type_checking, runtime_type_checking_setter
from PQAnalysis.traj import Trajectory
from PQAnalysis.traj import MDEngineFormat
from PQAnalysis.exceptions import PQFileNotFoundError

from .exceptions import BoxReaderError
from PQAnalysis.physical_data.box import Box
from .base import BaseReader


Expand Down Expand Up @@ -56,6 +58,10 @@ def __init__(
super().__init__(filename)
self.trajectory = None

elif engine_format is not MDEngineFormat.PQ and filename is not None:
super().__init__(filename)
self.trajectory = None

elif trajectory is not None:
self.trajectory = trajectory
self.filename = None
Expand All @@ -76,7 +82,7 @@ def read(self):
The lattice parameter data.
"""

if self.engine_format is MDEngineFormat.PQ:
if self.filename is not None:
return self._read_from_file()
else:
return self._read_from_trajectory()
Expand Down
22 changes: 16 additions & 6 deletions PQAnalysis/io/input_file_reader/pq_analysis/_parse.py
Original file line number Diff line number Diff line change
Expand Up @@ -291,10 +291,10 @@ def _parse_bool(input_dict: InputDictionary, key: str) -> bool | None:
return data[0]


def _parse_lists(input_dict: InputDictionary, key: str) -> Tuple[List[float]] | List[float] | None:
def _parse_lists(input_dict: InputDictionary, key: str) -> List[float] | None:
"""
Gets the value of a key from the input dictionary and
checks if it is a tuple of lists of floats or a list of floats.
checks if it is a list of floats.
Parameters
----------
Expand All @@ -305,13 +305,13 @@ def _parse_lists(input_dict: InputDictionary, key: str) -> Tuple[List[float]] |
Returns
-------
Tuple[List[float]] | List[float] | None
List[float] | None
the value of the key or None if the key is not in the dictionary
Raises
------
InputFileError
if the value is not a tuple of lists of floats or a list of floats
if the value is not a list of floats
"""

try:
Expand All @@ -321,7 +321,17 @@ def _parse_lists(input_dict: InputDictionary, key: str) -> Tuple[List[float]] |

data_type = data[1]

if data_type in {"glob", "list(float), tuple(list(float))"}:
if data_type == "float":
return [data[0]]

if data_type in {"glob", "list(float)"}:
return data[0]

return data[0]
logger.error(
(
f"The \"{key}\" value has to be either a "
"string, glob or a list of floats - actually "
f"it is parsed as a {data_type}"
),
exception=InputFileError
)
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,11 @@ def temperature_points(self) -> List[float] | None:
"""
List[float] | None: The temperature points.
"""
return _parse_lists(self, "temperature_points")
return _parse_lists(self.dictionary, self.temperature_points_key)

@property
def unit(self) -> str | None:
"""
str | None: The unit of the temperature points.
str | None: The unit of the box files.
"""
return _parse_string(self, "unit")
return _parse_string(self.dictionary, self.unit_key)

0 comments on commit ecf90a2

Please sign in to comment.