This is an archival dump of old wiki content --- see scipy.org for current material

David Huard

Adresse électronique : <David DOT Huard AT gmail DOT com>

I'm working on the assessment of uncertainties in hydrology via Bayesian analysis. Previously a Matlab user, I find I'm much more productive with Python. I'm using SciPy in conjuction with PyMC to evaluate the Bayesian posterior in cases where analytical integration is impossible. I'm exploiting the python glue to code heavily used pieces of code in Fortran and integrating them into modules with F2PY.

Here is a small collection of scripts I wrote that may be of general interest. These codes as put in the public domain. Use at your own risk.

histogram

This is an histogramming function using the fastest from three different strategies. It is born out of a thread on the NumPy user list, and includes contributions from Eric Jones, Rick White and Cameron Walsh. histogram.py typed_array_converter.py

array2tex

   1 def array2tex(a, toptitle=None, lefttitle=None, cols=None, format=None, \
   2 hlines=False, savefile=None):
   3     """Print the array inside a LaTeX tabular environment.
   4     Parameters
   5     ----------
   6     toptitle:  Sequence of strings or numbers to fill the first row.
   7     lefttitle: Sequence of strings or numbers to fill the first column.
   8     cols:      Column formatting argument, eg. '|l|ccc|'.
   9                Default is '|c|c|...c|'.
  10     format:    List of format string, eg ['%d', '%5.1f'].
  11                Defaults to array data type.
  12     hlines:    Boolean, if True, inserts an horizontal line between rows,or
  13                array of integers, inserts an horizontal line at each position in
  14                hlines. Example: [0,1,1,2,3,4].
  15     savefile:  Output file name. Default is to print to standard output.
  16     """
  17     arr = atleast_2d(a)
  18     nrow =arr.shape[0]
  19     ncol = arr.shape[1]
  20     if type(toptitle) is ndarray:
  21         toptitle = toptitle.astype(list)
  22     if cols is None:
  23         Cols = '|'+ncol*'c|'
  24     if lefttitle is not None:
  25         if type(lefttitle) == str:
  26             lefttitle = [lefttitle]
  27         if len(toptitle) == ncol: # Missing toptitle for  leftitle column
  28             concatenate(([' '], toptitle))
  29             if cols is None:
  30                 Cols += 'c|'
  31     code = []
  32     if toptitle is not None:
  33         code.append((' & ').join([str(t) for t in toptitle])+ r'\\')
  34     if format is None:
  35         if arr.dtype.name[:3] == 'int':
  36            format = ncol * ['%d']
  37         elif arr.dtype.name[:5] == 'float':
  38             format = ncol * ['%f']
  39         else:
  40             raise 'Data type not implemented.'
  41     if type(format)==str:
  42        format = [format]*ncol
  43     elif len(format) == 1:
  44         format = format*ncol
  45     for i, row in enumerate(arr):
  46         line = []
  47         if lefttitle is not None:
  48             line.append(str(lefttitle[i]))
  49         for x, f in zip(row, format):
  50             line.append(f%x)
  51         code.append((' & ').join(line)+r'\\')
  52     # Add horizontal lines
  53     if hlines is True:
  54         hlines = range(nrow+2)
  55     if hlines is not False:
  56         for i in hlines[::-1]:
  57             code.insert(i, r'\hline')
  58     code.insert(0, r'\begin{tabular}{%(Cols)s}'%vars())
  59     code.append(r'\end{tabular}')
  60     table = ('\n').join(code)
  61     if savefile is None:
  62         return table
  63     else:
  64         f = open(savefile, 'w')
  65         print >>f, table
  66         f.close()
  67         return None


SciPy: DavidHuard (last edited 2015-10-24 17:48:26 by anonymous)