def get_side_total(self, hemi): from itertools import chain from tms import tms_geom blocks = [b for b in self.blocks_model.blocks if b.block_type in {"ACCEPTED", "UNKNOWN"} and b.hemisphere == hemi] intercepts = [s.sphere_intersection for s in chain(*(b.samples for b in blocks)) if s.sphere_intersection is not None and not s.ignored] vec, circle_radius = tms_geom.circle_from_points_in_sphere(intercepts, self.experiment.sphere_radius, self.experiment.sphere_center) return circle_radius
def recalculate_mean_and_error(self, sphere_r, sphere_center): #print 'Recalculate mean and error' coil_centers = [p.coil_center for p in self.samples if not p.ignored] points_on_sphere = [p.sphere_intersection for p in self.samples if not p.ignored] points_on_sphere = filter(lambda x: x is not None, points_on_sphere) #print '1' if len(points_on_sphere) < 2: #print '2' v,r = np.array((0,0,0)),0 self.mean_intersection = np.zeros(3) self.mean_coil_center = np.zeros(3) else: #print '3' v, r = tms_geom.circle_from_points_in_sphere(points_on_sphere, sphere_r, sphere_center) #print '4' self.mean_intersection = np.mean(points_on_sphere, axis=0) #print '5' self.mean_coil_center = np.mean(coil_centers, axis=0) #print '6' self.circle_radius = r self.circle_relative_center = v self.position_error = r
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()