@nshiab/simple-data-analysis-core 2.1.7 → 2.1.9
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/esm/class/SimpleTable.d.ts +191 -108
- package/esm/class/SimpleTable.d.ts.map +1 -1
- package/esm/class/SimpleTable.js +188 -105
- package/esm/helpers/buildGraphTemporalCostStateSql.d.ts +1 -1
- package/esm/helpers/buildGraphTemporalCostStateSql.d.ts.map +1 -1
- package/esm/helpers/buildGraphTemporalCostStateSql.js +23 -9
- package/esm/helpers/graphRouteResultSchema.d.ts +1 -1
- package/esm/helpers/graphRouteResultSchema.d.ts.map +1 -1
- package/esm/helpers/graphRouteResultSchema.js +3 -3
- package/esm/helpers/graphRouteResultSelect.d.ts +1 -1
- package/esm/helpers/graphRouteResultSelect.d.ts.map +1 -1
- package/esm/helpers/graphRouteResultSelect.js +2 -2
- package/esm/helpers/prepareGraphMetricOptions.d.ts +2 -2
- package/esm/helpers/prepareGraphMetricOptions.d.ts.map +1 -1
- package/esm/helpers/prepareGraphMetricOptions.js +5 -4
- package/esm/helpers/prepareGraphRouteSql.d.ts +2 -2
- package/esm/helpers/prepareGraphRouteSql.d.ts.map +1 -1
- package/esm/helpers/prepareGraphRouteSql.js +4 -4
- package/esm/helpers/validateGraphRouteResultSchema.d.ts +1 -1
- package/esm/helpers/validateGraphRouteResultSchema.d.ts.map +1 -1
- package/esm/helpers/validateGraphRouteResultSchema.js +3 -1
- package/esm/methods/distances.d.ts +1 -1
- package/esm/methods/distances.d.ts.map +1 -1
- package/esm/methods/distances.js +235 -73
- package/esm/methods/shortestPath.d.ts +1 -1
- package/esm/methods/shortestPath.d.ts.map +1 -1
- package/esm/methods/shortestPath.js +44 -28
- package/esm/methods/similarityMahalanobis.js +3 -3
- package/llm.md +214 -119
- package/llms.txt +1 -1
- package/package.json +1 -1
- package/script/class/SimpleTable.d.ts +191 -108
- package/script/class/SimpleTable.d.ts.map +1 -1
- package/script/class/SimpleTable.js +188 -105
- package/script/helpers/buildGraphTemporalCostStateSql.d.ts +1 -1
- package/script/helpers/buildGraphTemporalCostStateSql.d.ts.map +1 -1
- package/script/helpers/buildGraphTemporalCostStateSql.js +23 -9
- package/script/helpers/graphRouteResultSchema.d.ts +1 -1
- package/script/helpers/graphRouteResultSchema.d.ts.map +1 -1
- package/script/helpers/graphRouteResultSchema.js +3 -3
- package/script/helpers/graphRouteResultSelect.d.ts +1 -1
- package/script/helpers/graphRouteResultSelect.d.ts.map +1 -1
- package/script/helpers/graphRouteResultSelect.js +2 -2
- package/script/helpers/prepareGraphMetricOptions.d.ts +2 -2
- package/script/helpers/prepareGraphMetricOptions.d.ts.map +1 -1
- package/script/helpers/prepareGraphMetricOptions.js +5 -4
- package/script/helpers/prepareGraphRouteSql.d.ts +2 -2
- package/script/helpers/prepareGraphRouteSql.d.ts.map +1 -1
- package/script/helpers/prepareGraphRouteSql.js +4 -4
- package/script/helpers/validateGraphRouteResultSchema.d.ts +1 -1
- package/script/helpers/validateGraphRouteResultSchema.d.ts.map +1 -1
- package/script/helpers/validateGraphRouteResultSchema.js +3 -1
- package/script/methods/distances.d.ts +1 -1
- package/script/methods/distances.d.ts.map +1 -1
- package/script/methods/distances.js +235 -73
- package/script/methods/shortestPath.d.ts +1 -1
- package/script/methods/shortestPath.d.ts.map +1 -1
- package/script/methods/shortestPath.js +44 -28
- package/script/methods/similarityMahalanobis.js +3 -3
|
@@ -53,7 +53,7 @@ export default function shortestPath(simpleTable, sourceColumn, targetColumn, ed
|
|
|
53
53
|
? undefined
|
|
54
54
|
: (input) => validateGraphTemporalEvents(input, temporalOptions, "shortestPath()", parameters),
|
|
55
55
|
values: (schema) => {
|
|
56
|
-
validateGraphRouteInputs(schema, sourceColumn, targetColumn, edgeId, endpoints, options.weight, "shortestPath()");
|
|
56
|
+
validateGraphRouteInputs(schema, sourceColumn, targetColumn, edgeId, endpoints, options.weight, "shortestPath()", options.weight !== undefined);
|
|
57
57
|
const temporal = temporalOptions === undefined
|
|
58
58
|
? undefined
|
|
59
59
|
: prepareGraphTemporalSql(schema, temporalOptions, "shortestPath()");
|
|
@@ -63,27 +63,28 @@ export default function shortestPath(simpleTable, sourceColumn, targetColumn, ed
|
|
|
63
63
|
},
|
|
64
64
|
buildSelect: (input, schema) => shortestPathSelect(input, schema, sourceColumn, targetColumn, edgeId, endpoints, direction, options.weight, temporalOptions, metrics),
|
|
65
65
|
outputSchema: (schema) => {
|
|
66
|
-
const validated = validateGraphRouteInputs(schema, sourceColumn, targetColumn, edgeId, endpoints, options.weight, "shortestPath()");
|
|
66
|
+
const validated = validateGraphRouteInputs(schema, sourceColumn, targetColumn, edgeId, endpoints, options.weight, "shortestPath()", options.weight !== undefined);
|
|
67
67
|
if (temporalOptions !== undefined) {
|
|
68
68
|
prepareGraphTemporalSql(schema, temporalOptions, "shortestPath()");
|
|
69
69
|
}
|
|
70
|
-
return graphRouteResultSchema(schema, validated.distanceType, "shortestPath()", metrics.elapsedTime);
|
|
70
|
+
return graphRouteResultSchema(schema, validated.distanceType, "shortestPath()", metrics.elapsedTime, options.weight !== undefined);
|
|
71
71
|
},
|
|
72
72
|
});
|
|
73
73
|
}
|
|
74
74
|
function shortestPathSelect(input, schema, source, target, edgeId, routeEndpoints, direction, weight, temporalOptions, metrics) {
|
|
75
|
-
const route = prepareGraphRouteSql(input, schema, source, target, edgeId, routeEndpoints, weight, "shortestPath()");
|
|
75
|
+
const route = prepareGraphRouteSql(input, schema, source, target, edgeId, routeEndpoints, weight, "shortestPath()", weight !== undefined);
|
|
76
76
|
const prepared = route.traversal;
|
|
77
77
|
if (temporalOptions !== undefined) {
|
|
78
78
|
const temporal = prepareGraphTemporalSql(schema, temporalOptions, "shortestPath()");
|
|
79
|
-
return temporalShortestPathSelect(route, direction, temporal, metrics);
|
|
79
|
+
return temporalShortestPathSelect(route, direction, temporal, metrics, weight !== undefined);
|
|
80
80
|
}
|
|
81
81
|
const distanceType = route.distanceType;
|
|
82
82
|
// Exact shortest routes have optimal prefixes. Floating addition can erase
|
|
83
83
|
// a prefix-cost difference later, so retain all simple prefixes up to the
|
|
84
84
|
// best complete cost before selecting the full-route ties.
|
|
85
|
-
const floating =
|
|
86
|
-
route.weightType === "DOUBLE";
|
|
85
|
+
const floating = metrics.minimize === "weight" &&
|
|
86
|
+
(route.weightType === "FLOAT" || route.weightType === "DOUBLE");
|
|
87
|
+
const metricType = metrics.minimize === "steps" ? "BIGINT" : distanceType;
|
|
87
88
|
const relations = prepared.relationNames([
|
|
88
89
|
"graph_route_endpoints",
|
|
89
90
|
"graph_edges",
|
|
@@ -109,8 +110,17 @@ function shortestPathSelect(input, schema, source, target, edgeId, routeEndpoint
|
|
|
109
110
|
const edgeToKey = `${q("edges")}.${q("__to_key")}`;
|
|
110
111
|
const edgeTo = `${q("edges")}.${q("__to")}`;
|
|
111
112
|
const edgeCost = `${q("edges")}.${q("__weight")}`;
|
|
113
|
+
const edgeMetric = metrics.minimize === "steps"
|
|
114
|
+
? "CAST(1 AS BIGINT)"
|
|
115
|
+
: edgeCost;
|
|
112
116
|
const routeDistance = `${q("routes")}.${q("distance")}`;
|
|
113
117
|
const candidateDistance = `CAST(${routeDistance} + ${edgeCost} AS ${distanceType})`;
|
|
118
|
+
const routeMetric = metrics.minimize === "steps"
|
|
119
|
+
? `len(${q("routes")}.${q("steps")})`
|
|
120
|
+
: routeDistance;
|
|
121
|
+
const candidateMetric = metrics.minimize === "steps"
|
|
122
|
+
? `${routeMetric} + 1`
|
|
123
|
+
: candidateDistance;
|
|
114
124
|
return `WITH RECURSIVE ${endpointRelation}(
|
|
115
125
|
${q("start")}, ${q("end")}, ${q("__start_key")}, ${q("__end_key")}
|
|
116
126
|
) AS (
|
|
@@ -142,13 +152,13 @@ function shortestPathSelect(input, schema, source, target, edgeId, routeEndpoint
|
|
|
142
152
|
${q("node")}, ${q("__node_key")}, ${q("distance")}
|
|
143
153
|
) USING KEY(${q("__node_key")}) AS (
|
|
144
154
|
SELECT ${q("start")}, ${q("__start_key")},
|
|
145
|
-
CAST(0 AS ${
|
|
155
|
+
CAST(0 AS ${metricType})
|
|
146
156
|
FROM ${endpointRelation}
|
|
147
157
|
INNER JOIN ${toEndRelation}
|
|
148
158
|
ON ${q("__start_key")} = ${q("__key")}
|
|
149
159
|
UNION
|
|
150
160
|
SELECT ${edgeTo}, ${edgeToKey},
|
|
151
|
-
MIN(CAST(${q("reached")}.${q("distance")} + ${
|
|
161
|
+
MIN(CAST(${q("reached")}.${q("distance")} + ${edgeMetric} AS ${metricType}))
|
|
152
162
|
FROM ${distancesRelation} AS ${q("reached")}
|
|
153
163
|
INNER JOIN ${edgesRelation} AS ${q("edges")}
|
|
154
164
|
ON ${q("reached")}.${q("__node_key")} = ${edgeFromKey}
|
|
@@ -158,7 +168,7 @@ function shortestPathSelect(input, schema, source, target, edgeId, routeEndpoint
|
|
|
158
168
|
ON ${edgeToKey} = ${q("best")}.${q("__node_key")}
|
|
159
169
|
GROUP BY ${edgeTo}, ${edgeToKey}, ${q("best")}.${q("distance")}
|
|
160
170
|
HAVING ${q("best")}.${q("distance")} IS NULL
|
|
161
|
-
OR MIN(CAST(${q("reached")}.${q("distance")} + ${
|
|
171
|
+
OR MIN(CAST(${q("reached")}.${q("distance")} + ${edgeMetric} AS ${metricType})) < ${q("best")}.${q("distance")}
|
|
162
172
|
), ${bestTotalRelation} AS (
|
|
163
173
|
SELECT ${q("distances")}.${q("distance")}
|
|
164
174
|
FROM ${distancesRelation} AS ${q("distances")}
|
|
@@ -197,23 +207,23 @@ function shortestPathSelect(input, schema, source, target, edgeId, routeEndpoint
|
|
|
197
207
|
WHERE ${q("routes")}.${q("__node_key")} <> ${q("__end_key")}
|
|
198
208
|
AND NOT list_contains(${q("routes")}.${q("__visited")}, ${edgeToKey})
|
|
199
209
|
AND ${floating
|
|
200
|
-
? `${
|
|
201
|
-
: `${
|
|
210
|
+
? `${candidateMetric} <= ${q("best_total")}.${q("distance")}`
|
|
211
|
+
: `${candidateMetric} = ${q("best_prefix")}.${q("distance")}`}
|
|
202
212
|
), ${shortestRelation} AS (
|
|
203
213
|
SELECT ${q("__edge_keys")}, ${q("steps")}
|
|
204
|
-
FROM ${routesRelation}
|
|
214
|
+
FROM ${routesRelation} AS ${q("routes")}
|
|
205
215
|
CROSS JOIN ${endpointRelation}
|
|
206
216
|
CROSS JOIN ${bestTotalRelation} AS ${q("best_total")}
|
|
207
217
|
WHERE ${q("__node_key")} = ${q("__end_key")}
|
|
208
|
-
AND ${
|
|
218
|
+
AND ${routeMetric} = ${q("best_total")}.${q("distance")}
|
|
209
219
|
), ${rankedRelation} AS (
|
|
210
220
|
SELECT CAST(row_number() OVER (ORDER BY ${q("__edge_keys")}) - 1 AS BIGINT) AS ${q("pathId")},
|
|
211
221
|
${q("steps")}
|
|
212
222
|
FROM ${shortestRelation}
|
|
213
223
|
)
|
|
214
|
-
${graphRouteResultSelect(rankedRelation, route.input, route.edgeIdColumn)}`;
|
|
224
|
+
${graphRouteResultSelect(rankedRelation, route.input, route.edgeIdColumn, false, false, weight !== undefined)}`;
|
|
215
225
|
}
|
|
216
|
-
function temporalShortestPathSelect(route, direction, temporal, metrics) {
|
|
226
|
+
function temporalShortestPathSelect(route, direction, temporal, metrics, includeTotal) {
|
|
217
227
|
const prepared = route.traversal;
|
|
218
228
|
const q = quoteIdentifier;
|
|
219
229
|
const startsSelect = `SELECT ${q("start")},
|
|
@@ -226,7 +236,7 @@ function temporalShortestPathSelect(route, direction, temporal, metrics) {
|
|
|
226
236
|
: buildGraphTemporalCostStateSql(prepared, startsSelect, direction, temporal, route.distanceType, route.edgeWeight, [
|
|
227
237
|
`${route.typedEdgeId} AS ${q("__edge_id")}`,
|
|
228
238
|
`${route.edgeKey} AS ${q("__edge_key")}`,
|
|
229
|
-
]);
|
|
239
|
+
], metrics.minimize === "steps" ? "steps" : "weight");
|
|
230
240
|
const relations = prepared.relationNames([
|
|
231
241
|
"graph_route_endpoints",
|
|
232
242
|
"graph_nodes",
|
|
@@ -256,15 +266,21 @@ function temporalShortestPathSelect(route, direction, temporal, metrics) {
|
|
|
256
266
|
const routeAnchor = `${q("routes")}.${q("__journey_anchor")}`;
|
|
257
267
|
const firstElapsed = temporal.journeyElapsed("edges", firstAnchor, direction);
|
|
258
268
|
const candidateElapsed = temporal.journeyElapsed("edges", routeAnchor, direction);
|
|
259
|
-
const firstMetric = metrics.minimize === "
|
|
260
|
-
?
|
|
261
|
-
:
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
269
|
+
const firstMetric = metrics.minimize === "steps"
|
|
270
|
+
? "1"
|
|
271
|
+
: metrics.minimize === "elapsedTime"
|
|
272
|
+
? firstElapsed
|
|
273
|
+
: edgeCost;
|
|
274
|
+
const routeMetric = metrics.minimize === "steps"
|
|
275
|
+
? `len(${q("routes")}.${q("steps")})`
|
|
276
|
+
: metrics.minimize === "elapsedTime"
|
|
277
|
+
? `${q("routes")}.${q("__elapsed")}`
|
|
278
|
+
: routeDistance;
|
|
279
|
+
const candidateMetric = metrics.minimize === "steps"
|
|
280
|
+
? `${routeMetric} + 1`
|
|
281
|
+
: metrics.minimize === "elapsedTime"
|
|
282
|
+
? candidateElapsed
|
|
283
|
+
: candidateDistance;
|
|
268
284
|
const step = (distance, elapsed) => `struct_pack(
|
|
269
285
|
${q("edgeId")} := ${q("edges")}.${q("__edge_id")},
|
|
270
286
|
${q("source")} := ${q("edges")}.${q("__from")},
|
|
@@ -297,7 +313,7 @@ function temporalShortestPathSelect(route, direction, temporal, metrics) {
|
|
|
297
313
|
INNER JOIN ${edgesRelation} AS ${q("edges")}
|
|
298
314
|
ON ${q("reached")}.${q("__key")} = ${edgeToKey}
|
|
299
315
|
), ${bestTotalRelation} AS (
|
|
300
|
-
SELECT MIN(${q("costs")}.${q("distance")}) AS ${q("distance")}
|
|
316
|
+
SELECT MIN(${q("costs")}.${q(metrics.minimize === "steps" ? "steps" : "distance")}) AS ${q("distance")}
|
|
301
317
|
FROM ${costs.costRelation} AS ${q("costs")}
|
|
302
318
|
INNER JOIN ${endpointRelation}
|
|
303
319
|
ON ${q("costs")}.${q("__node_key")} = ${q("__end_key")}
|
|
@@ -352,7 +368,7 @@ function temporalShortestPathSelect(route, direction, temporal, metrics) {
|
|
|
352
368
|
AS ${q("pathId")}, ${q("steps")}
|
|
353
369
|
FROM ${shortestRelation}
|
|
354
370
|
)
|
|
355
|
-
${graphRouteResultSelect(rankedRelation, route.input, route.edgeIdColumn, false, metrics.elapsedTime)}`;
|
|
371
|
+
${graphRouteResultSelect(rankedRelation, route.input, route.edgeIdColumn, false, metrics.elapsedTime, includeTotal)}`;
|
|
356
372
|
}
|
|
357
373
|
// At the same physical event a later departure (or earlier incoming arrival)
|
|
358
374
|
// dominates every future elapsed duration. Keyed improvements find the optimum
|
|
@@ -44,11 +44,11 @@ export default function similarityMahalanobis(table, columns, referencePoint, ne
|
|
|
44
44
|
}
|
|
45
45
|
async function execute(table, columns, referencePoint, newColumn, options) {
|
|
46
46
|
const sourceColumns = Object.keys(await table.getTypes());
|
|
47
|
-
const scoreColumn = options.
|
|
47
|
+
const scoreColumn = options.similarityColumn === true
|
|
48
48
|
? "similarity"
|
|
49
|
-
: options.
|
|
49
|
+
: options.similarityColumn === false
|
|
50
50
|
? undefined
|
|
51
|
-
: options.
|
|
51
|
+
: options.similarityColumn;
|
|
52
52
|
const outputNames = scoreColumn === undefined
|
|
53
53
|
? [newColumn]
|
|
54
54
|
: [newColumn, scoreColumn];
|