pymtg documentation

Installation

You can install pymtg using pip:

pip install git+https://github.com/MTG/pymtg

pymtg.io

pymtg.io.get_filenames_in_dir(dir_name, keyword='*', skip_foldername='', match_case=True, verbose=False)[source]

TODO: better document this function TODO: does a python 3 version of this function exist?

Parameters:
  • dir_name (str) – The foldername.
  • keyword (str) – The keyword to search (defaults to ‘*’).
  • skip_foldername (str) – An optional foldername to skip searching
  • match_case (bool) – Flag for case matching
  • verbose (bool) – Verbosity flag
Returns:

Tuple containing:
  • fullnames (list): List of the fullpaths of the files found
  • folder (list): List of the folders of the files
  • names (list): List of the filenames without the foldername

Return type:

(tuple)

Examples

>>> get_filenames_in_dir('/path/to/dir/', '*.mp3')  #doctest: +SKIP
(['/path/to/dir/file1.mp3', '/path/to/dir/folder1/file2.mp3'], ['/path/to/dir/', '/path/to/dir/folder1'], ['file1.mp3', 'file2.mp3'])
pymtg.io.json_dump(path, data, indent=4, verbose=False)[source]

Save python dictionary data to JSON file at path.

Parameters:
  • path (str) – Path to the file
  • verbose (bool) – Verbosity flag
pymtg.io.json_load(path, verbose=False)[source]

Load python dictionary stored in JSON file at path.

Parameters:
  • path (str) – Path to the file
  • verbose (bool) – Verbosity flag
Returns:

Loaded JSON contents

Return type:

(dict)

pymtg.io.mkdir_p(path)[source]

TODO: document this function

pymtg.io.save_to_file(path, data, verbose=False)[source]

Save arbitrary data to file at path.

Parameters:
  • path (str) – Path to the file
  • verbose (bool) – Verbosity flag

pymtg.iterables

pymtg.iterables.chunks(l, n)[source]

Yield successive n-sized chunks from l.

Examples

>>> chunks([1, 2, 3, 4, 5], 2) #doctest: +ELLIPSIS
<generator object chunks at 0x...>
>>> list(chunks([1, 2, 3, 4, 5], 2))
[[1, 2], [3, 4], [5]]

pymtg.plotting

pymtg.plotting.color_at_index(index)[source]

Return hexadecimal color at given index from COLORS.

