def test_combine_protocols(self): optimal_protocols = [ protocols.VoltageClampProtocol(steps=[ protocols.VoltageClampStep(duration=0.1, voltage=-0.08), protocols.VoltageClampStep(duration=0.1, voltage=-0.12), ]), protocols.VoltageClampProtocol(steps=[ protocols.VoltageClampStep(duration=0.5, voltage=-0.06), protocols.VoltageClampStep(duration=0.05, voltage=-0.04), ]), protocols.VoltageClampProtocol(steps=[ protocols.VoltageClampStep(duration=0.15, voltage=0.02), protocols.VoltageClampStep(duration=0.025, voltage=-0.08) ]), ] combined_protocol = voltage_clamp_optimization_experiments.\ combine_protocols(optimal_protocols=optimal_protocols) expected_combined_protocol = protocols.VoltageClampProtocol(steps=[ protocols.VoltageClampStep(duration=0.1, voltage=-0.08), protocols.VoltageClampStep(duration=0.1, voltage=-0.12), protocols.VoltageClampProtocol.HOLDING_STEP, protocols.VoltageClampStep(duration=0.5, voltage=-0.06), protocols.VoltageClampStep(duration=0.05, voltage=-0.04), protocols.VoltageClampProtocol.HOLDING_STEP, protocols.VoltageClampStep(duration=0.15, voltage=0.02), protocols.VoltageClampStep(duration=0.025, voltage=-0.08) ]) self.assertEqual(combined_protocol, expected_combined_protocol)
def test_mutate(self): protocol = protocols.VoltageClampProtocol(steps=[ protocols.VoltageClampStep(voltage=1.0, duration=0.5), protocols.VoltageClampStep(voltage=2.0, duration=0.75), ]) # Finds seed so that only second step is mutated. for i in range(500): random.seed(i) num_one = random.random() num_two = random.random() if num_two < self.vc_ga.config.gene_mutation_probability < num_one: random.seed(i) break else: self.fail(msg='Could not find seed to meet required behavior.') np.random.seed(3) self.vc_ga._mutate( individual=genetic_algorithm_results.VCOptimizationIndividual( protocol=protocol)) expected_protocol = protocols.VoltageClampProtocol(steps=[ protocols.VoltageClampStep(voltage=1.0, duration=0.5), protocols.VoltageClampStep(voltage=0.6, duration=1.0264562386575933), ]) self.assertEqual(protocol, expected_protocol)
def combine_protocols( optimal_protocols: List[protocols.VoltageClampProtocol] ) -> protocols.VoltageClampProtocol: """Combines protocols together.""" combined_protocol = protocols.VoltageClampProtocol() for i in optimal_protocols: combined_protocol.steps.extend(i.steps) return combined_protocol
def test_evaluate_returns_normal(self): protocol = protocols.VoltageClampProtocol(steps=[ protocols.VoltageClampStep(voltage=0.02, duration=0.1), protocols.VoltageClampStep(voltage=-0.03, duration=0.1865), ]) fitness = self.vc_ga._evaluate( individual=genetic_algorithm_results.VCOptimizationIndividual( protocol=protocol)) self.assertGreater(fitness, 0)
def _init_individual(self): """Initializes a individual with a randomized protocol.""" steps = [] for i in range(self.config.steps_in_protocol): random_step = protocols.VoltageClampStep( voltage=random.uniform(*self.config.step_voltage_bounds), duration=random.uniform(*self.config.step_duration_bounds)) steps.append(random_step) return genetic_algorithm_results.VCOptimizationIndividual( protocol=protocols.VoltageClampProtocol(steps=steps), fitness=0)
def test_evaluate_returns_zero_error(self): protocol = protocols.VoltageClampProtocol(steps=[ # Intentionally set to large voltage to cause null trace. protocols.VoltageClampStep(voltage=1000.0, duration=0.5), protocols.VoltageClampStep(voltage=3.7886, duration=1.1865), ]) error = self.vc_ga._evaluate( individual=genetic_algorithm_results.VCOptimizationIndividual( protocol=protocol)) self.assertEqual(error, 0)
def test_select(self): protocol = protocols.VoltageClampProtocol(steps=[ protocols.VoltageClampStep(voltage=0.2, duration=0.1), protocols.VoltageClampStep(voltage=-0.3, duration=0.1865), ]) population = [ genetic_algorithm_results.VCOptimizationIndividual( protocol=protocol, fitness=0), genetic_algorithm_results.VCOptimizationIndividual( protocol=protocol, fitness=5), genetic_algorithm_results.VCOptimizationIndividual( protocol=protocol, fitness=1), genetic_algorithm_results.VCOptimizationIndividual( protocol=protocol, fitness=3.4), genetic_algorithm_results.VCOptimizationIndividual( protocol=protocol, fitness=8), genetic_algorithm_results.VCOptimizationIndividual( protocol=protocol, fitness=10), genetic_algorithm_results.VCOptimizationIndividual( protocol=protocol, fitness=2.2), genetic_algorithm_results.VCOptimizationIndividual( protocol=protocol, fitness=1.2), ] random.seed(10) new_population = self.vc_ga._select(population=population) # Expected population was discovered through printing out # random.sample() with seed set to 10. expected_population = [ genetic_algorithm_results.VCOptimizationIndividual( protocol=protocol, fitness=3.4), genetic_algorithm_results.VCOptimizationIndividual( protocol=protocol, fitness=5), genetic_algorithm_results.VCOptimizationIndividual( protocol=protocol, fitness=2.2), genetic_algorithm_results.VCOptimizationIndividual( protocol=protocol, fitness=8), genetic_algorithm_results.VCOptimizationIndividual( protocol=protocol, fitness=1.2), genetic_algorithm_results.VCOptimizationIndividual( protocol=protocol, fitness=10), genetic_algorithm_results.VCOptimizationIndividual( protocol=protocol, fitness=5), genetic_algorithm_results.VCOptimizationIndividual( protocol=protocol, fitness=10), ] self.assertListEqual(new_population, expected_population)
def plot_baseline_voltage_clamp_trace(): model = paci_2018.PaciModel() steps = [ protocols.VoltageClampStep(duration=0.9, voltage=-0.08), protocols.VoltageClampStep(duration=0.7, voltage=-0.12), protocols.VoltageClampStep(duration=0.5, voltage=-0.06), protocols.VoltageClampStep(duration=0.9, voltage=-0.04), protocols.VoltageClampStep(duration=0.95, voltage=0.02), protocols.VoltageClampStep(duration=0.9, voltage=0.04), ] trace = model.generate_response( protocol=protocols.VoltageClampProtocol(steps=steps)) trace.plot_with_currents() plt.savefig( 'figures/Voltage Protocol Figure/baseline_voltage_clamp_trace.svg')
def test_mate(self): protocol_one = protocols.VoltageClampProtocol(steps=[ protocols.VoltageClampStep(voltage=1.0, duration=0.5), protocols.VoltageClampStep(voltage=2.0, duration=0.75), ]) protocol_two = protocols.VoltageClampProtocol(steps=[ protocols.VoltageClampStep(voltage=5.0, duration=1.0), protocols.VoltageClampStep(voltage=-2.0, duration=2.75), ]) for i in range(500): random.seed(i) r_num_one = random.random() r_num_two = random.random() if r_num_two < self.vc_ga.config.gene_swap_probability < r_num_one: random.seed(i) break else: self.fail(msg='Could not find seed to meet required behavior.') self.vc_ga._mate( i_one=genetic_algorithm_results.VCOptimizationIndividual( protocol=protocol_one), i_two=genetic_algorithm_results.VCOptimizationIndividual( protocol=protocol_two)) expected_protocol_one = protocols.VoltageClampProtocol(steps=[ protocols.VoltageClampStep(voltage=1.0, duration=0.5), protocols.VoltageClampStep(voltage=-2.0, duration=2.75), ]) expected_protocol_two = protocols.VoltageClampProtocol(steps=[ protocols.VoltageClampStep(voltage=5.0, duration=1.0), protocols.VoltageClampStep(voltage=2.0, duration=0.75), ]) self.assertEqual(protocol_one, expected_protocol_one) self.assertEqual(protocol_two, expected_protocol_two)
def test_mutate_steps_falls_between_bounds(self): protocol = protocols.VoltageClampProtocol(steps=[ protocols.VoltageClampStep(voltage=-1.0, duration=0.5), protocols.VoltageClampStep(voltage=0.4, duration=0.75), ]) individual = genetic_algorithm_results.VCOptimizationIndividual( protocol=protocol) for _ in range(5000): self.vc_ga._mutate(individual=individual) for i in individual.protocol.steps: self.assertTrue( self.vc_ga.config.step_duration_bounds[0] <= i.duration <= self.vc_ga.config.step_duration_bounds[1]) self.assertTrue( self.vc_ga.config.step_voltage_bounds[0] <= i.voltage <= self.vc_ga.config.step_voltage_bounds[1])
def setUp(self): vc_ga_config = ga_configs.VoltageOptimizationConfig( window=100, step_size=1, steps_in_protocol=6, step_duration_bounds=(0.1, 2.0), step_voltage_bounds=(-1.2, 0.6), population_size=2, max_generations=2, mate_probability=0.9, mutate_probability=1.0, gene_swap_probability=0.5, gene_mutation_probability=0.1, tournament_size=3) self.ga_result = genetic_algorithm_results. \ GAResultVoltageClampOptimization(config=vc_ga_config) sample_vc_protocol = protocols.VoltageClampProtocol(steps=[ protocols.VoltageClampStep(duration=0.1, voltage=-0.08), protocols.VoltageClampStep(duration=0.1, voltage=-0.12), protocols.VoltageClampStep(duration=0.5, voltage=-0.06), protocols.VoltageClampStep(duration=0.05, voltage=-0.04), protocols.VoltageClampStep(duration=0.15, voltage=0.02), protocols.VoltageClampStep(duration=0.025, voltage=-0.08), protocols.VoltageClampStep(duration=0.3, voltage=0.04), ]) generation_one = [ genetic_algorithm_results.VCOptimizationIndividual( protocol=sample_vc_protocol, fitness=0), genetic_algorithm_results.VCOptimizationIndividual( protocol=sample_vc_protocol, fitness=2), ] generation_two = [ genetic_algorithm_results.VCOptimizationIndividual( protocol=sample_vc_protocol, fitness=5), genetic_algorithm_results.VCOptimizationIndividual( protocol=sample_vc_protocol, fitness=9), ] self.ga_result.generations.append(generation_one) self.ga_result.generations.append(generation_two)
import paci_2018 import protocols import pdb import matplotlib.pyplot as plt VC_PROTOCOL_NEGATIVE80 = protocols.VoltageClampProtocol(steps=[ protocols.VoltageClampStep(duration=5.0, voltage=-0.08), ]) VC_PROTOCOL_ZERO = protocols.VoltageClampProtocol(steps=[ protocols.VoltageClampStep(duration=20.0, voltage=-0.0000008), ]) def main(): """Run parameter tuning or voltage clamp protocol experiments here """ paci_model = paci_2018.PaciModel() paci_model.generate_response(protocol=VC_PROTOCOL_ZERO) fig = plt.figure(figsize=(10, 5)) ax_1 = fig.add_subplot(511) ax_1.plot([1000 * i for i in paci_model.t], [i * 1000 for i in paci_model.y_voltage], 'b', label='Voltage') plt.xlabel('Time (ms)') plt.ylabel(r'$V_m$ (mV)') I_NaCa = [] I_CaL = []
def setUp(self): step_one = protocols.VoltageClampStep(duration=1.0, voltage=0.02) step_two = protocols.VoltageClampStep(duration=2.5, voltage=-0.03) step_three = protocols.VoltageClampStep(duration=0.3, voltage=0.05) steps = [step_one, step_two, step_three] self.voltage_protocol = protocols.VoltageClampProtocol(steps=steps)
def generate_config_list(): parameters = [ ga_configs.Parameter(name='g_na', default_value=3671.2302), ga_configs.Parameter(name='g_f_s', default_value=30.10312), ] sap_protocol = protocols.SingleActionPotentialProtocol() ip_protocol = protocols.IrregularPacingProtocol(duration=3, stimulation_offsets=[0.1]) vc_protocol = protocols.VoltageClampProtocol(steps=[ protocols.VoltageClampStep(duration=0.1, voltage=-0.08), protocols.VoltageClampStep(duration=0.1, voltage=-0.12), protocols.VoltageClampStep(duration=0.5, voltage=-0.06), protocols.VoltageClampStep(duration=0.05, voltage=-0.04), protocols.VoltageClampStep(duration=0.15, voltage=0.02), protocols.VoltageClampStep(duration=0.025, voltage=-0.08), protocols.VoltageClampStep(duration=0.3, voltage=0.04) ]) sap_config = ga_configs.ParameterTuningConfig( population_size=2, max_generations=2, protocol=sap_protocol, tunable_parameters=parameters, params_lower_bound=0.5, params_upper_bound=1.5, mutate_probability=1., mate_probability=0.9, gene_swap_probability=0.5, gene_mutation_probability=0.1, tournament_size=2) ip_config = ga_configs.ParameterTuningConfig(population_size=2, max_generations=2, protocol=ip_protocol, tunable_parameters=parameters, params_lower_bound=0.5, params_upper_bound=1.5, mutate_probability=1., mate_probability=0.9, gene_swap_probability=0.5, gene_mutation_probability=0.1, tournament_size=2) vc_config = ga_configs.ParameterTuningConfig(population_size=2, max_generations=2, protocol=vc_protocol, tunable_parameters=parameters, params_lower_bound=0.5, params_upper_bound=1.5, mutate_probability=1., mate_probability=0.9, gene_swap_probability=0.5, gene_mutation_probability=0.1, tournament_size=2) combined_config = ga_configs.ParameterTuningConfig( population_size=2, max_generations=2, protocol=ip_protocol, tunable_parameters=parameters, params_lower_bound=0.5, params_upper_bound=1.5, mutate_probability=1., mate_probability=0.9, gene_swap_probability=0.5, gene_mutation_probability=0.1, tournament_size=2, secondary_protocol=vc_protocol) return [sap_config, ip_config, vc_config, combined_config]
ga_configs.Parameter(name='G_bCa', default_value=1), ga_configs.Parameter(name='K_NaCa', default_value=1) ] # Parameters are sorted alphabetically to maintain order during each # generation of the genetic algorithm. PARAMETERS.sort(key=lambda x: x.name) SAP_PROTOCOL_KERNIK = protocols.SingleActionPotentialProtocol(1800) VC_PROTOCOL = protocols.VoltageClampProtocol(steps=[ protocols.VoltageClampStep(duration=0.050, voltage=-0.08), protocols.VoltageClampStep(duration=0.050, voltage=-0.12), protocols.VoltageClampStep(duration=0.500, voltage=-0.057), protocols.VoltageClampStep(duration=0.025, voltage=-0.04), protocols.VoltageClampStep(duration=0.075, voltage=0.02), protocols.VoltageClampStep(duration=0.025, voltage=-0.08), protocols.VoltageClampStep(duration=0.250, voltage=0.04), protocols.VoltageClampStep(duration=1.900, voltage=-0.03), protocols.VoltageClampStep(duration=0.750, voltage=0.04), protocols.VoltageClampStep(duration=1.725, voltage=-0.03), protocols.VoltageClampStep(duration=0.650, voltage=-0.08), ]) SAP_CONFIG = ga_configs.ParameterTuningConfig(population_size=10, max_generations=4, protocol=SAP_PROTOCOL_KERNIK, tunable_parameters=PARAMETERS, params_lower_bound=0.1, params_upper_bound=3, mate_probability=0.9, mutate_probability=0.9,