コード例 #1
0
    def __calculate_calibration(self,file_name):

        calibration_samples = read_and_transform.extract_calibration_samples(self.normalized_points)
        self.calibration_points = {}
        point_function = read_and_transform.get_pointer_transform_function(self.mid_date)
        for label, index in read_and_transform.CALIBRATION_LABELS.iteritems():
            raw_point = calibration_samples[index]
            tip = point_function(raw_point)
            self.calibration_points[label] = tip

        #Get file name
        temp = file_name.split('/')
        lastone = len(temp)
        newfile = temp[lastone-1]
        datalist=[]

        #Create order list of calibration points
        calibration_names = ['right', 'vertex', 'nasion', 'left']
        for key in calibration_names:
            temp_points = self.calibration_points[key]
            datalist.append([temp_points[0], temp_points[1], temp_points[2]])

        #Write csv file with calibration points
        with open("calib_data/calib_pills/tms/"+newfile, 'wb') as f:
            writer = csv.writer(f, delimiter=',')
            writer.writerows(datalist)

        #Matlab method, rigid transformation
        # Input new_data/calib_pills/tms/subject.csv (429.csv)
        # Output new_data/calib_pills/tms/new_pos/subject.csv (TMS-429.csv)
        #
        #
        #
        #

        #Get result of matlab method new pills position
        textlist = []
        with open('calib_data/calib_pills/tms/new_pos/'+newfile, 'rb') as csvfile:
            textlist = csv.reader(csvfile, delimiter=',', lineterminator='\n')
            my_list=list(textlist)

        #Parsing float the list items
        my_list = [[float(i) for i in tt] for tt in my_list]

        #Gen new calibration points
        for newvalue in range(0,self.calibration_points.__len__()):
            if newvalue == 0 :
                self.calibration_points['right'][0] = my_list[newvalue][0]
                self.calibration_points['right'][1] = my_list[newvalue][1]
                self.calibration_points['right'][2] = my_list[newvalue][2]
            if newvalue == 1 :
                self.calibration_points['vertex'][0] = my_list[newvalue][0]
                self.calibration_points['vertex'][1] = my_list[newvalue][1]
                self.calibration_points['vertex'][2] = my_list[newvalue][2]
            if newvalue == 2 :
                self.calibration_points['nasion'][0] = my_list[newvalue][0]
                self.calibration_points['nasion'][1] = my_list[newvalue][1]
                self.calibration_points['nasion'][2] = my_list[newvalue][2]
            if newvalue == 3 :
                self.calibration_points['left'][0] = my_list[newvalue][0]
                self.calibration_points['left'][1] = my_list[newvalue][1]
                self.calibration_points['left'][2] = my_list[newvalue][2]

        print 'Nuevos puntos calibracion'
        print self.calibration_points

        if self.hacks is not None:
            skip = set(self.hacks.get("skip_calib",set()))
            pts = [v for k,v in self.calibration_points.iteritems() if k not in skip]
            r, ctr = tms_geom.adjust_sphere(pts)
        else:
            r, ctr = tms_geom.adjust_sphere(self.calibration_points.itervalues())

        self.sphere_radius, self.sphere_center = r, ctr
コード例 #2
0
def __test_one():
    import os
    import vtk
    from itertools import izip

    test_dir = os.path.join(os.path.dirname(__file__), "data")
    test_file = os.path.join(test_dir, "TMS-441.csv")
    test_file = os.path.join(test_dir, "TMS-758.csv")
    #test_file = os.path.join(test_dir, "TMS-310.csv")

    points = tms_rt.read_csv_file(test_file)
    calibs = tms_rt.extract_calibration_samples(points)
    assert set(calibs.keys()) == {1, 2, 3, 4}
    ref = calibs[3]
    calibs_norm = dict(((k, tms_rt.normalize_point_to_ref(v, ref) ) for k, v in calibs.iteritems() ))

    ren_win = vtk.vtkRenderWindow()
    iren = vtk.vtkRenderWindowInteractor()
    iren.SetInteractorStyle(vtk.vtkInteractorStyleTrackballCamera())
    iren.SetRenderWindow(ren_win)
    ren = vtk.vtkRenderer()
    ren_win.AddRenderer(ren)

    date = ref.date

    pointer_fun = tms_rt.get_pointer_transform_function(date)
    calib_points = [pointer_fun(p) for p in calibs_norm.itervalues()]

    r,ctr = tms_geom.adjust_sphere(calib_points)

    for p in calib_points:
        __add_sphere_to_ren(p,r/20,ren)

    ac = __add_sphere_to_ren(ctr,r,ren)
    ac.GetProperty().SetColor(1,0,0)
    #fit sphere to calibration

    coil_samples = tms_rt.extract_coil_samples(points)[:50]
    coil_samples_norm = tms_rt.normalize_to_ref(coil_samples, ref)
    coil_function = tms_rt.get_coil_transform_function(date)
    coil_pairs = [coil_function(p) for p in coil_samples_norm]
    cc = (0, 1, 0)
    cc2 = (0.2,0.7,0.2)
    for (c, t),p in izip(coil_pairs,coil_samples_norm):
        ac = __add_sphere_to_ren(c, 0.002, ren)
        ac.GetProperty().SetColor(cc)
        ac = __add_line_to_ren(c, np.subtract(t, c), ren)
        ac.GetProperty().SetColor(cc)

    #draw intersection with sphere
    intersects = [tms_geom.intersect_point_with_sphere(c,np.subtract(t,c),r,ctr) for c,t in coil_pairs]
    for p in intersects:
        ac = __add_sphere_to_ren(p, 0.001, ren)
        ac.GetProperty().SetColor(cc2)
        ac.GetProperty().SetOpacity(0.5)

    #find plane
    vec,  circle_radius = tms_geom.circle_from_points_in_sphere(intersects,r,ctr)
    __add_plane_to_ren(ctr+vec,vec,r,ren)

    iren.Initialize()
    iren.Start()