Parameters:index (int) – Index of color to return (wraps if larger than the length of COLORS)
Returns:Hexadecimal color code (starts with #)
Return type:(str)

pymtg.processing

class pymtg.processing.WorkParallelizer(use_ipywidgets=False, use_threads=False)[source]

Utility class to parallelize tasks using either Threads or Processes.

This utility class is based on concurrent.futures. See https://docs.python.org/3/library/concurrent.futures.html.

# Inititalize the work parallelizer
wp = WorkParallelizer()

# Add tasks to the work parallelizer
for i in range(30):
    wp.add_task(my_function, i, i + 1, kwarg1='one', kwarg2='two')

# Start running the tasks with the specified number of workers
# This function will show periodic updates of the status of the tasks and block the main thread
# Check WorkParallelizer.start() to get tasks running in the background.
wp.run(num_workers=4)

# Show errors after computing all tasks (if any)
if wp.num_tasks_failed > 0:
    wp.show_errors()

pymtg.signal

pymtg.signal.linear_approximation(x, include_coeffs=False)[source]

Compute the first degree least squares polynomial fit of x (linear approximation).

This function returns the linear approximation as a signal of the same length of x. If requested, the function can also return the linear approximation coefficients as returned by Numpy’s ‘polyfit’ function. For more details in the method used for the linear approximation, see https://docs.scipy.org/doc/numpy/reference/generated/numpy.polyfit.html.

Parameters:
  • x (array) – The input signal
  • include_coeffs (bool) – Whether to return the computed linear approximation coefficients along with the approximated signal (default=False).
Returns:

The linear approximation of then input signal

Return type:

(array)

Examples

>>> linear_approximation([1, 1, 1])
array([ 1.,  1.,  1.])
>>> linear_approximation([0, 1, 2, 3, 4, 5])
array([ 0.,  1.,  2.,  3.,  4.,  5.])
>>> linear_approximation([1, 2, 4, 8, 16])
array([ -1. ,   2.6,   6.2,   9.8,  13.4])
>>> linear_approximation([1, 2, 4, 8, 16], include_coeffs=True)
(array([ -1. ,   2.6,   6.2,   9.8,  13.4]), (3.6000000000000001, -0.99999999999999778))
pymtg.signal.smooth(x, window_len=11, window='hanning', preserve_length=True)[source]

Smooth the data using a window with requested size.

This method is based on the convolution of a scaled window with the signal. The signal is prepared by introducing reflected copies of the signal (with the window size) in both ends so that transient parts are minimized in the beginning and end part of the output signal.

The code here is an adaptation of the smoothing code from Scipy Cookbook: http://scipy-cookbook.readthedocs.io/items/SignalSmooth.html

Parameters:
  • x (array) – The input signal
  • window_len (int) – The dimension of the smoothing window. Should be an odd integer.
  • window (string) – The type of window from ‘flat’, ‘hanning’, ‘hamming’, ‘bartlett’, ‘blackman’. Flat window will produce a moving average smoothing.
  • preserve_length (bool) – Whether the length oh the output signal should be the same as the length of the input signal (default=True).
Returns:

The smoothed signal

Return type:

(array)

Examples

>>> smooth([0, 1, 0, 1, 0, 1], 4)
array([ 0.5,  0.5,  0.5,  0.5,  0.5,  0.5])

pymtg.time

pymtg.time.datetime_range(start_datetime, end_datetime=None, step_interval=None, n_steps=1, snap_to_date=False, return_pairs=False)[source]

Return a list of dates inside the date range between start_datetime and end_datetime, equally spaced in step time intervals.

Parameters:
  • start_datetime (datetime) – Starting time of the range
  • end_datetime (datetime) – End of the time range (included if range is multiple of step). Defaults to today
  • step_interval (timedelta,str) – time interval of between list elements. Can be a datetime.timedelta object or a string from [‘day’, ‘second’, ‘microsecond’, ‘millisecond’, ‘minute’, ‘hour’, ‘week’]. Defaults to 1 day.
  • n_steps (int) – number of steps to be applied between list elements (default=1)
  • snap_to_date (bool) – Whether to disregard hour, minutes and seconds information (as a date object, default=False)
  • return_pairs (bool) – Whether to return a simple list or a list of pairs with edge dates for each interval (default=False)
Returns:

List of datetime.datetime objects (or tuples of two datetime.datetime if return_pairs=True)

Return type:

(list)

Examples

>>> datetime_range(datetime.datetime(2017,1,1), datetime.datetime(2017,1,3))
[datetime.datetime(2017, 1, 1, 0, 0), datetime.datetime(2017, 1, 2, 0, 0), datetime.datetime(2017, 1, 3, 0, 0)]
>>> datetime_range(datetime.datetime(2017,1,1,10,21,45), datetime.datetime(2017,1,3,10,30,54), snap_to_date=True)
[datetime.datetime(2017, 1, 1, 0, 0), datetime.datetime(2017, 1, 2, 0, 0), datetime.datetime(2017, 1, 3, 0, 0)]
>>> datetime_range(datetime.datetime(2017,1,1,11,0,0), datetime.datetime(2017,1,1,11,2,0), step_interval='minute')
[datetime.datetime(2017, 1, 1, 11, 0), datetime.datetime(2017, 1, 1, 11, 1), datetime.datetime(2017, 1, 1, 11, 2)]
>>> datetime_range(datetime.datetime(2017,1,1,11,0,0), datetime.datetime(2017,1,1,11,20,0), step_interval='minute', n_steps=10)
[datetime.datetime(2017, 1, 1, 11, 0), datetime.datetime(2017, 1, 1, 11, 10), datetime.datetime(2017, 1, 1, 11, 20)]
>>> datetime_range(datetime.datetime(2017,1,1), datetime.datetime(2017,1,3), return_pairs=True)
[(datetime.datetime(2017, 1, 1, 0, 0), datetime.datetime(2017, 1, 2, 0, 0)), (datetime.datetime(2017, 1, 2, 0, 0), datetime.datetime(2017, 1, 3, 0, 0))]
pymtg.time.time_stats(done, total, starttime)[source]

Count how far through a repeated operation you are.

Use this method if you are performing a repeated operation over a list of items and you want to check progress and time remaining after each iteration.

Parameters:
  • done (int) – how many items have been processed
  • total (int) – the total number of items that are to be processed
  • starttime – the result of an initial call to time.monotonic()
Returns:

A tuple of (time elapsed, time remaining), as a string representation of a timedelta