Skip to content

Commit

Permalink
Merge pull request #10 from discovery-unicamp/adding_options
Browse files Browse the repository at this point in the history
Adding validation options
  • Loading branch information
otavioon authored Aug 7, 2023
2 parents 84a4873 + 0c8ccd9 commit 7c98bd8
Show file tree
Hide file tree
Showing 5 changed files with 118 additions and 26 deletions.
6 changes: 3 additions & 3 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"

[project]
name = "librep"
version = "0.0.2-dev"
version = "0.0.3-dev"
description = "HIAAC-M4 representation learning framework"
readme = "README.md"
authors = [{name = "Otavio Napoli", email = "otavio.napoli@gmail.com"}]
Expand Down Expand Up @@ -57,5 +57,5 @@ deep-learning = [
]

[project.urls]
"Homepage" = "https://github.com/otavioon/hiaac-librep"
"Bug Tracker" = "https://github.com/otavioon/hiaac-librep/issues"
"Homepage" = "https://github.com/discovery-unicamp/hiaac-librep"
"Bug Tracker" = "https://github.com/discovery-unicamp/hiaac-librep/issues"
20 changes: 20 additions & 0 deletions src/librep/base/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
from librep.base.estimator import Estimator
from librep.base.transform import Transform, InvertibleTransform
from librep.base.evaluators import (
SupervisedEvaluator,
UnsupervisedEvaluator,
CustomSimpleEvaluator,
CustomMultiEvaluator,
Evaluators,
)

__all__ = [
"Estimator",
"Transform",
"InvertibleTransform",
"SupervisedEvaluator",
"UnsupervisedEvaluator",
"CustomSimpleEvaluator",
"CustomMultiEvaluator",
"Evaluators",
]
61 changes: 43 additions & 18 deletions src/librep/base/estimator.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,38 @@
# Wrap around scikit learn base API
# Borrowed from Sklearn API

class Estimator(Parametrizable):
"""An object which manages the estimation and decoding of a model.
"""

def fit(self, X: ArrayLike, y: ArrayLike = None, **estimator_params) -> 'Estimator':
class Estimator(Parametrizable):
"""An object which manages the estimation and decoding of a model."""

def fit(
self,
X: ArrayLike,
y: ArrayLike = None,
X_val: ArrayLike = None,
y_val: ArrayLike = None,
**estimator_params,
) -> "Estimator":
"""Fits the model function arround X. It takes some samples (X) and
the respective labels (y) if the model is supervised.
X_val and y_val are optional and may be used if needed.
Parameters
----------
X : ArrayLike
An array-like representing the whole dataset with shape:
An array-like representing the whole training dataset with shape:
(n_samples, n_features).
y : ArrayLike
The respective labels, with shape: (n_samples, ). This parameter is
optional and may be used if needed.
**estimator_params : type
The respective train labels, with shape: (n_samples, ). This parameter is
optional and may be used if needed. By default None.
X_val : ArrayLike
An array-like representing the whole validation dataset with shape:
(k_samples, n_features). This parameter is optional and may be used
if needed. By default None.
y_val : ArrayLike
The respective validation labels, with shape: (k_samples, ). This
parameter is optional and may be used if needed. By default None.
**estimator_params : Any
Optional data-dependent parameters.
Returns
Expand All @@ -30,8 +45,7 @@ def fit(self, X: ArrayLike, y: ArrayLike = None, **estimator_params) -> 'Estimat
The estimator (self).
"""

raise NotImplementedError
return self

def predict(self, X: ArrayLike) -> ArrayLike:
"""Predict or project the values of the samples (X).
Expand All @@ -51,21 +65,32 @@ def predict(self, X: ArrayLike) -> ArrayLike:

raise NotImplementedError

def fit_predict(self,
X: ArrayLike,
y: ArrayLike = None,
**estimator_params) -> ArrayLike:
def fit_predict(
self,
X: ArrayLike,
y: ArrayLike = None,
X_val: ArrayLike = None,
y_val: ArrayLike = None,
**estimator_params,
) -> ArrayLike:
"""Chain fit and predict methods, togheter. It fits the model and
predict the samples of X.
Parameters
----------
X : ArrayLike
An array-like representing the whole dataset with shape:
An array-like representing the whole training dataset with shape:
(n_samples, n_features).
y : ArrayLike
The respective labels, with shape: (n_samples, ). This parameter is
optional and may be used if needed.
The respective training labels, with shape: (n_samples, ). This parameter is
optional and may be used if needed. By default None.
X_val : ArrayLike
An array-like representing the whole validation dataset with shape:
(k_samples, n_features). This parameter is optional and may be used
if needed. By default None.
y_val : ArrayLike
The respective validation labels, with shape: (k_samples, ). This
parameter is optional and may be used if needed. By default None.
**estimator_params : type
Optional data-dependent parameters.
Expand All @@ -76,5 +101,5 @@ def fit_predict(self,
labels predicted for each samples.
"""
self.fit(X, y, **estimator_params)
self.fit(X, y, X_val, y_val, **estimator_params)
return self.predict(X)
52 changes: 49 additions & 3 deletions src/librep/base/transform.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,21 @@
# Wrap around scikit learn base API
# Borrowed from Sklearn API


class Transform(Parametrizable):
"""For filtering or modifying the data, in a supervised or unsupervised way.
`fit` allows implementing parametrizable transforms. This method sees the
whole dataset. `transform` allows transforming each sample.
"""

def fit(self, X: ArrayLike, y: ArrayLike = None, **fit_params) -> 'Transform':
def fit(
self,
X: ArrayLike,
y: ArrayLike = None,
X_val: ArrayLike = None,
y_val: ArrayLike = None,
**fit_params,
) -> "Transform":
"""Fit the transformation with information of the whole dataset.
Parameters
Expand All @@ -22,6 +30,13 @@ def fit(self, X: ArrayLike, y: ArrayLike = None, **fit_params) -> 'Transform':
y : ArrayLike
The respective labels, with shape: (n_samples, ). This parameter is
optional and may be used if needed.
X_val : ArrayLike
An array-like representing the whole validation dataset with shape:
(k_samples, n_features). This parameter is optional and may be used
if needed.
y_val : ArrayLike
The respective validation labels, with shape: (k_samples, ). This
parameter is optional and may be used if needed.
**fit_params : type
Optional data-dependent parameters.
Expand Down Expand Up @@ -49,8 +64,39 @@ def transform(self, X: ArrayLike) -> ArrayLike:
"""
raise NotImplementedError

def fit_transform(self, X: ArrayLike, y: ArrayLike = None, **fit_params):
self.fit(X, y, **fit_params)
def fit_transform(
self,
X: ArrayLike,
y: ArrayLike = None,
X_val: ArrayLike = None,
y_val: ArrayLike = None,
**fit_params,
) -> ArrayLike:
"""Chain fit and transform methods, toghether. It firs the model and
then transforms the training data.
Parameters
----------
X : ArrayLike
An array-like representing the whole dataset with shape:
(n_samples, n_features).
y : ArrayLike
The respective train labels, with shape: (n_samples, ). This parameter is
optional and may be used if needed. By default None.
X_val : ArrayLike
An array-like representing the whole validation dataset with shape:
(k_samples, n_features). This parameter is optional and may be used
if needed. By default None.
y_val : ArrayLike
The respective validation labels, with shape: (k_samples, ). This
parameter is optional and may be used if needed. By default None.
Returns
-------
ArrayLike
An array-like with the transformed samples.
"""
self.fit(X, y, X_val, y_val, **fit_params)
return self.transform(X)


Expand Down
5 changes: 3 additions & 2 deletions src/librep/config/type_definitions.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import os
import numpy
from typing import Union, Hashable
import pandas as pd
from typing import Union, Hashable, Iterable

# PathLike: The PathLike type is used for defining a file path.
PathLike = Union[str, os.PathLike]
ArrayLike = Union[numpy.ndarray]
ArrayLike = Union[numpy.ndarray, pd.DataFrame, Iterable]
KeyType = Hashable

0 comments on commit 7c98bd8

Please sign in to comment.