tcapraz / SOFA

Python package for the semi-supervised analysis of multi-omics data
MIT License
3 stars 0 forks source link

Conflict with sofa.utils.utils.load_model and sofa.models.SOFA.SOFA.__init__ definitions #2

Open KlausKruger opened 1 week ago

KlausKruger commented 1 week ago

The current definition for sofa.models.SOFA.SOFA.initdoes not include either the parameter guide_scale nor the parameter target scale. The first one is present in the currently publicly available documentation (https://tcapraz.github.io/SOFA/sofa.html) and the latter is being used for the loading function sofa.utils.utils.load_model() in biosofa 0.6.0 (pip installation). This generates a conflict (to the best of my knowledge) when trying to load a sofa.models.SOFA.SOFA object with the .lead_model() function, since it assigns a parameter to the SOFA object that it cannot take, here's the punctual error (with bold letters for convenience):

TypeError Traceback (most recent call last) Cell In[79], line 1 ----> [1]sofa.utils.utils.load_model(file_prefix = "model_3_centered_hf")

File ~/miniforge3/envs/anndata4sofa/lib/python3.8/site-packages/sofa/utils/utils.py:441, in load_model(file_prefix) [438]horseshoe = mdata.uns["horseshoe"] [440] seed = mdata.uns["seed"] --> [441] model = SOFA(Xmdata, [442]num_factors=num_factors, [443]Ymdata = Ymdata, [444]design = torch.tensor(design), [445]device=torch.device('cuda'), [446] horseshoe=horseshoe, [447] subsample=0, [448] target_scale=target_scale, [449] seed=seed) [450] model.Z = mdata.uns["Z"] [451] W = [mdata.uns[f"W_{i}"] for i in Xmdata.mod]

TypeError: init() got an unexpected keyword argument 'target_scale'

I cannot be certain that just eliminating target_scale=targetscale would solve the issue, but I would appreciate looking into it. If possible I would also recommend verifying that : W = [mdata.uns[f"W{i}"] for i in Xmdata.mod] model.W = W model.Xpred =[mdata.uns[f"X{i}"] for i in range(len(Xmdata.mod))] accommodate to the data structure given by the mudata generated by sofa.utils.utils.save_model; especially for models that contain guided factors.

Thank you for your time and effort working on this package, it has provided us with very interesting results and I've had a lot of fun using it.

Best Wishes

tcapraz commented 1 week ago

Hi Klaus,

Great to hear that you are using the package and that you got interesting results.

Thank you very much for spotting this bug. I recently changed that the scaling factor for the guiding variables are now in the individual AnnData objects within the MuData. So if you use sofa.tl.get_ad() you can either pass an argument scaling_factor or keep the default of 0.1, which turned out to be a sensible choice for many data sets. I forgot to remove the argument when loading the model in sofa.utils.utils.load_model. I hope it is fixed now in version 0.7.0.

Best Tümay

KlausKruger commented 1 week ago

Hello Tümay,

I've recently updated to biosofa 0.7.0 with pip and when I tried to use the sofa.utils.utils.save_model() I encountered a problem:

IORegistryError: No method registered for writing <class 'torch.device'> into <class 'h5py._hl.group.Group'>; digging a bit into it the problem is in model.save_as_mudata().write(file_prefix+".h5mu").

From what I understand, this function has not been modified, but this issue was not present in the 0.6.0 version. Have there been any additional modifications other than the one in sofa.utils.utils.load_model?

I would rather not take too much of your time with this, but a general list of changes from version to version would be helpful for this type of issue.

Thank you for your time

Best Wishes

Klaus Kruger

tcapraz commented 3 days ago

Hi Klaus,

thanks again! I recently added that when saving the model, the chosen device is also saved to disk. Apparently MuData can not save torch.device objects to disk. In version 0.7.1 I changed it to str, so it should be able to save the device information. I will include release changes from now on.

Best Tümay