Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add 'levels' parameter to as_discrete() #957

Merged
merged 21 commits into from
Dec 8, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
4a7f510
`as_discrete(levels)` generates 'data_meta/series_annotation'.
OLarionova-HORIS Nov 17, 2023
d8a3b44
Fix creating of 'factorLevelsByVar'-map .
OLarionova-HORIS Nov 17, 2023
89f09b2
Fix `applyTransform` to avoid error 'value is not in the domain'.
OLarionova-HORIS Nov 17, 2023
11452cf
Add examples.
OLarionova-HORIS Nov 17, 2023
4c36cad
Add tests.
OLarionova-HORIS Nov 22, 2023
4c74e31
Correct specified 'factor_levels' according to actual data and append…
OLarionova-HORIS Nov 23, 2023
d40eb00
Update test notebook.
OLarionova-HORIS Nov 23, 2023
a747f90
Fix description of 'levels' parameter.
OLarionova-HORIS Nov 24, 2023
122f46b
Minor.
OLarionova-HORIS Nov 24, 2023
d1cd7bf
Make variables specified in 'series_annotations' with levels discrete.
OLarionova-HORIS Nov 28, 2023
b53782b
Skip creation of 'mapping_annotations' for variable with specified 'f…
OLarionova-HORIS Nov 29, 2023
79180cf
Few improvements.
OLarionova-HORIS Nov 29, 2023
0c2037d
Refactor code.
OLarionova-HORIS Nov 30, 2023
83a4488
Add order to series_annotations.
OLarionova-HORIS Nov 30, 2023
4353f2a
Minor refactoring.
OLarionova-HORIS Nov 30, 2023
1407676
Refactor python code.
OLarionova-HORIS Dec 1, 2023
9173754
Minor refactoring (python).
OLarionova-HORIS Dec 1, 2023
4b6c988
Add ordering for levels in example.
OLarionova-HORIS Dec 1, 2023
3ff0510
Add example with facets to notebook.
OLarionova-HORIS Dec 5, 2023
35f55fb
Add demo notebook. Update future_changes.md.
OLarionova-HORIS Dec 5, 2023
01d497d
Improve notebook.
OLarionova-HORIS Dec 6, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
as_discrete(levels) generates 'data_meta/series_annotation'.
  • Loading branch information
OLarionova-HORIS committed Dec 5, 2023
commit 4a7f510223e1e8f0cf525b23746de33bbc8c4472
9 changes: 6 additions & 3 deletions python-package/lets_plot/mapping.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@


class MappingMeta:
def __init__(self, variable, annotation, **parameters):
def __init__(self, variable, annotation, levels, **parameters):
if variable is None:
raise ValueError("variable can't be none")

Expand All @@ -12,10 +12,11 @@ def __init__(self, variable, annotation, **parameters):

self.variable = variable
self.annotation = annotation
self.levels = levels
self.parameters = parameters


def as_discrete(variable, label=None, order_by=None, order=None):
def as_discrete(variable, label=None, order_by=None, order=None, levels=None):
"""
The function is used to annotate a numeric data series as categorical data with the possibility of its ordering for the purposes of given visualization.

Expand All @@ -29,6 +30,8 @@ def as_discrete(variable, label=None, order_by=None, order=None):
The variable name to order by.
order : int
The ordering direction. 1 for ascending, -1 for descending.
levels : list
The list of values that the variable can take.

Returns
-------
Expand Down Expand Up @@ -108,6 +111,6 @@ def as_discrete(variable, label=None, order_by=None, order=None):
"""
if isinstance(variable, str):
label = variable if label is None else label
return MappingMeta(variable, 'as_discrete', label=label, order_by=order_by, order=order)
return MappingMeta(variable, 'as_discrete', levels=levels, label=label, order_by=order_by, order=order)
# aes(x=as_discrete([1, 2, 3])) - pass as is
return variable
11 changes: 8 additions & 3 deletions python-package/lets_plot/plot/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,21 @@ def as_annotated_data(raw_data: Any, raw_mapping: Any) -> Tuple:
# mapping
mapping = {}
mapping_meta = []
# series annotations
series_meta = []

if raw_mapping is not None:
for aesthetic, variable in raw_mapping.as_dict().items():
if aesthetic == 'name': # ignore FeatureSpec.name property
continue

if isinstance(variable, MappingMeta):
if variable.levels is not None:
series_meta.append({
'column': variable.variable,
'factor_levels': variable.levels
})

mapping[aesthetic] = variable.variable
mapping_meta.append({
'aes': aesthetic,
Expand All @@ -54,9 +62,6 @@ def as_annotated_data(raw_data: Any, raw_mapping: Any) -> Tuple:
if len(mapping_meta) > 0:
data_meta.update({'mapping_annotations': mapping_meta})

# series annotations
series_meta = []

if is_dict_or_dataframe(data):
for column_name, values in data.items():
if isinstance(values, Iterable):
Expand Down