Skip to content

Commit

Permalink
refactor and fix
Browse files Browse the repository at this point in the history
  • Loading branch information
Remi-Gau committed Nov 7, 2023
1 parent a10337b commit 04f809c
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 32 deletions.
55 changes: 28 additions & 27 deletions eye2bids/edf2bids.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,6 @@ def _convert_edf_to_asc_samples(input_file: str | Path) -> Path:


def _calibrations(df: pd.DataFrame) -> pd.DataFrame:
print(df)
return df[df[3] == "CALIBRATION"]


Expand Down Expand Up @@ -350,7 +349,6 @@ def edf2bids(

# CONVERSION events
events_asc_file = _convert_edf_to_asc_events(input_file)
samples_asc_file = _convert_edf_to_asc_samples(input_file)

events = _load_asc_file(events_asc_file)
df_ms = _load_asc_file_as_df(events_asc_file)
Expand All @@ -362,12 +360,7 @@ def edf2bids(
with open(metadata_file) as f:
metadata = yaml.load(f, Loader=SafeLoader)

# file naming check

filename = Path(input_file).stem
substring_eyetrack = "_eyetrack"
substring_events = "_events"

# events.json Metadata
eyetrack_json = {
"Manufacturer": "SR-Research",
"EnvironmentCoordinates": metadata.get("EnvironmentCoordinates"),
Expand Down Expand Up @@ -396,13 +389,14 @@ def edf2bids(
"StopTime": _extract_StopTime(events),
}

suffix = "_eyetrack" if substring_eyetrack not in filename else ""
output_filename = output_dir / f"{filename}{suffix}.json"
output_filename = generate_output_filename(
output_dir=output_dir, input_file=input_file, suffix="_eyetrack", extension="json"
)
with open(output_filename, "w") as outfile:
json.dump(eyetrack_json, outfile, indent=4)
e2b_log.info(f"file generated: {output_filename}")

# Events.json Metadata
# events.json Metadata
events_json = {
"InstitutionAddress": metadata.get("InstitutionAddress"),
"InstitutionName": metadata.get("InstitutionName"),
Expand All @@ -415,26 +409,33 @@ def edf2bids(
"TaskName": _extract_TaskName(events),
}

if substring_events not in filename:
with open(output_dir / (filename + "_events.json"), "w") as outfile:
json.dump(events_json, outfile, indent=4)
e2b_log.info(f"file generated: {output_dir / (filename + '_events.json')}")
else:
with open(output_dir / (filename + ".json"), "w") as outfile:
json.dump(events_json, outfile, indent=4)
e2b_log.info(f"file generated: {output_dir / (filename + '.json')}")
output_filename = generate_output_filename(
output_dir=output_dir, input_file=input_file, suffix="_events", extension="json"
)
with open(output_filename, "w") as outfile:
json.dump(events_json, outfile, indent=4)
e2b_log.info(f"file generated: {output_filename}")

# Samples to eyetrack.tsv
samples_asc_file = _convert_edf_to_asc_samples(input_file)
eyetrack_tsv = _samples_to_data_frame(samples_asc_file)

if substring_eyetrack not in filename:
with open(output_dir / (filename + "_eyetrack.tsv"), "w") as outfile:
eyetrack_tsv.to_csv(outfile, sep="\t", index=False, compression="gzip")
e2b_log.info(f"file generated: {output_dir / (filename + '_eyetrack.tsv')}")
else:
with open(output_dir / (filename + ".tsv"), "w") as outfile:
eyetrack_tsv.to_csv(outfile, sep="\t", index=False, compression="gzip")
e2b_log.info(f"file generated: {output_dir / (filename + '.tsv')}")
output_filename = generate_output_filename(
output_dir=output_dir, input_file=input_file, suffix="_eyetrack", extension="tsv"
)
with open(output_filename, "w") as outfile:
eyetrack_tsv.to_csv(outfile, sep="\t", index=False, compression="gzip")
e2b_log.info(f"file generated: {output_filename}")


def generate_output_filename(
output_dir: Path, input_file: Path, suffix: str, extension: str
) -> Path:
"""Generate output filename."""
filename = Path(input_file).stem
if filename.endswith(suffix):
suffix = ""
return output_dir / f"{filename}{suffix}.{extension}"


if __name__ == "__main__":
Expand Down
15 changes: 15 additions & 0 deletions tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,18 @@ def test_edf_cli(use_relative_path, metadata_file, output_dir, eyelink_test_data
command.extend(["--output_dir", output_dir])

cli(command)


@pytest.mark.skipif(not _check_edf2asc_present(), reason="edf2asc missing")
@pytest.mark.parametrize(
"input_file", edf_test_files(input_dir=data_dir() / "osf" / "eyelink")
)
def test_all_edf_files(input_file):
command = [
"eye2bids",
"--input_file",
str(input_file),
"--output_dir",
str(data_dir() / "output"),
]
cli(command)
7 changes: 2 additions & 5 deletions tests/test_edf2bids.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ def test_convert_edf_to_asc_events(input_file):
@pytest.mark.skipif(not _check_edf2asc_present(), reason="edf2asc missing")
@pytest.mark.parametrize("metadata_file", [data_dir() / "metadata.yml", None])
def test_edf_end_to_end(metadata_file, eyelink_test_data_dir):
input_dir = eyelink_test_data_dir / "decisions"
input_dir = eyelink_test_data_dir / "satf"
input_file = edf_test_files(input_dir=input_dir)[0]

output_dir = data_dir() / "output"
Expand All @@ -61,7 +61,7 @@ def test_edf_end_to_end(metadata_file, eyelink_test_data_dir):
assert expected_data_sidecar.exists()
with open(expected_data_sidecar) as f:
eyetrack = json.load(f)
assert eyetrack["SamplingFrequency"] == 1000
assert eyetrack["SamplingFrequency"] == 500
assert eyetrack["RecordedEye"] == "Right"


Expand Down Expand Up @@ -99,9 +99,6 @@ def test_extract_CalibrationType(folder, expected, eyelink_test_data_dir):
def test_extract_ScreenResolution(folder, expected, eyelink_test_data_dir):
input_dir = eyelink_test_data_dir / folder
asc_file = asc_test_files(input_dir=input_dir)[0]
print(asc_test_files(input_dir=input_dir))
print(asc_file)
print(str(asc_file).endswith("samples.asc"))
df_ms_reduced = _load_asc_file_as_reduced_df(asc_file)
assert _extract_ScreenResolution(df_ms_reduced) == expected

Expand Down

0 comments on commit 04f809c

Please sign in to comment.