Skip to content

Commit

Permalink
calculate the profile uncertainty in a thread
Browse files Browse the repository at this point in the history
  • Loading branch information
bmaranville committed Nov 8, 2024
1 parent 327d386 commit 319e737
Showing 1 changed file with 32 additions and 5 deletions.
37 changes: 32 additions & 5 deletions refl1d/webview/server/api.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
import asyncio
from copy import deepcopy
from functools import lru_cache
from typing import Union, Dict, List, TypedDict
from pathlib import Path
import numpy as np
from refl1d.errors import calc_errors
from refl1d.experiment import Experiment, ExperimentBase, MixedExperiment
import refl1d.probe
from bumps.webview.server.api import (
Expand All @@ -16,7 +20,7 @@
logger,
)
import bumps.webview.server.api as bumps_api
from bumps.errplot import calc_errors_from_state
from bumps.errplot import error_points_from_state

# from refl1d.errors import show_errors
from .profile_uncertainty import show_errors
Expand Down Expand Up @@ -126,8 +130,8 @@ async def get_model_names():
return output


@register
async def get_profile_uncertainty_plot(
@lru_cache(maxsize=30)
def _get_profile_uncertainty_plot(
auto_align: bool = True,
align: float = 0.0,
nshown: int = 5000,
Expand All @@ -137,15 +141,17 @@ async def get_profile_uncertainty_plot(
):
if state.problem is None or state.problem.fitProblem is None:
return None
fitProblem = state.problem.fitProblem
fitProblem = deepcopy(state.problem.fitProblem)
uncertainty_state = state.fitting.uncertainty_state
align_arg = "auto" if auto_align else align
if uncertainty_state is not None:
import time

start_time = time.time()
logger.info(f"queueing new profile uncertainty plot... {start_time}")
errs = calc_errors_from_state(fitProblem, uncertainty_state, nshown=nshown, random=random, portion=1.0)
error_points = error_points_from_state(uncertainty_state, nshown=nshown, random=random, portion=1.0)
logger.info(f"points calculated: {time.time() - start_time}")
errs = calc_errors(fitProblem, error_points)
logger.info(f"errors calculated: {time.time() - start_time}")
error_result = show_errors(errs, npoints=npoints, align=align_arg, residuals=residuals)
error_result["fig"] = error_result["fig"].to_dict()
Expand All @@ -159,6 +165,27 @@ async def get_profile_uncertainty_plot(
return None


@register
async def get_profile_uncertainty_plot(
auto_align: bool = True,
align: float = 0.0,
nshown: int = 5000,
npoints: int = 5000,
random: bool = True,
residuals: bool = False,
):
result = await asyncio.to_thread(
_get_profile_uncertainty_plot,
auto_align=auto_align,
align=align,
nshown=nshown,
npoints=npoints,
random=random,
residuals=residuals,
)
return result


@register
async def load_probe_from_file(pathlist: List[str], filename: str, model_index: int = 0, fwhm: bool = True):
path = Path(*pathlist)
Expand Down

0 comments on commit 319e737

Please sign in to comment.