Skip to content

Commit

Permalink
Importing a dataset, if the readable labels are not available for nod…
Browse files Browse the repository at this point in the history
…es/edges in the semantic descriptions, users can generate a default one via `add-missing-readable-label` flag.
  • Loading branch information
Binh Vu committed Apr 13, 2024
1 parent fce3419 commit 963181f
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 3 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
- SAND Entity Editor UI, when users link new cell, it will automatically do an initial search using the cell as the query to save users time from re-entering the same information.
- SAND Entity Editor UI, we can apply search results to multiple cells at once.
- Users can export the linked entities
- Importing a dataset, if the readable labels are not available for nodes/edges in the semantic descriptions, users can generate a default one via `add-missing-readable-label` flag.

## [4.1.0] - 2024-04-13

Expand Down
65 changes: 62 additions & 3 deletions sand/commands/load.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
from __future__ import annotations

from pathlib import Path
from typing import List, Tuple
from typing import List, Optional, Tuple

import click
from dependency_injector.wiring import Provide, inject
from sm.dataset import Dataset, Example, FullTable
from sm.misc.funcs import import_func
from sm.outputs.semantic_model import DataNode
from sm.outputs.semantic_model import ClassNode, DataNode
from tqdm.auto import tqdm

from sand.container import use_container
from sand.helpers.dependency_injection import use_auto_inject
from sand.models import (
ContextPage,
Link,
Expand All @@ -20,10 +23,16 @@
)
from sand.models import db as dbconn
from sand.models import init_db
from sand.models.ontology import OntClassAR, OntPropertyAR


@click.command(name="load")
@click.option("-d", "--db", required=True, help="smc database file")
@click.option(
"-c",
"--config",
help="Path to the configuration file",
)
@click.option("-p", "--project", default="default", help="Project name")
@click.option(
"--dataset",
Expand All @@ -37,7 +46,19 @@
type=int,
help="Number of tables to load (negative number or zero to load all)",
)
def load_dataset(db: str, project: str, dataset: str, n_tables: int):
@click.option(
"--add-missing-readable-label",
is_flag=True,
help="Attempt to add readable label for nodes and edges that don't have them",
)
def load_dataset(
db: str,
config: Optional[str],
project: str,
dataset: str,
n_tables: int,
add_missing_readable_label: bool,
):
"""Load a dataset into a project"""
init_db(db)

Expand All @@ -50,6 +71,44 @@ def load_dataset(db: str, project: str, dataset: str, n_tables: int):
if n_tables > 0:
examples = examples[:n_tables]

with use_container(config) as container:
with use_auto_inject(container):
import_examples(
project,
examples,
add_missing_readable_label,
)


@inject
def import_examples(
project: str,
examples: list[Example[FullTable]],
add_missing_readable_label: bool,
ontclass_ar: OntClassAR = Provide["classes"],
ontprop_ar: OntPropertyAR = Provide["properties"],
):
if add_missing_readable_label:
for ex in tqdm(examples, "add missing readable labels"):
for sm in ex.sms:
for n in sm.iter_nodes():
if isinstance(n, ClassNode):
if n.readable_label is None:
n.readable_label = (
tmp.readable_label
if (tmp := ontclass_ar.get_by_uri(n.abs_uri))
is not None
else None
)
for e in sm.iter_edges():
if e.readable_label is None:
if e.readable_label is None:
e.readable_label = (
tmp.readable_label
if (tmp := ontprop_ar.get_by_uri(e.abs_uri)) is not None
else None
)

with dbconn:
p = Project.get(name=project)
for e in tqdm(examples, desc="Loading examples"):
Expand Down
1 change: 1 addition & 0 deletions sand/container.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ def use_container(config_file: Optional[Path | str] = None):
"sand.serializer",
"sand.helpers",
"sand.app",
"sand.commands.load",
],
modules=["sand.extensions.export.drepr.main"],
)
Expand Down

0 comments on commit 963181f

Please sign in to comment.