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

A snippet for directly writing the dataframe "recording" and metadata into BIDS format #480

Open
Evelyn92 opened this issue Jun 19, 2024 · 0 comments
Labels
eye-tracking ET and conversion to BIDS

Comments

@Evelyn92
Copy link

Hi,
First of all, thank you for your great work!
I attempted to write an EDF file into BIDS and followed the instructions on this page:
https://www.axonlab.org/hcph-sops/data-management/edf-to-bids/#writing-the-data-into-the-bids-structure
After parsing the recording and messages from the Pandas DataFrame, I became uncertain about how to use the 'write_bids' function.
So here I provide a code snippet for directly converting 'recording' and 'metadata' into BIDS format.

  • Convert the type of "int64" or "int32" in "metadata" into Type "int" for proper json saving.
def convert_to_int(metadata):
    if 'CalibrationCount' in metadata:
        metadata['CalibrationCount'] = int(metadata['CalibrationCount']) if isinstance(metadata['CalibrationCount'], (np.int32, np.int64, int)) else metadata['CalibrationCount']
    if "CalibrationLog" in metadata:
        metadata["CalibrationLog"] = [(int(x[0]),x[1]) if isinstance(x[0], (np.int32, np.int64, int)) else x for x in metadata['CalibrationLog']]
    return metadata

convert_metadata = convert_to_int(metadata)
  • Save BIDS from dataframe "recording" and dict "metadata"
def write_bids_from_df(
    recording, metadata,
    out_dir,
    filename,
    # exp_run: str | Path,
) -> List[str]:
    """
    Directly save the eye-tracking recording/metadata into a  BIDS structure.

    Parameters
    ----------
    recording : dataframe
        The recording data extracted from the EDF file.
    metadata : dict
        The metadata extracted from the EDF file.
    out_dir : obj:`os.pathlike`
        The path of EDF file. Refers to the folder (not the EDF file).
    filename: str
        The filename of the EDF file. The file name without the suffix, eg: "Subject001"
    
    Returns
    -------
    List[str]
        A list of generated files.

    """

    out_json = out_dir / (filename + ".json")
    out_json.write_text(
        json.dumps(metadata, sort_keys=True, indent=2)
    )

    # Write out data
    out_tsvgz = out_dir / (filename + ".tsv.gz")

    recording.to_csv(
        out_tsvgz,
        sep="\t",
        index=True,
        header=True,
        compression="gzip",
        na_rep="n/a",
    )

    return str(out_tsvgz), str(out_json)
out_dir = DATA_PATH
# eg: DATA_PATH = Path("\\...\\ET_EDF")
filename = edf_name.split('.')[0]
#eg: filename = 'Subject001'
print(f'bid filename: {filename}')

write_bids_from_df(
    recording, convert_metadata,
    out_dir,
    filename,
)

#455

@oesteban oesteban added the eye-tracking ET and conversion to BIDS label Jun 20, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
eye-tracking ET and conversion to BIDS
Projects
None yet
Development

No branches or pull requests

2 participants