Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SchemaView object should be instantiated when needed & not globally. #323

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .github/workflows/pypi-publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ jobs:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v3.0.2
- uses: actions/checkout@v3

- name: Set up Python
uses: actions/setup-python@v3.1.2
uses: actions/setup-python@v4.3.0
with:
python-version: 3.9

Expand Down
10 changes: 8 additions & 2 deletions sssom/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,15 @@
import click
import pandas as pd
import yaml
from linkml_runtime.utils.schema_as_dict import schema_as_dict
from linkml_runtime.utils.schemaview import SchemaView
from rdflib import Graph
from scipy.stats import chi2_contingency

from sssom.constants import (
DEFAULT_VALIDATION_TYPES,
MAPPING_SET_SLOTS,
MAPPING_SLOTS,
PREFIX_MAP_MODES,
SCHEMA_YAML,
SchemaValidationType,
)
from sssom.context import get_default_metadata
Expand Down Expand Up @@ -62,6 +63,11 @@
)
from .writers import write_table

SCHEMA_VIEW = SchemaView(SCHEMA_YAML)
hrshdhgd marked this conversation as resolved.
Show resolved Hide resolved
SCHEMA_DICT = schema_as_dict(SCHEMA_VIEW.schema)
MAPPING_SLOTS = SCHEMA_DICT["classes"]["mapping"]["slots"]
MAPPING_SET_SLOTS = SCHEMA_DICT["classes"]["mapping set"]["slots"]

# Click input options common across commands
input_argument = click.argument("input", required=True, type=click.Path())

Expand Down
23 changes: 3 additions & 20 deletions sssom/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,17 @@
from enum import Enum

import pkg_resources
from linkml_runtime.utils.schema_as_dict import schema_as_dict
from linkml_runtime.utils.schemaview import SchemaView

# from linkml_runtime.utils.introspection import package_schemaview

HERE = pathlib.Path(__file__).parent.resolve()

OWL_EQUIV_CLASS = "http://www.w3.org/2002/07/owl#equivalentClass"
RDFS_SUBCLASS_OF = "http://www.w3.org/2000/01/rdf-schema#subClassOf"

SCHEMA_YAML = pkg_resources.resource_filename(
"sssom_schema", "schema/sssom_schema.yaml"
)
SCHEMA_VIEW = SchemaView(SCHEMA_YAML)
# SCHEMA_VIEW = package_schemaview("sssom_schema")
SCHEMA_DICT = schema_as_dict(SCHEMA_VIEW.schema)
MAPPING_SLOTS = SCHEMA_DICT["classes"]["mapping"]["slots"]
MAPPING_SET_SLOTS = SCHEMA_DICT["classes"]["mapping set"]["slots"]

OWL_EQUIV_CLASS = "http://www.w3.org/2002/07/owl#equivalentClass"
RDFS_SUBCLASS_OF = "http://www.w3.org/2000/01/rdf-schema#subClassOf"

DEFAULT_MAPPING_PROPERTIES = [
"http://www.geneontology.org/formats/oboInOwl#hasDbXref",
Expand All @@ -42,16 +35,6 @@
PREFIX_MAP_MODE_SSSOM_DEFAULT_ONLY,
PREFIX_MAP_MODE_MERGED,
]
ENTITY_REFERENCE = "EntityReference"

MULTIVALUED_SLOTS = [
c for c in SCHEMA_VIEW.all_slots() if SCHEMA_VIEW.get_slot(c).multivalued
]
ENTITY_REFERENCE_SLOTS = [
c
for c in SCHEMA_VIEW.all_slots()
if SCHEMA_VIEW.get_slot(c).range == ENTITY_REFERENCE
]

# Slot Constants
MIRROR_FROM = "mirror_from"
Expand Down
13 changes: 9 additions & 4 deletions sssom/parsers.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
import yaml
from deprecation import deprecated
from linkml_runtime.loaders.json_loader import JSONLoader
from linkml_runtime.utils.schema_as_dict import schema_as_dict
from linkml_runtime.utils.schemaview import SchemaView
from rdflib import Graph, URIRef

# from .sssom_datamodel import Mapping, MappingSet
Expand All @@ -30,15 +32,14 @@
MAPPING_JUSTIFICATION,
MAPPING_JUSTIFICATION_UNSPECIFIED,
MAPPING_SET_ID,
MAPPING_SET_SLOTS,
MAPPING_SLOTS,
OBJECT_ID,
OBJECT_LABEL,
OBJECT_SOURCE,
OBJECT_SOURCE_ID,
OWL_EQUIV_CLASS,
PREDICATE_ID,
RDFS_SUBCLASS_OF,
SCHEMA_YAML,
SUBJECT_ID,
SUBJECT_LABEL,
SUBJECT_SOURCE,
Expand Down Expand Up @@ -310,6 +311,10 @@ def _get_mdict_ms_and_bad_attrs(
) -> Tuple[dict, MappingSet, Counter]:

mdict = {}
schema_view = SchemaView(SCHEMA_YAML)
schema_dict = schema_as_dict(schema_view.schema)
mapping_slots = schema_dict["classes"]["mapping"]["slots"]
mapping_set_slots = schema_dict["classes"]["mapping set"]["slots"]
hrshdhgd marked this conversation as resolved.
Show resolved Hide resolved

for k, v in row.items():
if v and v == v:
Expand All @@ -318,11 +323,11 @@ def _get_mdict_ms_and_bad_attrs(
k = str(k)
v = _address_multivalued_slot(k, v)
# if hasattr(Mapping, k):
if k in MAPPING_SLOTS:
if k in mapping_slots:
mdict[k] = v
ok = True
# if hasattr(MappingSet, k):
if k in MAPPING_SET_SLOTS:
if k in mapping_set_slots:
ms[k] = v
ok = True
if not ok:
Expand Down
19 changes: 15 additions & 4 deletions sssom/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@
import yaml
from jsonschema import ValidationError
from linkml_runtime.linkml_model.types import Uriorcurie
from linkml_runtime.utils.schema_as_dict import schema_as_dict
from linkml_runtime.utils.schemaview import SchemaView

# from .sssom_datamodel import Mapping as SSSOM_Mapping
# from .sssom_datamodel import slots
Expand All @@ -40,12 +42,9 @@
from .constants import (
COMMENT,
CONFIDENCE,
ENTITY_REFERENCE_SLOTS,
MAPPING_JUSTIFICATION,
MAPPING_SET_ID,
MAPPING_SET_SLOTS,
MAPPING_SET_SOURCE,
MULTIVALUED_SLOTS,
OBJECT_CATEGORY,
OBJECT_ID,
OBJECT_LABEL,
Expand All @@ -59,7 +58,6 @@
PREDICATE_MODIFIER_NOT,
PREFIX_MAP_MODES,
RDFS_SUBCLASS_OF,
SCHEMA_DICT,
SCHEMA_YAML,
SEMAPV,
SKOS_BROAD_MATCH,
Expand Down Expand Up @@ -1040,6 +1038,19 @@ class NoCURIEException(ValueError):


CURIE_RE = re.compile(r"[A-Za-z0-9_.]+[:][A-Za-z0-9_]")
SCHEMA_VIEW = SchemaView(SCHEMA_YAML)
ENTITY_REFERENCE = "EntityReference"
ENTITY_REFERENCE_SLOTS = [
hrshdhgd marked this conversation as resolved.
Show resolved Hide resolved
c
for c in SCHEMA_VIEW.all_slots()
if SCHEMA_VIEW.get_slot(c).range == ENTITY_REFERENCE
]
SCHEMA_DICT = schema_as_dict(SCHEMA_VIEW.schema)
MAPPING_SLOTS = SCHEMA_DICT["classes"]["mapping"]["slots"]
MAPPING_SET_SLOTS = SCHEMA_DICT["classes"]["mapping set"]["slots"]
MULTIVALUED_SLOTS = [
c for c in SCHEMA_VIEW.all_slots() if SCHEMA_VIEW.get_slot(c).multivalued
]
hrshdhgd marked this conversation as resolved.
Show resolved Hide resolved


def is_curie(string: str) -> bool:
Expand Down
3 changes: 1 addition & 2 deletions sssom/validators.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,11 @@
from linkml.validators.sparqlvalidator import SparqlDataValidator # noqa: F401
from sssom_schema import MappingSet

from sssom.constants import SCHEMA_YAML, SchemaValidationType
from sssom.context import add_built_in_prefixes_to_prefix_map
from sssom.parsers import to_mapping_set_document
from sssom.util import MappingSetDataFrame, get_all_prefixes

from .constants import SCHEMA_YAML, SchemaValidationType


def validate(
msdf: MappingSetDataFrame, validation_types: List[SchemaValidationType]
Expand Down
2 changes: 1 addition & 1 deletion sssom/writers.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@
# from .sssom_datamodel import slots
from sssom_schema import slots

from sssom.constants import SCHEMA_YAML
from sssom.validators import check_all_prefixes_in_curie_map

from .constants import SCHEMA_YAML
from .parsers import to_mapping_set_document
from .util import (
PREFIX_MAP_KEY,
Expand Down
8 changes: 7 additions & 1 deletion tests/test_sort.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,17 @@

import unittest

from sssom.constants import SCHEMA_DICT
from linkml_runtime.utils.schema_as_dict import schema_as_dict
from linkml_runtime.utils.schemaview import SchemaView

from sssom.constants import SCHEMA_YAML
from sssom.parsers import parse_sssom_table
from sssom.util import sort_df_rows_columns
from tests.constants import data_dir

SCHEMA_VIEW = SchemaView(SCHEMA_YAML)
SCHEMA_DICT = schema_as_dict(SCHEMA_VIEW.schema)
hrshdhgd marked this conversation as resolved.
Show resolved Hide resolved


class TestSort(unittest.TestCase):
"""A test case for sorting msdf columns."""
Expand Down