예제 #1
0
 def simulate():
     print "running"
     #if modelscales_select.value == "No_Models":
     #print modelscales_select.value
     #else:
     #print modelscales_select.value
     print pwd
     sys.path.append(pwd)  # uncomment only when using executiveViz
     ec = ExecutiveControl()
     modelscale = models_select.value.split()[0]
     modelname = models_select.value.split()[1]
     chosenmodel = ec.choose_model(modelscale=modelscale,
                                   modelname=modelname)
     parameters = ast.literal_eval(runtime_input.value)
     print parameters
     print stimulation.value
     if stimulation.value == "yes":
         stimparameters = ast.literal_eval(stim_type_input.value)
         stimparameters.update(
             {"stimlist": ast.literal_eval(stim_list_input.value)})
         ec.launch_model(parameters=parameters,
                         onmodel=chosenmodel,
                         stimparameters=stimparameters,
                         stimloc=stim_loc_input.value)
         #with Profiler() as prof, ResourceProfiler() as rprof:
         #    ec.launch_model ( parameters=parameters, onmodel=chosenmodel,
         #                      stimparameters=stimparameters, stimloc=stim_loc_input.value )
     else:
         ec.launch_model(parameters=parameters, onmodel=chosenmodel)
         #with Profiler() as prof, ResourceProfiler() as rprof:
         #    ec.launch_model( parameters=parameters, onmodel=chosenmodel )
         #myplot = visualize([prof, rprof])
     ec.save_response()
     print "done running"
 def produce_restingVm(self, **kwargs):
     """
     kwargs = { "parameters": dictionary with keys,
                "stimparameters": dictionary with keys "type" and "stimlist",
                "onmodel": instantiated model }
     """
     print("Sim produce_restingVm starting ...")
     ec = ExecutiveControl() # only works when in ~/cerebmodels
     model = ec.launch_model( parameters = kwargs["parameters"],
                              stimparameters = kwargs["stimparameters"],
                              stimloc = kwargs["stimloc"], onmodel = kwargs["onmodel"],
                              capabilities = {"model": "produce_voltage_response",
                                              "vtest": ProducesElectricalResponse},
                              mode="capability")
     #self.fullfilename # already saved by invoking produce_voltage_response above
     #print("Signal Processing ...")
     nwbfile = rm.load_nwbfile(model.fullfilename)
     orderedepochs = rm.order_all_epochs_for_region(nwbfile=nwbfile, region="soma")
     timestamps_over_epochs = [ rm.timestamps_for_epoch( orderedepochs[i] )
                                for i in range(len(orderedepochs)) ]
     data_over_epochs = [ rm.data_for_epoch( orderedepochs[i] )
                                for i in range(len(orderedepochs)) ]
     baseVms = spm.distill_Vm_pre_epoch( timestamps = timestamps_over_epochs,
                                         datavalues = data_over_epochs )
     #print("Signal Processing Done.")
     setattr(model, "prediction", baseVms)
     print("Simulation produce_restingVm Done.")
     return model
예제 #3
0
 def produce_inputR(self, roi, **kwargs):
     """
     roi, region of interest is a string, i.e, 1 key in chosenmodel.regions
     kwargs = { "parameters": dictionary with keys,
                "stimparameters": dictionary with keys "type" and "stimlist",
                "onmodel": instantiated model }
     """
     print("Sim produce_" + roi + "_inputR starting ...")
     ec = ExecutiveControl()  # only works when in ~/cerebmodels
     model = ec.launch_model(parameters=kwargs["parameters"],
                             stimparameters=kwargs["stimparameters"],
                             stimloc=kwargs["stimloc"],
                             onmodel=kwargs["onmodel"],
                             capabilities={
                                 "model": "produce_voltage_response",
                                 "vtest": ProducesElectricalResponse
                             },
                             mode="capability")
     nwbfile = rm.load_nwbfile(model.fullfilename)
     orderedepochs = rm.order_all_epochs_for_region(nwbfile=nwbfile,
                                                    region=roi)
     timestamps_over_epochs = [
         rm.timestamps_for_epoch(orderedepochs[i])
         for i in range(len(orderedepochs))
     ]
     data_over_epochs = [
         rm.data_for_epoch(orderedepochs[i])
         for i in range(len(orderedepochs))
     ]
     baseVms = spm.distill_baseVm_pre_epoch(
         timestamps=timestamps_over_epochs, datavalues=data_over_epochs)
     setattr(model, "prediction",
             self._compute_inputR(baseVms, kwargs["stimparameters"]))
     print("Simulation produce_" + roi + "_inputR Done.")
     return model
