GEMMA python module home

Table Of Contents

gemma Package

gemma Package

gemmaclass Module

class gemma.gemmaclass.gemma(fname='gemma_output.bin', version=34, persistent=True)[source]

Bases: object

Class to read out gemma output.bin

Reads in header information and further acts as an iterator object. Data from samples are not read by default.

Samples can be accessed as gemma[samplename] or gemma[sampleindex]

Parameters :

fname : str, filename of binary gemma output file, default gemma_output.bin

version : version of gemma executable, default 34

persistent : keep samples in memory if True (default), otherwise re-read from file

Attributes

fname str filename
nsamples int number of samples
niterations int number of iterations
diag bool g-emma diagnostics mode
useLLS bool linear least squares approximation used in g-emma
withintriangle bool behaviourals constrained within individual trapezoids/triangles
stopmax bool stopped iterations when maximum behaviourals were reached (and nbehave is therefore an approximation)
nendmembers int number of end-members
endmembers list of strings end-member names
nsolutes int number of solutes
solutes list of strings solute names
samples dict sample objects, with samplenames as keys
samplenames list sample names
persistent bool keep samples in memory
version int version of G-EMMA executable

Methods

em_contrib()[source]

Calculate end-member contributions for all samples, constructs DataFrame

Returns :emc : pandas.DataFrame with end-member contributions
next()[source]

Iterator over samples. Only through ordered samples list/dict

percentiles(percentiles, params, mask=None)[source]

Create DataFrame with percentiles of parameters for all samples. For subsequent use sets self.pctresult with the result. Uses self.pctresult if requested parameters / percentiles are in self.pctresult

Parameters :

percentiles : list-like of integers. Percentiles in the 0-100 range are reset to the 0-1 range.

params : list of parameters to calculate percentiles of.

mask : tuple with first item parameter name and second item equality

Returns :

pctresult : pandas.DataFrame

print_summary()[source]

Print summary data on gemma run

read_data(nrows=None)[source]

Read data from samples. Not done by default

Parameters :nrows : int, no of rows to read from sample data. Use None (default) for all

sampleclass Module

gemma.sampleclass.percentile(A, pct=0.5, W=1, method='interpolate')[source]

Calculate weighted percentiles of A, given weights in W

Parameters :

A : MxN (masked) array of values to determine percentiles on

pct : list of P percentiles to be determined

W : Mx1 array of weigths

method : what to return if there is no exact percentile?

!!only interpolate implemented: weighted mean of two surrounding values

Returns :

P : PxN array of calculated percentiles

class gemma.sampleclass.sample(gemma=None, read_data=False)[source]

Bases: object

Class that contains one g-emma sample run

Reads in sample header information and further acts as an iterator object along datarows. Data from samples are not read by default.

Data can be accessed as sample[column]

Parameters :

gemma : gemma-object

read_data : bool. Read data (default False)

Attributes

gemma gemma object parent gemma object
endmembers list of strings end-member names
name string name of sample (sample code)
date datetime date/time information of sample
nbehave int number of behavioural samples
data pandas.DataFrame sample data

Methods

boxplot(params, violin=True)[source]

Create boxplot or violin plot

Parameters :

params : list of parameters from data to draw

violin : bool, draw violin plot around boxplot if True (default)

Examples

(Source code, png, hires.png, pdf)

../_images/boxplot.png
dottyplot(params)[source]

Function to draw dotty plot of given parameters

Parameters :params : list of parameters from data to draw

Examples

(Source code, png, hires.png, pdf)

../_images/dottyplot.png
em_contrib()[source]

Function returns Series with frequencies of occurrence in the behavioural runs for each separate endmember and all possible end-member combinations

percentile(pct=[0.25, 0.5, 0.75], params=None, mask=None)[source]

Return DataFrame with values of params @ percentiles, weighed by likelihood (LH) column

Parameters :

pct : list of percentiles

params : list of parameters to calculate (default: all)

mask : boolean Series or ndarray to select data

Returns :

p : DataFrame PxN array of calculated percentiles

plot_residuals()[source]

Plot boxplot of residuals

read_data(nrows=None)[source]

Read data. Not done by default

Parameters :nrows : int, no of rows to read from sample data. Use None (default) for all

plotting Module

Plotting functions for emma glue results

gemma.plotting.create_colblack(params)[source]

Create color dictionary for use in params_time for a black/white plot

Parameters :params : list of parameters to color
Returns :col : dict of params -> colors (in this case black)
gemma.plotting.create_colcolor(params, dcol={'GWDB': 'yellow', 'INLET': 'b', 'GWAQD': 'darkorange', 'GWPH': 'g', 'PRECIP': 'lightblue', 'GWAQS': 'yellow', 'BOIL': 'darkorange'})[source]

Create color dictionary for use in params_time for a color plot Solutes part of end-members receive color of end-member

Parameters :

params : list of parameters to color

dcol : dict of end-member or solute names to colors

Returns :

col : dict of params -> colors

gemma.plotting.create_labels(params, dlabel={'GWDB': 'BD', 'BOIL': 'BL', 'PRECIP': 'PR', 'INLET': 'IL', 'MIX': 'Stream', 'GWAQS': 'AS', 'GWAQD': 'AD', 'GWPH': 'SL', 'SO4': 'SO_4^{2-}'})[source]

