// RogueViz -- SAG embedder: data manager // Copyright (C) 2011-24 Zeno Rogue, see 'hyper.cpp' for details #include "../rogueviz.h" #include #include namespace rogueviz { namespace sag { using namespace cells; edgetype *sag_edge; /** if this is true, no nodes are allowed to be on the same subcell */ bool allow_doubles = false; /** node i is on sagcells[sagid[i]] */ vector sagid; /** what node is on sagcells[i] (need loglik_repeat to be off) */ vector sagnode; /* separate hubs -- only for smClosest */ ld hub_penalty; string hub_filename; vector hubval; vector sagedges; vector> edges_yes, edges_no; ld edgepower=1, edgemul=1; void init(); void compute_cost(); void prepare_graph() { int DN = isize(sagid); println(hlog, "prepare_graph with DN = ", DN); set> alledges; for(auto e: sagedges) { if(e.i == e.j) continue; alledges.emplace(e.i, e.j); alledges.emplace(e.j, e.i); } edges_yes.clear(); edges_yes.resize(DN); edges_no.clear(); edges_no.resize(DN); for(int i=0; i qon(isize(sagcells), 0); for(int i=0; i qsf(isize(sagcells), 0); ld rad = .25 * cgi.scalefactor; if(isize(subcell_points) > 1) rad /= pow(isize(subcell_points), WDIM); for(int i=0; ibase = sagcells[ci].first; vdata[i].m->at = Id; if(allow_doubles) vdata[i].m->at = spin(TAU*(qsf[ci]++) / qon[ci]) * xpush(rad * (qon[ci]-1) / qon[ci]); if(isize(subcell_points) > 1) vdata[i].m->at = rgpushxto0(subcell_points[sagcells[ci].second]) * vdata[i].m->at; } } bool visualization_active; void forgetedges(int id) { for(int i=0; iorig = NULL; } void create_viz() { if(distance_only) return; int DN = isize(sagid); bool vact = state & SS_GRAPH; state |= SS_GRAPH; if(!vact) for(int i=0; i= SN || err < 1) sid = -1; if(!labeler.count(lab)) { printf("unknown vertex: %s\n", lab.c_str()); } else { int id = getid(lab); sagid[id] = sid; } } afterload: if(sf) fclose(sf); prepare_graph(); create_viz(); } void load_sag_solution_basic(const string& fname) { if(!(state & SS_DATA)) throw hr_exception("load_sag_solution_basic with no data"); FILE *f = fopen(fname.c_str(), "rt"); for(auto& i: sagid) if(fscanf(f, "%d", &i) < 1) throw hr_exception("read error in load_sag_solution_basic"); fclose(f); println(hlog, "loaded sagid = ", sagid); prepare_graph(); create_viz(); } void after_data() { state |= SS_DATA; init_snake_if_needed(); int DN = isize(vdata); int SN = isize(sagcells); if(SN < DN) { println(hlog, "SN = ", SN, " DN = ", DN); throw hr_exception("not enough cells for SAG"); } sagid.resize(DN); for(int i=0; i > edges; int all = 0, good = 0; while(!feof(f.f)) { string l1 = scan(f); string l2 = scan(f); if(l1 == "") continue; if(l2 == "") continue; edgeinfo ei(sag_edge); ei.i = getid(l1); ei.j = getid(l2); if(ei.i > ei.j) swap(ei.i, ei.j); all++; if(edges.count({ei.i, ei.j})) continue; good++; edges.emplace(ei.i, ei.j); ei.weight = 1; sagedges.push_back(ei); } println(hlog, "unweighted graph ", fname, " read successfully"); println(hlog, "N = ", isize(vdata), " edges = ", good, "/", all); after_data(); } void read_hubs(const string& fname) { if(!(state && SS_DATA)) throw hr_exception("read_hubs with no data"); hubval.resize(isize(vdata), -1); fhstream f(fname, "rt"); if(!f.f) { printf("Failed to open hub file: %s\n", fname.c_str()); exit(1); } println(hlog, "loading hubs: ", fname); while(!feof(f.f)) { string l1, l2; while(true) { int c = fgetc(f.f); if(c == EOF) return; else if(c == ';') break; else if(rv_ignore(c)) ; else l1 += c; } while(true) { int c = fgetc(f.f); if(c == EOF) return; else if(c == ';') return; else if(rv_ignore(c)) break; else l2 += c; } if(!id_known(l1)) { printf("label unknown: %s\n", l1.c_str()); exit(1); } hubval[getid(l1)] = atoi(l2.c_str()); } } void generate_fake_data(int n, int m) { if(state & SS_DATA) return; init_cells(); state |= SS_WEIGHTED; sagid.resize(n); for(int i=0; i n || m < 0) throw hr_exception("generate_fake_data parameters incorrect"); sagid.resize(m); int DN = isize(sagid); vdata.resize(DN); for(int i=0; i