pysb / pysb

Python framework for Systems Biology modeling
BSD 2-Clause "Simplified" License
163 stars 70 forks source link

Add units to model parameters, expressions, species #571

Open ortega2247 opened 1 year ago

ortega2247 commented 1 year ago

Hi, mistakes in units can lead to unexpected simulation results and some times hard to interpret results. Dimensional errors happen quite often. Would it be too hard to add dimensional analysis to pysb? I have been thinking we could add a unit argument (sympy has units) to parameter, expression objects, species and evaluate if the reaction rates have the desired units. Is something like this feasible?

lh64 commented 1 year ago

I think this is a good idea. Could we add a 'units' attribute to the 'Parameter' class? If we did, it should be possible to write a script that confirms that all units in a model are consistent. I'm not sure if we'd need to add a 'units' attribute to the 'Expression' class as well, or if units could be automatically derived for expressions based on parameter units. Something to think about, though.

FFroehlich commented 1 year ago

Agree that this would be interesting.

I think it should be possible to infer units for all model components from Monomer, time and parameter units for most models. Units for expressions could be necessary for some models, but that could also always be addressed by converting some factor in the expression to a parameter. Yet, it would be nice to make specification of units for expressions, observables, etc optional and only check consistency when units can be inferred from constituent symbols.

blakeaw commented 3 weeks ago

I'm currently working on a separate units add-on for pysb: https://github.com/Borealis-BioModeling/pysb-units

It still needs polishing but has many of the features mentioned here, including consistency checking and automatic inference of Expression units from constituent Parameters (and Observables, but this requires explicitly assigning units to the Observable at the moment).

Adapting it into the primary pysb source should be pretty straightforward, so if there is interest, I could potentially work on that and move the implementation towards a PR.

What do you think? @lh64 @FFroehlich @ortega2247.

FFroehlich commented 2 weeks ago

sounds great! We had some discussions about units in the context of PEtab and a draft for unit specification is currently being worked on. Would be great to have compatibility.