Example #1
0
    def run(self):
        
        ifile = self.options.input
        ofile = self.options.output
        
        reader = NrrdReader()
        iparams, bindata =  reader.getFileContent(ifile)
	#iparams = iparams._data
        
        b0n = iparams['b0num']
        print "%d b0 to be averaged" % b0n
        b0n -= 1

        difcropstr = getOrderStr(options.aorder, b0n)
        orderstr = getOrderStr(options.aorder, b0n)

        print '###########',orderstr

        if options.is_average:
            cmd = "unu crop -i %s -min %s -max M M M M -o tmp.nhdr; " % ( ifile, difcropstr)
            cmd += "unu crop -i %s -min 0 0 0 0 -max %d M M M | unu project -a %s -m mean | unu splice -i grad.nhdr -s - -a 0 -p 0 -o %s; rm tmp.*" % (ifile, b0n, options.aorder, ofile)        
        else:
            cmd = "unu crop -i %s -min %s -max M M M M -o %s " % ( ifile, orderstr, ofile)

        print cmd        
        os.system(cmd)
        
        
        ofilein = open(ofile,'r')
        content = []
        while ofilein:
            line = ofilein.readline()            
            if len(line) == 0:
                break;
            if line.startswith(NrrdReader.grdkey):
                continue
            content.append(line)
        ofilein.close()
        
        
        count = 0
        for n,i in enumerate(iparams[NrrdReader.grdkey]):
            if count > 0 and n <= b0n:
                continue

            oline =  '%s_%04d:=' % (NrrdReader.grdkey,count)
            for j in i:
                oline += '%1.6f ' % j
            content.append(oline+'\n')
            count+=1
            
            ofileout = open(ofile, 'w')
            for l in content:
                if options.verbose:
                    print l
                ofileout.write(l)
            ofileout.close()
def run():
    global options,args

    nrrdr = NrrdReader()

    params, bindata = nrrdr.getFileContent(options.input)
    for k in params.keys():
        print k,' : ',params[k]

    dim = params['sizes']
    dimX = dim[0]
    dimY = dim[1]
    dimZ = dim[2]

    numSeeds = 9
    seedThresh = numSeeds * 30
    radius = 0.5
    spacing = options.spacing

    if params['type'][0] == 'float':
        datatype = numpy.float32
    elif params['type'][0] == 'double':
        datatype = numpy.double 
    elif params['type'][0] == 'short':
        datatype = numpy.short

    size = dimX*dimY*dimZ
    shape = (dimX, dimY, dimZ)

    if 'data file' in params:
        filename = params['data file'][0]
        f = gzip.open(filename, 'r')
        FILE = f.read()
        f.close()
    else:
        FILE=gzip.GzipFile(fileobj=StringIO(bindata)).read()

    data = numpy.fromstring(FILE, dtype=datatype)

    print data.shape
    print shape
    print shape[0]*shape[1]*shape[2]
    data = data.reshape(shape, order='F') #matlab uses fortran order; numpy uses c order


    if not os.path.isdir("seeds"):
        print 'make seeds dirs'
        os.mkdir("seeds")
    os.chdir("seeds")
    print os.getcwd()

    filemap = {}

    seedPoints = 0;
    fileCount = 1;
    for z in range(0, dimZ):
        for y in range(0, dimY):
            for x in range(0, dimX):
                val = data[x][y][z]
                if val > 0:
                    if not filemap.has_key(val):
                        filename = 'IC%d.txt' % val
                        filemap[val] = open(filename, 'w')

                    print '(%d,%d,%d) \t %d' % (x,y,z,data[x][y][z])
                    vx = x
                    vy = y
                    vz = z

                    if options.isRand:
                        points = numpy.random.uniform(size=[options.num_points, 3]) + [vx-0.5, vy-0.5, vz-0.5]
                        for p in points:
                            str = '%f %f %f\n' % (p[0],p[1],p[2])
                            filemap[val].write(str)

                    else:
                        for zz in numpy.arange(vz-radius,vz+radius,spacing):
                            for yy in numpy.arange(vy-radius,vy+radius,spacing):
                                for xx in numpy.arange(vx-radius,vx+radius,spacing):
                                    str = '%f %f %f\n' % (xx,yy,zz)
                                    filemap[val].write(str)

                    seedPoints = seedPoints + numSeeds;

#                    if seedPoints % seedThresh == 0:
#                        OUT.close()
#                        fileCount += 1
#                        newfile = 'IC%d.txt' % fileCount
#                        OUT = open(newfile, 'w')
    for i in filemap.values():
            i.close()
Example #3
0
def run():
    global options, args

    nrrdr = NrrdReader()

    params, bindata = nrrdr.getFileContent(options.input)
    for k in params.keys():
        print k, ' : ', params[k]

    dim = params['sizes']
    dimX = dim[0]
    dimY = dim[1]
    dimZ = dim[2]

    numSeeds = 9
    seedThresh = numSeeds * 30
    radius = 0.5
    spacing = options.spacing

    if params['type'][0] == 'float':
        datatype = numpy.float32
    elif params['type'][0] == 'double':
        datatype = numpy.double
    elif params['type'][0] == 'short':
        datatype = numpy.short

    size = dimX * dimY * dimZ
    shape = (dimX, dimY, dimZ)

    if 'data file' in params:
        filename = params['data file'][0]
        f = gzip.open(filename, 'r')
        FILE = f.read()
        f.close()
    else:
        FILE = gzip.GzipFile(fileobj=StringIO(bindata)).read()

    data = numpy.fromstring(FILE, dtype=datatype)

    print data.shape
    print shape
    print shape[0] * shape[1] * shape[2]
    data = data.reshape(
        shape, order='F')  #matlab uses fortran order; numpy uses c order

    if not os.path.isdir("seeds"):
        print 'make seeds dirs'
        os.mkdir("seeds")
    os.chdir("seeds")
    print os.getcwd()

    filemap = {}

    seedPoints = 0
    fileCount = 1
    for z in range(0, dimZ):
        for y in range(0, dimY):
            for x in range(0, dimX):
                val = data[x][y][z]
                if val > 0:
                    if not filemap.has_key(val):
                        filename = 'IC%d.txt' % val
                        filemap[val] = open(filename, 'w')

                    print '(%d,%d,%d) \t %d' % (x, y, z, data[x][y][z])
                    vx = x
                    vy = y
                    vz = z

                    if options.isRand:
                        points = numpy.random.uniform(
                            size=[options.num_points, 3]) + [
                                vx - 0.5, vy - 0.5, vz - 0.5
                            ]
                        for p in points:
                            str = '%f %f %f\n' % (p[0], p[1], p[2])
                            filemap[val].write(str)

                    else:
                        for zz in numpy.arange(vz - radius, vz + radius,
                                               spacing):
                            for yy in numpy.arange(vy - radius, vy + radius,
                                                   spacing):
                                for xx in numpy.arange(vx - radius,
                                                       vx + radius, spacing):
                                    str = '%f %f %f\n' % (xx, yy, zz)
                                    filemap[val].write(str)

                    seedPoints = seedPoints + numSeeds


#                    if seedPoints % seedThresh == 0:
#                        OUT.close()
#                        fileCount += 1
#                        newfile = 'IC%d.txt' % fileCount
#                        OUT = open(newfile, 'w')
    for i in filemap.values():
        i.close()