99from curies import Converter
1010from linkml_runtime .linkml_model .meta import SlotDefinition
1111from linkml_runtime .utils .schemaview import SchemaView
12- from pandas import DataFrame
12+ from pandas import DataFrame , Series
1313from rdflib import BNode , Graph , Literal , Node , URIRef
1414from rdflib .namespace import RDF , RDFS , XSD
1515from typing_extensions import override
3636__all__ = ["MappingSetRDFConverter" ]
3737
3838TRIPLE = tuple [Node , Node , Node ]
39+ DICT_OR_SERIES = Union [Dict [str , Any ], Series ]
3940MAPPINGS_IRI = URIRef (MAPPINGS , SSSOM_URI_PREFIX )
4041EXTENSION_DEFINITION_IRI = URIRef (EXTENSION_DEFINITIONS , SSSOM_URI_PREFIX )
4142
@@ -563,7 +564,7 @@ def _multivalue_from_rdf(self, value: Any, slot_name: str, dest: Dict[str, Any])
563564 # Conversion to RDF
564565 #
565566
566- def dict_to_rdf (self , g : Graph , obj : Dict [ str , Any ] ) -> Node :
567+ def dict_to_rdf (self , g : Graph , obj : DICT_OR_SERIES ) -> Node :
567568 """Export a SSSOM object to a RDF graph.
568569
569570 :param g: The graph to export the object to.
@@ -576,10 +577,11 @@ def dict_to_rdf(self, g: Graph, obj: Dict[str, Any]) -> Node:
576577 g .add (cast (TRIPLE , [subject , RDF .type , self .object_uri ]))
577578
578579 for k , v in obj .items ():
579- if self ._process_slot (g , subject , k , v ):
580+ key = str (k )
581+ if self ._process_slot (g , subject , key , v ):
580582 continue
581583
582- slot = self .slots_by_name .get (k )
584+ slot = self .slots_by_name .get (key )
583585 if slot is not None :
584586 pred = self ._get_slot_uri (slot )
585587 converter = self ._get_value_converter (slot )
@@ -591,8 +593,8 @@ def dict_to_rdf(self, g: Graph, obj: Dict[str, Any]) -> Node:
591593 else :
592594 if not self ._is_empty (v ):
593595 g .add (cast (TRIPLE , [subject , pred , converter .to_rdf (v )]))
594- elif not self ._extension_to_rdf (g , subject , k , v ):
595- logging .warning (f"Ignoring unexpected { k } ={ v } slot" )
596+ elif not self ._extension_to_rdf (g , subject , key , v ):
597+ logging .warning (f"Ignoring unexpected { key } ={ v } slot" )
596598
597599 return subject
598600
@@ -611,7 +613,7 @@ def _is_empty(self, value: Any) -> bool:
611613 """
612614 return value is None or (hasattr (value , "__len__" ) and len (value ) == 0 )
613615
614- def _init_dict_to_rdf (self , g : Graph , obj : Dict [ str , Any ] ) -> Node :
616+ def _init_dict_to_rdf (self , g : Graph , obj : DICT_OR_SERIES ) -> Node :
615617 """Create the root node representing a SSSOM object.
616618
617619 Subclasses should override this method to customize the way
@@ -897,7 +899,7 @@ def msdf_to_rdf(
897899 return g
898900
899901 @override
900- def _init_dict_to_rdf (self , g : Graph , obj : Dict [ str , Any ] ) -> Node :
902+ def _init_dict_to_rdf (self , g : Graph , obj : DICT_OR_SERIES ) -> Node :
901903 if MAPPING_SET_ID in obj :
902904 return URIRef (obj [MAPPING_SET_ID ])
903905 else :
@@ -926,7 +928,7 @@ def _process_slot(self, g: Graph, subject: Node, name: str, value: Any) -> bool:
926928 return done
927929
928930 def _mapping_to_rdf (
929- self , g : Graph , subject : Node , mapping : Dict [ str , Any ] , hydrate : bool
931+ self , g : Graph , subject : Node , mapping : DICT_OR_SERIES , hydrate : bool
930932 ) -> None :
931933 mapping_node = self .mapping_converter .dict_to_rdf (g , mapping )
932934 g .add (cast (TRIPLE , [subject , MAPPINGS_IRI , mapping_node ]))
@@ -979,7 +981,7 @@ def _multivalue_from_rdf(self, value: Any, slot_name: str, dest: Dict[str, Any])
979981 dest [slot_name ] = dest [slot_name ] + "|" + value
980982
981983 @override
982- def _init_dict_to_rdf (self , g : Graph , obj : Dict [ str , Any ] ) -> Node :
984+ def _init_dict_to_rdf (self , g : Graph , obj : DICT_OR_SERIES ) -> Node :
983985 if RECORD_ID in obj :
984986 return URIRef (self .ccp ().expand (obj [RECORD_ID ]))
985987 else :
0 commit comments