MEWpy icon indicating copy to clipboard operation
MEWpy copied to clipboard

IndexError when running KineticSimulation

Open V0idLord opened this issue 1 year ago • 1 comments

Hello,

I am working with an inhouse kinetic model (SBML format) at ITQB NOVA, and while I can run simulations on MEWpy <= 0.1.28, latter versions of the package do not seem to work. After retesting, I can't get it to work on the older version too, as such I suspect its caused by an updated dependency, unfortunately I dont have the original environment saved.

Running the following code:

sim = KineticSimulation(model,timeout=0)
res = sim.simulate(t_points=range(0,800,50))
res.find()

Results in the following error:

{
IndexError                                Traceback (most recent call last)
Cell In[8], line 2
      1 sim = KineticSimulation(model,timeout=0)
----> 2 res = sim.simulate(t_points=range(0,800,50))
      3 res.find()

File c:\\Code_Projects\\Master Thesis\\kinetic_modeling\\.venv\\lib\\site-packages\\mewpy\\simulation\\kinetic.py:347, in KineticSimulation.simulate(self, parameters, initcon, factors, t_points)
    345         warnings.warn(str(e))
    346 else:
--> 347     status, sstateRates, sstateConc, t, y = kinetic_solve(self.model,
    348                                                           initConcentrations,
    349                                                           time_steps,
    350                                                           params,
    351                                                           _factors)
    353 return KineticSimulationResult(self.model, status, factors=_factors, rates=sstateRates,
    354                                concentrations=sstateConc, t=t, y=y)

File c:\\Code_Projects\\Master Thesis\\kinetic_modeling\\.venv\\lib\\site-packages\\mewpy\\simulation\\kinetic.py:67, in kinetic_solve(model, y0, time_steps, parameters, factors)
     50 \"\"\"Kinetic solve method that invokes an available ODE solver. 
     51 
     52 :param model: The kinetic model
   (...)
     63 :rtype: _type_
     64 \"\"\"
     66 rates = OrderedDict()    
---> 67 f = model.get_ode(r_dict=rates, params=parameters, factors=factors)
     68 solver = ode_solver_instance(f, KineticConfigurations.SOLVER_METHOD)
     70 try:

File c:\\Code_Projects\\Master Thesis\\kinetic_modeling\\.venv\\lib\\site-packages\\mewpy\\model\\kinetic.py:740, in ODEModel.get_ode(self, r_dict, params, factors)
    737 r = r_dict if r_dict is not None else dict()
    739 np.seterr(divide='ignore', invalid='ignore')
--> 740 exec(self.build_ode(factors), globals())
    741 ode_func = eval('ode_func')
    743 return lambda t, y: ode_func(t, y, r, p, v)

File c:\\Code_Projects\\Master Thesis\\kinetic_modeling\\.venv\\lib\\site-packages\\mewpy\\model\\kinetic.py:696, in ODEModel.build_ode(self, factors, local)
    693 v = {p_id: f\"v['{p_id}']\" for p_id in self.variable_params}
    694 rmap = OrderedDict({**m, **c, **p, **v})
--> 696 parsed_rates = {r_id: ratelaw.parse_law(rmap, local=local)
    697                 for r_id, ratelaw in self.ratelaws.items()}
    699 r = {r_id: f\"({parsed_rates[r_id]})\" for r_id in self.ratelaws.keys()}
    701 rmap.update(r)

File c:\\Code_Projects\\Master Thesis\\kinetic_modeling\\.venv\\lib\\site-packages\\mewpy\\model\\kinetic.py:696, in <dictcomp>(.0)
    693 v = {p_id: f\"v['{p_id}']\" for p_id in self.variable_params}
    694 rmap = OrderedDict({**m, **c, **p, **v})
--> 696 parsed_rates = {r_id: ratelaw.parse_law(rmap, local=local)
    697                 for r_id, ratelaw in self.ratelaws.items()}
    699 r = {r_id: f\"({parsed_rates[r_id]})\" for r_id in self.ratelaws.keys()}
    701 rmap.update(r)

File c:\\Code_Projects\\Master Thesis\\kinetic_modeling\\.venv\\lib\\site-packages\\mewpy\\model\\kinetic.py:295, in KineticReaction.parse_law(self, map, local)
    292 r_map = map.copy()
    293 r_map.update(m)
--> 295 return self.replace(r_map, local=local)

File c:\\Code_Projects\\Master Thesis\\kinetic_modeling\\.venv\\lib\\site-packages\\mewpy\\model\\kinetic.py:195, in Rule.replace(self, parameters, local, infix, latex)
    193 if local:
    194     param.update(self.parameters)
--> 195 t = self.tree.replace(param)
    196 if latex:
    197     return Latex(t.to_latex()[0])

File c:\\Code_Projects\\Master Thesis\\kinetic_modeling\\.venv\\lib\\site-packages\\mewpy\\model\\kinetic.py:150, in Rule.tree(self)
    144 \"\"\"Parsing tree of the law.
    145 
    146 Returns:
    147     Node: Root node of the parsing tree.
    148 \"\"\"
    149 if not self._tree:
--> 150     self._tree = build_tree(self.law, Arithmetic)
    151 return self._tree

File c:\\Code_Projects\\Master Thesis\\kinetic_modeling\\.venv\\lib\\site-packages\\mewpy\\util\\parsing.py:742, in build_tree(exp, rules)
    739     t = Node(f[0], Node(EMPTY_LEAF), build_tree(f[1], rules), 1)
    740 elif len(f) == 3:
    741     t = Node(
--> 742         f[0], build_tree(f[1], rules), build_tree(f[2], rules), 2
    743     )
    744 else:
    745     t = Node(token)

File c:\\Code_Projects\\Master Thesis\\kinetic_modeling\\.venv\\lib\\site-packages\\mewpy\\util\\parsing.py:769, in build_tree(exp, rules)
    767 t = Node(popped_item)
    768 t1 = tree_stack.pop()
--> 769 t2 = tree_stack.pop()
    770 t.right = t1
    771 t.left = t2

IndexError: pop from empty list"
}

Any idea what could be the problem?

V0idLord avatar Sep 09 '24 16:09 V0idLord

Some models I tried get loaded, but when trying to run KineticSimulation it fails with:

{
	"name": "AttributeError",
	"message": "'NoneType' object has no attribute 'keys'",
	"stack": "---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
Cell In[8], line 1
----> 1 ea.run()
      2 df_opt = ea.dataframe()
      3 df_opt.to_csv('optimization_simple_10.csv', index=False)

File c:\\Users\\plato\\Projects\\Master_Thesis\\optimizations\\.venv\\lib\\site-packages\\mewpy\\optimization\\ea.py:171, in AbstractEA.run(self, simplify)
    168 self.problem.pre_process()
    170 if self.problem.number_of_objectives == 1:
--> 171     final_pop = self._run_so()
    172 else:
    173     final_pop = self._run_mo()

File c:\\Users\\plato\\Projects\\Master_Thesis\\optimizations\\.venv\\lib\\site-packages\\mewpy\\optimization\\jmetal\\ea.py:116, in EA._run_so(self)
    114 algorithm.observable.register(observer=PrintObjectivesStatObserver())
    115 self.algorithm = algorithm
--> 116 algorithm.run()
    118 result = algorithm.solutions
    119 return result

File c:\\Users\\plato\\Projects\\Master_Thesis\\optimizations\\.venv\\lib\\site-packages\\jmetal\\core\\algorithm.py:77, in Algorithm.run(self)
     74 self.start_computing_time = time.time()
     76 self.solutions = self.create_initial_solutions()
---> 77 self.solutions = self.evaluate(self.solutions)
     79 LOGGER.debug('Initializing progress')
     80 self.init_progress()

File c:\\Users\\plato\\Projects\\Master_Thesis\\optimizations\\.venv\\lib\\site-packages\\jmetal\\algorithm\\singleobjective\\genetic_algorithm.py:60, in GeneticAlgorithm.evaluate(self, population)
     59 def evaluate(self, population: List[S]):
---> 60     return self.population_evaluator.evaluate(population, self.problem)

File c:\\Users\\plato\\Projects\\Master_Thesis\\optimizations\\.venv\\lib\\site-packages\\jmetal\\util\\evaluator.py:36, in SequentialEvaluator.evaluate(self, solution_list, problem)
     34 def evaluate(self, solution_list: List[S], problem: Problem) -> List[S]:
     35     for solution in solution_list:
---> 36         Evaluator.evaluate_solution(solution, problem)
     38     return solution_list

File c:\\Users\\plato\\Projects\\Master_Thesis\\optimizations\\.venv\\lib\\site-packages\\jmetal\\util\\evaluator.py:29, in Evaluator.evaluate_solution(solution, problem)
     27 @staticmethod
     28 def evaluate_solution(solution: S, problem: Problem) -> None:
---> 29     problem.evaluate(solution)

File c:\\Users\\plato\\Projects\\Master_Thesis\\optimizations\\.venv\\lib\\site-packages\\mewpy\\optimization\\jmetal\\problem.py:294, in JMetalOUProblem.evaluate(self, solution)
    292 def evaluate(self, solution: KOSolution) -> KOSolution:
    293     candidate = set(solution.variables)
--> 294     p = self.problem.evaluate_solution(candidate)
    295     for i in range(len(p)):
    296         # JMetalPy only deals with minimization problems
    297         if self.obj_directions[i] == self.MAXIMIZE:

File c:\\Users\\plato\\Projects\\Master_Thesis\\optimizations\\.venv\\lib\\site-packages\\mewpy\\problems\\kinetic.py:111, in KineticOUProblem.evaluate_solution(self, solution, decode)
    109 p = []
    110 factors = self.decode(solution) if decode else solution
--> 111 simulation_results = self.kinetic_sim.simulate(factors=factors)
    112 for f in self.fevaluation:
    113     p.append(f(simulation_results, factors))

File c:\\Users\\plato\\Projects\\Master_Thesis\\optimizations\\.venv\\lib\\site-packages\\mewpy\\simulation\\kinetic.py:342, in KineticSimulation.simulate(self, parameters, initcon, factors, t_points)
    335 else:
    336     status, sstateRates, sstateConc, t, y = kinetic_solve(self.model,
    337                                                           initConcentrations,
    338                                                           time_steps,
    339                                                           params,
    340                                                           _factors)
--> 342 return KineticSimulationResult(self.model, status, factors=_factors, rates=sstateRates,
    343                                concentrations=sstateConc, t=t, y=y)

File c:\\Users\\plato\\Projects\\Master_Thesis\\optimizations\\.venv\\lib\\site-packages\\mewpy\\simulation\\kinetic.py:174, in KineticSimulationResult.__init__(self, model, status, factors, rates, concentrations, t, y)
    172 self.t = t
    173 self.y = y
--> 174 self.m_indexes = {k: v for v, k in enumerate(concentrations.keys())}

AttributeError: 'NoneType' object has no attribute 'keys'"
}

After testing with different models, it looks like models containing ListOfFunctions in the .xml file do not work. I took a working model, opened it in copasi and created a new savefile, it no longer opens in mewpy, tested with different sbml levels and versions and I get the same result. Is there any solution for this problem? I really like the optimization functionality of mewpy, much more useful compared to other software that I tried.

V0idLord avatar Nov 28 '24 16:11 V0idLord