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

Minor bug fixes #37

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,21 @@ Extract NIHM Data Archive compatible metadata from Brain Imaging Data Structure
GUID_MAPPING Path to a text file with participant_id to GUID mapping.
You will need to use the GUID Tool
(https://ndar.nih.gov/contribute.html) to generate GUIDs
for your participants.
for your participants. Formatted as <participant_id> - <GUID>
OUTPUT_DIRECTORY Directory where NDA files will be stored
EXPERIMENT_ID experiment_id value assigned from NDA after setting the study up throught the NDA website (int)

optional arguments:
-h, --help show this help message and exit


## GUID_MAPPING file format
The is the file format produced by the GUID Tool: one line per subject in the format
The is the file format produced by the GUID Tool: one line per subject in the format:

`<participant_id> - <GUID>`

If your ids are in the format of `sub-sid001420`, be sure to not include 'sub-' in your `<participant_id>` e.g., `<sid001420> - <GUID>`

## Example outputs
See [/examples](/examples)

Expand Down
36 changes: 16 additions & 20 deletions bids2nda/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,9 @@ def cosine_to_orientation(iop):
express the direction you move, in the DPCS, as you move from row to row,
and therefore as the row index changes.

Notes:
Modified Yarik's original solution from https://stackoverflow.com/a/45469577 to use the argmax for increaesd flexibility.
Copy link
Collaborator

@yarikoptic yarikoptic Jul 12, 2023

Choose a reason for hiding this comment

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

~~hm -- was it mine? says user6867490 there. ~~
note the typo

Suggested change
Modified Yarik's original solution from https://stackoverflow.com/a/45469577 to use the argmax for increaesd flexibility.
Modified Yarik's original solution from https://stackoverflow.com/a/45469577 to use the argmax for increased flexibility.

I will bolt on codespell now

Copy link
Collaborator

Choose a reason for hiding this comment

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

actually -- I don't really see a reason for this note here at all. But I feel it is time to add tests for this function to ensure that it operates as expected... ATM can't quite grasp the ramification of the change, in particular for those cases where before a RuntimeError would have been raised.


Parameters
----------
iop: list of float
Expand All @@ -107,21 +110,9 @@ def cosine_to_orientation(iop):
-------
{'Axial', 'Coronal', 'Sagittal'}
"""
# Solution based on https://stackoverflow.com/a/45469577
iop_round = np.round(iop)
plane = np.cross(iop_round[0:3], iop_round[3:6])
plane = np.abs(plane)
if plane[0] == 1:
return "Sagittal"
elif plane[1] == 1:
return "Coronal"
elif plane[2] == 1:
return "Axial"
else:
raise RuntimeError(
"Could not deduce the image orientation of %r. 'plane' value is %r"
% (iop, plane)
)
planes = ['Sagittal', 'Coronal', 'Axial']
plane = np.abs(np.cross(np.round(iop[0:3]), np.round(iop[3:6])))
return planes[np.argmax(plane)]


def run(args):
Expand Down Expand Up @@ -204,10 +195,10 @@ def run(args):
suffix = file.split("_")[-1].split(".")[0]
if suffix == "bold":
description = suffix + " " + metadata["TaskName"]
dict_append(image03_dict, 'experiment_id', metadata.get("ExperimentID", ""))
dict_append(image03_dict, 'experiment_id', args.experiment_id)
else:
description = suffix
dict_append(image03_dict, 'experiment_id', '')
dict_append(image03_dict, 'experiment_id', args.experiment_id)
# Shortcut for the global.const section -- apparently might not be flattened fully
metadata_const = metadata.get('global', {}).get('const', {})
dict_append(image03_dict, 'image_description', description)
Expand Down Expand Up @@ -386,9 +377,9 @@ def run(args):

image03_df = pd.DataFrame(image03_dict)

with open(os.path.join(args.output_directory, "image03.txt"), "w") as out_fp:
out_fp.write('"image"\t"3"\n')
image03_df.to_csv(out_fp, sep="\t", index=False, quoting=csv.QUOTE_ALL)
with open(os.path.join(args.output_directory, "image03.csv"), "w") as out_fp:
out_fp.write('"image","3"\n')
image03_df.to_csv(out_fp, sep=",", index=False, quoting=csv.QUOTE_ALL)

def main():
class MyParser(argparse.ArgumentParser):
Expand All @@ -414,6 +405,11 @@ def error(self, message):
"output_directory",
help="Directory where NDA files will be stored",
metavar="OUTPUT_DIRECTORY")
parser.add_argument(
"experiment_id",
help="Experiment ID assigned by NDA for collection. Requred for fMRI studies",
metavar='EXPERIMENT_ID')
Copy link
Collaborator

Choose a reason for hiding this comment

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

hm, I wonder how we managed to upload before without this?

so we were taking experiment id from metadata and it worked on our few uploads I participated in.

Do you know if it may be recent or could be optional?

Copy link
Collaborator

Choose a reason for hiding this comment

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

nevermind -- it is in the notes of README.md , so good to have it here in the code now but we would need to remove that note from README.md here. care to do that @ljchang ?


args = parser.parse_args()

run(args)
Expand Down