Skip to content

Commit

Permalink
Patch pgf error
Browse files Browse the repository at this point in the history
  • Loading branch information
sarahshi committed Dec 10, 2023
1 parent ceb8734 commit 5506d46
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 41 deletions.
12 changes: 6 additions & 6 deletions src/mineralML/confusion_matrix.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

def pp_matrix(df_cm, annot=True, cmap="BuGn", fmt=".2f",
fz=12, lw=0.5, cbar=False, figsize=[10.5, 10.5],
show_null_values=0, pred_val_axis="x", savefig = None,):
show_null_values=0, pred_val_axis="x"): #, savefig = None,):

"""
Expand Down Expand Up @@ -107,8 +107,8 @@ def pp_matrix(df_cm, annot=True, cmap="BuGn", fmt=".2f",
ax.set_ylabel(ylbl)
plt.tight_layout() # set layout slim

if savefig != None:
plt.savefig(savefig + '.pdf')
# if savefig != None:
# plt.savefig(savefig + '.pdf')


def insert_totals(df_cm):
Expand Down Expand Up @@ -176,7 +176,7 @@ def config_cell_text_and_colors(array_df, lin, col, oText, facecolors, posi, fz,
"""

import matplotlib.font_manager as fm
# import matplotlib.font_manager as fm

