Skip to content

Instantly share code, notes, and snippets.

@suryadutta
Created March 3, 2018 20:37
Show Gist options
  • Save suryadutta/5563d9a21aa17b6275b9f337baf0ad4c to your computer and use it in GitHub Desktop.
Save suryadutta/5563d9a21aa17b6275b9f337baf0ad4c to your computer and use it in GitHub Desktop.
How to bin your data
def calculate_and_save_values(Msamp,Esamp,spin,num_analysis,index,temp,data_filename,corr_filename):
try:
M_mean = np.average(Msamp[-num_analysis:])
E_mean = np.average(Esamp[-num_analysis:])
M_std = np.std(Msamp[-num_analysis:])
E_std = np.std(Esamp[-num_analysis:])
M_std_array = []
for i in range(50):
bin_start = int(i*len(Msamp)/50)
bin_end = int((i+1)*len(Msamp)/50)
M_binned = Msamp[bin_start:bin_end]
M_binned_std = np.std(M_binned)
M_std_array.append(M_binned_std)
M_std_mean = np.average(M_std_array)
M_std_std = np.std(M_std_array)
data_array = [np.abs(M_mean),M_std,E_mean,E_std,M_std_mean,M_std_std]
#write data to CSV file
header_array = ['Temperature','Magnetization Mean','Magnetization Std Dev','Energy Mean','Energy Std Dev', 'CV Mean','CV Std Dev']
append_data_to_file(data_filename, header_array) if index == 0 else None
append_data_to_file(data_filename, data_array, temp)
#get correlation function
corr = compute_autocorrelation(spin)
#write correlation function to CSV file
header_array = ['Temperature','K','Spatial Spin Correlation']
append_data_to_file(corr_filename, header_array) if index == 0 else None
[append_data_to_file(corr_filename, corr_value, temp) for corr_value in corr]
return True
except:
logging.error("Temp="+str(temp)+": Statistical Calculation Failed. No Data Written.")
return False
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment