Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix float32 JSON serialization #6

Merged
merged 6 commits into from
Jun 20, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .devcontainer/devcontainer.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
"ms-python.black-formatter"
]
}
},
}

// Configure tool-specific properties.
// "customizations": {},
Expand Down
28 changes: 26 additions & 2 deletions src/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
import os
import tempfile
import anndata as ad
import pandas as pd
import json
import numpy as np

from sliders import slider_ui, slider_server
from distributions import distributions_server
Expand All @@ -12,13 +13,36 @@
from helpers import calculate_qc_metrics


def convert_float32_to_float(data):
if isinstance(data, dict):
#print('dict')
return {k: convert_float32_to_float(v) for k, v in data.items()}
elif isinstance(data, list):
#print('list')
return [convert_float32_to_float(v) for v in data]
elif isinstance(data, np.float32) or isinstance(data, np.float64):
#print(f"Converting {data} to float")
return float(data)
elif isinstance(data, np.ndarray):
#print('ndarray')
return data.tolist()
return data

# Monkey-patch json.dumps
original_json_dumps = json.dumps

def custom_json_dumps(data, *args, **kwargs):
non_float_32_data = convert_float32_to_float(data)
return original_json_dumps(non_float_32_data, *args, **kwargs)

json.dumps = custom_json_dumps

_pretty_names = reactive.value({
'total_counts': 'Total counts',
'n_genes_by_counts': 'Number of genes with counts',
'pct_counts_mt': 'Percentage of counts from mitochondrial genes',
})


app_ui = ui.page_navbar(
ui.nav_panel("1. Upload", ui.input_file("file_input", label="Upload your file", accept=".h5ad")),
ui.nav_panel("2. Metadata", metadata_ui("metadata")),
Expand Down
9 changes: 5 additions & 4 deletions src/plots.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,9 @@ def plot_scatter():
fig, ax = plt.subplots()

ax.scatter(
adata.obs[x_col],
adata.obs[y_col],
c=adata.obs[color_col],
adata.obs[x_col].astype(float),
adata.obs[y_col].astype(float),
c=adata.obs[color_col].astype(float),
s=dot_size,
cmap="viridis",
)
Expand Down Expand Up @@ -133,10 +133,11 @@ def plot_histograms():

return fig

@output
@render.text
def n_cells():
adata = _adata.get()

if adata is not None:
n_cells = adata.n_obs
n_cells = int(adata.n_obs)
return f"{n_cells} cells pass the current filtering thresholds"
8 changes: 4 additions & 4 deletions src/sliders.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ def slider_sample():
if adata is None:
return

n_obs = adata.n_obs
n_obs = int(adata.n_obs)
return ui.input_slider(
"random_sample_size",
"Random sample size",
Expand Down Expand Up @@ -120,9 +120,9 @@ def slider_filters():
absolute = ui.input_slider(
f"{col}_absolute",
"Absolute value",
distributions[col]["min"],
distributions[col]["max"],
[distributions[col]["min"], distributions[col]["max"]],
float(distributions[col]["min"]),
float(distributions[col]["max"]),
[float(distributions[col]["min"]), float(distributions[col]["max"])],
)
panel = ui.accordion_panel(pretty_name, mads, absolute)
panels.append(panel)
Expand Down