From 737e474cea766ed4b8376302a966928577eaa2d0 Mon Sep 17 00:00:00 2001 From: Martin Diehl Date: Fri, 22 May 2015 05:56:49 +0000 Subject: [PATCH] consistent handling of random number per file and always reporting as comment, fix for permuteData needed because of new asciitable behavior --- processing/post/permuteData.py | 15 ++++++++------- processing/pre/OIMlinear2linearODF.py | 2 +- processing/pre/geom_fromBarycentric.py | 3 +-- processing/pre/geom_fromVoronoiTessellation.py | 16 +++++++++++++--- processing/pre/hybridIA_linODFsampling.py | 10 +++++----- processing/pre/seeds_fromRandom.py | 11 ++++------- 6 files changed, 32 insertions(+), 25 deletions(-) diff --git a/processing/post/permuteData.py b/processing/post/permuteData.py index c83b84b40..3492f9bb5 100755 --- a/processing/post/permuteData.py +++ b/processing/post/permuteData.py @@ -35,16 +35,19 @@ datainfo = { } datainfo['scalar']['label'] += options.label -np.random.seed(options.randomSeed) + # --- loop over input files ------------------------------------------------------------------------- for name in filenames: if not os.path.exists(name): continue file = {'name':name, 'input':open(name), 'output':open(name+'_tmp','w'), 'croak':sys.stderr} file['croak'].write('\033[1m'+scriptName+'\033[0m: '+file['name']+'\n') + randomSeed = int(os.urandom(4).encode('hex'), 16) if options.randomSeed == None else options.randomSeed # radom seed per file for second phase + np.random.seed(randomSeed) table = damask.ASCIItable(file['input'],file['output'],buffered=False) # make unbuffered ASCII_table table.head_read() # read ASCII header info table.info_append(scriptID + '\t' + ' '.join(sys.argv[1:])) + table.info_append('random seed %i'%randomSeed) # --------------- figure out columns to process --------------------------------------------------- active = [] @@ -63,10 +66,10 @@ for name in filenames: # ------------------------------------------ process data ------------------------------------------ permutation = {} table.data_readArray(active) - for i,label in enumerate(active): - mySeed = np.random.randint(9999999) - np.random.seed(mySeed) + + + for i,label in enumerate(active): unique = list(set(table.data[:,i])) permutated = np.random.permutation(unique) permutation[label] = dict(zip(unique,permutated)) @@ -76,9 +79,7 @@ for name in filenames: outputAlive = True while outputAlive and table.data_read(): # read next data line of ASCII table for label in active: # loop over all requested stiffnesses - for c in xrange(column[label], - column[label]+datainfo['scalar']['len']): - table.data[c] = permutation[label][float(table.data[c])] # apply permutation + table.data[column[label]] = permutation[label][float(table.data[column[label]])] # apply permutation outputAlive = table.data_write() # output processed line diff --git a/processing/pre/OIMlinear2linearODF.py b/processing/pre/OIMlinear2linearODF.py index c1df133ee..c887af903 100755 --- a/processing/pre/OIMlinear2linearODF.py +++ b/processing/pre/OIMlinear2linearODF.py @@ -23,7 +23,7 @@ Transform the binned texture data from "TSL OIM Analysis" into linear ODF data, """, version = scriptID) parser.add_option('-s', '--symmetry', dest='symmetry', choices=sampleSym.keys(), - help='Sample symmetry, the default is [%default]') + help='Sample symmetry {%s} [Triclinic]'%(' '.join(sampleSym.keys()))) parser.set_defaults(symmetry = 'Triclinic') diff --git a/processing/pre/geom_fromBarycentric.py b/processing/pre/geom_fromBarycentric.py index ef35cef26..53b63a4a2 100755 --- a/processing/pre/geom_fromBarycentric.py +++ b/processing/pre/geom_fromBarycentric.py @@ -205,7 +205,6 @@ if (args.seedsFile): args.grid[1], args.grid[2]) outstr += "microstructures {}\n".format(len(set(s_id))) - outstr += "randomSeed 0\n" outstr += "x\ty\tz\tmicrostructure" if (args.addRim): for i in range(len(s_id)): @@ -296,4 +295,4 @@ with open(args.geomFile, "w") as f: for row in mesh_ids: row = [str(item) for item in list(row)] outstr += "\t".join(row) + "\n" - f.write(outstr) \ No newline at end of file + f.write(outstr) diff --git a/processing/pre/geom_fromVoronoiTessellation.py b/processing/pre/geom_fromVoronoiTessellation.py index 4b02d02cf..dbd66c38c 100755 --- a/processing/pre/geom_fromVoronoiTessellation.py +++ b/processing/pre/geom_fromVoronoiTessellation.py @@ -105,6 +105,8 @@ parser.add_option('--crystallite', dest='crystallite', type='int', metavar = 'in help='crystallite index to be used [%default]') parser.add_option('-c', '--configuration', dest='config', action='store_true', help='output material configuration [%default]') +parser.add_option('-r', '--rnd', dest='randomSeed', type='int', metavar='int', + help='seed of random number generator for second phase distribution [%default]') parser.add_option('--secondphase', type='float', dest='secondphase', metavar= 'float', help='volume fraction of randomly distribute second phase [%default]') parser.add_option('--laguerre', dest='laguerre', action='store_true', @@ -118,9 +120,13 @@ parser.set_defaults(crystallite = 1) parser.set_defaults(secondphase = 0.0) parser.set_defaults(config = False) parser.set_defaults(laguerre = False) +parser.set_defaults(randomSeed = None) (options,filenames) = parser.parse_args() +if options.secondphase > 1.0 or options.secondphase < 0.0: + parser.error('volume fraction of second phase (%f) out of bounds...'%options.secondphase) + #--- setup file handles --------------------------------------------------------------------------- files = [] if filenames == []: @@ -239,9 +245,12 @@ for file in files: phase=np.empty(info['microstructures'],'i') phase.fill(options.phase) phase[0:int(float(info['microstructures'])*options.secondphase)] = options.phase+1 + randomSeed = int(os.urandom(4).encode('hex'), 16) if options.randomSeed == None else options.randomSeed # radom seed per file for second phase + np.random.seed(randomSeed) np.random.shuffle(phase) formatwidth = 1+int(math.log10(info['microstructures'])) - file['output'].write('#' + scriptID + ' ' + ' '.join(sys.argv[1:])) + file['output'].write('#' + scriptID + ' ' + ' '.join(sys.argv[1:])+'\n') + if options.secondphase > 0.0: file['output'].write('# random seed for second phase %i\n'%randomSeed) file['output'].write('\n\n') for i,ID in enumerate(grainIDs): file['output'].write('\n[Grain%s]\n'%(str(ID).zfill(formatwidth)) + \ @@ -278,9 +287,10 @@ for file in files: newInfo['microstructures'] = info['microstructures'] for i in grainIDs: if i not in indices: newInfo['microstructures'] -= 1 - file['croak'].write({True:'all',False:'only'}[newInfo['microstructures'] == info['microstructures'] ] + + file['croak'].write('all' if [newInfo['microstructures'] == info['microstructures'] ] else 'only' + ' %i'%newInfo['microstructures'] + - {True:'',False:' out of %i'%info['microstructures']}[newInfo['microstructures'] == info['microstructures']] + + '' if [newInfo['microstructures'] == info['microstructures']] else \ + 'out of %i'%info['microstructures'] + ' grains mapped.\n') #--- write header --------------------------------------------------------------------------------- diff --git a/processing/pre/hybridIA_linODFsampling.py b/processing/pre/hybridIA_linODFsampling.py index 107be0468..f5bcdf8f6 100755 --- a/processing/pre/hybridIA_linODFsampling.py +++ b/processing/pre/hybridIA_linODFsampling.py @@ -194,7 +194,7 @@ Transform linear binned data into Euler angles. """, version = scriptID) -parser.add_option('-n', '--number', dest='number', type='int', metavar = 'int', +parser.add_option('-n', '--nsamples', dest='number', type='int', metavar = 'int', help='number of orientations to be generated [%default]') parser.add_option('-a','--algorithm', dest='algorithm', type='string', metavar = 'string', help='sampling algorithm. IA: direct inversion, STAT: Van Houtte, MC: Monte Carlo. [%default].') #make choice @@ -204,7 +204,6 @@ parser.add_option('--crystallite', dest='crystallite', type='int', metavar = 'in help='crystallite index to be used [%default]') parser.add_option('-r', '--rnd', dest='randomSeed', type='int', metavar='int', \ help='seed of random number generator [%default]') - parser.set_defaults(randomSeed = None) parser.set_defaults(number = 500) parser.set_defaults(algorithm = 'IA') @@ -215,9 +214,6 @@ parser.set_defaults(crystallite = 1) nSamples = options.number methods = [options.algorithm] -if options.randomSeed == None: - options.randomSeed = int(os.urandom(4).encode('hex'), 16) -random.seed(options.randomSeed) #--- setup file handles --------------------------------------------------------------------------- files = [] @@ -234,6 +230,8 @@ for file in files: table = damask.ASCIItable(file['input'],file['output'],buffered = False) table.head_read() + randomSeed = int(os.urandom(4).encode('hex'), 16) if options.randomSeed == None else options.randomSeed # radom seed per file for second phase + random.seed(randomSeed) # --------------- figure out columns in table ----------- ----------------------------------------- column = {} @@ -334,6 +332,8 @@ for file in files: strOpt = '(%i)'%ODF['nNonZero'] formatwidth = 1 + file['output'].write('#' + scriptID + ' ' + ' '.join(sys.argv[1:])+'\n') + file['output'].write('# random seed %i\n'%randomSeed) file['output'].write('#-------------------#') file['output'].write('\n\n') file['output'].write('#-------------------#\n') diff --git a/processing/pre/seeds_fromRandom.py b/processing/pre/seeds_fromRandom.py index 4eb3f8356..4d11a6933 100755 --- a/processing/pre/seeds_fromRandom.py +++ b/processing/pre/seeds_fromRandom.py @@ -60,12 +60,9 @@ if gridSize == 0: if options.N > gridSize: file['croak'].write('accommodating only %i seeds on grid.\n'%gridSize) options.N = gridSize -if options.randomSeed == None: - options.randomSeed = int(os.urandom(4).encode('hex'), 16) - - -np.random.seed(options.randomSeed) # init random generators -random.seed(options.randomSeed) +randomSeed = int(os.urandom(4).encode('hex'), 16) if options.randomSeed == None else options.randomSeed +np.random.seed(randomSeed) # init random generators +random.seed(randomSeed) grainEuler = np.random.rand(3,options.N) # create random Euler triplets grainEuler[0,:] *= 360.0 # phi_1 is uniformly distributed @@ -107,7 +104,7 @@ header = ["5\theader", scriptID + " " + " ".join(sys.argv[1:]), "grid\ta {}\tb {}\tc {}".format(options.grid[0],options.grid[1],options.grid[2]), "microstructures\t{}".format(options.N), - "randomSeed\t{}".format(options.randomSeed), + "randomSeed\t{}".format(randomSeed), "%s"%labels, ]