Esempio n. 1
0
  def setup(self,simName,expType,nMSD1=120,nMSD2=120,nFS=20,nLTS=0,nChIN=0):

    from snudda.init.init import SnuddaInit

    configName= simName + "/network-config.json"
    cnc = SnuddaInit(struct_def={}, config_file=configName, nChannels=1)
    cnc.define_striatum(num_dSPN=nMSD1, num_iSPN=nMSD2, num_FS=nFS, num_LTS=nLTS, num_ChIN=nChIN,
                        volume_type="slice", side_len=200e-6, slice_depth=150e-6)

    dirName = os.path.dirname(configName)
  
    if not os.path.exists(dirName):
      os.makedirs(dirName)

    cnc.write_json(configName)

    
    print("\n\npython3 snudda.py place " + str(simName))
    print("python3 snudda.py detect " + str(simName))
    print("python3 snudda.py prune " + str(simName))
    print("python3 snudda_cut.py " + str(simName) \
          + '/network-synapses.hdf5 "abs(z)<100e-6"')

    print("\nThe last command will pop up a figure and enter debug mode, press ctrl+D in the terminal window after inspecting the plot to continue")

    print("\n!!! Remember to compile the mod files: nrnivmodl data/neurons/mechanisms")

    print("\nTo run for example dSPN -> iSPN (and dSPN->dSPN) calibration:")
    print("mpiexec -n 12 -map-by socket:OVERSUBSCRIBE python3 snudda_calibrate_synapses.py run " + str(expType) + " " + str(simName) + "/network-cut-slice.hdf5 dSPN iSPN")

    print("\npython3 snudda_calibrate_synapses.py analyse " + str(expType) + " " + str(simName) + "/network-cut-slice.hdf5 --pre dSPN --post iSPN\npython3 snudda_calibrate_synapses.py analyse " + str(simName) + "/network-cut-slice.hdf5 --pre iSPN --post dSPN")
Esempio n. 2
0
    def test_population_units(self, stage="place-pop-unit-random"):

        network_path = os.path.join(os.path.dirname(__file__), "networks",
                                    "network_place_pop_unit_random")
        cnc = SnuddaInit(struct_def={}, network_path=network_path)
        cnc.define_striatum(num_dSPN=1000,
                            num_iSPN=1000,
                            num_FS=20,
                            num_LTS=0,
                            num_ChIN=0,
                            volume_type="cube")
        cnc.add_population_unit_random(structure_name="Striatum",
                                       neuron_types=["dSPN", "iSPN"],
                                       fraction_of_neurons=0.5)
        cnc.add_population_unit_random(structure_name="Striatum",
                                       neuron_types=["dSPN", "iSPN"],
                                       fraction_of_neurons=0.2)
        cnc.add_population_unit_random(structure_name="Striatum",
                                       neuron_types=["dSPN"],
                                       fraction_of_neurons=0.3)
        cnc.add_population_unit_random(structure_name="Striatum",
                                       neuron_types=["iSPN"],
                                       fraction_of_neurons=0.15)
        cnc.add_population_unit_random(structure_name="Striatum",
                                       neuron_types=["iSPN"],
                                       fraction_of_neurons=0.15,
                                       unit_id=10)
        cnc.write_json()

        npn = SnuddaPlace(network_path=network_path,
                          h5libver="latest",
                          verbose=True)
        npn.parse_config()
        npn.write_data()
Esempio n. 3
0
    def setUp(self):

        os.chdir(os.path.dirname(__file__))

        self.network_path = os.path.join("networks", "network_testing_input")
        self.config_file = os.path.join(self.network_path,
                                        "network-config.json")
        self.position_file = os.path.join(self.network_path,
                                          "network-neuron-positions.hdf5")
        self.save_file = os.path.join(self.network_path, "voxels",
                                      "network-putative-synapses.hdf5")

        # Setup network so we can test input generation
        from snudda.init.init import SnuddaInit
        cell_spec = os.path.join(os.path.dirname(__file__), "validation")
        cnc = SnuddaInit(struct_def={},
                         config_file=self.config_file,
                         random_seed=1234)
        cnc.define_striatum(num_dSPN=5,
                            num_iSPN=0,
                            num_FS=5,
                            num_LTS=0,
                            num_ChIN=0,
                            volume_type="cube",
                            neurons_dir=cell_spec)
        cnc.write_json(self.config_file)

        # Place neurons
        from snudda.place.place import SnuddaPlace
        npn = SnuddaPlace(
            config_file=self.config_file,
            log_file=None,
            verbose=True,
            d_view=
            None,  # TODO: If d_view is None code run sin serial, add test parallel
            h5libver="latest")
        npn.parse_config()
        npn.write_data(self.position_file)

        # Detect
        self.sd = SnuddaDetect(config_file=self.config_file,
                               position_file=self.position_file,
                               save_file=self.save_file,
                               rc=None,
                               hyper_voxel_size=120,
                               verbose=True)

        self.sd.detect(restart_detection_flag=True)

        # Prune
        self.network_file = os.path.join(self.network_path,
                                         "network-synapses.hdf5")

        sp = SnuddaPrune(network_path=self.network_path,
                         config_file=None)  # Use default config file
        sp.prune(pre_merge_only=False)
Esempio n. 4
0
    def setUp(self):

        # We want to setup a volume with density variations
        if os.path.dirname(__file__):
            os.chdir(os.path.dirname(__file__))
        print(
            f"Current directory (detect): {os.path.dirname(os.path.realpath(__file__))}"
        )

        neuron_dir = os.path.join(os.path.dirname(__file__), "validation")
        self.network_path = os.path.join("networks", "network_density")
        self.config_file = os.path.join(self.network_path,
                                        "network-config.json")
        cnc = SnuddaInit(struct_def={},
                         config_file=self.config_file,
                         random_seed=1234)

        mesh_file = os.path.join(self.network_path, "mesh", "slice.obj")

        cnc.define_striatum(num_dSPN=0,
                            num_iSPN=0,
                            num_FS=2000,
                            num_LTS=0,
                            num_ChIN=0,
                            mesh_file=mesh_file,
                            mesh_bin_width=5e-4,
                            neurons_dir=neuron_dir)

        create_slice_mesh(file_name=mesh_file,
                          centre_point=[1e-3, 1e-3, 1e-3],
                          x_len=2e-3,
                          y_len=2e-3,
                          z_len=2e-3,
                          description="Test slice")

        # Linear density = x coordinate, obs we give a relative density profile
        # (real density is scaled based on number of neurons)
        density_function = "abs(x)"
        cnc.add_neuron_density("Striatum",
                               "FSN",
                               density_func=density_function)

        cnc.write_json(self.config_file)

        self.position_file = os.path.join(self.network_path,
                                          "network-neuron-positions.hdf5")

        npn = SnuddaPlace(config_file=self.config_file,
                          log_file=None,
                          verbose=True,
                          d_view=None,
                          h5libver="latest")

        npn.parse_config()
        npn.write_data(self.position_file)
Esempio n. 5
0
    def setUp(self):
        if os.path.dirname(__file__):
            os.chdir(os.path.dirname(__file__))
        print(
            f"Current directory (detect): {os.path.dirname(os.path.realpath(__file__))}"
        )

        neuron_dir = os.path.join(os.path.dirname(__file__), "validation")
        self.sim_name = os.path.join("networks", "network_testing_place")
        self.config_file = os.path.join(self.sim_name, "network-config.json")
        cnc = SnuddaInit(struct_def={},
                         config_file=self.config_file,
                         random_seed=1234)
        cnc.define_striatum(num_dSPN=10,
                            num_iSPN=0,
                            num_FS=10,
                            num_LTS=0,
                            num_ChIN=0,
                            volume_type="cube",
                            neurons_dir=neuron_dir)
        cnc.write_json(self.config_file)
