Skip to content

Commit

Permalink
Use proper eq and allow multiple values in GroupFilter
Browse files Browse the repository at this point in the history
  • Loading branch information
mattpap committed May 10, 2024
1 parent b0de863 commit 6ceb35e
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 9 deletions.
27 changes: 20 additions & 7 deletions bokehjs/src/lib/models/filters/group_filter.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,22 @@
import {Filter} from "./filter"
import type {ColumnarDataSource} from "../sources/columnar_data_source"
import {SimilarComparator} from "core/util/eq"
import {isArray} from "core/util/types"
import type * as p from "core/properties"
import {Indices} from "core/types"
import {logger} from "core/logging"
import type {ColumnarDataSource} from "../sources/columnar_data_source"
import {Or, List, Primitive} from "core/kinds"

const GroupBy = Or(List(Primitive), Primitive)
type GroupBy = typeof GroupBy["__type__"]

export namespace GroupFilter {
export type Attrs = p.AttrsOf<Props>

export type Props = Filter.Props & {
column_name: p.Property<string>
group: p.Property<string>
group: p.Property<GroupBy>
// TODO: tolerance for FP
}
}

Expand All @@ -25,20 +32,26 @@ export class GroupFilter extends Filter {
static {
this.define<GroupFilter.Props>(({Str}) => ({
column_name: [ Str ],
group: [ Str ],
group: [ GroupBy ],
}))
}

compute_indices(source: ColumnarDataSource): Indices {
const column = source.get_column(this.column_name)
const {column_name} = this
const column = source.get_column(column_name)
const size = source.get_length() ?? 1
if (column == null) {
logger.warn(`${this}: groupby column '${this.column_name}' not found in the data source`)
logger.warn(`${this}: column '${column_name}' not found in the data source`)
return Indices.all_set(size)
} else {
const indices = new Indices(size, 0)
const group = (() => {
const {group} = this
return isArray(group) ? group : [group]
})()
const indices = Indices.all_unset(size)
const cmp = new SimilarComparator()
for (let i = 0; i < indices.size; i++) {
if (column[i] === this.group) {
if (group.some((value) => cmp.eq(column[i], value))) {
indices.set(i)
}
}
Expand Down
6 changes: 4 additions & 2 deletions src/bokeh/models/filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,12 @@
from ..core.properties import (
AnyRef,
Bool,
Either,
Instance,
Int,
NonEmpty,
Nullable,
Primitive,
Required,
RestrictedDict,
Seq,
Expand Down Expand Up @@ -176,8 +178,8 @@ class GroupFilter(Filter):
The name of the column to perform the group filtering operation on.
""")

group = Required(String, help="""
The value of the column indicating the rows of data to keep.
group = Required(Either(Seq(Primitive), Primitive), help="""
The value or a collection of values of the column indicating the rows of data to keep.
""")

def __init__(self, *args, **kwargs) -> None:
Expand Down

0 comments on commit 6ceb35e

Please sign in to comment.