ATLAS Offline Software
Static Public Member Functions | Static Private Member Functions | List of all members
TFCSNetworkFactory Class Reference

Copyright (C) 2002-2024 CERN for the benefit of the ATLAS collaboration. More...

#include <TFCSNetworkFactory.h>

Collaboration diagram for TFCSNetworkFactory:

Static Public Member Functions

static std::unique_ptr< VNetworkBasecreate (std::string input)
 Given a string, make a network. More...
 
static std::unique_ptr< VNetworkBasecreate (std::string input, bool graph_form)
 Given a string, and information about format, make a network. More...
 
static std::unique_ptr< VNetworkBasecreate (std::vector< char > const &input)
 Given a vector of chars (bytes), make a network. More...
 
static std::unique_ptr< VNetworkBasecreate (std::vector< char > const &vector_input, std::string string_input)
 Create a network from whichever input isn't empty. More...
 
static std::unique_ptr< VNetworkBasecreate (std::vector< char > const &vector_input, std::string string_input, bool graph_form)
 Create a network from whichever input isn't empty. More...
 

Static Private Member Functions

static void resolveGlobs (std::string &filename)
 If the filepath ends in . More...
 
static bool isOnnxFile (std::string const &filename)
 Check if a filename seems to be an onnx file. More...
 

Detailed Description

Copyright (C) 2002-2024 CERN for the benefit of the ATLAS collaboration.

Class to perform runtime selection from the derived classes of VNetworkBase given inout for a network.

Has only static functions becuase no statelike information is needed to make this decision.

Information about the which network would be apropreate can be specified, or left entirely to the factory to determine.

Definition at line 24 of file TFCSNetworkFactory.h.

Member Function Documentation

◆ create() [1/5]

std::unique_ptr< VNetworkBase > TFCSNetworkFactory::create ( std::string  input)
static

Given a string, make a network.

This function will first check if the string is the path of a readable file on the disk. If so, the file suffix is used to decide if it's an onnx (.onnx) or lwtnn file (.json). If the filepath ends in .* then first an onnx then an lwtnn file will be tried. The file is read and parsed into a network.

If the string is not a filepath, it is assumed to be the content of a json file to make an lwtnn network.

When an lwtnn network is created, first the TFCSSimpleLWTNNHandler format is tried, and if this raises an error, the TFCSGANLWTNNHandler is applied. The former is simpler than the latter, so it will always fail to parse the more complex graph format.

Parameters
inputEither a file path, or the content of a file.

Definition at line 66 of file TFCSNetworkFactory.cxx.

66  {
70  ATH_MSG_NOCLASS(logger, "Creating ONNX network from file ..."
71  << input.substr(input.length() - 10));
72  std::unique_ptr<VNetworkBase> created(new TFCSONNXHandler(input));
73  return created;
74  } else {
75  try {
76  std::unique_ptr<VNetworkBase> created(new TFCSSimpleLWTNNHandler(input));
78  "Succedeed in creating LWTNN nn from string starting "
79  << input.substr(0, 10));
80  return created;
81  } catch (const boost::property_tree::ptree_bad_path &e) {
82  // If we get this error, it was actually a graph, not a NeuralNetwork
83  std::unique_ptr<VNetworkBase> created(new TFCSGANLWTNNHandler(input));
84  ATH_MSG_NOCLASS(logger, "Succedeed in creating LWTNN graph from string");
85  return created;
86  };
87  };
88 };

◆ create() [2/5]

std::unique_ptr< VNetworkBase > TFCSNetworkFactory::create ( std::string  input,
bool  graph_form 
)
static

Given a string, and information about format, make a network.

This function will first check if the string is the path of a readable file on the disk. If so, the file suffix is used to decide if it's an onnx (.onnx) or lwtnn file (.json). If the filepath ends in .* then first an onnx then an lwtnn file will be tried. The file is read and parsed into a network.

If the string is not a filepath, it is assumed to be the content of a json file to make an lwtnn network.

