TINC
Toolkit for Interactive Computation
tinc::ProcessorScript Class Reference

The ProcessorScript class. More...

#include <ProcessorScript.hpp>

Inheritance diagram for tinc::ProcessorScript:
tinc::Processor tinc::IdObject

Public Types

typedef std::map< std::string, al::VariantValue > Configuration
 

Public Member Functions

 ProcessorScript (std::string id="")
 
virtual ~ProcessorScript ()
 
 ProcessorScript (ProcessorScript &p)
 
void setCommand (std::string command)
 Set the script's main command (e.g. python) More...
 
std::string getCommand ()
 Get the script's main command (e.g. python) More...
 
void setScriptName (std::string scriptName)
 Set name of script to be run. More...
 
std::string getScriptName ()
 Get name of script to be run. More...
 
std::string getScriptFile (bool fullPath=false)
 Query current script file name. More...
 
bool process (bool forceRecompute=false) override
 process More...
 
void enableJsonConfig (bool enable=true)
 if set to false disables creation and argument passing of json config More...
 
void setArgumentTemplate (std::string argTemplate)
 
bool isRunning ()
 returns true is the process() function is currently running More...
 
void setDataDirectory (std::string directory)
 Convenience function to set input and output directories in one call. More...
 
void setOutputDirectory (std::string outputDirectory)
 Set the directory for output files. More...
 
std::string getOutputDirectory ()
 Get the directory for output files. More...
 
void setInputDirectory (std::string inputDirectory)
 Set the directory for input files. More...
 
std::string getInputDirectory ()
 Get the directory for input files. More...
 
void setOutputFileNames (std::vector< std::string > outputFiles)
 Set the names of output files. More...
 
std::vector< std::string > getOutputFileNames ()
 Query the current output filenames. More...
 
void setInputFileNames (std::vector< std::string > inputFiles)
 Set the names of input files. More...
 
std::vector< std::string > getInputFileNames ()
 Query the current input filenames. More...
 
void setRunningDirectory (std::string directory)
 Set the current directory for process to run in. More...
 
std::string getRunningDirectory ()
 Get the directory for input files. More...
 
void registerStartCallback (std::function< void(void)> func)
 Register a function to be called at the start of process() More...
 
void registerDoneCallback (std::function< void(bool)> func)
 Register a function to be called at the end of process() More...
 
void setVerbose (bool verbose=true)
 
ProcessorregisterDimension (ParameterSpaceDimension &dim)
 Register a dimension so that the Processor is executed on changes. More...
 
Processoroperator<< (ParameterSpaceDimension &dim)
 Convenient syntax for registerDimension() More...
 
void useCache (bool use=true)
 Enable simple file based caching. More...
 
ProcessorregisterDependency (ParameterSpaceDimension &param)
 Register a dependency. More...
 
ProcessorregisterDependency (al::ParameterMeta *param)
 Convenient syntax for registerParameter() More...
 
std::vector< ParameterSpaceDimension * > getDependencies ()
 Return the dependencies registered with this Processor. More...
 
std::string getId ()
 get this object's id More...
 
void setId (std::string id)
 set this object's id More...
 
std::string getDocumentation () const
 
void setDocumentation (const std::string &documentation)
 

Static Public Member Functions

static std::string sanitizeName (std::string output_name)
 Cleans a name up so it can be written to disk. More...
 

Public Attributes

bool ignoreFail {false}
 
bool enabled {true}
 
std::function< bool(void)> prepareFunction
 Set a function to be called before computing to prepare data. More...
 
Configuration configuration
 Current internal configuration key value pairs. More...
 
std::function< void(al::Socket *src)> modified = [](al::Socket * ) {}
 

Protected Member Functions

std::string writeJsonConfig ()
 
bool readJsonConfig (std::string filename)
 
void parametersToConfig (nlohmann::json &j)
 
void callStartCallbacks ()
 
void callDoneCallbacks (bool result)
 
std::string metaFilename ()
 

Protected Attributes

std::string mInputDirectory
 
std::string mOutputDirectory
 
std::string mRunningDirectory
 
std::vector< std::string > mOutputFileNames
 
std::vector< std::string > mInputFileNames
 
bool mVerbose
 
bool mUseCache {false}
 
std::vector< ParameterSpaceDimension * > mParameters
 
