Source code for imednet.utils.tabular

"""Standard-library CSV and tabular record normalization utilities."""

from __future__ import annotations

import ast
import csv
import datetime
import io
import json
import math
import os
import re
from collections.abc import Mapping, Sequence
from enum import Enum
from pathlib import Path
from typing import TYPE_CHECKING, Any

from pydantic import BaseModel
from pydantic import ValidationError as PydanticValidationError

from imednet.core.context import strict_mode_context
from imednet.errors import ValidationError
from imednet.utils.typing import JsonDict, RecordsInput

if TYPE_CHECKING:
    from imednet.models.records import BaseRecordRequest
    from imednet.validation.cache import ValidationSchemaProtocol

MASKED_SENTINEL: str = "***MASKED***"

_INT_LITERAL_RE = re.compile(r"^-?(?:0|[1-9]\d*)$")
_DECIMAL_LITERAL_RE = re.compile(r"^-?(?:0|[1-9]\d*)\.\d+$")
_SLASH_SPACE_RE = re.compile(r"\s*/\s*")

_READ_ONLY_RECORD_COLUMNS: frozenset[str] = frozenset(
    {
        "study_key",
        "studyKey",
        "record_type",
        "recordType",
        "deleted",
        "date_created",
        "dateCreated",
        "date_modified",
        "dateModified",
        "last_updated",
        "lastUpdated",
        "parent_record_id",
        "parentRecordId",
        "keywords",
        "subject_status",
        "subjectStatus",
    }
)

_NESTED_DATA_COLUMNS: frozenset[str] = frozenset({"record_data", "recordData", "data"})

_SNAKE_TO_CAMEL_IDENTIFIERS: dict[str, str] = {
    "record_id": "recordId",
    "record_oid": "recordOid",
    "visit_id": "visitId",
    "record_status": "recordStatus",
    "reason_for_change": "reasonForChange",
    "form_key": "formKey",
    "form_id": "formId",
    "subject_key": "subjectKey",
    "subject_oid": "subjectOid",
    "subject_id": "subjectId",
    "site_name": "siteName",
    "site_id": "siteId",
    "interval_name": "intervalName",
    "interval_id": "intervalId",
}

_IDENTIFIER_COLUMNS: frozenset[str] = frozenset(
    {
        *_SNAKE_TO_CAMEL_IDENTIFIERS.keys(),
        *_SNAKE_TO_CAMEL_IDENTIFIERS.values(),
    }
)

_TEXT_LIKE_TYPES: frozenset[str] = frozenset(
    {
        "string",
        "text",
        "alphanumeric",
        "memo",
        "textarea",
        "text area",
        "notes",
        "note",
        "date",
        "date ymd",
        "date mdy",
        "date dmy",
        "datetime",
        "date/time",
        "date time",
        "durationdatetime",
        "intervaldatetime",
        "date/time precision",
        "datetime precision",
        "date time precision",
        "time",
        "file",
        "upload",
        "fileupload",
        "file upload",
        "signature",
        "descriptive",
        "label",
    }
)

_INT_LIKE_TYPES: frozenset[str] = frozenset({"int", "integer", "number"})
_FLOAT_LIKE_TYPES: frozenset[str] = frozenset({"float", "decimal", "double", "numeric"})
_NUMERIC_LIKE_TYPES: frozenset[str] = _INT_LIKE_TYPES | _FLOAT_LIKE_TYPES
_BOOL_LIKE_TYPES: frozenset[str] = frozenset({"bool", "boolean"})
_COMPUTED_FLEX_TYPES: frozenset[str] = frozenset({"calculation", "calc", "calculated", "slider"})


def _unwrap_scalar(value: Any) -> Any:
    """Unwrap NumPy/Pandas scalar wrappers into native Python primitives."""
    if value is None:
        return None
    cls_name = type(value).__name__
    if cls_name in {"NAType", "NaTType"}:
        return value
    if cls_name == "datetime64":
        return value
    if isinstance(
        value,
        (
            str,
            bytes,
            int,
            float,
            bool,
            dict,
            list,
            tuple,
            set,
            frozenset,
            datetime.date,
            datetime.time,
        ),
    ):
        return value
    item_fn = getattr(value, "item", None)
    if callable(item_fn):
        try:
            return item_fn()
        except (ValueError, TypeError):
            return value
    return value


