def test_evaluate_performance_with_combined_protocol(self):
        config = ga_configs.ParameterTuningConfig(
            population_size=2,
            max_generations=2,
            protocol=protocols.SingleActionPotentialProtocol(),
            tunable_parameters=[
                ga_configs.Parameter(name='g_b_na', default_value=0.95),
                ga_configs.Parameter(name='g_na', default_value=3671.2302)
            ],
            params_lower_bound=0.9,
            params_upper_bound=1.1,
            mutate_probability=1.,
            mate_probability=0.9,
            gene_swap_probability=0.5,
            gene_mutation_probability=0.15,
            tournament_size=2)
        single_ap_ga = parameter_tuning_genetic_algorithm.\
            ParameterTuningGeneticAlgorithm(config=config)
        single_ap_error = single_ap_ga._evaluate_performance(
            new_parameters=[0.94, 3000.])

        config.secondary_protocol = protocols.IrregularPacingProtocol(
            duration=5, stimulation_offsets=[0.1, 0.2])
        combined_protocol_ga = parameter_tuning_genetic_algorithm.\
            ParameterTuningGeneticAlgorithm(config=config)
        combined_protocol_error = combined_protocol_ga._evaluate_performance(
            new_parameters=[0.94, 3000.])

        # Combined protocol error should be larger than a single protocol error.
        self.assertGreater(combined_protocol_error, single_ap_error)
Example #2
0
 def setUp(self):
     config = ga_configs.ParameterTuningConfig(
         population_size=2,
         max_generations=2,
         protocol=protocols.SingleActionPotentialProtocol(),
         tunable_parameters=[
             ga_configs.Parameter(name='g_b_na', default_value=1.),
             ga_configs.Parameter(name='g_na', default_value=3600.)
         ],
         params_lower_bound=0.9,
         params_upper_bound=1.1,
         mate_probability=0.9,
         mutate_probability=1.,
         gene_swap_probability=0.5,
         gene_mutation_probability=0.15,
         tournament_size=2)
     self.ga_result = genetic_algorithm_results.GAResultParameterTuning(
         config=config)
     generation_one = [
         genetic_algorithm_results.ParameterTuningIndividual(
             parameters=[0.95, 3671.2302], fitness=0),
         genetic_algorithm_results.ParameterTuningIndividual(
             parameters=[0.1, 3400.0], fitness=2),
     ]
     generation_two = [
         genetic_algorithm_results.ParameterTuningIndividual(
             parameters=[0.6, 3500.0], fitness=5),
         genetic_algorithm_results.ParameterTuningIndividual(
             parameters=[0.9, 3600.0], fitness=9),
     ]
     self.ga_result.generations.append(generation_one)
     self.ga_result.generations.append(generation_two)
    def test_run_comparing_experiment_raises_value_error_double_protocol(self):
        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()
        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)
        configs = generate_config_list()
        configs.append(sap_config)

        with self.assertRaises(ValueError) as ve:
            parameter_tuning_experiments.run_comparison_experiment(
                configs=configs, iterations=2)

        self.assertEqual('Configs do not have unique protocols.',
                         str(ve.exception))
    def test_make_parameter_scaling_examples(self):
        params = [[1, 2], [1.5, 3]]
        protocol_type = 'SAP'
        default_params = [
            ga_configs.Parameter(name='g_na', default_value=15),
            ga_configs.Parameter(name='g_ca', default_value=1),
        ]

        examples = parameter_tuning_experiments._make_parameter_scaling_examples(
            params=params,
            protocol_type=protocol_type,
            default_params=default_params)

        expected_examples = [[1, 'g_na', 'SAP'], [2, 'g_ca', 'SAP'],
                             [1.5, 'g_na', 'SAP'], [3, 'g_ca', 'SAP']]

        self.assertListEqual(examples, expected_examples)
 def setUp(self):
     config = ga_configs.ParameterTuningConfig(
         population_size=2,
         max_generations=2,
         protocol=protocols.SingleActionPotentialProtocol(),
         tunable_parameters=[
             ga_configs.Parameter(name='g_b_na', default_value=0.95),
             ga_configs.Parameter(name='g_na', default_value=3671.2302)
         ],
         params_lower_bound=0.9,
         params_upper_bound=1.1,
         mutate_probability=1.0,
         mate_probability=0.9,
         gene_swap_probability=0.5,
         gene_mutation_probability=0.15,
         tournament_size=2)
     self.ga = parameter_tuning_genetic_algorithm.\
         ParameterTuningGeneticAlgorithm(config=config)
Example #6
0
    def test_update_parameters(self):
        protocol = protocols.SingleActionPotentialProtocol()
        tunable_parameters = [
            ga_configs.Parameter(name='G_Na', default_value=1),
            ga_configs.Parameter(name='G_F', default_value=1),
            ga_configs.Parameter(name='G_Ks', default_value=1),
            ga_configs.Parameter(name='G_Kr', default_value=1),
            ga_configs.Parameter(name='G_K1', default_value=1),
            ga_configs.Parameter(name='G_bNa', default_value=1),
            ga_configs.Parameter(name='G_NaL', default_value=1),
            ga_configs.Parameter(name='G_CaL', default_value=1),
            ga_configs.Parameter(name='G_pCa', default_value=1),
            ga_configs.Parameter(name='G_bCa', default_value=1)
        ]
        new_params = [.7, .7, .7, .7, .7, .7, .7, .7, .7, .7]

        baseline_trace = paci_2018.generate_trace(protocol)
        new_trace = paci_2018.generate_trace(protocol, tunable_parameters,
                                             new_params)

        tst.assert_raises(AssertionError, tst.assert_array_equal,
                          baseline_trace.y, new_trace.y,
                          'updating parameters does not change trace')
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]
"""Main driver for program. Before running, make sure to have a directory
called `figures` for matplotlib pictures to be stored in."""

import copy
import time

import parameter_tuning_experiments
import protocols
import ga_configs
import genetic_algorithm_results
import voltage_clamp_optimization_experiments

PARAMETERS = [
    ga_configs.Parameter(name='G_Na', default_value=1),
    ga_configs.Parameter(name='G_F', default_value=1),
    ga_configs.Parameter(name='G_Ks', default_value=1),
    ga_configs.Parameter(name='G_Kr', default_value=1),
    ga_configs.Parameter(name='G_K1', default_value=1),
    ga_configs.Parameter(name='G_bNa', default_value=1),
    ga_configs.Parameter(name='G_NaL', default_value=1),
    ga_configs.Parameter(name='G_CaL', default_value=1),
    ga_configs.Parameter(name='G_pCa', default_value=1),
    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)