getspams / spams-python

Python interface for SPAMS (SPArse Modeling Software)
https://thoth.inrialpes.fr/people/mairal/spams/
GNU General Public License v3.0
17 stars 7 forks source link

Numpy issues #33

Open carversh opened 1 year ago

carversh commented 1 year ago

Hi I'm running the following and getting an error. It seems to be an issue with numpy although I am using version 1.22.3:

import spams
import pandas as pd
import numpy as np

# load X input (genes by droplets/cells matrix)
X = np.load('matrix.npy')

params = {
    'K' : 15, # learns a dictionary with K modules/atoms
    'lambda1' : 0.15, 
    'lambda2':0,
    'mode':2,
    'modeD': 0,
    'numThreads' : 4, 
    'batchsize' : 400, # batchsize to learn the code
    'iter' : 15,
    'verbose':True,
    # 'gamma1': # (optional parameter for modeD >= 1),
    # 'gamma2': # (optional parameter for modeD = 2),
}

# dictionary initialization
D_init = np.random.randn(X.shape[0], params['K'])
params['D'] = D_init # dictionary initialization, if None, dictionary is initialized with random elements from the training set

#(D, model) = 
#D = spams.trainDL(np.asfortranarray(X), return_model = False, **params)
#(D,model) = spams.trainDL(X, return_model = True, lambda1=0.15, **params)
(D,model) = spams.trainDL(X, return_model = True,  **params)

And getting the following error:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/site-packages/spams/spams.py", line 2026, in trainDL
    return __allTrainDL(X, return_model, model, False, D, None, None, numThreads, 0.000001, True, False, batchsize, K, lambda1, lambda2, lambda3, iter, t0, mode, regul, posAlpha, posD, expand, modeD, whiten, clean, verbose, gamma1, gamma2, rho, iter_updateD, stochastic_deprecated, modeParam, batch, log_deprecated, logName)
  File "/site-packages/spams/spams.py", line 1868, in __allTrainDL
    x = spams_wrap.alltrainDL(
  File "/site-packages/spams_wrap/spams_wrap.py", line 313, in alltrainDL
    return _spams_wrap.alltrainDL(*args)
RuntimeError: matrix arg 10 must be a 2d double Fortran Array
carversh commented 1 year ago

When I convert X to a fortran array, this still produces an error

carversh commented 1 year ago

Sometimes the code runs and sometimes it doesn't

samuelstjean commented 1 year ago

I hit something similar because sometimes the type that is used is compiler, platform and python version dependent for an int type. Although here it seems caused by a float/double thing, but could be related.

carversh commented 1 year ago

Thanks for your response. Would you mind recommending a python and numpy version that prevents this from happening?

carversh commented 1 year ago

You recommend this: numpy>=1.12 Pillow>=6.0 scipy>=1.0 setuptools>=46.1

But this still gives me issues

samuelstjean commented 1 year ago

Well in this case the functions are probably all overloaded, so I don't think it would happen unless you are on some strange architecture nobody else was using. I would check the dtype of your float arrays, I'm not sure you can mix float and double in the same function call for example, just one or the other type for all of your inputs.

samuelstjean commented 1 year ago

I guess this was also related to #34? If so we'll just close it since it was just a bug in how the function is called.