Exemplo n.º 1
0
    def multiprocessing(self):
        print("\nDay = %s, trials = %s,..." %(self.day,self.n_trials))
        pool = pathos.multiprocessing.ProcessingPool()
        freeze_support()
        exp_params = []
        for drug in self.drugs:
            for trial in self.trials:
                exp_params.append([self.decision_type, self.drug_type, drug, trial, self.seed, self.P])
        #self.df_list = []
        self.df_list = pool.map(self.run, exp_params)
        #for i in range(self.n_trials):self.df_list.append(self.run(exp_params[i]))
        
        for i in range(len(self.df_list)):
            self.accuracy_data.append(self.df_list[i][2])
            
        self.accuracy_data = np.array(self.accuracy_data)
        
        for i in range(len(self.df_list)):
            self.spiking_array.append((self.df_list[i][3]))
        self.spiking_array = np.array(self.spiking_array)
        
        primary_dataframe = pd.concat([self.df_list[i][0] for i in range(len(self.df_list))], ignore_index=True)
        firing_dataframe = pd.concat([self.df_list[i][1] for i in range(len(self.df_list))], ignore_index=True)

        return primary_dataframe, firing_dataframe
Exemplo n.º 2
0
def optimizeDE(cost,
               xmin,
               xmax,
               npop=40,
               maxiter=1000,
               maxfun=1e+6,
               convergence_tol=1e-5,
               ngen=100,
               crossover=0.9,
               percent_change=0.9,
               MINMAX=1,
               x0=[],
               radius=0.2,
               parallel=False,
               nodes=16):
    from mystic.solvers import DifferentialEvolutionSolver2
    from mystic.termination import ChangeOverGeneration as COG
    from mystic.strategy import Best1Exp
    from mystic.monitors import VerboseMonitor, Monitor
    from pathos.helpers import freeze_support
    freeze_support()  # help Windows use multiprocessing

    stepmon = VerboseMonitor(20)  # step for showing live update
    evalmon = Monitor()
    ndim = len(xmin)

    solver = DifferentialEvolutionSolver2(ndim, npop)
    if parallel:
        solver.SetMapper(Pool(nodes).map)
    if x0 == []:
        solver.SetRandomInitialPoints(xmin, xmax)
    else:
        solver.SetInitialPoints(x0, radius)
    solver.SetStrictRanges(xmin, xmax)
    solver.SetEvaluationLimits(maxiter, maxfun)
    solver.SetEvaluationMonitor(evalmon)
    solver.SetGenerationMonitor(stepmon)
    tol = convergence_tol
    solver.Solve(cost,
                 termination=COG(tolerance=tol, generations=ngen),
                 strategy=Best1Exp,
                 CrossProbability=crossover,
                 ScalingFactor=percent_change)
    solved = solver.bestSolution
    func_max = MINMAX * solver.bestEnergy
    func_evals = solver.evaluations
    return solved, func_max, func_evals
Exemplo n.º 3
0
from mystic.math import poly1d

#from raw_chebyshev8 import chebyshev8cost as ChebyshevCost     # no globals
#from raw_chebyshev8b import chebyshev8cost as ChebyshevCost    # use globals
from mystic.models.poly import chebyshev8cost as ChebyshevCost  # no helper

ND = 9
NP = 80
MAX_GENERATIONS = NP * NP
NNODES = NP // 5

seed = 321

