Skip to content

Commit

Permalink
enable transitions for scatter and cluster hulls. setup state managem…
Browse files Browse the repository at this point in the history
…ent cleaned up a bit. cluster label management improved in setup.
  • Loading branch information
enjalot committed Feb 23, 2024
1 parent 7ea2ab9 commit c34d1fa
Show file tree
Hide file tree
Showing 10 changed files with 853 additions and 338 deletions.
50 changes: 19 additions & 31 deletions latentscope/server/datasets.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import os
import re
import json
import fnmatch
import pandas as pd
from flask import Blueprint, jsonify, request

Expand Down Expand Up @@ -31,14 +32,14 @@ def get_datasets():
"""
Get all metadata files from the given a directory.
"""
def scan_for_json_files(directory_path):
def scan_for_json_files(directory_path, match_pattern=r".*\.json$"):
try:
files = os.listdir(directory_path)
except OSError as err:
print('Unable to scan directory:', err)
return jsonify({"error": "Unable to scan directory"}), 500

json_files = [file for file in files if file.endswith('.json')]
json_files = [file for file in files if re.match(match_pattern, file)]
json_files.sort()
print("files", files)
print("json", json_files)
Expand Down Expand Up @@ -72,28 +73,15 @@ def update_dataset_meta(dataset):
json.dump(json_contents, json_file)
return jsonify(json_contents)


@datasets_bp.route('/<dataset>/embeddings', methods=['GET'])
def get_dataset_embeddings(dataset):
directory_path = os.path.join(DATA_DIR, dataset, "embeddings")
# directory_path = os.path.join(DATA_DIR, dataset, "umaps")
print("dataset", dataset, directory_path)
return scan_for_json_files(directory_path)
# print("dataset", dataset, directory_path)
# try:
# files = sorted(os.listdir(directory_path), key=lambda x: os.path.getmtime(os.path.join(directory_path, x)), reverse=True)
# except OSError as err:
# print('Unable to scan directory:', err)
# return jsonify({"error": "Unable to scan directory"}), 500

# npy_files = [file.replace(".npy", "") for file in files if file.endswith('.npy')]
# return jsonify(npy_files)


@datasets_bp.route('/<dataset>/umaps', methods=['GET'])
def get_dataset_umaps(dataset):
directory_path = os.path.join(DATA_DIR, dataset, "umaps")
print("dataset", dataset, directory_path)
return scan_for_json_files(directory_path)

@datasets_bp.route('/<dataset>/umaps/<umap>', methods=['GET'])
Expand All @@ -112,8 +100,7 @@ def get_dataset_umap_points(dataset, umap):
@datasets_bp.route('/<dataset>/clusters', methods=['GET'])
def get_dataset_clusters(dataset):
directory_path = os.path.join(DATA_DIR, dataset, "clusters")
print("dataset", dataset, directory_path)
return scan_for_json_files(directory_path)
return scan_for_json_files(directory_path, match_pattern=r"cluster-\d+\.json")

@datasets_bp.route('/<dataset>/clusters/<cluster>', methods=['GET'])
def get_dataset_cluster(dataset, cluster):
Expand All @@ -136,25 +123,25 @@ def get_dataset_cluster_indices(dataset, cluster):
df = pd.read_parquet(file_path)
return df.to_json(orient="records")

@datasets_bp.route('/<dataset>/clusters/<cluster>/labels/<model>', methods=['GET'])
def get_dataset_cluster_labels(dataset, cluster, model):
@datasets_bp.route('/<dataset>/clusters/<cluster>/labels/<id>', methods=['GET'])
def get_dataset_cluster_labels(dataset, cluster, id):
# if model == "default":
# return get_dataset_cluster_labels_default(dataset, cluster)
file_name = cluster + "-labels-" + model + ".parquet"
file_name = cluster + "-labels-" + id + ".parquet"
file_path = os.path.join(DATA_DIR, dataset, "clusters", file_name)
df = pd.read_parquet(file_path)
df.reset_index(inplace=True)
return df.to_json(orient="records")

@datasets_write_bp.route('/<dataset>/clusters/<cluster>/labels/<model>/label/<index>', methods=['GET'])
def overwrite_dataset_cluster_label(dataset, cluster, model, index):
@datasets_write_bp.route('/<dataset>/clusters/<cluster>/labels/<id>/label/<index>', methods=['GET'])
def overwrite_dataset_cluster_label(dataset, cluster, id, index):
index = int(index)
new_label = request.args.get('label')
print("write label", index, new_label)
if new_label is None:
return jsonify({"error": "Missing 'label' in request data"}), 400

file_name = cluster + "-labels-" + model + ".parquet"
file_name = cluster + "-labels-" + id + ".parquet"
file_path = os.path.join(DATA_DIR, dataset, "clusters", file_name)
try:
df = pd.read_parquet(file_path)
Expand All @@ -173,15 +160,16 @@ def overwrite_dataset_cluster_label(dataset, cluster, model, index):
@datasets_bp.route('/<dataset>/clusters/<cluster>/labels_available', methods=['GET'])
def get_dataset_cluster_labels_available(dataset, cluster):
directory_path = os.path.join(DATA_DIR, dataset, "clusters")
try:
files = sorted(os.listdir(directory_path), key=lambda x: os.path.getmtime(os.path.join(directory_path, x)), reverse=True)
except OSError as err:
print('Unable to scan directory:', err)
return jsonify({"error": "Unable to scan directory"}), 500
return scan_for_json_files(directory_path, match_pattern=rf"{cluster}-labels-.*\.json")
# try:
# files = sorted(os.listdir(directory_path), key=lambda x: os.path.getmtime(os.path.join(directory_path, x)), reverse=True)
# except OSError as err:
# print('Unable to scan directory:', err)
# return jsonify({"error": "Unable to scan directory"}), 500

pattern = re.compile(r'^' + cluster + '-labels-(.*).parquet$')
model_names = [pattern.match(file).group(1) for file in files if pattern.match(file)]
return jsonify(model_names)
# pattern = re.compile(r'^' + cluster + '-labels-(.*).parquet$')
# model_names = [pattern.match(file).group(1) for file in files if pattern.match(file)]
# return jsonify(model_names)


def get_next_scopes_number(dataset):
Expand Down
132 changes: 132 additions & 0 deletions web/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions web/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@
"dependencies": {
"d3-scale": "^4.0.2",
"d3-scale-chromatic": "^3.0.0",
"d3-selection": "^3.0.0",
"d3-shape": "^3.2.0",
"d3-transition": "^3.0.1",
"flubber": "^0.4.2",
"react": "^18.2.0",
"react-dom": "^18.2.0",
"react-router-dom": "^6.20.1",
Expand Down
Loading

0 comments on commit c34d1fa

Please sign in to comment.