API

Utilities for working with the alphaMELTS executable and associated tabular data. Note that these are currently experimental and not affiliated with alphaMELTS. In the future, these utilities will likely also make use of the under-development python-melts.

Todo

  • As it is developed, also make use of python-melts.

  • Develop functions for automation over a grid (of P, T, H2O, fO2, X)

  • Have an option to aggregate summary data, and options to discard experiment data

  • Expansion of documentation

See the alphaMELTS site for more info [1] [2] [3] [4] [5] [6] [7].

References

pyrolite_meltsutil.automation

This submodule contains functions for automated execution of alphamelts 1.9.

Issues

  • names are truncated for modifychem melts files?

  • need a timeout so processes can keep going, add unfinished experiments to failed list

class pyrolite_meltsutil.automation.MeltsExperiment(name='MeltsExperiment', title='MeltsExperiment', fromdir='./', meltsfile=None, env=None, timeout=None, executable=None)[source]

Melts Experiment Object. For a single call to melts, with one set of outputs. Autmatically creates the experiment folder, meltsfile and environment file, runs alphaMELTS and collects the results.

Todo

  • Automated creation of folders for experiment results (see make_meltsfolder())

  • Being able to run melts in an automated way (see MeltsProcess)

  • Compressed export/save function

  • Post-processing functions for i) validation and ii) plotting

set_meltsfile(meltsfile, **kwargs)[source]

Set the meltsfile for the experiment.

Parameters:

meltsfile (pandas.Series | str | pathlib.Path) – Either a path to a valid melts file, a pandas.Series, or a multiline string representation of a melts file object.

set_envfile(env)[source]

Set the environment for the experiment.

Parameters:

env (str | pathlib.Path) – Either a path to a valid environment file, a pandas.Series, or a multiline string representation of a environment file object.

run(log=False, superliquidus_start=True)[source]

Call ‘run_alphamelts.command’.

cleanup()[source]
pyrolite_meltsutil.automation.process_modifications(cfg)[source]

Process modifications to an configuration composition.

Parameters:

cfg (dict) – Configuratiion dictionary.

Returns:

cfg – Configuratiion dictionary.

Return type:

dict

class pyrolite_meltsutil.automation.MeltsBatch(comp_df, default_config={}, config_grid={}, fromdir=PosixPath('.'), env=None, executable=None, timeout=None, logger=<Logger pyrolite_meltsutil.automation (WARNING)>)[source]

Batch of MeltsExperiment, which may represent evaluation over a grid of parameters or configurations.

Parameters:
  • comp_df (pandas.DataFrame) – Dataframe of compositions.

  • default_config (dict) – Dictionary of default parameters.

  • config_grid (class:dict) – Dictionary of parameters to systematically vary.

  • fromdir (str | pathlib.Path) – Directory to run the set of experiments from, and where the results of each of the experiments will be saved.

  • env (str | pathlib.Path | pyrolite_meltsutil.env.MELTS_Env) – Environment file to use, if not the default.

  • executable (str | pathlib.Path) – Path to an executable to use, if not the default (specifically the path to run_alphamelts.command or run_alphamelts.bat).

  • timeout (int) – Timeout in seconds after which to try and terminate an experiment.

  • logger (logging.Logger) – Logger to use for logging output, if not the default.

Variables:
  • compositions (list of dict) – Compositions to use for the experiments.

  • configs (list of dict) – Set of experiment configurations to use.

  • experiments (list of dict) – Combination of compositions and configurations to generate a ‘grid’ of experiments.

Todo

  • Can start with a single composition or multiple compositions in a dataframe

  • Enable grid search for individual parameters

  • Improved output logging/reporting

  • Calculate relative number of calculations to be performed for the est duration

    This is currently about correct for an isobaric calcuation at 10 degree temperature steps over few hundred degrees - but won’t work for different T steps.

  • Does number precision make a difference?

dump(experiments=None, to_dir=None)[source]

Serialize the configuration to a json file.

Parameters:
  • experiments (dict) – Dictionary of experiments to be serialized.

  • to_dir (str | pathlib.Path) – Directory to export file to.

run(overwrite=False, exclude=[], superliquidus_start=True, timeout=None, log=False)[source]
cleanup()[source]

pyrolite_meltsutil.download

alphaMELTS download and installsation utilities.

pyrolite_meltsutil.download.extract_zip(zipfile, output_dir)[source]

Extracts a zipfile without the uppermost folder.

Parameters:
  • zipfile (zipfile object) – Zipfile object to extract.

  • output_dir (str | Path) – Directory to extract files to.

pyrolite_meltsutil.download.download_melts(directory, version=None)[source]

Download and extract melts zip file to a given directory.

Parameters:
  • directory (str | pathlib.Path) – Directory into which to extract melts.

  • version (str) – Which alphamelts version to use. Defaults to latest stable version.

Todo

  • Check version, enable update-style overwrite

pyrolite_meltsutil.download.install_melts(install_dir=None, link_dir=None, eg_dir=None, native=True, temp_dir=PosixPath('/home/docs/temp/temp_melts'), keep_tempdir=False, with_readline=True, local=False, version=None)[source]
Parameters:
  • install_dir (str | pathlib.Path) – Directory into which to install melts executable.

  • link_dir (str | pathlib.Path) – Directory into which to deposit melts links.

  • eg_dir (str | pathlib.Path) – Directory into which to deposit melts examples.

  • native (bool, True) – Whether to install using python (True) or the perl scripts (windows).

  • temp_dir (str | pathlib.Path, $USER$/temp/temp_melts) – Temporary directory for melts file download and install.

  • keep_tempdir (bool, False) – Whether to cache tempoary files and preserve the temporary directory.

  • with_readline (bool, True) – Whether to also attempt to install with_readline.

  • local (bool) – Whether to install a version of melts into an auxiliary pyrolite data folder. This will override

