Skip to content

Commit 26abe8b

Browse files
author
Eloi Massoulié
committed
Better indentation on column lists, and rough test
1 parent d2c1ef1 commit 26abe8b

2 files changed

Lines changed: 59 additions & 42 deletions

File tree

skrub/_table_vectorizer.py

Lines changed: 43 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -187,43 +187,32 @@ def _get_preprocessors(
187187
def _list_transformations(estimator):
188188
message = ""
189189

190-
try:
191-
post = estimator._postprocessors
192-
except AttributeError:
193-
post = []
190+
# if isinstance(estimator, TableVectorizer):
191+
# post = estimator._postprocessors
192+
# else:
193+
# post = []
194194

195195
for step in estimator._pipeline.named_steps:
196196
if step == "checkinputdataframe":
197197
continue
198198
transformer = estimator._pipeline.named_steps[step]
199-
if transformer not in post:
200-
match transformer.transformer:
201-
case DropUninformative():
202-
dropped = set(transformer.all_inputs_) - set(
203-
transformer.all_outputs_
204-
)
205-
if not dropped:
206-
message += "DropUninformative - " + "\n"
207-
message += f"Dropped columns {dropped}" + "\n"
208-
message += f"Used inputs: {transformer.used_inputs_}" + "\n"
209-
case ToFloat():
210-
message += "ToFloat - " + "\n"
211-
message += (
212-
f"Columns transformed to float: {transformer.used_inputs_}"
213-
+ "\n"
214-
)
215-
case ToDatetime():
216-
message += "ToDatetime - " + "\n"
217-
message += (
218-
f"Columns transformed to datetime: {transformer.used_inputs_}"
219-
+ "\n"
220-
)
221-
case CleanNullStrings():
222-
message += "CleanNullStrings - " + "\n"
223-
message += (
224-
f"Columns with standardized nulls: {transformer.used_inputs_}"
225-
+ "\n"
226-
)
199+
match transformer.transformer:
200+
case DropUninformative():
201+
dropped = set(transformer.all_inputs_) - set(transformer.all_outputs_)
202+
if dropped:
203+
message += "Columns dropped by DropUninformative:" + "\n\t"
204+
message += "\n\t".join(limit_cols(dropped))
205+
message += f"Used inputs: {transformer.used_inputs_}" + "\n"
206+
# case ToFloat():
207+
# if transformer not in post:
208+
# message += "Columns transformed to float:" + "\n"
209+
# message += "\n\t".join(transformer.used_inputs_)
210+
case ToDatetime():
211+
message += "Columns transformed to datetime:" + "\n\t"
212+
message += "\n\t".join(limit_cols(transformer.used_inputs_)) + "\n"
213+
case CleanNullStrings():
214+
message += "Columns with standardized nulls:" + "\n\t"
215+
message += "\n\t".join(limit_cols(transformer.used_inputs_)) + "\n"
227216
return message
228217

229218

@@ -1214,6 +1203,11 @@ def get_feature_names_out(self, input_features=None):
12141203
return np.asarray(self.all_outputs_)
12151204

12161205
def list_transformations(self):
1206+
"""Returns a string reporting the transformations applied by the table
1207+
vectorizer, and the columns they are each applied to. This covers every
1208+
preprocessing step, each of the `numeric`, `datetime`, `low cardinality`
1209+
and `high cardinality` transformations and any specific transformer.
1210+
"""
12171211
preprocessing_transformations = _list_transformations(self)
12181212
vectorize_transformations = ""
12191213
specific_transformations = ""
@@ -1222,23 +1216,24 @@ def list_transformations(self):
12221216
specific = all_transformers.pop("specific")
12231217

12241218
for transformer_type, transformer_cols in all_transformers.items():
1225-
if transformer_cols != []:
1219+
if transformer_cols:
12261220
vectorize_transformations += (
12271221
f"{transformer_type} transformer is "
1228-
f"{repr_format(getattr(self, transformer_type))} "
1229-
f"and was applied to {transformer_cols}." + "\n"
1222+
f"{getattr(self, transformer_type).__class__.__name__} "
1223+
f"and was applied to:" + "\n\t"
1224+
)
1225+
vectorize_transformations += (
1226+
"\n\t".join(limit_cols(transformer_cols)) + "\n"
12301227
)
12311228
else:
12321229
vectorize_transformations += (
1233-
f"{transformer_type} transformer is "
1234-
f"{repr_format(getattr(self, transformer_type))} "
1235-
"and was applied to nothing." + "\n"
1230+
f"No {transformer_type} columns have been detected." + "\n"
12361231
)
12371232

1238-
if self.specific_transformers != ():
1233+
if self.specific_transformers:
12391234
for t in self.specific_transformers:
12401235
specific_transformations += (
1241-
f"specific transformer {t} was applied to {specific}"
1236+
f"specific transformer {t} was applied to {limit_cols(specific)}"
12421237
)
12431238

12441239
return (
@@ -1254,3 +1249,11 @@ def repr_format(s):
12541249
without_spaces = repr(s).replace(" ", "")
12551250
without_lineskip = without_spaces.replace("\n", "")
12561251
return without_lineskip
1252+
1253+
1254+
def limit_cols(col_names, max_cols=10):
1255+
if len(col_names) > max_cols:
1256+
list_cols = list(col_names)[:max_cols] + ["..."]
1257+
else:
1258+
list_cols = list(col_names)
1259+
return list_cols

skrub/tests/test_table_vectorizer.py

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1285,8 +1285,22 @@ def test_list_transformations(df_module):
12851285

12861286
vectorizer = TableVectorizer()
12871287
_ = vectorizer.fit_transform(df)
1288-
_ = vectorizer.list_transformations()
1288+
vectorizer_output = vectorizer.list_transformations()
1289+
assert vectorizer_output == (
1290+
"Columns with standardized nulls:\n\tuid\n\tcities\nColumns "
1291+
"transformed to datetime:\n\tstart\n\tend\n\n\nnumeric transformer"
1292+
" is PassThrough and was applied to:\n\tencoded_cities\n\tmetric_0"
1293+
"\n\tmetric_1\n\tmetric_2\n\tmetric_3\ndatetime transformer is "
1294+
"DatetimeEncoder and was applied to:\n\tstart\n\tend\nlow_cardinality"
1295+
" transformer is OneHotEncoder and was applied to:\n\tcities\n"
1296+
"high_cardinality transformer is StringEncoder and was applied "
1297+
"to:\n\tuid\n\n\n"
1298+
)
12891299

12901300
vectorizer = Cleaner()
12911301
_ = vectorizer.fit_transform(df)
1292-
_ = vectorizer.list_transformations()
1302+
cleaner_output = vectorizer.list_transformations()
1303+
assert (
1304+
cleaner_output == "Columns with standardized nulls:\n\tuid\n\tcities\nColumns "
1305+
"transformed to datetime:\n\tstart\n\tend\n"
1306+
)

0 commit comments

Comments
 (0)