New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
add: Make function_name parametrized #166
add: Make function_name parametrized #166
Conversation
* Currently `m2cgen` generates a module in various languages that has a "score"/"Score" function/method. This is not always desirable, as many of the trained modules that are to be exported may provide their prediction via API functions with different names (such as `predict`). * This commit adds a way of specifying the name of the function both via the CLI and in the exporters (that is, in the `export_to_` funcitons) by specifying the `function_name` option/parameter while keeping the default set to "score"/"Score" for backwards compatilibity. Signed-off-by: mr.Shu <mr@shu.io>
* Add `function_name` to mock CLI args to be used in the tests. Signed-off-by: mr.Shu <mr@shu.io>
@mrshu Thanks a lot for submitting this PR together with such a detailed description! |
Thank you for your kind words @izeigerman! Right off the bat I'd like to tell you that the way the I am therefore looking forward to your review and any further discussion! |
Also, I am not completely sold on calling it Thanks! |
To be honest, I don't remember I was saying this 😃
I'm not sure that it is worthy of further consideration. As we are still in alpha phase, we are free to change a lot without any deprecation warnings and worrying about the backward compatibility. Especially for just a naming conventions. Line 20 in d28cb00
Please add a test for your PR. You may use the following tests as examples: Lines 89 to 126 in d28cb00
|
* Add a test for the `--function_name` CLI option. Signed-off-by: mr.Shu <mr@shu.io>
Thanks a lot for your comments @StrikerRUS! I did add a test for the CLI part of the PR -- if you think it would be worth it, I am happy to add some for the "API" part as well. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@mrshu Thanks a lot for your PR!
Please check my comments below.
return _export(model, interpreter) | ||
|
||
|
||
def export_to_c_sharp(model, namespace="ML", class_name="Model", indent=4): | ||
def export_to_c_sharp(model, namespace="ML", class_name="Model", indent=4, | ||
function_name="Score"): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
< Here and in all other places >
I believe it can be score
for the consistency. Refer to #166 (comment)
I'm not sure that it is worthy of further consideration. As we are still in alpha phase, we are free to change a lot without any deprecation warnings and worrying about the backward compatibility. Especially for just a naming conventions.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hey @StrikerRUS shouldn't we at least comply with basic code style principles of a target language? Like in this case method names are capitalized in C#. I believe this basic logic is worth keeping.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I really do not have a strong opinion here, but since C# seems to be using PascalCasing by definition, I would vote for being consistent with that.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
To be honest, I don't have a strong opinion on keeping code style for default values too. I just proposed the way which is the easiest to maintain 🙂 .
And then we'll need to apply conditional default values for the CLI part: #166 (comment).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@StrikerRUS All right, I went ahead and implemented the "default values for the CLI" part such that they directly use the default value of the function_name
keyword argument.
It relies on inspecting the export
function (the inspect module introduced in Python 3.5 so that should not be an issue) and might therefore feel a bit hacky, but it allows us to specify the default only once (namely in the keyword argument) and be done with it -- it should automatically down-propagate to the CLI options as well.
I also added a test which should give us a greater confidence that this works.
If you do not like this approach, I am happy to revert that particular commit and just leave it as it was.
Thanks again!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@mrshu Thanks a lot for implementing conditional default values! I like your approach very much! Just please add a comment about that default value is conditional and will be set later based on the export
function signature.
parser.add_argument(
"--function_name", "-fn", dest="function_name", type=str,
default=None, # some comment here
help="Name of the function in the generated code.")
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks @StrikerRUS , I just did that.
Please feel free to point out a simpler version of the comment -- I may have overcomplicated it quite a bit.
@StrikerRUS, thanks for reviewing this PR!
Oops, sorry. I confused 2 things 🤦♂ |
* Make sure function_name is actually used in the interpreters. Signed-off-by: mr.Shu <mr@shu.io>
* Make sure that the `function_name` docstring ends with a dot rather than the default value. * Add dot to various CLI argument's help messages. Signed-off-by: mr.Shu <mr@shu.io>
@StrikerRUS Thanks for your feedback! I believe I've addressed most of the things you've pointed out except for the C# one where I'd like to wait for your opinion. If you still see something worth updating here, feel free to let me know -- I'll gladly update it. Thanks! |
* Make sure the default function_name (when not provided via arguments) gets backpropagated from the actual function definitions (in other words, make sure that the keyword arguments are respected when the function_name is not specified via CLI). Signed-off-by: mr.Shu <mr@shu.io>
* Add a comment on function_name being conditionally set later. Signed-off-by: mr.Shu <mr@shu.io>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@mrshu Thank you very much for your valuable contribution! LGTM!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Great stuff, thank you 👍
Hello everyone,
First of all, thanks a ton for putting this tool/library together -- especially in resource-stranded environments, it does have a potential to literally save lives!
One small problem I was fighting with while using it was the
score
function it uses in the generated modules. When they are used as drop-in replacements for trained models, usingscore
is a bit strange, as the API generally provides function likepredict
orpredict_proba
. It would therefore be of great help to me if this name could be dynamically changed and I would not have to do so manually.Please do let me know if something like this sounds like a sensible addition. I'd be happy to update the code so that it reflect your vision, so please feel free to just let me know whenever that may be the case.
Thanks!
Currently
m2cgen
generates a module in various languages that has a"score"/"Score" function/method. This is not always desirable, as many
of the trained modules that are to be exported may provide their
prediction via API functions with different names (such as
predict
).This commit adds a way of specifying the name of the function both via
the CLI and in the exporters (that is, in the
export_to_
funcitons)by specifying the
function_name
option/parameter while keeping thedefault set to "score"/"Score" for backwards compatilibity.
Signed-off-by: mr.Shu mr@shu.io