Esempio n. 6
0
    def test_workflow(self):

        #with self.subTest(stage="create"):
        #    run_cli_command("create test-project --overwrite")

        if True:
            network_path = "test-project"
            if os.path.exists(network_path):
                import shutil
                shutil.rmtree(network_path)

                os.mkdir(network_path)
                os.chdir(network_path)

        with self.subTest(stage="setup-parallel"):
            os.environ["IPYTHONDIR"] = os.path.join(
                os.path.abspath(os.getcwd()), ".ipython")
            os.environ["IPYTHON_PROFILE"] = "Snudda_local"
            os.system(
                "ipcluster start -n 4 --profile=$IPYTHON_PROFILE --ip=127.0.0.1&"
            )
            time.sleep(10)

        # with self.subTest(stage="init-parallel-BIG"):
        #     run_cli_command("init tiny_parallel --size 1000000 --overwrite")

        # with self.subTest(stage="place-parallel-BIG"):
        #     run_cli_command("place tiny_parallel --parallel")

        with self.subTest(stage="init-parallel"):
            run_cli_command("init tiny_parallel --size 100 --overwrite")

        # Lets reinit but a smaller network that contains all types of cells, to speed things up
        with self.subTest(stage="small-reinit-1"):
            config_name = os.path.join("tiny_parallel", "network-config.json")
            cnc = SnuddaInit(struct_def={},
                             config_file=config_name,
                             random_seed=123456)
            cnc.define_striatum(num_dSPN=4,
                                num_iSPN=4,
                                num_FS=2,
                                num_LTS=2,
                                num_ChIN=2,
                                volume_type="cube")
            cnc.write_json(config_name)

        with self.subTest(stage="place-parallel"):
            run_cli_command("place tiny_parallel --parallel --raytraceBorders")

        with self.subTest(stage="detect-parallel"):
            run_cli_command("detect tiny_parallel --parallel")

        with self.subTest(stage="prune-parallel"):
            run_cli_command("prune tiny_parallel --parallel")

        from shutil import copyfile
        print(f"listdir: {os.listdir()}")
        print(f"parent listdir: {os.listdir('..')}")
        input_file = os.path.join(os.path.dirname(__file__), os.path.pardir,
                                  "snudda", "data", "input_config",
                                  "input-v10-scaled.json")
        copyfile(input_file, os.path.join("tiny_parallel", "input.json"))

        with self.subTest(stage="input"):
            run_cli_command(
                "input tiny_parallel --input tiny_parallel/input.json --parallel"
            )

        # with self.subTest(stage="init-parallel-full"):
        #     run_cli_command("init large_parallel --size 1670000 --overwrite")

        # with self.subTest(stage="place-parallel-full"):
        #    run_cli_command("place large_parallel --parallel")

        with self.subTest(stage="parallel-stop"):
            os.system("ipcluster stop")

        #  Only serial tests below this line, we stopped ipcluster.

        with self.subTest(stage="simulate"):
            print("Running nrnivmodl:")
            mech_dir = os.path.join(os.path.dirname(__file__), os.path.pardir,
                                    "snudda", "data", "neurons", "mechanisms")

            if not os.path.exists("mechanisms"):
                print("----> Copying mechanisms")
                # os.symlink(mech_dir, "mechanisms")
                from distutils.dir_util import copy_tree
                copy_tree(mech_dir, "mechanisms")
            else:
                print("------------->   !!! mechanisms already exists")

            eval_str = f"nrnivmodl mechanisms"  # f"nrnivmodl {mech_dir}
            print(f"Running: {eval_str}")
            os.system(eval_str)

            # print("---> Testing to run simulate using os.system instead")
            # os.system("snudda simulate tiny_parallel --time 0.1 --voltOut default")

            # For the unittest we for some reason need to load mechansism
            # separately
            from mpi4py import MPI  # This must be imported before neuron, to run parallel
            from neuron import h  # , gui
            import neuron

            # For some reason we need to import modules manually
            # when running the unit test.
            if os.path.exists("x86_64/.libs/libnrnmech.so"):
                print("!!! Manually loading libraries")
                try:
                    h.nrn_load_dll("x86_64/.libs/libnrnmech.so")
                except:
                    import traceback
                    tstr = traceback.format_exc()
                    print(tstr)

            if False:
                try:
                    from snudda.simulate.simulate import SnuddaSimulate
                    ss = SnuddaSimulate(network_path="tiny_parallel")
                    ss.run(100)
                    ss.write_spikes()
                except:
                    import traceback
                    tstr = traceback.format_exc()
                    print(tstr)
                    import pdb
                    pdb.set_trace()

            print("Time to run simulation...")
            run_cli_command(
                "simulate tiny_parallel --time 0.1 --voltOut default")

        os.environ["SLURM_JOBID"] = "1234"

        with self.subTest(stage="init-serial"):
            # Remove the old folder if it exists
            if os.path.exists("tiny_serial"):
                import shutil
                shutil.rmtree("tiny_serial")

            run_cli_command("init tiny_serial --size 100 --profile")

        with self.subTest(stage="init-overwrite-fail"):
            # Should not allow overwriting of existing folder if --overwrite is not specified
            self.assertRaises(AssertionError, run_cli_command,
                              "init tiny_serial --size 100")

        # Again, let us reinit to a smaller network to speed things up
        with self.subTest(stage="small-reinit-2"):
            config_name = os.path.join("tiny_serial", "network-config.json")
            cnc = SnuddaInit(struct_def={},
                             config_file=config_name,
                             random_seed=1234)
            cnc.define_striatum(num_dSPN=3,
                                num_iSPN=3,
                                num_FS=2,
                                num_LTS=2,
                                num_ChIN=2,
                                volume_type="cube")
            cnc.write_json(config_name)

        with self.subTest(stage="place-serial"):
            run_cli_command("place tiny_serial --h5legacy")

        with self.subTest(stage="detect-serial"):
            run_cli_command(
                "detect tiny_serial --volumeID Striatum --hvsize 120 --randomseed 123 --verbose --h5legacy"
            )

        with self.subTest(stage="detect-serial-cont"):
            run_cli_command(
                "detect tiny_serial --volumeID Striatum --hvsize 120 --cont --h5legacy"
            )

        with self.subTest(stage="prune-serial"):
            run_cli_command("prune tiny_serial --h5legacy")

        input_file = os.path.join(os.path.dirname(__file__), os.path.pardir,
                                  "snudda", "data", "input_config",
                                  "input-v10-scaled.json")
        copyfile(input_file, "tiny_serial/input.json")
        with self.subTest(stage="input"):
            run_cli_command(
                "input tiny_serial --time 1.0 --inputFile tiny_serial/input-spikes.hdf5"
            )
