1010import warnings
1111from abc import ABC , abstractmethod
1212from collections .abc import Iterable , Mapping , Sequence
13+ from itertools import cycle , islice
1314
1415import numpy as np
1516import pandas as pd
2122import khiops .core as kh
2223import khiops .core .internals .filesystems as fs
2324from khiops .core .dictionary import VariableBlock
24- from khiops .core .internals .common import is_dict_like , is_list_like , type_error_message
25+ from khiops .core .internals .common import (
26+ deprecation_message ,
27+ is_dict_like ,
28+ is_list_like ,
29+ type_error_message ,
30+ )
2531
2632# Disable PEP8 variable names because of scikit-learn X,y conventions
2733# To capture invalid-names other than X,y run:
@@ -171,6 +177,48 @@ def _check_multitable_spec(ds_spec):
171177 )
172178
173179
180+ def _table_name_of_path (table_path ):
181+ return table_path .split ("/" )[- 1 ]
182+
183+
184+ def _upgrade_mapping_spec (X ):
185+ assert is_dict_like (X )
186+ new_X = {}
187+ new_X ["additional_data_tables" ] = {}
188+ for table_name , table_data in X ["tables" ].items ():
189+ table_df , table_key = table_data
190+ if not is_list_like (table_key ):
191+ table_key = [table_key ]
192+ if table_name == X ["main_table" ]:
193+ new_X ["main_table" ] = (table_df , table_key )
194+ else :
195+ table_path = [table_name ]
196+ is_entity = False
197+
198+ # Cycle twice on the relations to get all transitive relation, like:
199+ # - current table name N
200+ # - main table name N1
201+ # - and relations: (N1, N2), (N2, N3), (N3, N)
202+ # the data-path must be N2/N3/N
203+ for relation in islice (cycle (X ["relations" ]), 2 * len (X ["relations" ])):
204+ left , right = relation [:2 ]
205+ if len (relation ) == 3 and right == table_name :
206+ is_entity = relation [2 ]
207+ if (
208+ left != X ["main_table" ]
209+ and left not in table_path
210+ and right in table_path
211+ ):
212+ table_path .insert (0 , left )
213+ table_path = "/" .join (table_path )
214+ if is_entity :
215+ table_data = (table_df , table_key , is_entity )
216+ else :
217+ table_data = (table_df , table_key )
218+ new_X ["additional_data_tables" ][table_path ] = table_data
219+ return new_X
220+
221+
174222def get_khiops_type (numpy_type ):
175223 """Translates a numpy dtype to a Khiops dictionary type
176224
@@ -417,14 +465,26 @@ def _check_input_sequence(self, X, key=None):
417465 # Check the key for the main_table (it is the same for the others)
418466 _check_table_key ("main_table" , key )
419467
420- def _table_name_of_path (self , table_path ):
421- # TODO: Add >= 128-character truncation and indexing scheme
422- return table_path .split ("/" )[- 1 ]
423-
424468 def _init_tables_from_mapping (self , X ):
425469 """Initializes the table spec from a dict-like 'X'"""
426470 assert is_dict_like (X ), "'X' must be dict-like"
427471
472+ # Detect if deprecated mapping specification syntax is used;
473+ # if so, issue deprecation warning and transform it to the new syntax
474+ if {"tables" , "relations" }.issubset (X .keys ()):
475+ warnings .warn (
476+ deprecation_message (
477+ "This multi-table dataset specification format" ,
478+ "11.0.1" ,
479+ replacement = (
480+ "the new data-path-based format, as documented in "
481+ ":doc:`multi_table_primer`."
482+ ),
483+ quote = False ,
484+ )
485+ )
486+ X = _upgrade_mapping_spec (X )
487+
428488 # Check the input mapping
429489 check_dataset_spec (X )
430490
@@ -443,7 +503,7 @@ def _init_tables_from_mapping(self, X):
443503 if "additional_data_tables" in X :
444504 for table_path , table_spec in X ["additional_data_tables" ].items ():
445505 table_source , table_key = table_spec [:2 ]
446- table_name = self . _table_name_of_path (table_path )
506+ table_name = _table_name_of_path (table_path )
447507 table = PandasTable (
448508 table_name ,
449509 table_source ,
@@ -460,7 +520,7 @@ def _init_tables_from_mapping(self, X):
460520 parent_table_name = self .main_table .name
461521 else :
462522 table_path_fragments = table_path .split ("/" )
463- parent_table_name = self . _table_name_of_path (
523+ parent_table_name = _table_name_of_path (
464524 "/" .join (table_path_fragments [:- 1 ])
465525 )
466526 self .relations .append (
0 commit comments