From 3c8348ef3d97cfe746f39e10539dee061ca68439 Mon Sep 17 00:00:00 2001 From: glass-ships Date: Wed, 6 Dec 2023 13:17:04 -0700 Subject: [PATCH] couple tweaks --- Makefile | 2 +- backend/src/monarch_py/datamodels/model.py | 2 +- backend/src/monarch_py/datamodels/solr.py | 2 +- backend/src/monarch_py/utils/utils.py | 8 ++++---- .../fixtures/phenotype_explorer_compare.py | 12 ++++++------ backend/tests/unit/test_solr_parsers.py | 12 +++--------- backend/tests/unit/test_solr_queries.py | 12 ++++++------ .../fixtures/phenotype-explorer-compare.json | 18 +++++++++--------- 8 files changed, 31 insertions(+), 37 deletions(-) diff --git a/Makefile b/Makefile index 2ddce5a7d..63f3bbf22 100644 --- a/Makefile +++ b/Makefile @@ -75,7 +75,7 @@ install-frontend: .PHONY: model model: install-backend - $(RUN) gen-pydantic --pydantic-version 2 --extra-fields ignore $(SCHEMADIR)/model.yaml > $(SCHEMADIR)/model.py + $(RUN) gen-pydantic --pydantic-version 2 --extra-fields allow $(SCHEMADIR)/model.yaml > $(SCHEMADIR)/model.py $(RUN) gen-typescript $(SCHEMADIR)/model.yaml > frontend/src/api/model.ts make format diff --git a/backend/src/monarch_py/datamodels/model.py b/backend/src/monarch_py/datamodels/model.py index 07c984dea..6761a879c 100644 --- a/backend/src/monarch_py/datamodels/model.py +++ b/backend/src/monarch_py/datamodels/model.py @@ -19,7 +19,7 @@ class ConfiguredBaseModel(BaseModel): model_config = ConfigDict( validate_assignment=True, validate_default=True, - extra="ignore", + extra="allow", arbitrary_types_allowed=True, use_enum_values=True, ) diff --git a/backend/src/monarch_py/datamodels/solr.py b/backend/src/monarch_py/datamodels/solr.py index 9dd4972eb..6597af3e3 100644 --- a/backend/src/monarch_py/datamodels/solr.py +++ b/backend/src/monarch_py/datamodels/solr.py @@ -67,7 +67,7 @@ def add_filter_query(self, filter_query): def query_string(self): return urllib.parse.urlencode( - {self._solrize(k): self._solrize(v) for k, v in self.dict().items() if v is not None}, + {self._solrize(k): self._solrize(v) for k, v in self..model_dump().items() if v is not None}, doseq=True, ) diff --git a/backend/src/monarch_py/utils/utils.py b/backend/src/monarch_py/utils/utils.py index c4a8e9303..2b96b44f5 100644 --- a/backend/src/monarch_py/utils/utils.py +++ b/backend/src/monarch_py/utils/utils.py @@ -114,7 +114,7 @@ def to_json(obj: Union[ConfiguredBaseModel, Dict, List[ConfiguredBaseModel]], fi elif isinstance(obj, dict): json_value = json.dumps(obj, indent=4) elif isinstance(obj, list): - json_value = json.dumps({"items": [o.dict() for o in obj]}, indent=4) + json_value = json.dumps({"items": [o.model_dump() for o in obj]}, indent=4) if file: with open(file, "w") as f: f.write(json_value) @@ -135,8 +135,8 @@ def to_tsv(obj: ConfiguredBaseModel, file: str) -> str: headers = get_headers_from_obj(obj) rows = [] else: - headers = obj.items[0].dict().keys() - rows = [list(item.dict().values()) for item in obj.items] + headers = obj.items[0].model_dump().keys() + rows = [list(item.model_dump().values()) for item in obj.items] else: console.print(f"\n[bold red]{FMT_INPUT_ERROR_MSG}[/]\n") raise typer.Exit(1) @@ -200,7 +200,7 @@ def to_yaml(obj: ConfiguredBaseModel, file: str): if isinstance(obj, Entity): yaml.dump(obj.model_dump(), fh, indent=4) elif isinstance(obj, Results) or isinstance(obj, HistoPheno) or isinstance(obj, AssociationCountList): - yaml.dump([item.dict() for item in obj.items], fh, indent=4) + yaml.dump([item.model_dump() for item in obj.items], fh, indent=4) else: console.print(f"\n[bold red]{FMT_INPUT_ERROR_MSG}[/]\n") raise typer.Exit(1) diff --git a/backend/tests/fixtures/phenotype_explorer_compare.py b/backend/tests/fixtures/phenotype_explorer_compare.py index 0e0776f24..48dccccd0 100644 --- a/backend/tests/fixtures/phenotype_explorer_compare.py +++ b/backend/tests/fixtures/phenotype_explorer_compare.py @@ -5,8 +5,8 @@ def phenotype_explorer_compare(): return { "subject_termset": { - "MP:0010771": {"id": "MP:0010771", "label": "integument phenotype (MPO)"}, "MP:0002169": {"id": "MP:0002169", "label": "no abnormal phenotype detected (MPO)"}, + "MP:0010771": {"id": "MP:0010771", "label": "integument phenotype (MPO)"}, }, "object_termset": {"HP:0004325": {"id": "HP:0004325", "label": "Decreased body weight (HPO)"}}, "subject_best_matches": { @@ -69,8 +69,8 @@ def phenotype_explorer_compare(): "HP:0004325": { "match_source": "HP:0004325", "match_source_label": "Decreased body weight (HPO)", - "match_target": "MP:0010771", - "match_target_label": "integument phenotype (MPO)", + "match_target": "MP:0002169", + "match_target_label": "no abnormal phenotype detected (MPO)", "score": 1.4431977534690428, "match_subsumer": None, "match_subsumer_label": None, @@ -78,7 +78,7 @@ def phenotype_explorer_compare(): "subject_id": "HP:0004325", "subject_label": None, "subject_source": None, - "object_id": "MP:0010771", + "object_id": "MP:0002169", "object_label": None, "object_source": None, "ancestor_id": "UPHENO:0001003", @@ -87,10 +87,10 @@ def phenotype_explorer_compare(): "object_information_content": None, "subject_information_content": None, "ancestor_information_content": 1.4431977534690428, - "jaccard_similarity": 0.3333333333333333, + "jaccard_similarity": 0.16216216216216217, "cosine_similarity": None, "dice_similarity": None, - "phenodigm_score": 0.6935891563620457, + "phenodigm_score": 0.48376861011243283, }, } }, diff --git a/backend/tests/unit/test_solr_parsers.py b/backend/tests/unit/test_solr_parsers.py index db79a69c4..2875371c2 100644 --- a/backend/tests/unit/test_solr_parsers.py +++ b/backend/tests/unit/test_solr_parsers.py @@ -47,9 +47,7 @@ def test_parse_histopheno(histopheno_response, histopheno, node): histopheno_response["response"]["numFound"] = histopheno_response["response"].pop("num_found") solr_response = SolrQueryResult(**histopheno_response) parsed = parse_histopheno(solr_response, subject_closure=Node(**node).id).model_dump() - assert ( - parsed == histopheno - ), f"Parsed result is not as expected. Difference: {dict_diff(parsed, histopheno)}" + assert parsed == histopheno, f"Parsed result is not as expected. Difference: {dict_diff(parsed, histopheno)}" def test_parse_search(search_response, search): @@ -63,15 +61,11 @@ def test_parse_autocomplete(autocomplete_response, autocomplete): autocomplete_response["response"]["numFound"] = autocomplete_response["response"].pop("num_found") solr_response = SolrQueryResult(**autocomplete_response) parsed = parse_autocomplete(solr_response).model_dump() - assert ( - parsed == autocomplete - ), f"Parsed result is not as expected. Difference: {dict_diff(parsed, autocomplete)}" + assert parsed == autocomplete, f"Parsed result is not as expected. Difference: {dict_diff(parsed, autocomplete)}" def test_parse_mappings(mapping_response, mappings): mapping_response["response"]["numFound"] = mapping_response["response"].pop("num_found") solr_response = SolrQueryResult(**mapping_response) parsed = parse_mappings(solr_response).model_dump() - assert ( - parsed == mappings - ), f"Parsed result is not as expected. Difference: {dict_diff(parsed, mappings)}" + assert parsed == mappings, f"Parsed result is not as expected. Difference: {dict_diff(parsed, mappings)}" diff --git a/backend/tests/unit/test_solr_queries.py b/backend/tests/unit/test_solr_queries.py index 0e6c75a42..800923a37 100644 --- a/backend/tests/unit/test_solr_queries.py +++ b/backend/tests/unit/test_solr_queries.py @@ -33,7 +33,7 @@ def test_build_association_query( direct=direct, facet_fields=facet_fields, facet_queries=facet_queries, - ).dict() + ).model_dump() expected = association_query_direct if direct else association_query_indirect if facet_fields: expected["facet_fields"] = facet_fields @@ -91,30 +91,30 @@ def test_build_association_multiple_objects(): def test_build_association_counts_query(association_counts_query, node): - query = build_association_counts_query(entity=Node(**node).id).dict() + query = build_association_counts_query(entity=Node(**node).id).model_dump() expected = association_counts_query assert compare_dicts(query, expected), f"Query is not as expected. Difference: {dict_diff(query, expected)}" def test_build_histopheno_query(histopheno_query): - query = build_histopheno_query("MONDO:0020121").dict() + query = build_histopheno_query("MONDO:0020121").model_dump() expected = histopheno_query assert compare_dicts(query, expected), f"Query is not as expected. Difference: {dict_diff(query, expected)}" def test_build_search_query(search_query): - query = build_search_query(q="fanconi").dict() + query = build_search_query(q="fanconi").model_dump() expected = search_query assert compare_dicts(query, expected), f"Query is not as expected. Difference: {dict_diff(query, expected)}" def test_build_autocomplete_query(autocomplete_query): - query = build_autocomplete_query(q="fanc").dict() + query = build_autocomplete_query(q="fanc").model_dump() expected = autocomplete_query assert compare_dicts(query, expected), f"Query is not as expected. Difference: {dict_diff(query, expected)}" def test_build_mappings_query(mapping_query): - query = build_mapping_query(entity_id=["MONDO:0020121"]).dict() + query = build_mapping_query(entity_id=["MONDO:0020121"]).model_dump() expected = mapping_query assert compare_dicts(query, expected), f"Query is not as expected. Difference: {dict_diff(query, expected)}" diff --git a/frontend/fixtures/phenotype-explorer-compare.json b/frontend/fixtures/phenotype-explorer-compare.json index a623376e6..da14ac58e 100644 --- a/frontend/fixtures/phenotype-explorer-compare.json +++ b/frontend/fixtures/phenotype-explorer-compare.json @@ -1,12 +1,12 @@ { "subject_termset": { - "MP:0010771": { - "id": "MP:0010771", - "label": "integument phenotype (MPO)" - }, "MP:0002169": { "id": "MP:0002169", "label": "no abnormal phenotype detected (MPO)" + }, + "MP:0010771": { + "id": "MP:0010771", + "label": "integument phenotype (MPO)" } }, "object_termset": { @@ -75,8 +75,8 @@ "HP:0004325": { "match_source": "HP:0004325", "match_source_label": "Decreased body weight (HPO)", - "match_target": "MP:0010771", - "match_target_label": "integument phenotype (MPO)", + "match_target": "MP:0002169", + "match_target_label": "no abnormal phenotype detected (MPO)", "score": 1.4431977534690428, "match_subsumer": null, "match_subsumer_label": null, @@ -84,7 +84,7 @@ "subject_id": "HP:0004325", "subject_label": null, "subject_source": null, - "object_id": "MP:0010771", + "object_id": "MP:0002169", "object_label": null, "object_source": null, "ancestor_id": "UPHENO:0001003", @@ -93,10 +93,10 @@ "object_information_content": null, "subject_information_content": null, "ancestor_information_content": 1.4431977534690428, - "jaccard_similarity": 0.3333333333333333, + "jaccard_similarity": 0.16216216216216217, "cosine_similarity": null, "dice_similarity": null, - "phenodigm_score": 0.6935891563620457 + "phenodigm_score": 0.48376861011243283 } } },