Skip to content

Commit

Permalink
Add docstrings for chart utils
Browse files Browse the repository at this point in the history
  • Loading branch information
chanhosuh committed Jul 11, 2023
1 parent a088fc1 commit a471930
Show file tree
Hide file tree
Showing 6 changed files with 657 additions and 11 deletions.
103 changes: 102 additions & 1 deletion curvesim/plot/altair/chart_properties.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,45 @@
"""
This module provides functionality for creating and manipulating chart properties.
It contains utility functions for creating keyword arguments for a chart, updating
and initializing properties, and handling property-specific behaviors. These
functions support nested properties and allow for different behaviors based on
property keys.
The key functions in this module are:
- make_chart_kwargs: Create the keyword arguments for a chart by updating a default
dictionary with values from override dictionaries.
- update_properties: Update properties in a dictionary, supporting nested properties.
- concat_properties: Concatenate values to a property in a dictionary.
- ignore_property: Ignore a property during an update operation.
- init_properties: Initialize the properties of a dictionary using a mapping of
classes.
"""
from copy import deepcopy
from functools import partial

from altair import Color, MarkDef, X, Y


def make_chart_kwargs(default, override_dicts):
"""
Create the keyword arguments for a chart by updating a default dictionary with
values from a list of override dictionaries.
Parameters
----------
default : dict
The default keyword arguments for the chart.
override_dicts : list
A list of dictionaries containing overrides for the default
keyword arguments.
Returns
-------
dict
A dictionary containing the keyword arguments for the chart.
"""
chart_kwargs = deepcopy(default)

for d in override_dicts:
Expand All @@ -16,6 +51,25 @@ def make_chart_kwargs(default, override_dicts):


def update_properties(prop_dict, key, val, depth=0, count=0):
"""
Update properties in a dictionary. If the current depth matches the target depth
(or the count), the value is updated directly. Otherwise, the function is called
recursively on nested dictionaries.
Parameters
----------
prop_dict : dict
The dictionary of properties to update.
key : str
The key of the property to update.
val : any
The new value for the property.
depth : int, optional
The target depth for the update. Defaults to 0.
count : int, optional
The current depth of the update. Defaults to 0.
"""

if depth in [0, count]:
prop_dict[key] = val
else:
Expand All @@ -25,6 +79,19 @@ def update_properties(prop_dict, key, val, depth=0, count=0):


def concat_properties(prop_dict, key, val):
"""
Concatenate values to a property in a dictionary. If the value is a list, extend
the property with it. Otherwise, append the value to the property.
Parameters
----------
prop_dict : dict
The dictionary of properties to update.
key : str
The key of the property to update.
val : any
The value to concatenate to the property.
"""
prop_dict.setdefault(key, [])

if isinstance(val, list):
Expand All @@ -34,10 +101,44 @@ def concat_properties(prop_dict, key, val):


def ignore_property(prop_dict, key, val): # pylint: disable=unused-argument
pass
"""
Ignore a property during an update operation. This function does nothing and is
intended to be used as a placeholder in UPDATE_RULES where certain properties
should be ignored.
Parameters
----------
prop_dict : dict
The dictionary of properties to update.
key : str
The key of the property to ignore.
val : any
The value of the property to ignore.
"""


def init_properties(prop_dict, classes=None):
"""
Initialize the properties of a dictionary using a mapping of classes. If a
property's key is present in the classes dictionary, its value is replaced
with an instance of the corresponding class, initialized with the value as
keyword arguments.
Parameters
----------
prop_dict : dict
The dictionary of properties to initialize.
classes : dict, optional
A dictionary mapping keys to classes. If a key in prop_dict matches a
key in classes, the value in prop_dict is replaced with an instance of
the corresponding class. Defaults to PROPERTY_CLASSES.
Returns
-------
dict
The dictionary of properties, with values replaced with class instances
where applicable.
"""
classes = classes or PROPERTY_CLASSES
for key, val in prop_dict.items():
try:
Expand Down
69 changes: 67 additions & 2 deletions curvesim/plot/altair/make_chart.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,55 @@
from altair import Scale, Chart, data_transformers
"""
This module provides functionality for creating Altair charts with custom styles.
It contains utility functions for creating charts and default chart properties. It
uses styles defined in the .styles module and allows custom configuration of chart
properties.
The key functions in this module are:
- make_chart: Create an interactive chart with custom properties and styles.
- make_defaults: Create default properties for a chart.
The data_transformers.disable_max_rows() call at the top of the module disables the
maximum row limit for Altair charts.
"""
from altair import Chart, Scale, data_transformers

from curvesim.exceptions import PlotError

from .chart_properties import make_chart_kwargs
from .styles import STYLES


data_transformers.disable_max_rows()


def make_chart(config, x=None, y=None, color=None, **kwargs):
"""
Create an interactive chart with custom properties and styles.
Parameters
----------
config : dict
The configuration dictionary for the chart. Must contain a 'style' key.
x : str or dict, optional
The x-axis property. Can be a shorthand string or a dictionary of alt.X kwargs.
y : str or dict, optional
The y-axis property. Can be a shorthand string or a dictionary of alt.Y kwargs.
color : str or dict, optional
The color property. Can be a shorthand string or a dictionary of alt.Color kwargs.
**kwargs
Additional keyword arguments are added to the chart configuration.
Returns
-------
altair.Chart
The created chart.
Raises
------
PlotError
If no style is found in the config or if the style is not found in STYLES.
"""
title = config.get("title", "")

try:
Expand All @@ -27,6 +68,30 @@ def make_chart(config, x=None, y=None, color=None, **kwargs):


def make_defaults(title, x, y, color):
"""
Create default properties for a chart.
Parameters
----------
title : str
The title of the chart.
x : str or dict, optional
The x-axis property. Can be a shorthand string or a dictionary of alt.X kwargs.
y : str or dict, optional
The y-axis property. Can be a shorthand string or a dictionary of alt.Y kwargs.
color : str or dict, optional
The color property. Can be a shorthand string or a dictionary of alt.Color kwargs.
Returns
-------
dict
The default properties for a chart.
Raises
------
PlotError
If x, y, or color is not a string (shorthand) or a dictionary (altair kwargs).
"""
defaults = {
"title": title,
"encoding": {
Expand Down
Loading

0 comments on commit a471930

Please sign in to comment.