Esempio n. 7
0
    def test_init(self):

        with self.subTest(stage="init_cube"):

            network_path = os.path.join(os.path.dirname(__file__), "networks",
                                        "network_testing_init_cube")

            config_name = os.path.join(network_path, "network-config.json")
            cnc = SnuddaInit(struct_def={}, config_file=config_name)
            cnc.define_striatum(num_dSPN=47500,
                                num_iSPN=47500,
                                num_FS=1300,
                                num_LTS=0,
                                num_ChIN=0,
                                volume_type="cube")
            cnc.write_json()

            # TODO: This only checks that the code runs. Add check for output

        with self.subTest(stage="init_slice"):
            network_path = os.path.join(os.path.dirname(__file__), "networks",
                                        "network_testing_init_slice")

            config_name = os.path.join(network_path)
            cnc = SnuddaInit(struct_def={}, network_path=network_path)
            cnc.define_striatum(num_dSPN=47500,
                                num_iSPN=47500,
                                num_FS=1300,
                                num_LTS=0,
                                num_ChIN=0,
                                volume_type="slice")
            cnc.write_json()

        with self.subTest(stage="init_full"):
            network_path = os.path.join(os.path.dirname(__file__), "networks",
                                        "network_testing_init_full")

            config_name = os.path.join(network_path, "network-config.json")
            cnc = SnuddaInit(struct_def={},
                             config_file=config_name,
                             random_seed=123)
            cnc.define_striatum(num_neurons=1670000)
            cnc.write_json()

        with self.subTest(stage="population-unit-random"):
            network_path = os.path.join(os.path.dirname(__file__), "networks",
                                        "network_pop_unit_random")
            cnc = SnuddaInit(struct_def={}, network_path=network_path)
            cnc.define_striatum(num_dSPN=1000,
                                num_iSPN=1000,
                                num_FS=20,
                                num_LTS=0,
                                num_ChIN=0,
                                volume_type="slice")
            cnc.add_population_unit_random(structure_name="Striatum",
                                           neuron_types=["dSPN", "iSPN"],
                                           fraction_of_neurons=0.5)
            cnc.add_population_unit_random(structure_name="Striatum",
                                           neuron_types=["dSPN", "iSPN"],
                                           fraction_of_neurons=0.2)
            cnc.add_population_unit_random(structure_name="Striatum",
                                           neuron_types=["dSPN"],
                                           fraction_of_neurons=0.3)
            cnc.add_population_unit_random(structure_name="Striatum",
                                           neuron_types=["iSPN"],
                                           fraction_of_neurons=0.15)
            cnc.add_population_unit_random(structure_name="Striatum",
                                           neuron_types=["iSPN"],
                                           fraction_of_neurons=0.15,
                                           unit_id=10)
            cnc.write_json()

        with self.subTest(stage="population-unit-sphere"):
            network_path = os.path.join(os.path.dirname(__file__), "networks",
                                        "network_pop_unit_sphere")
            cnc = SnuddaInit(struct_def={}, network_path=network_path)
            cnc.define_striatum(num_dSPN=10000,
                                num_iSPN=10000,
                                num_FS=200,
                                num_LTS=0,
                                num_ChIN=0,
                                volume_type="slice")

            cnc.add_population_unit_density(
                structure_name="Striatum",
                neuron_types=["dSPN", "iSPN"],
                unit_centre=[0, 0, 0],
                probability_function="(d < 100e-6)*1")

            cnc.add_population_unit_density(
                structure_name="Striatum",
                neuron_types=["dSPN", "iSPN"],
                unit_centre=[300e-6, 0, 0],
                probability_function="exp(-d/200e-6)")

            cnc.add_population_unit_density(
                structure_name="Striatum",
                neuron_types=["dSPN"],
                unit_centre=[0, 300e-6, 0],
                probability_function="exp(-d/150e-6)")

            cnc.write_json()
