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
Prev Previous commit
Next Next commit
Few improvements.
  • Loading branch information
OLarionova-HORIS committed Dec 5, 2023
commit 79180cff32aafcef19a6319a2abb463d31c7e066
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,9 @@ open class PlotConfigBackend(
// Clean-up data before sending it to the front-end.
dropUnusedDataBeforeEncoding(layerConfigs)

// Re-create the "natural order" existed before faceting.
// if (facets.isDefined) {
// Re-create the "natural order" existed before faceting
// or apply the specified order
if (facets.isDefined || specifiedFactorLeversByLayers.any { it.isNotEmpty() }) {
// When faceting, each layer' data was split to panels, then re-combined with loss of 'natural order'.
layerConfigs.forEachIndexed { layerIndex, layerConfig ->
val layerData = layerConfig.ownData
Expand All @@ -91,7 +92,7 @@ open class PlotConfigBackend(
layerConfig.update(DATA_META, layerDataMetaUpdated)
}
}
// }
}
}

private fun dropUnusedDataBeforeEncoding(layerConfigs: List<LayerConfig>) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -179,10 +179,10 @@ internal object DataConfigUtil {
if (clientSide) {
val variables = rawCombinedData.variables()
val orderSpecs = OrderOptionUtil.createOrderSpecs(orderOptions, variables, varBindings, aggregateOperation)

val factorLevelsByVar = DataMetaUtil.getFactorLevelsByVariable(ownDataMeta)
.mapKeys { (varName, _) -> variables.find { it.name == varName } }
.filterNotNullKeys()

this
.addOrderSpecs(orderSpecs)
.addFactorLevels(factorLevelsByVar)
Expand Down
19 changes: 11 additions & 8 deletions python-package/test/plot/test_series_annotations.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,25 +66,28 @@ def assert_series_annotations(data, expected):
'v2': [1, 2],
# add pandas Categorical
}
factor_levels = ['foo', 'bar']


def test_factor_levels():
mapping = aes(x=as_discrete('v1', levels=factor_levels), y='v2')
mapping = aes(as_discrete('v1', levels=['foo', 'bar']))
data_meta = get_data_meta(data_dict2, mapping)
expected_factor_levels = [
{'column': 'v1', 'factor_levels': ['foo', 'bar']}
]
assert expected_factor_levels == data_meta['series_annotations']


def test_factor_levels_with_ordering():
x = as_discrete("v1", order=-1)
fill = as_discrete("v1", levels=factor_levels)
data_meta = get_data_meta(data_dict2, mapping=aes(x=x, y='v2', fill=fill))
def test_skip_mapping_annotations_for_variable_with_levels():
mapping = aes(
as_discrete("v1", order=-1),
'v2',
a=as_discrete("v1", levels=['foo', 'bar']),
b=as_discrete("v2", levels=[2, 1])
)
data_meta = get_data_meta(data_dict2, mapping)
expected_factor_levels = [
{'column': 'v1', 'factor_levels': ['foo', 'bar']}
{'column': 'v1', 'factor_levels': ['foo', 'bar']},
{'column': 'v2', 'factor_levels': [2, 1]}
]
assert expected_factor_levels == data_meta['series_annotations']
# skip creation of 'mapping_annotations'
assert 'mapping_annotations' not in data_meta