std::vector< ParameterSpaceDimension * > mDependencies
 
std::vector< std::unique_ptr< ParameterSpaceDimension > > mInternalDimensions
 
std::mutex mProcessLock
 
std::string mId
 
std::string mDocumentation
 

Detailed Description

The ProcessorScript class.

ProcessorScript proc{"proc"};
proc.setDataDirectory("/data/dir");
proc.setRunningDirectory("/running/dir");
proc.setInputFileNames({"input_file"});
proc.setOutputFileNames({"output_file"});
proc.setCommand("python");
proc.setScriptName("script.py");
if (proc.process()) {
// Data is ready
}
ProcessorScript(std::string id="")

The script will be passed options through a json file, and can also recieve feedback from the script through the same file. The filename is passed to the script as its first argument.

Definition at line 78 of file ProcessorScript.hpp.

Member Typedef Documentation

◆ Configuration

typedef std::map<std::string, al::VariantValue> tinc::Processor::Configuration
inherited

Definition at line 272 of file Processor.hpp.

Constructor & Destructor Documentation

◆ ProcessorScript() [1/2]

tinc::ProcessorScript::ProcessorScript ( std::string  id = "")
inline

Definition at line 80 of file ProcessorScript.hpp.

◆ ~ProcessorScript()

virtual tinc::ProcessorScript::~ProcessorScript ( )
inlinevirtual

Definition at line 82 of file ProcessorScript.hpp.

◆ ProcessorScript() [2/2]

tinc::ProcessorScript::ProcessorScript ( ProcessorScript p)
inline

Definition at line 85 of file ProcessorScript.hpp.

Member Function Documentation

◆ callDoneCallbacks()

void tinc::Processor::callDoneCallbacks ( bool  result)
protectedinherited

◆ callStartCallbacks()

void tinc::Processor::callStartCallbacks ( )
protectedinherited

◆ enableJsonConfig()

void tinc::ProcessorScript::enableJsonConfig ( bool  enable = true)

if set to false disables creation and argument passing of json config

Parameters
enable

◆ getCommand()

std::string tinc::ProcessorScript::getCommand ( )
inline

Get the script's main command (e.g. python)

Definition at line 97 of file ProcessorScript.hpp.

◆ getDependencies()

std::vector<ParameterSpaceDimension *> tinc::Processor::getDependencies ( )
inlineinherited

Return the dependencies registered with this Processor.

Returns

Definition at line 268 of file Processor.hpp.

◆ getDocumentation()

std::string tinc::IdObject::getDocumentation ( ) const
inherited

◆ getId()

std::string tinc::IdObject::getId ( )
inherited

get this object's id

◆ getInputDirectory()

std::string tinc::Processor::getInputDirectory ( )
inlineinherited

Get the directory for input files.

Definition at line 119 of file Processor.hpp.

◆ getInputFileNames()

std::vector<std::string> tinc::Processor::getInputFileNames ( )
inherited

Query the current input filenames.

This function contains the filenames set in C++ using setInputFileNames or the filenames set by the Processor after running, see for example ProcessorScript

◆ getOutputDirectory()

std::string tinc::Processor::getOutputDirectory ( )
inlineinherited

Get the directory for output files.

Definition at line 109 of file Processor.hpp.

◆ getOutputFileNames()

std::vector<std::string> tinc::Processor::getOutputFileNames ( )
inherited

Query the current output filenames.

This function contains the filenames set in C++ using setOutputFileNames or the filenames set by the Processor after running, see for example ProcessorScript

◆ getRunningDirectory()

std::string tinc::Processor::getRunningDirectory ( )
inlineinherited

Get the directory for input files.

Definition at line 163 of file Processor.hpp.

◆ getScriptFile()

std::string tinc::ProcessorScript::getScriptFile ( bool  fullPath = false)

Query current script file name.

◆ getScriptName()

std::string tinc::ProcessorScript::getScriptName ( )
inline

Get name of script to be run.

Definition at line 112 of file ProcessorScript.hpp.

◆ isRunning()

bool tinc::Processor::isRunning ( )
inherited

returns true is the process() function is currently running

This is useful for soft queries, for example for user feedback. The processor might have started running before this function exits even if it reports it is idle and viceversa.

◆ metaFilename()

std::string tinc::Processor::metaFilename ( )
protectedinherited

◆ operator<<()

Processor& tinc::Processor::operator<< ( ParameterSpaceDimension dim)
inlineinherited

Convenient syntax for registerDimension()

Definition at line 225 of file Processor.hpp.

◆ parametersToConfig()

void tinc::ProcessorScript::parametersToConfig ( nlohmann::json &  j)
protected

◆ process()

bool tinc::ProcessorScript::process ( bool  forceRecompute = false)
overridevirtual

process

Implements tinc::Processor.

◆ readJsonConfig()

bool tinc::ProcessorScript::readJsonConfig ( std::string  filename)
protected

◆ registerDependency() [1/2]

Processor& tinc::Processor::registerDependency ( al::ParameterMeta *  param)
inherited

Convenient syntax for registerParameter()

Parameters
param
Returns

◆ registerDependency() [2/2]

Processor& tinc::Processor::registerDependency ( ParameterSpaceDimension param)
inherited

Register a dependency.

Parameters
param
Returns

A dependency is a parameter that affects the result of this processor but does not trigger computation

◆ registerDimension()

Processor& tinc::Processor::registerDimension ( ParameterSpaceDimension dim)
inherited

Register a dimension so that the Processor is executed on changes.

You should only register dimensions in this way if you are not using the processor through ParameterSpace::process() or ParameterSpace::sweep(). The tow modes are provided for flexibility but should not be used concurrently.

Processor is executed on changes of this parameter

◆ registerDoneCallback()

void tinc::Processor::registerDoneCallback ( std::function< void(bool)>  func)
inherited

Register a function to be called at the end of process()

This function is passed true if the process() function has been sucessful.

These callbacks should only be used for data related actions, as they will be skipped when reusing cache in ParameterSpace.

◆ registerStartCallback()

void tinc::Processor::registerStartCallback ( std::function< void(void)>  func)
inherited

Register a function to be called at the start of process()

These callbacks should only be used for data related actions, as they will be skipped when reusing cache in ParameterSpace.

◆ sanitizeName()

static std::string tinc::ProcessorScript::sanitizeName ( std::string  output_name)
static

Cleans a name up so it can be written to disk.

Parameters
output_namethe source name
Returns
the cleaned up name

This function will remove any characters not allowed by the operating system like :,/,\ etc. And will remove any characters like '.' that can confuse the parsing of the name on read.

◆ setArgumentTemplate()

void tinc::ProcessorScript::setArgumentTemplate ( std::string  argTemplate)
inline
Parameters
commandLineTemplateIf a template is set, it will be populated by parameters and file names and passed to the command as the command line flags after the command and script name. If the template is used, the json config file is still created, unless enableJsonConfig(false) is called

Definition at line 152 of file ProcessorScript.hpp.

◆ setCommand()

void tinc::ProcessorScript::setCommand ( std::string  command)
inline

Set the script's main command (e.g. python)

Definition at line 92 of file ProcessorScript.hpp.

◆ setDataDirectory()

void tinc::Processor::setDataDirectory ( std::string  directory)
inherited

Convenience function to set input and output directories in one call.

◆ setDocumentation()

void tinc::IdObject::setDocumentation ( const std::string &  documentation)
inherited

◆ setId()

void tinc::IdObject::setId ( std::string  id)
inherited

set this object's id

◆ setInputDirectory()

void tinc::Processor::setInputDirectory ( std::string  inputDirectory)
inherited

Set the directory for input files.

◆ setInputFileNames()

void tinc::Processor::setInputFileNames ( std::vector< std::string >  inputFiles)
inherited

Set the names of input files.

Parameters
outputFileslist of output file names.

◆ setOutputDirectory()

void tinc::Processor::setOutputDirectory ( std::string  outputDirectory)
inherited

Set the directory for output files.

◆ setOutputFileNames()

void tinc::Processor::setOutputFileNames ( std::vector< std::string >  outputFiles)
inherited

Set the names of output files.

Parameters
outputFileslist of output file names.

◆ setRunningDirectory()

void tinc::Processor::setRunningDirectory ( std::string  directory)
inherited

Set the current directory for process to run in.

You must ensure you don;t change the running directory while the processor is running, otherwise different parts of the processing (the callbacks and the processing function) might execute in a different directory

◆ setScriptName()

void tinc::ProcessorScript::setScriptName ( std::string  scriptName)
inline

Set name of script to be run.

Parameters
scriptName

Definition at line 104 of file ProcessorScript.hpp.

◆ setVerbose()

void tinc::Processor::setVerbose ( bool  verbose = true)
inlineinherited

Definition at line 183 of file Processor.hpp.

◆ useCache()

void tinc::Processor::useCache ( bool  use = true)
inlineinherited

Enable simple file based caching.

Parameters
useThis caching is a simple but brittle caching mechanism. Whenever possible use the caching provided by ParameterSpace, as it is more robust.

For this caching to work, you will need to ensure that every independent run produces unique and reproducible filenames. i.e. you will need to set the output file names on every run to include in their name all parameter values and dependencies, or have some other way of connecting the parameters/ parameter space to the file.

This will cache the output as written in the output files. You can set and query the files using getOutputFiles() setOutputFiles().

Definition at line 245 of file Processor.hpp.

◆ writeJsonConfig()

std::string tinc::ProcessorScript::writeJsonConfig ( )
protected

Member Data Documentation

◆ configuration

Configuration tinc::Processor::configuration
inherited

Current internal configuration key value pairs.

Reflects the most recently used configuration (whether successful or failed) or the configuration for the currently running process.

Definition at line 280 of file Processor.hpp.

◆ enabled

bool tinc::Processor::enabled {true}
inherited

If set to false, process() has no effect and will return true;

Definition at line 194 of file Processor.hpp.

◆ ignoreFail

bool tinc::Processor::ignoreFail {false}
inherited

If set to true, ProcessorChains will continue even if this processor fails. Has no effect if running chain asychronously

Definition at line 189 of file Processor.hpp.

◆ mDependencies

std::vector<ParameterSpaceDimension *> tinc::Processor::mDependencies
protectedinherited

Definition at line 293 of file Processor.hpp.

◆ mDocumentation

std::string tinc::IdObject::mDocumentation
protectedinherited

Definition at line 69 of file IdObject.hpp.

◆ mId

std::string tinc::IdObject::mId
protectedinherited

Definition at line 68 of file IdObject.hpp.

◆ mInputDirectory

std::string tinc::Processor::mInputDirectory
protectedinherited

Definition at line 283 of file Processor.hpp.

◆ mInputFileNames

std::vector<std::string> tinc::Processor::mInputFileNames
protectedinherited

Definition at line 287 of file Processor.hpp.

◆ mInternalDimensions

std::vector<std::unique_ptr<ParameterSpaceDimension> > tinc::Processor::mInternalDimensions
protectedinherited

Definition at line 294 of file Processor.hpp.

◆ modified

std::function<void(al::Socket *src)> tinc::IdObject::modified = [](al::Socket * ) {}
inherited

Definition at line 65 of file IdObject.hpp.

◆ mOutputDirectory

std::string tinc::Processor::mOutputDirectory
protectedinherited

Definition at line 284 of file Processor.hpp.

◆ mOutputFileNames

std::vector<std::string> tinc::Processor::mOutputFileNames
protectedinherited

Definition at line 286 of file Processor.hpp.

◆ mParameters

std::vector<ParameterSpaceDimension *> tinc::Processor::mParameters
protectedinherited

Definition at line 292 of file Processor.hpp.

◆ mProcessLock

std::mutex tinc::Processor::mProcessLock
protectedinherited

Definition at line 304 of file Processor.hpp.

◆ mRunningDirectory

std::string tinc::Processor::mRunningDirectory
protectedinherited

Definition at line 285 of file Processor.hpp.

◆ mUseCache

bool tinc::Processor::mUseCache {false}
protectedinherited

Definition at line 290 of file Processor.hpp.

◆ mVerbose

bool tinc::Processor::mVerbose
protectedinherited

Definition at line 288 of file Processor.hpp.

◆ prepareFunction

std::function<bool(void)> tinc::Processor::prepareFunction
inherited

Set a function to be called before computing to prepare data.

When writing the prepare function you should access values and ids through Processor::configuration. If you access values directly from dimensions, you will likely break ParameterSpace::sweep used with this Processor as sweep() does not change the internal values of the parameter space and its dimensions.

If this function returns false, computation is not performed and process() will return false. You can use this function to avoid computation when conditions are not right.

Definition at line 209 of file Processor.hpp.


The documentation for this class was generated from the following file: