def get_performance(task,image_hash,image_fs,model_config,convolve_func): stats = ['test_accuracy','ap','auc','mean_ap','mean_auc','train_accuracy'] classifier_kwargs = task.get('classifier_kwargs',{}) split_results = [] splits = generate_splits(task,image_hash) filterbank = filter_generation.get_filterbank(model_config) for (ind,split) in enumerate(splits): print ('split', ind) train_data = split['train_data'] test_data = split['test_data'] train_filenames = [t['filename'] for t in train_data] test_filenames = [t['filename'] for t in test_data] assert set(train_filenames).intersection(test_filenames) == set([]) train_features = sp.row_stack([extract_features(im, image_fs, filterbank, model_config, convolve_func) for im in train_data]) test_features = sp.row_stack([extract_features(im, image_fs, filterbank, model_config, convolve_func) for im in test_data]) train_labels = split['train_labels'] test_labels = split['test_labels'] res = svm.classify(train_features,train_labels,test_features,test_labels,**classifier_kwargs) split_results.append(res) model_results = SON([]) for stat in stats: if stat in split_results[0] and split_results[0][stat] != None: model_results[stat] = sp.array([split_result[stat] for split_result in split_results]).mean() return model_results, filterbank
def generate_split(config_path,tag,task_query,ntrain,ntest,ntrain_pos = None, universe = None): if universe is None: universe = {} data = DataCollection(config_path = config_path, steptag = tag) task_query.update(universe) task_data = list(data.find(task_query)) task_ids = [x['_id'] for x in task_data] N_task = len(task_data) nontask_query = {'_id':{'$nin':task_ids}} nontask_query.update(universe) nontask_data = list(data.find(nontask_query)) N_nontask = len(nontask_data) assert ntrain + ntest <= N_task + N_nontask, "Too many training and/or testing examples." if ntrain_pos is not None: ntrain_neg = ntrain - ntrain_pos assert ntrain_pos <= N_task assert ntrain_pos <= ntrain perm_pos = sp.random.permutation(len(task_data)) perm_neg = sp.random.permutation(len(nontask_data)) train_data = [task_data[i] for i in perm_pos[:ntrain_pos]] + [nontask_data[i] for i in perm_neg[:ntrain_neg]] all_test = [task_data[i] for i in perm_pos[ntrain_pos:]] + [nontask_data[i] for i in perm_neg[ntrain_neg:]] new_perm = sp.random.permutation(len(all_test)) test_data = [all_test[i] for i in new_perm[:ntest]] else: all_data = task_data + nontask_data perm = sp.random.permutation(len(all_data)) train_data = [all_data[i] for i in perm[:ntrain]] test_data = [all_data[i] for i in perm[ntrain:ntrain + ntest]] train_labels = sp.array([x['_id'] in task_ids for x in train_data]) test_labels = sp.array([x['_id'] in task_ids for x in test_data]) train_features = sp.row_stack([cPickle.loads(data.fs.get(r['_id']).read()) for r in train_data]) test_features = sp.row_stack([cPickle.loads(data.fs.get(r['_id']).read()) for r in test_data]) return {'train_data': train_data, 'test_data' : test_data, 'train_features' : train_features,'train_labels':train_labels,'test_features':test_features,'test_labels':test_labels}
def generate_multi_split(config_path,tag,queries,ntrain,ntest,ntrain_pos = None, universe = None): if universe is None: universe = {} for q in queries: q.update(universe) data = DataCollection(config_path = config_path, steptag = tag) task_data_list = [list(data.find(query)) for query in queries] task_id_list = [[(i,x['_id']) for x in X] for (i,X) in enumerate(task_data_list)] task_data = ListUnion(task_data_list) task_ids = validate(task_id_list) task_dist, task_ids = zip(*task_ids) task_dist = list(task_dist) ; task_ids = list(task_ids) nontask_query = {'_id':{'$nin':task_ids}} nontask_query.update(universe) nontask_data = list(data.find(nontask_query)) nontask_ids = [x['_id'] for x in nontask_data] all_ids = task_ids + nontask_ids all_data = task_data + nontask_data all_dist = task_dist + [len(queries)]*len(nontask_ids) assert ntrain + ntest <= len(all_ids) perm = sp.random.permutation(len(all_ids)) train_ids = [all_ids[i] for i in perm[:ntrain]] test_ids = [all_ids[i] for i in perm[ntrain:ntrain + ntest]] train_data = [all_data[i] for i in perm[:ntrain]] test_data = [all_data[i] for i in perm[ntrain:ntrain+ntest]] train_labels = sp.array([all_dist[i] for i in perm[:ntrain]]) test_labels = sp.array([all_dist[i] for i in perm[ntrain:ntrain+ntest]]) train_features = sp.row_stack([cPickle.loads(data.fs.get(r).read()) for r in train_ids]) test_features = sp.row_stack([cPickle.loads(data.fs.get(r).read()) for r in test_ids]) return {'train_data': train_data, 'test_data' : test_data, 'train_features' : train_features, 'train_labels':train_labels, 'test_features':test_features, 'test_labels':test_labels }
def Combinations(values, k): """This function outputs all possible combinations of k elements from the column vector values""" n = len(values) try: values = sp.row_stack(values) except: raise ValueError, "I need a 2d column array" if k > n: raise ValueError, "k must be <= %d" % n elif k <= 0 or k % 1 != 0: raise ValueError, "k must be > 0" #out = sp.array([],ndmin=2) if k == 1: return values else: #This loop iterates through all the elements of the values that have at least #k elements. For each element it then calls Combinations(values[i+1:], k-1) which #returns combinations of size k-1 for the elements succeeding the current element #We do not want to get repeats of combinations #print "for i in range(%d)" % (n-(k-1)) for i in range(n - (k - 1)): #Calculate the number of possible combinations (to allow proper concatenation #in the recursive call numCombs = sp.factorial(n - i) / (sp.factorial(k - 1) * sp.factorial(n - i - (k - 1))) combs = Combinations(values[i:], k - 1) ones = values[i] * sp.ones((numCombs, 1)) #print "ones: %s \t\t combs: %s" % (str(ones.shape), str(combs.shape)) print combs
def PathSPCA(A, k): M, N = A.shape # Loop through variables As = ((A * A).sum(axis=0)) vmax = As.max() vp = As.argmax() subset = [vp] vars = [] res = subset rhos = [(A[:, vp] * A[:, vp]).sum()] Stemp = array([rhos]) for i in range(1, k): lev, v = la.eig(Stemp) vars.append(real(lev).max()) vp = real(lev).argmax() x = dot(A[:, subset], v[:, vp]) x = x / la.norm(x) seto = list(range(0, N)) for j in subset: seto.remove(j) vals = dot(x.T, A[:, seto]) vals = vals * vals rhos.append(vals.max()) vpo = seto[vals.argmax()] Stemp = column_stack((Stemp, dot(A[:, subset].T, A[:, vpo]))) vbuf = append(dot(A[:, vpo].T, A[:, subset]), array([(A[:, vpo] * A[:, vpo]).sum()])) Stemp = row_stack((Stemp, vbuf)) subset.append(vpo) lev, v = la.eig(Stemp) vars.append(real(lev).max()) return vars, res, rhos
def Combinations(values, k): """This function outputs all possible combinations of k elements from the column vector values""" n = len(values) try: values = sp.row_stack(values) except: raise ValueError, "I need a 2d column array" if k > n: raise ValueError, "k must be <= %d" % n elif k<=0 or k%1 != 0: raise ValueError, "k must be > 0" #out = sp.array([],ndmin=2) if k == 1: return values else: #This loop iterates through all the elements of the values that have at least #k elements. For each element it then calls Combinations(values[i+1:], k-1) which #returns combinations of size k-1 for the elements succeeding the current element #We do not want to get repeats of combinations #print "for i in range(%d)" % (n-(k-1)) for i in range(n-(k-1)): #Calculate the number of possible combinations (to allow proper concatenation #in the recursive call numCombs = sp.factorial(n-i)/(sp.factorial(k-1)*sp.factorial(n-i-(k-1))) combs = Combinations(values[i:], k-1) ones = values[i]*sp.ones((numCombs,1)) #print "ones: %s \t\t combs: %s" % (str(ones.shape), str(combs.shape)) print combs
def extract_mode_values_for_ROM(self, eig): disp, angles, modedict = self.FindModeShape(eig) spring = modedict.bodies[2] accel = modedict.bodies[6] x_accel = accel['disps'] x_ddot = x_accel*eig**2 theta = spring['angles'] c = row_stack([x_ddot, theta]) return c
def run_clock(beta_file): """ Function to run clock given the path to a CSV file containing the normalised beta values """ probes = np.load("model/probes.npy") newx, samples = read_file(beta_file, probes) nbeta = np.load("model/nbeta.npy") result = scipy.dot(scipy.column_stack((scipy.ones([newx.shape[0], 1]), newx)), nbeta)[:, 0] return scipy.row_stack((samples, result.flatten()))
def bodes_to_string_matrix(freq, bode_list, labels): datalist = [freq] labellist = ['freq'] for bode, label in zip(bode_list, labels): dB = bode.dBmag() phase = bode.phase if label[0] != '_': label = '_' + label db_label = 'db' + label phase_label = 'phase' + label datalist.append(dB) datalist.append(phase) labellist.append(db_label) labellist.append(phase_label) data = column_stack(datalist) data_s = data.astype('S30') labels_m = row_stack([labellist]) data_out = row_stack([labels_m, data_s]) return data_out
def _RLFindRoots(sys, kvect): """Find the roots for the root locus.""" # Convert numerator and denominator to polynomials if they aren't (nump, denp) = _systopoly1d(sys) roots = [] for k in kvect: curpoly = denp + k * nump curroots = curpoly.r curroots.sort() roots.append(curroots) mymat = row_stack(roots) return mymat
def _RLFindRoots(nump, denp, kvect): """Find the roots for the root locus.""" # Convert numerator and denominator to polynomials if they aren't roots = [] for k in kvect: curpoly = denp + k * nump curroots = curpoly.r if len(curroots) < denp.order: # if I have fewer poles than open loop, it is because i have one at infinity curroots = np.insert(curroots, len(curroots), np.inf) curroots.sort() roots.append(curroots) mymat = row_stack(roots) return mymat
def teach_database_plusone(GP, X, y, X_t, y_t): # Force all data to be numpy arrays X, y = sp.asarray(X), sp.asarray(y) X_t, y_t = sp.asarray(X_t), sp.asarray(y_t) # From a fixed database (X,y), get alpha of some new configurations if added one at a time alphas = [] for i, (X_test, y_test) in enumerate(zip(X_t, y_t)): if y_test.size != 1: print "ERROR: output space must be 1D. Exiting..." return # Test configuration is placed at position 0 X_plus = sp.row_stack((X_test, X)) y_plus = sp.append(y_test, y) ttt = time.clock() GP.fit(X_plus, y_plus) print "TIMER teach", time.clock() - ttt alphas.append((gp.alpha[0]).flatten().copy()) GP.flush_data() return sp.array(alphas).flatten()
def main(f): i = sp.arange(1500, 2500 + 200, 200) y = [] with timer() as t: for n in i: if f == MatrixAdd: t.time(f, sp.rand(n, n), sp.rand(n, n)) elif f == MatrixSolve: t.time(f, sp.rand(n, n), sp.rand(n, 1)) else: t.time(f, sp.rand(n, n)) y.append(t.results[0][0]) X = sp.row_stack([sp.log(i), sp.ones_like(i)]) sol = la.lstsq(X.T, sp.log(y)) print sol[0][0] plt.loglog(i, y) plt.show()
def sample( self, n, words = 100 ): """Sample n samples from the topic model with the following process, (a) For each document, draw a particular topic (b) Draw w words at random from the topic """ # Draw the number of documents to be drawn for each topic cnts = multinomial( n, self.weights ) docs = [] for (k, cnt) in zip( xrange(self.K), cnts ): # For each document of type k for i in xrange( cnt ): # Generate a document with `words` words from the # topic docs.append( multinomial( words, self.topics.T[k] ) ) # Shuffle all of the data (not really necessary) docs = sc.row_stack(docs) sc.random.shuffle(docs) return docs
def teach_database_plusone(self, X, y, X_t, y_t): """ Gaussian Process model fitting, target is to get the correct regression coefficients for each of the configurations (X_i, y_i) i \ in t if configuration i were included in the teaching. Parameters ---------- X, X_t : double array_like An array with shape (n_samples, n_features) with the input at which observations were made. y, y_t : double array_like An array with shape (n_samples, ) or shape (n_samples, n_targets) with the observations of the output to be predicted. Returns ------- alpha: an array of regression coefficients with shape (n_samples, ). """ self.flush_data() # Force all data to be numpy arrays X, y = sp.asarray(X), sp.asarray(y) X_t, y_t = sp.asarray(X_t), sp.asarray(y_t) # From a fixed database (X,y), get alpha of some new configurations if added one at a time alphas = [] for i, (X_test, y_test) in enumerate(zip(X_t, y_t)): if y_test.size != 1: print "ERROR: output space must be 1D. Exiting..." return # Test configuration is placed at position 0 X_plus = sp.row_stack((X_test, X)) y_plus = sp.append(y_test, y) self.fit(X_plus, y_plus) alphas.append((self.alpha[0]).flatten().copy()) self.flush_data() return sp.array(alphas).flatten()
def generate_Pole(cleaned, file_name, scantype, line_count, state_indv, state_angmat, state_xyz, start, stop, start2, stop2): line_count = int(line_count) start = int(start) stop = int(stop) start2 = int(start2) stop2 = int(stop2) khi = cleaned[:,1] phi = cleaned[:,2] scanning = cleaned[:,8] intensity = cleaned[:,9] bkg = intensity[intensity!=0].min() # Determine azimuth and zenith scanning motors if (scanning[::int(line_count)]-khi[::int(line_count)]).sum()!=0: azimuth = scanning zenith = khi stepa = (azimuth.max()-azimuth.min())/(line_count-1) stepz = (zenith.max()-zenith.min())/((len(zenith)/line_count)-1) fast_phi = 1 print("Phi is the fast scanning motor.") #elif (scanning[::line_count]-khi[::line_count]).sum()==0: else: zenith = scanning azimuth = phi stepa = (azimuth.max()-azimuth.min())/((len(zenith)/line_count)-1) stepz = (zenith.max()-zenith.min())/(line_count-1) fast_phi = 0 print("Khi is the fast scanning motor.") #else: #return 0 # Check data validity if ((azimuth[1:]-azimuth[:-1]).sum() == 0) and ((zenith[1:]-zenith[:-1]).sum() == 0): status = 0 else: status = 1 # Crop data to desired dimensions if ((start!=0) or (stop!=0) or (start2!=0) or (stop2!=0)): intensity = crop_data(intensity, line_count, start, stop, start2, stop2) azimuth = crop_data(azimuth, line_count, start, stop, start2, stop2) zenith = crop_data(zenith, line_count, start, stop, start2, stop2) line_count = line_count - start - stop #Export data files if state_xyz == 1: savetxt(file_name + '_ChiPhiInt.txt', column_stack((zenith,azimuth, intensity)), fmt = '%10.8f') #Convert to matrix for further processing if (azimuth[1] != azimuth[0]): azimuth = azimuth[:int(line_count):] zenith = zenith[::int(line_count)] else: azimuth = azimuth[::int(line_count)] zenith = zenith[:int(line_count):] int_matrix = intensity.reshape(int(len(intensity)/line_count), int(line_count)) if state_indv == 1: for i in range(shape(int_matrix)[0]): if fast_phi == 1: out_scan = column_stack((azimuth, int_matrix[i,:])) savetxt(file_name + "_Scan"+str(i+1)+" PSI="+str(round(zenith[i],2))+".txt", out_scan, fmt = '%10.8f') else : out_scan = column_stack((zenith, int_matrix[i,:])) savetxt(file_name + "_Scan"+str(i+1)+" PHI="+str(round(azimuth[i],2))+".txt", out_scan, fmt = '%10.8f') if state_angmat == 1: if fast_phi == 1: out_matrix = column_stack((zenith, int_matrix)) else: out_matrix = column_stack((zenith, int_matrix.T)) out_matrix = row_stack((append([0], azimuth), out_matrix)) savetxt(file_name + '_ChiPhi_matrix.txt', out_matrix.T, fmt = '%10.8f') r, theta = meshgrid(zenith, azimuth*pi/180) plt.ion() fig, ax = plt.subplots(subplot_kw=dict(projection='polar')) if fast_phi == 1: ax.contourf(theta, r, log10(int_matrix.T+bkg), 25, cmap='jet') else: ax.contourf(theta, r, log10(int_matrix+bkg), 25, cmap='jet') plt.show() return status
def glmnetPredict(fit,\ newx = scipy.empty([0]), \ s = scipy.empty([0]), \ ptype = 'link', \ exact = False, \ offset = scipy.empty([0])): typebase = ['link', 'response', 'coefficients', 'nonzero', 'class'] indxtf = [x.startswith(ptype.lower()) for x in typebase] indl = [i for i in range(len(indxtf)) if indxtf[i] == True] ptype = typebase[indl[0]] if newx.shape[0] == 0 and ptype != 'coefficients' and ptype != 'nonzero': raise ValueError('You need to supply a value for ' 'newx' '') # python 1D arrays are not the same as matlab 1xn arrays # check for this. newx = x[0:1, :] is a python 2D array and would work; # but newx = x[0, :] is a python 1D array and should not be passed into # glmnetPredict if len(newx.shape) == 1 and newx.shape[0] > 0: raise ValueError('newx must be a 2D (not a 1D) python array') if exact == True and len(s) > 0: # It is very messy to go back into the caller namespace # and call glmnet again. The user should really do this at their end # by calling glmnet again using the correct array of lambda values that # includes the lambda for which prediction is sought raise NotImplementedError( 'exact = True option is not implemented in python') # we convert newx to full here since sparse and full operations do not seem to # be overloaded completely in scipy. if scipy.sparse.issparse(newx): newx = newx.todense() # elnet if fit['class'] in ['elnet', 'fishnet', 'lognet']: if fit['class'] == 'lognet': a0 = fit['a0'] else: a0 = scipy.transpose(fit['a0']) a0 = scipy.reshape(a0, [1, a0.size]) # convert to 1 x N for appending nbeta = scipy.row_stack((a0, fit['beta'])) if scipy.size(s) > 0: lambdau = fit['lambdau'] lamlist = lambda_interp(lambdau, s) nbeta = nbeta[:, lamlist['left']]*scipy.tile(scipy.transpose(lamlist['frac']), [nbeta.shape[0], 1]) \ + nbeta[:, lamlist['right']]*( 1 - scipy.tile(scipy.transpose(lamlist['frac']), [nbeta.shape[0], 1])) if ptype == 'coefficients': result = nbeta return (result) if ptype == 'nonzero': result = nonzeroCoef(nbeta[1:nbeta.shape[0], :], True) return (result) # use scipy.sparse.hstack instead of column_stack for sparse matrices result = (scipy.column_stack((scipy.ones([newx.shape[0], 1]), newx)), nbeta) if fit['offset']: if len(offset) == 0: raise ValueError( 'No offset provided for prediction, yet used in fit of glmnet' ) if offset.shape[1] == 2: offset = offset[:, 1] result = result + scipy.tile(offset, [1, result.shape[1]]) # fishnet if fit['class'] == 'fishnet' and ptype == 'response': result = scipy.exp(result) # lognet if fit['class'] == 'lognet': if ptype == 'response': pp = scipy.exp(-result) result = 1 / (1 + pp) elif ptype == 'class': result = (result > 0) * 1 + (result <= 0) * 0 result = fit['label'][result] # multnet / mrelnet if fit['class'] == 'mrelnet' or fit['class'] == 'multnet': if fit['class'] == 'mrelnet': if type == 'response': ptype = 'link' fit['grouped'] = True a0 = fit['a0'] nbeta = fit['beta'].copy() nclass = a0.shape[0] nlambda = s.size if len(s) > 0: lambdau = fit['lambdau'] lamlist = lambda_interp(lambdau, s) for i in range(nclass): kbeta = scipy.row_stack((a0[i, :], nbeta[i])) kbeta = kbeta[:, lamlist['left']]*scipy.tile(scipy.transpose(lamlist['frac']), [kbeta.shape[0], 1]) \ + kbeta[:, lamlist['right']]*( 1 - scipy.tile(scipy.transpose(lamlist['frac']), [kbeta.shape[0], 1])) nbeta[i] = kbeta else: for i in range(nclass): nbeta[i] = scipy.row_stack((a0[i, :], nbeta[i])) nlambda = len(fit['lambdau']) if ptype == 'coefficients': result = nbeta return (result) if ptype == 'nonzero': if fit['grouped']: result = list() tn = nbeta[0].shape[0] result.append(nonzeroCoef(nbeta[0][1:tn, :], True)) else: result = list() for i in range(nclass): tn = nbeta[0].shape[0] result.append(nonzeroCoef(nbeta[0][1:tn, :], True)) return (result) npred = newx.shape[0] dp = scipy.zeros([nclass, nlambda, npred], dtype=scipy.float64) for i in range(nclass): qq = scipy.column_stack((scipy.ones([newx.shape[0], 1]), newx)) fitk = scipy.dot(qq, nbeta[i]) dp[i, :, :] = dp[i, :, :] + scipy.reshape(scipy.transpose(fitk), [1, nlambda, npred]) if fit['offset']: if len(offset) == 0: raise ValueError( 'No offset provided for prediction, yet used in fit of glmnet' ) if offset.shape[1] != nclass: raise ValueError('Offset should be dimension %d x %d' % (npred, nclass)) toff = scipy.transpose(offset) for i in range(nlambda): dp[:, i, :] = dp[:, i, :] + toff if ptype == 'response': pp = scipy.exp(dp) psum = scipy.sum(pp, axis=0, keepdims=True) result = scipy.transpose(pp / scipy.tile(psum, [nclass, 1, 1]), [2, 0, 1]) if ptype == 'link': result = scipy.transpose(dp, [2, 0, 1]) if ptype == 'class': dp = scipy.transpose(dp, [2, 0, 1]) result = list() for i in range(dp.shape[2]): t = softmax(dp[:, :, i]) result = scipy.append(result, fit['label'][t['pclass']]) # coxnet if fit['class'] == 'coxnet': nbeta = fit['beta'] if len(s) > 0: lambdau = fit['lambdau'] lamlist = lambda_interp(lambdau, s) nbeta = nbeta[:, lamlist['left']]*scipy.tile(scipy.transpose(lamlist['frac']), [nbeta.shape[0], 1]) \ + nbeta[:, lamlist['right']]*( 1 - scipy.tile(scipy.transpose(lamlist['frac']), [nbeta.shape[0], 1])) if ptype == 'coefficients': result = nbeta return (result) if ptype == 'nonzero': result = nonzeroCoef(nbeta, True) return (result) result = scipy.dot(newx, nbeta) if fit['offset']: if len(offset) == 0: raise ValueError( 'No offset provided for prediction, yet used in fit of glmnet' ) result = result + scipy.tile(offset, [1, result.shape[1]]) if ptype == 'response': result = scipy.exp(result) return (result)
if target_properties == 'all': target_properties = [k for k in dataset.keys() if k not in ['description', 'idx', 'X']] else: target_properties = [k for k in dataset.keys() if k in target_properties] # histograms of properties histograms = [] # sp.empty((len(target_properties), 2)) for i, prop in enumerate(target_properties): y = (dataset[prop] - sp.asarray(dataset[prop]).mean()) / sp.asarray(dataset[prop]).std() frequency, bins, patches = plt.hist(y, bins=50, normed=True) bins_dummy = list(bins) bins_dummy.append(bins_dummy.pop(0)) bins = ((bins + sp.asarray(bins_dummy)) / 2)[:-1] histograms.append(sp.row_stack((bins, frequency))) plt.clf() for i, h in enumerate(histograms): plt.plot(h[0], h[1],'--', label=target_properties[i]) plt.xlabel("normalised property") plt.ylabel("frequency") legend = plt.legend() plt.show() raw_input("Press a key to continue") plt.clf() # Output space pair distance histograms histograms = [] for i, prop in enumerate(target_properties):
target_properties = [ k for k in dataset.keys() if k not in ['description', 'idx', 'X'] ] else: target_properties = [k for k in dataset.keys() if k in target_properties] # histograms of properties histograms = [] # sp.empty((len(target_properties), 2)) for i, prop in enumerate(target_properties): y = (dataset[prop] - sp.asarray(dataset[prop]).mean()) / sp.asarray( dataset[prop]).std() frequency, bins, patches = plt.hist(y, bins=50, normed=True) bins_dummy = list(bins) bins_dummy.append(bins_dummy.pop(0)) bins = ((bins + sp.asarray(bins_dummy)) / 2)[:-1] histograms.append(sp.row_stack((bins, frequency))) plt.clf() for i, h in enumerate(histograms): plt.plot(h[0], h[1], '--', label=target_properties[i]) plt.xlabel("normalised property") plt.ylabel("frequency") legend = plt.legend() plt.show() raw_input("Press a key to continue") plt.clf() # Output space pair distance histograms histograms = [] for i, prop in enumerate(target_properties):
latticeTypes = {0:"Unknown", 1:"Triclinic", 2:"Monoclinic", 3:"Orthorhombic", \ 4:"Tetragonal", 5:"Cubic", 6:"Trigonal-low", 7:"Trigonal-high/Hexagonal"} symmetryType = latticeTypes[int(latticeType)] magnitude = float(cijdat.readline()) for patt in range(1): for a in range(0,numsteps): pattern =cijdat.readline() line1 = cijdat.readline().split() line2 = cijdat.readline().split() line3 = cijdat.readline().split() if a == 0: strain = S.array([float(line1[0]),float(line2[1]),float(line3[2]),2*float(line2[2]),2*float(line1[2]),2*float(line1[1])]) else: strain = S.row_stack(S.array((strain,[float(line1[0]),float(line2[1]),float(line3[2]),2*float(line2[2]),2*float(line1[2]),2*float(line1[1])]))) (units, thisStress) = espresso.get_stress("MgO_cij__"+str(patt+1)+"__"+str(a+1)) if a == 0: stress = thisStress else: stress = S.row_stack((stress,thisStress)) def __fit(index1,index2): from scipy import stats, sqrt, square print strain print stress (cijFitted,intercept,r,tt,stderr) = stats.linregress(strain[:,index2-1],stress[:,index1-1]) if (S.__version__ < '0.7.0'): stderr = S.sqrt((numsteps * stderr**2)/(numsteps-2)) error = stderr/sqrt(sum(square(strain[:,index2-1]))) else:
mod_time = state_estimation['mod_time'] mod_out = state_estimation['mod_out'] val_data = state_estimation['val_data'] val_time = state_estimation['val_time'] m = np.size(X, 0) # number of states N = np.size(X, 1) # number of measurements meas_list = range(N) # list of measurement indices x1 = mod_data[0] x2 = mod_data[1] bias = netRBF.biases biases = np.ones((1, len(x1))) Xeval = np.row_stack((x1, x2, biases)) if bias else np.row_stack((x1, x2)) #netRBF.NhidL = 1 if netRBF.NhidL == 1: if not netRBF.distr: center_x = np.linspace(min(x1), max(x1), 4) center_y = np.linspace(min(x2), max(x2), 4) centersx, centersy = np.meshgrid(center_x, center_y) size_centers = np.size(centersx) centersx = np.reshape(centersx, (np.size(centersx))) centersy = np.reshape(centersy, (np.size(centersy))) netRBF.centers = np.zeros((3, size_centers)) if bias else np.zeros( (2, size_centers)) netRBF.Nhid = size_centers
def evaluate(outfile,feature_certificate,cpath,task,ext_hash): conn = pm.Connection(document_class=bson.SON) db = conn[DB_NAME] perf_fs = gridfs.GridFS(db,'performance') perf_coll = db['performance.files'] remove_existing(perf_coll,perf_fs,ext_hash) feature_certdict = cPickle.load(open(feature_certificate)) feature_hash = feature_certdict['feature_hash'] image_hash = feature_certdict['image_hash'] model_hash = feature_certdict['model_hash'] image_config_gen = feature_certdict['args']['images'] model_col = db['models.files'] feature_fs = gridfs.GridFS(db,'features') feature_col = db['features.files'] stats = ['test_accuracy','ap','auc','mean_ap','mean_auc','train_accuracy'] if isinstance(task,list): task_list = task else: task_list = [task] model_configs = get_most_recent_files(model_col,{'__hash__':model_hash}) for m in model_configs: print('Evaluating model',m) for task in task_list: task['universe'] = task.get('universe',SON([])) task['universe']['model'] = m['config']['model'] print('task', task) classifier_kwargs = task.get('classifier_kwargs',{}) split_results = [] splits = generate_splits(task,feature_hash,'features') for (ind,split) in enumerate(splits): print ('split', ind) train_data = split['train_data'] test_data = split['test_data'] train_filenames = [t['filename'] for t in train_data] test_filenames = [t['filename'] for t in test_data] assert set(train_filenames).intersection(test_filenames) == set([]) print('train feature extraction ...') train_features = sp.row_stack([load_features(f['filename'],feature_fs,m,task) for f in train_data]) print('test feature extraction ...') test_features = sp.row_stack([load_features(f['filename'],feature_fs,m,task) for f in test_data]) train_labels = split['train_labels'] test_labels = split['test_labels'] print('classifier ...') res = svm.classify(train_features,train_labels,test_features,test_labels,classifier_kwargs) print('Split test accuracy', res['test_accuracy']) split_results.append(res) model_results = SON([]) for stat in STATS: if stat in split_results[0] and split_results[0][stat] != None: model_results[stat] = sp.array([split_result[stat] for split_result in split_results]).mean() out_record = SON([('model',m['config']['model']), ('model_hash',model_hash), ('model_filename',m['filename']), ('images',son_escape(image_config_gen)), ('image_hash',image_hash), ('task',son_escape(task)), ]) filename = get_filename(out_record) out_record['filename'] = filename out_record['config_path'] = cpath out_record['__hash__'] = ext_hash out_record.update(model_results) print('dump out ...') out_data = cPickle.dumps(SON([('split_results',split_results),('splits',splits)])) perf_fs.put(out_data,**out_record) createCertificateDict(outfile,{'feature_file':feature_certificate})
def main(input_options, libmode=False): def dumpXML(seedname): import time S.set_printoptions(threshold=S.nan) #don't skip printing parts of the array if os.path.isfile(seedname+"-geomopt1.cml"): # we'll inject the data into this file, then basefile = seedname+"-geomopt1.cml" print "Inserting CML into: "+ basefile f = open(basefile, "r") tree = etree.parse(f) f.close() fp = d["{http://www.castep.org/cml/dictionary/}finalProperties"] finalproperties = fp.findin_context(tree) assert len(finalproperties) == 1 startnode = finalproperties[0] elif os.path.isfile(seedname+"-energy.cml"): # we'll inject the data into this file, then basefile = seedname+"-energy.cml" print "Inserting CML into: "+ basefile f = open(basefile, "r") tree = etree.parse(f) f.close() fp = d["{http://www.castep.org/cml/dictionary/}finalProperties"] finalproperties = fp.findin_context(tree) assert len(finalproperties) == 1 startnode = finalproperties[0] else: basefile = seedname+"_cij_analysis.cml" print "Outputting CML into: "+ basefile # create a header CML_NAMESPACE = "http://www.xml-cml.org/schema" DC_NAMESPACE ="http://purl.org/dc/elements/1.1/" CML = "{%s}" % CML_NAMESPACE NSMAP = {None : CML_NAMESPACE, "dc" : DC_NAMESPACE} startnode = etree.Element(CML +"cml", nsmap=NSMAP) metadata = etree.Element("metadata") metadata.attrib["name"] = "dc:date" metadata.attrib["content"] = time.strftime("%Y-%m-%dT%H:%M:%S") startnode.append(metadata) metadata = etree.Element("metadata") metadata.attrib["name"] = "dc:creator" metadata.attrib["content"] = "MGElastics" startnode.append(metadata) metadata = etree.Element("metadata") metadata.attrib["name"] = "dc:hasVersion" metadata.attrib["content"] = str(version) startnode.append(metadata) metadata = etree.Element("metadata") metadata.attrib["name"] = "dc:subject" metadata.attrib["content"] = "Analysis file for Cij calculations" startnode.append(metadata) tree = etree.ElementTree(startnode) # now append Cij data cijnode = etree.Element("propertyList") cijnode.attrib["title"] = "MaterialsGrid: Elastic Properties" cijnode.attrib["dictRef"] = "castep:elastic_properties" startnode.append(cijnode) cijProp = etree.Element("property") cijProp.attrib["dictRef"] = "castep:elastic_stiffness_constants" cijProp.attrib["title"] = "Elastic Stiffness Constants" cijnode.append(cijProp) cijTensor = etree.Element("matrix") cijTensor.attrib["rows"] = "6" cijTensor.attrib["columns"] = "6" cijTensor.attrib["dataType"] = "xsd:double" cijTensor.attrib["units"] = "castepunits:"+ units.lower() cijTensor.text = S.array2string(finalCijMatrix.reshape(1,36)[0],max_line_width=1000,suppress_small=True).strip("[] ") cijProp.append(cijTensor) sijProp = etree.Element("property") sijProp.attrib["dictRef"] = "castep:elastic_compliance_constants" sijProp.attrib["title"] = "Elastic Compliance Constants" cijnode.append(sijProp) sijTensor = etree.Element("matrix") sijTensor.attrib["rows"] = "6" sijTensor.attrib["columns"] = "6" sijTensor.attrib["dataType"] = "xsd:double" sijTensor.attrib["units"] = "castepunits:"+ units.lower()+"-1" #this has to be changed - i don't think the '/' is allowed sijTensor.text = S.array2string(sij.reshape(1,36)[0],max_line_width=1000,suppress_small=True).strip("[] ") sijProp.append(sijTensor) #### Young's Moduli #### youngsMod = etree.Element("propertyList") youngsMod.attrib["dictRef"] = "castep:youngs_moduli" youngsMod.attrib["title"] = "Young's Moduli" cijnode.append(youngsMod) # X youngsModValX = etree.Element("property") youngsModValX.attrib["title"] = "Young's Modulus X" youngsModValX.attrib["dictRef"] = "castep:young_x" youngsMod.append(youngsModValX) youngsModValXScalar = etree.Element("scalar") youngsModValXScalar.attrib["dataType"] = "xsd:double" youngsModValXScalar.attrib["units"] = "castepunits:"+ units.lower() youngsModValXScalar.text = str(youngX) youngsModValX.append(youngsModValXScalar) # Y youngsModValY = etree.Element("property") youngsModValY.attrib["title"] = "Young's Modulus Y" youngsModValY.attrib["dictRef"] = "castep:young_y" youngsMod.append(youngsModValY) youngsModValYScalar = etree.Element("scalar") youngsModValYScalar.attrib["dataType"] = "xsd:double" youngsModValYScalar.attrib["units"] = "castepunits:"+ units.lower() youngsModValYScalar.text = str(youngY) youngsModValY.append(youngsModValYScalar) # Z youngsModValZ = etree.Element("property") youngsModValZ.attrib["title"] = "Young's Modulus Z" youngsModValZ.attrib["dictRef"] = "castep:young_z" youngsMod.append(youngsModValZ) youngsModValZScalar = etree.Element("scalar") youngsModValZScalar.attrib["dataType"] = "xsd:double" youngsModValZScalar.attrib["units"] = "castepunits:"+ units.lower() youngsModValZScalar.text = str(youngZ) youngsModValZ.append(youngsModValZScalar) #### Poisson's Ratio #### poissonMod = etree.Element("propertyList") poissonMod.attrib["dictRef"] = "castep:poisson_ratio" poissonMod.attrib["title"] = "Poisson Ratio" cijnode.append(poissonMod) # XY poissonModValXY = etree.Element("property") poissonModValXY.attrib["title"] = "Poisson Ratio XY" poissonModValXY.attrib["dictRef"] = "castep:poisson_xy" poissonModValXY.attrib["units"] = "castepunits:dimensionless" poissonMod.append(poissonModValXY) poissonModValXYScalar = etree.Element("scalar") poissonModValXYScalar.attrib["dataType"] = "xsd:double" poissonModValXYScalar.attrib["units"] = "castepunits:dimensionless" poissonModValXYScalar.text = str(poissonXY) poissonModValXY.append(poissonModValXYScalar) # XZ poissonModValXZ = etree.Element("property") poissonModValXZ.attrib["title"] = "Poisson Ratio XZ" poissonModValXZ.attrib["dictRef"] = "castep:poisson_xz" poissonMod.append(poissonModValXZ) poissonModValXZScalar = etree.Element("scalar") poissonModValXZScalar.attrib["dataType"] = "xsd:double" poissonModValXZScalar.attrib["units"] = "castepunits:dimensionless" poissonModValXZScalar.text = str(poissonXZ) poissonModValXZ.append(poissonModValXZScalar) # YX poissonModValYX = etree.Element("property") poissonModValYX.attrib["title"] = "Poisson Ratio YX" poissonModValYX.attrib["dictRef"] = "castep:poisson_yx" poissonMod.append(poissonModValYX) poissonModValYXScalar = etree.Element("scalar") poissonModValYXScalar.attrib["dataType"] = "xsd:double" poissonModValYXScalar.attrib["units"] = "castepunits:dimensionless" poissonModValYXScalar.text = str(poissonYX) poissonModValYX.append(poissonModValYXScalar) # YZ poissonModValYZ = etree.Element("property") poissonModValYZ.attrib["title"] = "Poisson Ratio YZ" poissonModValYZ.attrib["dictRef"] = "castep:poisson_yz" poissonMod.append(poissonModValYZ) poissonModValYZScalar = etree.Element("scalar") poissonModValYZScalar.attrib["dataType"] = "xsd:double" poissonModValYZScalar.attrib["units"] = "castepunits:dimensionless" poissonModValYZScalar.text = str(poissonYZ) poissonModValYZ.append(poissonModValYZScalar) # ZX poissonModValZX = etree.Element("property") poissonModValZX.attrib["title"] = "Poisson Ratio ZX" poissonModValZX.attrib["dictRef"] = "castep:poisson_zx" poissonMod.append(poissonModValZX) poissonModValZXScalar = etree.Element("scalar") poissonModValZXScalar.attrib["dataType"] = "xsd:double" poissonModValZXScalar.attrib["units"] = "castepunits:dimensionless" poissonModValZXScalar.text = str(poissonZX) poissonModValZX.append(poissonModValZXScalar) # ZY poissonModValZY = etree.Element("property") poissonModValZY.attrib["title"] = "Poisson Ratio ZY" poissonModValZY.attrib["dictRef"] = "castep:poisson_zy" poissonMod.append(poissonModValZY) poissonModValZYScalar = etree.Element("scalar") poissonModValZYScalar.attrib["dataType"] = "xsd:double" poissonModValZYScalar.attrib["units"] = "castepunits:dimensionless" poissonModValZYScalar.text = str(poissonZY) poissonModValZY.append(poissonModValZYScalar) #### Polycrystalline Results #### #bulk moduli bulkModPL = etree.Element("propertyList") bulkModPL.attrib["dictRef"] = "castep:polycrystalline_bulk_moduli" bulkModPL.attrib["title"] = "Polycrystalline Bulk Moduli" cijnode.append(bulkModPL) bulkModVoigt = etree.Element("property") bulkModVoigt.attrib["title"] = "Voigt" bulkModVoigt.attrib["dictRef"] = "castep:bulk_modulus_voigt" bulkModPL.append(bulkModVoigt) bulkModReuss = etree.Element("property") bulkModReuss.attrib["title"] = "Reuss" bulkModReuss.attrib["dictRef"] = "castep:bulk_modulus_reuss" bulkModPL.append(bulkModReuss) bulkModHill = etree.Element("property") bulkModHill.attrib["title"] = "Hill" bulkModHill.attrib["dictRef"] = "castep:bulk_modulus_hill" bulkModPL.append(bulkModHill) bulkModVoigtScalar = etree.Element("scalar") bulkModVoigtScalar.attrib["dataType"] = "xsd:double" bulkModVoigtScalar.attrib["units"] = "castepunits:"+ units.lower() bulkModVoigtScalar.text = str(voigtB) bulkModVoigt.append(bulkModVoigtScalar) bulkModReussScalar = etree.Element("scalar") bulkModReussScalar.attrib["dataType"] = "xsd:double" bulkModReussScalar.attrib["units"] = "castepunits:"+ units.lower() bulkModReussScalar.text = str(reussB) bulkModReuss.append(bulkModReussScalar) bulkModHillScalar = etree.Element("scalar") bulkModHillScalar.attrib["dataType"] = "xsd:double" bulkModHillScalar.attrib["units"] = "castepunits:"+ units.lower() bulkModHillScalar.text = str((voigtB+reussB)/2) bulkModHill.append(bulkModHillScalar) #shear moduli shearModPL = etree.Element("propertyList") shearModPL.attrib["dictRef"] = "castep:polycrystalline_shear_moduli" shearModPL.attrib["title"] = "Polycrystalline Shear Moduli" cijnode.append(shearModPL) shearModVoigt = etree.Element("property") shearModVoigt.attrib["title"] = "Voigt" shearModVoigt.attrib["dictRef"] = "castep:shear_modulus_voigt" shearModPL.append(shearModVoigt) shearModReuss = etree.Element("property") shearModReuss.attrib["title"] = "Reuss" shearModReuss.attrib["dictRef"] = "castep:shear_modulus_reuss" shearModPL.append(shearModReuss) shearModHill = etree.Element("property") shearModHill.attrib["title"] = "Hill" shearModHill.attrib["dictRef"] = "castep:shear_modulus_hill" shearModPL.append(shearModHill) shearModVoigtScalar = etree.Element("scalar") shearModVoigtScalar.attrib["dataType"] = "xsd:double" shearModVoigtScalar.attrib["units"] = "castepunits:"+ units.lower() shearModVoigtScalar.text = str(voigtG) shearModVoigt.append(shearModVoigtScalar) shearModReussScalar = etree.Element("scalar") shearModReussScalar.attrib["dataType"] = "xsd:double" shearModReussScalar.attrib["units"] = "castepunits:"+ units.lower() shearModReussScalar.text = str(reussG) shearModReuss.append(shearModReussScalar) shearModHillScalar = etree.Element("scalar") shearModHillScalar.attrib["dataType"] = "xsd:double" shearModHillScalar.attrib["units"] = "castepunits:"+ units.lower() shearModHillScalar.text = str((voigtG+reussG)/2) shearModHill.append(shearModHillScalar) if(options.debug): print etree.tostring(startnode,pretty_print=True) else: # wrap it in an ElementTree instance, and save as XML tree.write(basefile,pretty_print=True) return def analysePatterns(strain): # these are the IRE conventions, except that integers are 0->5 rather than 1->6 strainDict = {0:"xx",1:"yy",2:"zz",3:"yz", 4:"zx", 5:"xy"} strainsUsed = S.zeros((6,1)) for a in range(0,S.size(strain)): if strain[a] != 0.0: print strainDict[a], "component is non-zero" strainsUsed[a] = 1 else: strainsUsed[a] = 0 return strainsUsed def getCMLStress(file): def __castep(dict, term): return dict["{http://www.castep.org/cml/dictionary/}%s" % term] stress = __castep(d, "stress") tree = etree.parse(file) stress_xml = stress.findin(tree) assert len(stress_xml) == 1 stressTensor = stress.getvalue(stress_xml[0]) return stressTensor, stressTensor.unit def cMatrix(symmetryType,TetrHigh): if symmetryType == "Cubic" : return S.matrix([[1, 7, 7, 0, 0, 0], [7, 1, 7, 0, 0, 0], [7, 7, 1, 0, 0, 0], [0, 0, 0, 4, 0, 0], [0, 0, 0, 0, 4, 0], [0, 0, 0, 0, 0, 4]]) elif symmetryType == "Trigonal-high/Hexagonal": return S.matrix([[1, 7, 8, 9, 10, 0], [7, 1, 8, 0,-9, 0], [8, 8, 3, 0, 0, 0], [9, -9, 0, 4, 0, 0], [10, 0, 0, 0, 4, 0], [0, 0, 0, 0, 0, 6]]) elif symmetryType == "Trigonal-low": return S.matrix([[1, 7, 8, 9, 10, 0], [7, 1, 8, -9, -10, 0], [8, 8, 3, 0, 0, 0], [9, -9, 0, 4, 0, -10], [10,-10, 0, 0, 4, 9], [0, 0, 0, -10, 9, 6]]) elif symmetryType == "Tetragonal": if TetrHigh == "-1": print "Higher-symmetry tetragonal (422,4mm,4-2m,4/mmm)" return S.matrix([[1, 7, 8, 0, 0, 0], [7, 1, 8, 0, 0, 0], [8, 8, 3, 0, 0, 0], [0, 0, 0, 4, 0, 0], [0, 0, 0, 0, 4, 0], [0, 0, 0, 0, 0, 6]]) else: print "Lower-symmetry tetragonal (4,-4,4/m)" return S.matrix([[1, 7, 8, 0, 0, 11], [7, 1, 8, 0, 0, -11], [8, 8, 3, 0, 0, 0], [0, 0, 0, 4, 0, 0], [0, 0, 0, 0, 4, 0], [11, -11, 0, 0, 0, 6]]) elif symmetryType == "Orthorhombic": return S.matrix([[ 1, 7, 8, 0, 0, 0], [ 7, 2, 12, 0, 0, 0], [ 8, 12, 3, 0, 0, 0], [ 0, 0, 0, 4, 0, 0], [ 0, 0, 0, 0, 5, 0], [ 0, 0, 0, 0, 0, 6]]) elif symmetryType == "Monoclinic": return S.matrix([[ 1, 7, 8, 0, 10, 0], [ 7, 2, 12, 0, 14, 0], [ 8, 12, 3, 0, 17, 0], [ 0, 0, 0, 4, 0, 20], [10, 14, 17, 0, 5, 0], [ 0, 0, 0, 20, 0, 6]]) elif symmetryType == "Triclinic": return S.matrix([[ 1, 7, 8, 9, 10, 11], [ 7, 2, 12, 13, 14, 15], [ 8, 12, 3, 16, 17, 18], [ 9, 13, 16, 4, 19, 20], [10, 14, 17, 19, 5, 21], [11, 15, 18, 20, 21, 6]]) def get_options(): # deal with options if not libmode: p = optparse.OptionParser() p.add_option('--xml', '-x', action='store_true', help="CML mode") p.add_option('--castep', '-c', action='store_true', help="CASTEP mode") p.add_option('--force-cml-output','-f', action='store_true', help="Force CML output",dest="force") p.add_option('--graphics', '-g', action='store_true', help="Show graphics (requires matplotlib)") p.add_option('--debug', '-d', action='store_true', help="Debug mode (output to stdout rather than file)") options,arguments = p.parse_args(args=input_options) else: class PotM_Options: xml = True graphics = True castep = False debug = False options = PotM_Options() taskRe = re.compile(r"(.+)-\w+\.cml") arguments = taskRe.findall(input_options[1][0]) global outfile outfile = input_options[0] if options.castep and options.xml: p.error("options -x and -c are mutually exclusive") elif not options.castep and not options.xml: p.error("one of -x or -c is required") # For some reason, golem/lxml needs to be imported first if options.xml or options.force: try: # import golem, for use globally global golem import golem # import etree, for use globally global etree from lxml import etree # set dictionary, for use globally global d d = golem.loadDictionary("castepDict.xml") except ImportError: print >> sys.stderr, "You need to have golem and lxml installed for the --xml option" sys.exit(1) if options.graphics: try: global P import pylab as P except ImportError: print >> sys.stderr, "You need to have matplotlib installed for the --graphics option" sys.exit(1) if(options.castep): print "Reading stress data from the .castep files" elif(options.xml): print "Reading stress data from the .xml files" else: print "Don't know where to get the stress data, please use flags --castep or --xml" sys.exit(1) return options, arguments options, arguments = get_options() # Not sure why the lattice types are enumerated like this, but this is how .cijdat does it... latticeTypes = {0:"Unknown", 1:"Triclinic", 2:"Monoclinic", 3:"Orthorhombic", \ 4:"Tetragonal", 5:"Cubic", 6:"Trigonal-low", 7:"Trigonal-high/Hexagonal"} # regular expression which matches the whole stress tensor block from a .castep file stressRE = re.compile("\s\*+\sSymmetrised\sStress\sTensor\s\*+\n.+\n.+?\((\w+)\).+\n.+\n.+\n.+\n\s\*\s+x\s+([\+\-]?\d+.\d+)\s+([\+\-]?\d+.\d+)\s+([\+\-]?\d+.\d+)\s+\*\n\s\*\s+y\s+[\+\-]?\d+.\d+\s+([\+\-]?\d+.\d+)\s+([\+\-]?\d+.\d+)\s+\*\n\s\*\s+z\s+[\+\-]?\d+.\d+\s+[\+\-]?\d+.\d+\s+([\+\-]?\d+.\d+)\s+\*\n") # Get strain tensors seedname = arguments[0] cijdat = open(seedname+".cijdat","r") print "\nReading strain data from ", seedname+".cijdat\n" numStrainPatterns = (len(cijdat.readlines())-2)/4 #total for all strain patterns #rewind cijdat.seek(0) # deal with those first four integers latticeType,numsteps,TetrHigh,TrigHigh = cijdat.readline().split() numsteps = int(numsteps) symmetryType = latticeTypes[int(latticeType)] print "System is", symmetryType,"\n" # get maximum magnitude of strains magnitude = float(cijdat.readline()) print numsteps, "steps of maximum magnitude",magnitude # if using graphics, do some initial set-up if options.graphics: fig = P.figure(num=1, figsize=(9.5,8),facecolor='white') fig.subplots_adjust(left=0.07,right=0.97,top=0.97,bottom=0.07,wspace=0.5,hspace=0.5) colourDict = {0: '#BAD0EF', 1:'#FFCECE', 2:'#BDF4CB', 3:'#EEF093',4:'#FFA4FF',5:'#75ECFD'} for index1 in range(6): for index2 in range(6): # position this plot in a 6x6 grid sp = P.subplot(6,6,6*(index1)+index2+1) sp.set_axis_off() # change the labels on the axes # xlabels = sp.get_xticklabels() # P.setp(xlabels,'rotation',90,fontsize=7) # ylabels = sp.get_yticklabels() # P.setp(ylabels,fontsize=7) P.text(0.4,0.4, "n/a") print "\n<>---------------------------- ANALYSIS ---------------------------------<>" # initialise 1d array to store all 21 unique elastic constants - will be transformed into 6x6 matrix later finalCijs = S.zeros((21,1)) for patt in range(numStrainPatterns/numsteps): print "\nAnalysing pattern", patt+1, ":" for a in range(0,numsteps): pattern = cijdat.readline() # grab the strain data from the .cijdat file line1 = cijdat.readline().split() line2 = cijdat.readline().split() line3 = cijdat.readline().split() # only take from the top right triangle # numbering according to IRE conventions (Proc IRE, 1949) if a == 0: strain = S.array([float(line1[0]),float(line2[1]),float(line3[2]),2*float(line2[2]),2*float(line1[2]),2*float(line1[1])]) else: strain = S.row_stack((strain,S.array([float(line1[0]),float(line2[1]),float(line3[2]),2*float(line2[2]),2*float(line1[2]),2*float(line1[1])]))) if(options.castep): # now get corresponding stress data from .castep dotCastep = open(seedname+"_cij__"+str(patt+1)+"__"+str(a+1)+".castep","r") stressData = stressRE.findall(dotCastep.read())[0] dotCastep.close() units = stressData[0] # again, top right triangle if a == 0: stress = S.array([float(stressData[1]),float(stressData[4]),float(stressData[6]),float(stressData[5]),float(stressData[3]),float(stressData[2])]) else: stress = S.row_stack((stress,S.array([float(stressData[1]),float(stressData[4]),float(stressData[6]),float(stressData[5]),float(stressData[3]),float(stressData[2])]))) elif(options.xml): # now get corresponding stress data from .xml (using Golem) dotXMLfilename = seedname+"_cij__"+str(patt+1)+"__"+str(a+1)+"-geomopt1.cml" cmlStressData, units = getCMLStress(dotXMLfilename) if a == 0: stress = S.array([float(cmlStressData[0][0]),float(cmlStressData[1][1]),float(cmlStressData[2][2]),float(cmlStressData[1][2]),float(cmlStressData[0][2]),float(cmlStressData[0][1])]) else: stress = S.row_stack((stress,S.array([float(cmlStressData[0][0]),float(cmlStressData[1][1]),float(cmlStressData[2][2]),float(cmlStressData[1][2]),float(cmlStressData[0][2]),float(cmlStressData[0][1])]) )) """ Both the stress and strain matrices use the IRE conventions to reduce the 3x3 matrices to 1x6 arrays. These 1D arrays are then stacked to form a Nx6 array, where N=number of steps. Note that strain and stress arrays are numbered 0->5 rather than 1->6 """ def __fit(index1, index2): from scipy import stats, sqrt, square # do the fit (cijFitted,intercept,r,tt,stderr) = stats.linregress(strain[:,index2-1],stress[:,index1-1]) # correct for scipy weirdness - see http://www.scipy.org/scipy/scipy/ticket/8 stderr = S.sqrt((numsteps * stderr**2)/(numsteps-2)) error = stderr/sqrt(sum(square(strain[:,index2-1]))) # print info about the fit print '\n' print 'Cij (gradient) : ',cijFitted print 'Error in Cij : ', error if abs(r) > 0.9: print 'Correlation coefficient : ',r else: print 'Correlation coefficient : ',r, ' <----- WARNING' # if using graphics, add a subplot if options.graphics: # position this plot in a 6x6 grid sp = P.subplot(6,6,6*(index1-1)+index2) sp.set_axis_on() # change the labels on the axes xlabels = sp.get_xticklabels() P.setp(xlabels,'rotation',90,fontsize=7) ylabels = sp.get_yticklabels() P.setp(ylabels,fontsize=7) # colour the plot depending on the strain pattern sp.set_axis_bgcolor(colourDict[patt]) # plot the data P.plot([strain[0,index2-1],strain[numsteps-1,index2-1]],[cijFitted*strain[0,index2-1]+intercept,cijFitted*strain[numsteps-1,index2-1]+intercept]) P.plot(strain[:,index2-1],stress[:,index1-1],'ro') return cijFitted def __appendOrReplace(valList,val): try: valList.append(val) return sum(valList)/len(valList) except NameError: return val def __createListAndAppend(val): newList = [] newList.append(val) return val, newList cij = S.zeros(21) # Analyse the patterns to see which strains were applied strainsUsed = analysePatterns(strain[0,:]) # should check strains are as expected if symmetryType == "Cubic": if S.all(strainsUsed.transpose() == S.array([[1.0, 0.0, 0.0, 1.0, 0.0, 0.0]])): # strain pattern e1+e4 finalCijs[0] = __fit(1,1) # fit C11 finalCijs[6] = (__fit(2,1) + __fit(3,1))/2 # fit C21+C31 finalCijs[3] = __fit(4,4) # fit C44 else: print "Unsupported strain pattern" sys.exit(1) elif symmetryType == "Trigonal-high/Hexagonal": if S.all(strainsUsed.transpose() == S.array([[0.0, 0.0, 1.0, 0.0, 0.0, 0.0]])): # strain pattern e3 (hexagonal) # fit C13 + C23, and add to list (more values coming...) cij13 = [] cij13.append(__fit(1,3)) cij13.append(__fit(2,3)) finalCijs[2] = __fit(3,3) # fit C33 elif S.all(strainsUsed.transpose() == S.array([[1.0, 0.0, 0.0, 1.0, 0.0, 0.0]])): # strain pattern e1+e4 (hexagonal) finalCijs[0] = __fit(1,1) # fit C11 finalCijs[6] = __fit(2,1) # fit C21 finalCijs[7] = __appendOrReplace(cij13,__fit(3,1)) # fit C31 finalCijs[3] = __fit(4,4) # fit C44 elif S.all(strainsUsed.transpose() == S.array([[1.0, 0.0, 0.0, 0.0, 0.0, 0.0]])): # strain pattern e1 (trigonal-high) finalCijs[0] = __fit(1,1) # fit C11 finalCijs[6] = __fit(2,1) # fit C21 finalCijs[7] = __fit(3,1) # fit C31 finalCijs[8] = __fit(4,1) # fit C41 finalCijs[9] = __fit(5,1) # fit C51 elif S.all(strainsUsed.transpose() == S.array([[0.0, 0.0, 1.0, 1.0, 0.0, 0.0]])): # strain pattern e3+e4 (trigonal-high) # could recalculate C13/C14/C23/C24/C46 here, but won't just now finalCijs[2] = __fit(3,3) # fit C33 finalCijs[3] = __fit(4,4) # fit C44 else: print "Unsupported strain pattern" sys.exit(1) elif symmetryType == "Trigonal-low": if S.all(strainsUsed.transpose() == S.array([[1.0, 0.0, 0.0, 0.0, 0.0, 0.0]])): # strain pattern e1 finalCijs[0] = __fit(1,1) # fit C11 finalCijs[6] = __fit(2,1) # fit C21 finalCijs[7] = __fit(3,1) # fit C31 finalCijs[8] = __fit(4,1) # fit C41 finalCijs[9] = __fit(5,1) # fit C51 elif S.all(strainsUsed.transpose() == S.array([[0.0, 0.0, 1.0, 1.0, 0.0, 0.0]])): # strain pattern e3+e4 # could recalculate C13/C14/C23/C24/C46 here, but won't just now finalCijs[2] = __fit(3,3) # fit C33 finalCijs[3] = __fit(4,4) # fit C44 else: print "Unsupported strain pattern" sys.exit(1) elif symmetryType == "Tetragonal": if S.all(strainsUsed.transpose() == S.array([[1.0, 0.0, 0.0, 1.0, 0.0, 0.0]])): # strain pattern e1+e4 finalCijs[0] = __fit(1,1) # fit C11 finalCijs[6] = __fit(2,1) # fit C21 finalCijs[7] = __fit(3,1) # fit C31 finalCijs[10] = __fit(6,1) # fit C61 finalCijs[3] = __fit(4,4) # fit C44 elif S.all(strainsUsed.transpose() == S.array([[0.0, 0.0, 1.0, 0.0, 0.0, 1.0]])): # strain pattern e3+e6 finalCijs[2] = __fit(3,3) # fit C33 finalCijs[5] = __fit(6,6) # fit C66 else: print "Unsupported strain pattern" sys.exit(1) elif symmetryType == "Orthorhombic": if S.all(strainsUsed.transpose() == S.array([[1.0, 0.0, 0.0, 1.0, 0.0, 0.0]])): # strain pattern e1+e4 finalCijs[0] = __fit(1,1) # fit C11 finalCijs[6], cij12 = __createListAndAppend(__fit(2,1)) # fit C21 finalCijs[7], cij13 = __createListAndAppend(__fit(3,1)) # fit C31 finalCijs[3] = __fit(4,4) # fit C44 elif S.all(strainsUsed.transpose() == S.array([[0.0, 1.0, 0.0, 0.0, 1.0, 0.0]])): # strain pattern e2+e5 finalCijs[6] = __appendOrReplace(cij12,__fit(1,2)) # fit C12 finalCijs[1] = __fit(2,2) # fit C22 finalCijs[11], cij23 = __createListAndAppend(__fit(3,2)) # fit C32 finalCijs[4] = __fit(5,5) # fit C55 elif S.all(strainsUsed.transpose() == S.array([[0.0, 0.0, 1.0, 0.0, 0.0, 1.0]])): # strain pattern e3+e6 finalCijs[7] = __appendOrReplace(cij13,__fit(1,3)) # fit C13 finalCijs[11] = __appendOrReplace(cij23,__fit(2,3)) # fit C23 finalCijs[2] = __fit(3,3) # fit C33 finalCijs[5] = __fit(6,6) # fit C66 else: print "Unsupported strain pattern" sys.exit(1) elif symmetryType == "Monoclinic": if S.all(strainsUsed.transpose() == S.array([[1.0, 0.0, 0.0, 1.0, 0.0, 0.0]])): # strain pattern e1+e4 finalCijs[0] = __fit(1,1) # fit C11 finalCijs[6], cij12 = __createListAndAppend(__fit(2,1)) # fit C21 finalCijs[7], cij13 = __createListAndAppend(__fit(3,1)) # fit C31 finalCijs[3] = __fit(4,4) # fit C44 finalCijs[9], cij51 = __createListAndAppend(__fit(5,1)) # fit C51 finalCijs[19], cij64 = __createListAndAppend(__fit(6,4)) # fit C64 elif S.all(strainsUsed.transpose() == S.array([[0.0, 0.0, 1.0, 0.0, 0.0, 1.0]])): # strain pattern e3+e6 finalCijs[7] = __appendOrReplace(cij13,__fit(1,3)) # fit C13 finalCijs[11], cij23 = __createListAndAppend(__fit(2,3)) # fit C23 finalCijs[2] = __fit(3,3) # fit C33 finalCijs[16], cij53 = __createListAndAppend(__fit(5,3)) # fit C53 finalCijs[19] = __appendOrReplace(cij64,__fit(4,6)) # fit C46 finalCijs[5] = __fit(6,6) # fit C66 elif S.all(strainsUsed.transpose() == S.array([[0.0, 1.0, 0.0, 0.0, 0.0, 0.0]])): # strain pattern e2 finalCijs[6] = __appendOrReplace(cij12,__fit(1,2)) # fit C12 finalCijs[1] = __fit(2,2) # fit C22 finalCijs[11] = __appendOrReplace(cij23,__fit(3,2)) # fit C32 finalCijs[13], cij52 = __createListAndAppend(__fit(5,2)) # fit C52 elif S.all(strainsUsed.transpose() == S.array([[0.0, 0.0, 0.0, 0.0, 1.0, 0.0]])): # strain pattern e5 finalCijs[9] = __appendOrReplace(cij51,__fit(1,5)) # fit C15 finalCijs[13] = __appendOrReplace(cij52,__fit(2,5)) # fit C25 finalCijs[16] = __appendOrReplace(cij53,__fit(3,5)) # fit C35 finalCijs[4] = __fit(5,5) # fit C55 else: print "Unsupported strain pattern" sys.exit(1) elif symmetryType == "Triclinic": if S.all(strainsUsed.transpose() == S.array([[1.0, 0.0, 0.0, 0.0, 0.0, 0.0]])): # strain pattern e1 finalCijs[0] = __fit(1,1) # fit C11 finalCijs[6], cij12 = __createListAndAppend(__fit(2,1)) # fit C21 finalCijs[7], cij13 = __createListAndAppend(__fit(3,1)) # fit C31 finalCijs[8], cij14 = __createListAndAppend(__fit(4,1)) # fit C41 finalCijs[9], cij15 = __createListAndAppend(__fit(5,1)) # fit C51 finalCijs[10],cij16 = __createListAndAppend(__fit(6,1)) # fit C61 elif S.all(strainsUsed.transpose() == S.array([[0.0, 1.0, 0.0, 0.0, 0.0, 0.0]])): # strain pattern e2 finalCijs[6] = __appendOrReplace(cij12,__fit(1,2)) # fit C12 finalCijs[1] = __fit(2,2) # fit C22 finalCijs[11], cij23 = __createListAndAppend(__fit(3,2)) # fit C32 finalCijs[12], cij24 = __createListAndAppend(__fit(4,2)) # fit C42 finalCijs[13], cij25 = __createListAndAppend(__fit(5,2)) # fit C52 finalCijs[14], cij26 = __createListAndAppend(__fit(6,2)) # fit C62 elif S.all(strainsUsed.transpose() == S.array([[0.0, 0.0, 1.0, 0.0, 0.0, 0.0]])): # strain pattern e3 finalCijs[7] = __appendOrReplace(cij13,__fit(1,3)) # fit C13 finalCijs[11] = __appendOrReplace(cij23,__fit(2,3)) # fit C23 finalCijs[2] = __fit(3,3) # fit C33 finalCijs[15], cij34 = __createListAndAppend(__fit(4,3)) # fit C43 finalCijs[16], cij35 = __createListAndAppend(__fit(5,3)) # fit C53 finalCijs[17], cij36 = __createListAndAppend(__fit(6,3)) # fit C63 elif S.all(strainsUsed.transpose() == S.array([[0.0, 0.0, 0.0, 1.0, 0.0, 0.0]])): # strain pattern e4 finalCijs[8] = __appendOrReplace(cij14,__fit(1,4)) # fit C14 finalCijs[12] = __appendOrReplace(cij24,__fit(2,4)) # fit C24 finalCijs[15] = __appendOrReplace(cij34,__fit(3,4)) # fit C34 finalCijs[3] = __fit(4,4) # fit C44 finalCijs[18], cij45 = __createListAndAppend(__fit(5,4)) # fit C54 finalCijs[19], cij46 = __createListAndAppend(__fit(6,4)) # fit C64 elif S.all(strainsUsed.transpose() == S.array([[0.0, 0.0, 0.0, 0.0, 1.0, 0.0]])): # strain pattern e5 finalCijs[9] = __appendOrReplace(cij15,__fit(1,5)) # fit C15 finalCijs[13] = __appendOrReplace(cij25,__fit(2,5)) # fit C25 finalCijs[16] = __appendOrReplace(cij35,__fit(3,5)) # fit C35 finalCijs[18] = __appendOrReplace(cij45,__fit(4,5)) # fit C45 finalCijs[4] = __fit(5,5) # fit C55 finalCijs[20], cij56 = __createListAndAppend(__fit(6,5)) # fit C65 elif S.all(strainsUsed.transpose() == S.array([[0.0, 0.0, 0.0, 0.0, 0.0, 1.0]])): # strain pattern e6 finalCijs[10] = __appendOrReplace(cij16,__fit(1,6)) # fit C16 finalCijs[14] = __appendOrReplace(cij26,__fit(2,6)) # fit C26 finalCijs[17] = __appendOrReplace(cij36,__fit(3,6)) # fit C36 finalCijs[19] = __appendOrReplace(cij46,__fit(4,6)) # fit C46 finalCijs[20] = __appendOrReplace(cij56,__fit(5,6)) # fit C56 finalCijs[5] = __fit(6,6) # fit C66 else: print "Unsupported strain pattern" sys.exit(1) else: print "Unsupported symmetry type. Exiting" sys.exit(1) if options.graphics: P.savefig(os.path.basename(seedname)+'_fits') cijdat.close() if symmetryType == "Trigonal-high/Hexagonal" or symmetryType == "Trigonal-low": # for these systems, C66 is calculated as a combination of the other Cijs. finalCijs[5] = 0.5*(finalCijs[0]-finalCijs[6]) c = cMatrix(symmetryType,TetrHigh) # Generate the 6x6 matrix of elastic constants # - negative values signify a symmetry relation finalCijMatrix = S.zeros((6,6)) for i in range(0,6): for j in range(0,6): index = int(c[i,j]) if index > 0: finalCijMatrix[i,j] = finalCijs[index-1] elif index < 0: finalCijMatrix[i,j] = -finalCijs[-index-1] # Tests if symmetryType == "Cubic": if finalCijs[3] <= 0: print "\n *** WARNING: C44 is less than or equal to zero ***\n" if finalCijs[0] <= abs(finalCijs[6]): print "\n *** WARNING: C11 is less than or equal to |C12| ***\n" if (finalCijs[0]+2*finalCijs[6]) <= 0: print "\n *** WARNING: C11+2C12 is less than or equal to zero ***\n" print "\n<>---------------------------- RESULTS ----------------------------------<>\n" print "Final Cij matrix ("+units+"):" print S.array2string(finalCijMatrix,max_line_width=130,suppress_small=True) sij = S.linalg.inv(finalCijMatrix) print "\nFinal Sij matrix ("+units+"-1):" print S.array2string(sij,max_line_width=130,suppress_small=True) # bulkModulus = (finalCijs[1]+2*finalCijs[7])/3 #cubic only youngX = 1/sij[0,0] youngY = 1/sij[1,1] youngZ = 1/sij[2,2] format = "%18s : %11.5f %8s" print "" # print format % ("Bulk Modulus", bulkModulus[0], units) # print format % ("Compressibility", 1/bulkModulus[0], "1/"+ units) print "\n x y z" print "%18s : %11.5f %11.5f %11.5f %6s" % ("Young's Modulus", youngX, youngY, youngZ, units) poissonXY = -1*sij[0,1]*youngX poissonXZ = -1*sij[0,2]*youngX poissonYX = -1*sij[1,0]*youngY poissonYZ = -1*sij[1,2]*youngY poissonZX = -1*sij[2,0]*youngZ poissonZY = -1*sij[2,1]*youngZ print "\n xy xz yx yz zx zy" format = "%18s : %6.5f %6.5f %6.5f %6.5f %6.5f %6.5f" print format % ("Poisson's Ratios", poissonXY, poissonXZ, poissonYX, poissonYZ, poissonZX, poissonZY) print "\n<>--------------------- POLYCRYSTALLINE RESULTS -------------------------<>\n" # These equations might be valid only for orthorhombic systems - check! voigtB = (1.0/9)*(finalCijMatrix[0,0] + finalCijMatrix[1,1] + finalCijMatrix[2,2] ) \ + (2.0/9)*(finalCijMatrix[0,1] + finalCijMatrix[0,2] + finalCijMatrix[1,2]) reussB = 1.0/((sij[0,0]+sij[1,1]+sij[2,2]) + 2*(sij[0,1]+sij[0,2]+sij[1,2])) voigtG = (1.0/15)*(finalCijMatrix[0,0] + finalCijMatrix[1,1] + finalCijMatrix[2,2] - finalCijMatrix[0,1] - finalCijMatrix[0,2] - finalCijMatrix[1,2]) \ + (1.0/5)*(finalCijMatrix[3,3] + finalCijMatrix[4,4] + finalCijMatrix[5,5]) reussG = 15.0/(4*(sij[0,0]+sij[1,1]+sij[2,2]) - 4*(sij[0,1]+sij[0,2]+sij[1,2]) + 3*(sij[3,3]+sij[4,4]+sij[5,5])) format = "%16s : %11.5f %11.5f %11.5f %6s" print " Voigt Reuss Hill" print format % ("Bulk Modulus", voigtB, reussB, (voigtB+reussB)/2, units) print format % ("Shear Modulus", voigtG, reussG, (voigtG+reussG)/2, units) print "\n<>-----------------------------------------------------------------------<>\n" """ Here on in is just the XML output """ if(options.xml or options.force): dumpXML(seedname)
def generate_Timescan(cleaned, file_name, line_count, state_indv, state_matrix, state_fit, start, stop, startX, stopX ): line_count = int(line_count) start = int(start) stop = int(stop) startX = int(startX) stopX = int(stopX) tx = cleaned[:,3] ty = cleaned[:,4] angle = cleaned[:,8] intensity = cleaned[:,9] time = cleaned[:,10] bkg = intensity[intensity>0].min() # Check data validity if ((time[1:]-time[:-1]).sum()==0): status = 0 else: status = 1 try: time_corr = loadtxt("time_correction.txt") except: time_corr = 0 tscan = time + time_corr #manual correction of time # Crop data to desired dimensions if ((start!=0) or (stop!=0) or (startX!=0) or (stopX!=0)): intensity = crop_data(intensity,line_count, start, stop, startX, stopX) angle = crop_data(angle,line_count, start, stop, startX, stopX) tscan = crop_data(tscan,line_count, start, stop, startX, stopX) line_count = line_count - start - stop int_matrix = intensity.reshape(int(len(intensity)/line_count), int(line_count)) angle = angle[:int(line_count):] tscan = tscan[int(line_count/2)::int(line_count)] if state_matrix == 1: out_I = column_stack((tscan, int_matrix)) out_I = row_stack((append([0],angle), out_I)) savetxt(file_name + '_matrix.txt', out_I.T, fmt = '%10.8f') #if state_indv == 1: #for i in range(len(tscan)): #out_scan = column_stack((angle, int_matrix[i,:])) #savetxt(file_name + "_Scan"+str(i+1)+" Tr="+str(int(tscan[i]))+".txt", out_scan, fmt = '%10.8f') if state_fit == 0: out_Ii = int_matrix.sum(axis=1) savetxt(file_name + "_int_I.txt", column_stack((tscan, out_Ii)), fmt = '%10.8f') #writes individual files if state_indv == 1: for i in range(len(tscan)): out_scan = column_stack((angle, int_matrix[i,:])) savetxt(file_name + "_Scan"+str(i+1)+" Tr="+str(round(tscan[i],2))+".txt", out_scan, fmt = '%10.8f') plt.ion() fig=plt.figure() ax0 = fig.add_subplot(211) ax0.set_xlabel(r"$Time\ (sec.)$", fontsize = 14) ax0.set_ylabel(r"$Scanning\ angle\ (deg.)$", fontsize = 14) plt.imshow(log10(int_matrix+bkg).T, extent=(tscan.min(), tscan.max(), angle.min(),angle.max()), origin='lower', aspect="auto", cmap='jet') ax = fig.add_subplot(212, sharex=ax0) ax.set_xlabel(r"$Time\ (sec.)$", fontsize = 14) ax.set_ylabel(r"$Integrated\ intensity\ (counts)$", fontsize = 14) plt.xlim(tscan.min(), tscan.max()) plt.plot(tscan, out_Ii) plt.tight_layout() else: outfile = open(file_name+ "_fitparams.txt", "w") outfile.write("#translation area esd position esd FWHM esd\n") for i in range(len(tscan)): p, err = pVfit_param_err(angle,int_matrix[i,:]) outfile.write(str(tscan[i])+" "+str(pVoigt_area(p))+" "+str(pVoigt_area_err(p,err))+" "+str(p[1])+" "+str(err[1])+" "+str(p[2])+" "+ str(err[2]) + "\n") #plt.plot(angle, int_matrix[i,:], 'ok', angle, pVoigt(angle, p), '-r') #plt.show() #uncomment to check fits #writes individual files (exp + fit) if state_indv == 1: out_scan = column_stack((angle, int_matrix[i,:])) out_scan = column_stack((out_scan, pVoigt(angle, p))) savetxt(file_name + "_Scan"+str(i+1)+" Tr="+str(round(tscan[i],2))+".txt", out_scan, fmt = '%10.8f') outfile.close() fit_p = loadtxt(file_name.split(".")[0]+ "_fitparams.txt") plt.ion() fig=plt.figure() ax0 = fig.add_subplot(221) ax0.set_xlabel(r"$Time\ (sec)$", fontsize = 14) ax0.set_ylabel(r"$Scanning\ angle\ (deg.)$", fontsize = 14) plt.imshow(log10(int_matrix+bkg).T, extent=(tscan.min(), tscan.max(), angle.min(),angle.max()), origin='lower', aspect="auto", cmap='jet') ax = fig.add_subplot(223) ax.set_xlabel(r"$Time\ (sec.)$", fontsize = 14) ax.set_ylabel(r"$Integrated\ intensity\ (Counts)$", fontsize = 14) plt.xlim(tscan.min(), tscan.max()) plt.plot(tscan, fit_p[:,1]) ax = fig.add_subplot(222) ax.set_xlabel(r"$Time\ (sec.)$", fontsize = 14) ax.set_ylabel(r"$Peak\ position\ (deg.)$", fontsize = 14) plt.xlim(tscan.min(), tscan.max()) plt.plot(tscan, fit_p[:,3]) ax = fig.add_subplot(224) ax.set_xlabel(r"$Time\ (sec.)$", fontsize = 14) ax.set_ylabel(r"$FWHM\ (deg.)$", fontsize = 14) plt.xlim(tscan.min(), tscan.max()) plt.plot(tscan, fit_p[:,5]) plt.tight_layout() plt.show() return status
def generate_RSM(cleaned, file_name, scantype, line_count, wl, state_log, state_angmat, state_qmat, state_xyz, step, start, stop, start2, stop2, thresh, threshmax): line_count = int(line_count) start = int(start) stop = int(stop) start2 = int(start2) stop2 = int(stop2) phi = cleaned[:,2] om = cleaned[:,5] offset = cleaned[:,6] tth = cleaned[:,7] scanning = cleaned[:,8] intensity = cleaned[:,9] bkg = intensity[intensity!=0].min() if threshmax == 0.0: threshmax = log10(intensity.max()) # Check data validity if ((om[1:]-om[:-1]).sum() == 0) and ((phi[1:]-phi[:-1]).sum() == 0): status = 0 else: status = 1 if state_log == 1: intensity = log10(intensity+bkg) # Crop data to desired dimensions if ((start!=0) or (stop!=0) or (start2!=0) or (stop2!=0)): intensity = crop_data(intensity, line_count, start, stop, start2, stop2) om = crop_data(om, line_count, start, stop, start2, stop2) offset = crop_data(offset, line_count, start, stop, start2, stop2) tth = crop_data(tth, line_count, start, stop, start2, stop2) scanning = crop_data(scanning, line_count, start, stop, start2, stop2) phi = crop_data(phi, line_count, start, stop, start2, stop2) line_count = line_count - start - stop # Compute Q, Qz for different scanning geometries if "PSDFIXED" in scantype: inplane = 0 Qx = 4*pi*sin(scanning*pi/360)*sin((om-scanning/2)*pi/180) / wl Qz = 4*pi*sin(scanning*pi/360)*cos((om-scanning/2)*pi/180) / wl if "COUPLED" in scantype: inplane = 0 # Check if theta is the scanning motor and correct accordingly if (scanning[::line_count]-om[::line_count]).sum() == 0: scanning = 2*(scanning - offset) print("test") Qx = 4*pi*sin(scanning*pi/360)*sin((offset)*pi/180) / wl Qz = 4*pi*sin(scanning*pi/360)*cos((offset)*pi/180) / wl if "THETA" in scantype and ((phi[1:]-phi[:-1]).sum() != 0): inplane = 1 Qx = (4*pi*sin(tth*pi/360)*sin((scanning-tth/2)*pi/180) / wl)*cos(phi*pi/180) Qz = (4*pi*sin(tth*pi/360)*sin((scanning-tth/2)*pi/180) / wl)*sin(phi*pi/180) #Actually correspond to Qy # interpolate intensity to a square mesh in reciprocal space grid_x, grid_z = mgrid[Qx.min():Qx.max()+step:step,Qz.min():Qz.max()+step:step] grid_I = griddata(column_stack((Qx, Qz)), intensity, (grid_x, grid_z), fill_value = 0, method='linear') #grid_I[grid_I<=thresh] = nan #Export data files if state_angmat == 1 and inplane == 0: scanning = scanning[:line_count:] if ((om[1:]-om[:-1]).sum() != 0): om = om[::line_count] else: om = tth[::line_count] int_matrix = intensity.reshape(int(len(intensity)/line_count), int(line_count)) int_matrix = column_stack((om, int_matrix)) int_matrix = row_stack((append([0], scanning), int_matrix)) savetxt(file_name + '_angular_matrix.txt', int_matrix.T, fmt = '%10.8f') if state_angmat == 1 and inplane == 1: scanning = scanning[:line_count:] phi = phi[::line_count] int_matrix = intensity.reshape(int(len(intensity)/line_count), int(line_count)) int_matrix = column_stack((phi, int_matrix)) int_matrix = row_stack((append([0], scanning), int_matrix)) savetxt(file_name + '_angular_matrix.txt', int_matrix.T, fmt = '%10.8f') if state_qmat == 1: qx = grid_x[:,0] qz = grid_z[0,:] out_I = column_stack((qx, grid_I[:])) out_I = row_stack((append([0],qz), out_I)) savetxt(file_name + '_Q_matrix.txt', out_I.T, fmt = '%10.8f') if state_xyz == 1: if inplane == 0: savetxt(file_name + '_QxQzlogI.txt', column_stack((Qx,Qz, intensity)), fmt = '%10.8f') if inplane == 1: savetxt(file_name + '_QxQylogI.txt', column_stack((Qx,Qz, intensity)), fmt = '%10.8f') if state_log == 0: grid_I = log10(grid_I+bkg) plt.ion() fig=plt.figure() ax = fig.add_subplot(111) ax.set_xlabel(r"$Q_x (2 \pi / \AA)$", fontsize=16) if inplane == 0: ax.set_ylabel(r"$Q_z (2 \pi / \AA)$", fontsize=16) if inplane == 1: ax.set_ylabel(r"$Q_y (2 \pi / \AA)$", fontsize=16) plt.imshow(grid_I.T, extent=(Qx.min(),Qx.max()+step,Qz.min(),Qz.max()+step), origin='lower', cmap='jet', vmin=thresh, vmax=threshmax) plt.tight_layout() plt.show() return status
def extract_and_evaluate_core(split,m,convolve_func_name,task,cache_port): classifier_kwargs = task.get('classifier_kwargs',{}) train_data = split['train_data'] test_data = split['test_data'] train_labels = split['train_labels'] test_labels = split['test_labels'] train_filenames = [t['filename'] for t in train_data] test_filenames = [t['filename'] for t in test_data] assert set(train_filenames).intersection(test_filenames) == set([]) existing_train_features = [get_from_cache((tf,m,task.get('transform_average')),FEATURE_CACHE) for tf in train_filenames] existing_train_labels = [train_labels[i] for (i,x) in enumerate(existing_train_features) if x is not None] new_train_filenames = [train_filenames[i] for (i,x) in enumerate(existing_train_features) if x is None] new_train_labels = [train_labels[i] for (i,x) in enumerate(existing_train_features) if x is None] existing_test_features = [get_from_cache((tf,m,task.get('transform_average')),FEATURE_CACHE) for tf in test_filenames] existing_test_labels = [test_labels[i] for (i,x) in enumerate(existing_test_features) if x is not None] new_test_filenames =[test_filenames[i] for (i,x) in enumerate(existing_test_features) if x is None] new_test_labels = [test_labels[i] for (i,x) in enumerate(existing_test_features) if x is None] if convolve_func_name == 'numpy': num_batches = multiprocessing.cpu_count() if num_batches > 1: pool = multiprocessing.Pool() elif convolve_func_name == 'pyfft': num_batches = get_num_gpus() if num_batches > 1: pool = multiprocessing.Pool(processes = num_batches) else: pool = None else: raise ValueError, 'convolve func name not recognized' if num_batches > 1: batches = get_data_batches(new_train_filenames,num_batches) results = [] for (bn,b) in enumerate(batches): results.append(pool.apply_async(extract_and_evaluate_inner_core,(b,m.to_dict(),convolve_func_name,bn,task.to_dict(),cache_port))) results = [r.get() for r in results] new_train_features = ListUnion(results) batches = get_data_batches(new_test_filenames,num_batches) results = [] for (bn,b) in enumerate(batches): results.append(pool.apply_async(extract_and_evaluate_inner_core,(b,m.to_dict(),convolve_func_name,bn,task.to_dict(),cache_port))) results = [r.get() for r in results] new_test_features = ListUnion(results) else: print('train feature extraction ...') new_train_features = extract_and_evaluate_inner_core(new_train_filenames,m,convolve_func_name,0,task,cache_port) print('test feature extraction ...') new_test_features = extract_and_evaluate_inner_core(new_test_filenames,m,convolve_func_name,0,task,cache_port) #TODO get the order consistent with original ordering train_features = sp.row_stack(filter(lambda x : x is not None,existing_train_features) + new_train_features) test_features = sp.row_stack(filter(lambda x : x is not None, existing_test_features) + new_test_features) train_labels = existing_train_labels + new_train_labels test_labels = existing_test_labels + new_test_labels for (im,f) in zip(new_train_filenames,new_train_features): put_in_cache((im,m,task.get('transform_average')),f,FEATURE_CACHE) for(im,f) in zip(new_test_filenames,new_test_features): put_in_cache((im,m,task.get('transform_average')),f,FEATURE_CACHE) print('classifier ...') res = svm.classify(train_features,train_labels,test_features,test_labels,classifier_kwargs) print('Split test accuracy', res['test_accuracy']) return res
def ova_classify(train_features, train_labels, test_features, test_labels): """ Classifier using one-vs-all on top of liblinear binary classification. Computes mean average precision (mAP) and mean area-under-the-curve (mAUC) by averaging these measure of the binary results. """ train_features, test_features = __sphere(train_features, test_features) labels = sp.unique(sp.concatenate((train_labels, test_labels))) label_to_id = dict([(k,v) for v, k in enumerate(labels)]) train_ids = sp.array([label_to_id[i] for i in train_labels]) test_ids = sp.array([label_to_id[i] for i in test_labels]) all_ids = sp.array(range(len(labels))) classifiers = [] aps = [] aucs = [] cls_datas = [] signs = [] for id in all_ids: binary_train_ids = sp.array([2*int(l == id) - 1 for l in train_ids]) binary_test_ids = sp.array([2*int(l == id) - 1 for l in test_ids]) signs.append(binary_train_ids[0]) res = classify(train_features, binary_train_ids, test_features, binary_test_ids,sphere=False) aps.append(res['ap']) aucs.append(res['auc']) cls_datas.append(res['cls_data']) mean_ap = sp.array(aps).mean() mean_auc = sp.array(aucs).mean() signs = sp.array(signs) weights = signs * (sp.row_stack([cls_data['coef'] for cls_data in cls_datas]).T) bias = signs * (sp.row_stack([cls_data['intercept'] for cls_data in cls_datas]).T) predictor = max_predictor(weights,bias,labels) test_prediction = predictor(test_features) test_accuracy = 100*(test_prediction == test_labels).sum() / len(test_prediction) train_prediction = predictor(train_features) train_accuracy = 100*(train_prediction == train_labels).sum() / len(train_prediction) cls_data = {'coef' : weights, 'intercept' : bias, 'train_labels': train_labels, 'test_labels' : test_labels, 'train_prediction': train_prediction, 'test_prediction' : test_prediction, 'labels' : labels } return {'cls_data' : cls_data, 'train_accuracy' : train_accuracy, 'test_accuracy' : test_accuracy, 'mean_ap' : mean_ap, 'mean_auc' : mean_auc }
importlib.reload(cvglmnet) importlib.reload(cvglmnetCoef) importlib.reload(cvglmnetPlot) importlib.reload(cvglmnetPredict) # parameters baseDataDir = '../data/' # load data x = scipy.loadtxt(baseDataDir + 'QuickStartExampleX.dat', dtype=scipy.float64) y = scipy.loadtxt(baseDataDir + 'QuickStartExampleY.dat', dtype=scipy.float64) # create weights t = scipy.ones((50, 1), dtype=scipy.float64) wts = scipy.row_stack((t, 2 * t)) # call glmnet fit = glmnet.glmnet(x = x.copy(), y = y.copy(), family = 'gaussian', \ weights = wts, \ alpha = 0.2, nlambda = 20 ) glmnetPrint.glmnetPrint(fit) glmnetPlot.glmnetPlot(fit, xvar='lambda', label=True) glmnetPlot.glmnetPlot(fit, xvar='dev', label=True) # any(fit['lambdau'] == 0.5) # coefApprx = glmnetCoef.glmnetCoef(fit, s=scipy.float64([0.5]), exact=False) print(coefApprx)
def main(input_options, libmode=False): def analysePatterns(strain): # these are the IRE conventions, except that integers are 0->5 rather than 1->6 strainDict = {0:"xx",1:"yy",2:"zz",3:"yz", 4:"zx", 5:"xy"} strainsUsed = S.zeros((6,1)) for a in range(0,S.size(strain)): if strain[a] != 0.0: print strainDict[a], "component is non-zero" strainsUsed[a] = 1 else: strainsUsed[a] = 0 return strainsUsed def cMatrix(symmetryType,TetrHigh): if symmetryType == "Cubic" : return S.matrix([[1, 7, 7, 0, 0, 0], [7, 1, 7, 0, 0, 0], [7, 7, 1, 0, 0, 0], [0, 0, 0, 4, 0, 0], [0, 0, 0, 0, 4, 0], [0, 0, 0, 0, 0, 4]]) elif symmetryType == "Trigonal-high/Hexagonal": return S.matrix([[1, 7, 8, 9, 10, 0], [7, 1, 8, 0,-9, 0], [8, 8, 3, 0, 0, 0], [9, -9, 0, 4, 0, 0], [10, 0, 0, 0, 4, 0], [0, 0, 0, 0, 0, 6]]) elif symmetryType == "Trigonal-low": return S.matrix([[1, 7, 8, 9, 10, 0], [7, 1, 8, -9, -10, 0], [8, 8, 3, 0, 0, 0], [9, -9, 0, 4, 0, -10], [10,-10, 0, 0, 4, 9], [0, 0, 0, -10, 9, 6]]) elif symmetryType == "Tetragonal": if TetrHigh == "-1": print "Higher-symmetry tetragonal (422,4mm,4-2m,4/mmm)" return S.matrix([[1, 7, 8, 0, 0, 0], [7, 1, 8, 0, 0, 0], [8, 8, 3, 0, 0, 0], [0, 0, 0, 4, 0, 0], [0, 0, 0, 0, 4, 0], [0, 0, 0, 0, 0, 6]]) else: print "Lower-symmetry tetragonal (4,-4,4/m)" return S.matrix([[1, 7, 8, 0, 0, 11], [7, 1, 8, 0, 0, -11], [8, 8, 3, 0, 0, 0], [0, 0, 0, 4, 0, 0], [0, 0, 0, 0, 4, 0], [11, -11, 0, 0, 0, 6]]) elif symmetryType == "Orthorhombic": return S.matrix([[ 1, 7, 8, 0, 0, 0], [ 7, 2, 12, 0, 0, 0], [ 8, 12, 3, 0, 0, 0], [ 0, 0, 0, 4, 0, 0], [ 0, 0, 0, 0, 5, 0], [ 0, 0, 0, 0, 0, 6]]) elif symmetryType == "Monoclinic": return S.matrix([[ 1, 7, 8, 0, 10, 0], [ 7, 2, 12, 0, 14, 0], [ 8, 12, 3, 0, 17, 0], [ 0, 0, 0, 4, 0, 20], [10, 14, 17, 0, 5, 0], [ 0, 0, 0, 20, 0, 6]]) elif symmetryType == "Triclinic": return S.matrix([[ 1, 7, 8, 9, 10, 11], [ 7, 2, 12, 13, 14, 15], [ 8, 12, 3, 16, 17, 18], [ 9, 13, 16, 4, 19, 20], [10, 14, 17, 19, 5, 21], [11, 15, 18, 20, 21, 6]]) def get_options(): # deal with options if not libmode: p = optparse.OptionParser() p.add_option('--force-cml-output','-f', action='store_true', help="Force CML output",dest="force") p.add_option('--graphics', '-g', action='store_true', help="Show graphics (requires matplotlib)") p.add_option('--debug', '-d', action='store_true', help="Debug mode (output to stdout rather than file)") p.add_option('--latex', action='store_true', help="dump LaTeX formatted table to file",dest="latex") p.add_option('--latex-nt', action='store_true', help="supress LaaTeX line titles", dest='latex_nt') p.add_option('--txt', action='store', help="Append line to text file",dest="txt", type="string") options,arguments = p.parse_args(args=input_options) else: class PotM_Options: xml = True graphics = True castep = False debug = False options = PotM_Options() taskRe = re.compile(r"(.+)-\w+\.cml") arguments = taskRe.findall(input_options[1][0]) global outfile outfile = input_options[0] if options.graphics: try: global P import pylab as P except ImportError: print >> sys.stderr, "You need to have matplotlib installed for the --graphics option" sys.exit(1) return options, arguments options, arguments = get_options() # Not sure why the lattice types are enumerated like this, but this is how .cijdat does it... latticeTypes = {0:"Unknown", 1:"Triclinic", 2:"Monoclinic", 3:"Orthorhombic", \ 4:"Tetragonal", 5:"Cubic", 6:"Trigonal-low", 7:"Trigonal-high/Hexagonal"} # Get strain tensors seedname = arguments[0] cijdat = open(seedname+".cijdat","r") print "\nReading strain data from ", seedname+".cijdat\n" numStrainPatterns = (len(cijdat.readlines())-2)/4 #total for all strain patterns #rewind cijdat.seek(0) # deal with those first four integers latticeType,numsteps,TetrHigh,TrigHigh = cijdat.readline().split() numsteps = int(numsteps) symmetryType = latticeTypes[int(latticeType)] print "System is", symmetryType,"\n" # get maximum magnitude of strains magnitude = float(cijdat.readline()) print numsteps, "steps of maximum magnitude",magnitude # if using graphics, do some initial set-up if options.graphics: fig = P.figure(num=1, figsize=(9.5,8),facecolor='white') fig.subplots_adjust(left=0.07,right=0.97,top=0.97,bottom=0.07,wspace=0.5,hspace=0.5) colourDict = {0: '#BAD0EF', 1:'#FFCECE', 2:'#BDF4CB', 3:'#EEF093',4:'#FFA4FF',5:'#75ECFD'} for index1 in range(6): for index2 in range(6): # position this plot in a 6x6 grid sp = P.subplot(6,6,6*(index1)+index2+1) sp.set_axis_off() # change the labels on the axes # xlabels = sp.get_xticklabels() # P.setp(xlabels,'rotation',90,fontsize=7) # ylabels = sp.get_yticklabels() # P.setp(ylabels,fontsize=7) P.text(0.4,0.4, "n/a") print "\n<>---------------------------- ANALYSIS ---------------------------------<>" # initialise 1d array to store all 21 unique elastic constants - will be transformed into 6x6 matrix later finalCijs = S.zeros((21,1)) errors = S.zeros((21,1)) for patt in range(numStrainPatterns/numsteps): print patt print "\nAnalysing pattern", patt+1, ":" for a in range(0,numsteps): pattern = cijdat.readline() # grab the strain data from the .cijdat file line1 = cijdat.readline().split() line2 = cijdat.readline().split() line3 = cijdat.readline().split() # only take from the top right triangle # numbering according to IRE conventions (Proc IRE, 1949) if a == 0: strain = S.array([float(line1[0]),float(line2[1]),float(line3[2]),2*float(line2[2]),2*float(line1[2]),2*float(line1[1])]) else: strain = S.row_stack((strain,S.array([float(line1[0]),float(line2[1]),float(line3[2]),2*float(line2[2]),2*float(line1[2]),2*float(line1[1])]))) # now get corresponding stress data from the output file (units, thisStress) = espresso.get_stress(seedname+ "_cij__"+str(patt+1)+"__"+str(a+1)) # again, top right triangle if a == 0: stress = thisStress else: stress = S.row_stack((stress,thisStress)) print stress print strain """ Both the stress and strain matrices use the IRE conventions to reduce the 3x3 matrices to 1x6 arrays. These 1D arrays aerror = sqrt((sum(square(stress[:,index1-1] - fit_str)) / \ (numsteps-2))/(sum(square(strain[:,index2-1]))))re then stacked to form a Nx6 array, where N=number of steps. Note that strain and stress arrays are numbered 0->5 rather than 1->6 """ def __fit(index1, index2): from scipy import stats, sqrt, square # do the fit (cijFitted,intercept,r,tt,stderr) = stats.linregress(strain[:,index2-1],stress[:,index1-1]) if (S.__version__ < '0.7.0'): # correct for scipy weirdness - see http://www.scipy.org/scipy/scipy/ticket/8 # This was fixed before 0.7.0 release. Maybe in some versions of 0.6.x too - # will report huge errors if the check is wrong stderr = S.sqrt((numsteps * stderr**2)/(numsteps-2)) error = stderr/sqrt(sum(square(strain[:,index2-1]))) else: # Work out the error ourselves as I cannot get it from # stderr and this has been checked with gnuplot's fitter fit_str = ((strain[:,index2-1] * cijFitted) + intercept) error = sqrt((sum(square(stress[:,index1-1] - fit_str)) / \ (numsteps-2))/(sum(square(strain[:,index2-1])))) # print info about the fit print '\n' print 'Cij (gradient) : ', cijFitted print 'Error in Cij : ', error print 'Intercept : ', intercept if abs(r) > 0.9: print 'Correlation coefficient : ',r else: print 'Correlation coefficient : ',r, ' <----- WARNING' # if using graphics, add a subplot if options.graphics: # position this plot in a 6x6 grid sp = P.subplot(6,6,6*(index1-1)+index2) sp.set_axis_on() # change the labels on the axes xlabels = sp.get_xticklabels() P.setp(xlabels,'rotation',90,fontsize=7) ylabels = sp.get_yticklabels() P.setp(ylabels,fontsize=7) # colour the plot depending on the strain pattern sp.set_axis_bgcolor(colourDict[patt]) # plot the data P.plot([strain[0,index2-1],strain[numsteps-1,index2-1]],[cijFitted*strain[0,index2-1]+intercept,cijFitted*strain[numsteps-1,index2-1]+intercept]) P.plot(strain[:,index2-1],stress[:,index1-1],'ro') return cijFitted, error def __appendOrReplace(valList,erList,val): try: valList.append(val[0]) erList.append(val[1]) return (sum(valList)/len(valList)), (S.sqrt(sum([x**2 for x in erList])/len(erList)**2)) except NameError: return val[0], val[1] def __createListAndAppend(val): newList = [] newList.append(val[0]) errorList = [] errorList.append(val[1]) return val[0], newList, val[1], errorList cij = S.zeros(21) # Analyse the patterns to see which strains were applied strainsUsed = analysePatterns(strain[0,:]) # should check strains are as expected if symmetryType == "Cubic": if S.all(strainsUsed.transpose() == S.array([[1.0, 0.0, 0.0, 1.0, 0.0, 0.0]])): # strain pattern e1+e4 finalCijs[0], errors[0] = __fit(1,1) # fit C11 fit_21, fit_21_error = __fit(2,1) fit_31, fit_31_error = __fit(3,1) finalCijs[6] = (fit_21 + fit_31)/2 # fit C21+C31 errors[6] = S.sqrt((fit_21_error**2)/4 + (fit_31_error**2)/4) finalCijs[3], errors[3] = __fit(4,4) # fit C44 else: print "Unsupported strain pattern" sys.exit(1) elif symmetryType == "Trigonal-high/Hexagonal": if S.all(strainsUsed.transpose() == S.array([[0.0, 0.0, 1.0, 0.0, 0.0, 0.0]])): # strain pattern e3 (hexagonal) # fit C13 + C23, and add to list (more values coming...) finalCijs[7], cij13, errors[7], er13 = __createListAndAppend(__fit(1,3)) finalCijs[7], cij13, errors[7], er13 = __createListAndAppend(__fit(2,3)) finalCijs[2], errors[2] = __fit(3,3) # fit C33 elif S.all(strainsUsed.transpose() == S.array([[1.0, 0.0, 0.0, 1.0, 0.0, 0.0]])): # strain pattern e1+e4 (hexagonal) finalCijs[0], errors[0] = __fit(1,1) # fit C11 finalCijs[6], errors[6] = __fit(2,1) # fit C21 finalCijs[7], errors[7] = __appendOrReplace(cij13,er13,__fit(3,1)) # fit C31 finalCijs[3], errors[3] = __fit(4,4) # fit C44 elif S.all(strainsUsed.transpose() == S.array([[1.0, 0.0, 0.0, 0.0, 0.0, 0.0]])): # strain pattern e1 (trigonal-high) finalCijs[0], errors[0] = __fit(1,1) # fit C11 finalCijs[6], errors[6] = __fit(2,1) # fit C21 finalCijs[7], errors[7] = __fit(3,1) # fit C31 finalCijs[8], errors[8] = __fit(4,1) # fit C41 finalCijs[9], errors[9] = __fit(5,1) # fit C51 elif S.all(strainsUsed.transpose() == S.array([[0.0, 0.0, 1.0, 1.0, 0.0, 0.0]])): # strain pattern e3+e4 (trigonal-high) # could recalculate C13/C14/C23/C24/C46 here, but won't just now finalCijs[2], errors[2] = __fit(3,3) # fit C33 finalCijs[3], errors[3] = __fit(4,4) # fit C44 else: print "Unsupported strain pattern" sys.exit(1) elif symmetryType == "Trigonal-low": if S.all(strainsUsed.transpose() == S.array([[1.0, 0.0, 0.0, 0.0, 0.0, 0.0]])): # strain pattern e1 finalCijs[0], errors[0] = __fit(1,1) # fit C11 finalCijs[6], errors[6] = __fit(2,1) # fit C21 finalCijs[7], errors[7] = __fit(3,1) # fit C31 finalCijs[8], errors[8] = __fit(4,1) # fit C41 finalCijs[9], errors[9] = __fit(5,1) # fit C51 elif S.all(strainsUsed.transpose() == S.array([[0.0, 0.0, 1.0, 1.0, 0.0, 0.0]])): # strain pattern e3+e4 # could recalculate C13/C14/C23/C24/C46 here, but won't just now finalCijs[2], errors[2] = __fit(3,3) # fit C33 finalCijs[3], errors[3] = __fit(4,4) # fit C44 else: print "Unsupported strain pattern" sys.exit(1) elif symmetryType == "Tetragonal": if S.all(strainsUsed.transpose() == S.array([[1.0, 0.0, 0.0, 1.0, 0.0, 0.0]])): # strain pattern e1+e4 finalCijs[0], errors[0] = __fit(1,1) # fit C11 finalCijs[6], errors[6] = __fit(2,1) # fit C21 finalCijs[7], errors[7] = __fit(3,1) # fit C31 finalCijs[10], errors[10] = __fit(6,1) # fit C61 finalCijs[3], errors[3] = __fit(4,4) # fit C44 elif S.all(strainsUsed.transpose() == S.array([[0.0, 0.0, 1.0, 0.0, 0.0, 1.0]])): # strain pattern e3+e6 finalCijs[2], errors[2] = __fit(3,3) # fit C33 finalCijs[5], errors[5] = __fit(6,6) # fit C66 else: print "Unsupported strain pattern" sys.exit(1) elif symmetryType == "Orthorhombic": if S.all(strainsUsed.transpose() == S.array([[1.0, 0.0, 0.0, 1.0, 0.0, 0.0]])): # strain pattern e1+e4 finalCijs[0], errors[0] = __fit(1,1) # fit C11 finalCijs[6], cij12, errors[6], er12 = __createListAndAppend(__fit(2,1)) # fit C21 finalCijs[7], cij13, errors[7], er13 = __createListAndAppend(__fit(3,1)) # fit C31 finalCijs[3], errors[3] = __fit(4,4) # fit C44 elif S.all(strainsUsed.transpose() == S.array([[0.0, 1.0, 0.0, 0.0, 1.0, 0.0]])): # strain pattern e2+e5 finalCijs[6], errors[6] = __appendOrReplace(cij12,er12,__fit(1,2)) # fit C12 finalCijs[1], errors[1] = __fit(2,2) # fit C22 finalCijs[11], cij23, errors[11], er23 = __createListAndAppend(__fit(3,2)) # fit C32 finalCijs[4], errors[4] = __fit(5,5) # fit C55 elif S.all(strainsUsed.transpose() == S.array([[0.0, 0.0, 1.0, 0.0, 0.0, 1.0]])): # strain pattern e3+e6 finalCijs[7], errors[7] = __appendOrReplace(cij13,er13,__fit(1,3)) # fit C13 finalCijs[11], errors[11] = __appendOrReplace(cij23,er23,__fit(2,3)) # fit C23 finalCijs[2], errors[2] = __fit(3,3) # fit C33 finalCijs[5], errors[5] = __fit(6,6) # fit C66 else: print "Unsupported strain pattern" sys.exit(1) elif symmetryType == "Monoclinic": if S.all(strainsUsed.transpose() == S.array([[1.0, 0.0, 0.0, 1.0, 0.0, 0.0]])): # strain pattern e1+e4 finalCijs[0], errors[0] = __fit(1,1) # fit C11 finalCijs[6], cij12, errors[6], er12 = __createListAndAppend(__fit(2,1)) # fit C21 finalCijs[7], cij13, errors[7], er13 = __createListAndAppend(__fit(3,1)) # fit C31 finalCijs[3], errors[3] = __fit(4,4) # fit C44 finalCijs[9], cij51, errors[9], er51 = __createListAndAppend(__fit(5,1)) # fit C51 finalCijs[19], cij64, errors[19], er64 = __createListAndAppend(__fit(6,4)) # fit C64 elif S.all(strainsUsed.transpose() == S.array([[0.0, 0.0, 1.0, 0.0, 0.0, 1.0]])): # strain pattern e3+e6 finalCijs[7], errors[7] = __appendOrReplace(cij13,er13,__fit(1,3)) # fit C13 finalCijs[11], cij23, errors[11], er23 = __createListAndAppend(__fit(2,3)) # fit C23 finalCijs[2], errors[2] = __fit(3,3) # fit C33 finalCijs[16], cij53, errors[16], er53 = __createListAndAppend(__fit(5,3)) # fit C53 finalCijs[19], errors[19] = __appendOrReplace(cij64,er64,__fit(4,6)) # fit C46 finalCijs[5], errors[5] = __fit(6,6) # fit C66 elif S.all(strainsUsed.transpose() == S.array([[0.0, 1.0, 0.0, 0.0, 0.0, 0.0]])): # strain pattern e2 finalCijs[6], errors[6] = __appendOrReplace(cij12,er12,__fit(1,2)) # fit C12 finalCijs[1], errors[1] = __fit(2,2) # fit C22 finalCijs[11],errors[11] = __appendOrReplace(cij23,er23,__fit(3,2)) # fit C32 finalCijs[13], cij52, errors[13], er52 = __createListAndAppend(__fit(5,2)) # fit C52 elif S.all(strainsUsed.transpose() == S.array([[0.0, 0.0, 0.0, 0.0, 1.0, 0.0]])): # strain pattern e5 finalCijs[9], errors[9] = __appendOrReplace(cij51,er51,__fit(1,5)) # fit C15 finalCijs[13],errors[13] = __appendOrReplace(cij52,er52,__fit(2,5)) # fit C25 finalCijs[16],errors[16] = __appendOrReplace(cij53,er53,__fit(3,5)) # fit C35 finalCijs[4], errors[4] = __fit(5,5) # fit C55 else: print "Unsupported strain pattern" sys.exit(1) elif symmetryType == "Triclinic": if S.all(strainsUsed.transpose() == S.array([[1.0, 0.0, 0.0, 0.0, 0.0, 0.0]])): # strain pattern e1 finalCijs[0], errors[0] = __fit(1,1) # fit C11 finalCijs[6], cij12, errors[6], er12 = __createListAndAppend(__fit(2,1)) # fit C21 finalCijs[7], cij13, errors[7], er13 = __createListAndAppend(__fit(3,1)) # fit C31 finalCijs[8], cij14, errors[8], er14 = __createListAndAppend(__fit(4,1)) # fit C41 finalCijs[9], cij15, errors[9], er15 = __createListAndAppend(__fit(5,1)) # fit C51 finalCijs[10],cij16, errors[10],er16 = __createListAndAppend(__fit(6,1)) # fit C61 elif S.all(strainsUsed.transpose() == S.array([[0.0, 1.0, 0.0, 0.0, 0.0, 0.0]])): # strain pattern e2 finalCijs[6], errors[6] = __appendOrReplace(cij12,er12,__fit(1,2)) # fit C12 finalCijs[1], errors[1] = __fit(2,2) # fit C22 finalCijs[11], cij23, errors[11], er23 = __createListAndAppend(__fit(3,2)) # fit C32 finalCijs[12], cij24, errors[12], er24 = __createListAndAppend(__fit(4,2)) # fit C42 finalCijs[13], cij25, errors[13], er25 = __createListAndAppend(__fit(5,2)) # fit C52 finalCijs[14], cij26, errors[14], er26 = __createListAndAppend(__fit(6,2)) # fit C62 elif S.all(strainsUsed.transpose() == S.array([[0.0, 0.0, 1.0, 0.0, 0.0, 0.0]])): # strain pattern e3 finalCijs[7], errors[7] = __appendOrReplace(cij13,er13,__fit(1,3)) # fit C13 finalCijs[11], errors[11] = __appendOrReplace(cij23,er23,__fit(2,3)) # fit C23 finalCijs[2], errors[2] = __fit(3,3) # fit C33 finalCijs[15], cij34, errors[15], er34 = __createListAndAppend(__fit(4,3)) # fit C43 finalCijs[16], cij35, errors[16], er35 = __createListAndAppend(__fit(5,3)) # fit C53 finalCijs[17], cij36, errors[17], er36 = __createListAndAppend(__fit(6,3)) # fit C63 elif S.all(strainsUsed.transpose() == S.array([[0.0, 0.0, 0.0, 1.0, 0.0, 0.0]])): # strain pattern e4 finalCijs[8], errors[8] = __appendOrReplace(cij14,er14,__fit(1,4)) # fit C14 finalCijs[12], errors[12] = __appendOrReplace(cij24,er24,__fit(2,4)) # fit C24 finalCijs[15], errors[15] = __appendOrReplace(cij34,er34,__fit(3,4)) # fit C34 finalCijs[3], errors[3] = __fit(4,4) # fit C44 finalCijs[18], cij45, errors[18], er45 = __createListAndAppend(__fit(5,4)) # fit C54 finalCijs[19], cij46, errors[19], er46 = __createListAndAppend(__fit(6,4)) # fit C64 elif S.all(strainsUsed.transpose() == S.array([[0.0, 0.0, 0.0, 0.0, 1.0, 0.0]])): # strain pattern e5 finalCijs[9], errors[9] = __appendOrReplace(cij15,er15,__fit(1,5)) # fit C15 finalCijs[13], errors[13] = __appendOrReplace(cij25,er25,__fit(2,5)) # fit C25 finalCijs[16], errors[16] = __appendOrReplace(cij35,er35,__fit(3,5)) # fit C35 finalCijs[18], errors[18] = __appendOrReplace(cij45,er45,__fit(4,5)) # fit C45 finalCijs[4], errors[4] = __fit(5,5) # fit C55 finalCijs[20], cij56, errors[20], er56 = __createListAndAppend(__fit(6,5)) # fit C65 elif S.all(strainsUsed.transpose() == S.array([[0.0, 0.0, 0.0, 0.0, 0.0, 1.0]])): # strain pattern e6 finalCijs[10], errors[10] = __appendOrReplace(cij16,er16,__fit(1,6)) # fit C16 finalCijs[14], errors[14] = __appendOrReplace(cij26,er26,__fit(2,6)) # fit C26 finalCijs[17], errors[17] = __appendOrReplace(cij36,er36,__fit(3,6)) # fit C36 finalCijs[19], errors[19] = __appendOrReplace(cij46,er46,__fit(4,6)) # fit C46 finalCijs[20], errors[20] = __appendOrReplace(cij56,er56,__fit(5,6)) # fit C56 finalCijs[5], errors[5] = __fit(6,6) # fit C66 else: print "Unsupported strain pattern" sys.exit(1) else: print "Unsupported symmetry type. Exiting" sys.exit(1) if options.graphics: P.savefig(os.path.basename(seedname)+'_fits') cijdat.close() if symmetryType == "Trigonal-high/Hexagonal" or symmetryType == "Trigonal-low": # for these systems, C66 is calculated as a combination of the other Cijs. finalCijs[5] = 0.5*(finalCijs[0]-finalCijs[6]) errors[5] = S.sqrt(0.25*(errors[0]**2+errors[6]**2)) c = cMatrix(symmetryType,TetrHigh) # Generate the 6x6 matrix of elastic constants # - negative values signify a symmetry relation finalCijMatrix = S.zeros((6,6)) finalErrors = S.zeros((6,6)) for i in range(0,6): for j in range(0,6): index = int(c[i,j]) if index > 0: finalCijMatrix[i,j] = finalCijs[index-1] finalErrors[i,j] = errors[index-1] elif index < 0: finalCijMatrix[i,j] = -finalCijs[-index-1] finalErrors[i,j] = -errors[-index-1] # Tests if symmetryType == "Cubic": if finalCijs[3] <= 0: print "\n *** WARNING: C44 is less than or equal to zero ***\n" if finalCijs[0] <= abs(finalCijs[6]): print "\n *** WARNING: C11 is less than or equal to |C12| ***\n" if (finalCijs[0]+2*finalCijs[6]) <= 0: print "\n *** WARNING: C11+2C12 is less than or equal to zero ***\n" print "\n<>---------------------------- RESULTS ----------------------------------<>\n" print "Final Cij matrix ("+units+"):" print S.array2string(finalCijMatrix,max_line_width=130,suppress_small=True) print "\nErrors on Cij matrix ("+units+"):" print S.array2string(finalErrors,max_line_width=130,suppress_small=True) (sij, esij, covsij) = CijUtil.invertCij(finalCijMatrix,finalErrors) print "\nFinal Sij matrix ("+units+"-1):" print S.array2string(sij,max_line_width=130,suppress_small=True) print "\nErrors on Sij matrix ("+units+"-1):" print S.array2string(esij,max_line_width=130,suppress_small=True) print"\n<>----------------------------------------------------------------------<>\n" if symmetryType == "Cubic": print " Zener anisotropy index : %6.5f +/- %6.5f" % (CijUtil.zenerAniso(finalCijMatrix,finalErrors)) print " Universal anisotropy index : %6.5f +/- %6.5f" % (CijUtil.uAniso(finalCijMatrix,finalErrors)) print " (Rangnthn and Ostoja-Starzewski, PRL 101, 055504)\n" (youngX, youngY, youngZ, eyoungX, eyoungY, eyoungZ, poissonXY, poissonXZ, poissonYX, poissonYZ, poissonZX, poissonZY, epoissonXY, epoissonXZ, epoissonYX, epoissonYZ, epoissonZX, epoissonZY) = CijUtil.youngsmod(finalCijMatrix,finalErrors) format = "%18s : %11.5f %8s" print "\n x y z" print "%18s : %11.5f %11.5f %11.5f %6s" % ("Young's Modulus", youngX, youngY, youngZ, units) print "%18s : %11.5f %11.5f %11.5f " % (" +/- ", eyoungX, eyoungY, eyoungZ) print "\n xy xz yx yz zx zy" format = "%18s : %6.5f %6.5f %6.5f %6.5f %6.5f %6.5f" print format % ("Poisson's Ratios", poissonXY, poissonXZ, poissonYX, poissonYZ, poissonZX, poissonZY) print format % (" +/-", epoissonXY, epoissonXZ, epoissonYX, epoissonYZ, epoissonZX, epoissonZY) print "\n<>--------------------- POLYCRYSTALLINE RESULTS -------------------------<>\n" (voigtB, reussB, voigtG, reussG, hillB, hillG, evB, erB, evG, erG, ehB, ehG) = CijUtil.polyCij(finalCijMatrix, finalErrors) format = "%16s : %11.5f %11.5f %11.5f %11.5f %11.5f %11.5f %6s" print " Voigt +/- Reuss +/- Hill +/-" print format % ("Bulk Modulus", voigtB, evB, reussB, erB, hillB, ehB, units) print format % ("Shear Modulus", voigtG, evG, reussG, erG, hillG, ehG, units) print "\n<>-----------------------------------------------------------------------<>\n" S.savetxt(seedname + '_cij.txt', finalCijMatrix) if options.latex: CijUtil.latexCij(finalCijMatrix, finalErrors, seedname + '.tex', options.latex_nt) if options.txt: CijUtil.txtCij(finalCijMatrix, options.txt)
def generate_Temp(cleaned, file_name, line_count, state_indv, state_matrix, state_fit, start, stop, startT, stopT): line_count = int(line_count) start = int(start) stop = int(stop) startT = int(startT) stopT = int(stopT) temperature = cleaned[:,0] angle = cleaned[:,8] intensity = cleaned[:,9] bkg = intensity[intensity!=0].min() # Check data validity if (temperature[1:]-temperature[:-1]).sum() == 0: status = 0 else: status = 1 # Crop data to desired dimensions if ((start!=0) or (stop!=0) or (startT!=0) or (stopT!=0)): intensity = crop_data(intensity,line_count, start, stop, startT, stopT) angle = crop_data(angle,line_count, start, stop, startT, stopT) temperature = crop_data(temperature,line_count, start, stop, startT, stopT) line_count = line_count - start - stop int_matrix = intensity.reshape(int(len(intensity)/line_count), int(line_count)) angle = angle[:int(line_count):] temperature = temperature[::int(line_count)] #Export data files if state_matrix == 1: out_I = column_stack((temperature, int_matrix)) out_I = row_stack((append([0],angle), out_I)) savetxt(file_name + '_matrix.txt', out_I.T, fmt = '%10.8f') # Compute integrated intensity + plotting if state_fit == 0: out_Ii = int_matrix.sum(axis=1) #writes result file savetxt(file_name + "_int_I.txt", column_stack((temperature, out_Ii)), fmt = '%10.8f') #writes individual files if state_indv == 1: for i in range(len(temperature)): out_scan = column_stack((angle, int_matrix[i,:])) savetxt(file_name + "_Scan"+str(i+1)+" T="+str(round(temperature[i],2))+".txt", out_scan, fmt = '%10.8f') plt.ion() fig=plt.figure() ax0 = fig.add_subplot(211) ax0.set_xlabel(r"$Temperature\ (deg.)$", fontsize = 14) ax0.set_ylabel(r"$Scanning\ angle\ (deg.)$", fontsize = 14) plt.imshow(log10(int_matrix+bkg).T, extent=(temperature.min(), temperature.max(), angle.min(),angle.max()), origin='lower', aspect="auto", cmap='jet') ax = fig.add_subplot(212, sharex=ax0) ax.set_xlabel(r"$Temperature\ (deg.)$", fontsize = 14) ax.set_ylabel(r"$Integrated\ intensity\ (counts)$", fontsize = 14) plt.xlim(temperature.min(), temperature.max()) plt.plot(temperature, out_Ii) plt.tight_layout() # Fit diffraction profiles with a pseudo Voigt + plotting else: outfile = open(file_name+ "_fit_params.txt", "w") outfile.write("#temperature area esd position esd FWHM esd\n") for i in range(len(temperature)): p, err = pVfit_param_err(angle,int_matrix[i,:]) outfile.write(str(temperature[i])+" "+str(pVoigt_area(p))+" "+str(pVoigt_area_err(p,err))+" "+str(p[1])+" "+str(err[1])+" "+str(p[2])+" "+ str(err[2]) + "\n") #plt.plot(angle, int_matrix[i,:], 'ok', angle, pVoigt(angle, p), '-r') #plt.show() #uncomment to check fits #writes individual files (exp and fit) if state_indv == 1: out_scan = column_stack((angle, int_matrix[i,:])) out_scan = column_stack((out_scan, pVoigt(angle, p))) savetxt(file_name + "_Scan"+str(i+1)+" T="+str(round(temperature[i],2))+".txt", out_scan, fmt = '%10.8f') outfile.close() fit_p = loadtxt(file_name.split(".")[0]+ "_fit_params.txt") plt.ion() fig=plt.figure() ax0 = fig.add_subplot(221) ax0.set_xlabel(r"$Temperature\ (deg.)$", fontsize = 14) ax0.set_ylabel(r"$Scanning\ angle\ (deg.)$", fontsize = 14) plt.imshow(log10(int_matrix+bkg).T, extent=(temperature.min(), temperature.max(), angle.min(),angle.max()), origin='lower', aspect="auto", cmap='jet') ax = fig.add_subplot(223) ax.set_xlabel(r"$Temperature\ (deg.)$", fontsize = 14) ax.set_ylabel(r"$Integrated\ intensity\ (counts)$", fontsize = 14) plt.xlim(temperature.min(), temperature.max()) plt.plot(temperature, fit_p[:,1]) ax = fig.add_subplot(222) ax.set_xlabel(r"$Temperature\ (deg.)$", fontsize = 14) ax.set_ylabel(r"$Peak\ position\ (deg.)$", fontsize = 14) plt.xlim(temperature.min(), temperature.max()) plt.plot(temperature, fit_p[:,3]) ax = fig.add_subplot(224) ax.set_xlabel(r"$Temperature\ (deg.)$", fontsize = 14) ax.set_ylabel(r"$FWHM\ (deg.)$", fontsize = 14) plt.xlim(temperature.min(), temperature.max()) plt.plot(temperature, fit_p[:,5]) plt.tight_layout() plt.show() return status
def main(input_options, libmode=False): def analysePatterns(strain): # these are the IRE conventions, except that integers are 0->5 rather than 1->6 strainDict = {0: "xx", 1: "yy", 2: "zz", 3: "yz", 4: "zx", 5: "xy"} strainsUsed = S.zeros((6, 1)) for a in range(0, S.size(strain)): if strain[a] != 0.0: print strainDict[a], "component is non-zero" strainsUsed[a] = 1 else: strainsUsed[a] = 0 return strainsUsed def cMatrix(symmetryType, TetrHigh): if symmetryType == "Cubic": return S.matrix([[1, 7, 7, 0, 0, 0], [7, 1, 7, 0, 0, 0], [7, 7, 1, 0, 0, 0], [0, 0, 0, 4, 0, 0], [0, 0, 0, 0, 4, 0], [0, 0, 0, 0, 0, 4]]) elif symmetryType == "Trigonal-high/Hexagonal": return S.matrix([[1, 7, 8, 9, 0, 0], [7, 1, 8, -9, 0, 0], [8, 8, 3, 0, 0, 0], [9, -9, 0, 4, 0, 0], [0, 0, 0, 0, 4, 9], [0, 0, 0, 0, 9, 6]]) elif symmetryType == "Trigonal-low": return S.matrix([[1, 7, 8, 9, 10, 0], [7, 1, 8, -9, -10, 0], [8, 8, 3, 0, 0, 0], [9, -9, 0, 4, 0, -10], [10, -10, 0, 0, 4, 9], [0, 0, 0, -10, 9, 6]]) elif symmetryType == "Tetragonal": if TetrHigh == "-1": print "Higher-symmetry tetragonal (422,4mm,4-2m,4/mmm)" return S.matrix([[1, 7, 8, 0, 0, 0], [7, 1, 8, 0, 0, 0], [8, 8, 3, 0, 0, 0], [0, 0, 0, 4, 0, 0], [0, 0, 0, 0, 4, 0], [0, 0, 0, 0, 0, 6]]) else: print "Lower-symmetry tetragonal (4,-4,4/m)" return S.matrix([[1, 7, 8, 0, 0, 11], [7, 1, 8, 0, 0, -11], [8, 8, 3, 0, 0, 0], [0, 0, 0, 4, 0, 0], [0, 0, 0, 0, 4, 0], [11, -11, 0, 0, 0, 6]]) elif symmetryType == "Orthorhombic": return S.matrix([[1, 7, 8, 0, 0, 0], [7, 2, 12, 0, 0, 0], [8, 12, 3, 0, 0, 0], [0, 0, 0, 4, 0, 0], [0, 0, 0, 0, 5, 0], [0, 0, 0, 0, 0, 6]]) elif symmetryType == "Monoclinic": return S.matrix([[1, 7, 8, 0, 10, 0], [7, 2, 12, 0, 14, 0], [8, 12, 3, 0, 17, 0], [0, 0, 0, 4, 0, 20], [10, 14, 17, 0, 5, 0], [0, 0, 0, 20, 0, 6]]) elif symmetryType == "Triclinic": return S.matrix([[1, 7, 8, 9, 10, 11], [7, 2, 12, 13, 14, 15], [8, 12, 3, 16, 17, 18], [9, 13, 16, 4, 19, 20], [10, 14, 17, 19, 5, 21], [11, 15, 18, 20, 21, 6]]) def get_options(): # deal with options if not libmode: p = optparse.OptionParser() p.add_option('--force-cml-output', '-f', action='store_true', help="Force CML output", dest="force") p.add_option('--graphics', '-g', action='store_true', help="Show graphics (requires matplotlib)") p.add_option('--debug', '-d', action='store_true', help="Debug mode (output to stdout rather than file)") p.add_option('--latex', action='store_true', help="dump LaTeX formatted table to file", dest="latex") p.add_option('--latex-nt', action='store_true', help="supress LaaTeX line titles", dest='latex_nt') p.add_option('--txt', action='store', help="Append line to text file", dest="txt", type="string") options, arguments = p.parse_args(args=input_options) else: class PotM_Options: xml = True graphics = True castep = False debug = False options = PotM_Options() taskRe = re.compile(r"(.+)-\w+\.cml") arguments = taskRe.findall(input_options[1][0]) global outfile outfile = input_options[0] if options.graphics: try: global P import pylab as P except ImportError: print >> sys.stderr, "You need to have matplotlib installed for the --graphics option" sys.exit(1) return options, arguments options, arguments = get_options() # Not sure why the lattice types are enumerated like this, but this is how .cijdat does it... latticeTypes = {0:"Unknown", 1:"Triclinic", 2:"Monoclinic", 3:"Orthorhombic", \ 4:"Tetragonal", 5:"Cubic", 6:"Trigonal-low", 7:"Trigonal-high/Hexagonal"} # Get strain tensors seedname = arguments[0] cijdat = open(seedname + ".cijdat", "r") print "\nReading strain data from ", seedname + ".cijdat\n" numStrainPatterns = (len(cijdat.readlines()) - 2) / 4 #total for all strain patterns #rewind cijdat.seek(0) # deal with those first four integers latticeType, numsteps, TetrHigh, TrigHigh = cijdat.readline().split() numsteps = int(numsteps) symmetryType = latticeTypes[int(latticeType)] print "System is", symmetryType, "\n" # get maximum magnitude of strains magnitude = float(cijdat.readline()) print numsteps, "steps of maximum magnitude", magnitude # if using graphics, do some initial set-up if options.graphics: fig = P.figure(num=1, figsize=(9.5, 8), facecolor='white') fig.subplots_adjust(left=0.07, right=0.97, top=0.97, bottom=0.07, wspace=0.5, hspace=0.5) colourDict = { 0: '#BAD0EF', 1: '#FFCECE', 2: '#BDF4CB', 3: '#EEF093', 4: '#FFA4FF', 5: '#75ECFD' } for index1 in range(6): for index2 in range(6): # position this plot in a 6x6 grid sp = P.subplot(6, 6, 6 * (index1) + index2 + 1) sp.set_axis_off() # change the labels on the axes # xlabels = sp.get_xticklabels() # P.setp(xlabels,'rotation',90,fontsize=7) # ylabels = sp.get_yticklabels() # P.setp(ylabels,fontsize=7) P.text(0.4, 0.4, "n/a") print "\n<>---------------------------- ANALYSIS ---------------------------------<>" # initialise 1d array to store all 21 unique elastic constants - will be transformed into 6x6 matrix later finalCijs = S.zeros((21, 1)) errors = S.zeros((21, 1)) for patt in range(numStrainPatterns / numsteps): print "\nAnalysing pattern", patt + 1, ":" for a in range(0, numsteps): pattern = cijdat.readline() # grab the strain data from the .cijdat file line1 = cijdat.readline().split() line2 = cijdat.readline().split() line3 = cijdat.readline().split() # only take from the top right triangle # numbering according to IRE conventions (Proc IRE, 1949) if a == 0: strain = S.array([ float(line1[0]), float(line2[1]), float(line3[2]), 2 * float(line2[2]), 2 * float(line1[2]), 2 * float(line1[1]) ]) else: strain = S.row_stack( (strain, S.array([ float(line1[0]), float(line2[1]), float(line3[2]), 2 * float(line2[2]), 2 * float(line1[2]), 2 * float(line1[1]) ]))) # now get corresponding stress data from .castep (units, thisStress) = castep.get_stress_dotcastep(seedname + "_cij__" + str(patt + 1) + "__" + str(a + 1) + ".castep") # again, top right triangle if a == 0: stress = thisStress else: stress = S.row_stack((stress, thisStress)) """ Both the stress and strain matrices use the IRE conventions to reduce the 3x3 matrices to 1x6 arrays. These 1D arrays are then stacked to form a Nx6 array, where N=number of steps. Note that strain and stress arrays are numbered 0->5 rather than 1->6 """ def __fit(index1, index2): from scipy import stats, sqrt, square # do the fit (cijFitted, intercept, r, tt, stderr) = stats.linregress(strain[:, index2 - 1], stress[:, index1 - 1]) if (S.__version__ < '0.7.0'): # correct for scipy weirdness - see http://www.scipy.org/scipy/scipy/ticket/8 # This was fixed before 0.7.0 release. Maybe in some versions of 0.6.x too - # will report huge errors if the check is wrong stderr = S.sqrt((numsteps * stderr**2) / (numsteps - 2)) error = stderr / sqrt(sum(square(strain[:, index2 - 1]))) else: # Work out the error ourselves as I cannot get it from # stderr and this has been checked with gnuplot's fitter fit_str = ((strain[:, index2 - 1] * cijFitted) + intercept) error = sqrt((sum(square(stress[:,index1-1] - fit_str)) / \ (numsteps-2))/(sum(square(strain[:,index2-1])))) # print info about the fit print '\n' print 'Cij (gradient) : ', cijFitted print 'Error in Cij : ', error print 'Intercept : ', intercept if abs(r) > 0.9: print 'Correlation coefficient : ', r else: print 'Correlation coefficient : ', r, ' <----- WARNING' # if using graphics, add a subplot if options.graphics: # position this plot in a 6x6 grid sp = P.subplot(6, 6, 6 * (index1 - 1) + index2) sp.set_axis_on() # change the labels on the axes xlabels = sp.get_xticklabels() P.setp(xlabels, 'rotation', 90, fontsize=7) ylabels = sp.get_yticklabels() P.setp(ylabels, fontsize=7) # colour the plot depending on the strain pattern sp.set_axis_bgcolor(colourDict[patt]) # plot the data P.plot([ strain[0, index2 - 1], strain[numsteps - 1, index2 - 1] ], [ cijFitted * strain[0, index2 - 1] + intercept, cijFitted * strain[numsteps - 1, index2 - 1] + intercept ]) P.plot(strain[:, index2 - 1], stress[:, index1 - 1], 'ro') return cijFitted, error def __appendOrReplace(valList, erList, val): try: valList.append(val[0]) erList.append(val[1]) return (sum(valList) / len(valList)), (S.sqrt( sum([x**2 for x in erList]) / len(erList)**2)) except NameError: return val[0], val[1] def __createListAndAppend(val): newList = [] newList.append(val[0]) errorList = [] errorList.append(val[1]) return val[0], newList, val[1], errorList cij = S.zeros(21) # Analyse the patterns to see which strains were applied strainsUsed = analysePatterns(strain[0, :]) # should check strains are as expected if symmetryType == "Cubic": if S.all(strainsUsed.transpose() == S.array( [[1.0, 0.0, 0.0, 1.0, 0.0, 0.0]])): # strain pattern e1+e4 finalCijs[0], errors[0] = __fit(1, 1) # fit C11 fit_21, fit_21_error = __fit(2, 1) fit_31, fit_31_error = __fit(3, 1) finalCijs[6] = (fit_21 + fit_31) / 2 # fit C21+C31 errors[6] = S.sqrt((fit_21_error**2) / 4 + (fit_31_error**2) / 4) finalCijs[3], errors[3] = __fit(4, 4) # fit C44 else: print "Unsupported strain pattern" sys.exit(1) elif symmetryType == "Trigonal-high/Hexagonal": if S.all(strainsUsed.transpose() == S.array([[ 0.0, 0.0, 1.0, 0.0, 0.0, 0.0 ]])): # strain pattern e3 (hexagonal) # fit C13 + C23, and add to list (more values coming...) finalCijs[7], cij13, errors[7], er13 = __createListAndAppend( __fit(1, 3)) finalCijs[7], cij13, errors[7], er13 = __createListAndAppend( __fit(2, 3)) finalCijs[2], errors[2] = __fit(3, 3) # fit C33 elif S.all(strainsUsed.transpose() == S.array([[ 1.0, 0.0, 0.0, 1.0, 0.0, 0.0 ]])): # strain pattern e1+e4 (hexagonal) finalCijs[0], errors[0] = __fit(1, 1) # fit C11 finalCijs[6], errors[6] = __fit(2, 1) # fit C21 finalCijs[7], errors[7] = __appendOrReplace( cij13, er13, __fit(3, 1)) # fit C31 finalCijs[3], errors[3] = __fit(4, 4) # fit C44 elif S.all(strainsUsed.transpose() == S.array( [[1.0, 0.0, 0.0, 0.0, 0.0, 0.0]])): # strain pattern e1 (trigonal-high) finalCijs[0], errors[0] = __fit(1, 1) # fit C11 finalCijs[6], errors[6] = __fit(2, 1) # fit C21 finalCijs[7], errors[7] = __fit(3, 1) # fit C31 finalCijs[8], errors[8] = __fit(4, 1) # fit C41 # Should be zero? finalCijs[9], errors[9] = __fit(5,1) # fit C51 elif S.all(strainsUsed.transpose() == S.array( [[0.0, 0.0, 1.0, 1.0, 0.0, 0.0]])): # strain pattern e3+e4 (trigonal-high) # could recalculate C13/C14/C23/C24/C46 here, but won't just now finalCijs[2], errors[2] = __fit(3, 3) # fit C33 finalCijs[3], errors[3] = __fit(4, 4) # fit C44 else: print "Unsupported strain pattern" sys.exit(1) elif symmetryType == "Trigonal-low": if S.all(strainsUsed.transpose() == S.array( [[1.0, 0.0, 0.0, 0.0, 0.0, 0.0]])): # strain pattern e1 finalCijs[0], errors[0] = __fit(1, 1) # fit C11 finalCijs[6], errors[6] = __fit(2, 1) # fit C21 finalCijs[7], errors[7] = __fit(3, 1) # fit C31 finalCijs[8], errors[8] = __fit(4, 1) # fit C41 finalCijs[9], errors[9] = __fit(5, 1) # fit C51 elif S.all(strainsUsed.transpose() == S.array( [[0.0, 0.0, 1.0, 1.0, 0.0, 0.0]])): # strain pattern e3+e4 # could recalculate C13/C14/C23/C24/C46 here, but won't just now finalCijs[2], errors[2] = __fit(3, 3) # fit C33 finalCijs[3], errors[3] = __fit(4, 4) # fit C44 else: print "Unsupported strain pattern" sys.exit(1) elif symmetryType == "Tetragonal": if S.all(strainsUsed.transpose() == S.array( [[1.0, 0.0, 0.0, 1.0, 0.0, 0.0]])): # strain pattern e1+e4 finalCijs[0], errors[0] = __fit(1, 1) # fit C11 finalCijs[6], errors[6] = __fit(2, 1) # fit C21 finalCijs[7], errors[7] = __fit(3, 1) # fit C31 finalCijs[10], errors[10] = __fit(6, 1) # fit C61 finalCijs[3], errors[3] = __fit(4, 4) # fit C44 elif S.all(strainsUsed.transpose() == S.array( [[0.0, 0.0, 1.0, 0.0, 0.0, 1.0]])): # strain pattern e3+e6 finalCijs[2], errors[2] = __fit(3, 3) # fit C33 finalCijs[5], errors[5] = __fit(6, 6) # fit C66 else: print "Unsupported strain pattern" sys.exit(1) elif symmetryType == "Orthorhombic": if S.all(strainsUsed.transpose() == S.array( [[1.0, 0.0, 0.0, 1.0, 0.0, 0.0]])): # strain pattern e1+e4 finalCijs[0], errors[0] = __fit(1, 1) # fit C11 finalCijs[6], cij12, errors[6], er12 = __createListAndAppend( __fit(2, 1)) # fit C21 finalCijs[7], cij13, errors[7], er13 = __createListAndAppend( __fit(3, 1)) # fit C31 finalCijs[3], errors[3] = __fit(4, 4) # fit C44 elif S.all(strainsUsed.transpose() == S.array( [[0.0, 1.0, 0.0, 0.0, 1.0, 0.0]])): # strain pattern e2+e5 finalCijs[6], errors[6] = __appendOrReplace( cij12, er12, __fit(1, 2)) # fit C12 finalCijs[1], errors[1] = __fit(2, 2) # fit C22 finalCijs[11], cij23, errors[11], er23 = __createListAndAppend( __fit(3, 2)) # fit C32 finalCijs[4], errors[4] = __fit(5, 5) # fit C55 elif S.all(strainsUsed.transpose() == S.array( [[0.0, 0.0, 1.0, 0.0, 0.0, 1.0]])): # strain pattern e3+e6 finalCijs[7], errors[7] = __appendOrReplace( cij13, er13, __fit(1, 3)) # fit C13 finalCijs[11], errors[11] = __appendOrReplace( cij23, er23, __fit(2, 3)) # fit C23 finalCijs[2], errors[2] = __fit(3, 3) # fit C33 finalCijs[5], errors[5] = __fit(6, 6) # fit C66 else: print "Unsupported strain pattern" sys.exit(1) elif symmetryType == "Monoclinic": if S.all(strainsUsed.transpose() == S.array( [[1.0, 0.0, 0.0, 1.0, 0.0, 0.0]])): # strain pattern e1+e4 finalCijs[0], errors[0] = __fit(1, 1) # fit C11 finalCijs[6], cij12, errors[6], er12 = __createListAndAppend( __fit(2, 1)) # fit C21 finalCijs[7], cij13, errors[7], er13 = __createListAndAppend( __fit(3, 1)) # fit C31 finalCijs[3], errors[3] = __fit(4, 4) # fit C44 finalCijs[9], cij51, errors[9], er51 = __createListAndAppend( __fit(5, 1)) # fit C51 finalCijs[19], cij64, errors[19], er64 = __createListAndAppend( __fit(6, 4)) # fit C64 elif S.all(strainsUsed.transpose() == S.array( [[0.0, 0.0, 1.0, 0.0, 0.0, 1.0]])): # strain pattern e3+e6 finalCijs[7], errors[7] = __appendOrReplace( cij13, er13, __fit(1, 3)) # fit C13 finalCijs[11], cij23, errors[11], er23 = __createListAndAppend( __fit(2, 3)) # fit C23 finalCijs[2], errors[2] = __fit(3, 3) # fit C33 finalCijs[16], cij53, errors[16], er53 = __createListAndAppend( __fit(5, 3)) # fit C53 finalCijs[19], errors[19] = __appendOrReplace( cij64, er64, __fit(4, 6)) # fit C46 finalCijs[5], errors[5] = __fit(6, 6) # fit C66 elif S.all(strainsUsed.transpose() == S.array( [[0.0, 1.0, 0.0, 0.0, 0.0, 0.0]])): # strain pattern e2 finalCijs[6], errors[6] = __appendOrReplace( cij12, er12, __fit(1, 2)) # fit C12 finalCijs[1], errors[1] = __fit(2, 2) # fit C22 finalCijs[11], errors[11] = __appendOrReplace( cij23, er23, __fit(3, 2)) # fit C32 finalCijs[13], cij52, errors[13], er52 = __createListAndAppend( __fit(5, 2)) # fit C52 elif S.all(strainsUsed.transpose() == S.array( [[0.0, 0.0, 0.0, 0.0, 1.0, 0.0]])): # strain pattern e5 finalCijs[9], errors[9] = __appendOrReplace( cij51, er51, __fit(1, 5)) # fit C15 finalCijs[13], errors[13] = __appendOrReplace( cij52, er52, __fit(2, 5)) # fit C25 finalCijs[16], errors[16] = __appendOrReplace( cij53, er53, __fit(3, 5)) # fit C35 finalCijs[4], errors[4] = __fit(5, 5) # fit C55 else: print "Unsupported strain pattern" sys.exit(1) elif symmetryType == "Triclinic": if S.all(strainsUsed.transpose() == S.array( [[1.0, 0.0, 0.0, 0.0, 0.0, 0.0]])): # strain pattern e1 finalCijs[0], errors[0] = __fit(1, 1) # fit C11 finalCijs[6], cij12, errors[6], er12 = __createListAndAppend( __fit(2, 1)) # fit C21 finalCijs[7], cij13, errors[7], er13 = __createListAndAppend( __fit(3, 1)) # fit C31 finalCijs[8], cij14, errors[8], er14 = __createListAndAppend( __fit(4, 1)) # fit C41 finalCijs[9], cij15, errors[9], er15 = __createListAndAppend( __fit(5, 1)) # fit C51 finalCijs[10], cij16, errors[10], er16 = __createListAndAppend( __fit(6, 1)) # fit C61 elif S.all(strainsUsed.transpose() == S.array( [[0.0, 1.0, 0.0, 0.0, 0.0, 0.0]])): # strain pattern e2 finalCijs[6], errors[6] = __appendOrReplace( cij12, er12, __fit(1, 2)) # fit C12 finalCijs[1], errors[1] = __fit(2, 2) # fit C22 finalCijs[11], cij23, errors[11], er23 = __createListAndAppend( __fit(3, 2)) # fit C32 finalCijs[12], cij24, errors[12], er24 = __createListAndAppend( __fit(4, 2)) # fit C42 finalCijs[13], cij25, errors[13], er25 = __createListAndAppend( __fit(5, 2)) # fit C52 finalCijs[14], cij26, errors[14], er26 = __createListAndAppend( __fit(6, 2)) # fit C62 elif S.all(strainsUsed.transpose() == S.array( [[0.0, 0.0, 1.0, 0.0, 0.0, 0.0]])): # strain pattern e3 finalCijs[7], errors[7] = __appendOrReplace( cij13, er13, __fit(1, 3)) # fit C13 finalCijs[11], errors[11] = __appendOrReplace( cij23, er23, __fit(2, 3)) # fit C23 finalCijs[2], errors[2] = __fit(3, 3) # fit C33 finalCijs[15], cij34, errors[15], er34 = __createListAndAppend( __fit(4, 3)) # fit C43 finalCijs[16], cij35, errors[16], er35 = __createListAndAppend( __fit(5, 3)) # fit C53 finalCijs[17], cij36, errors[17], er36 = __createListAndAppend( __fit(6, 3)) # fit C63 elif S.all(strainsUsed.transpose() == S.array( [[0.0, 0.0, 0.0, 1.0, 0.0, 0.0]])): # strain pattern e4 finalCijs[8], errors[8] = __appendOrReplace( cij14, er14, __fit(1, 4)) # fit C14 finalCijs[12], errors[12] = __appendOrReplace( cij24, er24, __fit(2, 4)) # fit C24 finalCijs[15], errors[15] = __appendOrReplace( cij34, er34, __fit(3, 4)) # fit C34 finalCijs[3], errors[3] = __fit(4, 4) # fit C44 finalCijs[18], cij45, errors[18], er45 = __createListAndAppend( __fit(5, 4)) # fit C54 finalCijs[19], cij46, errors[19], er46 = __createListAndAppend( __fit(6, 4)) # fit C64 elif S.all(strainsUsed.transpose() == S.array( [[0.0, 0.0, 0.0, 0.0, 1.0, 0.0]])): # strain pattern e5 finalCijs[9], errors[9] = __appendOrReplace( cij15, er15, __fit(1, 5)) # fit C15 finalCijs[13], errors[13] = __appendOrReplace( cij25, er25, __fit(2, 5)) # fit C25 finalCijs[16], errors[16] = __appendOrReplace( cij35, er35, __fit(3, 5)) # fit C35 finalCijs[18], errors[18] = __appendOrReplace( cij45, er45, __fit(4, 5)) # fit C45 finalCijs[4], errors[4] = __fit(5, 5) # fit C55 finalCijs[20], cij56, errors[20], er56 = __createListAndAppend( __fit(6, 5)) # fit C65 elif S.all(strainsUsed.transpose() == S.array( [[0.0, 0.0, 0.0, 0.0, 0.0, 1.0]])): # strain pattern e6 finalCijs[10], errors[10] = __appendOrReplace( cij16, er16, __fit(1, 6)) # fit C16 finalCijs[14], errors[14] = __appendOrReplace( cij26, er26, __fit(2, 6)) # fit C26 finalCijs[17], errors[17] = __appendOrReplace( cij36, er36, __fit(3, 6)) # fit C36 finalCijs[19], errors[19] = __appendOrReplace( cij46, er46, __fit(4, 6)) # fit C46 finalCijs[20], errors[20] = __appendOrReplace( cij56, er56, __fit(5, 6)) # fit C56 finalCijs[5], errors[5] = __fit(6, 6) # fit C66 else: print "Unsupported strain pattern" sys.exit(1) else: print "Unsupported symmetry type. Exiting" sys.exit(1) if options.graphics: P.savefig(os.path.basename(seedname) + '_fits') cijdat.close() if symmetryType == "Trigonal-high/Hexagonal" or symmetryType == "Trigonal-low": # for these systems, C66 is calculated as a combination of the other Cijs. finalCijs[5] = 0.5 * (finalCijs[0] - finalCijs[6]) errors[5] = S.sqrt(0.25 * (errors[0]**2 + errors[6]**2)) c = cMatrix(symmetryType, TetrHigh) # Generate the 6x6 matrix of elastic constants # - negative values signify a symmetry relation finalCijMatrix = S.zeros((6, 6)) finalErrors = S.zeros((6, 6)) for i in range(0, 6): for j in range(0, 6): index = int(c[i, j]) if index > 0: finalCijMatrix[i, j] = finalCijs[index - 1] finalErrors[i, j] = errors[index - 1] elif index < 0: finalCijMatrix[i, j] = -finalCijs[-index - 1] finalErrors[i, j] = errors[-index - 1] # Tests if symmetryType == "Cubic": if finalCijs[3] <= 0: print "\n *** WARNING: C44 is less than or equal to zero ***\n" if finalCijs[0] <= abs(finalCijs[6]): print "\n *** WARNING: C11 is less than or equal to |C12| ***\n" if (finalCijs[0] + 2 * finalCijs[6]) <= 0: print "\n *** WARNING: C11+2C12 is less than or equal to zero ***\n" print "\n<>---------------------------- RESULTS ----------------------------------<>\n" print "Final Cij matrix (" + units + "):" print S.array2string(finalCijMatrix, max_line_width=130, suppress_small=True) print "\nErrors on Cij matrix (" + units + "):" print S.array2string(finalErrors, max_line_width=130, suppress_small=True) (sij, esij, covsij) = CijUtil.invertCij(finalCijMatrix, finalErrors) print "\nFinal Sij matrix (" + units + "-1):" print S.array2string(sij, max_line_width=130, suppress_small=True) print "\nErrors on Sij matrix (" + units + "-1):" print S.array2string(esij, max_line_width=130, suppress_small=True) print "\n<>----------------------------------------------------------------------<>\n" if symmetryType == "Cubic": print " Zener anisotropy index : %6.5f +/- %6.5f" % ( CijUtil.zenerAniso(finalCijMatrix, finalErrors)) print " Universal anisotropy index : %6.5f +/- %6.5f" % (CijUtil.uAniso( finalCijMatrix, finalErrors)) print " (Rangnthn and Ostoja-Starzewski, PRL 101, 055504)\n" (youngX, youngY, youngZ, eyoungX, eyoungY, eyoungZ, poissonXY, poissonXZ, poissonYX, poissonYZ, poissonZX, poissonZY, epoissonXY, epoissonXZ, epoissonYX, epoissonYZ, epoissonZX, epoissonZY) = CijUtil.youngsmod(finalCijMatrix, finalErrors) format = "%18s : %11.5f %8s" print "\n x y z" print "%18s : %11.5f %11.5f %11.5f %6s" % ("Young's Modulus", youngX, youngY, youngZ, units) print "%18s : %11.5f %11.5f %11.5f " % (" +/- ", eyoungX, eyoungY, eyoungZ) print "\n xy xz yx yz zx zy" format = "%18s : %6.5f %6.5f %6.5f %6.5f %6.5f %6.5f" print format % ("Poisson's Ratios", poissonXY, poissonXZ, poissonYX, poissonYZ, poissonZX, poissonZY) print format % (" +/-", epoissonXY, epoissonXZ, epoissonYX, epoissonYZ, epoissonZX, epoissonZY) print "\n<>--------------------- POLYCRYSTALLINE RESULTS -------------------------<>\n" (voigtB, reussB, voigtG, reussG, hillB, hillG, evB, erB, evG, erG, ehB, ehG) = CijUtil.polyCij(finalCijMatrix, finalErrors) format = "%16s : %11.5f %11.5f %11.5f %11.5f %11.5f %11.5f %6s" print " Voigt +/- Reuss +/- Hill +/-" print format % ("Bulk Modulus", voigtB, evB, reussB, erB, hillB, ehB, units) print format % ("Shear Modulus", voigtG, evG, reussG, erG, hillG, ehG, units) print "\n<>-----------------------------------------------------------------------<>\n" S.savetxt(seedname + '_cij.txt', finalCijMatrix) if options.latex: CijUtil.latexCij(finalCijMatrix, finalErrors, seedname + '.tex', options.latex_nt) if options.txt: CijUtil.txtCij(finalCijMatrix, options.txt)
return la.lu(A) def solveSystem(A,b): return la.solve(A,b) # <codecell> i = sp.arange(1500,2500+200,200) k = 1 y = [] ans = 0 for n in i: with timer(loops =20) as t: ans = t.time(addArray, sp.rand(n,n), sp.rand(n,n)) y.append(ans[0][0]) X = sp.row_stack([sp.log(i), sp.ones_like(i)]).T sol = la.lstsq(X, sp.log(y)) print sol[0][0] plt.loglog(i,y) plt.show() # <codecell> i = sp.arange(1500,2500+200,200) k = 1 y = [] ans = 0 for n in i: with timer(loops =20) as t: ans = t.time(invArray, sp.rand(n,n)) y.append(ans[0][0])