if __name__ == '__main__':
    from pathos.helpers import freeze_support
    freeze_support()  # help Windows use multiprocessing

    def print_solution(func):
        print(poly1d(func))
        return

    psow = VerboseMonitor(10)
    ssow = VerboseMonitor(10)

    random_seed(seed)
    print("first sequential...")
    solver = DifferentialEvolutionSolver2(ND, NP)
    solver.SetRandomInitialPoints(min=[-100.0] * ND, max=[100.0] * ND)
    solver.SetEvaluationLimits(generations=MAX_GENERATIONS)
    solver.SetGenerationMonitor(ssow)
    solver.Solve(ChebyshevCost, VTR(0.01), strategy=Best1Exp, \
Exemplo n.º 4
0
    pool._id = old_id
    res = pool.map(squared, range(2))
    assert res == [0, 1]
    pool.terminate()
    pool.clear()
    assert len(state) == 0
    return

def test_basic():
    check_basic(ThreadPool(), tstate)
#   check_basic(ProcessPool(), mstate)
#   check_basic(ParallelPool(), pstate)

def test_rename():
    check_rename(ThreadPool(), tstate)
    check_rename(ProcessPool(), mstate)
    check_rename(ParallelPool(), pstate)

def test_nodes():
    check_nodes(ThreadPool(), tstate)
    check_nodes(ProcessPool(), mstate)
    check_nodes(ParallelPool(), pstate)


if __name__ == '__main__':
    from pathos.helpers import freeze_support
    freeze_support()
    test_basic()
    test_rename()
    test_nodes()
Exemplo n.º 5
0
    assert res == std


def test_processing():
    from pathos.pools import ProcessPool as MPP
    pool = MPP()
    res = timed_pool(pool, items, delay, verbose)
    assert res == std


def test_threading():
    from pathos.pools import ThreadPool as MTP
    pool = MTP()
    res = timed_pool(pool, items, delay, verbose)
    assert res == std


if __name__ == '__main__':
    if verbose:
        print("CONFIG: delay = %s" % delay)
        print("CONFIG: items = %s" % items)
        print("")

    from pathos.helpers import freeze_support, shutdown
    freeze_support()
    test_serial()
    test_pp()
    test_processing()
    test_threading()
    shutdown()
Exemplo n.º 6
0
   #from pathos.pools import ProcessPool as Pool
    from pathos.pools import ParallelPool as Pool

    solver = DifferentialEvolutionSolver2(ND, NP)
    solver.SetMapper(Pool().map)
    solver.SetRandomInitialPoints(min = [-100.0]*ND, max = [100.0]*ND)
    solver.SetEvaluationLimits(generations=MAX_GENERATIONS)
    solver.SetGenerationMonitor(VerboseMonitor(30))
    solver.enable_signal_handler()
  
    strategy = Best1Exp
    #strategy = Best1Bin

    solver.Solve(ChebyshevCost, termination=VTR(0.01), strategy=strategy, \
                 CrossProbability=1.0, ScalingFactor=0.9 , \
                 sigint_callback=plot_solution)

    solution = solver.Solution()

    return solution
  

if __name__ == '__main__':
    from pathos.helpers import freeze_support
    freeze_support() # help Windows use multiprocessing
    solution = main()
    print_solution(solution)
    plot_solution(solution)

# end of file
Exemplo n.º 7
0
    '''
    Command prompt yes or no question
    '''
    while "the answer is invalid":
        reply = str(input(question + ' (y/n): ')).lower().strip()
        if reply[:1] == 'y':
            return True
        if reply[:1] == 'n':
            return False
        print("invalid answer")


# end _yes_or_no

if __name__ == "__main__":
    freeze_support(
    )  # To prevent pathos from having issues when run from windows command line

    parser = argparse.ArgumentParser()
    parser.add_argument("path", help="Path to the spectral data file")
    parser.add_argument(
        "-sf",
        "--savefile",
        help=
        "Path to directory to save output files, by default will save to local calculations directory"
    )
    parser.add_argument(
        "-w1",
        "--width1",
        type=float,
        help="The beginning of the range of absorber widths (default 5 nm)")
    parser.add_argument(
Exemplo n.º 8
0
def main():
	import matplotlib.pyplot as plt
	import seaborn as sns
	import pandas as pd
	import numpy as np
	from helper import make_cues, make_addon, ch_dir, empirical_dataframe
	from pathos.multiprocessing import ProcessingPool as Pool
	from pathos.helpers import freeze_support #for Windows
	# import ipdb

	'''Import Parameters from File'''
	P=eval(open('parameters.txt').read()) #parameter dictionary
	seed=P['seed'] #sets tuning curves equal to control before drug application
	n_trials=P['n_trials']
	n_processes=P['n_processes']
	drug_type=str(P['drug_type'])
	decision_type=str(P['decision_type'])
	drugs=P['drugs']
	trials, perceived, cues = make_cues(P)
	P['timesteps']=np.arange(0,int((P['t_cue']+P['t_delay'])/P['dt_sample']))
	P['cues']=cues
	P['perceived']=perceived

	'''Multiprocessing'''
	print "drug_type=%s, decision_type=%s, trials=%s..." %(drug_type,decision_type,n_trials)
	freeze_support()
	pool = Pool(nodes=n_processes)
	exp_params=[]
	for drug in drugs:
		for trial in trials:
			exp_params.append([decision_type, drug_type, drug, trial, seed, P])
	# df_list=[run(exp_params[0]),run(exp_params[-1])] #for debugging, pool.map has bad traceback
	df_list = pool.map(run, exp_params)
	primary_dataframe = pd.concat([df_list[i][0] for i in range(len(df_list))], ignore_index=True)
	firing_dataframe = pd.concat([df_list[i][1] for i in range(len(df_list))], ignore_index=True)

	'''Plot and Export'''
	print 'Exporting Data...'
	datadir=ch_dir()
	primary_dataframe.to_pickle('primary_data.pkl')
	firing_dataframe.to_pickle('firing_data.pkl')
	param_df=pd.DataFrame([P])
	param_df.reset_index().to_json('params.json',orient='records')

	print 'Plotting...'
	emp_dataframe=empirical_dataframe()
	sns.set(context='poster')
	figure, (ax1, ax2) = plt.subplots(2, 1)
	sns.tsplot(time="time",value="wm",data=primary_dataframe,unit="trial",condition='drug',ax=ax1,ci=95)
	sns.tsplot(time="time",value="correct",data=primary_dataframe,unit="trial",condition='drug',ax=ax2,ci=95)
	sns.tsplot(time="time",value="accuracy",data=emp_dataframe,unit='trial',condition='drug',
				interpolate=False,ax=ax2,color=sns.color_palette('dark'))
	sns.tsplot(time="time",value="accuracy",data=emp_dataframe, unit='trial',condition='drug',
				interpolate=True,ax=ax2,color=sns.color_palette('dark'),legend=False)
	ax1.set(xlabel='',ylabel='decoded $\hat{cue}$',xlim=(0,9.5),ylim=(0,1),
				title="drug_type=%s, decision_type=%s, trials=%s" %(drug_type,decision_type,n_trials))
	ax2.set(xlabel='time (s)',xlim=(0,9.5),ylim=(0.5,1),ylabel='DRT accuracy')
	figure.savefig('primary_plots.png')

	figure2, (ax3, ax4) = plt.subplots(1, 2)
	if len(firing_dataframe.query("tuning=='weak'"))>0:
		sns.tsplot(time="time",value="firing_rate",unit="neuron-trial",condition='drug',ax=ax3,ci=95,
				data=firing_dataframe.query("tuning=='weak'").reset_index())
	if len(firing_dataframe.query("tuning=='nonpreferred'"))>0:
		sns.tsplot(time="time",value="firing_rate",unit="neuron-trial",condition='drug',ax=ax4,ci=95,
				data=firing_dataframe.query("tuning=='nonpreferred'").reset_index())
	ax3.set(xlabel='time (s)',xlim=(0.0,9.5),ylim=(0,250),ylabel='Normalized Firing Rate',title='Preferred Direction')
	ax4.set(xlabel='time (s)',xlim=(0.0,9.5),ylim=(0,250),ylabel='',title='Nonpreferred Direction')
	figure2.savefig('firing_plots.png')