def _curve( file, stimulus_time ): mat = mypy.load_csv( file, delimiter = ' ', cast = float, comment = '#', skip_empty_entries = "ends" ) curve = mypy.col( mat, ( 0, 1 ) ) values = [ value for ( time, value ) in curve if time >= stimulus_time ] baseline = values[ 0 ] values = [ value - baseline for value in values ] return values
def _lineage( tree_file ): lineage = {} tf = mypy.load_csv( tree_file, delimiter = '\t' ) for row in tf: self = row[ 0 ] parent = row[ 1 ] if parent == 'none': lineage[ self ] = [] else: lineage[ self ] = lineage[ parent ] + [ parent ] return lineage
def relationship( out_file, cell_file, rows = (), cols = () ): """Generates a matrix which tells the relationship between one set of nodes (rows) and another (cols). Entries in the matrix can be one of: -1: separate branches 1: self 0: col is ancestor of row 2: col is descendant of row If 'rows' is not specified then all the nodes from the 'cell_file' are taken. Likewise for 'cols'. """ print "[ relationship ]" print " out_file :", out_file print " cell_file :", cell_file print " rows :", rows print " cols :", cols lineage = _lineage( cell_file ) if rows == () or cols == (): cf = mypy.load_csv( cell_file, delimiter = '\t', comment = '#', cast = str ) all_compts = [ c[ 0 ] for c in cf ] if isinstance( rows, str ): rows = ( rows, ) elif rows == (): rows = all_compts if isinstance( cols, str ): cols = ( cols, ) elif cols == (): cols = all_compts header = [ '#Compartment' ] for i in cols: header.append( i ) matrix = [ header ] for i in rows: row = [ i ] for j in cols: row.append( _relationship_ij( lineage, i, j ) ) matrix.append( row ) with open( out_file, "w" ) as f: writer = csv.writer( f, delimiter = '\t' ) writer.writerows( matrix )
import mypy import pprint pp = pprint.PrettyPrinter() print "=== Default args. (delimiter = '\\t') ===" pp.pprint( mypy.load_csv( 'sample1.dat' ) ) print print "=== Delimiter = ' ' ===" pp.pprint( mypy.load_csv( 'sample1.dat', delimiter = ' ' ) ) print print "=== Delimiter = ' ', comment = '#' ===" pp.pprint( mypy.load_csv( 'sample1.dat', delimiter = ' ', comment = '#' ) ) print print "=== Delimiter = ' ', comment = '#;' ===" pp.pprint( mypy.load_csv( 'sample1.dat', delimiter = ' ', comment = '#;' ) ) print print "=== Delimiter = ' ', comment = '#', skip_empty_lines = True ===" pp.pprint( mypy.load_csv( 'sample1.dat', delimiter = ' ', comment = '#', skip_empty_lines = True ) ) print print "=== Delimiter = ' ', comment = '#', skip_empty_entries = 'ends' ===" pp.pprint( mypy.load_csv( 'sample1.dat', delimiter = ' ', comment = '#', skip_empty_entries = 'ends' ) ) print print "=== Delimiter = ' ', comment = '#;', skip_empty_entries = 'ends' ===" pp.pprint( mypy.load_csv( 'sample1.dat', delimiter = ' ', comment = '#;', skip_empty_entries = 'ends' ) )
import mypy import pprint pp = pprint.PrettyPrinter() print "=== Delimiter = ' ', comment = '#' ===" pp.pprint(mypy.load_csv("sample2.dat", delimiter=" ", comment="#")) print print "=== Delimiter = ' ', comment = '#', cast = str ===" pp.pprint(mypy.load_csv("sample2.dat", delimiter=" ", comment="#", cast=str)) print print "=== Delimiter = ' ', comment = '#', cast = float ===" try: pp.pprint(mypy.load_csv("sample2.dat", delimiter=" ", comment="#", cast=float)) except Exception as message: print message, print print "=== Delimiter = ' ', comment = '#', cast = float, skip_empty_entries = 'ends' ===" try: pp.pprint(mypy.load_csv("sample2.dat", delimiter=" ", comment="#", cast=float, skip_empty_entries="ends")) except Exception as message: print message, print print "=== Delimiter = ' ', comment = '#', cast = float, skip_empty_entries = 'ends', skip_empty_lines = True ===" try: pp.pprint( mypy.load_csv(
def distance( physical_out_file, electrotonic_out_file, cell_file, length_file, reference = (), moving = () ): print "[ distance ]" print " physical_out_file :", physical_out_file print " electrotonic_out_file :", electrotonic_out_file print " cell_file :", cell_file print " length_file :", length_file print " reference :", reference print " moving :", moving length = {} lineage = _lineage( cell_file ) if reference == () or moving == (): cf = mypy.load_csv( cell_file, delimiter = '\t', comment = '#', cast = str ) all_compts = [ c[ 0 ] for c in cf ] lf = mypy.load_csv( length_file, delimiter = ' ', comment = '#', cast = str ) for row in lf: self, physical, L, electrotonic = row length[ self ] = ( float( physical ), float( electrotonic ) ) if isinstance( reference, str ): reference = ( reference, ) elif reference == (): reference = all_compts if isinstance( moving, str ): moving = ( moving, ) elif moving == (): moving = all_compts physical_matrix = [ ] electrotonic_matrix = [ ] for i in moving: physical_row = [ ] electrotonic_row = [ ] for j in reference: ( physical, electrotonic ) = _distance_ij( lineage, length, i, j ) physical_row.append( physical ) electrotonic_row.append( electrotonic ) physical_matrix.append( physical_row ) electrotonic_matrix.append( electrotonic_row ) header = [ '#Compartment' ] header.extend( reference ) physical_matrix_labelled = physical_matrix physical_matrix_labelled = zip( *physical_matrix_labelled ) physical_matrix_labelled.insert( 0, moving ) physical_matrix_labelled = zip( *physical_matrix_labelled ) physical_matrix_labelled.insert( 0, header ) electrotonic_matrix_labelled = electrotonic_matrix electrotonic_matrix_labelled = zip( *electrotonic_matrix_labelled ) electrotonic_matrix_labelled.insert( 0, moving ) electrotonic_matrix_labelled = zip( *electrotonic_matrix_labelled ) electrotonic_matrix_labelled.insert( 0, header ) with open( physical_out_file, "w" ) as f: writer = csv.writer( f, delimiter = '\t' ) writer.writerows( physical_matrix_labelled ) with open( electrotonic_out_file, "w" ) as f: writer = csv.writer( f, delimiter = '\t' ) writer.writerows( electrotonic_matrix_labelled ) return ( numpy.array( physical_matrix ), numpy.array( electrotonic_matrix ) )