gravelLab / tracts

A set of tools for modelling ancestry patterns along the genome.
GNU General Public License v2.0
22 stars 14 forks source link

Error when attempting to fix multiple parameters using ancestry proportions. #29

Open General-Solution opened 1 year ago

General-Solution commented 1 year ago

Tracts gives the following error when attempting to fix multiple parameters using ancestry proportions.


    Traceback (most recent call last):
      File "/share/hennlab/projects/tracts2_testing/at2.py", line 14, in <module>
        tracts.run_tracts('test_driver.yaml')
      File "/share/hennlab/projects/tracts2_testing/tracts/tracts/driver.py", line 59, in run_tracts
        params_found, likelihoods = run_model_multi_params(func, bound, pop, population_labels, parse_start_params(driver_spec['start_params'], driver_spec['repetitions'], driver_spec['seed'], model, time_scaling_factor), exclude_tracts_below_cM=exclude_tracts_below_cM)
      File "/share/hennlab/projects/tracts2_testing/tracts/tracts/driver.py", line 157, in run_model_multi_params
        params_found, likelihood_found = run_model(model_func, bound_func, population, population_labels, start_params, exclude_tracts_below_cM=exclude_tracts_below_cM)
      File "/share/hennlab/projects/tracts2_testing/tracts/tracts/driver.py", line 168, in run_model
        xopt = tracts.optimize_cob(startparams, bins, Ls, data, nind, model_func, outofbounds_fun=bound_func, cutoff=cutoff, epsilon=1e-2)
      File "/home/awreynolds/.local/lib/python3.10/site-packages/tracts/core.py", line 1846, in optimize_cob
        outputs = scipy.optimize.fmin_cobyla(
      File "/home/awreynolds/.local/lib/python3.10/site-packages/scipy/optimize/_cobyla_py.py", line 34, in wrapper
        return func(*args, **kwargs)
      File "/home/awreynolds/.local/lib/python3.10/site-packages/scipy/optimize/_cobyla_py.py", line 181, in fmin_cobyla
        sol = _minimize_cobyla(func, x0, args, constraints=con,
      File "/home/awreynolds/.local/lib/python3.10/site-packages/scipy/optimize/_cobyla_py.py", line 34, in wrapper
        return func(*args, **kwargs)
      File "/home/awreynolds/.local/lib/python3.10/site-packages/scipy/optimize/_cobyla_py.py", line 264, in _minimize_cobyla
        f = c['fun'](x0, *c['args'])
      File "/share/hennlab/projects/tracts2_testing/tracts/tracts/driver.py", line 46, in <lambda>
        bound = lambda params: model.out_of_bounds(scale_select_indices(params, model.is_time_param(), time_scaling_factor))
      File "/home/awreynolds/.local/lib/python3.10/site-packages/tracts/parametrized_demography.py", line 387, in out_of_bounds
        return super().out_of_bounds(self.get_full_params(params))
      File "/home/awreynolds/.local/lib/python3.10/site-packages/tracts/parametrized_demography.py", line 409, in get_full_params
        solved_params = scipy.optimize.fsolve(lambda params_to_solve: _param_objective_func(self, params_to_solve), (.2,))
      File "/home/awreynolds/.local/lib/python3.10/site-packages/scipy/optimize/_minpack_py.py", line 162, in fsolve
        res = _root_hybr(func, x0, args, jac=fprime, **options)
      File "/home/awreynolds/.local/lib/python3.10/site-packages/scipy/optimize/_minpack_py.py", line 228, in _root_hybr
        shape, dtype = _check_func('fsolve', 'func', func, x0, args, n, (n,))
      File "/home/awreynolds/.local/lib/python3.10/site-packages/scipy/optimize/_minpack_py.py", line 25, in _check_func
        res = atleast_1d(thefunc(*((x0[:numinputs],) + args)))
      File "/home/awreynolds/.local/lib/python3.10/site-packages/tracts/parametrized_demography.py", line 409, in <lambda>
        solved_params = scipy.optimize.fsolve(lambda params_to_solve: _param_objective_func(self, params_to_solve), (.2,))
      File "/home/awreynolds/.local/lib/python3.10/site-packages/tracts/parametrized_demography.py", line 403, in _param_objective_func
        full_params = self.insert_params(full_params, params_to_solve)
      File "/home/awreynolds/.local/lib/python3.10/site-packages/tracts/parametrized_demography.py", line 440, in insert_params
        raise ValueError('Incorrect number of parameters to be solved')
    ValueError: Incorrect number of parameters to be solved```
General-Solution commented 1 year ago

The issue appears to have been that, when using scipy.optimize.fsolve(func, x0) to solve the parameters fixed by ancestry proportions, the passed initial value x0 was not scaled to have the same length as the number of parameters to be fixed. This led to an error when inserting the fixed parameters into the already provided parameters to get the full parameters of the model.