Feature Selection Toolbox FST4
UTIA · Czech Academy of Sciences
Loading...
Searching...
No Matches
distance.hpp
Go to the documentation of this file.
1#ifndef FSTDISTANCE_H
2#define FSTDISTANCE_H
3 /*
27=========================================================================
28Copyright and license:
29 * FST4 software (with the exception of any bundled or externally linked
30 third-party components) is Copyright (C) 2000-present Institute of
31 Information Theory and Automation of the Czech Academy of Sciences (UTIA).
32 * FST4 is free software: you can redistribute it and/or modify it under
33 the terms of the GNU Affero General Public License as published by the
34 Free Software Foundation, version 3. The full text of the license is in
35 the file LICENSE in the distribution root and at
36 https://www.gnu.org/licenses/agpl-3.0.html.
37 * FST4 is distributed in the hope that it will be useful, but WITHOUT ANY
38 WARRANTY; without even the implied warranty of MERCHANTABILITY or
39 FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public
40 License for more details.
41 * Closed-source use without the AGPL's copyleft obligations requires a
42 commercial license from UTIA; in case of interest write to
43 utia@utia.cas.cz. LICENSING.md in the distribution root explains both
44 options, the contributor license agreement that every contribution
45 requires, and the contacts (technical questions and pull requests:
46 somol@utia.cas.cz, with "FST" in the subject line).
47========================================================================== */
48
49#include <memory>
50#include <cassert>
51#include <iostream>
52#include <cmath>
53#include <list>
54#include <cstring> // memcpy
55#include "error.hpp"
56#include "global.hpp"
57#include "clonable.hpp"
58
59/*============== Template parameter type naming conventions ==============
60--------- Numeric types: -------------------------------------------------
61DATATYPE - data sample values - usually real numbers (but may be integers
62 in text processing etc.)
63REALTYPE - must be real numbers - for representing intermediate results of
64 calculations like mean, covariance etc.
65IDXTYPE - index values for enumeration of data samples - (nonnegative) integers,
66 extent depends on numbers of samples in data
67DIMTYPE - index values for enumeration of features (dimensions), or classes (not
68 class sizes) - (nonnegative) integers, usually lower extent than IDXTYPE,
69 but be aware of expressions like _classes*_features*_features !
70 in linearized representations of feature matrices for all classes
71BINTYPE - feature selection marker type - represents ca. <10 different feature
72 states (selected, deselected, sel./desel. temporarily 1st nested loop, 2nd...)
73RETURNTYPE - criterion value: real value, but may be extended in future to support
74 multiple values
75--------- Class types: ---------------------------------------------------
76SUBSET - class of class type Subset
77CLASSIFIER - class implementing interface defined in abstract class Classifier
78EVALUATOR - class implementing interface defined in abstract class Sequential_Step
79DISTANCE - class implementing interface defined in abstract class Distance
80DATAACCESSOR - class implementing interface defined in abstract class Data_Accessor
81INTERVALCONTAINER - class of class type Data_Intervaller
82CONTAINER - STL container of class type Data_Interval
83========================================================================== */
84
85namespace FST {
86
88template<typename DATATYPE, typename DIMTYPE, class SUBSET>
89class Distance : public Clonable {
90public:
91 typedef std::shared_ptr<SUBSET> PSubset;
92 Distance() : _n(0), _d(0) {}
94 Distance(const Distance &d);
95 virtual ~Distance() {}
97 void reset(const DIMTYPE n);
98
104 void narrow_to(const PSubset sub); // fill _index[] -> access to data is then mapped to sub-matrix
106 void denarrow(); // reset _index[] -> access is reset to the full matrix
107
108 DIMTYPE get_n() const {return _n;}
109 DIMTYPE get_d() const {return _d;}
110
121 virtual DATATYPE distance(const DATATYPE *pattern1, const DATATYPE *pattern2) =0;
122
123 Distance* clone() const override =0;
124 Distance* sharing_clone() const override {throw fst_error("Distance::sharing_clone() not supported, use Distance::clone() instead.");}
125 Distance* stateless_clone() const override {throw fst_error("Distance::stateless_clone() not supported, use Distance::clone() instead.");}
126protected:
127 DIMTYPE _n;
128 DIMTYPE _d;
129 std::unique_ptr<DIMTYPE[]> _index;
130};
131
132template<typename DATATYPE, typename DIMTYPE, class SUBSET>
133Distance<DATATYPE,DIMTYPE,SUBSET>::Distance(const Distance& d) :
134 _n(d._n),
135 _d(d._d)
136{
137 notify("Distance copy-constructor.");
138 if(_n>0) {_index.reset(new DIMTYPE[_n]); memcpy((void *)_index.get(),(void *)(d._index).get(),sizeof(DIMTYPE)*_n);}
139}
140
141template<typename DATATYPE, typename DIMTYPE, class SUBSET>
143{
144 assert(n>0);
145 _n=n;
146 _index.reset(new DIMTYPE[_n]);
147 for(DIMTYPE i=0;i<_n;i++) _index[i]=i;
148 _d=_n;
149}
150
151template<typename DATATYPE, typename DIMTYPE, class SUBSET>
153{
154 assert(_n>=sub->get_d_raw()); // precondition: enough space to store the subset
155 assert(_index);
156 _d=0;
157 for(DIMTYPE f : sub->selected_features()) _index[_d++]=f;
158 assert(_d>0);
159}
160
161template<typename DATATYPE, typename DIMTYPE, class SUBSET>
163{
164 assert(_index);
165 for(DIMTYPE i=0;i<_n;i++) _index[i]=i;
166 _d=_n;
167}
168
169
170} // namespace
171#endif // FSTDISTANCE_H ///:~
Abstract class, defines interface for distance evaluators to be used in Classifier_kNN.
Definition distance.hpp:89
Distance * clone() const override=0
DIMTYPE get_n() const
the full (raw, unnarrowed) feature count fixed by reset()
Definition distance.hpp:108
Distance * stateless_clone() const override
Definition distance.hpp:125
void denarrow()
Undoes narrow_to(): _index[] reverts to the identity map and get_d() returns to get_n().
Definition distance.hpp:162
void narrow_to(const PSubset sub)
Restricts distance() to sub's selected features only. Rewrites _index[] to the subset's selected feat...
Definition distance.hpp:152
DIMTYPE _d
currently active feature count (see get_d())
Definition distance.hpp:128
void reset(const DIMTYPE n)
Allocates for n raw features and makes _index[] the identity map 0..n-1, with all n features active (...
Definition distance.hpp:142
DIMTYPE get_d() const
the currently active feature count: the narrowed subset size after narrow_to(), get_n() after reset()...
Definition distance.hpp:109
std::shared_ptr< SUBSET > PSubset
shared pointer to a feature subset, as consumed by narrow_to()
Definition distance.hpp:91
virtual DATATYPE distance(const DATATYPE *pattern1, const DATATYPE *pattern2)=0
Distance between two raw patterns over the currently active features only. Reads exactly get_d() entr...
std::unique_ptr< DIMTYPE[]> _index
maps active-feature position [0,_d) to a raw feature index in [0,_n)
Definition distance.hpp:129
DIMTYPE _n
full raw feature count, fixed by reset()
Definition distance.hpp:127
Distance * sharing_clone() const override
Definition distance.hpp:124
FST4 specific error passing class.
Definition error.hpp:61
Defines interface for classes that may need to get cloned.
FST4 specific error passing class.
Global definitions.
The Feature Selection Toolbox 4 library: every library component – data access, criteria and classifi...
Definition data_accessor.hpp:80
void notify(const std::string msg)
Prints msg followed by a newline via syncout::print(); a no-op unless the preprocessor macro DEBUG is...
Definition global.hpp:112
Copyright © 2000–present ÚTIA, Institute of Information Theory and Automation of the Czech Academy of Sciences · Contacts · This site sets no cookies and uses no analytics.