When an lwtnn network is created, if graph_form is true the network will be a TFCSSimpleLWTNNHandler otherwise it is a TFCSGANLWTNNHandler.

Parameters
inputEither a file path, or the content of a file.
graph_formIs the network the more complex graph form?

Definition at line 90 of file TFCSNetworkFactory.cxx.

91  {
95  ATH_MSG_NOCLASS(logger, "Creating ONNX network from file ..."
96  << input.substr(input.length() - 10));
97  std::unique_ptr<VNetworkBase> created(new TFCSONNXHandler(input));
98  return created;
99  } else if (graph_form) {
100  ATH_MSG_NOCLASS(logger, "Creating LWTNN graph from string");
101  std::unique_ptr<VNetworkBase> created(new TFCSGANLWTNNHandler(input));
102  return created;
103  } else {
104  std::unique_ptr<VNetworkBase> created(new TFCSSimpleLWTNNHandler(input));
105  ATH_MSG_NOCLASS(logger, "Creating LWTNN nn from string");
106  return created;
107  };
108 };

◆ create() [3/5]

std::unique_ptr< VNetworkBase > TFCSNetworkFactory::create ( std::vector< char > const input)
static

Given a vector of chars (bytes), make a network.

This function will always create a TFCSONNXHandler. Caution: this function is designed to modify its input.

Parameters
inputThe content of an onnx proto file.

Definition at line 58 of file TFCSNetworkFactory.cxx.

58  {
60  ATH_MSG_NOCLASS(logger, "Directly creating ONNX network from bytes length "
61  << input.size());
62  std::unique_ptr<VNetworkBase> created(new TFCSONNXHandler(input));
63  return created;
64 };

◆ create() [4/5]

std::unique_ptr< VNetworkBase > TFCSNetworkFactory::create ( std::vector< char > const vector_input,
std::string  string_input 
)
static

Create a network from whichever input isn't empty.

If the vector_input is not empty, construct a network from that, otherwise, use the string_input to construct a network.

Parameters
vector_inputThe content of an onnx proto file.
string_inputEither a file path, or the content of a file.

Definition at line 111 of file TFCSNetworkFactory.cxx.

112  {
114  ATH_MSG_NOCLASS(logger, "Given both bytes and a string to create an nn.");
115  resolveGlobs(string_input);
116  if (vector_input.size() > 0) {
118  "Bytes contains data, size=" << vector_input.size()
119  << ", creating from bytes.");
120  return create(vector_input);
121  } else if (string_input.length() > 0) {
122  ATH_MSG_NOCLASS(logger, "No data in bytes, string contains data, "
123  << "creating from string.");
124  return create(string_input);
125  } else {
126  throw std::invalid_argument(
127  "Neither vector_input nor string_input contain data");
128  };
129 };

◆ create() [5/5]

std::unique_ptr< VNetworkBase > TFCSNetworkFactory::create ( std::vector< char > const vector_input,
std::string  string_input,
bool  graph_form 
)
static

Create a network from whichever input isn't empty.

If the vector_input is not empty, construct a network from that, otherwise, use the string_input to construct a network. Whether the network is in graph form is specifed for LWTNN networks.

Parameters
vector_inputThe content of an onnx proto file.
string_inputEither a file path, or the content of a file.
graph_formIs the network the more compelx graph form?

Definition at line 132 of file TFCSNetworkFactory.cxx.

133  {
136  logger,
137  "Given both bytes, a string and graph form sepcified to create an nn.");
138  resolveGlobs(string_input);
139  if (vector_input.size() > 0) {
141  "Bytes contains data, size=" << vector_input.size()
142  << ", creating from bytes.");
143  return create(vector_input);
144  } else if (string_input.length() > 0) {
145  ATH_MSG_NOCLASS(logger, "No data in bytes, string contains data, "
146  << "creating from string.");
147  return create(string_input, graph_form);
148  } else {
149  throw std::invalid_argument(
150  "Neither vector_input nor string_input contain data");
151  };
152 };

◆ isOnnxFile()

bool TFCSNetworkFactory::isOnnxFile ( std::string const filename)
staticprivate

Check if a filename seems to be an onnx file.

Really just checks if the input ends in ".onnx"

Parameters
filenamePath to check.

Definition at line 42 of file TFCSNetworkFactory.cxx.

42  {
44  const std::string ending = ".onnx";
45  const int ending_len = ending.length();
46  const int filename_len = filename.length();
47  bool is_onnx;
48  if (filename_len < ending_len) {
49  is_onnx = false;
50  } else {
51  is_onnx =
52  (0 == filename.compare(filename_len - ending_len, ending_len, ending));
53  };
54  return is_onnx;
55 };

◆ resolveGlobs()

void TFCSNetworkFactory::resolveGlobs ( std::string &  filename)
staticprivate

If the filepath ends in .

  • change it to .onnx or .json

If the filepath doesn't end in .*, no change is made. Will check first for a .onnx file, then look for a .json. Throws an exception if niether are found.

Parameters
filenamePath to check.

Definition at line 19 of file TFCSNetworkFactory.cxx.

19  {
21  const std::string ending = ".*";
22  const int ending_len = ending.length();
23  const int filename_len = filename.length();
24  if (filename_len < ending_len) {
25  ATH_MSG_NOCLASS(logger, "Filename is implausably short.");
26  } else if (0 ==
27  filename.compare(filename_len - ending_len, ending_len, ending)) {
28  ATH_MSG_NOCLASS(logger, "Filename ends in glob.");
29  // Remove the glob
30  filename.pop_back();
31  if (std::filesystem::exists(filename + "onnx")) {
32  filename += "onnx";
33  } else if (std::filesystem::exists(filename + "json")) {
34  filename += std::string("json");
35  } else {
36  throw std::invalid_argument("No file found matching globbed filename " +
37  filename);
38  };
39  };
40 };

The documentation for this class was generated from the following files:
AllowedVariables::e
e
Definition: AsgElectronSelectorTool.cxx:37
ISF_FCS::MLogging
Cut down AthMessaging.
Definition: MLogging.h:176
TFCSGANLWTNNHandler
Copyright (C) 2002-2024 CERN for the benefit of the ATLAS collaboration.
Definition: TFCSGANLWTNNHandler.h:37
TFCSNetworkFactory::resolveGlobs
static void resolveGlobs(std::string &filename)
If the filepath ends in .
Definition: TFCSNetworkFactory.cxx:19
VNetworkBase::isFile
bool isFile() const
Check if the argument inputFile is the path of a file on disk.
Definition: VNetworkBase.cxx:117
TFCSSimpleLWTNNHandler
Copyright (C) 2002-2024 CERN for the benefit of the ATLAS collaboration.
Definition: TFCSSimpleLWTNNHandler.h:34
TFCSNetworkFactory::create
static std::unique_ptr< VNetworkBase > create(std::string input)
Given a string, make a network.
Definition: TFCSNetworkFactory.cxx:66
PlotPulseshapeFromCool.input
input
Definition: PlotPulseshapeFromCool.py:106
TFCSNetworkFactory::isOnnxFile
static bool isOnnxFile(std::string const &filename)
Check if a filename seems to be an onnx file.
Definition: TFCSNetworkFactory.cxx:42
ATH_MSG_NOCLASS
#define ATH_MSG_NOCLASS(logger_name, x)
Definition: MLogging.h:52
CaloCellTimeCorrFiller.filename
filename
Definition: CaloCellTimeCorrFiller.py:24
python.dummyaccess.exists
def exists(filename)
Definition: dummyaccess.py:9
python.iconfTool.gui.pad.logger
logger
Definition: pad.py:14
TFCSONNXHandler
Copyright (C) 2002-2024 CERN for the benefit of the ATLAS collaboration.
Definition: TFCSONNXHandler.h:43