Example #1
0
 def save_protocol(self):
     """
     Tests if the correct parts are saved/loaded from disk using the
     ``save_protocol()`` method.
     """
     ipath = os.path.join(myotest.DIR_DATA, 'lr-1991.mmt')
     opath = os.path.join(myotest.DIR_OUT, 'loadsavetest.mmt')
     if os.path.exists(opath):
         os.remove(opath)
     # Test example loading
     p = myokit.load_protocol('example')
     self.assertIsInstance(p, myokit.Protocol)
     # Test file loading
     p = myokit.load_protocol(ipath)
     self.assertIsInstance(p, myokit.Protocol)
     if os.path.exists(opath):
         os.remove(opath)
     try:
         myokit.save_protocol(opath, p)
         # Test no other parts were written
         with open(opath, 'r') as f:
             text = f.read()
         self.assertFalse('[[model]]' in text)
         self.assertTrue('[[protocol]]' in text)
         self.assertFalse('[[script]]' in text)
         # Test reloading
         pp = myokit.load_protocol(opath)
         self.assertIsInstance(pp, myokit.Protocol)
         self.assertEqual(pp.code(), p.code())
     finally:
         os.remove(opath)
Example #2
0
 def test_save_protocol(self):
     """
     Test if the correct parts are saved/loaded from disk using the
     ``save_protocol()`` method.
     """
     ipath = os.path.join(DIR_DATA, 'lr-1991.mmt')
     # Test example loading
     p = myokit.load_protocol('example')
     self.assertIsInstance(p, myokit.Protocol)
     # Test file loading
     p = myokit.load_protocol(ipath)
     self.assertIsInstance(p, myokit.Protocol)
     with TemporaryDirectory() as d:
         opath = d.path('test.mmt')
         myokit.save_protocol(opath, p)
         # Test no other parts were written
         with open(opath, 'r') as f:
             text = f.read()
         self.assertFalse('[[model]]' in text)
         self.assertTrue('[[protocol]]' in text)
         self.assertFalse('[[script]]' in text)
         # Test reloading
         pp = myokit.load_protocol(opath)
         self.assertIsInstance(pp, myokit.Protocol)
         self.assertEqual(pp.code(), p.code())
Example #3
0
def load_myokit_protocol(protocol, variant=False):
    """
    Loads the Myokit protocol with the given index (1-7). For Pr6 and Pr7, the
    protocol only has the steps for capacitance filtering.
    """
    protocol_files = {
        1: os.path.join(PROTO, 'pr1-activation-kinetics-1.mmt'),
        2: os.path.join(PROTO, 'pr2-activation-kinetics-2.mmt'),
        3: os.path.join(PROTO, 'pr3-steady-activation.mmt'),
        4: os.path.join(PROTO, 'pr4-inactivation.mmt'),
        5: os.path.join(PROTO, 'pr5-deactivation.mmt'),
        6: os.path.join(PROTO, 'pr6-ap-steps.mmt'),
        7: os.path.join(PROTO, 'pr7-sine-wave-steps.mmt'),
    }

    # Load variants for Pr1 and Pr2 for cells 7 and 8
    if variant:
        if protocol == 1:
            protocol = os.path.join(PROTO, 'pr1b.mmt')
        elif protocol == 2:
            protocol = os.path.join(PROTO, 'pr2b.mmt')
        else:
            raise ValueError('Variants only exist for Pr1 and Pr2')
    else:
        protocol = protocol_files[protocol]

    # Load Myokit protocol
    return myokit.load_protocol(protocol)
Example #4
0
def run_model(model,
              cell,
              protocol,
              time,
              voltage,
              current,
              plot='unfold',
              label=None,
              axes=None):

    # Select protocol file
    protocol_file = os.path.join(root, protocol + '.mmt')
    print(protocol_file)
    myokit_protocol = myokit.load_protocol(protocol_file)

    # Estimate noise from start of data
    sigma_noise = np.std(current[:2000], ddof=1)

    # fetch cmaes parameters
    obtained_parameters = model.fetch_parameters()

    # Cell-specific parameters
    temperature = model.temperature(cell)
    lower_conductance = model.conductance_limit(cell)

    # Apply capacitance filter based on protocol
    print('Applying capacitance filtering')
    time, voltage, current = model.capacitance(myokit_protocol, 0.1, time,
                                               voltage, current)

    forward_model = model.ForwardModel(myokit_protocol,
                                       temperature,
                                       sine_wave=False)
    problem = pints.SingleOutputProblem(forward_model, time, current)
    log_likelihood = pints.KnownNoiseLogLikelihood(problem, sigma_noise)
    log_prior = model.LogPrior(lower_conductance)
    log_posterior = pints.LogPosterior(log_likelihood, log_prior)

    # Show obtained parameters and score
    obtained_log_posterior = log_posterior(obtained_parameters)
    print('Kylie sine-wave parameters:')
    for x in obtained_parameters:
        print(pints.strfloat(x))
    print('Final log-posterior:')
    print(pints.strfloat(obtained_log_posterior))

    # Simulate
    simulated = forward_model.simulate(obtained_parameters, time)

    if plot == 'unfold':
        axes[0].plot(time, voltage, color='red')  #, label='voltage')
        #axes[0].legend(loc='upper right')
        axes[1].plot(time, current, alpha=0.3,
                     color='red')  #, label='measured current')

        if label == 0:
            model_name = 'circularCOIIC'
            axes[1].plot(time,
                         simulated,
                         alpha=1,
                         color='blue',
                         label=model_name)
        elif label == 1:
            model_name = 'linearCOI'
            axes[1].plot(time,
                         simulated,
                         alpha=1,
                         color='magenta',
                         label=model_name)
        elif label == 2:
            model_name = 'linearCCOI'
            axes[1].plot(time,
                         simulated,
                         alpha=1,
                         color='seagreen',
                         label=model_name)
        elif label == 3:
            model_name = 'linearCCCOI'
            axes[1].plot(time,
                         simulated,
                         alpha=1,
                         color='seagreen',
                         label=model_name)
    #axes.subplot(2,1,1)

    else:

        IkrModel.fold_plot(protocol, time, voltage, [current, simulated])