def _contains_masked_sentinel(value: Any) -> bool:
    """Return True if *value* or any nested element contains ``***MASKED***``."""
    unwrapped = _unwrap_scalar(value)
    if isinstance(unwrapped, str):
        return MASKED_SENTINEL in unwrapped
    if isinstance(unwrapped, Mapping):
        return any(_contains_masked_sentinel(v) for v in unwrapped.values())
    if isinstance(unwrapped, (list, tuple, set, frozenset)):
        return any(_contains_masked_sentinel(v) for v in unwrapped)
    return False


[docs]def ensure_no_masked_sentinel( records: Sequence[BaseModel | Mapping[str, Any]], ) -> None: """Raise :class:`ValidationError` if any record model or mapping contains ``***MASKED***``.""" for row_idx, rec in enumerate(records): rec_map: Mapping[str, Any] if isinstance(rec, BaseModel): rec_map = rec.model_dump(by_alias=True, exclude_none=True) elif isinstance(rec, Mapping): rec_map = rec else: continue for key, value in rec_map.items(): if key in _NESTED_DATA_COLUMNS and isinstance(value, Mapping): for var_name, var_val in value.items(): if _contains_masked_sentinel(var_val): raise ValidationError( f"Row {row_idx}: variable '{var_name}' contains masked PHI sentinel " f"'{MASKED_SENTINEL}' and cannot be submitted." ) elif _contains_masked_sentinel(value): raise ValidationError( f"Row {row_idx}: column '{key}' contains masked PHI sentinel " f"'{MASKED_SENTINEL}' and cannot be submitted." )
def _is_missing_cell(value: Any) -> bool: """Return True when a tabular cell represents a null, NaN, or blank value.""" unwrapped = _unwrap_scalar(value) if unwrapped is None: return True if isinstance(unwrapped, (dict, list, tuple, set, frozenset)): return False if isinstance(unwrapped, float) and math.isnan(unwrapped): return True if isinstance(unwrapped, str): return unwrapped.strip() == "" cls_name = type(unwrapped).__name__ if cls_name in {"NAType", "NaTType"}: return True return cls_name == "datetime64" and str(unwrapped) == "NaT" def _coerce_str_identifier(value: Any) -> str: """Convert an identifier cell to a clean string, coercing integer floats.""" unwrapped = _unwrap_scalar(value) if isinstance(unwrapped, float) and math.isfinite(unwrapped) and unwrapped.is_integer(): return str(int(unwrapped)) return str(unwrapped).strip() def _coerce_int_identifier(value: Any, *, row_index: int, column_name: str) -> int: """Convert an integer identifier cell (e.g. ``formId``, ``siteId``) to ``int``.""" unwrapped = _unwrap_scalar(value) if isinstance(unwrapped, bool): raise ValidationError(f"Row {row_index}: identifier '{column_name}' must be an integer.") if isinstance(unwrapped, int): return unwrapped if isinstance(unwrapped, float) and math.isfinite(unwrapped) and unwrapped.is_integer(): return int(unwrapped) if isinstance(unwrapped, str): stripped = unwrapped.strip() if _INT_LITERAL_RE.match(stripped): return int(stripped) if _DECIMAL_LITERAL_RE.match(stripped): fval = float(stripped) if math.isfinite(fval) and fval.is_integer(): return int(fval) raise ValidationError(f"Row {row_index}: identifier '{column_name}' must be an integer.") def _lookup_form_key_from_id( schema: ValidationSchemaProtocol, form_id: int, *, study_key: str | None = None, ) -> str | None: """Resolve ``formId`` to ``formKey`` while forwarding ``study_key`` when supported.""" if study_key is not None: try: return schema.form_key_from_id(form_id, study_key=study_key) # type: ignore[call-arg] except TypeError: pass return schema.form_key_from_id(form_id) def _lookup_variables_for_form( schema: ValidationSchemaProtocol, form_key: str, *, study_key: str | None = None, ) -> Mapping[str, Any]: """Resolve form variables while forwarding ``study_key`` when supported.""" if study_key is not None: try: return schema.variables_for_form(form_key, study_key=study_key) # type: ignore[call-arg] except TypeError: pass return schema.variables_for_form(form_key) def _resolve_expected_var_type( schema: ValidationSchemaProtocol | None, form_key: str | None, form_id: int | None, var_name: str, *, study_key: str | None = None, ) -> str | None: """Look up normalized variable type from *schema* when available.""" if schema is None: return None resolved_fk = form_key if not resolved_fk and form_id is not None: resolved_fk = _lookup_form_key_from_id(schema, form_id, study_key=study_key) if not resolved_fk: return None variables = _lookup_variables_for_form(schema, resolved_fk, study_key=study_key) if not variables or var_name not in variables: return None var_obj = variables[var_name] raw_type = getattr(var_obj, "variable_type", None) or getattr(var_obj, "variableType", None) if not isinstance(raw_type, str): return None cleaned = re.sub(r"[\s_-]+", " ", raw_type.strip().lower()) if "/" in cleaned: cleaned = _SLASH_SPACE_RE.sub("/", cleaned) return cleaned def _coerce_clinical_value( value: Any, *, from_csv_str: bool, expected_var_type: str | None = None, ) -> Any: """Coerce integer floats, timestamps, and typed CSV scalars while preserving strings.""" unwrapped = _unwrap_scalar(value) if unwrapped is None: return None cls_name = type(unwrapped).__name__ if cls_name in {"NAType", "NaTType"}: return None if cls_name == "datetime64": text_dt = str(unwrapped) return None if text_dt == "NaT" else text_dt if isinstance(unwrapped, (datetime.datetime, datetime.date, datetime.time)): return unwrapped.isoformat() if isinstance(unwrapped, bool): return unwrapped if expected_var_type is not None: if expected_var_type in _BOOL_LIKE_TYPES: if isinstance(unwrapped, str): low = unwrapped.strip().lower() if low in {"true", "false"}: return low == "true" return unwrapped if expected_var_type in _NUMERIC_LIKE_TYPES: if isinstance(unwrapped, int): return unwrapped if isinstance(unwrapped, float): if ( expected_var_type in _INT_LIKE_TYPES and math.isfinite(unwrapped) and unwrapped.is_integer() ): return int(unwrapped) return unwrapped if isinstance(unwrapped, str): stripped = unwrapped.strip() if _INT_LITERAL_RE.match(stripped): return int(stripped) if _DECIMAL_LITERAL_RE.match(stripped): fval = float(stripped) if math.isfinite(fval): if expected_var_type in _INT_LIKE_TYPES and fval.is_integer(): return int(fval) return fval return unwrapped if expected_var_type not in _COMPUTED_FLEX_TYPES: if isinstance(unwrapped, str): return unwrapped if isinstance(unwrapped, float) and math.isfinite(unwrapped): return str(int(unwrapped)) if unwrapped.is_integer() else str(unwrapped) if expected_var_type in _TEXT_LIKE_TYPES: return str(unwrapped) return unwrapped if isinstance(unwrapped, int): return unwrapped if isinstance(unwrapped, float): if math.isfinite(unwrapped) and unwrapped.is_integer(): return int(unwrapped) return unwrapped if from_csv_str and isinstance(unwrapped, str): return unwrapped return unwrapped def _parse_nested_data_cell( raw_nested: Any, *, row_index: int, column_name: str, ) -> Mapping[str, Any]: """Parse a ``record_data`` / ``recordData`` / ``data`` cell from dict or serialized text.""" unwrapped = _unwrap_scalar(raw_nested) if isinstance(unwrapped, Mapping): return unwrapped if isinstance(unwrapped, str): text = unwrapped.strip() if not text: return {} try: parsed = json.loads(text) except ValueError: try: parsed = ast.literal_eval(text) except (ValueError, SyntaxError) as exc: raise ValidationError( f"Row {row_index}: column '{column_name}' is not a valid JSON object." ) from exc if isinstance(parsed, Mapping): return parsed raise ValidationError( f"Row {row_index}: column '{column_name}' must be a mapping of variable values." )
[docs]def normalize_tabular_row( raw_row: Mapping[str, Any], *, row_index: int = 0, from_csv_str: bool = False, schema: ValidationSchemaProtocol | None = None, study_key: str | None = None, default_form_key: str | None = None, default_form_id: int | None = None, ) -> JsonDict | None: """Normalize a single DataFrame or CSV row into a REST create-records payload dict. Returns ``None`` if every cell in the row is empty. """ # 1. Reject masked PHI sentinels across all columns before any stripping or coercion for col_name, raw_val in raw_row.items(): if col_name is None: raise ValidationError( f"Row {row_index}: CSV row contains more values than header columns." ) if _contains_masked_sentinel(raw_val): raise ValidationError( f"Row {row_index}: column '{col_name}' contains masked PHI sentinel " f"'{MASKED_SENTINEL}' and cannot be imported." ) if all(_is_missing_cell(v) for v in raw_row.values()): return None def _first_present(*names: str) -> tuple[str, Any] | None: for name in names: if name in raw_row and not _is_missing_cell(raw_row[name]): return name, raw_row[name] return None eff_study_key = study_key if not eff_study_key: study_pair = _first_present("studyKey", "study_key") if study_pair is not None: eff_study_key = _coerce_str_identifier(study_pair[1]) envelope: JsonDict = {} # 2. Map identifier and update-envelope columns according to REST create-records contract rec_id_pair = _first_present("recordId", "record_id") if rec_id_pair is not None: envelope["recordId"] = _coerce_int_identifier( rec_id_pair[1], row_index=row_index, column_name=rec_id_pair[0] ) rec_oid_pair = _first_present("recordOid", "record_oid") if rec_oid_pair is not None: envelope["recordOid"] = _coerce_str_identifier(rec_oid_pair[1]) visit_id_pair = _first_present("visitId", "visit_id") if visit_id_pair is not None: envelope["visitId"] = _coerce_int_identifier( visit_id_pair[1], row_index=row_index, column_name=visit_id_pair[0] ) rec_status_pair = _first_present("recordStatus", "record_status") if rec_status_pair is not None: envelope["recordStatus"] = _coerce_str_identifier(rec_status_pair[1]) reason_pair = _first_present("reasonForChange", "reason_for_change") if reason_pair is not None: envelope["reasonForChange"] = _coerce_str_identifier(reason_pair[1]) form_key_pair = _first_present("formKey", "form_key") form_id_pair = _first_present("formId", "form_id") resolved_form_key: str | None = None resolved_form_id: int | None = None if form_key_pair is not None: resolved_form_key = _coerce_str_identifier(form_key_pair[1]) envelope["formKey"] = resolved_form_key elif form_id_pair is not None: resolved_form_id = _coerce_int_identifier( form_id_pair[1], row_index=row_index, column_name=form_id_pair[0] ) envelope["formId"] = resolved_form_id elif default_form_key: resolved_form_key = default_form_key envelope["formKey"] = default_form_key elif default_form_id is not None: resolved_form_id = default_form_id envelope["formId"] = default_form_id subj_key_pair = _first_present("subjectKey", "subject_key") subj_oid_pair = _first_present("subjectOid", "subject_oid") subj_id_pair = _first_present("subjectId", "subject_id") if subj_key_pair is not None: envelope["subjectKey"] = _coerce_str_identifier(subj_key_pair[1]) elif subj_oid_pair is not None: envelope["subjectOid"] = _coerce_str_identifier(subj_oid_pair[1]) elif subj_id_pair is not None: envelope["subjectId"] = _coerce_int_identifier( subj_id_pair[1], row_index=row_index, column_name=subj_id_pair[0] ) site_name_pair = _first_present("siteName", "site_name") site_id_pair = _first_present("siteId", "site_id") if site_name_pair is not None: envelope["siteName"] = _coerce_str_identifier(site_name_pair[1]) elif site_id_pair is not None: envelope["siteId"] = _coerce_int_identifier( site_id_pair[1], row_index=row_index, column_name=site_id_pair[0] ) interval_name_pair = _first_present("intervalName", "interval_name") interval_id_pair = _first_present("intervalId", "interval_id") if interval_name_pair is not None: envelope["intervalName"] = _coerce_str_identifier(interval_name_pair[1]) elif interval_id_pair is not None: envelope["intervalId"] = _coerce_int_identifier( interval_id_pair[1], row_index=row_index, column_name=interval_id_pair[0] ) # 3. Assemble clinical data from nested data column(s) + flattened variable columns clinical_data: JsonDict = {} for nested_col in ("record_data", "recordData", "data"): if nested_col in raw_row and not _is_missing_cell(raw_row[nested_col]): parsed_nested = _parse_nested_data_cell( raw_row[nested_col], row_index=row_index, column_name=nested_col ) for var_key, var_val in parsed_nested.items(): if _contains_masked_sentinel(var_val): raise ValidationError( f"Row {row_index}: variable '{var_key}' contains masked PHI sentinel " f"'{MASKED_SENTINEL}' and cannot be imported." ) if _is_missing_cell(var_val): continue vtype = _resolve_expected_var_type( schema, resolved_form_key, resolved_form_id, str(var_key), study_key=eff_study_key, ) clinical_data[str(var_key)] = _coerce_clinical_value( var_val, from_csv_str=False, expected_var_type=vtype ) for col_name, raw_val in raw_row.items(): if ( col_name in _READ_ONLY_RECORD_COLUMNS or col_name in _IDENTIFIER_COLUMNS or col_name in _NESTED_DATA_COLUMNS or _is_missing_cell(raw_val) ): continue vtype = _resolve_expected_var_type( schema, resolved_form_key, resolved_form_id, col_name, study_key=eff_study_key, ) clinical_data[col_name] = _coerce_clinical_value( raw_val, from_csv_str=from_csv_str, expected_var_type=vtype, ) envelope["data"] = clinical_data return envelope
def _read_csv_stream_records( stream: Any, *, schema: ValidationSchemaProtocol | None = None, study_key: str | None = None, default_form_key: str | None = None, default_form_id: int | None = None, ) -> list[JsonDict]: records: list[JsonDict] = [] reader = csv.DictReader(stream) if not reader.fieldnames: return [] for idx, raw_row in enumerate(reader): normalized = normalize_tabular_row( raw_row, row_index=idx, from_csv_str=True, schema=schema, study_key=study_key, default_form_key=default_form_key, default_form_id=default_form_id, ) if normalized is not None: records.append(normalized) return records
[docs]def csv_text_to_records( raw_csv: str, *, schema: ValidationSchemaProtocol | None = None, study_key: str | None = None, default_form_key: str | None = None, default_form_id: int | None = None, ) -> list[JsonDict]: """Parse CSV text into REST create-records payload dicts.""" cleaned = raw_csv.lstrip("\ufeff") return _read_csv_stream_records( io.StringIO(cleaned), schema=schema, study_key=study_key, default_form_key=default_form_key, default_form_id=default_form_id, )
[docs]def csv_to_records( path: str | os.PathLike[str], *, encoding: str = "utf-8-sig", schema: ValidationSchemaProtocol | None = None, study_key: str | None = None, default_form_key: str | None = None, default_form_id: int | None = None, ) -> list[JsonDict]: """Load a CSV file into REST create-records payload dicts using the standard library. Works in ``packages/core`` even when ``pandas`` is not installed. """ csv_path = Path(path) with csv_path.open("r", encoding=encoding, newline="") as handle: return _read_csv_stream_records( handle, schema=schema, study_key=study_key, default_form_key=default_form_key, default_form_id=default_form_id, )
def _is_dataframe_instance(obj: object) -> bool: """Return True if *obj* is a ``pandas.DataFrame`` without requiring ``pandas`` import.""" cls = type(obj) return cls.__name__ == "DataFrame" and cls.__module__.startswith("pandas")
[docs]def dataframe_rows_to_records( df: Any, *, schema: ValidationSchemaProtocol | None = None, study_key: str | None = None, default_form_key: str | None = None, default_form_id: int | None = None, ) -> list[JsonDict]: """Convert a ``pandas.DataFrame`` into REST create-records payload dicts.""" if getattr(df, "empty", False): return [] columns = [str(col) for col in df.columns] records: list[JsonDict] = [] for idx, values in enumerate(df.itertuples(index=False, name=None)): raw_row = dict(zip(columns, values, strict=False)) normalized = normalize_tabular_row( raw_row, row_index=idx, from_csv_str=False, schema=schema, study_key=study_key, default_form_key=default_form_key, default_form_id=default_form_id, ) if normalized is not None: records.append(normalized) return records
def _needs_mapping_normalization( item: Mapping[str, Any], *, default_form_key: str | None = None, default_form_id: int | None = None, convert_record_data_alias: bool = True, ) -> bool: """Return True if *item* requires copying or key normalization for canonical wire JSON.""" if type(item) is not dict or "data" not in item or type(item.get("data")) is not dict: return True for key, val in item.items(): if ( val is None or key in _SNAKE_TO_CAMEL_IDENTIFIERS or key in _READ_ONLY_RECORD_COLUMNS or key == "record_data" or (convert_record_data_alias and key == "recordData") or isinstance(val, (Enum, BaseModel)) ): return True return bool( (default_form_key or default_form_id is not None) and not ( item.get("formKey") or item.get("formId") is not None or item.get("recordId") is not None or item.get("recordOid") ) )
[docs]def normalize_record_mapping( item: BaseModel | Mapping[str, Any], *, default_form_key: str | None = None, default_form_id: int | None = None, convert_record_data_alias: bool = True, ) -> JsonDict: """Normalize a single record request model or mapping into a canonical ``camelCase`` dict.""" if isinstance(item, BaseModel): dumped = item.model_dump(by_alias=True, exclude_none=True) for ro_key in _READ_ONLY_RECORD_COLUMNS: dumped.pop(ro_key, None) if "recordData" in dumped: rec_payload = dumped.pop("recordData") if "data" not in dumped and rec_payload is not None: dumped["data"] = ( dict(rec_payload) if isinstance(rec_payload, Mapping) else rec_payload ) for key, val in list(dumped.items()): if val is None: dumped.pop(key, None) elif isinstance(val, Enum): dumped[key] = str(val.value) if "data" not in dumped: dumped["data"] = {} if not ( dumped.get("formKey") or dumped.get("formId") is not None or dumped.get("recordId") is not None or dumped.get("recordOid") ): if default_form_key: dumped["formKey"] = default_form_key elif default_form_id is not None: dumped["formId"] = default_form_id return dumped if not isinstance(item, Mapping): raise TypeError( "All items in records_data sequence must be BaseRecordRequest or Mapping; " f"got {type(item).__name__}." ) if not _needs_mapping_normalization( item, default_form_key=default_form_key, default_form_id=default_form_id, convert_record_data_alias=convert_record_data_alias, ): return item # type: ignore[return-value] out: JsonDict = {} for key, val in item.items(): if key in _READ_ONLY_RECORD_COLUMNS: continue if key == "record_data" or (convert_record_data_alias and key == "recordData"): continue if val is None: continue target_key = _SNAKE_TO_CAMEL_IDENTIFIERS.get(key, key) if ( key in _SNAKE_TO_CAMEL_IDENTIFIERS and target_key in item and item[target_key] is not None ): continue if isinstance(val, BaseModel): dumped_val = val.model_dump(by_alias=True, exclude_none=True) out[target_key] = dumped_val elif isinstance(val, Enum): out[target_key] = str(val.value) elif target_key == "data" and isinstance(val, Mapping): out[target_key] = dict(val) else: out[target_key] = val if "data" not in out: if convert_record_data_alias and "recordData" in item and item["recordData"] is not None: raw_rec_data = item["recordData"] out["data"] = dict(raw_rec_data) if isinstance(raw_rec_data, Mapping) else raw_rec_data elif "record_data" in item and item["record_data"] is not None: raw_rec_data = item["record_data"] out["data"] = dict(raw_rec_data) if isinstance(raw_rec_data, Mapping) else raw_rec_data else: out["data"] = {} if not ( out.get("formKey") or out.get("formId") is not None or out.get("recordId") is not None or out.get("recordOid") ): if default_form_key: out["formKey"] = default_form_key elif default_form_id is not None: out["formId"] = default_form_id return out
def _validate_record_request_payloads(records: Sequence[Mapping[str, Any]]) -> None: """Validate normalized record dictionaries against :class:`BaseRecordRequest`.""" from imednet.models.records import BaseRecordRequest with strict_mode_context(True): for idx, rec in enumerate(records): try: BaseRecordRequest.model_validate(rec) except PydanticValidationError as exc: raise ValidationError( f"Invalid record request payload at index {idx}: {exc}" ) from exc
[docs]def normalize_record_payload_item(item: BaseRecordRequest | Mapping[str, Any]) -> JsonDict: """Normalize a :class:`BaseRecordRequest` or mapping into a camelCase ``JsonDict``.""" return normalize_record_mapping(item)
[docs]def normalize_record_sequence( records: Sequence[Any], *, default_form_key: str | None = None, default_form_id: int | None = None, convert_record_data_alias: bool = True, skip_validation: bool = False, ) -> list[JsonDict]: """Validate sentinels and normalize a sequence of record models or mappings to ``camelCase``.""" for item in records: if not isinstance(item, (BaseModel, Mapping)): raise TypeError( "All items in records_data sequence must be BaseRecordRequest or Mapping; " f"got {type(item).__name__}." ) ensure_no_masked_sentinel(records) materialized = [ normalize_record_mapping( item, default_form_key=default_form_key, default_form_id=default_form_id, convert_record_data_alias=convert_record_data_alias, ) for item in records ] ensure_no_masked_sentinel(materialized) if not skip_validation: _validate_record_request_payloads(materialized) return materialized
[docs]def normalize_records_input( records_data: RecordsInput, *, schema: ValidationSchemaProtocol | None = None, study_key: str | None = None, default_form_key: str | None = None, default_form_id: int | None = None, skip_validation: bool = False, ) -> list[JsonDict]: """Normalize ``DataFrame``, CSV path, or sequence of requests/mappings into record payload dicts.""" if isinstance(records_data, (str, os.PathLike)): records = csv_to_records( records_data, schema=schema, study_key=study_key, default_form_key=default_form_key, default_form_id=default_form_id, ) if not skip_validation: _validate_record_request_payloads(records) return records if _is_dataframe_instance(records_data): records = dataframe_rows_to_records( records_data, schema=schema, study_key=study_key, default_form_key=default_form_key, default_form_id=default_form_id, ) if not skip_validation: _validate_record_request_payloads(records) return records if isinstance(records_data, Sequence) and not isinstance(records_data, (str, bytes, bytearray)): return normalize_record_sequence( records_data, default_form_key=default_form_key, default_form_id=default_form_id, convert_record_data_alias=True, skip_validation=skip_validation, ) raise TypeError( "records_data must be a sequence of BaseRecordRequest or dicts, pandas.DataFrame, " f"or CSV path; got {type(records_data).__name__}." )
__all__ = [ "MASKED_SENTINEL", "csv_text_to_records", "csv_to_records", "dataframe_rows_to_records", "ensure_no_masked_sentinel", "normalize_record_mapping", "normalize_record_payload_item", "normalize_record_sequence", "normalize_records_input", "normalize_tabular_row", ]