예제 #1
0
    void GraphValidator::verify_output_socket_satisfied(const GraphNode &node, const InputSocket& input_socket, ValidationResults &results) const
    {
        auto possible_connection = input_socket.connection();
        if(!possible_connection)
        {
            if(!(node.is_graph_internal_node() && input_socket.optional()))
            {
                std::string text = boost::str(boost::format("Output node %1% is missing an input connection.") % node.display_name());
                results.add(text);
                return;
            }

            if(graph_.get_input_buffer(node.name()))
                return;

            std::string text = boost::str(boost::format("Output node %1% is missing an input connection, or is missing data being set directly on the Graph.") % node.display_name());
            results.add(text);
            return;
        }

        const Connection& connection = possible_connection->get();
        const OutputSocket& output = connection.output();

        if(output.parent() == nullptr)
            throw std::logic_error("Output socket did not have a parent! Internal error!");

        verify_node_satisfied(*output.parent(), results);
    }
예제 #2
0
int main(int argc, char *argv[])
{
    // Load the document that is to be validated
    rapidjson::Document targetDocument;
    if (!valijson::utils::loadDocument(argv[1], targetDocument)) {
        cerr << "Failed to load target document." << endl;
        return 1;
    }

    // Populate a schema
    Schema schema;
    addPropertiesConstraint(schema);
    addRequiredConstraint(schema);
    addTypeConstraint(schema);

    // Perform validation
    Validator validator(schema);
    ValidationResults results;
    RapidJsonAdapter targetDocumentAdapter(targetDocument);
    if (!validator.validate(targetDocumentAdapter, &results)) {
        std::cerr << "Validation failed." << endl;
        ValidationResults::Error error;
        unsigned int errorNum = 1;
        while (results.popError(error)) {
            cerr << "Error #" << errorNum << std::endl
                 << "  context: " << error.context << endl
                 << "  desc:    " << error.description << endl;
            ++errorNum;
        }
        return 1;
    }

    return 0;
}
예제 #3
0
int main(int argc, char *argv[])
{
    if (argc != 3) {
        cerr << "Usage: " << argv[0] << " <schema document> <test/target document>" << endl;
        return 1;
    }

    // Load the document containing the schema
    rapidjson::Document schemaDocument;
    if (!valijson::utils::loadDocument(argv[1], schemaDocument)) {
        cerr << "Failed to load schema document." << endl;
        return 1;
    }

    // Load the document that is to be validated
    rapidjson::Document targetDocument;
    if (!valijson::utils::loadDocument(argv[2], targetDocument)) {
        cerr << "Failed to load target document." << endl;
        return 1;
    }

    // Parse the json schema into an internal schema format
    Schema schema;
    SchemaParser parser;
    RapidJsonAdapter schemaDocumentAdapter(schemaDocument);
    try {
        parser.populateSchema(schemaDocumentAdapter, schema);
    } catch (std::exception &e) {
        cerr << "Failed to parse schema: " << e.what() << endl;
        return 1;
    }

    // Perform validation
    Validator validator(schema);
    validator.setStrict(false);
    ValidationResults results;
    RapidJsonAdapter targetDocumentAdapter(targetDocument);
    if (!validator.validate(targetDocumentAdapter, &results)) {
        std::cerr << "Validation failed." << endl;
        ValidationResults::Error error;
        unsigned int errorNum = 1;
        while (results.popError(error)) {
        
            std::string context;
            std::vector<std::string>::iterator itr = error.context.begin();
            for (; itr != error.context.end(); itr++) {
                context += *itr;
            }
            
            cerr << "Error #" << errorNum << std::endl
                 << "  context: " << context << endl
                 << "  desc:    " << error.description << endl;
            ++errorNum;
        }
        return 1;
    }

    return 0;
}
예제 #4
0
    void GraphValidator::verify_node_socket_satisfied(const GraphNode &node, const InputSocket& input_socket, ValidationResults &results) const
    {
        auto possible_connection = input_socket.connection();
        if(!possible_connection)
        {
            if(input_socket.optional() == false)
            {
                std::string text = boost::str(boost::format("Node %1% is missing an input connection for %2%, which is not optional.")
                                              % node.display_name() % input_socket.name());
                results.add(text);
            }
        }
        else
        {
            const Connection& connection = possible_connection->get();
            const OutputSocket& output = connection.output();

            if(output.parent() == nullptr)
                throw std::logic_error("Output socket did not have a parent! Internal error!");

            verify_node_satisfied(*output.parent(), results);
        }
    }
예제 #5
0
    void GraphValidator::verify_all_connections(ValidationResults &results) const
    {
        auto connections = graph_.connections();
        auto it = connections.begin();
        for(; it != connections.end(); ++it)
        {
            const Connection& connection = it->get();

            if(!connection.input_output_type_compatible())
            {
                std::string text;
                text += "Incompatible connection. Output type is ";
                text += connection.output().data_type().name_description();
                text += ", and input supports: ";

                for(auto& accepted_data_type : connection.input().accepted_types())
                {
                    text += accepted_data_type.get().name_description() + ", ";
                }

                results.add(text);
            }
        }
    }
예제 #6
0
void   JobValidation::EvaluateNode ()
{
  log.Level (9) << "  " << endl;
  log.Level (9) << "JobValidation::EvaluteNode JobId[" << jobId << "]" << endl;
  status = BinaryJobStatus::Started;

  bool  configFileFormatGood = true;
  
  TrainingConfiguration2Ptr  config = new TrainingConfiguration2 ();
  config->Load (configFileName, false, log);
  if  (!config->FormatGood ())
    configFileFormatGood;

  config->SetFeatureNums (features);
  config->C_Param (cParm);
  config->Gamma   (gammaParm);
  config->A_Param (aParm);
  config->SelectionMethod (processor->SelectionMethod ());
  
  switch  (processor->ResultType ())
  {
  case  FinalResultType::MfsFeaturesSel:
  case  FinalResultType::NoTuningAllFeatures:
  case  FinalResultType::MfsParmsTuned: 
  case  FinalResultType::MfsParmsTunedFeaturesSel: 
           config->MachineType (SVM_MachineType::OneVsOne);
           break;
    
  case  FinalResultType::BfsFeaturesSel:
  case  FinalResultType::BfsParmsTuned:
  case  FinalResultType::BfsFeaturesSelParmsTuned:
           config->MachineType (SVM_MachineType::BinaryCombos);
           break;
  }

  bool  cancelFlag = false;

  FeatureVectorListPtr  trainData       = processor->TrainingData ();
  FeatureVectorListPtr  validationData  = processor->ValidationData ();

  VectorDouble  trainDataMeans      = trainData->ExtractMeanFeatureValues ();
  VectorDouble  validationDataMeans = validationData->ExtractMeanFeatureValues ();


  CrossValidationPtr  crossValidation = new CrossValidation  
                                           (config,
                                            trainData,
                                            processor->MLClasses (),
                                            processor->NumOfFolds (),
                                            processor->AlreadyNormalized (),
                                            processor->FileDesc (),
                                            log,
                                            cancelFlag
                                           );

  delete  classedCorrectly;
  classedCorrectlySize = validationData->QueueSize ();
  classedCorrectly = new bool[classedCorrectlySize];

  crossValidation->RunValidationOnly (validationData, classedCorrectly, log);

  testAccuracy      = crossValidation->Accuracy ();
  testAccuracyNorm  = crossValidation->AccuracyNorm ();
  testAvgPredProb   = (float)crossValidation->AvgPredProb () * 100.0f;
  testFMeasure      = (float)crossValidation->ConfussionMatrix ()->FMeasure (processor->PositiveClass (), log);

  if  (processor->GradingMethod () == GradingMethodType::Accuracy)
    testGrade = testAccuracy;

  else if  (processor->GradingMethod () == GradingMethodType::AccuracyNorm)
    testGrade = testAccuracyNorm;

  else if  (processor->GradingMethod () == GradingMethodType::FMeasure)
    testGrade = testFMeasure;

  else
    testGrade = testAccuracy;

  testNumSVs  = crossValidation->NumOfSupportVectors ();

  {
    // Save results of this Split in Results file.
    processor->Block ();

    {
      uint  fn = 0;
      ofstream rl ("FinalResults.log", ios_base::app);
      rl << endl << endl
         << "ConfigFileName"          << "\t" << configFileName  << "\t" << "Format Good[" << (configFileFormatGood ? "Yes" : "No") << endl
         << "SummaryResultsFileName"  << "\t" << processor->SummaryResultsFileName () << endl
         << "Configuration CmdLine"   << "\t" << config->SVMparamREF (log).ToString ()   << endl
         << "ImagesPerClass"          << "\t" << config->ImagesPerClass ()            << endl
         << endl;

      rl << endl << endl
         << "Training Data Status" << endl
         << endl;
      trainData->PrintClassStatistics (rl);
      rl << endl << endl;


      rl << "TrainingDataMeans";
      for  (fn = 0;  fn < trainDataMeans.size ();  fn++)
        rl << "\t" << trainDataMeans[fn];
      rl << endl;

      rl << "ValidationDataMeans";
      for  (fn = 0;  fn < validationDataMeans.size ();  fn++)
        rl << "\t" << validationDataMeans[fn];
      rl << endl
         << endl;

      crossValidation->ConfussionMatrix ()->PrintConfusionMatrixTabDelimited (rl);
      rl << endl << endl << endl << endl;
      rl.close ();
    }

    {
      ofstream  f (processor->SummaryResultsFileName ().Str (), ios_base::app);
      ValidationResults r (processor->ResultType (), 
                           config, 
                           crossValidation,
                           trainData,
                           osGetHostName ().value_or ("*** unknown ***"),
                           classedCorrectlySize,
                           classedCorrectly,
                           this,
                           log
                          );
      r.Write (f);
      f.close ();
    }
    processor->EndBlock ();
  }

  delete  crossValidation;     crossValidation    = NULL;
  delete  config;              config = NULL;
  status = BinaryJobStatus::Done;
}  /* EvaluateNode */
예제 #7
0
    void GraphValidator::verify_no_cycles(ValidationResults &results) const
    {
        //http://en.wikipedia.org/wiki/Topological_sorting#Algorithms
        std::vector<int> sorted_nodes;
        std::vector<int> working_list;
        std::vector<int> edges;

        // Add all nodes without incoming edges
        for(auto& node : graph_.nodes())
        {
            bool has_incoming_edge = false;
            for(auto& socket_ref : node.get().inputs().all_sockets())
            {
                const InputSocket& socket = socket_ref.get();
                if(socket.connection())
                {
                    has_incoming_edge = true;
                    break;
                }
            }
            if(has_incoming_edge == false)
                working_list.push_back(node.get().id());
        }

        // Input buffer nodes are considered automatically to have no incoming edges
        for(auto& node : graph_.input_nodes())
        {
            working_list.push_back(node.get().id());
        }

        // Add all connections as edges
        for(const Connection& connection : graph_.connections())
        {
            edges.push_back(connection.id());
        }

        while(working_list.size() > 0)
        {
            int node_id = working_list.back();
            working_list.pop_back();
            sorted_nodes.push_back(node_id);
            const GraphNode& node = graph_.get_node_by_id(node_id)->get();

            for(const OutputSocket& socket : node.outputs().all_sockets())
            {
                for(const Connection& connection : socket.connections())
                {
                    edges.erase(std::remove(edges.begin(), edges.end(), connection.id()), edges.end());

                    const GraphNode& node_m = *connection.input().parent();
                    bool has_other_incoming_edges = false;

                    for(const InputSocket& m_input : node_m.inputs().all_sockets())
                    {
                        auto connection = m_input.connection();
                        if(connection)
                        {
                            bool connection_has_incoming_edge = std::find(edges.begin(), edges.end(), connection->get().id()) != edges.end();
                            if(connection_has_incoming_edge)
                                has_other_incoming_edges = true;
                        }
                    }

                    if(has_other_incoming_edges == false)
                        working_list.push_back(node_m.id());
                }
            }
        }

        if(edges.size() > 0)
        {
            results.add("Cycle detected in graph. Graph must be an acyclic directed graph.");
        }
    }
예제 #8
0
TEST_F(TestValidationErrors, AllOfConstraintFailure)
{
    // Load schema document
    rapidjson::Document schemaDocument;
    ASSERT_TRUE( loadDocument(TEST_DATA_DIR "/schemas/allof_integers_and_numbers.schema.json", schemaDocument) );
    RapidJsonAdapter schemaAdapter(schemaDocument);

    // Parse schema document
    Schema schema;
    SchemaParser schemaParser;
    ASSERT_NO_THROW( schemaParser.populateSchema(schemaAdapter, schema) );

    // Load test document
    rapidjson::Document testDocument;
    ASSERT_TRUE( loadDocument(TEST_DATA_DIR "/documents/array_doubles_1_2_3.json", testDocument) );
    RapidJsonAdapter testAdapter(testDocument);

    Validator validator;
    ValidationResults results;
    EXPECT_FALSE( validator.validate(schema, testAdapter, &results) );

    ValidationResults::Error error;

    EXPECT_TRUE( results.popError(error) );
    EXPECT_EQ( size_t(2), error.context.size() );
    EXPECT_EQ( "<root>", error.context[0] );
    EXPECT_EQ( "[0]", error.context[1] );
    EXPECT_EQ( "Value type not permitted by 'type' constraint.", error.description );

    EXPECT_TRUE( results.popError(error) );
    EXPECT_EQ( size_t(1), error.context.size() );
    EXPECT_EQ( "<root>", error.context[0] );
    EXPECT_EQ( "Failed to validate item #0 in array.", error.description );

    EXPECT_TRUE( results.popError(error) );
    EXPECT_EQ( size_t(2), error.context.size() );
    EXPECT_EQ( "<root>", error.context[0] );
    EXPECT_EQ( "[1]", error.context[1] );
    EXPECT_EQ( "Value type not permitted by 'type' constraint.", error.description );

    EXPECT_TRUE( results.popError(error) );
    EXPECT_EQ( size_t(1), error.context.size() );
    EXPECT_EQ( "<root>", error.context[0] );
    EXPECT_EQ( "Failed to validate item #1 in array.", error.description );

    EXPECT_TRUE( results.popError(error) );
    EXPECT_EQ( size_t(2), error.context.size() );
    EXPECT_EQ( "<root>", error.context[0] );
    EXPECT_EQ( "[2]", error.context[1] );
    EXPECT_EQ( "Value type not permitted by 'type' constraint.", error.description );

    EXPECT_TRUE( results.popError(error) );
    EXPECT_EQ( size_t(1), error.context.size() );
    EXPECT_EQ( "<root>", error.context[0] );
    EXPECT_EQ( "Failed to validate item #2 in array.", error.description );

    EXPECT_TRUE( results.popError(error) );
    EXPECT_EQ( size_t(1), error.context.size() );
    EXPECT_EQ( "<root>", error.context[0] );
    EXPECT_EQ( "Failed to validate against child schema #0.", error.description );

    EXPECT_FALSE( results.popError(error) );

    while (results.popError(error)) {
        //std::cerr << error.context << std::endl;
        std::cerr << error.description << std::endl;
    }
}