(options: ParsedProfileOptions, schema: Schema)
| 32 | |
| 33 | /** Build fixed pandas statistics; only metadata is returned to AVA. */ |
| 34 | export 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 |
| 51 | models = json.loads(${JSON.stringify( |
| 52 | JSON.stringify( |
| 53 | schema.models.map((model) => ({ name: model.name, columns: model.columns.map(({ name }) => ({ name })) })) |
| 54 | ) |
| 55 | )}) |
| 56 | import math |
| 57 | from collections import Counter |
| 58 | from functools import cached_property |
| 59 | |
| 60 | SKIP = object() |
| 61 | metrics = json.loads(${JSON.stringify(JSON.stringify(metrics))}) |
| 62 | expressions = { |
| 63 | ${expressions} |
| 64 | } |
| 65 | |
| 66 | def 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 | |
| 82 | class 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): |
no test coverage detected