Skip to content

Commit

Permalink
Fix async stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
nictru committed May 9, 2024
1 parent 945457d commit 5ef20d1
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 43 deletions.
8 changes: 6 additions & 2 deletions src/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,14 +41,15 @@
def server(input, output, session: Session):
_adata: reactive.Value[ad.AnnData] = reactive.value(None)
_adata_meta: reactive.Value[ad.AnnData] = reactive.value(None)
_adata_qc: reactive.Value[ad.AnnData] = reactive.value(None)
_adata_filtered: reactive.Value[ad.AnnData] = reactive.value(None)
_file_name = reactive.value(None)
_distributions = reactive.value({})
_metadata = reactive.value(None)
_calculate_metrics_bool = reactive.value(False)

distributions_server("distributions", _adata_meta, _pretty_names, _distributions)
slider_server("sliders", _adata, _metadata, _adata_meta, _adata_filtered, _pretty_names, _distributions, _calculate_metrics_bool)
distributions_server("distributions", _adata_qc, _pretty_names, _distributions)
slider_server("sliders", _adata_meta, _adata_qc, _adata_filtered, _pretty_names, _distributions, _calculate_metrics_bool)
plots_server("plots", _adata_filtered, _pretty_names, _distributions)
metadata_server("metadata", _adata, _metadata)

Expand All @@ -73,6 +74,9 @@ def update_adata_meta():
metadata = _metadata.get()
if adata is None or metadata is None:
return
adata_meta = adata.copy()
adata_meta.obs = metadata
_adata_meta.set(adata_meta)
_calculate_metrics_bool.set(True)

@render.download(
Expand Down
55 changes: 14 additions & 41 deletions src/sliders.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,10 @@ def slider_ui():
ui.output_ui("slider_filters")
)

pool = concurrent.futures.ThreadPoolExecutor()

@module.server
def slider_server(input, output, session,
_adata: reactive.Value[ad.AnnData],
_metadata: reactive.value[pd.DataFrame],
_adata_meta: reactive.Value[ad.AnnData],
_adata_qc: reactive.Value[ad.AnnData],
_adata_filtered: reactive.Value[ad.AnnData],
_pretty_names: reactive.Value[Dict[str, str]],
_distributions: reactive.Value[Dict[str, Dict[str, float]]],
Expand All @@ -36,7 +33,7 @@ def slider_server(input, output, session,
@output
@render.ui
def slider_sample():
adata = _adata_meta.get()
adata = _adata_qc.get()
if adata is None:
return

Expand All @@ -55,54 +52,30 @@ def button():
else:
return None

@reactive.effect
def recalc_logic(adata, metadata, calculate_metrics_bool, adata_meta):
time.sleep(1)
print('recalculate')
adata = _adata.get()
print('got')
metadata = _metadata.get()
print('got')
if adata is not None and metadata is not None:
adata_meta = adata.copy()
adata_meta.obs = metadata.copy()
calculate_qc_metrics(adata)
calculate_qc_metrics(adata_meta)
adata.set(adata)
adata_meta.set(adata_meta)

calculate_metrics_bool.set(False)
print('end')
print(adata.obs.head(1))
return adata, adata_meta, metadata, calculate_metrics_bool
else:
print('none')


@ui.bind_task_button(button_id="calculate_button")
@reactive.extended_task
async def recalculate_qc(adata, metadata, calculate_metrics_bool, adata_meta):
loop = asyncio.get_event_loop()
return await loop.run_in_executor(pool, recalc_logic, adata, metadata, calculate_metrics_bool, adata_meta)

async def recalc_logic(adata_meta):
adata_meta_copy = adata_meta.copy()
calculate_qc_metrics(adata_meta_copy)
return adata_meta_copy

@reactive.effect
@reactive.event(input.calculate_button, ignore_none=True)
def handle_click():
print('handle_click')
adata = _adata.get()
metadata = _metadata.get()
calculate_metrics_bool = _calculate_metrics_bool.get()
adata_meta = _adata_meta.get()
#recalculate_qc(adata, metadata, calculate_metrics_bool, adata_meta)
recalculate_qc(_adata, _metadata, _calculate_metrics_bool, _adata_meta)
if adata_meta is None:
return
recalc_logic(adata_meta)

@reactive.effect
def return_of_adata_meta():
return recalculate_qc.result()
result = recalc_logic.result()
_adata_qc.set(result)
_calculate_metrics_bool.set(False)

@reactive.effect
def random_sample():
adata = _adata_meta.get()
adata = _adata_qc.get()
sample_size = input['random_sample_size'].get()

if adata is None:
Expand Down

0 comments on commit 5ef20d1

Please sign in to comment.