Example #1
0
def single_fit(model_version_id: int, location_id: int,
               sex_id: int) -> List[_CascadeOperation]:
    """
    Create a sequence of tasks to do a single fit both model. Configures
    inputs, does a fit fixed, then fit both, then predict and uploads the result.
    Will fit the model based on the settings attached to the model version ID.

    Parameters
    ----------
    model_version_id
        The model version ID.
    location_id
        The parent location ID to run the model for.
    sex_id
        The sex ID to run the model for.

    Returns
    -------
    List of CascadeOperations.
    """
    t1 = ConfigureInputs(model_version_id=model_version_id)
    t2 = Fit(model_version_id=model_version_id,
             parent_location_id=location_id,
             sex_id=sex_id,
             fill=True,
             predict=True,
             both=True,
             save_fit=True,
             save_prior=False,
             upstream_commands=[t1.command])
    t3 = Upload(model_version_id=model_version_id,
                fit=True,
                upstream_commands=[t2.command])
    return [t1, t2, t3]
Example #2
0
def single_fit_with_uncertainty(model_version_id: int,
                                location_id: int,
                                sex_id: int,
                                n_sim: int = 100,
                                n_pool: int = 20) -> List[_CascadeOperation]:
    """
    Create a sequence of tasks to do a single fit both model. Configures
    inputs, does a fit fixed, then fit both, then predict and uploads the result.
    Will fit the model based on the settings attached to the model version ID.

    Parameters
    ----------
    model_version_id
        The model version ID.
    location_id
        The parent location ID to run the model for.
    sex_id
        The sex ID to run the model for.
    n_sim
        The number of simulations to do, number of draws to make
    n_pool
        The number of multiprocessing pools to use in creating the draws
    Returns
    -------
    List of CascadeOperations.
    """
    t1 = ConfigureInputs(model_version_id=model_version_id)
    t2 = Fit(model_version_id=model_version_id,
             parent_location_id=location_id,
             sex_id=sex_id,
             fill=True,
             predict=True,
             both=True,
             save_fit=True,
             upstream_commands=[t1.command])
    t3 = Sample(model_version_id=model_version_id,
                parent_location_id=location_id,
                sex_id=sex_id,
                n_sim=n_sim,
                n_pool=n_pool,
                fit_type='both',
                upstream_commands=[t2.command],
                executor_parameters={'num_cores': n_pool},
                asymptotic=True)
    t4 = Predict(model_version_id=model_version_id,
                 parent_location_id=location_id,
                 sex_id=sex_id,
                 save_final=True,
                 prior_grid=False,
                 sample=True,
                 upstream_commands=[t3.command])
    t5 = Upload(model_version_id=model_version_id,
                fit=True,
                final=True,
                upstream_commands=[t4.command])
    return [t1, t2, t3, t4, t5]
def test_configure_inputs():
    obj = ConfigureInputs(model_version_id=0)
    assert obj.command == (f'configure_inputs --model-version-id 0 '
                           f'--make --configure')
    assert obj.template_kwargs['model_version_id'] == '--model-version-id 0'
    assert obj.template_kwargs['make'] == '--make'
    assert obj.template_kwargs['configure'] == '--configure'
    assert obj.template_kwargs['log_level'] == '--log-level info'
    assert obj.template_kwargs['json_file'] == ''
    assert obj.template_kwargs['test_dir'] == ''

    assert obj.name == 'dmat_configure_inputs_0'
Example #4
0
def root_fit(model_version_id: int,
             location_id: int,
             sex_id: int,
             child_locations: List[int],
             child_sexes: List[int],
             skip_configure: bool = False,
             mulcov_stats: bool = True,
             n_sim: int = 10,
             n_pool: int = 10) -> List[_CascadeOperation]:
    """
    Create a sequence of tasks to do a top-level prior fit.
    Does a fit fixed, then fit both, then creates posteriors
    that can be used as priors later on. Saves its fit to be uploaded.

    Parameters
    ----------
    model_version_id
        The model version ID.
    location_id
        The parent location ID to run the model for.
    sex_id
        The sex ID to run the model for.
    child_locations
        The children to fill the avgint table with
    child_sexes
        The sexes to predict for.
    skip_configure
        Don't run a task to configure the inputs. Only do this if it has already happened.
    mulcov_stats
        Compute mulcov statistics at this level
    n_sim
    n_pool
    Returns
    -------
    List of CascadeOperations.
    """
    tasks = []
    if not skip_configure:
        t1 = ConfigureInputs(model_version_id=model_version_id)
        upstream = [t1.command]
        tasks.append(t1)
    else:
        upstream = None
    t2 = Fit(model_version_id=model_version_id,
             parent_location_id=location_id,
             sex_id=sex_id,
             fill=True,
             both=False,
             predict=True,
             upstream_commands=upstream,
             save_fit=True)
    tasks.append(t2)
    t3 = Predict(model_version_id=model_version_id,
                 parent_location_id=location_id,
                 sex_id=sex_id,
                 child_locations=child_locations,
                 child_sexes=child_sexes,
                 sample=False,
                 upstream_commands=[t2.command])
    tasks.append(t3)
    if mulcov_stats:
        t4 = Sample(model_version_id=model_version_id,
                    parent_location_id=location_id,
                    sex_id=sex_id,
                    n_sim=n_sim,
                    n_pool=n_pool,
                    fit_type='fixed',
                    asymptotic=True,
                    upstream_commands=[t3.command],
                    executor_parameters={'num_cores': n_pool})
        tasks.append(t4)
        t5 = MulcovStatistics(model_version_id=model_version_id,
                              locations=[location_id],
                              sexes=[sex_id],
                              sample=True,
                              mean=True,
                              std=True,
                              quantile=[0.025, 0.975],
                              upstream_commands=[t4.command])
        tasks.append(t5)
    return tasks
Example #5
0
def test_configure_inputs():
    obj = ConfigureInputs(model_version_id=0)
    assert obj.command == (f'configure_inputs --model-version-id 0 '
                           f'--make --configure')