Esempio n. 8
0
    def defineNetwork(self, simName, simType=None):

        if (simType is None):
            simType = self.simType

        configName = simName + "/network-config.json"
        cnc = SnuddaInit(struct_def={}, config_file=configName, nChannels=1)

        # In a 1x1x0.15 mm slice there are 12000 neurons normally
        # We want 10% of MS population only, since those are the ones being
        # stimulated (Chuhma et al 2011)
        #
        # 47.5% dSPN normally, now 4.75% of normal density = 570 dSPN, 570 iSPN
        # We assume we measure only monosynaptic connections.
        #
        # Adding 10 FSN, 10 ChIN, 10 dSPN, 10 iSPN to measure from
        #

        if (False):
            #Small debug version
            #cnc.defineStriatum(nMSD1=20,nMSD2=20,nFS=0,nLTS=0,nChIN=0,
            #                   volumeType="slice",sideLen=200e-6)
            #cnc.defineStriatum(nMSD1=20,nMSD2=20,nFS=10,nLTS=0,nChIN=10,
            #                   volumeType="slice",sideLen=200e-6)
            cnc.define_striatum(num_dSPN=153,
                                num_iSPN=153,
                                num_FS=10,
                                num_LTS=0,
                                num_ChIN=10,
                                volume_type="slice",
                                side_len=500e-6)

        if (simType == "Chuhma2011"):
            cnc.define_striatum(
                num_dSPN=1140 + self.nNrns,
                num_iSPN=1140 + self.nNrns,
                num_FS=5,
                num_LTS=0,
                num_ChIN=self.nNrns,
                volume_type="slice",
                side_len=1000e-6,
                slice_depth=300e-6)  # 400mum, assume 100 mum dead

        elif (simType == "Straub2016FS"):
            # nFS must be correct density, but readout neurons can be any density
            cnc.define_striatum(
                num_dSPN=self.nNrns,
                num_iSPN=self.nNrns,
                num_FS=182,
                num_LTS=0,
                num_ChIN=self.nNrns,
                volume_type="slice",
                side_len=1000e-6,
                slice_depth=175e-6)  #275e-6 m slice, assume 100e-6 dead

        elif (simType == "Straub2016LTS"):
            cnc.define_striatum(num_dSPN=self.nNrns,
                                num_iSPN=self.nNrns,
                                num_FS=0,
                                num_LTS=98,
                                num_ChIN=self.nNrns,
                                volume_type="slice",
                                side_len=1000e-6,
                                slice_depth=175e-6)
        elif (simType == "Szydlowski2013"):
            cnc.define_striatum(num_dSPN=0,
                                num_iSPN=0,
                                num_FS=156,
                                num_LTS=self.nNrns,
                                num_ChIN=0,
                                volume_type="slice",
                                side_len=1000e-6,
                                slice_depth=150e-6)
        else:
            print("setup : Unkown simType: " + str(simType))
            exit(-1)

        dirName = os.path.dirname(configName)

        if not os.path.exists(dirName):
            os.makedirs(dirName)

        cnc.write_json(configName)
Esempio n. 9
0
  #simName = "networks/FSmorphTest2orig"
  #simName = "networks/FSmorphTest1b"
  #simName = "LTStest"
  #simName = "networks/twoFS"
  #simName = "networks/FSmorphTest4"
  #simName = "networks/3types-striatum"
  # simName = "networks/SynTest-v6" # MSMS tuning
  #simName = "networks/SynTest-v15"  

  neuronsDir = "cellspecs.parkinson/" + str(args.level) + "/"
  
  configName= simName + "/network-config.json"
  cnc = SnuddaInit(struct_def={}, config_file=configName, nChannels=1)
  cnc.define_striatum(num_dSPN=1500, num_iSPN=1500, num_FS=0, num_LTS=0, num_ChIN=0,
                      neurons_dir=neuronsDir,
                      volume_type="cube")

  # cnc.defineStriatum(nMSD1=0,nMSD2=0,nFS=100,nLTS=100,nChIN=0,volumeType="slice")



  dirName = os.path.dirname(configName)
  
  if not os.path.exists(dirName):
    os.makedirs(dirName)

  cnc.write_json(configName)

  if(not connectNeurons):
    print("Removing all target information, and rewriting config file")