Unable to import sklearn.mixture in CP3

cellprofiler
sklearn

#1

Hello! I’m currently writing my own module that uses a 2-component Gaussian mixture model (sklearn.mixture.GaussianMixture) from scikit-learn 0.19.0 to cluster the intensity of objects into background and signal. I’ve managed to make it work in a standalone python script using exported data from another cpproj, but as soon as I place imports into my module.py, I get the following behavior:

If I include import sklearn, no error is thrown until sklearn.mixture.GaussianMixture is called during the module’s run() in Start Test Mode, at which point I get:

AttributeError: ‘module’ object has no attribute ‘mixture’

If I instead include import sklearn.mixture, a different error is thrown at startup:

ImportError: No module named mixture

I’m not sure how to utilize sklearn in my module. Any help would be appreciated. Thanks!

EDIT: The import is successful in the dev build of cellprofiler (3.1.4).


#2

I realize this is something you solved yourself but at the moment I’m trying to help out with dependency management and the associated expectations.

The binary releases of CellProfiler 3.0.0 for Windows and macOS both ship with scikit-learn 0.19.0. sklearn.mixture.GaussianMixture was added in version 0.18.0 of scikit-learn.

Do you remember or have access to how you had CellProfiler installed before you solved the problem?


#3

Sorry for the late reply. I just had Cellprofiler 3.0.0 (the stable build) binary installed; upon initialization with the plugins directory pointing to where I had the module with sklearn.mixture, the terminal displayed the above errors. Please let me know if you’re able to replicate it (my machine is running Windows 10).


#4

A ha!

In that case I’m pretty sure the problem is that you need to import sklearn.mixture as well as import sklearn but in order to tell you more I would have to see the full source code of your plugin.


#5

With both import sklearn and import sklearn.mixture in the script, I still get an ImportError: no module named mixture (in both orders).


#6

Odd. You’ll have to post your entire plugin and the full stack trace for me to be much further help I’m afraid.


#7

I’m able to replicate the error on a fresh install of the stable version of cellprofiler (edit: CP3.0.0), with the following plugin code:

import sklearn
import sklearn.mixture
import cellprofiler.module

class SklearnTest(cellprofiler.module.Module):
    category = 'Test'
    module_name = 'SklearnTest'
    variable_revision_number = 1

    def create_settings(self):
        self.divider = cellprofiler.setting.Divider()

    def settings(self):
        return [self.divider]

Upon starting cellprofiler, I get the following traceback:

Could not load sklearntest
Traceback (most recent call last):
  File "CellProfiler\cellprofiler\modules\__init__.py", line 321, in add_module
  File "C:\Users\User\Documents\PycharmProjects\CellProfilerPlugins\plugins\sklearntest.py", line 2, in <module>
    import sklearn.mixture
ImportError: No module named mixture
could not load these modules: sklearntest

#8

Weird.

What do you get as output if you insert print sklearn.__version__ right after import sklearn?


#9

I get 0.19.0; no change in the traceback.


#10

Definitely 0.19.0 then so should certainly have sklearn.mixture. Can’t explain why it’s missing. It’s not imported anywhere in CellProfiler itself in 3.0.0 so it could be an artifact of the pyinstaller process that produces CellProfiler.exe.

Don’t think there’s any way to make that work unfortunately. Sorry I couldn’t be of more help.


#11

I see. Thanks so much for your help!


#12

Hi,

CellProfiler 3.1.5 has sklearn-0.19.1, so that version may work for you.


#13

Yup! I can verify that sklearn.mixture imports successfully in 3.1.5.