예제 #4
0
 def generate_prediction(self, model, verbose=False):
     """
     Generates resting Vm from soma.
     The function is automatically called by sciunit.Test which this test is a child of.
     Therefore as part of sciunit generate_prediction is mandatory.
     """
     #self.confidence = confidence # set confidence for test 90%, 95% (default), 99%
     #
     runtimeparam = {
         "dt": 0.025,
         "celsius": 30,
         "tstop": 1000.0,
         "v_init": -80.
     }
     stimparam = {
         "type": ["current", "IClamp"],
         "stimlist": [{
             "amp": 0.006,
             "dur": 800.0,
             "delay": 100.0
         }],
         "tstop": runtimeparam["tstop"]
     }
     ec = ExecutiveControl()
     #ec.chosenmodel = model
     #ec.chosenmodel.restingVm = \
     model = ec.launch_model(parameters=runtimeparam,
                             stimparameters=stimparam,
                             stimloc="soma",
                             onmodel=model,
                             capabilities={
                                 "model": "produce_restingVm",
                                 "vtest": ProducesSomaRestingVm
                             },
                             mode="capability")
     #return pq.Quantity( numpy.mean(ec.chosenmodel.prediction), # prediction
     #                    units = self.observation["units"] )
     return pq.Quantity(
         numpy.mean(model.prediction),  # prediction
         units=self.observation["units"])
    def produce_voltage_response(self, **kwargs):
        """generic/essential model response

        **Keyword Arguments:**

        kwargs = { "parameters": dictionary with keys,
                   "stimparameters": None or dictionary with keys "type" and "stimlist",
                   "onmodel": instantiated model }
        """
        #ExecutiveControl.launch_model_raw("cells")
        print("Simulation produce_voltage_response starting ...")
        ec = ExecutiveControl() # only works when in ~/cerebmodels
        model = ec.launch_model( parameters = kwargs["parameters"],
                                 stimparameters = kwargs["stimparameters"],
                                 stimloc = kwargs["stimloc"],
                                 onmodel = kwargs["onmodel"], mode = "raw" )
        print("File saving ...")
        fullfilename = ec.save_response()
        setattr(model, "fullfilename", fullfilename)
        print("File saved.")
        print("Simulation produce_voltage_response Done.")
        return model
예제 #6
0
 def produce_soma_spikeheight(self, **kwargs):
     """
     kwargs = { "parameters": dictionary with keys,
                "stimparameters": dictionary with keys "type" and "stimlist",
                "onmodel": instantiated model }
     """
     print("Sim produce_soma_spikeheight starting ...")
     ec = ExecutiveControl()  # only works when in ~/cerebmodels
     model = ec.launch_model(parameters=kwargs["parameters"],
                             stimparameters=kwargs["stimparameters"],
                             stimloc=kwargs["stimloc"],
                             onmodel=kwargs["onmodel"],
                             capabilities={
                                 "model": "produce_voltage_response",
                                 "vtest": ProducesElectricalResponse
                             },
                             mode="capability")
     nwbfile = rm.load_nwbfile(model.fullfilename)
     orderedepochs = rm.order_all_epochs_for_region(nwbfile=nwbfile,
                                                    region="soma v")
     timestamps_over_epochs = [
         rm.timestamps_for_epoch(orderedepochs[i])
         for i in range(len(orderedepochs))
     ]
     data_over_epochs = [
         rm.data_for_epoch(orderedepochs[i])
         for i in range(len(orderedepochs))
     ]
     baseVm = spm.distill_baseVm_pre_epoch(
         timestamps=timestamps_over_epochs, datavalues=data_over_epochs)
     try:
         peakVms = spm.distill_peakVm_from_spikes(
             timestamps=timestamps_over_epochs, datavalues=data_over_epochs)
     except:
         peakVms = baseVm
     setattr(model, "prediction", peakVms[0] - baseVm[0])
     print("Simulation produce_soma_spikeheight Done.")
     return model
