Lisandro Dalcin <dalc...@...> writes:
> Could you tell me what language are you going to use for your "main"
> application? I mean, are you coding in Python, and then want to reuse
> the Fortran library? Or perhaps you are coding in C/Fortran, but want
> to be able to write your functions in Python? Depending on that, the
> approach is quite different (at least from my POV).
* Python is my main language.
* C is used for a rate function in a differential equation (provided to me from
elsewhere).
* Fortran has the numerical differentiation library, which I'd like to use for
the following reasons:
- Don't have to code it myself 8-)
- Presumably robust, efficient and numerically accurate.
- Citable publication.
- Parallel implementations are available (though I'm unsure how that will play
with a Cython callback)
I'd be very grateful if you could recommend a strategy for this use case, or
suggest other options. I include some more detail below in case you're
interested. As for the mixed-language programming, I can see the following
scenarios:
== Ideal case: f2cy lets me call Fortran library from Cython ==
(f2cy is described at fortrancython.wordpress.com.)
Then I could do, in principle:
cimport f2cy
import my_model # heavy initialization goes here
ndl = f2cy("NDL") # make Fortran library callable from Cython
cdef double phenotype(double* parameters, int number_of_parameters):
"""The function I want to differentiate,
with an argument list matching the NDL specification."""
return my_model.phenotype(parameters) # expensive function evaluation
# calling an NDL function which makes callbacks to my function:
print ndl.hessian(phenotype, ...) # returns a 2-d array of double
(In real life, the Fortran names are six characters long and devoid of vowels,
of course.)
== "Practicality beats elegance" case: do whatever it takes... ==
"cdef double phenotype..." as above, but calling phenotype() from Fortran
(supposedly as easy as calling it from C). Then the main program will have to
be in Fortran:
! This is how I make compiled C functions in my_model_cy.o
! available to Fortran. Incredibly simple:
external my_model_cy
! Would need to figure out a way
! to make the answer available for further processing...
print *, hessian(phenotype, ...)
(Maybe I could even make a reusable, flexible Fortran function that could be
called from Cython, but I wouldn't know how to pass function pointers to
it...and if I could, I should be able to call the NDL library directly.)
Thanks a lot for your help!
Jon Olav
== Details about my use case ==
My overall goal is to estimate parameter interaction in a detailed model of
heart cell physiology. The parameters represent conductances of cellular ion
channels and lots of other stuff. The model simulates the change over time in
transmembrane voltage and concentrations of various ions in the cell. I will
aggregate this to a single scalar-valued cellular phenotype, e.g. a measure of
the duration of the voltage peak. This gives me a scalar function H of an n-
dimensional parameter vector p.
The derivative d(H)/d(p_i) is a measure of the "sensitivity" of the chosen
phenotype to each model parameter p_j. However, this sensitivity may depend on
other parameters p_j. The second derivatives d^2 H / [d(p_j) d(p_i)] is a
measure of how a (small) change in parameter p_j modifies the effect of a
(small) change in parameter p_j on the chosen phenotype.
I will evaluate these derivatives at different points in parameter space and
study various phenotypic measures, orchestrating it all in Python/Numpy:
iterating over parameter scenarios, computing aggregate phenotypic measures,
storing results, making graphs (Matplotlib) and exporting for statistical
analysis (rpy2). Also, I'll want to concentrate on just a few parameters p_i
and p_j; this could be neatly done with ad hoc wrappers like:
p = ... # Full n-dimensional parameter vector
i = ... # Indices to the parameters I'll be looking at
def f(x, n):
p0 = p # copy of original parameter values
p0[i] = x # substitute the given parameters
return phenotype(p0) # call with full parameter vector
Having to code each of these in Fortran would probably make me cry.
Because the function evaluations are rather expensive, I'd like to use an
efficient, robust, and perhaps parallel library for the differentation.
_______________________________________________
Cython-dev mailing list
[email protected]
http://codespeak.net/mailman/listinfo/cython-dev