Document sources carry any field of the target from their own columns, such as its ids; the ADOS-2 items of a SQL source's PDF upsert into the existing row
This commit is contained in:
@@ -150,7 +150,8 @@ class DocumentConnector(DataFrameConnector):
|
||||
self.include_pattern = _compile_filter(include_pattern, "include_pattern")
|
||||
self.exclude_pattern = _compile_filter(exclude_pattern, "exclude_pattern")
|
||||
self.skipped: list[dict[str, str]] = []
|
||||
known = {spec.name for spec in self.specs}
|
||||
# A source's own ids (`ado_id`) are never read off a page, but a source may carry them.
|
||||
known = set(entity.schema.model_fields)
|
||||
unknown = sorted({name for values in self.source_fields.values() for name in values} - known)
|
||||
if unknown:
|
||||
raise ConnectorError(f"fields desconocidos para {target}: {', '.join(unknown)}")
|
||||
@@ -264,6 +265,7 @@ class DocumentConnector(DataFrameConnector):
|
||||
if self._df is None:
|
||||
columns = (
|
||||
[spec.name for spec in self.specs]
|
||||
+ self._carried_columns()
|
||||
+ ([self.nlp_field] if self.nlp_field else [])
|
||||
+ self.caption_columns()
|
||||
+ [DOCUMENT_COLUMN, IMAGE_COLUMN, CONFIDENCE_COLUMN, SOURCES_COLUMN]
|
||||
@@ -272,11 +274,19 @@ class DocumentConnector(DataFrameConnector):
|
||||
self._df = pd.DataFrame(rows, columns=columns, dtype=object)
|
||||
return self._df
|
||||
|
||||
def _carried_columns(self) -> list[str]:
|
||||
"""Fields the source carries that no document spec reads, such as its own ids, in the order first seen."""
|
||||
specs = {spec.name for spec in self.specs}
|
||||
carried: list[str] = []
|
||||
for values in self.source_fields.values():
|
||||
carried.extend(name for name in values if name not in specs and name not in carried)
|
||||
return carried
|
||||
|
||||
def caption_columns(self) -> list[str]:
|
||||
"""Every caption the documents print that no field of the target
|
||||
claims, in the order first seen: columns of their own, mappable
|
||||
and usable in derived fields like any column of a table."""
|
||||
reserved = {spec.name for spec in self.specs} | {DOCUMENT_COLUMN, IMAGE_COLUMN, CONFIDENCE_COLUMN, SOURCES_COLUMN}
|
||||
reserved = {spec.name for spec in self.specs} | set(self._carried_columns()) | {DOCUMENT_COLUMN, IMAGE_COLUMN, CONFIDENCE_COLUMN, SOURCES_COLUMN}
|
||||
columns: list[str] = []
|
||||
for record in self.records:
|
||||
for found in record.discovered:
|
||||
@@ -294,7 +304,7 @@ class DocumentConnector(DataFrameConnector):
|
||||
|
||||
def _row(self, index: int, record: ExtractedRecord) -> dict[str, Any]:
|
||||
row: dict[str, Any] = record.row()
|
||||
reserved = {spec.name for spec in self.specs} | {DOCUMENT_COLUMN, IMAGE_COLUMN, CONFIDENCE_COLUMN, SOURCES_COLUMN}
|
||||
reserved = {spec.name for spec in self.specs} | set(self._carried_columns()) | {DOCUMENT_COLUMN, IMAGE_COLUMN, CONFIDENCE_COLUMN, SOURCES_COLUMN}
|
||||
for found in record.discovered:
|
||||
row.setdefault(_caption_column(found.caption, reserved), found.value)
|
||||
row.update(self.overrides.get(str(index), {}))
|
||||
@@ -468,11 +478,11 @@ def document_options(raw: dict[str, Any]) -> dict[str, Any]:
|
||||
|
||||
def connector_for_batch(batch: DocumentBatch, target: str, options: dict[str, Any]) -> "DocumentConnector":
|
||||
"""A `DocumentConnector` over documents read from a source's columns,
|
||||
their row values restricted to the fields extraction knows."""
|
||||
their row values restricted to the target's fields."""
|
||||
entity = ENTITY_REGISTRY.get(target)
|
||||
if entity is None:
|
||||
raise ConnectorError(f"destino desconocido: {target!r}")
|
||||
known = {spec.name for spec in field_specs_for(entity)}
|
||||
known = set(entity.schema.model_fields)
|
||||
source_fields = {name: {k: v for k, v in values.items() if k in known} for name, values in batch.source_fields.items()}
|
||||
return DocumentConnector(
|
||||
batch.files, target=target, source_fields=source_fields, record_keys=batch.record_keys, **document_options(options)
|
||||
|
||||
Reference in New Issue
Block a user