def signatures(with_retval=True): """Generate signatures. Returns ------- Hypothesis strategy Strategy to generate signatures. """ return (builds( lambda r, x: Signature(r, **x), r=retvals(), x=dictionaries(keys=identifiers(), values=params()), ) if with_retval else builds( lambda x: Signature(**x), x=dictionaries(keys=identifiers(), values=params()), ))
from .converters import Column, Columns, to_dataframe, init_cols from autosig import Signature, param from functools import partial recipe = Signature( data=param( default=None, converter=to_dataframe, position=0, docstring="""`altair.Data` or `pandas.DataFrame` or csv or json file URL The data from which the statistical graphics is being generated""", ), height=param( default=600, converter=int, position=-2, docstring="""`int` The height of the chart""", ), width=param( default=800, converter=int, position=-1, docstring="""`int` The width of the chart""", ), ).set_late_init(init_cols) column = partial(param, converter=Column) univariate_recipe = recipe + Signature(column=column(
"""Autocorrelation plot.""" from .signatures import univariate_recipe import altair as alt from autosig import autosig, Signature, param import numpy as np import pandas as pd @autosig(univariate_recipe + Signature(max_lag=param( default=None, position=2, docstring="""int Maximum lag to show in the plot, defaults to number of rows in data""", ))) def autocorrelation(data, column=0, max_lag=None, height=300, width=400): """Generate an autocorrelation plot.""" max_lag = data.shape[0] - 1 if max_lag is None else int(max_lag) lags = np.arange(0, max_lag + 1) _data = pd.DataFrame( dict(Lag=lags, Autocorrelation=[data[column].autocorr(lag=lag) for lag in lags])) return (alt.Chart(_data, height=height, width=width).mark_bar().encode( x="Lag:O", y="Autocorrelation" + ":Q"))
"""Smoother graph.""" from .signatures import bivariate_recipe import altair as alt from autosig import autosig, param, Signature @autosig(bivariate_recipe + Signature( window=param( default=None, position=3, docstring="""int The size of the smoothing window""", ), interquartile_area=param( default=True, position=4, docstring="""interquartile_area: bool Whether to plot the IRQ as an area""", ), )) def smoother(data, x=0, y=1, window=None, interquartile_area=True, height=300, width=400): """Generate a smooth line plot with optional IRQ shading area.""" window = data.shape[0] // 4 if window is None else int(window) _data = data.sort_values(by=x) _data["x"] = _data["x"].rolling(window).median()
"""Generate stripplots.""" from .common import multivariate_preprocess from .signatures import multivariate_recipe, opacity, color import altair as alt from autosig import autosig, Signature @autosig(multivariate_recipe + Signature(color=color(default=None, position=3), opacity=opacity(position=4))) def stripplot(data=None, columns=None, group_by=None, color=None, opacity=1, height=600, width=800): """Generate a stripplot.""" data, key, value = multivariate_preprocess(data, columns, group_by) enc_args = dict() if color is not None: enc_args["color"] = color return (alt.Chart(data, height=height, width=width).mark_tick(opacity=opacity, thickness=2).encode(x=key + ":N", y=value, **enc_args))
from functools import partial, wraps from loguru import logger from os import fork, wait, _exit, getpid from sys import stderr, settrace, setprofile, gettrace, getprofile try: logger.remove(0) except ValueError: logger.warning("can't clean up logger handlers", logger) pass logger.add(stderr, level="DEBUG") top_level = True checkpoint_sig = Signature(retries=param( default=1, converter=int, docstring="""int Max # of times checkpoint can be reused""", )) times = partial( param, converter=int, validator=int, docstring="Number of checkpoints to rewind back to, up to 254.", ) rewind_sig = Signature(times=times(default=1)) checkpoint_code_to_name = bidict() @logger.catch
"""Scatterplots.""" from .common import choose_kwargs, hue_scale_dark, hue_scale_light from .signatures import bivariate_recipe, multivariate_recipe, color, tooltip import altair as alt from autosig import autosig, Signature, param from numbers import Number scatterplot_sig = Signature( color=color(default=None, position=3), opacity=param( default=1, position=4, converter=float, docstring="""`float` A constant value for the opacity of the mark""", ), tooltip=tooltip(default=None, position=5), ) @autosig(bivariate_recipe + scatterplot_sig) def scatterplot(data=None, x=0, y=1, color=None, opacity=1, tooltip=None, height=600, width=800): """Generate a scatterplot.""" if color is not None:
"""Boxplot implementation.""" from .common import multivariate_preprocess from .signatures import multivariate_recipe, use_color import altair as alt from autosig import autosig, Signature @autosig(multivariate_recipe + Signature(color=use_color(position=3))) def boxplot(data=None, columns=None, group_by=None, color=False, height=600, width=800): """Generate a boxplot.""" data, key, value = multivariate_preprocess(data, columns, group_by) # long form assumed from here chart = alt.Chart(height=height, width=width) chart_bar = chart.mark_bar(stroke="black", fill="#4682b4") chart_tick = chart.mark_tick(color="black") x = key + ":N" stats = { stat: stat + "(" + value + ")" for stat in ["min", "max", "median", "q1", "q3"] } ticks = { tick: chart_tick.encode(x=x, y=stats[tick]) for tick in ["min", "max"] } encode_args = dict(x=x)
""" auto = None # no, must not pass anything true = True false = False normalize = "normalize" @autosig( bivariate_recipe + Signature( color=color(default=None, position=3), stack=param( default=StackType.auto, position=4, converter=StackType, docstring="""StackType One of `StackType.auto` (automatic selection), `StackType.true` (force), `StackType.false` (no stacking) and `StackType.normalize` (for normalized stacked)""", ), ) ) def areaplot( data=None, x=0, y=1, color=None, stack=StackType.auto, height=600, width=800 ): """Generate an areaplot.""" warn_not_distinct(data, x, color) if stack is not StackType.auto: y = alt.Y(y, stack=stack.value) opt_args = choose_kwargs(locals(), ["color"]) return ( alt.Chart(data=data, height=height, width=width)
from .common import to_dataframe, to_column, to_columns from autosig import Signature, param from functools import partial recipe = Signature( data=param( converter=to_dataframe, position=0, docstring="""`altair.Data` or `pandas.DataFrame` or csv or json file URL The data from which the statistical graphics is being generated""", ), height=param( default=300, converter=int, position=-2, docstring="""`int` The height of the chart""", ), width=param( default=400, converter=int, position=-1, docstring="""`int` The height of the chart""", ), ) column = partial(param, validator=to_column) univariate_recipe = recipe + Signature(column=column( default=0,
"""Scatter plots.""" from .signatures import bivariate_recipe, multivariate_recipe, color, tooltip import altair as alt from autosig import autosig, Signature from .common import choose_kwargs scatter_sig = Signature(color=color(default=None, position=3), tooltip=tooltip(default=None, position=4)) @autosig(bivariate_recipe + scatter_sig) def scatter(data, x=0, y=1, color=None, tooltip=None, height=300, width=400): """Generate a scatter plot.""" kwargs = choose_kwargs(from_=locals(), which=["color", "tooltip"]) return (alt.Chart(data, height=height, width=width).mark_point().encode(x=x, y=y, **kwargs)) @autosig(multivariate_recipe + scatter_sig) def multiscatter(data, columns=None, group_by=None, color=None, tooltip=None, height=300, width=400): """Generate many scatter plots. Based on several columns, pairwise. """ kwargs = choose_kwargs(from_=locals(), which=["color", "tooltip"])
"""Lineplots.""" from .common import warn_not_distinct, choose_kwargs from .signatures import bivariate_recipe, color import altair as alt from autosig import autosig, Signature @autosig(bivariate_recipe + Signature(color=color(default=None, position=3))) def lineplot(data=None, x=0, y=1, color=None, height=600, width=800): """Generate a lineplot.""" warn_not_distinct(data, x, color) opt_args = choose_kwargs(locals(), ["color"]) return ( alt.Chart(data=data, height=height, width=width) .encode(x=x, y=y, **opt_args) .mark_line() )
def maxbins(data): """Return a pair of ints with reasonable defaults for binning in heatmap.""" n = sqrt(data.shape[0]) return data, n * 4 // 3, n * 3 // 4 @autosig(bivariate_recipe + Signature( color=color(default=2, position=3), opacity=column( default=None, position=4, docstring="""`str` The column containing the data that determines opacity of the mark""", ), aggregate=param( default="average", converter=str, position=5, docstring="""`str` The aggregation function to set the color of each mark, see https://altair-viz.github.io/user_guide/encoding.html#encoding-aggregates for available options""", ), )) def heatmap( data=None, x=0, y=1, color=2, opacity=None, aggregate="average", height=600,