Skip to content

Commit

Permalink
Adjusting how log is produced
Browse files Browse the repository at this point in the history
  • Loading branch information
caufieldjh committed Sep 16, 2024
1 parent 3852c2f commit 6384d51
Showing 1 changed file with 33 additions and 8 deletions.
41 changes: 33 additions & 8 deletions src/kg_bioportal/transformer.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@
from kgx.transformer import Transformer as KGXTransformer

from kg_bioportal.downloader import ONTOLOGY_LIST_NAME
from kg_bioportal.robot_utils import (initialize_robot, robot_convert,
robot_relax)
from kg_bioportal.robot_utils import initialize_robot, robot_convert, robot_relax

# TODO: Don't repeat steps if the products already exist
# TODO: Fix KGX hijacking logging
Expand Down Expand Up @@ -105,11 +104,15 @@ def transform_all(self) -> None:
else:
logging.info(f"Transformed {filepath}.")
status = True
if status == False:
strstatus = "Failed"
else:
strstatus = "OK"
onto_log[ontology_name] = {
"status": status,
"nodecount": nodecount,
"edgecount": edgecount,
}
"status": strstatus,
"nodecount": nodecount,
"edgecount": edgecount,
}

# Write total stats to a yaml
logging.info("Writing total stats to total_stats.yaml.")
Expand All @@ -122,9 +125,20 @@ def transform_all(self) -> None:
f.write("totalcount: " + str(success_count) + "\n")

# Dump onto_log to a yaml
# Rearrange it a bit first
logging.info("Writing ontology stats to onto_stats.yaml.")
onto_stats_list = []
for onto in onto_log:
onto_stats_list.append(
{
"id": onto,
"status": onto_log[onto]["status"],
"nodecount": onto_log[onto]["nodecount"],
"edgecount": onto_log[onto]["edgecount"],
}
)
with open(os.path.join(self.output_dir, "onto_stats.yaml"), "w") as of:
yaml.dump({"ontologies": onto_log}, of)
yaml.dump({"ontologies": onto_stats_list}, of)

return None

Expand All @@ -141,6 +155,8 @@ def transform(self, ontology_path: str) -> Tuple[bool, int, int]:
Number of edges in the ontology.
"""
status = False
nodecount = 0
edgecount = 0

ontology_name = (os.path.relpath(ontology_path, self.input_dir)).split(os.sep)[
0
Expand Down Expand Up @@ -214,10 +230,19 @@ def transform(self, ontology_path: str) -> Tuple[bool, int, int]:
f"Nodes and edges written to {nodefilename} and {edgefilename}."
)
status = True

# Get length of nodefile
with open(nodefilename, "r") as f:
nodecount = len(f.readlines()) - 1

# Get length of edgefile
with open(edgefilename, "r") as f:
edgecount = len(f.readlines()) - 1

except Exception as e:
logging.error(
f"Error transforming {ontology_name} to KGX nodes and edges: {e}"
)
status = False

return status, 0, 0
return status, nodecount, edgecount

0 comments on commit 6384d51

Please sign in to comment.