def from_center(self, center, width = None, nsamples = all): """Initialize worm model from center line and width Arguments: center_line (nx2 array): points of center line width (array or None): width of worm at reference points, if None use initial guess nsamples (int or all): number of intermediate samples points """ theta, orientation, xy, length = wormgeo.theta_from_center_discrete(center, npoints = self.npoints, nsamples = nsamples, resample=False, smooth=0); #points = np.linspace(0,1,self.npoints-2); #self.theta.set_parameter_from_values(theta, points = points); self.theta.set_values(theta); self.xy = xy; self.orientation = orientation; self.length = length; if width is not None: #points = np.linspace(0,1,width.shape[0]); #self.width.set_parameter_from_values(width, points = points); self.width.set_values(width);
def from_center(self, center, width=None, nsamples=all): """Initialize worm model from center line and width Arguments: center_line (nx2 array): points of center line width (array or None): width of worm at reference points, if None use initial guess nsamples (int or all): number of intermediate samples points """ theta, orientation, xy, length = wormgeo.theta_from_center_discrete( center, npoints=self.npoints, nsamples=nsamples, resample=False, smooth=0) #points = np.linspace(0,1,self.npoints-2); #self.theta.set_parameter_from_values(theta, points = points); self.theta = theta self.xy = xy self.orientation = orientation self.length = length if width is not None: #points = np.linspace(0,1,width.shape[0]); #self.width.set_parameter_from_values(width, points = points); self.width = width
def from_shape(self, left, right, center=None, width=None, nsamples=all, nneighbours=all): """Initialize worm model from the left and right border lines Arguments: left (nx2 array): points of left border line right (nx2 array): points of right border line center (nx2 array): optional center line width (n array): optional width of the worm nsamples (int): number of intermediate sample points """ # get center line and width profile if width is None: with_width = True else: with_width = False if center is None or width is None: cw = wormgeo.center_from_sides_discrete(left, right, with_width=with_width, nsamples=nsamples, npoints=nsamples, resample=False, smooth=0, nneighbours=nneighbours, method='projection') if width is None: center, width = cw else: center = cw npoints = self.theta.npoints theta, orientation, xy, length = wormgeo.theta_from_center_discrete( center, npoints=npoints, nsamples=nsamples, resample=False, smooth=0) # non uniform version #oints = np.linspace(0,1,npoints-2); #self.theta.set_parameter_from_values(theta, points = points); #points = np.linspace(0,1,nsamples); #self.width.set_parameter_from_values(width, points = points); #theta and width uniform self.theta.set_values(theta) self.width.set_values(width) self.xy = xy self.orientation = orientation self.length = length
def curve(self, mode_amplitudes): """Change curvature properties of the worm Arguments: mode_amplitudes (number or array): additional power in the first fourier modes of the worms angles """ #changes curvature by the mode amplitudes; #cos = np.cos(self.theta); -> ok to use theta directly theta, orientation, xy, length = wormgeo.theta_from_center_discrete(self.center); t = np.fft.rfft(theta); mode_amplitudes = np.array(mode_amplitudes); t[:mode_amplitudes.shape[0]] += mode_amplitudes; theta = np.fft.irfft(t, n = self.npoints-2); self.center = wormgeo.center_from_theta_discrete(theta, orientation, xy, length);
def curve(self, mode_amplitudes): """Change curvature properties of the worm Arguments: mode_amplitudes (number or array): additional power in the first fourier modes of the worms angles """ #changes curvature by the mode amplitudes; #cos = np.cos(self.theta); -> ok to use theta directly theta, orientation, xy, length = wormgeo.theta_from_center_discrete( self.center) t = np.fft.rfft(theta) mode_amplitudes = np.array(mode_amplitudes) t[:mode_amplitudes.shape[0]] += mode_amplitudes theta = np.fft.irfft(t, n=self.npoints - 2) self.center = wormgeo.center_from_theta_discrete( theta, orientation, xy, length)
def bend(self, bend, exponent = 4, head = True): """Change curvature properties of the worm Arguments: bend (number): bending amplitude exponent (number): expoential modulation of the bending head (bool): if True bend head side otherwise tail side """ #head tail bend profile theta, orientation, xy, length = wormgeo.theta_from_center_discrete(self.center); n2 = theta.shape[0]//2; if head: theta[:n2-1] += bend * np.exp(-exponent * np.linspace(0,1,n2-1)); else: theta[-(n2-1):] += bend * np.exp(-exponent * np.linspace(1,0,n2-1)); self.center = wormgeo.center_from_theta_discrete(theta, orientation, xy, length);
def from_shape(self, left, right, center = None, width = None, nsamples = all, nneighbours = all): """Initialize worm model from the left and right border lines Arguments: left (nx2 array): points of left border line right (nx2 array): points of right border line center (nx2 array): optional center line width (n array): optional width of the worm nsamples (int): number of intermediate sample points """ # get center line and width profile if width is None: with_width = True; else: with_width = False; if center is None or width is None: cw = wormgeo.center_from_sides_discrete(left, right, with_width = with_width, nsamples = nsamples, npoints = nsamples, resample = False, smooth=0, nneighbours=nneighbours, method='projection'); if width is None: center, width = cw; else: center = cw; npoints = self.theta.npoints; theta, orientation, xy, length = wormgeo.theta_from_center_discrete(center, npoints = npoints, nsamples = nsamples, resample=False, smooth=0); # non uniform version #oints = np.linspace(0,1,npoints-2); #self.theta.set_parameter_from_values(theta, points = points); #points = np.linspace(0,1,nsamples); #self.width.set_parameter_from_values(width, points = points); #theta and width uniform self.theta.set_values(theta); self.width.set_values(width); self.xy = xy; self.orientation = orientation; self.length = length;
def bend(self, bend, exponent=4, head=True): """Change curvature properties of the worm Arguments: bend (number): bending amplitude exponent (number): expoential modulation of the bending head (bool): if True bend head side otherwise tail side """ #head tail bend profile theta, orientation, xy, length = wormgeo.theta_from_center_discrete( self.center) n2 = theta.shape[0] // 2 if head: theta[:n2 - 1] += bend * np.exp(-exponent * np.linspace(0, 1, n2 - 1)) else: theta[-(n2 - 1):] += bend * np.exp( -exponent * np.linspace(1, 0, n2 - 1)) self.center = wormgeo.center_from_theta_discrete( theta, orientation, xy, length)
g = tf.Graph() sess = tf.Session(); #%% fids = range(300000, 400000, 1000)[:batch_size]; img = [exp.image_data(frame = f, smooth = True) for f in fids]; imgc = [to_color(i) for i in img]; img_pre = np.array([vgg.preprocess(i, mean_pixel) for i in imgc]) features = net['relu5_4'].eval(feed_dict={image: img_pre}, session = sess) features_flat = np.reshape(features, (batch_size, -1)); centers =[exp.shape_data(frame = f)[:2,:] for f in fids]; thetas = np.array([wgeo.theta_from_center_discrete(c.T)[0] for c in centers]); #%% Regression from sklearn import linear_model from sklearn.metrics import mean_squared_error, r2_score model = linear_model.LinearRegression() model.fit(features_flat, thetas) #%% Test fids_test = range(300500, 400500, 1000)[:batch_size];
fids = range(600000, 600000+40000); #len(data_shape)); ds = data_shape[fids, :2,:]; #ds = np.reshape(ds, (ds.shape[0], -1)); print ds.shape #%% calculate import worm.geometry as wgeo data_theta = np.zeros((len(ds), data_shape.shape[-1]-2)); for i in range(len(ds)): if i%100==0: print('%d/%d' % (i,len(ds))); theta,a,b,c = wgeo.theta_from_center_discrete(ds[i].T); data_theta[i,:] = theta; #%% #%% import sklearn.manifold as sl; n_components = 2; metric = 'euclidean'; tsne = sl.TSNE(n_components=n_components, init = 'pca', random_state = 10, metric = metric) Y = tsne.fit_transform(data_theta) #%% tsne of shapes
def theta(self): return wormgeo.theta_from_center_discrete(self.center);
g = tf.Graph() sess = tf.Session() #%% fids = range(300000, 400000, 1000)[:batch_size] img = [exp.image_data(frame=f, smooth=True) for f in fids] imgc = [to_color(i) for i in img] img_pre = np.array([vgg.preprocess(i, mean_pixel) for i in imgc]) features = net['relu5_4'].eval(feed_dict={image: img_pre}, session=sess) features_flat = np.reshape(features, (batch_size, -1)) centers = [exp.shape_data(frame=f)[:2, :] for f in fids] thetas = np.array([wgeo.theta_from_center_discrete(c.T)[0] for c in centers]) #%% Regression from sklearn import linear_model from sklearn.metrics import mean_squared_error, r2_score model = linear_model.LinearRegression() model.fit(features_flat, thetas) #%% Test fids_test = range(300500, 400500, 1000)[:batch_size] img = [exp.image_data(frame=f, smooth=True) for f in fids_test] imgc = [to_color(i) for i in img]
def theta(self): return wormgeo.theta_from_center_discrete(self.center)