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

How to structure torchio.Subject for regression? #297

Closed
sarthakpati opened this issue Sep 10, 2020 · 3 comments
Closed

How to structure torchio.Subject for regression? #297

sarthakpati opened this issue Sep 10, 2020 · 3 comments

Comments

@sarthakpati
Copy link
Contributor

Hi,

I am trying to use TorchIO for regression and I was wondering if there is any way to add the "value to predict" per subject and get it during the training cycle?

Here is what I have (for segmentation):

for patient in range(num_row):
    # We need this dict for storing the meta data for each subject such as different image modalities, labels, any other data
    subject_dict = {}
    # iterating through the channels/modalities/timepoints of the subject
    for channel in channelHeaders:
        # assigning the dict key to the channel
        subject_dict[str(channel)] = Image(str(dataframe[channel][patient]), type=torchio.INTENSITY)
    if labelHeader is not None:
        subject_dict['label'] = Image(str(dataframe[labelHeader][patient]), type=torchio.LABEL)
        if not train:
            subject_dict['path_to_metadata'] = str(dataframe[labelHeader][patient])
    else:
        subject_dict['label'] = "NA"
        if not train:
            subject_dict['path_to_metadata'] = str(dataframe[channel][patient])
    # Initializing the subject object using the dict
    subject = Subject(subject_dict) 
    # Appending this subject to the list of subjects
    subjects_list.append(subject)

Cheers,
Sarthak

@romainVala
Copy link
Contributor

Hi
Have a look to Subject class

class Subject(dict):

it gives an example where subject age in added at the init call
so you just need to add a key to subject_dict, ... is that what you are looking for ?

@fepegar
Copy link
Owner

fepegar commented Sep 16, 2020

@sarthakpati have you tried what @romainVala has suggested? Here's some code that might help:

In [10]: import torchio as tio

In [11]: subjects = [tio.Subject(t1=tio.ScalarImage('t1.nii.gz'), age=30+i) for i in range(5)]

In [12]: dataset = tio.SubjectsDataset(subjects)

In [13]: loader = DataLoader(dataset, batch_size=2, shuffle=True)

In [14]: for batch in loader:
    ...:     print(batch['age'])
    ...:
tensor([31, 33])
tensor([30, 32])
tensor([34])

Where the subject_dict syntax could have also been used.

@sarthakpati
Copy link
Contributor Author

Hey, sorry I forgot to reply. Yes, I was able to get it working using @romainVala's suggestion.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants