Filter
labchain.base.base_clases.BaseFilter
¶
Bases: BasePlugin
Base class for filter components in the framework.
This abstract class extends BasePlugin and provides a structure for implementing filter operations, including fit and predict methods. It serves as the foundation for all filter types in the framework, ensuring consistent behavior and interfaces for machine learning operations.
Key Features
- Implements fit and predict methods for machine learning operations
- Provides caching mechanisms for model and data storage
- Supports verbose output for debugging and monitoring
- Implements equality and hashing methods for filter comparison
- Supports serialization and deserialization of filter instances
Usage
To create a new filter type, inherit from this class and implement the required methods. For example:
class MyCustomFilter(BaseFilter):
def __init__(self, n_components: int = 2):
super().__init__(n_components=n_components)
self._model = None # Private: internal state
def fit(self, x: XYData, y: Optional[XYData] = None) -> None:
self._print_acction("Fitting MyCustomFilter")
# Implement fitting logic here
data = x.value
self._model = np.linalg.svd(data - np.mean(data, axis=0), full_matrices=False)
def predict(self, x: XYData) -> XYData:
self._print_acction("Predicting with MyCustomFilter")
if self._model is None:
raise ValueError("Model not fitted yet.")
# Implement prediction logic here
data = x.value
U, s, Vt = self._model
transformed = np.dot(data - np.mean(data, axis=0), Vt.T[:, :self.n_components])
return XYData(_value=transformed, _hash=x._hash, _path=self._m_path)
Attributes:
| Name | Type | Description |
|---|---|---|
_verbose |
bool
|
Controls the verbosity of output. |
_m_hash |
str
|
Hash of the current model. |
_m_str |
str
|
String representation of the current model. |
_m_path |
str
|
Path to the current model. |
_original_fit |
method
|
Reference to the original fit method. |
_original_predict |
method
|
Reference to the original predict method. |
Methods:
| Name | Description |
|---|---|
__init__ |
Initializes the filter instance, setting up attributes and method wrappers. |
fit |
XYData, y: Optional[XYData]) -> Optional[float]: Fits the filter to the input data. |
predict |
XYData) -> XYData: Makes predictions using the fitted filter. |
verbose |
bool) -> None: Sets the verbosity level for output. |
init |
Initializes filter-specific attributes. |
_get_model_key |
str) -> Tuple[str, str]: Generates a unique key for the model. |
_get_data_key |
str, data_hash: str) -> Tuple[str, str]: Generates a unique key for the data. |
grid |
Dict[str, List[Any] | Tuple[Any, Any]]) -> BaseFilter: Sets up grid search parameters. |
unwrap |
Returns the base filter without any wrappers. |
Note
This is an abstract base class. Concrete implementations should override the fit and predict methods to provide specific functionality.
Source code in labchain/base/base_clases.py
564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 | |
__eq__(other)
¶
Check equality between this filter and another object.
Two filters are considered equal if they are of the same type and have the same public attributes.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
other
|
object
|
The object to compare with this filter. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
bool |
bool | NotImplementedType
|
True if the objects are equal, False otherwise. |
Source code in labchain/base/base_clases.py
__getstate__()
¶
Prepare the object for pickling.
This method ensures that the original fit and predict methods are stored for serialization.
Returns:
| Type | Description |
|---|---|
Dict[str, Any]
|
Dict[str, Any]: The object's state dictionary. |
Source code in labchain/base/base_clases.py
__hash__()
¶
Generate a hash value for this filter.
The hash is based on the filter's type and its public attributes.
Returns:
| Name | Type | Description |
|---|---|---|
int |
int
|
The hash value of the filter. |
Source code in labchain/base/base_clases.py
__init__(verbose=True, *args, **kwargs)
¶
Initialize the BaseFilter instance.
This method sets up attributes for storing model-related information and wraps the fit and predict methods with pre-processing steps.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
verbose
|
bool
|
If True, enables verbose output. Defaults to True. |
True
|
*args
|
Any
|
Variable length argument list. |
()
|
**kwargs
|
Any
|
Arbitrary keyword arguments. |
{}
|
Source code in labchain/base/base_clases.py
__setstate__(state)
¶
Restore the object from its pickled state.
This method restores the wrapper methods after deserialization.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
state
|
Dict[str, Any]
|
The pickled state of the object. |
required |
Source code in labchain/base/base_clases.py
clear_memory()
staticmethod
¶
fit(x, y)
¶
Method for fitting the filter to the data.
This method should be overridden by subclasses to implement specific fitting logic.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
XYData
|
The input data. |
required |
y
|
Optional[XYData]
|
The target data, if applicable. |
required |
Returns:
| Type | Description |
|---|---|
Optional[float | dict]
|
Optional[float]: An optional float value, typically used for metrics or loss. |
Raises:
| Type | Description |
|---|---|
NotTrainableFilterError
|
If the filter does not support fitting. |
Source code in labchain/base/base_clases.py
grid(grid)
¶
Set up grid search parameters for the filter.
This method allows defining a grid of hyperparameters for optimization.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
grid
|
Dict[str, List[Any] | Tuple[Any, Any]]
|
A dictionary where keys are parameter names and values are lists or tuples of possible values. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
BaseFilter |
BaseFilter
|
The filter instance with grid search parameters set. |
Source code in labchain/base/base_clases.py
predict(x)
abstractmethod
¶
Abstract method for making predictions using the filter.
This method must be implemented by subclasses to provide specific prediction logic.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
XYData
|
The input data. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
XYData |
XYData
|
The prediction results. |
Source code in labchain/base/base_clases.py
unwrap()
¶
Return the base filter without any wrappers.
This method is useful when you need to access the original filter without any additional layers or modifications added by wrappers.
Returns:
| Name | Type | Description |
|---|---|---|
BaseFilter |
BaseFilter
|
The unwrapped base filter. |
Source code in labchain/base/base_clases.py
verbose(value)
¶
Set the verbosity of the filter.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
value
|
bool
|
If True, enables verbose output; if False, disables it. |
required |
Returns:
| Type | Description |
|---|---|
None
|
None |