Skip to content

Commit

Permalink
undo last commit
Browse files Browse the repository at this point in the history
  • Loading branch information
hrshdhgd committed Feb 5, 2024
1 parent f2306dc commit e766384
Showing 1 changed file with 17 additions and 93 deletions.
110 changes: 17 additions & 93 deletions tests/test_conversion.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,29 +3,24 @@
import json
import logging
import unittest
from typing import Dict
from os import stat


import pandas as pd
import yaml
from curies import Converter
from rdflib import Graph

from sssom.parsers import get_parsing_function, parse_sssom_table, to_mapping_set_document
from sssom.parsers import get_parsing_function, to_mapping_set_document
from sssom.sssom_document import MappingSetDocument
from sssom.util import MappingSetDataFrame, to_mapping_set_dataframe
from sssom.util import read_pandas, to_mapping_set_dataframe
from sssom.writers import (
to_dataframe,
to_json,
to_ontoportal_json,
to_owl_graph,
to_rdf_graph,
write_json,
write_owl,
write_rdf,
write_table,
)
from tests.constants import data_dir
from tests.test_data import SSSOMTestCase, get_all_test_cases

from .test_data import SSSOMTestCase, get_all_test_cases


class SSSOMReadWriteTestSuite(unittest.TestCase):
Expand All @@ -38,12 +33,7 @@ def test_conversion(self):
for test in test_cases:
with self.subTest(test=test.id):
read_func = get_parsing_function(test.inputformat, test.filepath)
if test.metadata_file:
with open(data_dir / test.metadata_file, "r") as f:
meta = yaml.safe_load(f)
else:
meta = None
msdf = read_func(test.filepath, prefix_map=test.prefix_map, meta=meta)
msdf = read_func(test.filepath, prefix_map=test.prefix_map)
mdoc = to_mapping_set_document(msdf)
logging.info(f"Testing {test.filepath}")
self.assertEqual(
Expand All @@ -60,8 +50,6 @@ def test_conversion(self):
logging.info("Testing JSON export")
self._test_to_json_dict(mdoc, test)
self._test_to_json(mdoc, test)
logging.info("Testing ontoportal JSON export")
self._test_to_ontoportal_json(mdoc, test)

def _test_to_owl_graph(self, mdoc, test):
msdf = to_mapping_set_dataframe(mdoc)
Expand All @@ -87,18 +75,6 @@ def _test_to_json(self, mdoc, test: SSSOMTestCase):
with open(test.get_out_file("json"), "w") as file:
write_json(msdf, file, serialisation="json")

def _test_to_ontoportal_json(self, mdoc, test: SSSOMTestCase):
msdf = to_mapping_set_dataframe(mdoc)
jsonob = to_ontoportal_json(msdf)
self.assertEqual(len(jsonob), test.ct_data_frame_rows)
first_ob: Dict = jsonob[0]
self.assertIn("classes", first_ob)
self.assertIsInstance(first_ob["classes"], list)
self.assertEqual(2, len(first_ob["classes"]))
self.assertIn("relation", first_ob)
self.assertIsInstance(first_ob["relation"], list)
self.assertGreater(len(first_ob["relation"]), 0)

def _test_to_rdf_graph(self, mdoc, test):
msdf = to_mapping_set_dataframe(mdoc)
g = to_rdf_graph(msdf)
Expand All @@ -117,7 +93,9 @@ def _test_to_rdf_graph(self, mdoc, test):
)

