import * as $N from "@/core/base/BaseNode" ; import * as $E from "@/core/base/BaseEdge" ; import * as $G from "@/core/base/BaseGraph" ; import { JSONInput , IJSONInConfig } from "@/io/input/JSONInput" ; import { JSONOutput } from "@/io/output/JSONOutput" ; import * as $EME from "@/energyminimization/ExpansionBoykov" ; import { CSV_DATA_PATH , JSON_DATA_PATH } from "_/config/test_paths" ; let json = new JSONInput ( { explicit_direction : false , directed : false , weighted : true } ) , eme_graph = `${ JSON_DATA_PATH }/energy_minimization_expansion_graph.json` , graph , labels = [ "1" , "2" ] , eme ; /**
 *
 */ describe ( "EME Boykov Tests - " , ( ) => { beforeEach ( ( ) => { graph = json . readFromJSONFile ( eme_graph ) ; eme = new $EME . EMEBoykov ( graph , labels ) ; } ) ; describe ( "Base Tests - " , ( ) => { test ( "should instantiate a standard config" , ( ) => { expect ( eme . prepareEMEStandardConfig ( ) ) . toHaveProperty ( "directed" ) ; expect ( eme . prepareEMEStandardConfig ( ) ) . toHaveProperty ( "labeled" ) ; expect ( eme . prepareEMEStandardConfig ( ) ) . toHaveProperty ( "interactionTerm" ) ; expect ( eme . prepareEMEStandardConfig ( ) ) . toHaveProperty ( "dataTerm" ) ; expect ( eme . prepareEMEStandardConfig ( ) . directed ) . toBe ( false ) ; expect ( eme . prepareEMEStandardConfig ( ) . labeled ) . toBe ( false ) ; expect ( eme . prepareEMEStandardConfig ( ) . interactionTerm ) . toBeInstanceOf ( Function ) ; expect ( eme . prepareEMEStandardConfig ( ) . dataTerm ) . toBeInstanceOf ( Function ) ; } ) ; } ) ; describe ( "Helper Functions Tests - " , ( ) => { test ( "should correctly initialize the graph with the correct labels" , ( ) => { let lGraph = eme . initGraph ( graph ) ; let nodes = lGraph . getNodes ( ) ; for ( let i = 0 ; i < Object . keys ( nodes ) . length ; i ++ ) { let node = nodes [ Object . keys ( nodes ) [ i ] ] ; expect ( node . getLabel ( ) ) . toBe ( node . getFeature ( "label" ) ) ; } } ) ; test ( "should correctly construct the expansion graph" , ( ) => { let eGraph = eme . constructGraph ( ) ; let nodes = eGraph . getNodes ( ) ; let edges = eGraph . getDirEdges ( ) ; let pixel_nodes = Object . keys ( graph . getNodes ( ) ) ; expect ( pixel_nodes ) . toEqual ( expect . arrayContaining ( [ "A" , "B" , "C" , "D" , "E" , "F" , "G" , "H" , "I" ] ) ) ; let aux_nodes_ind = Object . keys ( nodes ) . filter ( element => pixel_nodes . concat ( [ "SOURCE" , "SINK" ] ) . indexOf ( element ) < 0 ) ; let non_zero_nodes = [ "A" , "C" , "D" , "G" ] ; let zero_nodes = [ "B" , "E" , "F" , "H" , "I" ] ; expect ( Object . keys ( nodes ) . length ) . toBe ( 16 ) ; expect ( Object . keys ( edges ) . length ) . toBe ( 80 ) ; non_zero_nodes . forEach ( element => { let edge = eGraph . getDirEdgeByNodeIDs ( element , "SINK" ) ; expect ( edge . getWeight ( ) ) . toBe ( 0 ) ; } ) ; zero_nodes . forEach ( element => { let edge = eGraph . getDirEdgeByNodeIDs ( element , "SINK" ) ; expect ( edge . getWeight ( ) ) . toBe ( Infinity ) ; } ) ; aux_nodes_ind . forEach ( element => { let edge = eGraph . getDirEdgeByNodeIDs ( element , "SINK" ) ; expect ( edge . getWeight ( ) ) . toBe ( 1 ) ; } ) ; non_zero_nodes . forEach ( element => { let edge = eGraph . getDirEdgeByNodeIDs ( element , "SOURCE" ) ; expect ( edge . getWeight ( ) ) . toBe ( 1.5 ) ; } ) ; zero_nodes . forEach ( element => { let edge = eGraph . getDirEdgeByNodeIDs ( element , "SOURCE" ) ; expect ( edge . getWeight ( ) ) . toBe ( 0 ) ; } ) ; expect ( eGraph . getDirEdgeByNodeIDs ( "E" , "F" ) . getWeight ( ) ) . toBe ( 0 ) ; expect ( eGraph . getDirEdgeByNodeIDs ( "A" , "D" ) . getWeight ( ) ) . toBe ( 1 ) ; expect ( eGraph . getDirEdgeByNodeIDs ( "B" , "E" ) . getWeight ( ) ) . toBe ( 0 ) ; expect ( eGraph . getDirEdgeByNodeIDs ( "D" , "G" ) . getWeight ( ) ) . toBe ( 1 ) ; expect ( eGraph . getDirEdgeByNodeIDs ( "E" , "H" ) . getWeight ( ) ) . toBe ( 0 ) ; expect ( eGraph . getDirEdgeByNodeIDs ( "F" , "I" ) . getWeight ( ) ) . toBe ( 0 ) ; expect ( eGraph . getDirEdgeByNodeIDs ( "H" , "I" ) . getWeight ( ) ) . toBe ( 0 ) ; } ) ; } ) ; describe ( "Simple Potts Test - " , ( ) => { test ( "should solve the potts model" , ( ) => { let pGraph = eme . calculateCycle ( ) . graph ; let nodes = pGraph . getNodes ( ) ; expect ( nodes [ "A" ] . getLabel ( ) ) . toBe ( "2" ) ; expect ( nodes [ "B" ] . getLabel ( ) ) . toBe ( "1" ) ; expect ( nodes [ "C" ] . getLabel ( ) ) . toBe ( "1" ) ; expect ( nodes [ "D" ] . getLabel ( ) ) . toBe ( "2" ) ; expect ( nodes [ "E" ] . getLabel ( ) ) . toBe ( "1" ) ; expect ( nodes [ "F" ] . getLabel ( ) ) . toBe ( "1" ) ; expect ( nodes [ "G" ] . getLabel ( ) ) . toBe ( "2" ) ; expect ( nodes [ "H" ] . getLabel ( ) ) . toBe ( "1" ) ; expect ( nodes [ "I" ] . getLabel ( ) ) . toBe ( "1" ) ; } ) ; } ) ; } ) ;