def cooperative_tradeoff(community, min_growth, fraction, fluxes, pfba): """Find the best tradeoff between community and individual growth.""" with community as com: check_modification(community) min_growth = _format_min_growth(min_growth, community.species) _apply_min_growth(community, min_growth) com.objective = 1000.0 * com.variables.community_objective min_growth = optimize_with_retry( com, message="could not get community growth rate.") / 1000.0 if not isinstance(fraction, Sized): fraction = [fraction] else: fraction = np.sort(fraction)[::-1] # Add needed variables etc. regularize_l2_norm(com, 0.0) results = [] for fr in fraction: com.variables.community_objective.lb = fr * min_growth com.variables.community_objective.ub = min_growth sol = solve(community, fluxes=fluxes, pfba=pfba) if sol.status != OPTIMAL: sol = crossover(com, sol, fluxes=fluxes, pfba=pfba) results.append((fr, sol)) if len(results) == 1: return results[0][1] return pd.DataFrame.from_records(results, columns=["tradeoff", "solution"])
def cooperative_tradeoff(community, min_growth, fraction, fluxes, pfba): """Find the best tradeoff between community and individual growth.""" with community as com: solver = interface_to_str(community.problem) check_modification(community) min_growth = _format_min_growth(min_growth, community.taxa) _apply_min_growth(community, min_growth) com.objective = com.scale * com.variables.community_objective min_growth = (optimize_with_retry( com, message="could not get community growth rate.") / com.scale) if not isinstance(fraction, Sized): fraction = [fraction] else: fraction = np.sort(fraction)[::-1] # Add needed variables etc. regularize_l2_norm(com, 0.0) results = [] for fr in fraction: com.variables.community_objective.lb = fr * min_growth com.variables.community_objective.ub = min_growth sol = solve(community, fluxes=fluxes, pfba=pfba) # OSQP is better with QPs then LPs # so it won't get better with the crossover if (sol.status != OPTIMAL and solver != "osqp"): sol = crossover(com, sol, fluxes=fluxes, pfba=pfba) results.append((fr, sol)) if len(results) == 1: return results[0][1] return pd.DataFrame.from_records(results, columns=["tradeoff", "solution"])
def knockout_taxa( community, taxa, fraction, method, progress, diag=True ): """Knockout a taxon from the community.""" with community as com: check_modification(com) min_growth = _format_min_growth(0.0, com.taxa) _apply_min_growth(com, min_growth) com.objective = com.scale * com.variables.community_objective community_min_growth = ( optimize_with_retry(com, "could not get community growth rate.") / com.scale ) regularize_l2_norm(com, fraction * community_min_growth) old = com.optimize().members["growth_rate"] results = [] iter = track(taxa, description="Knockouts") if progress else taxa for sp in iter: with com: logger.info("getting growth rates for " "%s knockout." % sp) [ r.knock_out() for r in com.reactions.query( lambda ri: ri.community_id == sp ) ] sol = optimize_with_fraction(com, fraction) new = sol.members["growth_rate"] if "change" in method: new = new - old if "relative" in method: new /= old results.append(new) ko = pd.DataFrame(results, index=taxa).drop("medium", 1) ko = ko.loc[ko.index.sort_values(), ko.columns.sort_values()] if not diag: np.fill_diagonal(ko.values, np.NaN) return ko
def knockout_species(community, species, fraction, method, progress, diag=True): """Knockout a species from the community.""" with community as com: check_modification(com) min_growth = _format_min_growth(0.0, com.species) _apply_min_growth(com, min_growth) com.objective = 1000.0 * com.variables.community_objective community_min_growth = ( optimize_with_retry(com, "could not get community growth rate.") / 1000.0) regularize_l2_norm(com, fraction * community_min_growth) old = com.optimize().members["growth_rate"] results = [] if progress: species = tqdm(species, unit="knockout(s)") for sp in species: with com: logger.info("getting growth rates for " "%s knockout." % sp) [ r.knock_out() for r in com.reactions.query( lambda ri: ri.community_id == sp) ] sol = optimize_with_fraction(com, fraction) new = sol.members["growth_rate"] if "change" in method: new = new - old if "relative" in method: new /= old results.append(new) ko = pd.DataFrame(results, index=species).drop("medium", 1) if not diag: np.fill_diagonal(ko.values, np.NaN) return ko
def test_retry(community): sol = ms.optimize_with_retry(community) assert sol == approx(0.874, 0.001)