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

Auto: error on underspecified reduce #1833

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
20 changes: 13 additions & 7 deletions src/marks/auto.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {ascending, InternSet} from "d3";
import {marks} from "../mark.js";
import {isColor, isNumeric, isObject, isOptions, isOrdinal, labelof, valueof} from "../options.js";
import {isColor, isNumeric, isObject, isOptions, isOrdinal, isEvery, labelof, valueof} from "../options.js";
import {bin, binX, binY} from "../transforms/bin.js";
import {group, groupX, groupY} from "../transforms/group.js";
import {areaX, areaY} from "./area.js";
Expand All @@ -15,11 +15,6 @@ import {ruleX, ruleY} from "./rule.js";
export function autoSpec(data, options) {
options = normalizeOptions(options);

for (const channel of ["x", "y", "color", "size"]) {
if (isUnderspecifiedReduce(options[channel]))
throw new Error(`setting ${channel} reducer to "${options[channel].reduce}" requires setting ${channel} field`);
}

// Greedily materialize columns for type inference; we’ll need them anyway to
// plot! Note that we don’t apply any type inference to the fx and fy
// channels, if present; these are always ordinal (at least for now).
Expand All @@ -29,6 +24,13 @@ export function autoSpec(data, options) {
const C = materializeValue(data, color);
const S = materializeValue(data, size);

if (!isPrimitive(data)) {
for (const channel of ["x", "y", "color", "size"]) {
if (isUnderspecifiedReduce(options[channel]))
throw new Error(`setting ${channel} reducer to "${options[channel].reduce}" requires setting ${channel} field`);
}
}

// Compute the default options.
let {
fx,
Expand Down Expand Up @@ -346,7 +348,11 @@ function isReducer(reduce) {
}

function isUnderspecifiedReduce({value, reduce}) {
return value == null && reduce != null && /^count$/i.test(reduce);
return value == null && reduce != null && !/^count$/i.test(reduce);
}

function isPrimitive(values) {
return isEvery(values, (d) => ["number", "boolean", "string"].includes(typeof d) || d instanceof Date);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Couple nits here.

First, isEvery requires values to be an iterable, but mark data can also be an “arrayish” value that is supported by Array.from such as {length: 10}. So, we’d need to handle that case, too. I recommend protecting the call as isIterable(data) && !isPrimitive(data) (since any data is not iterable by definition can only contain undefined, as in a sparse array, which we would not consider to be primitive).

Second, we should also treat bigints as primitive. (There’s also the symbol type to consider. I guess you could use the mode reducer on non-primitive values? But that’s obscure enough that I wouldn’t worry about it. If you’re doing these things you probably can debug the error.)

Last, I would avoid array.includes(type) in the inner loop. Unless the JavaScript engine is smart, it’ll have to allocate that array afresh for each value, and the call to array.includes is also probably slower than an inline logical expression. It’s better to just write this by hand; see isOrdinal for an example.

}

function isHighCardinality(value) {
Expand Down
39 changes: 39 additions & 0 deletions test/marks/auto-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -243,3 +243,42 @@ it("Plot.autoSpec makes a faceted heatmap", () => {
colorMode: "fill"
});
});

it.only("autoSpec rejects an underspecified reducer", () => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

☝️ it.only

// primitive array ok
assert.deepStrictEqual(Plot.autoSpec([74, 65], {x: Plot.identity, y: "max"}), {
fx: null,
fy: null,
x: {value: Plot.identity, reduce: null, zero: false},
y: {value: null, reduce: "max", zero: false},
color: {value: null, reduce: null},
size: {value: null, reduce: null},
mark: "rule",
markImpl: "ruleX",
markOptions: {
fx: undefined,
fy: undefined,
x: {value: Object.assign([74, 65], {label: undefined})},
y: undefined,
stroke: undefined,
z: undefined,
r: undefined,
tip: true
},
transformImpl: "binX",
transformOptions: {stroke: undefined, r: undefined, y: "max"},
colorMode: "stroke"
});
// in this case, reducer "max" has no input
assert.throws(
() =>
Plot.autoSpec(
[
{name: "Toph", height: 74},
{name: "Claire", height: 65}
],
{x: "height", y: "max"}
),
/^Error: setting y reducer to "max" requires setting y field$/
);
});