text_add = []
text_del = []
Expand Down Expand Up @@ -213,8 +213,8 @@ def config_cell_text_and_colors(array_df, lin, col, oText, facecolors, posi, fz,
warnings.filterwarnings("ignore", category=DeprecationWarning)

# text to ADD
font_prop = fm.FontProperties(weight="bold", size=fz)
text_kwargs = dict(color="k", ha="center",va="center", gid="sum", fontproperties=font_prop,)
# font_prop = fm.FontProperties(weight="bold", size=fz)
text_kwargs = dict(color="k", ha="center",va="center", gid="sum") #, fontproperties=font_prop,)
lis_txt = ["%d" % (cell_val), per_ok_s, "%.1f%%" % (per_err)]
lis_kwa = [text_kwargs]
dic = text_kwargs.copy()
Expand Down
2 changes: 1 addition & 1 deletion src/mineralML/supervised.py
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,7 @@ class MultiClassClassifier(nn.Module):
"""

def __init__(self, input_dim=10, classes=12, dropout_rate=0.1, hidden_layer_sizes=[8]):
def __init__(self, input_dim=10, classes=12, dropout_rate=0.1, hidden_layer_sizes=[64, 32, 16]):
super(MultiClassClassifier, self).__init__()
self.input_dim = input_dim
self.classes = classes
Expand Down
70 changes: 36 additions & 34 deletions src/mineralML/unsupervised.py
Original file line number Diff line number Diff line change
Expand Up @@ -441,36 +441,36 @@ def autoencode(df, name, AE_Model, hidden_layer_sizes, epochs):
train_loss, valid_loss = train(model, optimizer, feature_loader, valid_loader, epochs, criterion)
np.savez('parametermatrix_autoencoder/' + name + '_tanh_loss.npz', train_loss = train_loss, valid_loss = valid_loss)

fig, ax = plt.subplots(1, 2, figsize = (16, 8))
ax = ax.flatten()
ax[0].plot(np.linspace(1, len(train_loss), len(train_loss)), train_loss, '.-', label = 'Train Loss')
ax[0].plot(np.linspace(1, len(train_loss), len(train_loss)), valid_loss, '.-', label = 'Test Loss')
ax[0].set_xlabel("Epochs")
ax[0].set_ylabel("Loss")
ax[0].legend(prop={'size': 10})

#transform entire dataset to latent space
z = getLatent(model, array_norm)

phase = np.array(['Amphibole', 'Apatite', 'Biotite', 'Clinopyroxene',
'Garnet', 'Ilmenite', 'KFeldspar', 'Magnetite', 'Muscovite', 'Olivine',
'Orthopyroxene', 'Plagioclase', 'Quartz', 'Rutile', 'Spinel', 'Tourmaline',
'Zircon'])
phasez = range(1,len(phase))
tab = plt.get_cmap('tab20')
cNorm = mcolors.Normalize(vmin=0, vmax=len(phase))
scalarMap = mcm.ScalarMappable(norm=cNorm, cmap=tab)

# plot latent representation
for i in range(len(phase)):
indx = df['Mineral'] == phase[i]
ax[1].scatter(z[indx, 0], z[indx, 1], s=15, color=scalarMap.to_rgba(i), lw=1, label=phase[i], rasterized = True)
ax[1].set_xlabel("Latent Variable 1")
ax[1].set_ylabel("Latent Variable 2")
ax[1].set_title(name + " Latent Space Representation")
ax[1].legend(prop={'size': 8})
plt.tight_layout()
plt.savefig('parametermatrix_autoencoder/' + name + '_loss_latentspace.pdf',)
# fig, ax = plt.subplots(1, 2, figsize = (16, 8))
# ax = ax.flatten()
# ax[0].plot(np.linspace(1, len(train_loss), len(train_loss)), train_loss, '.-', label = 'Train Loss')
# ax[0].plot(np.linspace(1, len(train_loss), len(train_loss)), valid_loss, '.-', label = 'Test Loss')
# ax[0].set_xlabel("Epochs")
# ax[0].set_ylabel("Loss")
# ax[0].legend(prop={'size': 10})

# #transform entire dataset to latent space
# z = getLatent(model, array_norm)

# phase = np.array(['Amphibole', 'Apatite', 'Biotite', 'Clinopyroxene',
# 'Garnet', 'Ilmenite', 'KFeldspar', 'Magnetite', 'Muscovite', 'Olivine',
# 'Orthopyroxene', 'Plagioclase', 'Quartz', 'Rutile', 'Spinel', 'Tourmaline',
# 'Zircon'])
# phasez = range(1,len(phase))
# tab = plt.get_cmap('tab20')
# cNorm = mcolors.Normalize(vmin=0, vmax=len(phase))
# scalarMap = mcm.ScalarMappable(norm=cNorm, cmap=tab)

# # plot latent representation
# for i in range(len(phase)):
# indx = df['Mineral'] == phase[i]
# ax[1].scatter(z[indx, 0], z[indx, 1], s=15, color=scalarMap.to_rgba(i), lw=1, label=phase[i], rasterized = True)
# ax[1].set_xlabel("Latent Variable 1")
# ax[1].set_ylabel("Latent Variable 2")
# ax[1].set_title(name + " Latent Space Representation")
# ax[1].legend(prop={'size': 8})
# plt.tight_layout()
# plt.savefig('parametermatrix_autoencoder/' + name + '_loss_latentspace.pdf',)

# save main model params
model_path = 'parametermatrix_autoencoder/' + name + "_tanh_params.pt"
Expand Down Expand Up @@ -511,7 +511,7 @@ def getLatent(model, dataset:np):
model.to(device)

dataset_ = FeatureDataset(dataset)
loader = DataLoader(dataset_,batch_size=16,shuffle=False)
loader = DataLoader(dataset_,batch_size=20,shuffle=False)

model.eval()
with torch.no_grad():
Expand Down Expand Up @@ -543,13 +543,14 @@ def get_latent_space(df):
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")

current_dir = os.path.dirname(__file__)
model_path = os.path.join(current_dir, 'ae_best_model.pt')
# model_path = os.path.join(current_dir, 'ae_best_model.pt')
model_path = os.path.join(current_dir, 'ae_best_model_noP_tanh.pt')
model = Tanh_Autoencoder(input_dim=10, hidden_layer_sizes=(256, 64, 16)).to(device)
optimizer = torch.optim.Adam(model.parameters(), lr=5e-4, weight_decay=0)
load_model(model, optimizer, model_path)

# Set the model to evaluation mode
model.eval()
# # Set the model to evaluation mode
# model.eval()

norm_wt = norm_data_ae(df)
z = getLatent(model, norm_wt)
Expand Down Expand Up @@ -757,4 +758,5 @@ def plot_latent_space(df_pred):
ax[1].tick_params(axis="x", direction='in', length=5, pad = 6.5)
ax[1].tick_params(axis="y", direction='in', length=5, pad = 6.5)
plt.tight_layout()
plt.show()

0 comments on commit 5506d46

Please sign in to comment.