Skip to content

Commit

Permalink
invoke format
Browse files Browse the repository at this point in the history
  • Loading branch information
keurfonluu committed May 19, 2024
1 parent fad2464 commit caefb3b
Show file tree
Hide file tree
Showing 12 changed files with 54 additions and 49 deletions.
2 changes: 1 addition & 1 deletion toughio/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion toughio/_io/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion toughio/_io/h5/_write.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion toughio/_io/output/__init__.py
Original file line number Diff line number Diff line change
@@ -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__ = [
Expand Down
63 changes: 34 additions & 29 deletions toughio/_io/output/_common.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from abc import ABC, abstractmethod
import logging
from abc import ABC, abstractmethod

import numpy as np

Expand All @@ -24,13 +24,13 @@ def __init__(self, time, data, labels=None):
def __getitem__(self):
"""Slice output."""
pass

Check warning on line 26 in toughio/_io/output/_common.py

View check run for this annotation

Codecov / codecov/patch

toughio/_io/output/_common.py#L26

Added line #L26 was not covered by tests

@abstractmethod
def index(self):
"""Get index of element or connection."""
if self.labels is None:
raise AttributeError()

Check warning on line 32 in toughio/_io/output/_common.py

View check run for this annotation

Codecov / codecov/patch

toughio/_io/output/_common.py#L31-L32

Added lines #L31 - L32 were not covered by tests

@property
def n_data(self):
"""Return number of data points."""
Expand All @@ -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

Check warning on line 46 in toughio/_io/output/_common.py

View check run for this annotation

Codecov / codecov/patch

toughio/_io/output/_common.py#L46

Added line #L46 was not covered by tests

@property
def data(self):
"""Return data arrays."""
return self._data

@data.setter
def data(self, value):
self._data = value

Check warning on line 55 in toughio/_io/output/_common.py

View check run for this annotation

Codecov / codecov/patch

toughio/_io/output/_common.py#L55

Added line #L55 was not covered by tests

@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()

Check warning on line 66 in toughio/_io/output/_common.py

View check run for this annotation

Codecov / codecov/patch

toughio/_io/output/_common.py#L64-L66

Added lines #L64 - L66 were not covered by tests

self._labels = list(value)

Check warning on line 68 in toughio/_io/output/_common.py

View check run for this annotation

Codecov / codecov/patch

toughio/_io/output/_common.py#L68

Added line #L68 was not covered by tests

else:
Expand All @@ -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
Expand All @@ -101,26 +101,26 @@ def __getitem__(self, islice):
-------
dict or :class:`toughio.ElementOutput`
Sliced element outputs.
"""
if self.labels is None:
raise AttributeError()

Check warning on line 107 in toughio/_io/output/_common.py

View check run for this annotation

Codecov / codecov/patch

toughio/_io/output/_common.py#L106-L107

Added lines #L106 - L107 were not covered by tests

if np.ndim(islice) == 0:
if isinstance(islice, slice):
islice = np.arange(self.n_data)[islice]

Check warning on line 111 in toughio/_io/output/_common.py

View check run for this annotation

Codecov / codecov/patch

toughio/_io/output/_common.py#L109-L111

Added lines #L109 - L111 were not covered by tests

else:
islice = self.index(islice) if isinstance(islice, str) else islice

Check warning on line 114 in toughio/_io/output/_common.py

View check run for this annotation

Codecov / codecov/patch

toughio/_io/output/_common.py#L114

Added line #L114 was not covered by tests

return {k: v[islice] for k, v in self.data.items()}

Check warning on line 116 in toughio/_io/output/_common.py

View check run for this annotation

Codecov / codecov/patch

toughio/_io/output/_common.py#L116

Added line #L116 was not covered by tests

elif np.ndim(islice) == 1:
islice = [self.index(i) if isinstance(i, str) else i for i in islice]

Check warning on line 119 in toughio/_io/output/_common.py

View check run for this annotation

Codecov / codecov/patch

toughio/_io/output/_common.py#L118-L119

Added lines #L118 - L119 were not covered by tests

else:
raise ValueError()

Check warning on line 122 in toughio/_io/output/_common.py

View check run for this annotation

Codecov / codecov/patch

toughio/_io/output/_common.py#L122

Added line #L122 was not covered by tests

return ElementOutput(

Check warning on line 124 in toughio/_io/output/_common.py

View check run for this annotation

Codecov / codecov/patch

toughio/_io/output/_common.py#L124

Added line #L124 was not covered by tests
self.time,
{k: v[islice] for k, v in self.data.items()},
Expand All @@ -130,7 +130,7 @@ def __getitem__(self, islice):
def index(self, label):
"""
Get index of element.
Parameters
----------
label : str
Expand All @@ -140,10 +140,10 @@ def index(self, label):
-------
int
Index of element.
"""
super().index()

Check warning on line 145 in toughio/_io/output/_common.py

View check run for this annotation

Codecov / codecov/patch

toughio/_io/output/_common.py#L145

Added line #L145 was not covered by tests

return self.labels.index(label)

Check warning on line 147 in toughio/_io/output/_common.py

View check run for this annotation

Codecov / codecov/patch

toughio/_io/output/_common.py#L147

Added line #L147 was not covered by tests


Expand All @@ -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
Expand All @@ -177,15 +177,16 @@ def __getitem__(self, islice):
-------
dict or :class:`toughio.ConnectionOutput`
Sliced connection outputs.
"""
if self.labels is None:
raise AttributeError()

Check warning on line 183 in toughio/_io/output/_common.py

View check run for this annotation

Codecov / codecov/patch

toughio/_io/output/_common.py#L182-L183

Added lines #L182 - L183 were not covered by tests

if np.ndim(islice) == 0:
if isinstance(islice, str):
islice = [

Check warning on line 187 in toughio/_io/output/_common.py

View check run for this annotation

Codecov / codecov/patch

toughio/_io/output/_common.py#L185-L187

Added lines #L185 - L187 were not covered by tests
i for i, (label1, label2) in enumerate(self.labels)
i
for i, (label1, label2) in enumerate(self.labels)
if label1 == islice or label2 == islice
]

Expand All @@ -194,13 +195,13 @@ def __getitem__(self, islice):

else:
return {k: v[islice] for k, v in self.data.items()}

Check warning on line 197 in toughio/_io/output/_common.py

View check run for this annotation

Codecov / codecov/patch

toughio/_io/output/_common.py#L197

Added line #L197 was not covered by tests

elif np.ndim(islice) <= 2:
islice = [self.index(*i) if np.ndim(i) == 1 else i for i in islice]

Check warning on line 200 in toughio/_io/output/_common.py

View check run for this annotation

Codecov / codecov/patch

toughio/_io/output/_common.py#L199-L200

Added lines #L199 - L200 were not covered by tests

else:
raise ValueError()

Check warning on line 203 in toughio/_io/output/_common.py

View check run for this annotation

Codecov / codecov/patch

toughio/_io/output/_common.py#L203

Added line #L203 was not covered by tests

return ConnectionOutput(

Check warning on line 205 in toughio/_io/output/_common.py

View check run for this annotation

Codecov / codecov/patch

toughio/_io/output/_common.py#L205

Added line #L205 was not covered by tests
self.time,
{k: v[islice] for k, v in self.data.items()},
Expand All @@ -210,7 +211,7 @@ def __getitem__(self, islice):
def index(self, label1, label2):
"""
Get index of connection.
Parameters
----------
label1 : str
Expand All @@ -222,7 +223,7 @@ def index(self, label1, label2):
-------
int
Index of connection.
"""
super().index()
labels = ["".join(label) for label in self.labels]

Check warning on line 229 in toughio/_io/output/_common.py

View check run for this annotation

Codecov / codecov/patch

toughio/_io/output/_common.py#L228-L229

Added lines #L228 - L229 were not covered by tests
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion toughio/_io/output/_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -184,5 +184,5 @@ def get_output_type(filename):
else:
file_format = "element"
file_type = None

Check warning on line 186 in toughio/_io/output/_helpers.py

View check run for this annotation

Codecov / codecov/patch

toughio/_io/output/_helpers.py#L185-L186

Added lines #L185 - L186 were not covered by tests

return file_type, file_format
6 changes: 3 additions & 3 deletions toughio/_io/output/csv/_csv.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from ...._common import open_file
from .._common import to_output, ElementOutput
from .._common import ElementOutput, to_output

__all__ = [
"read",
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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()
Expand Down
8 changes: 3 additions & 5 deletions toughio/_io/output/petrasim/_petrasim.py
Original file line number Diff line number Diff line change
@@ -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",
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion toughio/_io/output/save/_save.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
4 changes: 2 additions & 2 deletions toughio/_io/output/tecplot/_tecplot.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -93,7 +93,7 @@ def read_buffer(f, time_steps=None):

if "I" not in zone:
raise ValueError()

else:
t_step += 1

Expand Down
4 changes: 2 additions & 2 deletions toughio/_io/output/tough/_tough.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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]))
Expand Down
6 changes: 4 additions & 2 deletions toughio/_mesh/_mesh.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(

Check warning on line 499 in toughio/_mesh/_mesh.py

View check run for this annotation

Codecov / codecov/patch

toughio/_mesh/_mesh.py#L499

Added line #L499 was not covered by tests
file_or_output, time_steps=time_step, connection=connection
)[0]

else:
out = file_or_output
Expand Down

0 comments on commit caefb3b

Please sign in to comment.