predictive-clinical-neuroscience / PCNtoolkit-demo

Demo and tutorial for PCNtoolkit routines
GNU General Public License v3.0
28 stars 15 forks source link

Issue with transferring pre-trained models to new data #13

Open GuidoGuberman opened 6 months ago

GuidoGuberman commented 6 months ago

Dear PCN team,

I'm trying to get started applying your normative modelling approach to my data on pediatric concussions. I've trained a blr model, and I'm now trying to apply it to other data from the same sites (acquired at later time points). I'm using the transfer_pretrained_normative_models script, but I've encountered an issue that I haven't been able to resolve. The 'predict' function attempts to load the mY (posterior mean from the training set) from the meta-data, but it crashes with an error that is due to the fact that mY is a object of class 'list' which does not have an attribute 'reshape' (issue comes up on line 204 of the normative.py script). The example data provided with the script works well, and I can't really tell what is different between that data and mine. I'm a complete beginner in python so I apologize in advance if this issue is just the result of my python illiteracy, but I've been bumping my head against this problem for a few days now so I thought I'd ask.

Thanks! Guido

MDijsselhof commented 1 month ago

Dear PCN team,

I'm running into a similar issue as described above, is there any update on this issue?

Many thanks!

amarquand commented 1 month ago

Hi there - thanks for the reminder. We'll take a look at this and make sure it gets fixed in the next release. Hopefully not too far away

In the meantime could you please confirm which version of pcntoolkit you are using ?

MDijsselhof commented 1 month ago

I'm using pcntoolkit version 0.30.post2, main branch!

amarquand commented 2 weeks ago

Hi - we are looking at this now. Could you please confirm where you are seeing the error? line 204 of normative.py is unrelated to predict. We already catch some of these errors, but maybe we missed one. I think this is related to the use of cross-validation, where the mean of the different folds are stored as a list.

Could you please either send me some code to replicate this (can be with dummy data) or try to wrap the error with something like on line 868 here https://github.com/amarquand/PCNtoolkit/blob/master/pcntoolkit/normative.py

e.g.

if type(mY) is list: mY = mY[fold][models]

If that works, please let me know what you did, or send a pull request so we can integrate it into the dist

Thanks!

AuguB commented 1 week ago

Hi - Sorry for the long delay on our side.

Could you tell me if you are still experiencing the error? And did you find a workaround?

Did you adapt the transfer_pretrained_normative_models.ipynb notebook to use your models? In that case, can you put this code in a new cell directly before the cell that throws the error and post the output here:

# print the type of the mean_resp value
for idp_num, idp in enumerate(idp_ids): 
    idp_dir = os.path.join(out_dir, idp)
    meta_data_file = os.path.join(idp_dir,'Models','meta_data.md')
    with open(meta_data_file, 'rb') as file:
        meta_data = pickle.load(file)
    print(type(meta_data['mean_resp'][0]))