From 77695e8f2219ec7c8837dad99c30ed7a72d11a57 Mon Sep 17 00:00:00 2001 From: Joshua Moerman Date: Tue, 31 Mar 2015 11:40:56 +0200 Subject: [PATCH] Rewrites pre_gephi_tool to handle more hypotheses --- src/pre_gephi_tool.cpp | 81 +++++++++++++++++++++++++++++------------- 1 file changed, 56 insertions(+), 25 deletions(-) diff --git a/src/pre_gephi_tool.cpp b/src/pre_gephi_tool.cpp index 4a31a68..9485c09 100644 --- a/src/pre_gephi_tool.cpp +++ b/src/pre_gephi_tool.cpp @@ -1,64 +1,95 @@ -#include #include +#include #include -#include -#include +#include + #include #include #include +#include #include using namespace std; +using namespace boost; int main(int argc, char *argv[]){ - if(argc != 3) return 1; + if(argc != 5) return argc; - const string hypo_filename = argv[1]; - const string mach_filename = argv[2]; + // Program options + const string machince_filename = argv[1]; + const string machince_positions_filename = argv[2]; + const string hypo_filename_pattern = argv[3]; + const size_t maximal_hypothesis = stoul(argv[4]); + // Read all the hypothesis translation t; + vector hypotheses; + for(size_t i = 0; i <= maximal_hypothesis; ++i){ + clog << "Reading hypo " << i << endl; + string hypothesis_filename = hypo_filename_pattern; + auto it = hypothesis_filename.find('%'); + hypothesis_filename.replace(it, 1, to_string(i)); + hypotheses.push_back(read_mealy_from_dot(hypothesis_filename, t)); + } - const auto hypothesis = read_mealy_from_dot(hypo_filename, t); - const auto machine = read_mealy_from_dot(mach_filename, t); + const auto machine = read_mealy_from_dot(machince_filename, t); auto input_to_string = create_reverse_map(t.input_indices); auto output_to_string = create_reverse_map(t.output_indices); - vector visited(machine.graph_size, false); - { - const auto state_cover = create_transfer_sequences(hypothesis, 0); + // Read the positions by gephi, indexed by state + // (export to .net file and then `tail +2 Untitled.net | awk '{print $3, $4}' > positions.txt`) + vector> positions(machine.graph_size); + ifstream position_file(machince_positions_filename); + for(auto & p : positions){ + position_file >> p.first >> p.second; + } + + // Visit all states and record in which hypo it is reached + vector> visited(machine.graph_size); + for(size_t i = 0; i <= maximal_hypothesis; ++i){ + clog << "Visiting hypo " << i << endl; + const auto state_cover = create_transfer_sequences(hypotheses[i], 0); for(auto && p : state_cover){ state s = 0; - for(auto && i : p){ - visited[s.base()] = true; - s = apply(machine, s, i).to; + for(auto && inp : p){ + if(!visited[s.base()]) visited[s.base()] = i; + s = apply(machine, s, inp).to; } - visited[s.base()] = true; + if(!visited[s.base()]) visited[s.base()] = i; } } - { - ofstream out("gephi.dot"); - out << "digraph g {\n"; + // Output a dot per hypo, making a movie + for(size_t i = 0; i < hypotheses.size(); ++i){ + clog << "Saving frame " << i << endl; + string hypothesis_filename = hypo_filename_pattern + ".movie"; + auto it = hypothesis_filename.find('%'); + hypothesis_filename.replace(it, 1, to_string(i)); + + ofstream out(hypothesis_filename); + out << "digraph {\n"; for(state s = 0; s < machine.graph_size; ++s){ + bool is_visited = visited[s.base()] ? (visited[s.base()].value() <= i) : false; out << "\t" << "s" << s << " ["; - out << "visited=\"" << (visited[s.base()] ? "1" : "0") << "\""; + out << "color=\"" << (is_visited ? "green" : "red") << "\"" << ", "; + out << "pos=\"" << positions[s.base()].first << "," << positions[s.base()].second << "\""; out << "]\n"; } for(state s = 0; s < machine.graph_size; ++s){ + vector visited(machine.graph_size, false); + visited[s.base()] = true; for(input i = 0; i < machine.input_size; ++i){ - const auto ret = apply(machine, s, i); - out << "\t" << "s" << s << " -> " << "s" << ret.to << " ["; - out << "input=\"" << input_to_string[i.base()] << "\", "; - out << "output=\"" << output_to_string[ret.output.base()] << "\""; - out << "]\n"; + const auto t = apply(machine, s, i).to; + if(visited[t.base()]) continue; + out << "\t" << "s" << s << " -> " << "s" << t << "\n"; + visited[t.base()] = true; } } out << "}" << endl; } } -