def invFile(filenameIn, filenameOut): print " -- Checking if we need to invert?", # Find the amount of translation we need to do! data = ReadFile(filenameIn) dataLines = [l for l in data.split("\n") if l.strip() and not l.strip().startswith("#") ] CommentLines = [l for l in data.split("\n") if l.strip() and l.strip().startswith("#") ] xmin,xmax = None,None outData = CommentLines for l in dataLines: i,t,x,y,z,X,Y,Z,p = extractXYZLine(l) xmin = min( [xmin,x,X] ) if xmin != None else min( [x,X] ) xmax = max( [xmax,x,X] ) if xmax != None else max( [x,X] ) if not( xmin<=0 and xmax <=0): print " No, xmin,max:", xmin,xmax WriteFile(filenameOut, data) else: print " Yes, xmin,max:", xmin,xmax # Ok, we have no positive x's: outData = CommentLines for l in dataLines: i,t,x,y,z,X,Y,Z,p = extractXYZLine(l) x = x * -1 X = X * -1 outData.append( "\t".join( [str(g) for g in [i,t,x,y,z,X,Y,Z,p] ] ) ) WriteFile( filenameOut, "\n".join( outData) )
def nrnXYZToSWC(srcFilename, sinkFilename): datestr = datetime.datetime.now().strftime("%d-%B-%Y") print " -- Converting to SWC" # Find the amount of translation we need to do! data = ReadFile(srcFilename) dataLines = [l for l in data.split("\n") if l.strip() and not l.strip().startswith("#") ] CommentLines = [l for l in data.split("\n") if l.strip and l.strip().startswith("#") ] outData = CommentLines + ["# Converted from xyz to swc using nrnXYZtoSWC.py" + " on: %s" %datestr] #["# Translated by (%d,%d,%d) using nrnTranslate.py"%translation + " On: %s"%datestr] + ["# Corrected for shrinkage (x1.28) using nrnScaled.py" + " On: %s"%datestr] + ["# Files Concatenated by nrnConcatenate.py" + " on: %s"%datestr] + ["# Converted from xyz to swc using nrnXYZtoSWC.py" + " on: %s" %datestr] for l in dataLines: i,t,x,y,z,X,Y,Z,p = extractXYZLine(l) sX = (float(x)+float(X)) / 2.0 sY = (float(y)+float(Y)) / 2.0 sZ = (float(z)+float(Z)) / 2.0 dX = float(x) - float(X) dY = float(y) - float(Y) dZ = float(z) - float(Z) d = math.sqrt( dX**2 + dY**2 + dZ**2 ) r = d / 2.0 r = r if r > 0.15 else 0.15 outData.append( "\t".join( [str(g) for g in [i,t,sX,sY,sZ,r,p] ] ) ) WriteFile( sinkFilename, "\n".join( outData) )
def nrnConcatenateXYZS( srcFilename1, srcFilename2, sinkFilename): datestr = datetime.datetime.now().strftime("%d-%B-%Y") print " -- Concatentating Files" D1 = ReadFile(srcFilename1) D2 = ReadFile(srcFilename2) dataLines1 = [l for l in D1.split("\n") if l.strip() and not l.strip().startswith("#") ] dataLines2 = [l for l in D2.split("\n") if l.strip() and not l.strip().startswith("#") ] CommentLines1 = [l for l in D1.split("\n") if l.strip() and l.strip().startswith("#") ] CommentLines2 = [l for l in D2.split("\n") if l.strip() and l.strip().startswith("#") ] # copies comment lines and adds comment line showing the processing that's been done plus date. #outData = [ "#Comment From File1: %s"%srcFilename1 ] + CommentLines1 + [ "#Comment From File2: %s"%srcFilename2 ] + CommentLines2 + ["# Files Concatenated by nrnConcatenate.py" + " on: %s"%datestr] outData = CommentLines1 + CommentLines2 + ["# Files Concatenated by nrnConcatenate.py" + " on: %s"%datestr] # Remap the id's # Write Datafile 1 idMap = {} for l in dataLines1: i,t,x,y,z,X,Y,Z,p = extractXYZLine(l) new_id = len(idMap) + 1 new_p = p if p ==-1 else idMap[ (0,p) ] outData.append( "\t".join( [str(g) for g in [new_id,t,x,y,z,X,Y,Z,new_p] ] ) ) idMap[ (0,i) ] = new_id #print srcFilename2 # Write Datafile 2 for l in dataLines2: #print l #print i,t,x,y,z,X,Y,Z,p = extractXYZLine(l) new_id = len(idMap) + 1 #print p new_p = p if p ==-1 else idMap[(1,p)] outData.append( "\t".join( [str(g) for g in [new_id,t,x,y,z,X,Y,Z,new_p] ] ) ) idMap[(1,i)] = new_id WriteFile( sinkFilename, "\n".join( outData) )
def nrnScale(srcFilename, sinkFilename, scaleFactor): datestr = datetime.datetime.now().strftime("%d-%B-%Y") # Find the amount of translation we need to do! data = ReadFile(srcFilename) dataLines = [l for l in data.split("\n") if l.strip() and not l.strip().startswith("#") ] CommentLines = [l for l in data.split("\n") if l.strip() and l.strip().startswith("#") ] outData = CommentLines + ["# Corrected for shrinkage (x1.28) using nrnScaled.py" + " On: %s"%datestr] for l in dataLines: i,t,x,y,z,X,Y,Z,p = extractXYZLine(l) x *= scaleFactor y *= scaleFactor z *= scaleFactor X *= scaleFactor Y *= scaleFactor Z *= scaleFactor outData.append( "\t".join( [str(g) for g in [i,t,x,y,z,X,Y,Z,p] ] ) ) WriteFile( sinkFilename, "\n".join( outData) )
def TranslateFile(filenameIn, filenameOut, translation): datestr = datetime.datetime.now().strftime("%d-%B-%Y") # Find the amount of translation we need to do! data = ReadFile(filenameIn) dataLines = [l for l in data.split("\n") if l.strip() and not l.strip().startswith("#") ] CommentLines = [l for l in data.split("\n") if l.strip() and l.strip().startswith("#") ] # copies comment lines and adds comment line showing the processing that's been done plus date. #outData = CommentLines + ["# Translated by (%d,%d,%d) using nrnTranslate.py"%translation] + ["# On: %s"%datestr] outData = CommentLines + ["# Translated by (%d,%d,%d) using nrnTranslate.py"%translation + " On: %s"%datestr] #outData = outData + ["# On: %s"%datestr] for l in dataLines: i,t,x,y,z,X,Y,Z,p = extractXYZLine(l) x = x + translation[0] y = y + translation[0] z = z + translation[0] X = X + translation[0] Y = Y + translation[0] Z = Z + translation[0] outData.append( "\t".join( [str(g) for g in [i,t,x,y,z,X,Y,Z,p] ] ) ) WriteFile( filenameOut, "\n".join( outData) )
def nrnTranslate( nsFilenameSrc, nrnFilenameSrc,nsFilenameSink, nrnFilenameSink): print " -- Finding Translation" nsData = ReadFile(nsFilenameSrc) # Find the amount of translation we need to do! dataLines = [l for l in nsData.split("\n") if l.strip() and not l.strip().startswith("#") ] tPoint = None for l in dataLines: i,t,x,y,z,X,Y,Z,p = extractXYZLine(l) if p==-1 and t==9: assert not tPoint tPoint = (-1.0*x,-1.0*y,-1.0*z) assert tPoint print " -- Translating by", tPoint TranslateFile(nsFilenameSrc, nsFilenameSink, tPoint) TranslateFile(nrnFilenameSrc, nrnFilenameSink, tPoint)