-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbasic_clusters.py
More file actions
45 lines (37 loc) · 1.51 KB
/
basic_clusters.py
File metadata and controls
45 lines (37 loc) · 1.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
"""
A script to cluster and visualise tactile images:
1. Reads tactile images from a data folder in grayscale
2. Flattens the images into 1D arrays for clustering
3. Uses PCA, t-SNE and UMAP to reduce the dimensionality of the images
4. Visualises the results with labels and colors
Author: Gemma McLean
Date: August 2025
"""
import cluster_functions
import data_functions
import numpy as np
# Read images and labels from the data folder
print("Processing tactile images...")
images, labels = data_functions.read_data_directory("data", grayscale=True)
# Flatten the images for PCA/t-SNE
images_flat = np.array([img.flatten() for img in images])
# Set random state for reproducibility
random_state = 42
# PCA
print("Performing PCA...")
features_2d = cluster_functions.pca_reduce(images_flat, random_state)
# Plot the results
cluster_functions.plot_labels_2d("PCA", "plots/basic", features_2d, labels)
cluster_functions.plot_objects_2d("PCA", "plots/basic", features_2d, labels)
# t-SNE
print("Performing t-SNE...")
features_2d = cluster_functions.perform_tsne(images_flat, random_state)
# Plot the results
cluster_functions.plot_labels_2d("t-SNE", "plots/basic", features_2d, labels)
cluster_functions.plot_objects_2d("t-SNE", "plots/basic", features_2d, labels)
# UMAP
print("Performing UMAP...")
features_2d = cluster_functions.perform_umap(images_flat, random_state)
# Plot the results
cluster_functions.plot_labels_2d("UMAP", "plots/basic", features_2d, labels)
cluster_functions.plot_objects_2d("UMAP", "plots/basic", features_2d, labels)