Skip to content
This repository has been archived by the owner on Nov 29, 2023. It is now read-only.

Create Simple-ML stub files #292

Closed
6 tasks done
lars-reimann opened this issue Nov 22, 2021 · 4 comments · Fixed by #326
Closed
6 tasks done

Create Simple-ML stub files #292

lars-reimann opened this issue Nov 22, 2021 · 4 comments · Fixed by #326
Assignees
Labels
enhancement 💡 New feature or request

Comments

@lars-reimann
Copy link
Member

lars-reimann commented Nov 22, 2021

Is your feature request related to a problem? Please describe

In addition to the Python files that are generated in #288, Simple-ML stub files should be generated.

Stub files syntax

package simpleml.model.regression

import simpleml.dataset.Dataset
import simpleml.model.Estimator

open class Lasso constructor(regularizationStrength: Float or 0.5) sub Estimator {
    attr regularizationStrength: Float

    override fun fit(features: Dataset, target: Dataset) -> trainedModel: TrainedLasso
}

fun println(obj: Any)

Let's go through this step by step:

package simpleml.model.regression

Package declaration as in Java. Corresponds to a Python file simpleml/model/regression.py.


import simpleml.dataset.Dataset
import simpleml.model.Estimator

Imports like in Java. First we import a declaration called Dataset from the simpleml.dataset package then a declaration called Estimator from simpleml.model.


open class Lasso

Declares that a class called Lasso exists in this package (simpleml.model.regression) and that it is open for extension. Opposite to Java classes are final by default and need to be marked explicitly as open so other classes can inherit from them.


constructor(regularizationStrength: Float or 0.5)

The class has a constructor (__init__ function in Python) that take on parameter called regularizationStrength of type Float and has a default value of 0.5. The self parameter from Python must not be added and is implicitly assumed.


sub Estimator

The class is a subclass of Estimator.


attr regularizationStrength: Float

The class has an attribute (instance variable) called regularizationStrength with type Float.


override fun fit(features: Dataset, target: Dataset) -> trainedModel: TrainedLasso

The class overrides the function called fit it inherits from Estimator. It takes two parameters called features and target, both of which have type Dataset and produces one result called trainedModel that has type TrainedLasso.


fun println(obj: Any?)

A global function called println that takes one parameter that is an instance of any type or null (indicated by the question mark). null corresponds to None in Python and the question mark to Optional.

Limitations

Not all the information specified above is available yet. Here's a list of possible problems and their resolution:

  • Datatypes might not be known. In that case use Any? as the type.
  • Imports can be challenging since we need the qualified name of the datatypes.
  • Subclassing is challenging (open, sub, override). In a first iteration we may ignore subclassing and omit all this information. The work necessary to implement this is tracked separately in Handle subclassing properly in stub files #293.
  • Attributes are not currently known. However, in scikit-learn all parameters of a constructor are also available as attributes on the class under the same name.

Tasks

  • Package declaration
  • Class declaration
  • Constructors of classes
  • Attributes of classes
  • Methods of classes
  • Global functions
@paul0314
Copy link
Contributor

paul0314 commented Dec 1, 2021

Should two separate directories be generated for the adapter and stub files or should they be placed in the same one?

Example:

api-editor_inferredAPI/adapter/simpleml/model/test.py
api-editor_inferredAPI/stub/simpleml/model/test.pyi

or:
api-editor_inferredAPI/simpleml/model/test.py
api-editor_inferredAPI/simpleml/model/test.pyi

I assume the second approach of placing them in the same directories is the desired solution, please correct me if I got that wrong.

@lars-reimann
Copy link
Member Author

api-editor_inferredAPI/adapter/simpleml/model/test.py
api-editor_inferredAPI/stub/simpleml/model/test.pyi

This, only with an extension of .stub.simpleml. It's not the same thing as the Python stubs.

@paul0314
Copy link
Contributor

paul0314 commented Dec 1, 2021

What is the expected output for a class without a constructor?

@lars-reimann
Copy link
Member Author

lars-reimann commented Dec 1, 2021

open class Lasso constructor() sub Estimator, i.e. a constructor without arguments.

Reason: There's always a default parameterless constructor in Python.

Repository owner moved this from To do to ✔️ Done in Safe-DS Dec 5, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
enhancement 💡 New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants