Year: 2009

  • Don’t trust science, look for evidence.

    My wife has the flu (along with several other people I know), so it’s time for the usual recommendations from her mother to use a lot of vitamin C. Bad Science has fanned the flames of my natural combative streak, so I went out and tried to find a good study for the Vitamin C thing. I found this, and sent it on to the mother-in-law (who hasn’t replied yet). I also posted it in reply to a friend on Facebook’s status about dosing up on vit. C. Not long after that, a friend of hers replies with this link — implying, of course, that anyone can prove anything by linking to stuff on the Internet.

    So, of course, I reply, mentioning the number of people involved in the trials I had quoted, the significance of the results etc, which are all in the article and their links, while the other link has some hand-wavey trials with very little real evidence. And while I was posting this reply (which I’ll quote, because I’m lazy):
    “Your point was quite clear, which is why I had to reply: It’s very popular at the moment to roll one’s eyes and point to all the contradictory evidence on the internet, typically ending in “so in the end we don’t know as much as we think” equivocation of the options. The reality is that in many cases we do know quite well what is happening (as in the case with Vit. C), and the underlying meme that science is always to be taken with a pinch of salt is damaging to proper understanding of scientific method and results.

    This story repeats itself so many times that people become less likely to investigate the real results themselves (note that I’m not saying we should just trust scientists, we should be capable of understanding and evaluating the results). This goes for antiretrovirals, vaccines and many other treatments that have been shown to work, but are being used less than they should be because of general public distrust.”

    Of course, I can think of many other examples: Cell phones and/or their towers causing cancer (which is silly even on the face of it considering the power of the transmitters, but has also been pretty solidly disproved by long studies of cancer incidence), stuff in plastic water bottles causing cancer, overhead power lines, induction stoves causing sterility (same argument as with the cell phones). I think there is a general distrust of science, perhaps because people trusted science so much in the past and have become disillusioned.
    So here’s the thing: science isn’t really a trust thing. The whole point of science is that the evidence should be so overwhelming when you make a positive statement that it cannot be avoided. In fact, many scientists would say that no hypothesis can ever be proved, we can just fail to disprove it after trying very hard. Reading popular media summaries of scientific work makes this difficult because headlines like “Scientists do many tests and find insufficient evidence to disprove this hypothesis” don’t really sell papers. It also doesn’t gel well with the way people talk and think in general. The problem is that most of our decisions every day are made in extremely unscientific ways, including a wide array of effects that mean we can’t even reason accurately about our own experience. Trust is a proxy for research. If someone I trust a lot (or a source I trust a lot) says something, I am unlikely to repeat their experiments and likely to take them at their word. If I know something about the field in question, I can also interrogate my understanding of the situation to see if the results are consistent with other things I know to be true. In an ideal world, we would all be repeating all the experiments to make sure they work. In reality, we lack the time, skills and funds to do so, so trust is necessary.
    Where does this leave us? We need to base our trust on good, objective measures. In science, this means being able to look at the results and weigh the evidence, not the authors or publishers. It means checking consistency of theories and avoiding repeating claims that have poor statistical significance. It means doing real science, and not cargo cult science. More on that later.
  • OpenModelica Mac OS X binaries

    If you are getting strange error messages when trying to run OMShell or other parts of the stuff that gets installed when you install OpenModelica from the Mac OS X .dmg, try using the .command files instead of the application files. This is discussed in the README.osx file.

    The error you will see is

    basic_string::_S_construct NULL not valid

  • PySUNDIALS problems

    I’m still installing JModelica. I am now in the annoying “nothing broke during install, but somethings still fail when I use them” phase. I am using the instructions here, and using the example to test if things are working because JModelica doesn’t seem to have a test suite. This is what is happening now:


    In [1]: import jmodelica.examples.cstr as cstr
    /Library/Python/2.6/site-packages/jpype/_pykeywords.py:18: DeprecationWarning: the sets module is deprecated
    import sets
    ---------------------------------------------------------------------------
    AttributeError Traceback (most recent call last)

    /Users/alchemyst/ in ()

    /usr/local/Python/jmodelica/examples/cstr.py in ()
    9 from jmodelica.initialization.ipopt import NLPInitialization
    10 from jmodelica.initialization.ipopt import InitializationOptimizer
    ---> 11 from jmodelica.simulation.sundials import TrajectoryLinearInterpolation
    12 from jmodelica.simulation.sundials import SundialsDAESimulator
    13 from jmodelica.optimization import ipopt

    /usr/local/Python/jmodelica/simulation/sundials.py in ()
    14
    15 try:
    ---> 16 from pysundials import cvodes
    17 from pysundials import ida
    18 from pysundials import nvecserial

    /Library/Python/2.6/site-packages/pysundials/cvodes.py in ()
    39 import ctypes
    40 import sundials_core
    ---> 41 import nvecserial
    42
    43 realtype = nvecserial.realtype

    /Library/Python/2.6/site-packages/pysundials/nvecserial.py in ()
    56 PVector = ctypes.POINTER(_NVector)
    57
    ---> 58 nvecserial.N_VNew_Serial.restype = PVector
    59
    60 nvecserial.N_VLinearSum_Serial.argtypes = [realtype, PVector, realtype, PVector, PVector]

    /System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/ctypes/__init__.pyc in __getattr__(self, name)
    356 if name.startswith('__') and name.endswith('__'):
    357 raise AttributeError(name)
    --> 358 func = self.__getitem__(name)
    359 setattr(self, name, func)
    360 return func

    /System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/ctypes/__init__.pyc in __getitem__(self, name_or_ordinal)
    361
    362 def __getitem__(self, name_or_ordinal):
    --> 363 func = self._FuncPtr((name_or_ordinal, self))
    364 if not isinstance(name_or_ordinal, (int, long)):
    365 func.__name__ = name_or_ordinal

    AttributeError: dlsym(0x102e957d0, N_VNew_Serial): symbol not found

    It’s clear something is wrong with pysundials. On closer inspection, it seems that the dynamic library isn’t loading correctly. A part of the problem is explained by this:


    $ nm /usr/local/lib/libsundials*dylib

    /usr/local/lib/libsundials_cvode.1.0.0.dylib:
    0000000000000000 s __mh_dylib_header
    U dyld_stub_binder

    And many more like it. It seems like the .dylib built by the sundials build process doesn’t include the correct names. After much browsing and learning about how .dylibs work, I realise that this can be solved with a simple change to the configure script call (typical – it was actually documented: use –enable-shared in the build!):


    FFLAGS="-arch x86_64" ./configure --prefix=/usr/local --enable-examples --enable-shared
    make
    sudo make install

    Now nm shows the items in the dylib.

    I know sundials is working right because the examples compile and run fine. Now for pysundials:


    In [2]: from pysundials import cvode
    ---------------------------------------------------------------------------
    AttributeError Traceback (most recent call last)

    /Users/alchemyst/tmp/pysundials-2.3.0-rc2/examples/cvode/serial/ in ()

    /Library/Python/2.6/site-packages/pysundials/cvode.py in ()
    350 if ret 352 cvode.CVodeSetFdata.argtypes = [ctypes.c_void_p, ctypes.c_void_p]
    353 cvode.CVodeSetFdata.restype = ctypes.c_int
    354

    /System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/ctypes/__init__.pyc in __getattr__(self, name)
    356 if name.startswith('__') and name.endswith('__'):
    357 raise AttributeError(name)
    --> 358 func = self.__getitem__(name)
    359 setattr(self, name, func)
    360 return func

    /System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/ctypes/__init__.pyc in __getitem__(self, name_or_ordinal)
    361
    362 def __getitem__(self, name_or_ordinal):
    --> 363 func = self._FuncPtr((name_or_ordinal, self))
    364 if not isinstance(name_or_ordinal, (int, long)):
    365 func.__name__ = name_or_ordinal

    AttributeError: dlsym(0x1014d04f0, CVodeSetFdata): symbol not found

    And the original thing I was trying to do:


    In [1]: import jmodelica.examples.cstr as cstr
    /Library/Python/2.6/site-packages/jpype/_pykeywords.py:18: DeprecationWarning: the sets module is deprecated
    import sets
    ---------------------------------------------------------------------------
    OSError Traceback (most recent call last)

    /Users/alchemyst/tmp/pysundials-2.3.0-rc2/examples/cvode/serial/ in ()

    /usr/local/Python/jmodelica/examples/cstr.py in ()
    9 from jmodelica.initialization.ipopt import NLPInitialization
    10 from jmodelica.initialization.ipopt import InitializationOptimizer
    ---> 11 from jmodelica.simulation.sundials import TrajectoryLinearInterpolation
    12 from jmodelica.simulation.sundials import SundialsDAESimulator
    13 from jmodelica.optimization import ipopt

    /usr/local/Python/jmodelica/simulation/sundials.py in ()
    14
    15 try:
    ---> 16 from pysundials import cvodes
    17 from pysundials import ida
    18 from pysundials import nvecserial

    /Library/Python/2.6/site-packages/pysundials/cvodes.py in ()
    44 NVector = nvecserial.NVector
    45
    ---> 46 cvodes = sundials_core.loadlib("cvodes")
    47
    48 #Linear Multistep method constants


    /Library/Python/2.6/site-packages/pysundials/sundials_core.pyc in loadlib(libname)
    62 lib = ctypes.CDLL(libpaths[libname])
    63 except OSError, e:
    ---> 64 raise OSError("%snCannot load shared library %s. Please check you config file and ensure the paths to the shared libraries are correct."%(e, libpaths[libname]))
    65 return lib
    66

    OSError: dlopen(/usr/local/libsundials_cvodes.dylib, 6): image not found
    Cannot load shared library /usr/local/libsundials_cvodes.dylib. Please check you config file and ensure the paths to the shared libraries are correct.

    So the error has changed to it not finding the library. Something must be screwy with paths. Frustrating afternoon.

  • Matplotlib missing pylab

    I love matplotlib because it allowed me to move away from Matlab to Python as my platform of choice for quick problem solving. It is a very good plotting library. During my previous post on installing JModelica, I mentioned that matplotlib is failing. I recompiled a lot of stuff and finally thought I had it all licked (some of the problems had to do with gfortran not doing 64 bit code by default). Then I ran ipython, and import pylab fails. Strange, considering that import matplotlib works fine. Even stranger, ipython -pylab works fine, too.


    In [1]: import pylab
    ---------------------------------------------------------------------------
    ImportError Traceback (most recent call last)

    /Users/alchemyst/tmp/ in ()

    /Library/Python/2.6/site-packages/pylab.py in ()
    ----> 1 from matplotlib.pylab import *
    2 import matplotlib.pylab
    3 __doc__ = matplotlib.pylab.__doc__

    ImportError: No module named pylab
    After some very frustrating searching around and reading of forum posts, it finally dawns on me that I am in my tmp directory, where the matplotlib directory I built it is still around. So that’s what’s happening! import matplotlib is importing the module from this directory. There is no pylab module there, so it’s giving the error. Hopefully someone experiencing similar problems may find some help in this post.
  • Building JModelica on Mac OS X Snow Leopard

    I am again working on my PhD and I need to get my development environment sorted out. I have almost had it with OpenModelica, because it is so hard to get going on my Mac. I have tried to contribute code to the build process, but the configure script still bombs out based on environment variables that have to be set by hand and so forth. I have therefore decided to try JModelica instead. I duly followed the start of the install instructions, including grabbing a version of the repository using svn (I was not warned that the repo is about 130 MiB, though!).

    Installing Ipopt

    Ipopt is an interior point solver, part of the COIN-OR project. JModelica includes some nifty optimisation functionality via Optimica so it is quite good to have Ipopt (even though it is not required). So I download the latest Ipopt tarball and now I’m faced with more dependancies: BLAS and LAPACK. After some toying around with these packages, I learned the following:

    • The Mac dev tools inclode cBLAS and cLAPACK as part of the vecLib framework (something to remember). However, these don’t play well with gfortran on 10.6
    • You can download and build them yourself with little effort, but most build tools assume you are going to use -lblas to access it, which is a bit harder to override.
    • Ipopt actually has scripts for downloading these for compiling Ipopt, as documented here (although, crucially, not in the install documentation I was following in the distribution)
    • The Ipopt instructions advise using the HSL libraries, but the process you have to go through to get them takes time, I’ve done it, but I still don’t have access, so I’m just going for Mumps instead.
    So, after wasting much time, all I really needed to do was

    cd ThirdParty
    for lib in Blas Lapack ASL Metis Mumps; do
    cd $lib;
    ./get.$lib
    cd ..
    done
    cd ..
    ./configure --prefix=/usr/local # to get stuff where I want it
    make
    Well, after that the configure script worked, but there was a build error (or a couple of them, all with the same form):


    lpBlas.cpp:211: error: ‘dtrsm’ was not declared in this scope
    IpBlas.cpp:211: error: ‘DTRSM’ was not declared in this scope
    IpBlas.cpp:211: error: ‘F77_FUNC’ cannot be used as a function

    Looking at the source, it was clear that F77_FUNC was supposed to be a macro that helps with fortran execution or something. Further investigation (grep -R “#define F77_FUNC”) shows that it’s supposed to be a mangling thing. Even further investigation shows that one of the configure messages that wizzed by is “WARNING: unknown Fortran name-mangling scheme”). The configure script then just silently doesn’t define F77_FUNC, leading to these errors. After scrutinising

    Ipopt/config.log

    I noticed that ld was complaining about the archetecture. I modify my call to configure thus:

    FFLAGS="-arch=x86_64"

    Now make runs! Make test succeeds and I can do

    sudo make install

    and get to the main game.

    Building JModelica

    So, back to the original build instructions. I modify the configure again to point to the right places.


    cd JModelica
    mkdir build
    cd build
    ../configure --with-ipopt=/usr/local/ --prefix=/usr/local
    make

    Three minutes later, success! Now do build docs (which requires doxygen, obtained via fink and graphviz, using the Mac install package), which takes ages.

    Python support

    Last step is getting Python support, one of the other cool things about JModelica. The docs list the following packages:

    Package URL Steps I took to install
    JPype http://jpype.sourceforge.net/ Download, run sudo python setup.py install
    lxml http://codespeak.net/lxml/ sudo easy_install lxml
    NumPy 1.2.0 http://numpy.scipy.org/ Already installed
    Scipy 0.7 http://www.scipy.org/ Already installed
    PySundials 2.3 http://sourceforge.net/projects/pysundials/ Install SUNDIALS, change reference in src/sundials_core_aux.c from sundials/sundials_smalldense.h to sundials/sundials_dense.h. sudo python setup.py install
    pyreadline =1.5 http://ipython.scipy.org/dist/ sudo easy_install pyreadline
    Matplotlib http://matplotlib.sourceforge.net/ already installed
    Nose http://code.google.com/p/python-nose/ sudo easy_install nose

    And now it still doesn’t work, because Matplotlib is causing segfaults. I guess it’s time to call it a day — more in getting Matplotlib to go tomorrow.