Spaces:
Runtime error
Runtime error
Commit
·
d932430
1
Parent(s):
41a501f
Delete uilit.py
Browse files
uilit.py
DELETED
|
@@ -1,135 +0,0 @@
|
|
| 1 |
-
from init import *
|
| 2 |
-
import numpy as np
|
| 3 |
-
import pandas as pd
|
| 4 |
-
import matplotlib.pyplot as plt
|
| 5 |
-
import os
|
| 6 |
-
from PIL import Image
|
| 7 |
-
|
| 8 |
-
import matplotlib.animation as animation
|
| 9 |
-
from matplotlib.animation import FuncAnimation
|
| 10 |
-
from IPython.display import clear_output
|
| 11 |
-
from autograd.core.engine import Value
|
| 12 |
-
from sklearn.datasets import make_moons, make_blobs
|
| 13 |
-
import plotly.graph_objects as go
|
| 14 |
-
import plotly.io as pio
|
| 15 |
-
import imageio
|
| 16 |
-
|
| 17 |
-
clear_output()
|
| 18 |
-
|
| 19 |
-
# Iterate through the first 6 images
|
| 20 |
-
|
| 21 |
-
def extract_path_df(path_dir, index_show):
|
| 22 |
-
path_file = []
|
| 23 |
-
|
| 24 |
-
for filesname in os.listdir(path_dir):
|
| 25 |
-
path_file.append(os.path.join(path_dir,filesname))
|
| 26 |
-
|
| 27 |
-
for data_df in range(0,len(path_file)):
|
| 28 |
-
data_frame = pd.read_csv(path_file[data_df])
|
| 29 |
-
show_df = data_frame.head(index_show)
|
| 30 |
-
return path_file , f"dataframe: {show_df}"
|
| 31 |
-
|
| 32 |
-
def loading_df_to_numpy(path_file):
|
| 33 |
-
data_df = pd.read_csv(path_file)
|
| 34 |
-
data = np.array(data_df)
|
| 35 |
-
m, n = data.shape
|
| 36 |
-
data_train = data[1000:m].T
|
| 37 |
-
Y_train = data_train[0][40900:]
|
| 38 |
-
X_train = data_train[1:n][:, 40900:]
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
# Manually split the data into training and testing sets
|
| 42 |
-
split_index = int(X_train.shape[0] * 0.8) # 80% for training, 20% for testing
|
| 43 |
-
X_train_split = X_train[:, :10]
|
| 44 |
-
Y_train_split = Y_train[:10]
|
| 45 |
-
X_test_split = X_train[:,:5]
|
| 46 |
-
Y_test_split = Y_train[:5]
|
| 47 |
-
|
| 48 |
-
return X_train_split, X_test_split, Y_train_split, Y_test_split
|
| 49 |
-
|
| 50 |
-
def initialize_data(n_samples: int, noise: float):
|
| 51 |
-
input_data, Target = make_moons(n_samples=n_samples, noise=noise)
|
| 52 |
-
|
| 53 |
-
Target = Target * 2 - 1 # make y be -1 or 14
|
| 54 |
-
# fig.close()
|
| 55 |
-
return input_data, Target
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
def plot_sample(DATA_TRAIN,DATA_LABEL):
|
| 59 |
-
num_images = min(6, DATA_TRAIN.shape[1])
|
| 60 |
-
fig, axs = plt.subplots(2, 3, figsize=(10, 7))
|
| 61 |
-
|
| 62 |
-
for i in range(num_images):
|
| 63 |
-
label = DATA_LABEL
|
| 64 |
-
image = DATA_TRAIN[:, i]
|
| 65 |
-
current_image = image.reshape(28, 28) * 255
|
| 66 |
-
|
| 67 |
-
# Determine the subplot coordinates
|
| 68 |
-
row = i // 3
|
| 69 |
-
col = i % 3
|
| 70 |
-
|
| 71 |
-
# Plot the image in the corresponding subplot
|
| 72 |
-
axs[row, col].imshow(current_image, cmap='gray')
|
| 73 |
-
axs[row, col].set_title("Label: {}".format(label))
|
| 74 |
-
axs[row, col].axis('off')
|
| 75 |
-
|
| 76 |
-
plt.tight_layout()
|
| 77 |
-
plt.savefig("sample.png")
|
| 78 |
-
|
| 79 |
-
def copy(model):
|
| 80 |
-
model = SparseMLP(nin=2, nouts=[16, 16, 1], sparsities=[0.,0.9,0.8])
|
| 81 |
-
model.parameters()
|
| 82 |
-
return model
|
| 83 |
-
|
| 84 |
-
def Zvals(model , X_train):
|
| 85 |
-
global X
|
| 86 |
-
h = 0.25
|
| 87 |
-
x_min, x_max = X_train[:, 0].min() - 1, X_train[:, 0].max() + 1
|
| 88 |
-
y_min, y_max = X_train[:, 1].min() - 1, X_train[:, 1].max() + 1
|
| 89 |
-
xx, yy = np.meshgrid(np.arange(x_min, x_max, h),
|
| 90 |
-
np.arange(y_min, y_max, h))
|
| 91 |
-
Xmesh = np.c_[xx.ravel(), yy.ravel()]
|
| 92 |
-
inputs = [list(map(Value, xrow)) for xrow in Xmesh]
|
| 93 |
-
scores = list(map(model, inputs))
|
| 94 |
-
Z = np.array([s.data > 0 for s in scores])
|
| 95 |
-
Z = Z.reshape(xx.shape)
|
| 96 |
-
return Z
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
def dboundary(model):
|
| 100 |
-
global X
|
| 101 |
-
global Y
|
| 102 |
-
h = 0.25
|
| 103 |
-
x_min, x_max = X[:, 0].min() - 1, X[:, 0].max() + 1
|
| 104 |
-
y_min, y_max = X[:, 1].min() - 1, X[:, 1].max() + 1
|
| 105 |
-
xx, yy = np.meshgrid(np.arange(x_min, x_max, h),
|
| 106 |
-
np.arange(y_min, y_max, h))
|
| 107 |
-
Xmesh = np.c_[xx.ravel(), yy.ravel()]
|
| 108 |
-
fig, ax = plt.subplots(figsize=(8,8))
|
| 109 |
-
Z = Zvals(model)
|
| 110 |
-
ln = ax.contourf(xx, yy, Z, cmap=plt.cm.Spectral, alpha=0.8)
|
| 111 |
-
ax.scatter(X[:, 0], X[:, 1], c=Y, s=40, cmap=plt.cm.Spectral)
|
| 112 |
-
ax.set_xlim(xx.min(), xx.max())
|
| 113 |
-
ax.set_ylim(yy.min(), yy.max())
|
| 114 |
-
return fig,ax,ln
|
| 115 |
-
|
| 116 |
-
def graph_trace(Path, nframes, interval):
|
| 117 |
-
animation_frames = []
|
| 118 |
-
# Load the first image to get its dimensions and color mode
|
| 119 |
-
first_frame_path = f"assets/{Path}_0.png"
|
| 120 |
-
first_image = Image.open(first_frame_path)
|
| 121 |
-
width, height = first_image.size
|
| 122 |
-
color_mode = first_image.mode
|
| 123 |
-
|
| 124 |
-
# Resize and convert all the images to the same dimensions and color mode
|
| 125 |
-
resized_width = 900 # Set your desired width
|
| 126 |
-
resized_height = 700 # Set your desired height
|
| 127 |
-
|
| 128 |
-
for i in range(nframes):
|
| 129 |
-
frame_path = f"assets/{Path}_{i}.png"
|
| 130 |
-
image = Image.open(frame_path)
|
| 131 |
-
resized_image = image.resize((resized_width, resized_height)).convert(color_mode)
|
| 132 |
-
animation_frames.append(resized_image)
|
| 133 |
-
|
| 134 |
-
animation_path = "out/Graph.mp4"
|
| 135 |
-
imageio.mimsave(animation_path, animation_frames, format="mp4")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|