Merge pull request #1166 from quantified-uncertainty/fix-multiple-plots
Fixes multiple plots
This commit is contained in:
commit
9ddd1bfae0
|
@ -1,5 +1,11 @@
|
||||||
import * as yup from "yup";
|
import * as yup from "yup";
|
||||||
import { SqDistribution, result, SqRecord } from "@quri/squiggle-lang";
|
import {
|
||||||
|
SqValue,
|
||||||
|
SqValueTag,
|
||||||
|
SqDistribution,
|
||||||
|
result,
|
||||||
|
SqRecord,
|
||||||
|
} from "@quri/squiggle-lang";
|
||||||
|
|
||||||
export type LabeledDistribution = {
|
export type LabeledDistribution = {
|
||||||
name: string;
|
name: string;
|
||||||
|
@ -21,48 +27,55 @@ function ok<a, b>(x: a): result<a, b> {
|
||||||
|
|
||||||
const schema = yup
|
const schema = yup
|
||||||
.object()
|
.object()
|
||||||
.strict()
|
|
||||||
.noUnknown()
|
.noUnknown()
|
||||||
|
.strict()
|
||||||
.shape({
|
.shape({
|
||||||
distributions: yup.object().shape({
|
distributions: yup
|
||||||
tag: yup.mixed().oneOf(["array"]),
|
|
||||||
value: yup
|
|
||||||
.array()
|
.array()
|
||||||
|
.required()
|
||||||
.of(
|
.of(
|
||||||
yup.object().shape({
|
yup.object().required().shape({
|
||||||
tag: yup.mixed().oneOf(["record"]),
|
name: yup.string().required(),
|
||||||
value: yup.object({
|
distribution: yup.mixed().required(),
|
||||||
name: yup.object().shape({
|
|
||||||
tag: yup.mixed().oneOf(["string"]),
|
|
||||||
value: yup.string().required(),
|
|
||||||
}),
|
|
||||||
// color: yup
|
|
||||||
// .object({
|
|
||||||
// tag: yup.mixed().oneOf(["string"]),
|
|
||||||
// value: yup.string().required(),
|
|
||||||
// })
|
|
||||||
// .default(undefined),
|
|
||||||
distribution: yup.object({
|
|
||||||
tag: yup.mixed().oneOf(["distribution"]),
|
|
||||||
value: yup.mixed(),
|
|
||||||
}),
|
|
||||||
}),
|
|
||||||
})
|
})
|
||||||
)
|
),
|
||||||
.required(),
|
|
||||||
}),
|
|
||||||
});
|
});
|
||||||
|
|
||||||
|
type JsonObject =
|
||||||
|
| string
|
||||||
|
| { [key: string]: JsonObject }
|
||||||
|
| JsonObject[]
|
||||||
|
| SqDistribution;
|
||||||
|
|
||||||
|
function toJson(val: SqValue): JsonObject {
|
||||||
|
if (val.tag === SqValueTag.String) {
|
||||||
|
return val.value;
|
||||||
|
} else if (val.tag === SqValueTag.Record) {
|
||||||
|
return toJsonRecord(val.value);
|
||||||
|
} else if (val.tag === SqValueTag.Array) {
|
||||||
|
return val.value.getValues().map(toJson);
|
||||||
|
} else if (val.tag === SqValueTag.Distribution) {
|
||||||
|
return val.value;
|
||||||
|
} else {
|
||||||
|
throw new Error("Could not parse object of type " + val.tag);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function toJsonRecord(val: SqRecord): JsonObject {
|
||||||
|
let recordObject: JsonObject = {};
|
||||||
|
val.entries().forEach(([key, value]) => (recordObject[key] = toJson(value)));
|
||||||
|
return recordObject;
|
||||||
|
}
|
||||||
|
|
||||||
export function parsePlot(record: SqRecord): result<Plot, string> {
|
export function parsePlot(record: SqRecord): result<Plot, string> {
|
||||||
try {
|
try {
|
||||||
const plotRecord = schema.validateSync(record);
|
const plotRecord = schema.validateSync(toJsonRecord(record));
|
||||||
return ok({
|
if (plotRecord.distributions) {
|
||||||
distributions: plotRecord.distributions.value.map((x) => ({
|
return ok({ distributions: plotRecord.distributions.map((x) => x) });
|
||||||
name: x.value.name.value,
|
} else {
|
||||||
// color: x.value.color?.value, // not supported yet
|
// I have no idea why yup's typings thinks this is possible
|
||||||
distribution: x.value.distribution.value,
|
return error("no distributions field. Should never get here");
|
||||||
})),
|
}
|
||||||
});
|
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
const message = e instanceof Error ? e.message : "Unknown error";
|
const message = e instanceof Error ? e.message : "Unknown error";
|
||||||
return error(message);
|
return error(message);
|
||||||
|
|
Loading…
Reference in New Issue
Block a user