Skip to content

Commit

Permalink
Updated data parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
vyrjana committed Nov 2, 2024
1 parent d28e168 commit 1239a13
Show file tree
Hide file tree
Showing 8 changed files with 47 additions and 21 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
- Updated the `plot_gamma` function to include a horizontal line marking zero on the y-axis.
- Updated the automatic adjustment of initial values of circuit parameters to be bypassed when a non-default value is detected in a `Circuit` that is provided to the m(RQ)-fit method without a corresponding `FitResult`.
- Updated the m(RQ)-fit method to support resistive-inductive `(RQ)` elements. The `n` parameter of the `ConstantPhaseElement` instance would then need to be `-1.0 <= n < 0.0`.
- Updated the parsing of data files to better support cases where a decimal comma is used instead of a decimal point.
- Fixed a bug that caused methods such as `DRTResult.get_peaks` to miss peaks at the time constant extremes.
- Fixed a bug that caused an element's parameters in `FitResult.to_parameters_dataframe` to not be in a sorted order.
- Fixed the previously unimplemented `FitResult.get_parameters` method.
Expand Down
11 changes: 7 additions & 4 deletions src/pyimpspec/data/formats/dfr.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,10 @@
Path,
Union,
)
from .helpers import _validate_path
from .helpers import (
_parse_string_as_float,
_validate_path,
)


def parse_dfr(path: Union[str, Path]) -> List[DataSet]:
Expand Down Expand Up @@ -81,9 +84,9 @@ def parse_dfr(path: Union[str, Path]) -> List[DataSet]:
"Expected to have nine lines to parse for each measured frequency"
)

freq.append(float(lines.pop(0)))
real.append(float(lines.pop(0)))
imag.append(-float(lines.pop(0)))
freq.append(_parse_string_as_float(lines.pop(0)))
real.append(_parse_string_as_float(lines.pop(0)))
imag.append(-_parse_string_as_float(lines.pop(0)))
lines.pop(0) # E_dc
lines.pop(0) # I_dc
lines.pop(0) # time
Expand Down
7 changes: 5 additions & 2 deletions src/pyimpspec/data/formats/dta.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,10 @@
Path,
Union,
)
from .helpers import _validate_path
from .helpers import (
_parse_string_as_float,
_validate_path,
)


def parse_dta(path: Union[str, Path]) -> List[DataSet]:
Expand Down Expand Up @@ -99,7 +102,7 @@ def parse_dta(path: Union[str, Path]) -> List[DataSet]:
# Pt Time Freq Zreal Zimag Zsig Zmod Zphz Idc Vdc IERange
# s Hz ohm ohm V ohm ° A V
try:
values: List[float] = list(map(float, line.split()))
values: List[float] = list(map(_parse_string_as_float, line.split()))
except ValueError:
break

Expand Down
7 changes: 7 additions & 0 deletions src/pyimpspec/data/formats/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,10 @@ def _validate_path(path: Union[str, Path]):
raise FileNotFoundError(path)
else:
raise TypeError(f"Expected a string or pathlib.Path instead of {path=}")


def _parse_string_as_float(string: str) -> float:
try:
return float(string)
except ValueError:
return float(string.replace(",", "."))
7 changes: 5 additions & 2 deletions src/pyimpspec/data/formats/i2b.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,10 @@
Path,
Union,
)
from .helpers import _validate_path
from .helpers import (
_parse_string_as_float,
_validate_path,
)


def parse_i2b(path: Union[str, Path]) -> List[DataSet]:
Expand Down Expand Up @@ -65,7 +68,7 @@ def parse_i2b(path: Union[str, Path]) -> List[DataSet]:
f: float
re: float
im: float
f, re, im = tuple(map(float, line.split(" ")))
f, re, im = tuple(map(_parse_string_as_float, line.split(" ")))

freq.append(f)
real.append(re)
Expand Down
13 changes: 8 additions & 5 deletions src/pyimpspec/data/formats/ids.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,10 @@
Union,
_is_integer,
)
from .helpers import _validate_path
from .helpers import (
_parse_string_as_float,
_validate_path,
)


def _extract_primary_data(
Expand All @@ -58,16 +61,16 @@ def _extract_primary_data(

while lines:
line: str = lines.pop(0)
values: List[str] = line.replace(",", ".").split()
values: List[str] = line.split()

if len(values) != 3:
raise UnsupportedFileFormat(
f"Expected to parse three values instead of {values=}"
)

re.append(float(values.pop(0)))
im.append(float(values.pop(0)))
f.append(float(values.pop(0)))
re.append(_parse_string_as_float(values.pop(0)))
im.append(_parse_string_as_float(values.pop(0)))
f.append(_parse_string_as_float(values.pop(0)))

if len(re) == num_freq:
break
Expand Down
11 changes: 7 additions & 4 deletions src/pyimpspec/data/formats/mpt.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,10 @@
Path,
Union,
)
from .helpers import _validate_path
from .helpers import (
_parse_string_as_float,
_validate_path,
)


def parse_mpt(path: Union[str, Path]) -> List[DataSet]:
Expand Down Expand Up @@ -68,9 +71,9 @@ def parse_mpt(path: Union[str, Path]) -> List[DataSet]:
if len(columns) < 3:
raise UnsupportedFileFormat(f"Expected to parse three columns instead of {len(columns)} columns")

freq.append(float(columns[0]))
real.append(float(columns[1]))
imag.append(-float(columns[2]))
freq.append(_parse_string_as_float(columns[0]))
real.append(_parse_string_as_float(columns[1]))
imag.append(-_parse_string_as_float(columns[2]))

if not (len(freq) == len(real) == len(imag) > 0):
raise ValueError(
Expand Down
11 changes: 7 additions & 4 deletions src/pyimpspec/data/formats/p00.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,10 @@
Path,
Union,
)
from .helpers import _validate_path
from .helpers import (
_parse_string_as_float,
_validate_path,
)


def parse_p00(path: Union[str, Path]) -> List[DataSet]:
Expand Down Expand Up @@ -71,9 +74,9 @@ def parse_p00(path: Union[str, Path]) -> List[DataSet]:
if len(columns) != 6:
raise UnsupportedFileFormat(f"Expected to parse 6 columns instead of {len(columns)} columns")

freq.append(float(columns[0]))
real.append(float(columns[1]))
imag.append(-float(columns[2]))
freq.append(_parse_string_as_float(columns[0]))
real.append(_parse_string_as_float(columns[1]))
imag.append(-_parse_string_as_float(columns[2]))

if not (len(freq) == len(real) == len(imag) > 0):
raise ValueError(
Expand Down

0 comments on commit 1239a13

Please sign in to comment.