Exemple #1
0
    def test_cython_mapping(self):

        set_up_parameters = {
            'sigma_G': 0.0,  # Below electrode
            'sigma_T': 0.3,  # Tissue
            'sigma_S': 0.3,  # Saline
            'slice_thickness': 200.,
            'steps': 20
        }

        xstart = np.array([100., 0, -100., -110])
        xend = np.array([100., 0, -100., -110]) + 100.

        ystart = np.array([100., 0, -100, 200.])
        yend = np.array([100., 0, -100, 200]) + 100.

        zstart = np.array([10, 0, -10, -50.])
        zend = np.array([10, 0, -10, -50]) + 10.

        xmid = xstart
        ymid = ystart
        zmid = zstart

        elec_x = (np.arange(3) - 1) * 50.
        elec_y = (np.arange(3) - 1) * 50.

        #elec_x = np.array([0.])
        #elec_y = np.array([0.])

        elec_z = -set_up_parameters['slice_thickness'] / 2.
        elec_r = 1
        n_avrg_points = 100

        ext_sim_dict = {
            'elec_x': elec_x,
            'elec_y': elec_y,
            'elec_z': elec_z,
            'include_elec': False,
            'use_line_source': False,
            'moi_steps': set_up_parameters['steps'],
            'n_avrg_points': n_avrg_points,
            'elec_radius': elec_r
        }
        moi = MoI(set_up_parameters=set_up_parameters)
        mapping = moi.make_mapping_standalone(ext_sim_dict,
                                              xstart=xstart,
                                              ystart=ystart,
                                              zstart=zstart,
                                              xend=xend,
                                              yend=yend,
                                              zend=zend,
                                              xmid=xmid,
                                              ymid=ymid,
                                              zmid=zmid)

        mapping2 = moi.make_mapping_cython(ext_sim_dict,
                                           xstart=xstart,
                                           ystart=ystart,
                                           zstart=zstart,
                                           xend=xend,
                                           yend=yend,
                                           zend=zend,
                                           xmid=xmid,
                                           ymid=ymid,
                                           zmid=zmid)

        error = np.abs((mapping - mapping2) / mapping2)

        self.assertLessEqual(np.max(error), 0.001)
Exemple #2
0
    def test_cython_mapping(self):

        set_up_parameters = {
            "sigma_G": 0.0,  # Below electrode
            "sigma_T": 0.3,  # Tissue
            "sigma_S": 0.3,  # Saline
            "slice_thickness": 200.0,
            "steps": 20,
        }

        xstart = np.array([100.0, 0, -100.0, -110])
        xend = np.array([100.0, 0, -100.0, -110]) + 100.0

        ystart = np.array([100.0, 0, -100, 200.0])
        yend = np.array([100.0, 0, -100, 200]) + 100.0

        zstart = np.array([10, 0, -10, -50.0])
        zend = np.array([10, 0, -10, -50]) + 10.0

        xmid = xstart
        ymid = ystart
        zmid = zstart

        elec_x = (np.arange(3) - 1) * 50.0
        elec_y = (np.arange(3) - 1) * 50.0

        # elec_x = np.array([0.])
        # elec_y = np.array([0.])

        elec_z = -set_up_parameters["slice_thickness"] / 2.0
        elec_r = 1
        n_avrg_points = 100

        ext_sim_dict = {
            "elec_x": elec_x,
            "elec_y": elec_y,
            "elec_z": elec_z,
            "include_elec": False,
            "use_line_source": False,
            "moi_steps": set_up_parameters["steps"],
            "n_avrg_points": n_avrg_points,
            "elec_radius": elec_r,
        }
        moi = MoI(set_up_parameters=set_up_parameters)
        mapping = moi.make_mapping_standalone(
            ext_sim_dict,
            xstart=xstart,
            ystart=ystart,
            zstart=zstart,
            xend=xend,
            yend=yend,
            zend=zend,
            xmid=xmid,
            ymid=ymid,
            zmid=zmid,
        )

        mapping2 = moi.make_mapping_cython(
            ext_sim_dict,
            xstart=xstart,
            ystart=ystart,
            zstart=zstart,
            xend=xend,
            yend=yend,
            zend=zend,
            xmid=xmid,
            ymid=ymid,
            zmid=zmid,
        )

        error = np.abs((mapping - mapping2) / mapping2)

        self.assertLessEqual(np.max(error), 0.001)
Exemple #3
0
    def compute_electrodecoeffs(self):
        '''
        compute for each cell their corresponding mapping of compartment
        membrane currents to each electrode contact point
        '''
        coefffile = os.path.join(self.savefolder, 'electrodecoeffs.h5')
        
        moi = MoI(self.paramsMoI)

        coeffs = []

        if os.path.isfile(coefffile):
            if MASTER_MODE:
                print 'loading electrode mappings from file %s' % coefffile                
                f = h5py.File(coefffile, 'r')
                allcoeffs = {}
                for cellindex in range(self.POPULATION_SIZE):
                    allcoeffs.update({
                        cellindex : f['cell%.3i' % cellindex].value
                        })
                f.close()
            else:
                allcoeffs = None
            return COMM.bcast(allcoeffs, root=0)
        else:       
            for cellindex in range(self.POPULATION_SIZE):
                if divmod(cellindex, SIZE)[1] == RANK:
                    cell = self.cellsim(cellindex, return_just_cell=True)
    
                    mapping = moi.make_mapping_cython(self.paramsMapping,
                                                    xmid=cell.xmid,
                                                    ymid=cell.ymid,
                                                    zmid=cell.zmid,
                                                    xstart=cell.xstart,
                                                    ystart=cell.ystart,
                                                    zstart=cell.zstart,
                                                    xend=cell.xend,
                                                    yend=cell.yend,
                                                    zend=cell.zend,
                                                    morphology=cell.morphology)
                    coeffs.append({cellindex : mapping})
            
            #sync MPI threads, communicate coefficients between ranks
            COMM.Barrier()
            coeffs = COMM.gather(coeffs, root=0)
            if RANK == 0:
                allcoeffs = {}
                for i in range(SIZE):
                    for item in coeffs[i]:
                        allcoeffs.update(item)
                
                #write mappings to HDF5
                print 'writing electrode mappings to file %s' % coefffile
                f = h5py.File(coefffile, 'a', compression='gzip')
                for cellindex in range(self.POPULATION_SIZE):
                    f['cell%.3i' % cellindex] = allcoeffs[cellindex]
                f.close()            
            else:
                allcoeffs = None
            
            return COMM.bcast(allcoeffs, root=0)