예제 #7
0
class ReaderTest(unittest.TestCase):
    def setUp(self):
        self.pwd = os.getcwd()
        # Create two files with/without stimulus
        #fab = Fabricator() # NOTE: Reader() is an exception for not instantiating
        os.chdir(rootwd)
        self.chosenmodel = DummyCell()
        os.chdir(pwd)
        #
        self.ec = ExecutiveControl()
        #
        if os.path.isdir(pwd + os.sep + "responses"):
            shutil.rmtree("responses")
        #
        runtimeparam = {"dt": 0.1, "celsius": 30, "tstop": 200.0, "v_init": 65}
        stimparam = {
            "type": ["current", "IClamp"],
            "stimlist": [{
                "amp": 0.5,
                "dur": 100.0,
                "delay": 10.0
            }, {
                "amp": 1.0,
                "dur": 50.0,
                "delay": 10.0 + 100.0
            }],
            "tstop":
            runtimeparam["tstop"]
        }
        #
        # Write for stimulus
        self.ec.launch_model(parameters=runtimeparam,
                             stimparameters=stimparam,
                             stimloc=self.chosenmodel.cell.soma,
                             onmodel=self.chosenmodel)
        self.ec.save_response()
        # Get filename
        self.file_stim = fm.show_filenames_with_path(
            ["responses", "cells", "DummyTest"])
        # save for comparison
        self.stimulus_for_comparison = numpy.array(
            self.ec.recordings["stimulus"])
        #
        # Write for nostimulus
        self.ec.launch_model(parameters=runtimeparam, onmodel=self.chosenmodel)
        self.ec.save_response()
        # Get filename
        self.file_nostim = fm.show_filenames_with_path(
            ["responses", "cells", "DummyTest"])
        for key in self.file_stim:
            del self.file_nostim[key]

    #@unittest.skip("reason for skipping")
    def test_1_init(self):
        # loads nwbfile, extracts modelname, modelscale & instantiate model
        os.chdir(rootwd)
        for key in self.file_nostim:
            reader_io_nostim = Reader(self.file_nostim[key])
        for key in self.file_stim:
            reader_io_stim = Reader(self.file_stim[key])
        os.chdir(pwd)
        #
        reader_io_nostim.chosenmodel = self.chosenmodel
        reader_io_stim.chosenmodel = self.chosenmodel
        #print self.chosenmodel.name
        # this tests extract_modelname_modelscale & load_model
        compare1 = [
            reader_io_nostim.modelname,  # output of 
            reader_io_nostim.modelscale
        ]  # extract_modelname_modelscale()
        compare2 = [
            reader_io_stim.chosenmodel.modelname,  # output of
            reader_io_stim.chosenmodel.modelscale
        ]  # load_model()
        self.assertEqual(compare1, compare2)
        reader_io_nostim.closefile()
        reader_io_stim.closefile()

    #@unittest.skip("reason for skipping")
    def test_2_visualizetable(self):
        # gives you session of the nwbfile
        os.chdir(rootwd)
        for key in self.file_nostim:
            reader_io_nostim = Reader(self.file_nostim[key])
        os.chdir(pwd)
        reader_io_nostim.session_info()  # pretty prints table
        reader_io_nostim.closefile()

    #@unittest.skip("reason for skipping")
    def test_3_pull_epochindices_chosenregion(self):
        os.chdir(rootwd)
        for key in self.file_stim:
            reader_io_stim = Reader(self.file_stim[key])
        os.chdir(pwd)
        epoch_indices_soma = reader_io_stim.pull_epochindices_chosenregion(
            'soma')
        epoch_indices_axon = reader_io_stim.pull_epochindices_chosenregion(
            'axon')
        #print epoch_indices_soma
        #print epoch_indices_axon
        self.assertNotEqual(epoch_indices_soma, epoch_indices_axon)
        reader_io_stim.closefile()
        #shutil.rmtree("responses")

    #@unittest.skip("reason for skipping")
    def test_4_pull_epochid(self):
        os.chdir(rootwd)
        for key in self.file_stim:
            reader_io_stim = Reader(self.file_stim[key])
        os.chdir(pwd)
        epoch_indices_soma = reader_io_stim.pull_epochindices_chosenregion(
            'soma')
        epoch_indices_axon = reader_io_stim.pull_epochindices_chosenregion(
            'axon')
        epoch_ids_soma = [
            reader_io_stim.pull_epochid(epoch_indices_soma[0]),
            reader_io_stim.pull_epochid(epoch_indices_soma[1]),
            reader_io_stim.pull_epochid(epoch_indices_soma[2]),
            reader_io_stim.pull_epochid(epoch_indices_soma[3])
        ]
        epoch_ids_axon = [
            reader_io_stim.pull_epochid(epoch_indices_axon[0]),
            reader_io_stim.pull_epochid(epoch_indices_axon[1]),
            reader_io_stim.pull_epochid(epoch_indices_axon[2]),
            reader_io_stim.pull_epochid(epoch_indices_axon[3])
        ]
        #print epoch_indices_soma
        #print epoch_indices_axon
        #print epoch_ids_soma
        #print epoch_ids_axon
        #print len(reader_io_stim.nwbfile.epochs.epochs.data)
        #print reader_io_stim.nwbfile.epochs.epochs.data[0]#[3][0]
        #print reader_io_stim.nwbfile.epochs.epochs.data[3]#[3][0]
        #print reader_io_stim.nwbfile.epochs.epochs.data[4]#[3][0]
        #print reader_io_stim.nwbfile.epochs.epochs.data[5]#[3][0]
        #print epoch_indices_soma, epoch_id_soma
        #print epoch_indices_soma, reader_io_stimulus.pull_epochid(epoch_indices_soma[1])
        #print epoch_indices_axon, epoch_id_axon
        #print reader_io_stimulus.nwbfile.epochs.epochs.data
        #print reader_io_stimulus.nwbfile.epochs.epochs.data[0]
        #print reader_io_stimulus.nwbfile.epochs.epochs.data[1][2]
        self.assertNotEqual(epoch_ids_soma, epoch_ids_axon)
        reader_io_stim.closefile()

    #@unittest.skip("reason for skipping")
    def test_5_drawout_orderedepochs(self):
        os.chdir(rootwd)
        for key in self.file_stim:
            reader_io_stim = Reader(self.file_stim[key])
        os.chdir(pwd)
        orderedepochs_soma = reader_io_stim.drawout_orderedepochs('soma')
        #print orderedepochs_soma
        #print len(orderedepochs_soma)
        #print orderedepochs_soma[0]          # first, i.e, epoch
        #print orderedepochs_soma[0][2]       # get root tuple
        #print orderedepochs_soma[0][2][3]    # get array
        #print orderedepochs_soma[0][2][3][0] # take out tuple from the array
        #print orderedepochs_soma[0][2][3][0][2] # take out timeseries object from tuple
        #print orderedepochs_soma[0][2][3][0][2].data.value
        #print type(orderedepochs_soma[0][2][3][0][2].data.value)
        #print orderedepochs_soma[0][2][3][0][2].timestamps.value
        #print len([ orderedepochs_soma[0][2][3][0][2].timestamps.value[i]
        #          for i in numpy.arange(orderedepochs_soma[0][2][3][0][0],
        #                                orderedepochs_soma[0][2][3][0][1]) ])
        #print orderedepochs_soma[0][1]
        #print orderedepochs_soma[0][0]
        #print reader_io_stim.nwbfile.epochs.epochs.data[orderedepochs_soma[0][0]]
        #to compare
        compare1 = [orderedepochs_soma[0][1], orderedepochs_soma[1][1]]
        self.assertEqual(compare1, [0, 1])
        reader_io_stim.closefile()

    #@unittest.skip("reason for skipping")
    def test_6_get_tuple_for_epoch(self):
        # To get an epoch
        os.chdir(rootwd)
        for key in self.file_stim:
            reader_io_stim = Reader(self.file_stim[key])
        os.chdir(pwd)
        orderedepochs_soma = reader_io_stim.drawout_orderedepochs(
            'soma')  # drawout orderedepochs
        #epochs = []
        #for i in range(len(orderedepochs_soma)):
        #    epoch_id = orderedepochs_soma[i][1]
        #    epochs.append( Reader.get_array_for_epoch(epoch_id, orderedepochs_soma) ) # get epoch
        #print epochs
        #print epochs[0]
        #print epochs[1]
        epoch_id = 0
        epochtuple = Reader.get_tuple_for_epoch(epoch_id, orderedepochs_soma)
        #print epochtuple
        #print epochtuple[2].timestamps_unit #description/unit/time_unit/timestamps_unit
        self.assertNotEqual(
            epochtuple[0],  # tstart index
            epochtuple[1])  # counts
        #self.assertNotEqual( epochs[0], epochs[1] )
        reader_io_stim.closefile()

    #@unittest.skip("reason for skipping")
    def test_7_get_timestamps_for_epoch(self):
        os.chdir(rootwd)
        for key in self.file_nostim:
            reader_io_nostim = Reader(self.file_nostim[key])
        os.chdir(pwd)
        orderedepochs_soma = reader_io_nostim.drawout_orderedepochs(
            'soma')  #drawout orderedepoch
        orderedepochs_axon = reader_io_nostim.drawout_orderedepochs('axon')
        epoch_id = 0
        times_soma = reader_io_nostim.get_timestamps_for_epoch(
            epoch_id, orderedepochs_soma)
        times_axon = reader_io_nostim.get_timestamps_for_epoch(
            epoch_id, orderedepochs_axon)
        #
        self.assertEqual([len(times_soma), times_soma],
                         [len(times_axon), times_axon])
        reader_io_nostim.closefile()

    #@unittest.skip("reason for skipping")
    def test_8_get_datavalues_for_epoch(self):
        os.chdir(rootwd)
        for key in self.file_stim:
            reader_io_stim = Reader(self.file_stim[key])
        os.chdir(pwd)
        orderedepochs_soma = reader_io_stim.drawout_orderedepochs(
            'soma')  #drawout orderedepoch
        orderedepochs_axon = reader_io_stim.drawout_orderedepochs('axon')
        epoch_id = 1
        data_soma = reader_io_stim.get_datavalues_for_epoch(
            epoch_id, orderedepochs_soma)
        data_axon = reader_io_stim.get_datavalues_for_epoch(
            epoch_id, orderedepochs_axon)
        #
        self.assertNotEqual(data_soma, data_axon)
        reader_io_stim.closefile()

    #@unittest.skip("reason for skipping")
    def test_9_get_stimulus(self):
        os.chdir(rootwd)
        for key in self.file_stim:
            reader_io_stim = Reader(self.file_stim[key])
        os.chdir(pwd)
        #print help(reader_io_stim.nwbfile)
        #print reader_io_stim.nwbfile.get_stimulus["DummyTest_stimulus"]
        #print help(reader_io_stim.nwbfile)
        #print reader_io_stim.nwbfile.acquisition
        #print reader_io_stim.nwbfile.stimulus
        #print reader_io_stim.nwbfile
        #
        #print reader_io_stim.nwbfile.stimulus
        #print reader_io_stim.nwbfile.stimulus["DummyTest_stimulus"]
        #print reader_io_stim.nwbfile.stimulus["DummyTest_stimulus"].data
        #print reader_io_stim.nwbfile.stimulus["DummyTest_stimulus"].data.value
        reader_io_stim.chosenmodel = self.chosenmodel
        stimulus = reader_io_stim.get_stimulus()
        #print stimulus
        #print stimulus.data
        #print stimulus.timestamps
        a = all(
            boolean == True
            for boolean in stimulus.data.value == self.stimulus_for_comparison)
        self.assertTrue(a is True)
        reader_io_stim.closefile()

    #@unittest.skip("reason for skipping")
    def test_x_get_stimulus(self):
        os.chdir(rootwd)
        for key in self.file_nostim:
            reader_io_nostim = Reader(self.file_nostim[key])
        for key in self.file_stim:
            reader_io_stim = Reader(self.file_stim[key])
        os.chdir(pwd)
        reader_io_stim.chosenmodel = self.chosenmodel
        stimulus = reader_io_stim.get_stimulus()
        timestamps = reader_io_nostim.get_timestamps()
        #print stimulus
        #print stimulus.data
        #print stimulus.timestamps.value
        #print timestamps.value
        #print help(timestamps)
        a = all(boolean == True
                for boolean in stimulus.timestamps.value == timestamps.value)
        self.assertTrue(a is True)
        reader_io_stim.closefile()

    #@unittest.skip("reason for skipping")
    def test_xi_get_description_epoch(self):
        #print "test XI"
        os.chdir(rootwd)
        for key in self.file_stim:
            reader_io_stim = Reader(self.file_stim[key])
        os.chdir(pwd)
        orderedepochs_soma = reader_io_stim.drawout_orderedepochs(
            'soma')  #drawout ordered epoch
        descrip_0soma = Reader.get_description_epoch(
            0, orderedepochs_soma)  # get epoch
        descrip_1soma = Reader.get_description_epoch(1, orderedepochs_soma)
        #
        #print descrip_0soma
        #print descrip_1soma
        self.assertNotEqual(descrip_0soma, descrip_1soma)
        reader_io_stim.closefile()
        shutil.rmtree("responses")
