Transformations

Normal Score Transformation

Forward Transformation

pygeostat.transformations.nscore.nscore(df, variables, wtcol=None, catcol=None, ltrim=-1e+20, utrim=1e+20, nquant=101, refdf=None, refvars=None, refwtcol=None, outputtables=False, trnfolder='trn/', trnprefix='nscore', trnext='.trn', mute=False)

Python and f2py wrapper of nscore_module.f90 by John Manchuk See pygeostat/fortran/ to build the required nscore.pyd

** weights AND categories together untested. Writing out not working yet **

Parameters:
  • df (pd.DataFrame) – DataFrame containing the source information
  • variables (list or string) – List or string of variables from df that are to be Normal Scored
  • wtcol (str) – Column in df containing the weights
  • ltrim (float) – lower trimming limit
  • utrim (float) – upper trimming limit
  • nquant (int) – number of quantiles in the transformation table
  • refdf (pd.DataFrame) – reference distribution used to build the transformation tables, assumes this table contains the data and weight for each observation, columns indicated by wtcol
  • refvars (list or str) – List or string of the variable in refdf to use
  • refwtcol (str) – Column in refdf containing the weights
  • outputtables (bool) – indicate whether or not to output the transtables after nscore
  • trnfolder (str) – Folder to output transform tables to
  • trnprefix (str) – prefix to use for .trn files
  • trnext (str) – extension to use for transform files
  • mute (bool) – mute the warning that this function gives
Returns:

DataFrame of the normal scored variables trans (gs.TransTable): Transformation Tables for the set of variables

Return type:

rdf (pd.DataFrame)

Note

  • Only supports a single column of weights that should be in the df or refdf DataFrames, delineated by the column ‘wtcol’ or ‘refwtcol’, respectively
  • NScore for N-variables seems to be working.
  • Can consider 1 reference for all n-variables in df, or have n variables in refdf
  • Weights appear to be incorperated correctly…. this is not thoroughly tested.
  • categories not impelmented currently
Usage:

To normal score some data, a DataFrame can be created using pygeostat:

>>> import pygeostat as gs
>>> df = gs.DataFile('transdat.dat').data # the .data grabs the dataframe from the datafile

Now, choosing a column in the dataframe, can use this function:

>>> nsdf, transtable = gs.nscore( df, 'Au' )

The result is a dataframe nsdf containing the normal scores of ‘Au’ from df. Transtable contains the typical .trn that is commonly output with the standard GSLIB programs. By specifying columns in a list, n-variables can be transformed at once:

>>> nsdf, transtables = gs.nscore( df, ['Au','Cu'] )

Any kind of weights that are desired to be used in this function should be included in the passed dataframe, i.e. the column ‘wt’ should be a column in df, (and similar for the refdf and refwtcol):

>>> nsdf, transtable = gs.nscore( df, 'Au', wtcol='wt' )

To use a reference distribuion, import a second dataframe and specify the correct keyword arguments in the function call:

>>> refdf = gs.DataFile('refdat.dat').data
>>> nsdf, transtables = gs.nscore( df, ['Au','Cu'], refdf=refdf, refvars='var' )

Code author: Ryan Martin - 2016-04-01

Backward Transformation

pygeostat.transformations.nscore.backnscore(data, transtables, variables=None, rocktypes=None, rtcol=None, tlim=(-998, 1e+20), ltail=0, ltpar=None, utail=0, utpar=None)

Not working right yet

If rocktypes is left to its default value of None but rtcol is passed, all of the rock types found in the dataframe data are back-transformed.

If a rock type(s) is found in the data dataframe that is not specified in the argument rocktypes, the corresponding values are no back-transformed and will be returned as np. nan.

Parameters:
  • data (long-form) – If there is only one variable and one rock type, then a 1-D pd.
  • pd.Series, or np.ndarray of the data to be back-transformed can be passed. If (DataFrame,) –
  • is not the case, then a pd.DataFrame must be past. (this) –
  • transtables
  • variables
  • rocktypes
  • rtcol
  • tlim
  • ltail
  • tpar
  • utail
  • utpar

Returns:

Code author: Warren E. Black - 2016-04-05

Transformation Reference Tables Class

class pygeostat.transformations.trnstbls.TransTables(variables=None, rocktypes=None, pklfl=None)

Class to aid in the storage and loading of transformation tables. The goal is to standardize the way transformation table are stored so they can be accessed easier during a geostatistics workflow.

Transformation tables from GSLIB programs can be easily loaded. Alternatively, the class provides a space for transformation tables to be stored by pygeostat functions.

All tables are saved as a pandas dataframe and stored in the dictionary self.transtables. The dictionary is nested in that there is a parent dictionary which contains entries for all of the rock types. Within each rock type dictionary, there are dictionaries containing the transformation tables. As an example, the following would be called to access the transformation table for the variable 'Ag' within the rock type '2':

>>> transtables.transtables['2']['Ag']

If different rock types are not being considered and the parameter rocktypes is not passed, a default value of '0' is used to maintain the structure described above. This also holds true of there is only one variable and the parameter variables is not passed.

Parameters:
  • variables (list) – Variables that will be stored within the class
  • rocktypes (list) – Rock types that will be stored within the class

Examples

Create a TransTable class and load transformation tables from a folder:

>>> transtable = gs.TransTable(variables=['PVar1', 'PVar2'], rocktypes=[100, 200])
# By not specifying variables or rock types, it is assumed that the folder will contain
# all 4 t
>>> transtable.loadfolder(folder='./trns')

Code author: Warren E. Black - 2016-04-05

Get Number of Variables Stored

TransTables.nvar()

Return the number of variables stored within the class. This method is used to get this value in case variables are added after the initialization of the class.

Get Number of Rock Types Stored

TransTables.nrt()

Return the number of rock types stored within the class. This method is used to get this value in case rock types are added after the initialization of the class.

Load Single Transformation Table

TransTables.loadsingle(rocktype, variable, data)

From somewhere else, append the data for the variable and rocktype to the correct structure in this file. Assumes the class is initialized so that things can be added similar to the method in self.loadfolder()

Parameters:
  • rocktype (int) – integer value for the rocktype
  • variable (str) – variable for this transformation data
  • data (Type to Change??) – the Data used in transformation.. figure the type will change based on what transformation is being applied?

Load Transformation Tables from Folder

TransTables.loadfolder(folder, variables=None, rocktypes=None, prefix='nscore', ext='trn')

Load transformation table files from a folder. Currently, only the convention associated with the GSLIB program backtr version 4.1 is implemented which is as follows:

  1. prefix_<variable number>_<RT category code>.trn
  2. prefix<variable number>.trn
  3. prefix_<RT category code>.trn
Parameters:
  • folder (str) – Folder containing the transformation tables
  • variables (list) – List of the variables associated with the codes found in the filename, in order
  • rocktypes (list) – List of the rock types associated with the codes found in the filename, in order
  • prefix (str) – The prefix of the files to load (e.g., ‘nscore’ or ‘unscore’)

Code author: Warren E. Black - 2016-04-05

Load Transformation Table from File

TransTables.loadfile(file, variable=None, rocktype=None)

Load an individual transformation file into the class (i.e., self.transtabl)

If the parameter variable is not passed, the first value found in self.variables will be used to store the table. If the parameter rocktype is not passed, the first value found in self.variables will be used to store the table.

Parameters:
  • file (str) – Location of the file to load
  • variable (str) – Variable name associated with the table to load
  • rocktype (str) – Rocktype ID associated with the table to load

Code author: Warren E. Black - 2016-04-05

Export TransTable Class

TransTables.export(file)

Export the class and all of its contents to a pickle file. This will allow the class and its structure to be easily reloaded at a later time.

Parameters:file (str) – Location of the file to export the class into

Code author: Warren E. Black - 2016-04-05

Load TransTable Class

TransTables.load(file)

Load a previously exported gs.transtabless class. All data stored within the class currently will be overwritten.

Parameters:file (str) – Location of the file to import class data from

Examples

A simple call loading a previously build and exported gs.TransTables class.

>>> transtables = gs.TransTables()
>>> transtables.load(file)

Code author: Warren E. Black - 2016-04-05

Rotations

rotations.py: Contains utilities for GSLIB rotations

Get GSLIB Rotation Matrix

pygeostat.transformations.rotations.gsrotmat(azm, dip, tilt)

Returns the GSLIB rotation matrix given GSLIB angles for azimuth, dip and tilt

Get GSLIB Principal Vectors

pygeostat.transformations.rotations.principalvectors(azm, dip, tilt, lefthandrule=True, majorlen=1.0, minorlen=1.0, vertlen=1.0)

Returns the GSLIB principal vectors: NOTE: we typically think of things/visualize using the left hand rule in which case the original axes are: major_axis, minor_axis, vert_axis = [1, 0, 0], [0, -1, 0], [0, 0, 1] although the GSLIB rotation matrix actually defines them as: major_axis, minor_axis, vert_axis = [1, 0, 0], [0, 1, 0], [0, 0, 1] This is only an issue in practice for an asymmetric minor axis

Direction to Point from Origin

pygeostat.transformations.rotations.azmdip(point_vector)

Calculates the azimuth/dip to a point (length 3 vector [x,y,z]) from the origin

Calculate Principal Directions

pygeostat.transformations.rotations.principaldirs(azm, dip, tilt)

Calculate the principal directions from a plane of major continuity

3-D Arrow Class

class pygeostat.transformations.rotations.Arrow3D(xs, ys, zs, *args, **kwargs)

Arrow3D from http://stackoverflow.com/questions/11140163/python-matplotlib-plotting-a-3d-cube-a-sphere-and-a-vector

Draw 3-D Arrow

Arrow3D.draw(renderer)

Draw the Patch to the given renderer.

Draw Principal Directions Arrows

pygeostat.transformations.rotations.plotprincipalvectors(axes, azm, dip, tilt, lefthandrule=True, majorlen=1.0, minorlen=1.0, vertlen=1.0)

Add principal direction unit arrows to plot

Interactive GSLIB Vector Widget

pygeostat.transformations.rotations.drawgsvectorwidget(legend=True, sidetext=True, majortitle=False)

Draws an interactive GSLIB vector widget in an IPython notebook.

Warning

Must be run with either:

%matplotlib inline or %matplotlib notebook activated

Code author: Jared Deutsch 2016-02-21

Draw Orientable Ellipsoid

pygeostat.transformations.rotations.drawellipsoid(ax, hmax, hmin, vert, azm=0.0, dip=0.0, tilt=0.0, color='#87CEFA', alpha=0.5)

Draws an orientatable ellipsoid with GSLIB conventions.

Spherical coordinate calculation from:

Rotation uses GSLIB rotation matrix definition

Code author: Jared Deutsch 2016-03-06

Interactive Anisotropy Widget

pygeostat.transformations.rotations.drawgsaniswidget(legend=True, sidetext=True, majortitle=False)

Draws an interactive anisotropy widget in an IPython notebook.

Warning

Must be run with either:

%matplotlib inline or %matplotlib notebook activated

Code author: Jared Deutsch 2016-03-06