openneuropet / PET2BIDS

PET2BIDS helps you convert your PET data into BIDS! raw PET scanner files (e.g. ecat, dicom) and additional side files like .e.g excel sheets -- paper @JOSS https://doi.org/10.21105/joss.06067
https://pet2bids.readthedocs.io
MIT License
25 stars 20 forks source link

BUG Times, reported in sidecar JSON not calculated from TimeZero #288

Closed nbeliy closed 4 months ago

nbeliy commented 5 months ago

Describe the bug BIDS standard for PET states that all times in sidecar JSON must be calculated w.r.t. TimeZero:

Time of start of scan with respect to TimeZero in the default unit seconds.

Running ecat_cli.py on my ECAT files, I found the times calculated as absolute times:

    "PharmaceuticalDoseTime": "11:38:59",
    "TimeZero": "11:38:59",
    "ScanStart": "11:38:59",

To Reproduce

> python pypet2bids/ecat_cli.py ../../xxxx_de10.v -c
Reading subheader from frame 1
...
Reading subheader from frame 34
2024-03-21 16:05:22,827 - pypet2bids - WARNING - Metadata TimeZero is missing -- set to ScanStart or empty to use the scanning time as injection time (ecat2nii.py:104)
Loading frame 34
...
Loading frame 1

Expected behaviour The output sidecar json should show times relative to TimeZero:

    "PharmaceuticalDoseTime": "0",
    "TimeZero": "11:38:59",
    "ScanStart": "0",

Desktop (please complete the following information):

mnoergaard commented 5 months ago

Hi @nbeliy. Thank you for raising this issue. You are right - both PharmaceuticalDoseTime and and ScanStart should be numbers (or array of numbers for PharmaceuticalDoseTime) relative to TimeZero in seconds (not strings as in your example). @bendhouseart can confirm, but it should be a relatively quick fix using something like this example:

from datetime import datetime

time_str1 = "11:38:59"
time_str2 = "11:38:59"

time1 = datetime.strptime(time_str1, '%H:%M:%S')
time2 = datetime.strptime(time_str2, '%H:%M:%S')

time_difference = time2 - time1

round(time_difference.total_seconds())
bendhouseart commented 4 months ago

@nbeliy updated on repo. Will add this and some other changes in version 1.3.6 and push to pypi next week on not a Friday.