def _test_graph_roundtrip(self, g: Graph, test: SSSOMTestCase, file_format: str):
self._test_graph_size(g, getattr(test, f"ct_graph_queries_{file_format}"), test.filename)
self._test_graph_size(
g, getattr(test, f"ct_graph_queries_{file_format}"), test.filename
)
f_roundtrip = test.get_out_file(f"roundtrip.{file_format}")
g.serialize(destination=f_roundtrip, format=test.graph_serialisation)
self._test_load_graph_size(
Expand Down Expand Up @@ -146,16 +124,15 @@ def _test_graph_size(self, graph: Graph, queries: list, file: str):

def _test_to_dataframe(self, mdoc, test):
msdf = to_mapping_set_dataframe(mdoc)
df = msdf.df
df = to_dataframe(msdf)
self.assertEqual(
len(df),
test.ct_data_frame_rows,
f"The pandas data frame has less elements than the orginal one for {test.filename}",
)
path = test.get_out_file("roundtrip.tsv")
with open(path, "w") as file:
write_table(msdf, file=file)
data = parse_sssom_table(path).df
df.to_csv(test.get_out_file("roundtrip.tsv"), sep="\t")
# data = pd.read_csv(test.get_out_file("roundtrip.tsv"), sep="\t")
data = read_pandas(test.get_out_file("roundtrip.tsv"))
self.assertEqual(
len(data),
test.ct_data_frame_rows,
Expand All @@ -164,9 +141,8 @@ def _test_to_dataframe(self, mdoc, test):
path = test.get_out_file("tsv")
with open(path, "w") as file:
write_table(msdf, file)
# self._test_files_equal(test.get_out_file("tsv"), test.get_validate_file("tsv"))
df = parse_sssom_table(path).df

self._test_files_equal(test.get_out_file("tsv"), test.get_validate_file("tsv"))
df = read_pandas(path)
self.assertEqual(
len(df),
test.ct_data_frame_rows,
Expand All @@ -184,11 +160,9 @@ def _test_to_json_dict(self, mdoc: MappingSetDocument, test: SSSOMTestCase):
f"JSON document has less elements than the orginal one for {test.filename}. Json: {json.dumps(json_dict)}",
)

self.assertIsNotNone(msdf.df)
self.assertIsInstance(json_dict["mappings"], list)
self.assertEqual(
len(json_dict["mappings"]),
len(msdf.df.index), # type:ignore
len(msdf.df),
f"JSON document has less mappings than the orginal ({test.filename}). Json: {json.dumps(json_dict)}",
)

Expand Down Expand Up @@ -216,53 +190,3 @@ def _test_to_json_dict(self, mdoc: MappingSetDocument, test: SSSOMTestCase):
test.ct_json_elements,
f"The exported JSON file has less elements than the orginal one for {test.filename}",
)

def test_ontoportal_writer(self):
"""Test dumping to OntoPortal JSON."""
rows = [
{
"subject_id": "mesh:C067604",
"subject_source": "mesh",
"predicate_id": "skos:exactMatch",
"object_id": "CHEBI:10001",
"object_source": "chebi",
"mapping_justification": "semapv:ManualMappingCuration",
"creator_id": ["orcid:0000-0001-9439-5346"],
"mapping_date": "2023-09-13",
}
]
df = pd.DataFrame(rows)
metadata = {
"mapping_set_title": "Test Mappings",
"mapping_set_id": "https://example.org/test_id",
}
prefix_map = {
"mesh": "http://id.nlm.nih.gov/mesh/",
"CHEBI": "http://purl.obolibrary.org/obo/CHEBI_",
"semapv": "https://w3id.org/semapv/vocab/",
"skos": "http://www.w3.org/2004/02/skos/core#",
"orcid": "https://orcid.org/",
}
converter = Converter.from_prefix_map(prefix_map)
msdf = MappingSetDataFrame(df=df, metadata=metadata, converter=converter)
results = to_ontoportal_json(msdf)
self.assertIsInstance(results, list)
self.assertEqual(1, len(results))
result = results[0]
self.assertEqual(
{
"classes": [
"http://id.nlm.nih.gov/mesh/C067604",
"http://purl.obolibrary.org/obo/CHEBI_10001",
],
"subject_source_id": "mesh",
"object_source_id": "chebi",
"name": "Test Mappings",
"source_name": "https://example.org/test_id",
"source_contact_info": "orcid:0000-0001-9439-5346",
"source": "https://w3id.org/semapv/vocab/ManualMappingCuration",
"relation": ["http://www.w3.org/2004/02/skos/core#exactMatch"],
"date": "2023-09-13",
},
result,
)

0 comments on commit e766384

Please sign in to comment.