Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extract information from temporary files for unfinished experiments #102

Open
wants to merge 7 commits into
base: master
Choose a base branch
from

Conversation

JhonFlash3008
Copy link
Contributor

When acquisition is still running, the mpr file does not contain the log or loop modules. Instead, these data are stored in a mpl and _LOOP.txt file, respectively and are adeed to the mpr files only when the experiment stops (and the temporary files are deleted).
I added 2 functions to extract the loop_index and the timestamp from these temp files during the MPRfile initialization. They are not called if a loop_module and/or a log_module is already found

I wrote the tests but unfortunately I cannot upload the test files, I receive an error saying I exceeded the LFS quota

chatcannon and others added 7 commits February 7, 2024 10:00
…p_index and timestamp from the temporary _LOOP.txt and .mpl files during MPRfile initialization

Added unitary tests but cannot upload test files due to LFS quota exceeded
from 498 to 502, had to replace other column ids. This is dangerous, as it also modifies dtype from f8 to f4
- supports files without setting module --> extracted loops and cycles
- handles conflict between colIDs for the "stack mode" (VMP3 hardware)
- supports old files with a timestamp with variable byte index
- added lots of new colIDs
- supports data_module version 1
- MPRfile attributes startdate, enddate, timestamp are initialized with None value because it is weird if all MPRfile objects don't have the same attributes
…ules, but only if the given argument of MPRfile is a path. This avoids keeping a file busy if an error occurred during class initialization
Copy link
Collaborator

@chatcannon chatcannon left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would rather see os.path.foo than path.foo.

@@ -9,7 +9,7 @@

import re
import csv
from os import SEEK_SET
from os import SEEK_SET, path
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
from os import SEEK_SET, path
from os import SEEK_SET
import os.path

@@ -614,49 +828,39 @@ def __init__(self, file_or_path):
raise ValueError(
"Unrecognised version for data module: %d" % data_module["version"]
)
else:
if path.isfile(loop_file):
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if path.isfile(loop_file):
if os.path.isfile(loop_file):

raise ValueError(
"Date mismatch:\n"
+ " Start date: %s\n" % self.startdate
+ " End date: %s\n" % self.enddate
+ " Timestamp: %s\n" % self.timestamp
)
else:
if path.isfile(log_file):
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if path.isfile(log_file):
if os.path.isfile(log_file):

Comment on lines +553 to +554
for key, value in stack_mode_colID_dtype_map.items():
dtype_map[key] = value
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
for key, value in stack_mode_colID_dtype_map.items():
dtype_map[key] = value
dtype_map.update(stack_mode_colID_dtype_map)

"Column ID {cid} after column {prev} "
"is unknown".format(cid=colID, prev=type_list[-1][0])
)
if type_list:
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if type_list:
if len(type_list) > 0:

Better to make it explicit what you are checking than relying on implicit boolean conversion.

Comment on lines +607 to +608
if isinstance(file_or_path, str):
fileobj.close()
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should really be done with a contextmanager but the if ...: close() is better than nothing.

@@ -522,7 +645,83 @@ def read_VMP_modules(fileobj, read_module_data=True):
yield hdr_dict
fileobj.seek(hdr_dict["offset"] + hdr_dict["length"], SEEK_SET)

def loop_from_file(file: str, encoding: str ="latin1"):
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, this is the code that matches the description of the pull request.

It would be nicer if you kept this change and then moved the other changes to separate pull requests with their own descriptions.


if maybe_log_module:
(log_module,) = maybe_log_module
self.enddate = parse_BioLogic_date(log_module["date"])

# There is a timestamp at either 465 or 469 bytes
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This comment needs to be updated rather than just removed completely.

Comment on lines -260 to +282
index_inf_100k = np.where(mpr.data["freq/Hz"] < 100000)[0]
assert_allclose(
mpr.data[index_inf_100k][fieldname],
mpt[index_inf_100k][fieldname].astype(mpr.data[fieldname].dtype),
)
if len(mpr.data) == 1:
if mpt["THD Ewe/%"] != -1:
assert_allclose(
mpr.data[fieldname],
mpt[fieldname].astype(mpr.data[fieldname].dtype),
)
else:
indexes = np.where(mpt["THD Ewe/%"] != -1)[0]

assert_allclose(
mpr.data[indexes][fieldname],
mpt[indexes][fieldname].astype(mpr.data[fieldname].dtype),
)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't understand what you are changing here and why.

Comment on lines +386 to +391
if mpr.loop_index is None:
raise AssertionError("No loop_index found")
elif not len(mpr.loop_index) == 4:
raise AssertionError("loop_index is not the right size")
elif not (mpr.loop_index == [0, 4, 8, 11]).all():
raise AssertionError("loop_index values are wrong")
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There should be a way to assert all of this in one statement using the pytest numpy integrations.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if mpr.loop_index is None:
raise AssertionError("No loop_index found")
elif not len(mpr.loop_index) == 4:
raise AssertionError("loop_index is not the right size")
elif not (mpr.loop_index == [0, 4, 8, 11]).all():
raise AssertionError("loop_index values are wrong")
assert mpr.loop_index is not None
assert_array_equal(mpr.loop_index, [0, 4, 8, 11])

mpr = MPRfile(os.path.join(testdata_dir, "running", "running_OCV.mpr"))
if not hasattr(mpr, "timestamp"):
raise AssertionError("No timestamp found")
elif not mpr.timestamp.timestamp() == 1707299985.908:
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With floating point numbers you need to use assert_close() or similar, because they will not always be exactly equal.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You changed the exec bit on this file which will stop it working as a command line script. Undo that change.

Comment on lines +398 to +401
if not hasattr(mpr, "timestamp"):
raise AssertionError("No timestamp found")
elif not mpr.timestamp.timestamp() == 1707299985.908:
raise AssertionError("timestamp value is wrong")
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if not hasattr(mpr, "timestamp"):
raise AssertionError("No timestamp found")
elif not mpr.timestamp.timestamp() == 1707299985.908:
raise AssertionError("timestamp value is wrong")
assert mpr.timestamp.timestamp() == pytest.approx(1707299985.908)

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This might still break if you have effects depending on the system timezone. I need to check this myself locally.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants