As theorists we often write complex computer code to generate the results which we present in our papers. We’re making that code openly available so that others can use it in their own research, or simply check that what we’re doing is correct. Here, you’ll find the code, frameworks and documentation we write to support our research.


  • C

  • Python

  • Bash

  • MMA

  • Guides

  • Papers

  • Demos
  • Linux memory profiler
  • Output to Mathematica

    Convert Python structures to Mathematica structures

    Get the code here.

    The module

    from mmaformatter import get_mma, save_as_mma

    provides functions for converting Python floats, complex numbers, lists, tuples, sets and dictionaries to their corresponding Mathematica form, e.g. scientific notation, associations. An equivalent module for C can be found here.

    One can pass any nested structure of arrays, tuples, sets, dictionaries (of primitives ints, floats, comple numbers, bools and strings) to get_mma to get strings of their Mathematica form.

    For example

    >>> get_mma(-1/999)
    '-1.00100*10^-03'
    
    >>> get_mma(1/(3+2j), precision=2)
    '2.31*10^-01-1.54*10^-01I'
    
    >>> get_mma([1, 1/2, 3j])
    '{1, 5.00000*10^-01, 0+3I}'

    and here printing the output (for clarity)

    >>> get_mma({1:2, "cat":"dog"})
    <|  
        1 -> 2,  
        "cat" -> "dog"  
    |>
    
    >>> get_mma(
            {"Tyson":(0,0,7), "nest":{"birds":4}}, 
            keep_symbols=True)
    <|  
        Tyson -> {0, 0, 7},  
        nest -> <| birds -> 4 |>   
    |>
    

    The function save_as_mma can take the same structures and write their Mathematica form to file, to be read by Mathematica.

    For example,

    data = [1.013, 10.331, .59102, 5.0, 6.0]
    struc = {"patient":"Tyson", "trial":112, "conductivity":data}
    save_as_mma(struc, "assoc.txt", precision=2)

    produces a file "assoc.txt" with contents

    <|
        "trial" -> 112,
        "patient" -> "Tyson",
        "conductivity" -> {1.01*10^+00, 1.03*10^+01, 5.91*10^-01, 5, 6}
    |>
    

    which can then be read into Mathematica via

    SetDirectory @ NotebookDirectory[];
    assoc = Get["assoc.txt"]
    
    ListLinePlot @ assoc["conductivity"]

    which produces the very-terrific graph

    a terrible graph


    Both get_mma and save_as_mma accept optional arguments

    • key_order: use explicit ordering in the Mathematica association for the keys in the passed dictionary
    • keep_symbols: whether python strings should be converted to Mathematica symbols, else be enquoted to become strings
    • precision: the number of decimal digits in the scientific notation format of floats
    • keep_ints: whether integers should be kept formatted as such, else converted to scientific notation (at the precision supplied); applies to the real and imaginary components of complex numbers too
  • SLURM param sweeper

4 Comments

Tyson Jones

Tyson Jones · January 10, 2018 at 6:15 pm

Feel free to comment below (using a WordPress account) to ask questions, point out errors or suggest changes, or otherwise email me at

firstname.lastname@materials.ox.ac.uk

Simon Benjamin

Simon Benjamin · April 17, 2018 at 2:38 pm

Instructions worked like a charm, thanks Tyson!
Two minor things:
1. The in-page links from the contents to the item don’t seem to work in Chrome.
2. For the examples, maybe add “gsl_matrix_get(myMatr, 1,2);” after populating the matrix, to show users that they can get the number out with …_get

Simon Benjamin

Simon Benjamin · April 21, 2018 at 12:39 am

Something for the GSL Guide under “construct matrices and vectors”… It really useful to stress that the matrices and vectors are NOT zero initialised, so they are fully of random junk when instantiated (albeit the random junk is quite likely to be a zero). Programmer must take care to set every value.
I forgot this and spent ages chasing a bug.

    Simon Benjamin

    Simon Benjamin · April 21, 2018 at 5:07 pm

    Extra: I see that things like gsl_permutation_calloc (i.e. ending in calloc not alloc) will initialise to zero for you.

Leave a Reply

Your email address will not be published. Required fields are marked *