%load_ext autoreload
%autoreload 2
import re
import os
import sys
import shutil
from shutil import copyfile, copy2
from shutil import move
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import matplotlib.pylab as pylab
import seaborn as sns
from scipy import stats
# Cause plots to be displayed in the notebook:
%matplotlib inline
import subprocess
from matplotlib import cm
from latt2D_modules import calc_diffuse, calc_diffuse_cfs3, calc_diffuse_cfs4
from latt2D_modules import get_occ_map, get_2D_occ_map_from_seq,store_occ_map_as_seq
from latt2D_modules import plot_occ_map,read_bin,output_16bit_pgm
import time
from sklearn.metrics import r2_score
from sklearn.metrics import accuracy_score
from sklearn.metrics import mean_squared_error
from sklearn.metrics import mean_absolute_error
# read in all the correlation datum for CFS2 griding
df1=pd.read_csv('output_correlations_1000.csv')
df2=pd.read_csv('output_correlations_2000.csv')
df3=pd.read_csv('output_correlations_3000.csv')
df_corfu=pd.concat([df1,df2,df3],axis=0)
df_corfu=df_corfu.drop('Unnamed: 0',axis=1)
df_corfu.reset_index(inplace=True,drop=True)
df_corfu.describe()
00 | 01 | 10 | 11 | |
---|---|---|---|---|
count | 5000.000000 | 5000.000000 | 5000.000000 | 5000.000000 |
mean | 0.999619 | -0.011148 | -0.007468 | 0.001628 |
std | 0.000528 | 0.469855 | 0.476303 | 0.475627 |
min | 0.994465 | -0.921764 | -0.920231 | -0.915264 |
25% | 0.999498 | -0.401060 | -0.401672 | -0.403457 |
50% | 0.999836 | -0.017684 | -0.014525 | 0.009275 |
75% | 0.999959 | 0.371125 | 0.387132 | 0.392816 |
max | 1.000000 | 0.913594 | 0.927990 | 0.916544 |
# read in all the correlation datum for CFS3 griding
df_corfu_cfs3=pd.read_csv('output_correlations_cfs3_5000.csv')
df_corfu_cfs3=df_corfu_cfs3.drop('Unnamed: 0',axis=1)
df_corfu_cfs3.reset_index(inplace=True,drop=True)
df_corfu_cfs3.describe()
00 | 01 | 02 | 10 | 11 | 12 | 20 | 21 | 22 | |
---|---|---|---|---|---|---|---|---|---|
count | 5000.000000 | 5000.000000 | 5000.000000 | 5000.000000 | 5000.000000 | 5000.000000 | 5000.000000 | 5000.000000 | 5000.000000 |
mean | 0.999595 | -0.001524 | 0.030086 | 0.003661 | 0.001820 | -0.005355 | 0.035394 | -0.002054 | 0.034959 |
std | 0.000552 | 0.313728 | 0.397112 | 0.315408 | 0.337084 | 0.380114 | 0.402072 | 0.386541 | 0.363526 |
min | 0.995267 | -0.879424 | -0.859210 | -0.895424 | -0.839097 | -0.862953 | -0.867137 | -0.882524 | -0.878833 |
25% | 0.999462 | -0.244261 | -0.297618 | -0.240006 | -0.269163 | -0.318172 | -0.304072 | -0.312611 | -0.255047 |
50% | 0.999815 | -0.000518 | 0.028689 | 0.003190 | 0.006080 | -0.016108 | 0.030357 | -0.011228 | 0.043116 |
75% | 0.999959 | 0.239661 | 0.356709 | 0.247908 | 0.270532 | 0.302623 | 0.375400 | 0.324498 | 0.320213 |
max | 1.000000 | 0.906503 | 0.846394 | 0.870275 | 0.907827 | 0.850767 | 0.872361 | 0.865904 | 0.864945 |
# read in all the correlation datum for CFS4 griding
df_corfu_cfs4=pd.read_csv('output_correlations_cfs4_5000.csv')
df_corfu_cfs4=df_corfu_cfs4.drop('Unnamed: 0',axis=1)
df_corfu_cfs4.reset_index(inplace=True,drop=True)
df_corfu_cfs4.describe()
00 | 01 | 02 | 03 | 10 | 11 | 12 | 13 | 20 | 21 | 22 | 23 | 30 | 31 | 32 | 33 | |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
count | 5000.000000 | 5000.000000 | 5000.000000 | 5000.000000 | 5000.000000 | 5000.000000 | 5000.000000 | 5000.000000 | 5000.000000 | 5000.000000 | 5000.000000 | 5000.000000 | 5000.000000 | 5000.000000 | 5000.000000 | 5000.000000 |
mean | 0.999615 | 0.001224 | 0.013416 | 0.004695 | -0.002513 | 0.000781 | -0.004900 | -0.002588 | 0.015622 | -0.004634 | 0.024057 | 0.000096 | -0.006685 | -0.005733 | 0.004220 | -0.000245 |
std | 0.000545 | 0.256659 | 0.289073 | 0.339949 | 0.255911 | 0.276429 | 0.295587 | 0.327140 | 0.288942 | 0.291742 | 0.301712 | 0.314419 | 0.339040 | 0.329032 | 0.315226 | 0.301563 |
min | 0.993853 | -0.763325 | -0.757056 | -0.772841 | -0.797169 | -0.780998 | -0.840339 | -0.769641 | -0.718423 | -0.826340 | -0.827252 | -0.755431 | -0.753807 | -0.790508 | -0.763203 | -0.771482 |
25% | 0.999498 | -0.187328 | -0.208502 | -0.274235 | -0.195215 | -0.208425 | -0.235281 | -0.265685 | -0.207778 | -0.228512 | -0.211749 | -0.246443 | -0.283774 | -0.267258 | -0.251281 | -0.232110 |
50% | 0.999836 | 0.004515 | 0.015746 | 0.009579 | -0.000213 | 0.001466 | -0.008188 | -0.009795 | 0.013938 | -0.003211 | 0.028741 | -0.003223 | -0.003555 | -0.006739 | 0.001586 | 0.002956 |
75% | 0.999959 | 0.188684 | 0.232081 | 0.282336 | 0.185581 | 0.212943 | 0.228630 | 0.263630 | 0.239518 | 0.222379 | 0.265429 | 0.246362 | 0.265493 | 0.257337 | 0.260744 | 0.236522 |
max | 1.000000 | 0.803056 | 0.771075 | 0.780769 | 0.761024 | 0.795136 | 0.772769 | 0.768304 | 0.777369 | 0.774256 | 0.812800 | 0.772800 | 0.766144 | 0.766348 | 0.759892 | 0.751744 |
# read in all the correlation datum for CFS4 griding
df_corfu_cfs4_big=pd.read_csv('output_correlations_cfs4_big_5000.csv')
df_corfu_cfs4_big=df_corfu_cfs4_big.drop('Unnamed: 0',axis=1)
df_corfu_cfs4_big.reset_index(inplace=True,drop=True)
df_corfu_cfs4_big.describe()
00 | 01 | 02 | 03 | 10 | 11 | 12 | 13 | 20 | 21 | 22 | 23 | 30 | 31 | 32 | 33 | |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
count | 5000.000000 | 5000.000000 | 5000.000000 | 5000.000000 | 5000.000000 | 5000.000000 | 5000.000000 | 5000.000000 | 5000.000000 | 5000.000000 | 5000.000000 | 5000.000000 | 5000.000000 | 5000.000000 | 5000.000000 | 5000.000000 |
mean | 0.999891 | 0.004946 | 0.012484 | 0.001425 | 0.004712 | 0.007505 | -0.004842 | 0.000767 | 0.023760 | -0.005177 | 0.018360 | 0.001835 | -0.006725 | 0.002540 | -0.000443 | 0.009603 |
std | 0.000154 | 0.254295 | 0.293658 | 0.338722 | 0.256082 | 0.269184 | 0.288865 | 0.325434 | 0.292073 | 0.298116 | 0.301633 | 0.311789 | 0.345432 | 0.326947 | 0.312520 | 0.303450 |
min | 0.998702 | -0.759754 | -0.733507 | -0.766066 | -0.762588 | -0.772244 | -0.818260 | -0.795656 | -0.785648 | -0.795576 | -0.730039 | -0.743131 | -0.800899 | -0.791766 | -0.787836 | -0.751304 |
25% | 0.999858 | -0.186765 | -0.210973 | -0.275793 | -0.183321 | -0.199189 | -0.226696 | -0.259579 | -0.198752 | -0.237934 | -0.222335 | -0.252029 | -0.287547 | -0.264763 | -0.244908 | -0.233233 |
50% | 0.999949 | 0.009490 | 0.007923 | 0.009292 | 0.001884 | 0.005712 | -0.007321 | 0.004972 | 0.027776 | -0.010679 | 0.028775 | 0.007744 | -0.016932 | -0.000269 | -0.006122 | 0.011245 |
75% | 0.999988 | 0.196134 | 0.237760 | 0.282020 | 0.195265 | 0.209314 | 0.222980 | 0.266406 | 0.254085 | 0.230358 | 0.248921 | 0.243817 | 0.276468 | 0.266467 | 0.252579 | 0.255392 |
max | 1.000000 | 0.789808 | 0.762998 | 0.760733 | 0.785265 | 0.870522 | 0.792439 | 0.776439 | 0.766461 | 0.777772 | 0.853416 | 0.783755 | 0.778503 | 0.798040 | 0.774717 | 0.792878 |
# helper functions
def plot_selected_image_from_cfs2(k,ls=12):
fig, axes = plt.subplots(1, 2, figsize=(10,6))
occ2d=get_2D_occ_map_from_seq('./image_inputs_seq/ising2D_seq_%s.dat'%(str(k).zfill(6)))
axes[0].imshow(np.transpose(occ2d),interpolation='nearest',cmap='gray')
axes[0].set_xlabel('a-axis',size=ls)
axes[0].set_ylabel('b-axis',size=ls)
imdat = read_bin('./image_inputs_bin/hk0_%s.bin'%str(k).zfill(6), npixels=64, offset=1280)
axes[1].imshow(np.flip(imdat,0),cmap='gray')
axes[1].set_xlabel('h-axis',size=ls)
axes[1].set_ylabel('k-axis',size=ls)
def plot_selected_image_from_cfs3(k,ls=12):
fig, axes = plt.subplots(1, 2, figsize=(10,6))
occ2d=get_2D_occ_map_from_seq('./cfs3_image_inputs_seq/ising2D_seq_%s.dat'%(str(k).zfill(6)))
axes[0].imshow(np.transpose(occ2d),interpolation='nearest',cmap='gray')
axes[0].set_xlabel('a-axis',size=ls)
axes[0].set_ylabel('b-axis',size=ls)
imdat = read_bin('./cfs3_image_inputs_bin/hk0_%s.bin'%str(k).zfill(6), npixels=64, offset=1280)
axes[1].imshow(np.flip(imdat,0),cmap='gray')
axes[1].set_xlabel('h-axis',size=ls)
axes[1].set_ylabel('k-axis',size=ls)
def plot_selected_image_from_cfs4(k,ls=12):
fig, axes = plt.subplots(1, 2, figsize=(10,6))
occ2d=get_2D_occ_map_from_seq('./cfs4_image_inputs_seq/ising2D_seq_%s.dat'%(str(k).zfill(6)))
axes[0].imshow(np.transpose(occ2d),interpolation='nearest',cmap='gray')
axes[0].set_xlabel('a-axis',size=ls)
axes[0].set_ylabel('b-axis',size=ls)
imdat = read_bin('./cfs4_image_inputs_bin/hk0_%s.bin'%str(k).zfill(6), npixels=64, offset=1280)
axes[1].imshow(np.flip(imdat,0),cmap='gray')
axes[1].set_xlabel('h-axis',size=ls)
axes[1].set_ylabel('k-axis',size=ls)
def plot_selected_image_from_cfs4_big(k,ls=12):
fig, axes = plt.subplots(1, 2, figsize=(10,6))
occ2d=get_2D_occ_map_from_seq('./cfs4_big_image_inputs_seq/ising2D_seq_%s.dat'%(str(k).zfill(6)))
axes[0].imshow(np.transpose(occ2d),interpolation='nearest',cmap='gray')
axes[0].set_xlabel('a-axis',size=ls)
axes[0].set_ylabel('b-axis',size=ls)
imdat = read_bin('./cfs4_big_image_inputs_bin/hk0_%s.bin'%str(k).zfill(6), npixels=64, offset=1280)
axes[1].imshow(np.flip(imdat,0),cmap='gray')
axes[1].set_xlabel('h-axis',size=ls)
axes[1].set_ylabel('k-axis',size=ls)
def plot_from_exp1(ls=12):
fig, axes = plt.subplots(1, 2, figsize=(10,6))
occ3D=get_occ_map('./expfiles_1/ising2D_occ.txt')
axes[0].imshow(np.transpose(occ3D[:,:,0]),interpolation='nearest',cmap='gray')
axes[0].set_xlabel('a-axis',size=ls)
axes[0].set_ylabel('b-axis',size=ls)
imdat = read_bin('./expfiles_1/hk0.bin', npixels=64, offset=1280)
axes[1].imshow(np.flip(imdat,0),cmap='gray')
axes[1].set_xlabel('h-axis',size=ls)
axes[1].set_ylabel('k-axis',size=ls)
corr_out=np.loadtxt('expfiles_1/corr.out')
return corr_out
def plot_corfunc(df,cfs=2):
corrf=df.values.reshape((cfs,-1))
sns.heatmap(corrf, cmap='bwr',square=True, annot=True,annot_kws={"fontsize":20},vmax=1.0,vmin=-1.0 ,cbar_kws={'shrink':0.85} )
def get_metrics_compare_regen_images(k_test,exp=1):
corr_out_0=df_corfu.iloc[k_test].values
corr_out_1=np.loadtxt('expfiles_%d/corr.out'%exp)
imdat_0 = read_bin('./image_inputs_bin/hk0_%s.bin'%str(k_test).zfill(6), npixels=64, offset=1280)
imdat_1 = read_bin('./expfiles_%d/hk0.bin'%exp, npixels=64, offset=1280)
r2c = r2_score(corr_out_0.flatten(), corr_out_1.flatten())
msec=mean_squared_error(corr_out_0,corr_out_1.flatten())
maec=mean_absolute_error(corr_out_0,corr_out_1.flatten())
r2i=r2_score(imdat_0.flatten(),imdat_1.flatten())
msei=mean_squared_error(imdat_0.flatten(),imdat_1.flatten())
maei=mean_absolute_error(imdat_0.flatten(),imdat_1.flatten())
return [r2c,msec,maec,r2i,msei,maei]
def get_metrics_compare_regen_images_cfs3(k_test,corr_out_0,exp=1):
corr_out_1=np.loadtxt('expfiles_%d/corr.out'%exp)
imdat_0 = read_bin('./cfs3_image_inputs_bin/hk0_%s.bin'%str(k_test).zfill(6), npixels=64, offset=1280)
imdat_1 = read_bin('./expfiles_%d/hk0.bin'%exp, npixels=64, offset=1280)
r2c = r2_score(corr_out_0.flatten(), corr_out_1.flatten())
msec=mean_squared_error(corr_out_0,corr_out_1.flatten())
maec=mean_absolute_error(corr_out_0,corr_out_1.flatten())
r2i=r2_score(imdat_0.flatten(),imdat_1.flatten())
msei=mean_squared_error(imdat_0.flatten(),imdat_1.flatten())
maei=mean_absolute_error(imdat_0.flatten(),imdat_1.flatten())
return [r2c,msec,maec,r2i,msei,maei]
def get_metrics_compare_regen_images_cfs4(k_test,corr_out_0,exp=1):
corr_out_1=np.loadtxt('expfiles_%d/corr.out'%exp)
imdat_0 = read_bin('./cfs4_image_inputs_bin/hk0_%s.bin'%str(k_test).zfill(6), npixels=64, offset=1280)
imdat_1 = read_bin('./expfiles_%d/hk0.bin'%exp, npixels=64, offset=1280)
r2c = r2_score(corr_out_0.flatten(), corr_out_1.flatten())
msec=mean_squared_error(corr_out_0,corr_out_1.flatten())
maec=mean_absolute_error(corr_out_0,corr_out_1.flatten())
r2i=r2_score(imdat_0.flatten(),imdat_1.flatten())
msei=mean_squared_error(imdat_0.flatten(),imdat_1.flatten())
maei=mean_absolute_error(imdat_0.flatten(),imdat_1.flatten())
return [r2c,msec,maec,r2i,msei,maei]
#
# k=np.random.randint(0,5000,1)[0] # grab a random image from the 5000
# print(k)
k=0
plot_selected_image_from_cfs2(k,ls=20)
# print(df_corfu.iloc[k])
plot_corfunc(df_corfu.iloc[k],cfs=2)
k=0
plot_selected_image_from_cfs3(k,ls=20)
plot_corfunc(df_corfu_cfs3.iloc[k],cfs=3)
plot_selected_image_from_cfs4(k,ls=20)
plot_corfunc(df_corfu_cfs4.iloc[k],cfs=4)
corrin=df_corfu.iloc[k].values
fhout=open('corr.in','w')
fhout.write("%.6f %.6f\n%.6f %.6f\n"%(corrin[0],corrin[1],corrin[2],corrin[3]))
fhout.close()
calc_diffuse(iconc,1,icycles,ianneal,1)
corr_out=plot_from_exp1()
corr_out.flatten()
my_metrics=get_metrics_compare_regen_images(k)
my_metrics
iconc=0.50 # spin concentration
cread=0 # reading correlation function from input file otherwise it will be generated randomly
'corr.in'
'jswitch.in'
icycles=200 # MC cycles
ianneal=200 # MC input
corrin=np.array([[1.000000, 0.000000],
[0.00000, 0.000000]])
fhout=open('corr.in','w')
fhout.write("%.6f %.6f\n%.6f %.6f\n"%(corrin[0,0],corrin[0,1],corrin[1,0],corrin[1,1]))
fhout.close()
jsw=np.array([[1, 1],
[1, 1]])
fhout=open('jswitch.in','w')
fhout.write("%d %d\n%d %d\n"%(jsw[0,0],jsw[0,1],jsw[1,0],jsw[1,1]))
fhout.close()
iconc=0.50 # spin concentration
cread=0 # reading correlation function from input file otherwise it will be generated randomly
icycles=200 # MC cycles
ianneal=200 # MC input
df_metrics=pd.DataFrame(columns=['r2_c','mse_c','mae_c','r2_i','mse_i','mae_i'])
for t in range(1000):
k=np.random.randint(0,5000,1)[0] # grab a random image from the 5000
corrin=df_corfu.iloc[k].values
fhout=open('corr.in','w')
fhout.write("%.6f %.6f\n%.6f %.6f\n"%(corrin[0],corrin[1],corrin[2],corrin[3]))
fhout.close()
calc_diffuse(iconc,1,icycles,ianneal,1) # do the regen on the image
df_metrics.loc[t]=get_metrics_compare_regen_images(k)
df_metrics.describe()
r2_c | mse_c | mae_c | r2_i | mse_i | mae_i | |
---|---|---|---|---|---|---|
count | 1000.000000 | 1000.000000 | 1000.000000 | 1000.000000 | 1000.000000 | 1000.000000 |
mean | 0.998620 | 0.000232 | 0.010299 | 0.575500 | 0.140021 | 0.119456 |
std | 0.005136 | 0.000285 | 0.006057 | 0.195479 | 0.130911 | 0.011697 |
min | 0.902308 | 0.000002 | 0.000803 | -0.579130 | 0.022547 | 0.081005 |
25% | 0.998856 | 0.000057 | 0.006010 | 0.498459 | 0.072451 | 0.112157 |
50% | 0.999524 | 0.000134 | 0.008845 | 0.619858 | 0.106709 | 0.118541 |
75% | 0.999809 | 0.000285 | 0.013200 | 0.701552 | 0.158587 | 0.125467 |
max | 0.999996 | 0.002604 | 0.039636 | 0.946180 | 1.147219 | 0.174708 |
df_metrics.to_csv('regen_metrics_cs2.csv')
iconc=0.50 # spin concentration
cread=0 # reading correlation function from input file otherwise it will be generated randomly
icycles=200 # MC cycles
ianneal=200 # MC input
df_metrics_cfs3=pd.DataFrame(columns=['r2_c','mse_c','mae_c','r2_i','mse_i','mae_i'])
for t in range(1000):
k=np.random.randint(0,5000,1)[0] # grab a random image from the 5000
corrin=df_corfu_cfs3.iloc[k].values
fhout=open('corr.in','w')
fhout.write("%.6f %.6f %.6f\n%.6f %.6f %.6f\n%.6f %.6f %.6f\n"%(tuple(corrin.flatten())))
fhout.close()
calc_diffuse_cfs3(iconc,1,icycles,ianneal,1) # do the regen on the image
df_metrics_cfs3.loc[t]=get_metrics_compare_regen_images_cfs3(k,corrin)
df_metrics_cfs3.to_csv('regen_metrics_cfs3.csv')
df_metrics_cfs3.head()
r2_c | mse_c | mae_c | r2_i | mse_i | mae_i | |
---|---|---|---|---|---|---|
0 | 0.999792 | 0.000048 | 0.005723 | 0.868685 | 0.071819 | 0.096689 |
1 | 0.993157 | 0.000539 | 0.018862 | 0.577352 | 0.077156 | 0.118850 |
2 | 0.999662 | 0.000056 | 0.006447 | 0.462761 | 0.087680 | 0.124752 |
3 | 0.998844 | 0.000208 | 0.011734 | 0.452269 | 0.132856 | 0.122487 |
4 | 0.999492 | 0.000103 | 0.008789 | 0.550464 | 0.109030 | 0.130901 |
iconc=0.50 # spin concentration
cread=0 # reading correlation function from input file otherwise it will be generated randomly
icycles=300 # MC cycles
ianneal=300 # MC input
df_metrics_cfs4=pd.DataFrame(columns=['r2_c','mse_c','mae_c','r2_i','mse_i','mae_i'])
for t in range(1000):
k=np.random.randint(0,5000,1)[0] # grab a random image from the 5000
corrin=df_corfu_cfs4.iloc[k].values
fhout=open('corr.in','w')
fhout.write("%.6f %.6f %.6f %.6f\n%.6f %.6f %.6f %.6f\n%.6f %.6f %.6f %.6f\n%.6f %.6f %.6f %.6f\n"%(tuple(corrin.flatten())))
fhout.close()
calc_diffuse_cfs4(iconc,1,icycles,ianneal,1) # do the regen on the image
df_metrics_cfs4.loc[t]=get_metrics_compare_regen_images_cfs4(k,corrin)
#df_metrics_cfs4.to_csv('regen_metrics_cfs4.csv')
df_metrics_cfs4.describe()
r2_c | mse_c | mae_c | r2_i | mse_i | mae_i | |
---|---|---|---|---|---|---|
count | 1000.000000 | 1000.000000 | 1000.000000 | 1000.000000 | 1000.000000 | 1000.000000 |
mean | 0.998680 | 0.000176 | 0.009439 | 0.560471 | 0.167191 | 0.121729 |
std | 0.002599 | 0.000468 | 0.004962 | 0.245160 | 0.113352 | 0.014032 |
min | 0.928371 | 0.000014 | 0.002711 | -1.481295 | 0.037417 | 0.082284 |
25% | 0.998472 | 0.000070 | 0.006565 | 0.453696 | 0.101437 | 0.112689 |
50% | 0.999132 | 0.000113 | 0.008486 | 0.611818 | 0.136019 | 0.120670 |
75% | 0.999516 | 0.000192 | 0.010997 | 0.717961 | 0.192997 | 0.130327 |
max | 0.999919 | 0.013512 | 0.091849 | 0.913122 | 1.459778 | 0.179457 |
df_metrics['gen_mode']=['cfs2' for i in range(1000)]
df_metrics_cfs3['gen_mode']=['cfs3' for i in range(1000)]
df_metrics_cfs4['gen_mode']=['cfs4' for i in range(1000)]
df_metrics_all=pd.concat([df_metrics,df_metrics_cfs3,df_metrics_cfs4],axis=0)
ax=sns.histplot(data=df_metrics_all,x='r2_c',hue='gen_mode',alpha=0.3,kde=True,stat='density',line_kws={'linewidth':2,'alpha':1.0})
ax=plt.xlim([0.99,1.0])
sns.histplot(data=df_metrics_all,x='mse_c',hue='gen_mode',alpha=0.3,kde=True,stat='density',line_kws={'linewidth':2,'alpha':1.0})
ax=plt.xlim([0.0,0.002])
sns.histplot(data=df_metrics_all,x='mae_c',hue='gen_mode',alpha=0.3,kde=True,stat='density',line_kws={'linewidth':2,'alpha':1.0})
ax=plt.xlim([0.0,0.06])
sns.histplot(data=df_metrics_all,x='r2_i',hue='gen_mode',alpha=0.3,kde=True,stat='density',line_kws={'linewidth':2,'alpha':1.0})
ax=plt.xlim([-1.0,1.00])
sns.histplot(data=df_metrics_all,x='mse_i',hue='gen_mode',alpha=0.3,kde=True,stat='density',line_kws={'linewidth':2,'alpha':1.0})
ax=plt.xlim([0.0,0.8])
sns.histplot(data=df_metrics_all,x='mae_i',hue='gen_mode',alpha=0.3,kde=True,stat='density',line_kws={'linewidth':2,'alpha':1.0})
<AxesSubplot: xlabel='mae_i', ylabel='Density'>
k=0
imdat = read_bin('./cfs4_image_inputs_bin/hk0_%s.bin'%str(k).zfill(6), npixels=64, offset=1280)
fig, ax = plt.subplots(1, 3, figsize=(12,3))
ax[0].imshow(np.flip(imdat,0),cmap='gray')
sns.histplot(data=imdat.flatten(),ax=ax[1],color='r', bins=64,kde=True,stat='density')
ax[1].set_xlim([0.0,0.8])
sns.histplot(data=np.log(imdat.flatten()),ax=ax[2],color='b', bins=64,kde=True,stat='density')
# ax[2].set_xlim([0.0,2.0])
<AxesSubplot: ylabel='Density'>
k=0
imdat = read_bin('./cfs3_image_inputs_bin/hk0_%s.bin'%str(k).zfill(6), npixels=64, offset=1280)
fig, ax = plt.subplots(1, 3, figsize=(14,3))
ax[0].imshow(np.flip(imdat,0),cmap='gray')
sns.histplot(data=imdat.flatten(),ax=ax[1],color='r', bins=64,kde=True,stat='density')
ax[1].set_xlim([0.0,0.8])
sns.histplot(data=np.log(imdat.flatten()),ax=ax[2],color='b', bins=64,kde=True,stat='density')
# ax[2].set_xlim([0.0,2.0])
<AxesSubplot: ylabel='Density'>
k=0
imdat = read_bin('./image_inputs_bin/hk0_%s.bin'%str(k).zfill(6), npixels=64, offset=1280)
fig, ax = plt.subplots(1, 3, figsize=(14,3))
ax[0].imshow(np.flip(imdat,0),cmap='gray')
sns.histplot(data=imdat.flatten(),ax=ax[1],color='r', bins=64,kde=True,stat='density')
ax[1].set_xlim([0.0,0.8])
sns.histplot(data=np.log(imdat.flatten()),ax=ax[2],color='b', bins=64,kde=True,stat='density')
# ax[2].set_xlim([0.0,2.0])
<AxesSubplot: ylabel='Density'>
from matplotlib.animation import FuncAnimation
from IPython.display import HTML
# Create a figure and axis for the plot
rows,cols =2,2
fig, axes = plt.subplots(rows, cols, figsize=(6,6))
# Define a function to update the plot
def update_plot(i):
k=np.random.randint(0,5000,1)[0] # grab a random image from the 5000
# Update the data for the plot
# plt.title(get_metrics_compare_regen_images(k))
occ2d=get_2D_occ_map_from_seq('./image_inputs_seq/ising2D_seq_%s.dat'%str(k).zfill(6))
axes[0,0].imshow(np.transpose(occ2d),interpolation='nearest',cmap='gray')
axes[0,0].axis("off")
imdat = read_bin('./image_inputs_bin/hk0_%s.bin'%str(k).zfill(6), npixels=64, offset=1280)
axes[0,1].imshow(np.flip(imdat,0),cmap='gray')
axes[0,1].axis("off")
corrin=df_corfu.iloc[k].values
fhout=open('corr.in','w')
fhout.write("%.6f %.6f\n%.6f %.6f\n"%(corrin[0],corrin[1],corrin[2],corrin[3]))
fhout.close()
calc_diffuse(iconc,1,icycles,ianneal,1) # do the regen on the image
occ3D=get_occ_map('./expfiles_1/ising2D_occ.txt')
axes[1,0].imshow(np.transpose(occ3D[:,:,0]),interpolation='nearest',cmap='gray')
axes[1,0].axis("off")
imdat = read_bin('./expfiles_1/hk0.bin', npixels=64, offset=1280)
axes[1,1].imshow(np.flip(imdat,0),cmap='gray')
axes[1,1].axis("off")
my_metrics=get_metrics_compare_regen_images(k)
fig.suptitle(" metrics(r2, mse, mae)\n corrfunc: %.3f, %.3f, %.3f \n FT_metrics: %.3f, %.3f, %.3f " %(tuple(my_metrics)), fontsize=10)
# df_metrics.loc[t]=get_metrics_compare_regen_images(k)
# update_plot(1)
# Create an animation object
ani = FuncAnimation(fig, update_plot, frames=10, interval=200)
# Display the animation in the notebook
# HTML(ani.to_jshtml())
HTML(ani.to_jshtml())
from matplotlib import animation
f = './MC_regen_error_cfs2.gif'
writergif = animation.PillowWriter(fps=1)
ani.save(f, writer=writergif)
from matplotlib.animation import FuncAnimation
from IPython.display import HTML
# Create a figure and axis for the plot
rows,cols =2,2
fig, axes = plt.subplots(rows, cols, figsize=(6,6))
# Define a function to update the plot
def update_plot_cfs3(i):
k=np.random.randint(0,5000,1)[0] # grab a random image from the 5000
# Update the data for the plot
# plt.title(get_metrics_compare_regen_images(k))
occ2d=get_2D_occ_map_from_seq('./cfs3_image_inputs_seq/ising2D_seq_%s.dat'%str(k).zfill(6))
axes[0,0].imshow(np.transpose(occ2d),interpolation='nearest',cmap='gray')
axes[0,0].axis("off")
imdat = read_bin('./cfs3_image_inputs_bin/hk0_%s.bin'%str(k).zfill(6), npixels=64, offset=1280)
axes[0,1].imshow(np.flip(imdat,0),cmap='gray')
axes[0,1].axis("off")
corrin=df_corfu_cfs3.iloc[k].values
fhout=open('corr.in','w')
fhout.write("%.6f %.6f %.6f\n%.6f %.6f %.6f\n%.6f %.6f %.6f\n"%(tuple(corrin.flatten())))
fhout.close()
calc_diffuse_cfs3(iconc,1,icycles,ianneal,1) # do the regen on the image
occ3D=get_occ_map('./expfiles_1/ising2D_occ.txt')
axes[1,0].imshow(np.transpose(occ3D[:,:,0]),interpolation='nearest',cmap='gray')
axes[1,0].axis("off")
imdat = read_bin('./expfiles_1/hk0.bin', npixels=64, offset=1280)
axes[1,1].imshow(np.flip(imdat,0),cmap='gray')
axes[1,1].axis("off")
my_metrics=get_metrics_compare_regen_images_cfs3(k,corrin)
fig.suptitle(" metrics(r2, mse, mae)\n corrfunc: %.3f, %.3f, %.3f \n FT_metrics: %.3f, %.3f, %.3f " %(tuple(my_metrics)), fontsize=10)
# df_metrics.loc[t]=get_metrics_compare_regen_images(k)
# update_plot(1)
# Create an animation object
ani = FuncAnimation(fig, update_plot_cfs3, frames=10, interval=200)
# Display the animation in the notebook
# HTML(ani.to_jshtml())
from matplotlib import animation
f = './MC_regen_error_cfs3.gif'
writergif = animation.PillowWriter(fps=1)
ani.save(f, writer=writergif)
from matplotlib.animation import FuncAnimation
from IPython.display import HTML
# Create a figure and axis for the plot
rows,cols =2,2
fig, axes = plt.subplots(rows, cols, figsize=(6,6))
# Define a function to update the plot
def update_plot_cfs4(i):
k=np.random.randint(0,5000,1)[0] # grab a random image from the 5000
# Update the data for the plot
# plt.title(get_metrics_compare_regen_images(k))
occ2d=get_2D_occ_map_from_seq('./cfs4_image_inputs_seq/ising2D_seq_%s.dat'%str(k).zfill(6))
axes[0,0].imshow(np.transpose(occ2d),interpolation='nearest',cmap='gray')
axes[0,0].axis("off")
imdat = read_bin('./cfs4_image_inputs_bin/hk0_%s.bin'%str(k).zfill(6), npixels=64, offset=1280)
axes[0,1].imshow(np.flip(imdat,0),cmap='gray')
axes[0,1].axis("off")
corrin=df_corfu_cfs4.iloc[k].values
fhout=open('corr.in','w')
fhout.write("%.6f %.6f %.6f %.6f\n%.6f %.6f %.6f %.6f\n%.6f %.6f %.6f %.6f\n%.6f %.6f %.6f %.6f\n"%(tuple(corrin.flatten())))
fhout.close()
calc_diffuse_cfs4(iconc,1,icycles,ianneal,1) # do the regen on the image
occ3D=get_occ_map('./expfiles_1/ising2D_occ.txt')
axes[1,0].imshow(np.transpose(occ3D[:,:,0]),interpolation='nearest',cmap='gray')
axes[1,0].axis("off")
imdat = read_bin('./expfiles_1/hk0.bin', npixels=64, offset=1280)
axes[1,1].imshow(np.flip(imdat,0),cmap='gray')
axes[1,1].axis("off")
my_metrics=get_metrics_compare_regen_images_cfs4(k,corrin)
fig.suptitle(" metrics(r2, mse, mae)\n corrfunc: %.3f, %.3f, %.3f \n FT_metrics: %.3f, %.3f, %.3f " %(tuple(my_metrics)), fontsize=10)
# df_metrics.loc[t]=get_metrics_compare_regen_images(k)
# update_plot(1)
# Create an animation object
ani = FuncAnimation(fig, update_plot_cfs4, frames=10, interval=200)
# Display the animation in the notebook
# HTML(ani.to_jshtml())
from matplotlib import animation
f = './MC_regen_error_cfs4.gif'
writergif = animation.PillowWriter(fps=1)
ani.save(f, writer=writergif)
better to do this in a seperate notebook
The intensity data is log normally distributed but spans many orders of magnitudes. The log=True argument passed to plt.hist specifies that the y-axis should be scaled logarithmically. This is useful when the data spans several orders of magnitude, as it allows for better visualization of the distribution. The resulting histogram will show the frequency of values in the transformed data within each bin on a logarithmic scale.
One common distribution that is used to model scattering intensities is the log-normal distribution, which arises from the random nature of the particle sizes and shapes and the physical processes that lead to scattering. In this case, taking the logarithm of the scattering intensities can result in a normal distribution, which is easier to work with mathematically.
However, other distributions can also be used to model scattering intensities, including the Weibull distribution and other distributions that can account for skewness or heavy tails in the data. The Weibull distribution, in particular, is commonly used to model failure times in materials science, but it can also be used to model scattering intensities in some cases.
In summary, while the log-normal distribution is commonly used to model scattering intensities, other distributions such as the Weibull distribution may be more appropriate in certain cases. The choice of distribution should be based on the specific properties of the sample, the type of scattering, and the instrument used for the measurement, as well as the assumptions and limitations of the particular modeling approach.
# Fit the data to the model function using curve_fit
popt, pcov = curve_fit(exponential, newbcent,newloghist)
# Plot the data and the fitted curve
x = np.linspace(0, 35, 50)
ax=sns.scatterplot(x=newbcent,y=newloghist,marker='o',edgecolor='r',c='none',s=10,alpha=0.7,label='logplot hist')
ax=plt.plot(x, exponential(x, *popt), color='k',linestyle='--',label='curve fit: %.2f*exp(-%.2fx)+(%.2f)'%tuple(popt))
plt.legend()
print(popt)
import plotly.express as px
Recall df1 is the first 1000 samples from the cfs2 dataset
from scipy.spatial import distance_matrix
def get_nearest_nieghbor_ordering(coords,initx):
# Calculate the distance matrix
dist_matrix = distance_matrix(coords, coords)
# Find the nearest neighbor for each point
order = [initx]
while len(order) < len(coords):
last_point = coords[order[-1]]
nearest_neighbors = np.argsort(dist_matrix[order[-1]])[1:]
for neighbor in nearest_neighbors:
if neighbor not in order:
order.append(neighbor)
break
return order
# Print the ordered coordinates
# plt.plot(coords[:,0],coords[:,1],label='unordered',alpha=0.5)
# plt.plot(coords[order][:,0],coords[order][:,1],label='ordered')
df1=pd.read_csv('output_correlations_1000.csv')
df1.columns=['image_idx','00','01','10','11']
df1.sort_values(['01','10','11'])
df1['L2']=df1[['01','10','11']].apply(lambda x: np.linalg.norm(x),axis=1)
df1_L2sorted=df1.sort_values('L2').reset_index(drop=True)
coords=df1[['01','10']].head(100).values
sum(coords[0])
coords=df1_L2sorted[['01','10','11']].values
order=get_nearest_nieghbor_ordering(coords,0)
df1_nn_ord=df1_L2sorted.loc[order]
df1_L2sorted.L2.plot.line()
<AxesSubplot: >
df1_nn_ord.head()
image_idx | 00 | 01 | 10 | 11 | L2 | |
---|---|---|---|---|---|---|
0 | 758 | 0.999793 | -0.109007 | 0.007793 | 0.044593 | 0.118033 |
1 | 50 | 0.999999 | -0.035201 | 0.100799 | 0.084799 | 0.136347 |
4 | 42 | 0.999977 | 0.177577 | 0.091177 | 0.063977 | 0.209618 |
27 | 524 | 0.999260 | 0.231260 | 0.272860 | 0.008860 | 0.357788 |
49 | 730 | 0.999990 | 0.268790 | 0.366390 | -0.017610 | 0.454752 |
# Define a function to update the plot
def init_anim():
# fig, axes = plt.subplots(1, 3, figsize=(8,3))
idx=0
k=df1_nn_ord.image_idx.iloc[idx]
myL2=df1_nn_ord.L2.iloc[idx]
# Update the data for the plot
# plt.title(get_metrics_compare_regen_images(k))
occ2d=get_2D_occ_map_from_seq('./image_inputs_seq/ising2D_seq_%s.dat'%str(k).zfill(6))
axes[0].imshow(np.transpose(occ2d),interpolation='nearest',cmap='gray')
axes[0].axis("off")
imdat = read_bin('./image_inputs_bin/hk0_%s.bin'%str(k).zfill(6), npixels=64, offset=1280)
axes[1].imshow(np.flip(imdat,0),cmap='gray')
axes[1].axis("off")
corrf=df1_nn_ord.iloc[idx][1:5].values
# sns.heatmap(corrf.reshape((2,-1)), ax=axes[2], cmap='bwr', annot=True,square=True,cbar_kws={'shrink':0.75})
sns.heatmap(corrf.reshape((2,-1)), ax=axes[2], cmap='bwr',square=True, annot=False ,cbar_kws={'shrink':0.75},vmax=1.0,vmin=-1.0 ) #cbar=0 )
# for i in range(2):
# for j in range(2):
# text = axes[2].text(j + 0.5, i + 0.5, "{:.2f}".format(corrf[2*i+j]), ha="center", va="center", color="w")
# axes[2].set_xlim([0,2])
# axes[2].set_ylim([0,2])
# axes[2].set_aspect('equal')
axes[2].axis('off')
fig.suptitle("CFS2 vector L2 distance from origin %.3f " %(myL2), fontsize=10, y=0.95)
# text.remove()
# Define a function to update the plot
def plot_image_from_idx(idx):
# fig, axes = plt.subplots(1, 3, figsize=(8,3))
k=df1_nn_ord.image_idx.iloc[idx]
myL2=df1_nn_ord.L2.iloc[idx]
# Update the data for the plot
# plt.title(get_metrics_compare_regen_images(k))
occ2d=get_2D_occ_map_from_seq('./image_inputs_seq/ising2D_seq_%s.dat'%str(k).zfill(6))
axes[0].imshow(np.transpose(occ2d),interpolation='nearest',cmap='gray')
axes[0].axis("off")
imdat = read_bin('./image_inputs_bin/hk0_%s.bin'%str(k).zfill(6), npixels=64, offset=1280)
axes[1].imshow(np.flip(imdat,0),cmap='gray')
axes[1].axis("off")
corrf=df1_nn_ord.iloc[idx][1:5].values
# sns.heatmap(corrf.reshape((2,-1)), ax=axes[2], cmap='bwr', annot=True,square=True,cbar_kws={'shrink':0.75})
sns.heatmap(corrf.reshape((2,-1)), ax=axes[2], cmap='bwr',square=True, cbar=0 ,vmax=1.0,vmin=-1.0 ) #cbar=0 )
axes[2].axis('off')
tscale=0.1
text1.set_text("{:.2f}".format(corrf[2*0+0]))
text2.set_text("{:.2f}".format(corrf[2*1+0]))
text3.set_text("{:.2f}".format(corrf[2*0+1]))
text4.set_text("{:.2f}".format(corrf[2*1+1]))
# axes[2].set_xlim([0,2])
# axes[2].set_ylim([0,2])
# axes[2].set_aspect('equal')
fig.suptitle("CFS2 vector L2 distance from origin %.3f " %(myL2), fontsize=10, y=0.95)
# text.remove()
df1_nn_ord
Unnamed: 0 | 00 | 01 | 10 | 11 | L2 | |
---|---|---|---|---|---|---|
0 | 758 | 0.999793 | -0.109007 | 0.007793 | 0.044593 | 0.118033 |
1 | 50 | 0.999999 | -0.035201 | 0.100799 | 0.084799 | 0.136347 |
4 | 42 | 0.999977 | 0.177577 | 0.091177 | 0.063977 | 0.209618 |
27 | 524 | 0.999260 | 0.231260 | 0.272860 | 0.008860 | 0.357788 |
49 | 730 | 0.999990 | 0.268790 | 0.366390 | -0.017610 | 0.454752 |
... | ... | ... | ... | ... | ... | ... |
928 | 288 | 0.999969 | 0.862369 | 0.577569 | 0.463969 | 1.136896 |
896 | 341 | 0.998817 | 0.850017 | 0.539617 | 0.411617 | 1.087724 |
805 | 444 | 0.999999 | -0.584001 | -0.305601 | 0.707199 | 0.966737 |
942 | 954 | 0.999303 | -0.488697 | -0.568697 | 0.893703 | 1.166596 |
900 | 638 | 0.998400 | -0.443200 | -0.726400 | 0.689600 | 1.095277 |
1000 rows × 6 columns
# Create an animation object
fig, axes = plt.subplots(1, 3, figsize=(8,3))
mylabels=[]
text1 = axes[2].text(0 + 0.5, 0 + 0.5, '', ha="center", va="center", color="k")
text2 = axes[2].text(0 + 0.5, 1 + 0.5, '', ha="center", va="center", color="k")
text3 = axes[2].text(1 + 0.5, 0 + 0.5, '', ha="center", va="center", color="k")
text4 = axes[2].text(1 + 0.5, 1 + 0.5, '', ha="center", va="center", color="k")
ani = FuncAnimation(fig, plot_image_from_idx, init_func=init_anim(), frames=200, interval=200)
# Display the animation in the notebook
# HTML(ani.to_jshtml())
# HTML(ani.to_html5_video())
# this will only work if you have ffmpeg
# just use animated gif for now
HTML(ani.to_jshtml())
from matplotlib import animation
f = './CFS2_vector_L2_anime.gif'
writergif = animation.PillowWriter(fps=3)
ani.save(f, writer=writergif)
from aux_functions import get_CFS_data_anim
df_corfu_cfs3=pd.read_csv('output_correlations_cfs3_5000.csv')
list(['image_idx'])+list(df_corfu_cfs3.columns.values[1:])
['image_idx', '00', '01', '02', '10', '11', '12', '20', '21', '22']
def get_NN_ordering(df):
coord_names=list(df.columns.values[1:])
df.columns=list(['image_idx'])+list(df.columns.values[1:])
df.sort_values(coord_names)
df['L2']=df[coord_names].apply(lambda x: np.linalg.norm(x),axis=1)
df_L2sorted=df.sort_values('L2').reset_index(drop=True)
coords=df_L2sorted[ coord_names ].values
order=get_nearest_nieghbor_ordering(coords,0)
df_nn_ord=df_L2sorted.loc[order]
return df_nn_ord
df_corfu_cfs3=pd.read_csv('output_correlations_cfs3_5000.csv')
df_nn_ord_cfs3=get_NN_ordering(df_corfu_cfs3)
ani2=get_CFS_data_anim(df_nn_ord_cfs3.head(200),seq_dir='./cfs3_image_inputs_seq',bin_dir='cfs3_image_inputs_bin',cfs=3,nframes=200)
HTML(ani2.to_jshtml())
f = './CFS3_vector_L2_anime.gif'
writergif = animation.PillowWriter(fps=3)
ani2.save(f, writer=writergif)
df_corfu_cfs4=pd.read_csv('output_correlations_cfs4_5000.csv')
df_nn_ord_cfs4=get_NN_ordering(df_corfu_cfs4)
ani3=get_CFS_data_anim(df_nn_ord_cfs4.head(200),seq_dir='./cfs4_image_inputs_seq',bin_dir='cfs4_image_inputs_bin',cfs=4,nframes=200)
f = './CFS4_vector_L2_anime.gif'
writergif = animation.PillowWriter(fps=3)
ani3.save(f, writer=writergif)
import h5py
# test Open the HDF5 file in read-only mode
with h5py.File('cfs4_big_bin_2.h5', 'r') as f:
# Get a list of dataset names in the HDF5 file
dataset_names = list(f.keys())
# Print the names of all datasets
for name in dataset_names:
print(name)
dset=f[dataset_names[0]]
X0=dset[:]
cfs4_big_bin_2
np.shape(X0)
(5, 64, 64)
import plotly.express as px
fig = px.imshow(X0, binary_string=True, animation_frame=0, zmax=4)
fig.show()