striplog.utils module#

Helper functions for the striplog package.

class striplog.utils.CustomFormatter#

Bases: string.Formatter

Extends the Python string formatter to some new functions.

convert_field(value, conversion)#

Define some extra field conversion functions.

get_field(field_name, args, kwargs)#

Return an underscore if the attribute is absent. Not all components have the same attributes.

striplog.utils.add_subplot_axes(ax, rect, axisbg='w')#

Facilitates the addition of a small subplot within another plot. From: http://stackoverflow.com/questions/17458580/ embedding-small-plots-inside-subplots-in-matplotlib License: CC-BY-SA

Parameters
  • ax (axis) – A matplotlib axis.

  • rect (list) – A rect specifying [left pos, bot pos, width, height]

Returns

The sub-axis in the specified position.

Return type

axis

striplog.utils.are_close(x, y)#
striplog.utils.axis_transform(ax, x, y, xlim=None, ylim=None, inverse=False)#

http://stackoverflow.com/questions/29107800

inverse = FalseAxis => Data

= True : Data => Axis

striplog.utils.binary_closing(arr, p)#

1D binary morphology: closing.

Parameters
  • arr – A binary array (e.g. [1,0,0,1,0]).

  • p – Structuring element length.

Returns

ndarray

striplog.utils.binary_dilation(arr, p)#

1D binary morphology: dilation.

TODO

None of these operators work with ‘greyscale’ (i.e. non-binary) data. And none of them work with non-boxcar structure elements. They could be easily re-written to be much more general. (They are only here to avoid dependency on SciPy, which probably is not that big a deal, so could just revert to scipy.morphology.)

Parameters
  • arr – A binary array (e.g. [1,0,0,1,0]).

  • p – Structuring element length.

Returns

ndarray

striplog.utils.binary_erosion(arr, p)#

1D binary morphology: erosion.

Parameters
  • arr – A binary array (e.g. [1,0,0,1,0]).

  • p – Structuring element length.

Returns

ndarray

striplog.utils.binary_opening(arr, p)#

1D binary morphology: opening.

Parameters
  • arr – A binary array (e.g. [1,0,0,1,0]).

  • p – Structuring element length.

Returns

ndarray

striplog.utils.dict_repr_html(dictionary)#

Jupyter Notebook magic repr function.

striplog.utils.flatten_list(l)#

Unpacks lists in a list:

[1, 2, [3, 4], [5, [6, 7]]]

becomes

[1, 2, 3, 4, 5, 6, 7]

http://stackoverflow.com/a/12472564/3381305

striplog.utils.geology_from_macrostrat(lng, lat, buffer_size=0.2)#

Request data from MacroStrat within buffer_size of a given lng, lat pair. We can do this by creating a WKT polygon, which is simply a square with

each side a buffer_size distance from the given lng, lat.

Parameters
  • lng (float) – longitude in decimal degrees.

  • lat (float) – latitude in decimal degrees.

  • buffer_size (float) – distance in decimal degrees to add to lng and lat to request geology in.

Returns

polygon_request (requests.models.Response)

striplog.utils.get_liths_from_macrostrat(s)#

Parse a Macrostrat ‘lith’ string into lithologies.

striplog.utils.hex_is_dark(hexx, percent=50)#

Function to decide if a hex colour is dark.

Parameters

hexx (str) – A hexadecimal colour, starting with ‘#’.

Returns

The colour’s brightness is less than the given percent.

Return type

bool

striplog.utils.hex_to_name(hexx)#

Convert hex to a color name, using matplotlib’s colour names.

Parameters

hexx (str) – A hexadecimal colour, starting with ‘#’.

Returns

The name of the colour, or None if not found.

Return type

str

striplog.utils.hex_to_rgb(hexx)#

Utility function to convert hex to (r,g,b) triples. http://ageo.co/1CFxXpO

Parameters

hexx (str) – A hexadecimal colour, starting with ‘#’.

Returns

The equivalent RGB triple, in the range 0 to 255.

Return type

tuple

striplog.utils.hollow_matrix(M)#

Return hollow matrix (zeros on diagonal).

Args

M (ndarray): a ‘square’ ndarray.

Returns

ndarray. The same array with zeros on the diagonal.

striplog.utils.identity(x)#

Null function. Used for default in functions that can apply a user- supplied function to data before returning.

striplog.utils.inspect_petrel(filename)#
striplog.utils.list_and_add(a, b)#

Coerce to lists and concatenate.

Parameters
  • a – A thing.

  • b – A thing.

Returns

List. All the things.

striplog.utils.loglike_from_image(filename, offset)#

Get a log-like stream of RGB values from an image.

Parameters
  • filename (str) – The filename of a PNG image.

  • offset (Number) – If < 1, interpreted as proportion of way across the image. If > 1, interpreted as pixels from left.

Returns

A 2d array (a column of RGB triples) at the specified offset.

Return type

ndarray

striplog.utils.name_to_hex(name)#

Convert a color name to hex, using matplotlib’s colour names.

Parameters

name (str) – The name of a colour, e.g. ‘red’.

Returns

The hex code for the colour.

Return type

str

striplog.utils.null(x)#

Null function. Used for default in functions that can apply a user- supplied function to data before returning.

striplog.utils.null_default(x)#

Null function. Used for default in functions that can apply a user- supplied function to data before returning.

striplog.utils.observations(seq_of_seqs, states, step=1, include_self=False)#

Compute observation matrix.

class striplog.utils.partialmethod#

Bases: functools.partial

Extends partial to work on class methods.

striplog.utils.read_petrel(filename, function=None, remap=None)#

Read a Petrel well tops ASCII export file.

Parameters
  • filename (str) – The name of the ASCII file.

  • function (function or dict) – A function to apply to every field, or a mapping of field name to function. The result of the function will be used as the value for that field.

  • remap (dict) – A mapping of field name to new field name. The field name will be replaced.

Returns

A mapping of field names to lists of data. You can give this

straight to pandas.

Return type

dict

striplog.utils.rgb_is_dark(rgb, percent=50)#

Helpful wrapper for hex_is_dark.

striplog.utils.rgb_to_hex(rgb)#

Utility function to convert (r,g,b) triples to hex. http://ageo.co/1CFxXpO

Parameters

rgb (tuple) – A sequence of RGB values in the range 0-255 or 0-1.

Returns

The hex code for the colour.

Return type

str

striplog.utils.skip(x)#

Always returns None.

striplog.utils.text_colour_for_hex(hexx, percent=50, dark='#000000', light='#ffffff')#

Function to decide what colour to use for a given hex colour.

Parameters

hexx (str) – A hexadecimal colour, starting with ‘#’.

Returns

The colour’s brightness is less than the given percent.

Return type

bool

striplog.utils.tops_from_loglike(a, offset=0, null=None)#

Take a log-like stream of numbers or strings, and return two arrays: one of the tops (changes), and one of the values from the stream.

Parameters
  • loglike (array-like) – The input stream of loglike data.

  • offset (int) – Offset (down) from top at which to get lithology, to be sure of getting ‘clean’ pixels.

Returns

Two arrays, tops and values.

Return type

ndarray