Skip to content

Sourcery Starbot ⭐ refactored MAli82/py2sas#1

Open
SourceryAI wants to merge 1 commit into
MAli82:masterfrom
SourceryAI:master
Open

Sourcery Starbot ⭐ refactored MAli82/py2sas#1
SourceryAI wants to merge 1 commit into
MAli82:masterfrom
SourceryAI:master

Conversation

@SourceryAI
Copy link
Copy Markdown

Thanks for starring sourcery-ai/sourcery ✨ 🌟 ✨

Here's your pull request refactoring your most popular Python repo.

If you want Sourcery to refactor all your Python repos and incoming pull requests install our bot.

Review changes via command line

To manually merge these changes, make sure you're on the master branch, then run:

git fetch https://github.com/sourcery-ai-bot/py2sas master
git merge --ff-only FETCH_HEAD
git reset HEAD^

Comment thread main.py
Comment on lines -5 to +16
usageString = "Usage: python3 %s <infile> <outfile> [out_var_name]" % sys.argv[0]
usageString = f"Usage: python3 {sys.argv[0]} <infile> <outfile> [out_var_name]"

# Basic checks
if len(sys.argv) < 3 or len(sys.argv) > 4:
print("Wrong parameters! %s" % usageString)
print(f"Wrong parameters! {usageString}")
sys.exit(1)

# Load input and output file names
in_file = sys.argv[1]
out_file = sys.argv[2]

out_var_name = "P_TARGET"
if len(sys.argv) == 4:
out_var_name = sys.argv[3]

out_var_name = sys.argv[3] if len(sys.argv) == 4 else "P_TARGET"
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Lines 5-19 refactored with the following changes:

Comment thread pyml2ds/pyml2ds.py
Comment on lines -32 to +39
comp_types = ["xgboost.sklearn.XGBModel", "lightgbm.LGBMModel", "lightgbm.basic.Booster", "sklearn.ensemble.RandomForestClassifier", "GBM.pmml file"]

parser = None
if xgboost and isinstance(model, xgboost.sklearn.XGBModel):
if model.booster not in ['gbtree', 'dart']:
raise RuntimeError("Model is xgboost. Unsupported booster type: %s. Supported types are: %s" % (model.booster, ', '.join(comp_types)))
comp_types = ["xgboost.sklearn.XGBModel", "lightgbm.LGBMModel", "lightgbm.basic.Booster", "sklearn.ensemble.RandomForestClassifier", "GBM.pmml file"]

raise RuntimeError(
f"Model is xgboost. Unsupported booster type: {model.booster}. Supported types are: {', '.join(comp_types)}"
)
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Function _check_type refactored with the following changes:

Comment thread pyml2ds/basic/tree.py
Comment on lines -100 to +104
output = unicodedata.normalize('NFKD', input).encode('ASCII', 'ignore').decode()
else:
# On Python < 3.0.0
if type(input) == str:
input = unicode(input, 'ISO-8859-1')
output = unicodedata.normalize('NFKD', input).encode('ASCII', 'ignore')

return output
return unicodedata.normalize('NFKD', input).encode('ASCII', 'ignore').decode()
# On Python < 3.0.0
if type(input) == str:
input = unicode(input, 'ISO-8859-1')
return unicodedata.normalize('NFKD', input).encode('ASCII', 'ignore')
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Function TreeParser._remove_diacritic refactored with the following changes:

Comment thread pyml2ds/basic/tree.py
Comment on lines -121 to +118
if node is not None:
self._node = node
else:
self._node = self._root

self._node = node if node is not None else self._root
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Function TreeParser.parse_node refactored with the following changes:


def _aggregate(self, booster_count):
return "treeValue = sum({});\n".format(', '.join(["treeValue%d" % i for i in range(booster_count)]))
return f"""treeValue = sum({', '.join(["treeValue%d" % i for i in range(booster_count)])});\n"""
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Function EnsembleParser._aggregate refactored with the following changes:

Comment on lines -89 to +88
return "treeValue = sum({}) / {};\n".format(', '.join(["treeValue%d" % i for i in range(booster_count)]), booster_count)
return f"""treeValue = sum({', '.join(["treeValue%d" % i for i in range(booster_count)])}) / {booster_count};\n"""
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Function ForestParser._aggregate refactored with the following changes:

Comment on lines -61 to +66

self._dump = booster.dump_model()
if self._dump['objective'] != 'binary sigmoid:1':
raise Exception("Unfortunately only binary sigmoid objective function is supported right now. Your objective is %s. Please, open an issue at https://gitlab.sas.com/from-russia-with-love/lgb2sas." % self.dump['objective'])
raise Exception(
f"Unfortunately only binary sigmoid objective function is supported right now. Your objective is {self.dump['objective']}. Please, open an issue at https://gitlab.sas.com/from-russia-with-love/lgb2sas."
)
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Function LightgbmParser.__init__ refactored with the following changes:

Comment thread pyml2ds/connectors/ensembles/pmml.py
Comment on lines -73 to +81
raise RuntimeError("Lxml is not installed. Lxml is needed to parse xml files.")
raise RuntimeError("Lxml is not installed. Lxml is needed to parse xml files.")
objectify.deannotate(tree_root, cleanup_namespaces=True)

self._forest = tree_root.find('MiningModel/Segmentation')[0].find('MiningModel')

rescaleConstant = self._forest.find('Targets/Target').get('rescaleConstant')
self.out_transform = "1 / (1 + exp(-{}))".format("({0} + " + "{})".format(rescaleConstant))
self.out_transform = "1 / (1 + exp(-{}))".format(
"({0} + " + f"{rescaleConstant})"
)
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Function PmmlParser.__init__ refactored with the following changes:

Comment on lines -77 to +88

self._dump = booster.get_dump(dump_format='json')
self._objective = objective
self._features = booster.feature_names

if objective == 'binary:logistic':
self.out_transform = "1 / (1 + exp(-{0}))"
elif objective == 'reg:linear':
pass
else:
raise Exception("Unsupported objective: %s. Supported objectives are: binary:logistic and reg:linear." % objective)
elif objective != 'reg:linear':
raise Exception(
f"Unsupported objective: {objective}. Supported objectives are: binary:logistic and reg:linear."
)

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Function XgbParser.__init__ refactored with the following changes:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant