Sidecar_changes in dcm2bids version 3.0.1 does not write numbers

Summary of what happened:

I am using the latest version of dcm2bids, namely 3.0.1. When using sidecar_changes, it write the text and boolean but not numbers. Any idea?

Command used (and if a helper script was used, a link to the helper script or the command generated):

Version:

3.0.1

Environment (Docker, Singularity, custom installation):

custom installation

Data formatted according to a validatable standard? Please provide the output of the validator:

Relevant log outputs (up to 20 lines)

I used the following to add extra key values to sidecar:

      "sidecar_changes": {
         "RepetitionTimePreparation": 7.4,
         "M0Type": "Separate",
         "LabelingDuration": 1.800,
         "PostLabelingDelay": [1.500, 1.500, 1.600, 1.600, 1.700, 1.700, 1.800, 1.800, 2.000,  2.000],
         "TotalAcquiredPairs": 1,
         "AcquisitionVoxelSize": [1.6, 1.6, 3.0],
         "BackgroundSuppression": true

Screenshots / relevant information: Here is how it looks like

"RepetitionTimePreparation": [],
    "M0Type": "Separate",
    "LabelingDuration": [],
    "PostLabelingDelay": [],
    "TotalAcquiredPairs": [],
    "AcquisitionVoxelSize": [],
    "BackgroundSuppression": true

Hi @igholami, and welcome to Neurostars!

As you can see in the code here, dcm2bids is currently set to only write out string and boolean objects to the JSONs. So, in the meantime, you can make all of the numerical objects strings.

@abore, would it be worth opening a PR to write out direct numerical objects to JSONs?

Best,
Steven

Hi @Steven,
Thanks for your fast reply. But if I pass the numbers as string, doesn’t it make problem for the analysis tools which read this sidecars? Because in this case I will have “7.4” instead of 7.4. Or even for the arrays I am not sure if this will be considered as array. This is basically string not arrays of numbers.
“[1.500, 1.500, 1.600, 1.600, 1.700, 1.700, 1.800, 1.800, 2.000, 2.000]”

Depends on the analysis and tools you are using, I suppose. If you are making a script yourself it shouldn’t be too hard to just convert the string to numerical if needed. I personally do not know of any toolboxes or analyses that use those fields.

I think you would need to do it as ["1.500", "1.500" .... etc].

Best,
Steven

OK. Thank you very much for your kind support.

No problem! Will update here if a PR adds direct numerical support too.

1 Like

Just to say I’ve opened an issue to make sure we fix this before the next release :slight_smile:

I appreciate that. Thanks.

Hey @igholami,

If you have your dcm2bids installation locally, can you try to make the following small change and see if it helps?

  1. Go to dcm2bids/acquisition.py file
  2. Look the code block in line 214
    Try changing to the following:
 for val in value:
    if isinstance(val, (bool, str, int, float)):
        if val not in idList and key in DEFAULT.keyWithPathsidecar_changes:
            logging.warning(f"No id found for '{key}' value '{val}'.")
            logging.warning(f"No sidecar changes for field '{key}' "
                            f"will be made "
                            f"for json file '{self.dstFile}.json' "
                            "with this id.")
        else:
            values.append(idList.get(val, val))
            if values[-1] != val and isinstance(values[-1], str):
                if isinstance(values[-1], list):
                    values[-1] = "bids::" + values[-1][0]
                else:
                     values[-1] = "bids::" + values[-1]
  1. Rerun your dcm2bids code.

Perhaps make a copy of the original acquisition.py called acquisition_ORIG.py, so you do not lose it. Then make changes to acquisition.py after having the copy.

Best,
Steven

Hi @Steven, Thanks for that. I have a very naive question. I installed dcm2bids through conda, how can I access the source code in order to modify it. Alternatively I downloaded the source code and made the changes you suggested. But how can I run this source code locally?

python /pathto/dcm2bids/cli/dcm2bids.py ARGUMENTS

You can also make a new conda environment, cd to the cloned repo, and run pip install -e ., and then run dcm2bids in that environment as normal.

Thank you very much. It solved the issue. I appreciate your effort.

1 Like

Great, thanks for testing it out and for reporting the issue! I just opened a PR on the repo so it should be a permanent feature in the next release. Allowing Numericals in JSON custom fields by smeisler · Pull Request #250 · UNFmontreal/Dcm2Bids · GitHub

1 Like

Hi @Steven, Sorry if here is not a good ticket to ask this. But when I run the dcm2bids using the command " python /pathto/dcm2bids/cli/dcm2bids.py ARGUMENTS"
I get the following error.
“ModuleNotFoundError: No module named ‘dcm2bids.dcm2bids_gen’; ‘dcm2bids’ is not a package”

So, basically this line of the code “from dcm2bids.dcm2bids_gen import Dcm2BidsGen”, is where the error is originated. Based on this, we should have dcm2bids.py inside a folder that contains the dcm2bids directory with dcm2bids_gen.py and other files in it. However, the structure of the original directory is different. Am I doing something wrong?

In that case I would do this method: