bcdaniels / coniii

Convenient Interface to Inverse Ising
MIT License
4 stars 1 forks source link

ImportError: No module named ising_eqn #14

Closed ekvall93 closed 6 years ago

ekvall93 commented 6 years ago

I get the following error after implementing the innit code, and I cannot find a file with the name ising_eqn either...

eltrompetero commented 6 years ago

Yikes. I can't see the error that you get.

I did update the pip package online, if you wouldn't mind trying to updating to the 0.2.7 version. pip install --upgrade coniii

eltrompetero commented 6 years ago

Actually, you should be able to find 0.2.8 for Mac OS.

ekvall93 commented 6 years ago

I was on able to upgrade to Version: 0.2.7, I can try to upgrade soon again If it might help.

My module error disappeared after upgrading. But when running through the guide I get these following error:

AttributeError: 'MCH' object has no attribute 'N'

and

AttributeError: 'int' object has no attribute 'copy'

ekvall93 commented 6 years ago

Sorry, Should be more specific...

In the paragrapgh Monte Carlo histogram (MCH) i got the following error:


AttributeError Traceback (most recent call last)

in () 24 25 # Run solver. ---> 26 estMultipliers = solver.solve(sisj,maxiter=40,custom_convergence_f=learn_settings) 27 28 print "Error on multipliers: %E"%error_on_multipliers(estMultipliers) /Users/markusekvall/Desktop/entropy_tool/venv/lib/python2.7/site-packages/coniii/solvers.pyc in solve(self, initial_guess, constraints, X, tol, tolNorm, n_iters, burnin, maxiter, custom_convergence_f, disp, full_output, learn_params_kwargs, generate_kwargs) 615 Log of errors in matching constraints at each step of iteration. 616 """ --> 617 if (self.N*10)>burnin: 618 warn("Number of burn in MCMC iterations between samples may be too small for "+ 619 "convergence to stationary distribution.") AttributeError: 'MCH' object has no attribute 'N'
eltrompetero commented 6 years ago

Hm. This is not good. There must be something I did wrong when debugging because I didn't have any of these problems. Let me run through the usage notebook again and see what happened.

ekvall93 commented 6 years ago

And in the paragrapgh Regularized mean field:


AttributeError Traceback (most recent call last)

in () 1 solver = RegularizedMeanField(n,calc_observables=calc_observables) ----> 2 estMultipliers = solver.solve(sample,numSamples=100) 3 4 print "Error on multipliers: %E"%error_on_multipliers(estMultipliers) 5 print "Error on sample corr: %E"%error_on_correlations( /Users/markusekvall/Desktop/entropy_tool/venv/lib/python2.7/site-packages/coniii/solvers.pyc in solve(self, samples, numSamples, nSkip, seed, changeSeed, numProcs, numDataSamples, minSize, minimizeCovariance, minimizeIndependent, coocCov, priorLmbda, bracket, numGridPoints) 2080 solution = minimize_scalar(func,bracket=gridBracket) 2081 else: -> 2082 solution = minimize_scalar(func) 2083 2084 gammaPrimeMin = solution['x'] /Users/markusekvall/Desktop/entropy_tool/venv/lib/python2.7/site-packages/scipy/optimize/_minimize.pyc in minimize_scalar(fun, bracket, bounds, args, method, tol, options) 651 return method(fun, args=args, bracket=bracket, bounds=bounds, **options) 652 elif meth == 'brent': --> 653 return _minimize_scalar_brent(fun, bracket, args, **options) 654 elif meth == 'bounded': 655 if bounds is None: /Users/markusekvall/Desktop/entropy_tool/venv/lib/python2.7/site-packages/scipy/optimize/optimize.pyc in _minimize_scalar_brent(func, brack, args, xtol, maxiter, **unknown_options) 2107 full_output=True, maxiter=maxiter) 2108 brent.set_bracket(brack) -> 2109 brent.optimize() 2110 x, fval, nit, nfev = brent.get_result(full_output=True) 2111 return OptimizeResult(fun=fval, x=x, nit=nit, nfev=nfev, /Users/markusekvall/Desktop/entropy_tool/venv/lib/python2.7/site-packages/scipy/optimize/optimize.pyc in optimize(self) 1891 # set up for optimization 1892 func = self.func -> 1893 xa, xb, xc, fa, fb, fc, funcalls = self.get_bracket_info() 1894 _mintol = self._mintol 1895 _cg = self._cg /Users/markusekvall/Desktop/entropy_tool/venv/lib/python2.7/site-packages/scipy/optimize/optimize.pyc in get_bracket_info(self) 1865 ### carefully DOCUMENT any CHANGES in core ## 1866 if brack is None: -> 1867 xa, xb, xc, fa, fb, fc, funcalls = bracket(func, args=args) 1868 elif len(brack) == 2: 1869 xa, xb, xc, fa, fb, fc, funcalls = bracket(func, xa=brack[0], /Users/markusekvall/Desktop/entropy_tool/venv/lib/python2.7/site-packages/scipy/optimize/optimize.pyc in bracket(func, xa, xb, args, grow_limit, maxiter) 2290 _gold = 1.618034 # golden ratio: (1.0+sqrt(5.0))/2.0 2291 _verysmall_num = 1e-21 -> 2292 fa = func(*(xa,) + args) 2293 fb = func(*(xb,) + args) 2294 if (fa < fb): # Switch so fa > fb /Users/markusekvall/Desktop/entropy_tool/venv/lib/python2.7/site-packages/coniii/solvers.pyc in func(meanFieldGammaPrime) 2047 2048 # sample from J -> 2049 isingSamples = samples(J) 2050 2051 # calculate residuals, including prior if necessary /Users/markusekvall/Desktop/entropy_tool/venv/lib/python2.7/site-packages/coniii/solvers.pyc in samples(J) 2031 J = J + J.T 2032 self._multipliers = np.concatenate([J.diagonal(),squareform(mean_field_ising.zeroDiag(-J))]) -> 2033 self.generate_samples(nSkip,burninDefault,int(numSamples),'metropolis') 2034 isingSamples = np.array(self.samples,dtype=float) 2035 return isingSamples /Users/markusekvall/Desktop/entropy_tool/venv/lib/python2.7/site-packages/coniii/solvers.pyc in generate_samples(self, n_iters, burnin, multipliers, sample_size, sample_method, initial_sample, generate_kwargs) 178 179 elif sample_method=='metropolis': --> 180 self.sampler.theta = multipliers.copy() 181 # Burn in. 182 self.sampler.generate_samples_parallel( sample_size, AttributeError: 'int' object has no attribute 'copy'
ekvall93 commented 6 years ago

Alright

eltrompetero commented 6 years ago

Ok. I think I did it right this time. You should be able to find the working v0.2.9 on PyPI. Thanks for pointing out the problems.

Let me know if you have any more trouble.

ekvall93 commented 6 years ago

I have upgraded to v0.2.9 and indeed the old error is gone. I instead get insertion errors.


AssertionError Traceback (most recent call last)

in () 24 25 # Run solver. ---> 26 estMultipliers = solver.solve(sisj,maxiter=40,custom_convergence_f=learn_settings) 27 28 print "Error on multipliers: %E"%error_on_multipliers(estMultipliers) /Users/markusekvall/Desktop/entropy_tool/venv/lib/python2.7/site-packages/coniii/solvers.pyc in solve(self, initial_guess, constraints, X, tol, tolNorm, n_iters, burnin, maxiter, custom_convergence_f, disp, full_output, learn_params_kwargs, generate_kwargs) 629 elif not X is None: 630 self.constraints = self.calc_observables(X).mean(0) --> 631 else: assert not self.constraints is None 632 633 # Set initial guess for parameters. self._multipliers is where the current guess for the AssertionError:
ekvall93 commented 6 years ago

coocSampleCovariance : WARNING : using ad-hoc 'Laplace' correction

AssertionError Traceback (most recent call last)

in () 1 solver = RegularizedMeanField(n,calc_observables=calc_observables) ----> 2 estMultipliers = solver.solve(sample,numSamples=100) 3 4 print "Error on multipliers: %E"%error_on_multipliers(estMultipliers) 5 print "Error on sample corr: %E"%error_on_correlations( /Users/markusekvall/Desktop/entropy_tool/venv/lib/python2.7/site-packages/coniii/solvers.pyc in solve(self, samples, numSamples, nSkip, seed, changeSeed, numProcs, numDataSamples, minSize, minimizeCovariance, minimizeIndependent, coocCov, priorLmbda, bracket, numGridPoints) 2080 solution = minimize_scalar(func,bracket=gridBracket) 2081 else: -> 2082 solution = minimize_scalar(func) 2083 2084 gammaPrimeMin = solution['x'] /Users/markusekvall/Desktop/entropy_tool/venv/lib/python2.7/site-packages/scipy/optimize/_minimize.pyc in minimize_scalar(fun, bracket, bounds, args, method, tol, options) 651 return method(fun, args=args, bracket=bracket, bounds=bounds, **options) 652 elif meth == 'brent': --> 653 return _minimize_scalar_brent(fun, bracket, args, **options) 654 elif meth == 'bounded': 655 if bounds is None: /Users/markusekvall/Desktop/entropy_tool/venv/lib/python2.7/site-packages/scipy/optimize/optimize.pyc in _minimize_scalar_brent(func, brack, args, xtol, maxiter, **unknown_options) 2107 full_output=True, maxiter=maxiter) 2108 brent.set_bracket(brack) -> 2109 brent.optimize() 2110 x, fval, nit, nfev = brent.get_result(full_output=True) 2111 return OptimizeResult(fun=fval, x=x, nit=nit, nfev=nfev, /Users/markusekvall/Desktop/entropy_tool/venv/lib/python2.7/site-packages/scipy/optimize/optimize.pyc in optimize(self) 1891 # set up for optimization 1892 func = self.func -> 1893 xa, xb, xc, fa, fb, fc, funcalls = self.get_bracket_info() 1894 _mintol = self._mintol 1895 _cg = self._cg /Users/markusekvall/Desktop/entropy_tool/venv/lib/python2.7/site-packages/scipy/optimize/optimize.pyc in get_bracket_info(self) 1865 ### carefully DOCUMENT any CHANGES in core ## 1866 if brack is None: -> 1867 xa, xb, xc, fa, fb, fc, funcalls = bracket(func, args=args) 1868 elif len(brack) == 2: 1869 xa, xb, xc, fa, fb, fc, funcalls = bracket(func, xa=brack[0], /Users/markusekvall/Desktop/entropy_tool/venv/lib/python2.7/site-packages/scipy/optimize/optimize.pyc in bracket(func, xa, xb, args, grow_limit, maxiter) 2290 _gold = 1.618034 # golden ratio: (1.0+sqrt(5.0))/2.0 2291 _verysmall_num = 1e-21 -> 2292 fa = func(*(xa,) + args) 2293 fb = func(*(xb,) + args) 2294 if (fa < fb): # Switch so fa > fb /Users/markusekvall/Desktop/entropy_tool/venv/lib/python2.7/site-packages/coniii/solvers.pyc in func(meanFieldGammaPrime) 2047 2048 # sample from J -> 2049 isingSamples = samples(J) 2050 2051 # calculate residuals, including prior if necessary /Users/markusekvall/Desktop/entropy_tool/venv/lib/python2.7/site-packages/coniii/solvers.pyc in samples(J) 2031 J = J + J.T 2032 self.multipliers = np.concatenate([J.diagonal(),squareform(mean_field_ising.zeroDiag(-J))]) -> 2033 self.generate_samples(n_iters=nSkip,burnin=burninDefault,sample_size=int(numSamples)) 2034 isingSamples = np.array(self.samples,dtype=float) 2035 return isingSamples /Users/markusekvall/Desktop/entropy_tool/venv/lib/python2.7/site-packages/coniii/solvers.pyc in generate_samples(self, n_iters, burnin, multipliers, sample_size, sample_method, initial_sample, generate_kwargs) 183 n_iters=burnin, 184 cpucount=self.n_jobs, --> 185 initial_sample=initial_sample ) 186 self.sampler.generate_samples_parallel( sample_size, 187 n_iters=n_iters, /Users/markusekvall/Desktop/entropy_tool/venv/lib/python2.7/site-packages/coniii/samplers.pyc in generate_samples_parallel(self, sample_size, n_iters, cpucount, initial_sample, systematic_iter) 1436 self.samples,self.E = zip(*pool.map(f,zip(self.samples, 1437 self.E, -> 1438 np.random.randint(2**31-1,size=sample_size)))) 1439 pool.close() 1440 /Users/markusekvall/Desktop/entropy_tool/venv/lib/python2.7/site-packages/multiprocess/pool.pyc in map(self, func, iterable, chunksize) 249 ''' 250 assert self._state == RUN --> 251 return self.map_async(func, iterable, chunksize).get() 252 253 def imap(self, func, iterable, chunksize=1): /Users/markusekvall/Desktop/entropy_tool/venv/lib/python2.7/site-packages/multiprocess/pool.pyc in get(self, timeout) 565 return self._value 566 else: --> 567 raise self._value 568 569 def _set(self, i, obj): AssertionError:
eltrompetero commented 6 years ago

Your first error when calling the MCH solution method is because the ipynb notebook did not update with the pip package. I'm not sure why that is the case, but you can find here in the GitHub the working version. Basically, you want to specify the constraints kwarg. estMultipliers = solver.solve(constraints=sisj,maxiter=40,custom_convergence_f=learn_settings) The usage guide is here: https://github.com/bcdaniels/coniii/blob/master/ipynb/usage_guide.ipynb

As for the second error, I'm a bit puzzled. Could be that multiprocess is not the latest version. I definitely do not have this bug when I run it. If you would try updating multiprocess and let me know if that works, I could then make sure people have the right version installed before running coniii.

ekvall93 commented 6 years ago

I have copy pasted the paragraph for MCH in the user guid to my jupyter, but still the same error.

I have tried updating multiprocesses, I have currently (0.70.5)

eltrompetero commented 6 years ago

Regarding the MCH error, did you specify the constraints argument as in solver.solve(constraints=sisj,maxiter=40,custom_convergence_f=learn_settings)?

I'm definitely not getting this error once I've specified that.

ekvall93 commented 6 years ago

Strange, i use the exact same command:


AssertionError Traceback (most recent call last)

in () 19 20 # Run solver. ---> 21 estMultipliers = solver.solve(constraints=sisj,maxiter=40,custom_convergence_f=learn_settings) 22 23 print "Error on multipliers: %E"%error_on_multipliers(estMultipliers) /Users/markusekvall/Desktop/entropy_tool/venv/lib/python2.7/site-packages/coniii/solvers.pyc in solve(self, initial_guess, constraints, X, tol, tolNorm, n_iters, burnin, maxiter, custom_convergence_f, disp, full_output, learn_params_kwargs, generate_kwargs) 657 generate_kwargs=generate_kwargs, 658 initial_sample=np.random.choice([-1.,1.], --> 659 size=(self.sampleSize,self.n)) ) 660 thisConstraints = self.calc_observables(self.samples).mean(0) 661 errors.append( thisConstraints-self.constraints ) /Users/markusekvall/Desktop/entropy_tool/venv/lib/python2.7/site-packages/coniii/solvers.pyc in generate_samples(self, n_iters, burnin, multipliers, sample_size, sample_method, initial_sample, generate_kwargs) 183 n_iters=burnin, 184 cpucount=self.n_jobs, --> 185 initial_sample=initial_sample ) 186 self.sampler.generate_samples_parallel( sample_size, 187 n_iters=n_iters, /Users/markusekvall/Desktop/entropy_tool/venv/lib/python2.7/site-packages/coniii/samplers.pyc in generate_samples_parallel(self, sample_size, n_iters, cpucount, initial_sample, systematic_iter) 1436 self.samples,self.E = zip(*pool.map(f,zip(self.samples, 1437 self.E, -> 1438 np.random.randint(2**31-1,size=sample_size)))) 1439 pool.close() 1440 /Users/markusekvall/Desktop/entropy_tool/venv/lib/python2.7/site-packages/multiprocess/pool.pyc in map(self, func, iterable, chunksize) 249 ''' 250 assert self._state == RUN --> 251 return self.map_async(func, iterable, chunksize).get() 252 253 def imap(self, func, iterable, chunksize=1): /Users/markusekvall/Desktop/entropy_tool/venv/lib/python2.7/site-packages/multiprocess/pool.pyc in get(self, timeout) 565 return self._value 566 else: --> 567 raise self._value 568 569 def _set(self, i, obj): AssertionError:
eltrompetero commented 6 years ago

Ok. So you don't get the constraints assertion error anymore which is good.

This multiprocess error is a bit more opaque. It might be helpful to know if your multiprocess code works on any simple code like def f(x): return np.random.rand() pool=mp.Pool(mp.cpu_count()) pool.map(f,range(100)) pool.close()

If you really need to sample from the model, I would avoid multiprocess parallelization for now. There is a method in the sampler for regular sampling generate_samples(). I don't think that the interface is set up to make this a simple option with MCH, but you can change the Solver class' generate_samples() method.

I'll put that on the todo list.

ekvall93 commented 6 years ago

So I would actually want to try your MCH model for an experimentell dataset of 50 neurons with activity over time. I there any easy way to fix so the model trains with respect to my experimentell data? I guess it's calc_observables I somehow should calculate for my data right?

But I guess it does not really matter until I get the the MCH to work anyways...

Btw, this work

import multiprocess as mp

def f(x): return np.random.rand() pool=mp.Pool(mp.cpu_count()) pool.map(f,range(100)) pool.close()

eltrompetero commented 6 years ago

I would recommend using the MPF solver first to find an initial set of parameters before running MCH.

The problem right now is that the parallel MCMC sampling using multiprocess is not working for you. I would suggest changing the self.sampler.generate_samples_parallel call on lines 182 and 186 in file solvers.py to self.sampler.generate_samples. This is implemented in samplers.py in the Metropolis class. If you have problems questions about calling it, you can look there.

Of course, this is going to be slower. Generally, N processors will speed up your sampling routine by a factor of N.

ekvall93 commented 6 years ago

I have now changed these two lines two: self.sampler.generate_samples( sample_size, n_iters=burnin, cpucount=self.n_jobs, initial_sample=initialsample) self.sampler.generate_samples( sample_size, n_iters=n_iters, cpucount=self.n_jobs, initial_sample=self.sampler.samples)

But I now get this following error:


TypeError Traceback (most recent call last)

in () 4 sample_size=100, 5 sample_method='metropolis', ----> 6 mch_approximation=mchApproximation) 7 8 # Define function for changing learning parameters as we converge. /Users/markusekvall/Desktop/entropy_tool/venv/lib/python2.7/site-packages/coniii/solvers.py in __init__(self, *args, **kwargs) 545 ------- 546 """ --> 547 super(MCH,self).__init__(*args,**kwargs) 548 assert not self.sampleSize is None, "Must specify sample_size." 549 assert not self.sampleMethod is None, "Must specify sample_method." TypeError: super(type, obj): obj must be an instance or subtype of type
ekvall93 commented 6 years ago

I feels that these errors are a bottomless pit. I'm the first one that get these kinds of error? Should I wait for some update instead of trying to fix one error after the other?'

ekvall93 commented 6 years ago

Have you had any other people trying out the code lately? I might have a software that is not updated or something.

Another thing that yield problems for me is:

Another thing that have been giving mer error is the following:

elif sample_method=='metropolis': self.sampler.theta = multipliers.copy()

Burn in.

        self.sampler.generate_samples_parallel( sample_size,
                                                n_iters=burnin,
                                                cpucount=self.n_jobs,
                                                initial_sample=initial_sample )

Where I guess I should change initial_sample=initial_sample -> initial_sample=self.sampler.samples ?

It's when changing this I get this following error:

TypeError Traceback (most recent call last) in () 4 sample_size=100, 5 sample_method='metropolis', ----> 6 mch_approximation=mchApproximation) 7 8 # Define function for changing learning parameters as we converge.

/Users/markusekvall/Desktop/entropy_tool/venv/lib/python2.7/site-packages/coniii/solvers.py in init(self, *args, *kwargs) 545 ------- 546 """ --> 547 super(MCH,self).init(args,**kwargs) 548 assert not self.sampleSize is None, "Must specify sample_size." 549 assert not self.sampleMethod is None, "Must specify sample_method."

TypeError: super(type, obj): obj must be an instance or subtype of type

eltrompetero commented 6 years ago

Really sorry about the errors. Of course, it's hard to know if other people have tried using the package before and just have given up after meeting some errors, so I appreciate that you've contacted us about them. I've been using the package without any problem, but there are bound to be things like system differences and tweaks that I've put in somewhere else that might be taking care of these problems for me elsewhere.

I'm guessing the last error that you are showing me is because you need to kill the kernel and reload the package. Otherwise, the super reference won't work properly.

I can write up an update that adds functionality for switching to a non-parallelized sampler pretty quickly, but hung up on a few other projects with priority right now. I'd have to get back to you sometime this weekend probably.

If you don't mind digging into the source code a bit, adjusting the code to handle the non-parallelized code might not take too long.

ekvall93 commented 6 years ago

I fully understand!

I will play around a bit and see if it works out.

Cheers

ekvall93 commented 6 years ago

How you found any buggs with the parallel_sampling yet?

I'm currently running the vanilla sampling method, but it seems not to be plausible for system with for example n=159.

I have two other questions as well:

1) The Is the mch_approximation function the histogram re-weighting part?

2) What kind of correaltion do you use in pair_corr? Why not use for example np.cov()?

Thanks for answers

eltrompetero commented 6 years ago

No, I haven't. I am looking into updating the code to use regular sampling. I will see if I can replicate your error with my Mac today. (Also it's possible that your bug has been addressed in a newer version of pathos or on their issue forum: https://github.com/uqfoundation/pathos)

  1. Yes. It's the histogram approximation.
  2. It will do the same thing, but pair_corr provides support for missing data points. If you have a data set with binary spins {-1,1} and label missing data points with 0's, you can hit the exclude_empty switch to drop those from the averaging.
eltrompetero commented 6 years ago

Ok, I have no problem with the latest version on my Mac.

I was looking at your error again, and it looks like multiprocess is saying there is some timeout problem. Just to check, you're using conda and pip installing multiprocess?

ekvall93 commented 6 years ago

Thank you for the answers!

My multiprocess works now! I'm not sure what fixed the bug, but it might be the newer versions of pathos that fixed it.

ekvall93 commented 6 years ago

Do you have any other tip on speeding up the simulation? Just by following the article by Tkacïk he claims that the can run the simulation for around 150 neurons in a couple of minutes. When I run yours it seems that I have to runt it for way longer? Do I implement some of the parameters wrong?

For 30 neurons I set the following parameters:

solver = MCH(n,calc_observables=calc_observables, sample_size=6000, sample_method='metropolis', mch_approximation=mchApproximation)

stMultipliers, sample_MCH = solver.solve(constraints=sisj,n_iters=300, burnin=300,maxiter=10,custom_convergence_f=learn_settings,disp=False)

And it will take approxamately 1,5h...

eltrompetero commented 6 years ago

I'm glad you figured that out.

I would guess that their sampling routine is highly optimized and that they may have different settings. The initial solution is also very important.

I'm going to close this issue and open up a feature request for you. We can discuss there.