janelia-flyem / gala

Automatic segmentation of electron microscopy volumes
BSD 3-Clause "New" or "Revised" License
76 stars 29 forks source link

unexpected keyword argument 'random_state' #83

Closed thanujadax closed 8 years ago

thanujadax commented 8 years ago

Hello, I was trying to run an adapted version of example.py as shown in the appendix below, with the example data provided with the source code.

However, I get the following error:

python example_1.py 
pylibtiff not available: http://www.lfd.uci.edu/~gohlke/pythonlibs/#pylibtiff
~/miniconda2/envs/gala/lib/python3.5/site-packages/gala/agglo.py:265: RuntimeWarning: invalid value encountered in double_scalars
  return p3*log2(p3) - p1*log2(p1) - p2*log2(p2) - \
Traceback (most recent call last):
  File "galaTest1.py", line 47, in <module>
    (X, y, w, merges) = g_train.learn_agglomerate(gt_train, fc)[0]
  File "~/miniconda2/envs/gala/lib/python3.5/site-packages/gala/agglo.py", line 1091, in learn_agglomerate
    cl = get_classifier(classifier, random_state=random_state)
  File "~/miniconda2/envs/gala/lib/python3.5/site-packages/gala/classify.py", line 168, in get_classifier
    return VigraRandomForest(*args, **kwargs)
TypeError: __init__() got an unexpected keyword argument 'random_state'

Any idea of what's gone wrong?

Thanks Thanuja


Appendix: code

from gala import imio, classify, features, agglo, evaluate as ev
import numpy as np
#import scipy.misc.imsave
import os
from PIL import Image

'''
Inputs
'''

h5File_train_gt = 'train-gt.lzf.h5'
h5File_train_ws = 'train-ws.lzf.h5'
h5File_train_probMap = 'train-p1.lzf.h5'

h5File_test_ws = 'test-ws.lzf.h5'
h5File_test_probMap = 'test-p1.lzf.h5'

'''
Outputs
'''
outputRoot = '20160627_gala_sstem'

# read in training data
# groundtruth volume, probability maps, superpixe/watershed map
gt_train, pr_train, ws_train = (map(imio.read_h5_stack,
                                [h5File_train_gt, h5File_train_probMap,
                                 h5File_train_ws]))

# create a feature manager
fm = features.moments.Manager()
fh = features.histogram.Manager()
fc = features.base.Composite(children=[fm, fh])

# create Region Adjacency Graph (RAG) and obtain a training dataset
g_train = agglo.Rag(ws_train, pr_train, feature_manager=fc)
(X, y, w, merges) = g_train.learn_agglomerate(gt_train, fc)[0]
y = y[:, 0] # gala has 3 truth labeling schemes, pick the first one ????
print((X.shape, y.shape)) # standard scikit-learn input format

# train a classifier, scikit-learn syntax
rf = classify.DefaultRandomForest().fit(X, y)
# a policy is the composition of a feature map and a classifier
# policy = merge priority function
learned_policy = agglo.classifier_probability(fc, rf)

# get the test data and make a RAG with the trained policy
pr_test, ws_test = (map(imio.read_h5_stack,
                        [h5File_test_probMap, h5File_test_ws]))
g_test = agglo.Rag(ws_test, pr_test, learned_policy, feature_manager=fc)
g_test.agglomerate(0.5) # best expected segmentation obtained with a threshold of 0.5
seg_test1 = g_test.get_segmentation()

# convert hdf into png and save 
np_data = np.array(seg_test1)
sizeZ,sizeY,sizeX = np_data.shape
for i in range(0,sizeZ):
    im1 = np_data[i,:,:]
    im = Image.fromarray(im1.astype('uint8'))
    imFileName = str(i).zfill(3) + ".png"
    imFileName = os.path.join(outputRoot,imFileName)
    #scipy.misc.toimage(im, cmin=0.0, cmax=...).save(imFileName)
    im.save(imFileName)
thanujadax commented 8 years ago

Hi again, Turns out this issue was due to having vigra inside the python search path. It went away once I removed the vigra package from my virtual env.

Thanks anyway Thanuja

jni commented 8 years ago

Ach, sorry I was slow to respond @thanujadax!

I haven't used Vigra for a while. I presume that the Python bindings allow setting the random state (this is used to get consistent results between different runs of the random forest), but uses a different keyword. So the slightly less hacky way to do this is to update the VigraRandomForest wrapper to accept random_state as a keyword, and, if possible, set the corresponding variable in Vigra.

Would you like to submit a pull request that does this?