Create label dictionary for use in params_time Solutes part of end-members receive label of end-member - solute

Parameters :

params : list of parameters to label

dlabel : dict of parameters -> labels

Returns :

col : dict of params -> colors

gemma.plotting.endmembers_time(pct=(0, 50, 100), result='gemma_output.bin', separate=False, stacked=True, fracload='frac', ylim=None, col=None, labels=None, labelstr=None, unitstr=None, f_conv=8.64, **kwargs)[source]

Convenience function for plotting endmembers over time, including uncertainty. Params and param dicts are set to end-members,

labelstr and unitstr are also set.
Parameters :

fracload : plot either fractions plot or load plot (default ‘frac’)

others : see params_time

Examples

(Source code, png, hires.png, pdf)

../_images/plot_em_time.png
gemma.plotting.params_time(params=None, pct=(0, 50, 100), result='gemma_output.bin', separate=True, stacked=False, calcload=False, hiatuses=[], plot_peq=True, fname=None, xlim=None, ylim=None, col=None, labels=None, labelstr='fraction', unitstr='-', f_conv=1.0, returnfig=False, figsize=None)[source]

Workhorse function for plotting a lineplot of parameter values over time, including uncertainty

Parameters :

params : list of parameters to plot

pct : list of three or more percentiles: lower bound, median value, upper bound. If more than three are given, additional uncertainty bands are drawn

result : g-emma result, either filename or gemma object

separate : bool, plot separate plots for each param (default True)

stacked : bool, stack plots, eg when contributions are additional (default: False)

calcload : bool, calculate loads for solutes (default False)

hiatuses : list of lists of two datetime objects. Periods are excluded from the resulting plot

plot_peq : bool, plot precip, evap and discharge data (default True). Assumes a module+function plot_data.create_petq(afig=fig, box=box.next())

fname : filename to write plot to. If None (default), output to screen

xlim : list of limits in x (default None)

ylim : list of limits in y (default None)

col : dictionary of colors for each param

labels : dictionary of labels for each param,

labelstr : string, additional part of label, denoting figure content (default ‘fraction’)

unitstr : string, additional part of label, denoting unit of data (default ‘-‘)

f_conv : float, conversion factor for load calculation (default 1.)

returnfig : bool, return figure object and list of axes

figsize : size of figure

gemma.plotting.plot_calc_meas(fname_stream='gemma_stream.txt', pct=(0, 50, 100), result='gemma_output.bin', hiatuses=[], plot_peq=True, fname=None, returnfig=None, col=None, labels=None, **kwargs)[source]

Function to plot calculated concentrations against measured concentrations

Parameters :

fname_stream : filename of g-emma stream input file

pct : list-like of percentiles (default: 0,50,100)

result : g-emma result, either filename or gemma object

hiatuses : list of lists of two datetime objects. Draws vertical grey bands to exclude period from figure

fname : filename to write plot to. If None (default), output to screen

returnfig : bool, return figure object and list of axes

figsize : size of figure

kwargs : additional parameters to pass to params_time

Examples

(Source code, png, hires.png, pdf)

../_images/plot_calc_meas.png
gemma.plotting.plot_em_solutes(em, pct=(0, 50, 100), result='gemma_output.bin', solutes=None, plot_peq=False, calcload=False, hiatuses=[], xlim=None, ylim=None, fname=None)[source]

Convenience function to plot all solutes of a given endmember. including uncertainty. Params and param dicts are set to end-members,

labelstr and unitstr are also set.
Parameters :

em : end-member

others : see params_time

gemmafiles Module

File readers for G-EMMA input files

gemma.gemmafiles.endmembers_to_sep_dfs(df)[source]

Convert multi-index dataframe of endmembers - solute to dataframes per end-member of solute characteristics

Parameters :

df : pandas.DataFrame, usually result of read_gemma_endmembers

Returns :

mean : pandas.DataFrame of mean concentration end-members / solutes

sd : pandas.DataFrame of standard deviation end-members / solutes

min : pandas.DataFrame of minimum concentration end-members / solutes

max : pandas.DataFrame of max concentration end-members / solutes

gemma.gemmafiles.read_gemma_endmembers(fname='gemma_endmembers.txt')[source]

Read g-emma end-members file into a pandas DataFrame

Parameters :fname : filename (default: gemma_endmembers.txt)
Returns :p : multi-index (end-member, solute) pandas.DataFrame
gemma.gemmafiles.read_gemma_stream(fname='gemma_stream.txt')[source]

Read g-emma stream file into a pandas DataFrame

Parameters :fname : filename (default: gemma_stream.txt)
Returns :p : pandas.DataFrame (solutes as index, samples as columns)

readfuncs Module

gemma.readfuncs.readarray(f, m, n)[source]
gemma.readfuncs.readbool(f)[source]
gemma.readfuncs.readfloat(f)[source]
gemma.readfuncs.readint(f)[source]
gemma.readfuncs.readlong(f, x64=False)[source]

if x64: return struct.unpack(‘q’, f.read(8))[0] else:

gemma.readfuncs.readstring(f)[source]