def load_directory(path, pattern='.txt', sep=None, comment_char='#', dt=None, t_unit='', coord_unit='', **attrs): """ load_directory(path,pattern = '.txt',sep = None,comment_char = '#',dt=None,t_unit='',**attrs) loads all the trajectories listed in 'path', which have the same 'pattern'. columns are separated by 'sep' (default is None: a indefinite number of white spaces). Comments in the trajectory start with 'comment_char'. **attrs is used to assign columns to the trajectory attributes and to add annotations. If the time interval is added (and 't' is not called in the **attrs) then the time column 't' is added, and the 't_unit' can be set. If 'coord' is called then the unit must be added. """ if ('coord' in attrs.keys()) & (len(coord_unit) == 0): raise AttributeError( 'Please, specify the coordinate unit \'coord_unit\'') if ('t' in attrs.keys()) & (len(t_unit) == 0): raise AttributeError('Please, specify the time unit \'t_unit\'') if (dt != None) & (len(t_unit) == 0): raise AttributeError('Please, specify the time unit \'t_unit\'') if (dt != None) & ('t' in attrs.keys()): raise AttributeError( 'Time is already loaded by the trajectories, you cannot also compute it from frames. Please, either remove the dt option or do not load the \'t\' column from the trajectories' ) trajectories = [] #the list of trajectories if (pattern[len(pattern) - 1] == '$'): files = [f for f in os.listdir(path) if f.endswith(pattern[:-1]) ] #list all the files in path that have pattern else: files = [f for f in os.listdir(path) if pattern in f ] #list all the files in path that have pattern for file in files: trajectory = Traj(experiment=path, path=os.getcwd() + '/' + path, file=file) trajectory.load(path + '/' + file, sep=sep, comment_char=comment_char, **attrs) if (dt != None): trajectory.time(dt, t_unit) if ('coord' in attrs.keys()): trajectory.annotations('coord_unit', coord_unit) trajectory.fill() trajectory.norm_f() trajectories.append(trajectory) return trajectories
def load_directory(path , pattern = '.txt' , sep = None , comment_char = '#' , dt = None , t_unit = '' , coord_unit = '' , intensity_normalisation = 'None' , **attrs ): """ load_directory(path , pattern = '.txt' , sep = None , comment_char = '#' , dt = None , t_unit = '' , coord_unit = '' , intensity_normalisation = 'None' , **attrs ): loads all the trajectories listed in 'path', which have the same 'pattern'. columns are separated by 'sep' (default is None: a indefinite number of white spaces). Comments in the trajectory start with 'comment_char'. intensity_normalisation can be: 'None' (no normalisation, default), 'Integral' (normalise over the integral of the fluorescence intensity), or 'Absolute' (normalise the fluorescence intensity values between 0 and 1)" **attrs is used to assign columns to the trajectory attributes and to add annotations. If the time interval is added (and 't' is not called in the **attrs) then the time column 't' is added, and the 't_unit' can be set. If 'coord' is called then the unit must be added. """ if ('coord' in attrs.keys()) & (len(coord_unit) == 0): raise AttributeError('Please, specify the coordinate unit \'coord_unit\'') if ('t' in attrs.keys()) & (len(t_unit) == 0): raise AttributeError('Please, specify the time unit \'t_unit\'') if (dt != None) & (len(t_unit) == 0): raise AttributeError('Please, specify the time unit \'t_unit\'') if (dt != None) & ('t' in attrs.keys()): raise AttributeError('Time is already loaded by the trajectories, you cannot also compute it from frames. Please, either remove the dt option or do not load the \'t\' column from the trajectories') trajectories = [] #the list of trajectories if ( pattern[ len( pattern ) - 1 ] == '$' ) : files = [ f for f in sorted( os.listdir(path) ) if f.endswith( pattern[ : - 1 ] ) ] #list all the files in path that have pattern else : files = [ f for f in sorted( os.listdir(path) ) if pattern in f] #list all the files in path that have pattern for file in files: trajectory = Traj(experiment = path, path = os.getcwd()+'/'+path, file = file) trajectory.load(path+'/'+file,sep = sep, comment_char = comment_char, **attrs) if (dt != None): trajectory.time(dt,t_unit) if ('coord' in attrs.keys()): trajectory.annotations('coord_unit',coord_unit) if intensity_normalisation == 'Integral' : trajectory.scale_f() elif intensity_normalisation == 'Absolute' : trajectory.norm_f() elif intensity_normalisation != 'None' : raise AttributeError( "load_directory: Please, choose a value for the variable intensity_normalisation between 'None' (no normalisation, default), 'Integral' (normalise over the integral of the fluorescence intensity), or 'Absolute' (normalise the fluorescence intensity values between 0 and 1)" ) trajectory.annotations( 'intensity_normalisation' , intensity_normalisation ) trajectory.fill() trajectories.append(trajectory) print( "\n >> load_directory: The 'intensity_normalisation' applied to the trajectories is '" + intensity_normalisation + "' <<\n" ) return trajectories
d.time(0.1045, 's') # print some elements of the trajectory print(d.t(0, 1, len(d) - 1)) print(d.start()) print(d.end()) print(d.coord(range(0, 10))) # the trajectory lifetime print(d.lifetime()) #fill the missing frames print(max([d.frames(i) - d.frames(i - 1) for i in range(1, len(d))])) # there are some missing frames, let's fill them: d.fill() print(max([d.frames(i) - d.frames(i - 1) for i in range(1, len(d))])) d.save('my_first_trajectory') #save our trajectory, as a .txt file: # move the trajectory to its center of mass d.translate(-d.center_mass()) plt.figure() #plot the trajectory 'd' plt.plot( d.coord()[ 0 ] , d.coord()[ 1 ] ,\ 'b-' , label = 'Original traj.') #rotate the trajectory 'd' by ~pi/2 d.rotate(3.14 / 2)