MCPcopy Create free account
hub / github.com/antvis/AVA / profileCode

Function profileCode

src/interpreter/python/profile.ts:34–139  ·  view source on GitHub ↗
(options: ParsedProfileOptions, schema: Schema)

Source from the content-addressed store, hash-verified

32
33/** Build fixed pandas statistics; only metadata is returned to AVA. */
34export function profileCode(options: ParsedProfileOptions, schema: Schema): string {
35 const kinds: LogicalType[] = ['numeric', 'string', 'boolean', 'date', 'unknown'];
36
37 const metrics = options.metrics.map((metric) => {
38 const definition = resolveMetric(metric, BUILTIN_METRICS);
39 const targets = definition.enable({ target: 'table' })
40 ? ['table']
41 : kinds.filter((logicalType) => definition.enable({ target: 'column', field: { logicalType } }));
42
43 return { ...metric, targets };
44 });
45
46 const expressions = BUILTIN_METRICS.filter(({ id }) => metrics.some((metric) => metric.id === id))
47 .map(({ id, expression }) => `${JSON.stringify(id)}: lambda ctx: ${expression}`)
48 .join(',\n ');
49
50 return `import json
51models = json.loads(${JSON.stringify(
52 JSON.stringify(
53 schema.models.map((model) => ({ name: model.name, columns: model.columns.map(({ name }) => ({ name })) }))
54 )
55 )})
56import math
57from collections import Counter
58from functools import cached_property
59
60SKIP = object()
61metrics = json.loads(${JSON.stringify(JSON.stringify(metrics))})
62expressions = {
63 ${expressions}
64}
65
66def logical_type(series):
67 if pd.api.types.is_bool_dtype(series.dtype):
68 return 'boolean'
69 if pd.api.types.is_numeric_dtype(series.dtype):
70 return 'numeric'
71 if pd.api.types.is_datetime64_any_dtype(series.dtype):
72 return 'date'
73
74 inferred = pd.api.types.infer_dtype(series, skipna=True)
75 if inferred == 'boolean':
76 return 'boolean'
77 if inferred in ('string', 'unicode', 'empty'):
78 return 'string'
79
80 return 'unknown'
81
82class Context:
83 def __init__(self, frame, name, kind):
84 self.row_count = len(frame)
85 self.series = frame[name] if name is not None else None
86 self.kind = kind
87 self.metric = None
88
89 # Compute each intermediate only when requested, once per target.
90 @cached_property
91 def values(self):

Callers 1

profileMethod · 0.90

Calls 1

resolveMetricFunction · 0.90

Tested by

no test coverage detected