Example #5
0
        #
        # Load data
        #
        log = myokit.DataLog.load_csv(data_file).npview()
        time = log.time()
        current = log['current']
        voltage = log['voltage']
        del (log)

        #
        # Load protocol
        #
        if protocol_name == 'sine-wave':
            protocol_file = os.path.join(root, 'steps.mmt')
            protocol = myokit.load_protocol(protocol_file)
            sw = 1
            #
            # Apply capacitance filter based on protocol
            #
            print('Applying capacitance filtering')
            time, voltage, current = forwardModel.capacitance(
                protocol, 0.1, time, voltage, current)

        elif protocol_name == 'original-sine':
            root = os.path.abspath('original-sine-data')
            protocol_file = os.path.join(root, 'steps.mmt')
            protocol = myokit.load_protocol(
                protocol_file)  # Same steps before sine wave
            print('Applying capacitance filtering')
            time, voltage, current = forwardModel.capacitance(
Example #6
0
#
# Select protocol file
#
protocol_file = os.path.join(root, 'steps.mmt')

#
# Cell-specific parameters
#
temperature = forwardModel.temperature(cell)
lower_conductance = forwardModel.conductance_limit(cell)

#
# Load protocol
#
protocol = myokit.load_protocol(protocol_file)

#
# Load data
#
log = myokit.DataLog.load_csv(data_file).npview()
time = log.time()
current = log['current']
voltage = log['voltage']
del (log)

#
# Estimate noise from start of data
# Kylie uses the first 200ms, where I = 0 + noise
#
sigma_noise = np.std(current[:2000], ddof=1)
Example #7
0
                    metavar='N',
                    help='get model stats for mcmc or optimisation')
args = parser.parse_args()
#sys.path.append(os.path.abspath('models_forward'))

cell = args.cell
root = os.path.abspath('sine-wave-data')
print(root)
data_file_sine = os.path.join(root, 'cell-' + str(cell) + '.csv')
protocol_file_sine = os.path.join(root, 'steps.mmt')

root = os.path.abspath('ap-data')
print(root)
data_file_ap = os.path.join(root, 'cell-' + str(cell) + '.csv')

protocol_sine = myokit.load_protocol(protocol_file_sine)

log_sine = myokit.DataLog.load_csv(data_file_sine).npview()
time_sine = log_sine.time()
current_sine = log_sine['current']
voltage_sine = log_sine['voltage']
del (log_sine)

log_ap = myokit.DataLog.load_csv(data_file_ap).npview()
time_ap = log_ap.time()
current_ap = log_ap['current']
voltage_ap = log_ap['voltage']
del (log_ap)
protocol_ap = [time_ap, voltage_ap]

model_ppc_tarces = []
Example #8
0
    def _test(self, e, model=None, protocol=None):
        """
        Test a given exporter `e`.
        """
        # Test info method.
        self.assertIsInstance(e.post_export_info(), basestring)

        # Load model, protocol
        m, p = model, protocol
        if m is None:
            m = myokit.load_model('example')
        if p is None:
            p = myokit.load_protocol('example')

        with TemporaryDirectory() as d:
            path = d.path()

            # Try exports
            exports = 0

            # Try model export
            if e.supports_model():
                exports += 1

                # Basic export
                fpath = os.path.join(path, 'model.txt')
                ret = e.model(fpath, m)
                self.assertIsNone(ret)
                self.assertTrue(os.path.isfile(fpath))

                # Unnamed model
                name = m.name()
                try:
                    m.set_name(None)
                    ret = e.model(fpath, m)
                    self.assertIsNone(ret)
                    self.assertTrue(os.path.isfile(fpath))
                finally:
                    m.set_name(name)

            else:
                self.assertRaises(NotImplementedError, e.model, path, m)

            # Try runnable export
            if e.supports_runnable():
                exports += 1

                # Check without protocol
                dpath = os.path.join(path, 'runnable1')
                ret = e.runnable(dpath, m)
                self.assertIsNone(ret)
                self.assertTrue(os.path.isdir(dpath))
                self.assertTrue(len(os.listdir(dpath)) > 0)

                # Check with protocol
                dpath = os.path.join(path, 'runnable2')
                ret = e.runnable(dpath, m, p)
                self.assertIsNone(ret)
                self.assertTrue(os.path.isdir(dpath))
                self.assertTrue(len(os.listdir(dpath)) > 0)

                # Write to complex path
                dpath = os.path.join(path, 'runnable3', 'nest', 'test')
                ret = e.runnable(dpath, m, p)
                self.assertIsNone(ret)
                self.assertTrue(os.path.isdir(dpath))
                self.assertTrue(len(os.listdir(dpath)) > 0)

            else:

                self.assertRaises(NotImplementedError, e.runnable, path, m, p)

            # Test if any exports were available
            if exports == 0:
                raise Exception('No types of export supported by: ' + exporter)