pyrolite_meltsutil.env

alphaMELTS environment managment.

pyrolite_meltsutil.env.output_formatter(value)[source]

Output formatter for environment variable values.

Parameters:

value – Value to format.

Returns:

Formatted value.

Return type:

str

class pyrolite_meltsutil.env.MELTS_Env(prefix='ALPHAMELTS_', variable_model=None)[source]

Melts environment object.

Todo

  • Implement use as context manager.

export_default_env()[source]

Parse any environment variables which are already set. Reset environment variables after substituting defaults for unset variables.

dump(unset_variables=True, prefix=False, cast=<function MELTS_Env.<lambda>>)[source]

Export environment configuration to a dictionary.

Parameters:
  • unset_variables (bool) – Whether to include variables which are currently unset.

  • prefix (bool) – Whether to prefix environment variables (i.e with ALPHAMELTS_).

  • cast (callable) – Function to cast environment variable values.

Returns:

Dictionary of environent variables and their values.

Return type:

dict

to_envfile(unset_variables=False)[source]

Create a string representation equivalent to the alphamelts defualt environment file.

Parameters:

unset_variables (bool) – Whether to include unset variables in the output (commented out).

Returns:

String-representation of the environment which can be writen to a file.

Return type:

str

pyrolite_meltsutil.meltsfile

Utilities for reading and writing .melts files.

pyrolite_meltsutil.meltsfile.dict_to_meltsfile(d, linesep='\n', writetraces=True, modes=[], exclude=[], **kwargs)[source]

Converts a dictionary to a MELTSfile text representation. It requires ‘title’ and ‘initial composition’ lines, major elements to be represented as oxides in Wt% and trace elements in µg/g.

Parameters:
  • d (dict) – Dictionary to convert to a melts file.

  • linesep (str) – Line separation character.

  • writetraces (bool) – Whether to include traces in the output file.

  • modes (list) – List of modes to use (e.g. ‘isobaric’, ‘fractionate solids’).

  • exclude (list) – List of chemical components to exclude from the meltsfile.

Returns:

String representation of the meltsfile, which can be immediately written to a file object.

Return type:

str

Notes

  • Some of the parameters are one-to-many, including modes, phase fractionation,

    supression and coexist-limits.

Todo

  • Parameter validation.

pyrolite_meltsutil.meltsfile.ser_to_meltsfile(ser, linesep='\n', writetraces=True, modes=[], exclude=[], **kwargs)[source]

Converts a series to a MELTSfile text representation. It requires ‘title’ and ‘initial composition’ lines, major elements to be represented as oxides in Wt% and trace elements in µg/g.

Parameters:
  • ser (pandas.Series) – Series to convert to a melts file.

  • linesep (str) – Line separation character.

  • writetraces (bool) – Whether to include traces in the output file.

  • modes (list) – List of modes to use (e.g. ‘isobaric’, ‘fractionate solids’).

  • exclude (list) – List of chemical components to exclude from the meltsfile.

Returns:

String representation of the meltsfile, which can be immediately written to a file object.

Return type:

str

Todo

  • Parameter validation.

pyrolite_meltsutil.meltsfile.df_to_meltsfiles(df, linesep='\n', **kwargs)[source]

Creates a number of melts files from a dataframe.

Parameters:
  • df (pandas.DataFrame) – Dataframe from which to take the rows and create melts files.

  • linesep (str) – Line separation character.

Returns:

List of strings which can be written to file objects.

Return type:

list

pyrolite_meltsutil.meltsfile.from_meltsfile(filename)[source]

Read from a meltsfile into a pandas.DataFrame.

Parameters:

filename (str | pathlib.Path | io.BytesIO) – Filename, filepath or bytes object to read from.

Returns:

Dataframe containing meltsfile parameters.

Return type:

pandas.DataFrame

pyrolite_meltsutil.parse

Parsing utilities for use with alphaMELTS.

pyrolite_meltsutil.parse.read_meltsfile(meltsfile, **kwargs)[source]

Read in a melts file from a Series, Path or string.

Parameters:

meltsfile (pandas.Series | str | pathlib.Path) – Either a path to a valid melts file, a pandas.Series, or a multiline string representation of a melts file object.

Returns:

  • file (str) – Multiline string representation of a meltsfile.

  • path – Path to the original file, if it exists.

Notes

This function deconvolutes the possible ways in which one can pass either a meltsfile, or reference to a meltsfile.

pyrolite_meltsutil.parse.read_envfile(envfile, **kwargs)[source]

Read in a environment file from a MELTS_Env, Path or string.

Parameters:

envfile (MELTS_Env | str | pathlib.Path) – Either a path to a valid environment file, a pandas.Series, or a multiline string representation of a environment file object.

Returns:

  • file (str) – Multiline string representation of an environment file.

  • path – Path to the original file, if it exists.

pyrolite_meltsutil.parse.from_melts_cstr(composition_str, formula=True)[source]

Parses melts composition strings to dictionaries or formulae.

Parameters:
  • composition_str (str) – Composition to parse.

  • formula (bool) – Whether to output a periodictable.formula.Formula

Returns:

Dictionary containing components, or alternatively if formula = True, a Formula representation of the composition.

Return type:

dict | periodictable.formulas.Formula

Todo

  • Enable parsing of nested brackets in composition.

pyrolite_meltsutil.tables

Utilities for reading alphaMELTS table outputs.

pyrolite_meltsutil.vis

Visualisation utilities for working with alphaMELTS table outputs.

Todo

  • Pull out mineral compostional trends

pyrolite_meltsutil.util

Utility functions for pyrolite-meltsutil.