generativepy.analytics module

By Martin McBride, 2023-04-27
Tags: formula tex latex
Categories: generativepy generative art


The analytics module provides functions for analysing the distribution of values in a NumPy array.

This is particularly useful when exploring generative art techniques such as fractal imaging. Often the result of the calculation will be a large array of numerical values.

In order to create an image we usually need to convert the values into colours. To do this, it is useful to know how the values are distributed.

For example, if we run an imaging process and the output array contains values in the range 0 to 100, we could create a color map with 101 colours to map those values onto a colour scheme.

But if we discovered that the output array contained values in the range 0 to 1000, we would need a different colour map. And if the range was 2000 to 2500, we would probably want to subtract 2000 from each value to use a colour mapo in the range 0 to 500.

These functions will work on any NumPy array that has a numeric data type (which most arrays do). They are designed to work with single channel image data such as greyscale data, or just the output from a fractal algorithm. That would be an array with shape (height, width).

They can be used with multichannel data, such as RGB data, an array with shape (height, width, 3). However in that case it would produce one value for all the R, G amd B values combined.

print_stats

Prints the statistics for a NumPy array

print_stats(array, title='stats')
Parameter Type Description
array NumPy Image data
title string Title to display (defaults to stats)

This function takes a NumPy array, and calculates the minium, maximum, mean and median values.

It prints the result to the console.

title is printed before the stats. It can be useful if you are printing more than one set of statistics in a run.

print_histogram

Prints the histogram for a NumPy array

print_histogram(array, title='histogram', bins=10)
Parameter Type Description
array NumPy Image data
title string Title to display (defaults to histogram)

This function takes a NumPy array, and calculates a histogram of the values.

It prints the result to the console.

title is printed before the stats. It can be useful if you are printing more than one set of statistics in a run.

bins controls the number of bins in the histogram.

See also

If you found this article useful, you might be interested in the book NumPy Recipes or other books by the same author.

Join the PythonInformer Newsletter

Sign up using this form to receive an email when new content is added:

Popular tags

2d arrays abstract data type alignment and angle animation arc array arrays bar chart bar style behavioural pattern bezier curve built-in function callable object chain circle classes clipping close closure cmyk colour combinations comparison operator comprehension context context manager conversion count creational pattern data science data types decorator design pattern device space dictionary drawing duck typing efficiency ellipse else encryption enumerate fill filter font font style for loop formula function function composition function plot functools game development generativepy tutorial generator geometry gif global variable gradient greyscale higher order function hsl html image image processing imagesurface immutable object in operator index inner function input installing iter iterable iterator itertools join l system lambda function latex len lerp line line plot line style linear gradient linspace list list comprehension logical operator lru_cache magic method mandelbrot mandelbrot set map marker style matplotlib monad mutability named parameter numeric python numpy object open operator optimisation optional parameter or pandas partial application path pattern permutations pie chart pil pillow polygon pong positional parameter print product programming paradigms programming techniques pure function python standard library radial gradient range recipes rectangle recursion reduce regular polygon repeat rgb rotation roundrect scaling scatter plot scipy sector segment sequence setup shape singleton slice slicing sound spirograph sprite square str stream string stroke structural pattern subpath symmetric encryption template tex text text metrics tinkerbell fractal transform translation transparency triangle truthy value tuple turtle unpacking user space vectorisation webserver website while loop zip zip_longest