From fdd977217315dcd816dc0978e5ff21f6395f1d12 Mon Sep 17 00:00:00 2001 From: unknown Date: Thu, 17 Oct 2024 18:19:47 +0300 Subject: [PATCH] mnist stuff --- .gitignore | 1 + gen_stimulat.py | 40 ++++++++++++++++++++++++++++++++++++++++ list_all_nines.py | 15 +++++++++++++++ 3 files changed, 56 insertions(+) create mode 100644 .gitignore create mode 100644 gen_stimulat.py create mode 100644 list_all_nines.py diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..f7275bb --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +venv/ diff --git a/gen_stimulat.py b/gen_stimulat.py new file mode 100644 index 0000000..e68cca0 --- /dev/null +++ b/gen_stimulat.py @@ -0,0 +1,40 @@ +import numpy as np +from tensorflow.keras.datasets import mnist +import matplotlib.pyplot as plt + +# Load the MNIST dataset +(x_train, y_train), (x_test, y_test) = mnist.load_data() + +# Find the first '9' in the dataset +index_of_nine = np.where(y_train == 9)[0][0] + +# Get the 28x28 pixel image of the number 9 +image_of_nine = x_train[index_of_nine] + +# Normalize the pixel values to [0, 1] range +normalized_image = image_of_nine / 255.0 + +# Function to convert a pixel value to an electrical stimulation level +def pixel_to_stimulation(pixel_value, max_stimulation=5.0): + """ + Converts a normalized pixel value (0.0 - 1.0) to an electrical stimulation (e.g., voltage). + Assumes max_stimulation is the maximum output voltage/current, e.g., 5V. + """ + return pixel_value * max_stimulation + +# Apply the conversion to the entire image +stimulation_pattern = np.vectorize(pixel_to_stimulation)(normalized_image) + +# Now `stimulation_pattern` is a 28x28 array of electrical stimulation levels (e.g., voltages) +print("Electrical stimulation pattern for '9':") +print(stimulation_pattern) + + +# Create a plot to visualize the stimulation pattern +plt.figure(figsize=(5, 5)) +plt.imshow(stimulation_pattern, cmap='gray', interpolation='nearest') +plt.title("Electrical Stimulation Pattern for '9'") +plt.axis('off') # Hide the axis for a cleaner look +plt.show() + +# Optionally, you could output this to an external device or system that applies electrical stimulation. diff --git a/list_all_nines.py b/list_all_nines.py new file mode 100644 index 0000000..8704626 --- /dev/null +++ b/list_all_nines.py @@ -0,0 +1,15 @@ +# Load the MNIST dataset +from tensorflow.keras.datasets import mnist +import numpy as np + + +(x_train, y_train), (x_test, y_test) = mnist.load_data() + +# Find all indices of the digit '9' in the training data +indices_of_nines = np.where(y_train == 9)[0] + +# Count how many '9's are there in the training set +num_nines = len(indices_of_nines) + +# Display the count +num_nines