Skip to content

Commit

Permalink
UMAP can read user uploaded zip file
Browse files Browse the repository at this point in the history
  • Loading branch information
runboj committed Nov 10, 2023
1 parent 5d62734 commit 9dc84bd
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 2 deletions.
4 changes: 3 additions & 1 deletion umap_run.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import json
import pandas as pd

from utils import UMAPParameters
from utils import UMAPParameters, load_images_from_directory

""" Compute UMAP
Input: 1d data (N, M) or 2d data (N, H, W)
Expand Down Expand Up @@ -47,6 +47,8 @@ def computeUMAP(data,
if images_dir == "data/example_latentrepresentation/f_vectors.parquet":
df = pd.read_parquet(images_dir)
images = df.values
else: # user uploaded zip file
images = load_images_from_directory(images_dir)
print(images.shape)

# Load dimension reduction parameter
Expand Down
18 changes: 17 additions & 1 deletion utils.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
from pydantic import BaseModel, Field
import os
from PIL import Image
import numpy as np

class PCAParameters(BaseModel):
n_components: int = Field(description='number of components to keep')
Expand All @@ -8,4 +11,17 @@ class UMAPParameters(BaseModel):
min_dist: float = Field(description='min distance between points')
n_neighbors: int = Field(description='number of nearest neighbors')


def load_images_from_directory(directory_path):
image_data = []
for filename in os.listdir(directory_path):
if filename.endswith(".png"):
file_path = os.path.join(directory_path, filename)
try:
img = Image.open(file_path)
img_array = np.array(img)
image_data.append(img_array)
except Exception as e:
print(f"Error processing {file_path}: {e}")

image_data = np.array(image_data)
return image_data

0 comments on commit 9dc84bd

Please sign in to comment.