diff --git a/toughio/__init__.py b/toughio/__init__.py index f68c4895..91893487 100644 --- a/toughio/__init__.py +++ b/toughio/__init__.py @@ -2,8 +2,8 @@ from .__about__ import __version__ from ._helpers import convert_labels from ._io import ( - ElementOutput, ConnectionOutput, + ElementOutput, read_input, read_output, read_table, diff --git a/toughio/_io/__init__.py b/toughio/_io/__init__.py index c59fd595..9c50ea33 100644 --- a/toughio/_io/__init__.py +++ b/toughio/_io/__init__.py @@ -2,7 +2,7 @@ from .input import read as read_input from .input import register as register_input from .input import write as write_input -from .output import ElementOutput, ConnectionOutput +from .output import ConnectionOutput, ElementOutput from .output import read as read_output from .output import register as register_output from .output import write as write_output diff --git a/toughio/_io/h5/_write.py b/toughio/_io/h5/_write.py index 16fbd833..4f9ffede 100644 --- a/toughio/_io/h5/_write.py +++ b/toughio/_io/h5/_write.py @@ -2,7 +2,7 @@ import h5py -from ..output import ElementOutput, ConnectionOutput +from ..output import ConnectionOutput, ElementOutput from ..output import read as read_output from ..table import read as read_table diff --git a/toughio/_io/output/__init__.py b/toughio/_io/output/__init__.py index 40b9e3d4..b02cea87 100644 --- a/toughio/_io/output/__init__.py +++ b/toughio/_io/output/__init__.py @@ -1,5 +1,5 @@ from . import csv, petrasim, save, tecplot, tough -from ._common import ElementOutput, ConnectionOutput +from ._common import ConnectionOutput, ElementOutput from ._helpers import read, register, write __all__ = [ diff --git a/toughio/_io/output/_common.py b/toughio/_io/output/_common.py index f84c694c..b559e805 100644 --- a/toughio/_io/output/_common.py +++ b/toughio/_io/output/_common.py @@ -1,5 +1,5 @@ -from abc import ABC, abstractmethod import logging +from abc import ABC, abstractmethod import numpy as np @@ -24,13 +24,13 @@ def __init__(self, time, data, labels=None): def __getitem__(self): """Slice output.""" pass - + @abstractmethod def index(self): """Get index of element or connection.""" if self.labels is None: raise AttributeError() - + @property def n_data(self): """Return number of data points.""" @@ -40,31 +40,31 @@ def n_data(self): def time(self): """Return time step (in seconds).""" return self._time - + @time.setter def time(self, value): self._time = value - + @property def data(self): """Return data arrays.""" return self._data - + @data.setter def data(self, value): self._data = value - + @property def labels(self): """Return labels.""" return self._labels - + @labels.setter def labels(self, value): if value is not None: if len(value) != self.n_data: raise ValueError() - + self._labels = list(value) else: @@ -84,14 +84,14 @@ def __init__(self, time, data, labels=None): Data arrays. labels : sequence of str or None, default, None Labels of elements. - + """ super().__init__(time, data, labels) def __getitem__(self, islice): """ Slice element output. - + Parameters ---------- islice : int, str, slice, sequence of int or sequence of str @@ -101,26 +101,26 @@ def __getitem__(self, islice): ------- dict or :class:`toughio.ElementOutput` Sliced element outputs. - + """ if self.labels is None: raise AttributeError() - + if np.ndim(islice) == 0: if isinstance(islice, slice): islice = np.arange(self.n_data)[islice] else: islice = self.index(islice) if isinstance(islice, str) else islice - + return {k: v[islice] for k, v in self.data.items()} - + elif np.ndim(islice) == 1: islice = [self.index(i) if isinstance(i, str) else i for i in islice] else: raise ValueError() - + return ElementOutput( self.time, {k: v[islice] for k, v in self.data.items()}, @@ -130,7 +130,7 @@ def __getitem__(self, islice): def index(self, label): """ Get index of element. - + Parameters ---------- label : str @@ -140,10 +140,10 @@ def index(self, label): ------- int Index of element. - + """ super().index() - + return self.labels.index(label) @@ -160,14 +160,14 @@ def __init__(self, time, data, labels=None): Data arrays. labels : sequence of str or None, default, None Labels of connections. - + """ super().__init__(time, data, labels) def __getitem__(self, islice): """ Slice connection output. - + Parameters ---------- islice : int, str, slice, sequence of int or sequence of str @@ -177,15 +177,16 @@ def __getitem__(self, islice): ------- dict or :class:`toughio.ConnectionOutput` Sliced connection outputs. - + """ if self.labels is None: raise AttributeError() - + if np.ndim(islice) == 0: if isinstance(islice, str): islice = [ - i for i, (label1, label2) in enumerate(self.labels) + i + for i, (label1, label2) in enumerate(self.labels) if label1 == islice or label2 == islice ] @@ -194,13 +195,13 @@ def __getitem__(self, islice): else: return {k: v[islice] for k, v in self.data.items()} - + elif np.ndim(islice) <= 2: islice = [self.index(*i) if np.ndim(i) == 1 else i for i in islice] else: raise ValueError() - + return ConnectionOutput( self.time, {k: v[islice] for k, v in self.data.items()}, @@ -210,7 +211,7 @@ def __getitem__(self, islice): def index(self, label1, label2): """ Get index of connection. - + Parameters ---------- label1 : str @@ -222,7 +223,7 @@ def index(self, label1, label2): ------- int Index of connection. - + """ super().index() labels = ["".join(label) for label in self.labels] @@ -240,7 +241,11 @@ def to_output(file_type, labels_order, headers, times, labels, data): "data": {k: v for k, v in zip(headers, np.transpose(data_))}, } - output = ElementOutput(**kwargs) if file_type == "element" else ConnectionOutput(**kwargs) + output = ( + ElementOutput(**kwargs) + if file_type == "element" + else ConnectionOutput(**kwargs) + ) outputs.append(output) # Some older versions of TOUGH3 have duplicate connection outputs when running in parallel diff --git a/toughio/_io/output/_helpers.py b/toughio/_io/output/_helpers.py index 0146881d..0d569ee1 100644 --- a/toughio/_io/output/_helpers.py +++ b/toughio/_io/output/_helpers.py @@ -184,5 +184,5 @@ def get_output_type(filename): else: file_format = "element" file_type = None - + return file_type, file_format diff --git a/toughio/_io/output/csv/_csv.py b/toughio/_io/output/csv/_csv.py index 6c1a9448..b7ba3142 100644 --- a/toughio/_io/output/csv/_csv.py +++ b/toughio/_io/output/csv/_csv.py @@ -1,5 +1,5 @@ from ...._common import open_file -from .._common import to_output, ElementOutput +from .._common import ElementOutput, to_output __all__ = [ "read", @@ -58,7 +58,7 @@ def read(filename, file_type, labels_order=None, time_steps=None): if any(i < 0 for i in time_steps): n_steps = _count_time_steps(filename) time_steps = [i if i >= 0 else n_steps + i for i in time_steps] - + time_steps = set(time_steps) with open_file(filename, "r") as f: @@ -115,7 +115,7 @@ def _read_csv(f, file_type, time_steps=None): else: labels[-1].append([l.replace('"', "").strip() for l in line[:ilab]]) - + data[-1].append([float(l.strip()) for l in line[ilab:]]) line = f.readline() diff --git a/toughio/_io/output/petrasim/_petrasim.py b/toughio/_io/output/petrasim/_petrasim.py index b8af3e75..b9d5b916 100644 --- a/toughio/_io/output/petrasim/_petrasim.py +++ b/toughio/_io/output/petrasim/_petrasim.py @@ -1,7 +1,7 @@ import numpy as np from ...._common import open_file -from .._common import to_output, ElementOutput +from .._common import ElementOutput, to_output __all__ = [ "read", @@ -37,7 +37,7 @@ def read(filename, file_type, labels_order=None, time_steps=None): if any(i < 0 for i in time_steps): n_steps = _count_time_steps(filename) time_steps = [i if i >= 0 else n_steps + i for i in time_steps] - + time_steps = set(time_steps) with open_file(filename, "r") as f: @@ -122,9 +122,7 @@ def write(filename, output): if isinstance(out, ElementOutput) else ["TIME [sec]", "ELEM1", "ELEM2", "INDEX"] ) - record = ",".join( - f"{header:>18}" for header in headers_ + headers - ) + record = ",".join(f"{header:>18}" for header in headers_ + headers) f.write(f"{record}\n") # Data diff --git a/toughio/_io/output/save/_save.py b/toughio/_io/output/save/_save.py index e1191fe8..0400c970 100644 --- a/toughio/_io/output/save/_save.py +++ b/toughio/_io/output/save/_save.py @@ -30,7 +30,7 @@ def read(filename, file_type=None, labels_order=None, time_steps=None): """ parameters = tough.read(filename) - + data = [v["values"] for v in parameters["initial_conditions"].values()] data = {f"X{i + 1}": x for i, x in enumerate(np.transpose(data))} data["porosity"] = np.array( diff --git a/toughio/_io/output/tecplot/_tecplot.py b/toughio/_io/output/tecplot/_tecplot.py index ccb4af8a..768bd172 100644 --- a/toughio/_io/output/tecplot/_tecplot.py +++ b/toughio/_io/output/tecplot/_tecplot.py @@ -56,7 +56,7 @@ def read(filename, file_type, labels_order=None, time_steps=None): if any(i < 0 for i in time_steps): n_steps = _count_time_steps(filename) time_steps = [i if i >= 0 else n_steps + i for i in time_steps] - + time_steps = set(time_steps) with open_file(filename, "r") as f: @@ -93,7 +93,7 @@ def read_buffer(f, time_steps=None): if "I" not in zone: raise ValueError() - + else: t_step += 1 diff --git a/toughio/_io/output/tough/_tough.py b/toughio/_io/output/tough/_tough.py index f3d89b7c..5b10da92 100644 --- a/toughio/_io/output/tough/_tough.py +++ b/toughio/_io/output/tough/_tough.py @@ -39,7 +39,7 @@ def read(filename, file_type, labels_order=None, time_steps=None): if any(i < 0 for i in time_steps): n_steps = _count_time_steps(filename) time_steps = [i if i >= 0 else n_steps + i for i in time_steps] - + time_steps = set(time_steps) with open_file(filename, "r") as f: @@ -110,7 +110,7 @@ def _read_table(f, file_type, time_steps=None): if not (time_steps is None or t_step in time_steps): continue - + # Read time step in following line line = next(f).strip() times.append(float(line.split()[0])) diff --git a/toughio/_mesh/_mesh.py b/toughio/_mesh/_mesh.py index 36a090da..a4ea03b6 100644 --- a/toughio/_mesh/_mesh.py +++ b/toughio/_mesh/_mesh.py @@ -490,13 +490,15 @@ def read_output( """ from .. import read_output - from .._io.output._common import ElementOutput, ConnectionOutput + from .._io.output._common import ConnectionOutput, ElementOutput if not isinstance(time_step, int): raise TypeError() if isinstance(file_or_output, str): - out = read_output(file_or_output, time_steps=time_step, connection=connection)[0] + out = read_output( + file_or_output, time_steps=time_step, connection=connection + )[0] else: out = file_or_output