1#ifndef FSTCRITERIONEXTERNVALUE_H
2#define FSTCRITERIONEXTERNVALUE_H
162template<
class RETURNTYPE,
typename DATATYPE,
typename REALTYPE,
typename IDXTYPE,
typename DIMTYPE,
class SUBSET,
class DATAACCESSOR>
163class Criterion_Extern_Value :
public Clonable_Impl<Criterion_Extern_Value<RETURNTYPE,DATATYPE,REALTYPE,IDXTYPE,DIMTYPE,SUBSET,DATAACCESSOR>,Criterion<RETURNTYPE,SUBSET> > {
202 virtual std::ostream&
print(std::ostream& os)
const override {os <<
"Criterion_Extern_Value(cmd=\"" <<
_bridge.get_command() <<
"\", monotonic=" << (
_monotonic?
"asserted":
"no") << (
_no_data?
", no-data":
"") <<
")";
return os;}
207 _bridge(
"Criterion_Extern_Value"),
218 notify(
"Criterion_Extern_Value copy-constructor.");
236template<
class RETURNTYPE,
typename DATATYPE,
typename REALTYPE,
typename IDXTYPE,
typename DIMTYPE,
class SUBSET,
class DATAACCESSOR>
239 notify(
"Criterion_Extern_Value::initialize().");
241 assert(da->getNoOfFeatures()>0);
245 if(!
_no_data && da->getNoOfClasses()<2)
throw fst_error(
"Criterion_Extern_Value requires at least 2 classes");
246 if(
_bridge.get_command().empty())
throw fst_error(
"Criterion_Extern_Value: no command set (see --extern-cmd)");
256template<
class RETURNTYPE,
typename DATATYPE,
typename REALTYPE,
typename IDXTYPE,
typename DIMTYPE,
class SUBSET,
class DATAACCESSOR>
259 std::FILE *out=
_bridge.to_child();
261 std::snprintf(buf,
sizeof(buf),
"%ld",(
long)label); std::fputs(buf,out);
264 std::snprintf(buf,
sizeof(buf),
"%.17g",(
double)pattern[f]);
268 if(std::fputc(
'\n',out)==EOF)
272template<
class RETURNTYPE,
typename DATATYPE,
typename REALTYPE,
typename IDXTYPE,
typename DIMTYPE,
class SUBSET,
class DATAACCESSOR>
276 unsigned long long n=0;
277 {IDXTYPE s;
for(DIMTYPE c=0;c<
_classes;c++) {
_da->setClass(c);
for(
const auto &blk :
_da->blocks(
TRAIN)) {s=blk.size; n+=(
unsigned long long)s;}}}
278 if(n==0)
return false;
279 _bridge.send_line(
"DATA "+std::to_string(n)+
" "+std::to_string((
long)
_features)+
" "+std::to_string((
long)
_classes));
281 typename DATAACCESSOR::PPattern p;
283 const DIMTYPE stride=
_da->getNoOfFeatures();
287 for(
const auto &blk :
_da->blocks(
TRAIN))
for(i=0,p=blk.data,s=blk.size;i<s;i++)
288 send_row(c,&p[(std::size_t)i*stride]);
291 const std::string resp=
_bridge.read_line();
292 if(resp!=
"OK")
throw fst_error(
"Criterion_Extern_Value: '"+
_bridge.get_command()+
"' rejected DATA: "+resp);
297template<
class RETURNTYPE,
typename DATATYPE,
typename REALTYPE,
typename IDXTYPE,
typename DIMTYPE,
class SUBSET,
class DATAACCESSOR>
303 const char *s=line.c_str();
305 const double v=std::strtod(s,&end);
306 if(end==s || *end!=
'\0')
307 {
_bridge.shutdown_process();
throw fst_error(
"Criterion_Extern_Value: '"+
_bridge.get_command()+
"' returned a non-numeric value line: '"+line+
"'");}
311 if(!std::isfinite(v))
312 {
_bridge.shutdown_process();
throw fst_error(
"Criterion_Extern_Value: '"+
_bridge.get_command()+
"' returned a non-finite value: '"+line+
"' (criterion values must be finite; negate error-style measures inside the tool rather than returning infinities)");}
316template<
class RETURNTYPE,
typename DATATYPE,
typename REALTYPE,
typename IDXTYPE,
typename DIMTYPE,
class SUBSET,
class DATAACCESSOR>
319 notify(
"Criterion_Extern_Value::evaluate().");
324 assert(sub->get_n_raw()==
_da->getNoOfFeatures());
332 const bool was_running=
_bridge.running();
340 std::string req=
"VALUE "+std::to_string((
long)
_subfeatures);
CRTP mixin implementing the Clonable protocol for a concrete class.
Definition clonable.hpp:184
External-tool criterion: a user-supplied command computes the criterion value itself - unlike wrapper...
Definition criterion_extern_value.hpp:163
virtual bool is_monotonic() const override
Definition criterion_extern_value.hpp:176
bool send_data_block()
sends one full-width DATA block over the current split's TRAIN part (returns false on an empty train ...
Definition criterion_extern_value.hpp:273
std::shared_ptr< SUBSET > PSubset
shared pointer to the feature subset being evaluated
Definition criterion_extern_value.hpp:168
virtual std::ostream & print(std::ostream &os) const override
writes a human-readable identification of the object; base default writes nothing
Definition criterion_extern_value.hpp:202
void send_row(const DIMTYPE label, const PPattern &pattern)
sends one %.17g-formatted labelled FULL-WIDTH data row
Definition criterion_extern_value.hpp:257
Criterion_Extern_Value(const Criterion_Extern_Value &cev)
Copy-constructor; spawns its own fresh bridge (clones never share pipes) and copies the recorded data...
Definition criterion_extern_value.hpp:206
DATAACCESSOR::PPattern PPattern
raw feature-vector pointer type, as sent full-width by send_row()
Definition criterion_extern_value.hpp:169
void set_no_data(const bool nd)
--extern-no-data: suppress the DATA block - pure set-function optimization, the tool answers index-on...
Definition criterion_extern_value.hpp:177
DIMTYPE _features
raw feature count recorded by initialize(), from _da->getNoOfFeatures()
Definition criterion_extern_value.hpp:227
Clonable_Impl< Criterion_Extern_Value, parent > parent_impl
implements clone(); sharing/stateless_clone throw in Criterion
Definition criterion_extern_value.hpp:166
DIMTYPE _subfeatures
d = current subset size (number of valid entries in _index)
Definition criterion_extern_value.hpp:228
std::shared_ptr< DATAACCESSOR > PDataAccessor
shared pointer to the data accessor supplying the DATA block's TRAIN part
Definition criterion_extern_value.hpp:167
bool _no_data
--extern-no-data: never send DATA (the tool is a pure set function over index sets)
Definition criterion_extern_value.hpp:223
void set_monotonic(const bool m)
the user's assertion (--extern-monotonic); see the class note
Definition criterion_extern_value.hpp:175
double parse_value(const std::string &line)
strict full-line numeric parse incl. finiteness (throws on violation, symmetric with the classifier b...
Definition criterion_extern_value.hpp:298
virtual bool evaluate(RETURNTYPE &result, const PSubset sub) override
Evaluates sub by one VALUE request to the external tool.
Definition criterion_extern_value.hpp:317
Extern_Bridge _bridge
persistent child process + line I/O (per instance; never shared by clones)
Definition criterion_extern_value.hpp:221
const std::string & get_command() const
returns the currently configured external command line
Definition criterion_extern_value.hpp:174
DIMTYPE _classes
class count recorded by initialize(), from _da->getNoOfClasses()
Definition criterion_extern_value.hpp:226
bool _monotonic
the user's cardinality-monotonicity assertion
Definition criterion_extern_value.hpp:222
PDataAccessor _da
the data accessor whose TRAIN part feeds the DATA block
Definition criterion_extern_value.hpp:225
Criterion< RETURNTYPE, SUBSET > parent
base class: the abstract criterion interface
Definition criterion_extern_value.hpp:165
void initialize(const PDataAccessor da, const PDataAccessor da_for_evaluate=PDataAccessor())
Records the data shape and marks the tool's stored DATA stale; the criterion then evaluates on da's T...
Definition criterion_extern_value.hpp:237
std::vector< DIMTYPE > _index
subset position -> raw feature index (first _subfeatures entries valid)
Definition criterion_extern_value.hpp:229
bool _data_stale
the tool's stored DATA no longer matches the current split (set by initialize() and child respawn)
Definition criterion_extern_value.hpp:224
void set_command(const std::string &cmd)
sets the external command line (--extern-cmd); required before the first evaluate()
Definition criterion_extern_value.hpp:173
Abstract class, defines interface for feature selection criterion classes.
Definition criterion.hpp:84
Persistent-child-process line-protocol plumbing shared by the external-tool bridges: Classifier_Exter...
Definition extern_bridge.hpp:105
const std::string & get_command() const
returns the configured command line
Definition extern_bridge.hpp:113
FST4 specific error passing class.
Definition error.hpp:61
Defines interface for classes that may need to get cloned.
Defines interface for feature selection criterion classes.
FST4 specific error passing class.
Persistent-child-process line-protocol plumbing shared by the external-tool bridges (Classifier_Exter...
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
@ TRAIN
the split's training part
Definition data_accessor.hpp:85