Skip to content

Commit

Permalink
Re-introduce the heuristic used for param parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewrosss committed Nov 3, 2023
1 parent 7b52f89 commit 6d097e7
Showing 1 changed file with 15 additions and 5 deletions.
20 changes: 15 additions & 5 deletions src/fmri_physio_log/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from typing import NamedTuple
from typing import TextIO

from fmri_physio_log._generated import Lark_StandAlone as _Lark_StandAlone
from fmri_physio_log._generated import Lark_StandAlone as _PhysioLogParser
from fmri_physio_log._generated import Tree as _Tree
from fmri_physio_log._generated import Visitor_Recursive as _Visitor_Recursive

Expand All @@ -18,13 +18,20 @@


class PhysioLog:
def __init__(self, content: str, *, n_params: int = 4):
def __init__(self, content: str, *, n_params: int | None = None):
# constructor args
self.content = content
self.n_params = n_params
self.n_params = (
self.determine_params_heuristically(content)
if n_params is None
else n_params
)

self._parser = _Lark_StandAlone()
# internal attributes
self._parser = _PhysioLogParser()
self._visitor = _PhysioLogVisitor()

# public attributes
self.data: list[int]

self.params: tuple[int, ...]
Expand Down Expand Up @@ -73,7 +80,7 @@ def _body(self) -> None:
and the data.
"""
self.params = tuple(self._visitor._data[: self.n_params])
self.rate = self.params[2] if len(self.params) == 4 else self.params[3]
self.rate = self.params[2] if self.n_params == 4 else self.params[3]
self.info = self._visitor._info[:]
self.ts = self._visitor._data[self.n_params :]

Expand All @@ -84,13 +91,16 @@ def _footer(self) -> None:
(freq, per, min, max, etc.), the 'nr summary' and 'log times'
"""
# set the nr summary attributes
# TODO(andrewrosss): add runtime (length) checks splatting args
self.nr = NrSummary(*self._visitor._nr)

# set the measurement summary attributes
# TODO(andrewrosss): add runtime (length) checks splatting args
for attr, values in self._visitor._summaries.items():
setattr(self, attr.lower(), MeasurementSummary(*values))

# set the log time attributes
# TODO(andrewrosss): add runtime (length) checks splatting args
for attr, values in self._visitor._logs.items():
setattr(self, attr.lower(), LogTime(*values))

Expand Down

0 comments on commit 6d097e7

Please sign in to comment.