예제 #8
0
class ExecutiveControlTest(unittest.TestCase):
    def setUp(self):
        self.ec = ExecutiveControl(
        )  #instance for non: static & class methods.
        self.pwd = os.getcwd()

    #@unittest.skip("reason for skipping")
    def test_1_list_modelscales(self):
        x = len(self.ec.list_modelscales()) != 0
        self.assertEqual(x, True)

    #@unittest.skip("reason for skipping")
    def test_2_list_models(self):
        dummyscale_path = self.pwd + os.sep + "models" + os.sep + "dummyscale"
        for i in range(3):  # create three dummymodels
            os.makedirs(dummyscale_path + os.sep + "dummymodel" + str(i + 1))
        self.assertEqual(len(self.ec.list_models(modelscale="dummyscale")), 3)
        shutil.rmtree(dummyscale_path)

    #@unittest.skip("reason for skipping")
    def test_3_choose_model(self):
        # NOTE: this only works if the 'minimal' script
        # ~/models/cells/model2015Masoli.py exists
        # 'minimal' => a class with __init__ method with
        # self.modelname = "PC2015Masoli.py"
        x = self.ec.choose_model(modelscale="cells",
                                 modelname="PC2015Masoli")  #"DummyTest"
        self.assertEqual(x.modelname, "PC2015Masoli")

    #@unittest.skip("reason for skipping")
    def test_4_launch_model_NEURON_nostimulus_with_capability(self):
        pickedmodel = self.ec.choose_model(modelscale="cells",
                                           modelname="DummyTest")
        parameters = {"dt": 0.01, "celsius": 30, "tstop": 100, "v_init": 65}
        self.assertEqual(
            self.ec.launch_model(parameters=parameters,
                                 onmodel=pickedmodel,
                                 capabilities={
                                     'model': None,
                                     'vtest': None
                                 }), pickedmodel)

    @unittest.skip("reason for skipping")
    def test_5_launch_model_NEURON_nostimulus_raw(self):
        pickedmodel = self.ec.choose_model(modelscale="cells",
                                           modelname="PC2015Masoli")
        parameters = {"dt": 0.1, "celsius": 30, "tstop": 10, "v_init": 65}
        self.assertEqual(
            self.ec.launch_model(parameters=parameters, onmodel=pickedmodel),
            pickedmodel)

    @unittest.skip("reason for skipping")
    def test_6_save_response(self):
        pickedmodel = self.ec.choose_model(modelscale="cells",
                                           modelname="PC2015Masoli")
        parameters = {"dt": 0.1, "celsius": 30, "tstop": 10, "v_init": 65}
        stimparameters = {
            "type": ["current", "IClamp"],
            "stimlist": [{
                "amp": 0.5,
                "dur": 5.0,
                "delay": 1.0
            }, {
                "amp": 1.0,
                "dur": 5.0,
                "delay": 0.0 + 5.0
            }],
            "tstop":
            parameters["tstop"]
        }
        model = self.ec.launch_model(parameters=parameters,
                                     onmodel=pickedmodel,
                                     stimparameters=stimparameters,
                                     stimloc="soma")
        fullfilename = self.ec.save_response()
        #
        sesstime = str(self.ec.tm.nwbfile.session_start_time).replace(
            " ", "_")[0:-6]
        filename_shouldbe = self.ec.tm.nwbfile.session_id + "_" + sesstime.replace(
            ":", "-") + ".h5"
        #
        path = os.getcwd(
        ) + os.sep + "responses" + os.sep + model.modelscale + os.sep + model.modelname
        shutil.rmtree(path)
        #
        fullname_shouldbe = path + os.sep + filename_shouldbe
        #
        self.assertEqual(fullfilename, fullname_shouldbe)