consistent handling of random number per file and always reporting as comment, fix for permuteData needed because of new asciitable behavior

This commit is contained in:
Martin Diehl 2015-05-22 05:56:49 +00:00
parent fbfa55e910
commit 737e474cea
6 changed files with 32 additions and 25 deletions

View File

@ -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

View File

@ -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')

View File

@ -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)
f.write(outstr)

View File

@ -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<microstructure>\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 ---------------------------------------------------------------------------------

View File

@ -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<microstructure>\n')
file['output'].write('#-------------------#\n')

View File

@ -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,
]