Skip to content
This repository was archived by the owner on Jun 17, 2024. It is now read-only.
Open
Show file tree
Hide file tree
Changes from 4 commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 37 additions & 5 deletions sapientml_preprocess/generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
# See the License for the specific language governing permissions and
# limitations under the License.

import collections
import os
import re
from pathlib import Path
Expand All @@ -31,7 +32,7 @@

logger = setup_logger()

INHIBITED_SYMBOL_PATTERN = re.compile(r"[\{\}\[\]\",:<'\\]+")
INHIBITED_SYMBOL_PATTERN = re.compile(r"[\{\}\[\]\",:<'\\\+]+")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why did you add \+?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was checking if "+" is treated as a symbol in the test string.
I put it back.



template_env = Environment(loader=FileSystemLoader(f"{os.path.dirname(__file__)}/templates"), trim_blocks=True)
Expand Down Expand Up @@ -229,15 +230,46 @@ def generate_code(self, dataset: Dataset, task: Task) -> Tuple[Dataset, Code]:
logger.warning(
f"Symbols that inhibit training and visualization will be removed from column name {str(cols_has_symbols)}."
)
org_df_column = df.columns.values
org_target_column = task.target_columns
df = df.rename(columns=lambda col: remove_symbols(col) if col in cols_has_symbols else col)
task.target_columns = [
remove_symbols(col) if col in cols_has_symbols else col for col in task.target_columns
]
same_column = {k: v for k, v in collections.Counter(list(df.columns.values)).items() if v > 1}
rename_dict = {}
if len(same_column) != 0:
for target in same_column.keys():
rename_dict = {}
rename_target_col = []
df_cols = list(df.columns.values)
i = 1
for col in df_cols:
if target in col:
rename_dict[org_df_column[len(rename_dict)]] = str(col + str(i))
i = i + 1
else:
rename_dict[org_df_column[len(rename_dict)]] = col
df = df.set_axis(list(rename_dict.values()), axis=1)
i = 1
for col in org_target_column:
rename_target_col.append(rename_dict[col])

task.target_columns = rename_target_col

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • I think the code may rename column names which originally don't contain symbols, right? If so, could you keep the names when the column names contain symbols.
  • This code is hard to read and can have unexpected issues. Could you rewrite it?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The review has been applied.

tpl = template_env.get_template("rename_columns.py.jinja")
code.validation += _render(tpl, training=True, test=True, cols_has_symbols=cols_has_symbols)
code.test += _render(tpl, training=True, test=True, cols_has_symbols=cols_has_symbols)
code.train += _render(tpl, training=True, test=False, cols_has_symbols=cols_has_symbols)
code.predict += _render(tpl, training=False, test=True, cols_has_symbols=cols_has_symbols)
code.validation += _render(
tpl, training=True, test=True, cols_has_symbols=cols_has_symbols, rename_dict=rename_dict
)
code.test += _render(
tpl, training=True, test=True, cols_has_symbols=cols_has_symbols, rename_dict=rename_dict
)
code.train += _render(
tpl, training=True, test=False, cols_has_symbols=cols_has_symbols, rename_dict=rename_dict
)
code.predict += _render(
tpl, training=False, test=True, cols_has_symbols=cols_has_symbols, rename_dict=rename_dict
)

# handle list(tuple, dict) value in dataframe.
# in generated scripts, visualisation will be executed before pre-processing such as handle mixed-type.
Expand Down
16 changes: 10 additions & 6 deletions sapientml_preprocess/templates/rename_columns.py.jinja
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
# Remove special symbols that interfere with visualization and model training
import re
cols_has_symbols = {{ cols_has_symbols }}
inhibited_symbol_pattern = re.compile(r"[\{\}\[\]\",:<'\\]+")
rename_dict = {{ rename_dict }}
inhibited_symbol_pattern = re.compile(r"[\{\}\[\]\",:<'\\\+]+")
if len(rename_dict) == 0 :
rename_symbol_cols = {col: inhibited_symbol_pattern.sub("", col) if col in cols_has_symbols else col in cols_has_symbols for col in cols_has_symbols }
else:
rename_symbol_cols = rename_dict
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We don't need show this conditional branch to users. Could you move it to "template's if statement"?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed conditional branching in template if statement.

{% if training %}
rename_symbol_cols = {col: inhibited_symbol_pattern.sub("", col) if col in cols_has_symbols else col in cols_has_symbols for col in cols_has_symbols }
rename_symbol_cols = {v: k for k, v in rename_symbol_cols.items()}
train_dataset = train_dataset.rename(columns=lambda col: inhibited_symbol_pattern.sub("", col) if col in cols_has_symbols else col)
train_dataset = train_dataset.rename(columns=rename_symbol_cols)
{% endif %}
{% if test %}
test_dataset = test_dataset.rename(columns=lambda col: inhibited_symbol_pattern.sub("", col) if col in cols_has_symbols else col)
{% endif %}
test_dataset = test_dataset.rename(columns=rename_symbol_cols)
{% endif %}
rename_symbol_cols = {v: k for k, v in rename_symbol_cols.items()}