Beispiel #1
0
    def runTest(self):
        self.assertTrue(self.g1 == self.g1)

        g1_other = g1_graph.G1(0, G_in=self.g1)
        self.assertTrue(self.g1 == g1_other)

        v0_pos = np.array(self.g1.get_position(0))
        v3_pos = np.array(self.g1.get_position(3))

        g1_other.set_position(0, v3_pos)
        g1_other.set_position(0, v0_pos)
        self.assertTrue(self.g1 == g1_other)

        g1_other.set_orientation(0, np.array([2.0, 0.0, 0.0]))
        self.assertFalse(self.g1 == g1_other)
        g1_other.set_orientation(0, self.orientation)

        try:
            g1_other.set_position(3, np.array([0.0, 0.0, 0.0]))
            self.g1 == g1_other
            self.assertTrue(False)
        except ValueError:
            pass

        g1_other.set_position(3, np.array([0.0, 0.0, 2.0]))
        self.assertFalse(g1_other == self.g1)
Beispiel #2
0
    def __roi_to_g1(self, vertices, edges):
        if not vertices:
            raise ValueError("Vertex list is empty")

        g1 = g1_graph.G1(len(vertices), init_empty=False)
        # Init index map, global <-> local:
        index_map = {}
        index_map_inv = {}

        # Flag solved and selected edges & vertices:
        g1.new_vertex_property("selected", dtype="bool", value=False)
        g1.new_vertex_property("solved", dtype="bool", value=False)

        g1.new_edge_property("selected", dtype="bool", value=False)
        g1.new_edge_property("solved", dtype="bool", value=False)

        partner = {}
        n = 0
        for v in vertices:
            g1.set_position(n, np.array([v["px"], v["py"], v["pz"]]))
            g1.set_orientation(n, np.array([v["ox"], v["oy"], v["oz"]]))

            if v["selected"]:
                g1.select_vertex(n)

            if v["solved"]:
                g1.solve_vertex(n)

            partner[n] = v["id_partner"]
            index_map[v["id"]] = n
            index_map_inv[n] = v["id"]

            n += 1

        index_map[-1] = -1
        index_map_inv[-1] = -1

        for v in g1.get_vertex_iterator():
            g1.set_partner(v, index_map[partner[v]])

        for e in edges:
            v0 = index_map[np.uint64(e["id0"])]
            v1 = index_map[np.uint64(e["id1"])]
            e_g1 = g1.add_edge(v0, v1)

            try:
                if e["cost"] is not None:
                    g1.set_edge_cost(e_g1, float(e["cost"]))
            except KeyError:
                pass

            if e["selected"]:
                g1.select_edge(e_g1)

            if e["solved"]:
                g1.solve_edge(e_g1)

        return g1, index_map_inv
Beispiel #3
0
    def setUp(self):
        self.g1_vertices_N = 3

        self.g1 = g1_graph.G1(self.g1_vertices_N)
        self.g1.set_position(0, np.array([0.0, 0.0, 0.0]))
        self.g1.set_position(1, np.array([0.0, 1.0, 0.0]))
        self.g1.set_position(2, np.array([1.0, 1.0, 0.0]))
        self.g1.add_edge(0, 1)
        self.g1.add_edge(1, 2)

        self.orientation = np.array([1.0, 0.0, 0.0])

        for v in self.g1.get_vertex_iterator():
            self.g1.set_orientation(v, self.orientation)

        self.g1_high = g1_graph.G1(self.g1_vertices_N)
        self.g1_high.set_position(0, np.array([0.0, 0.0, 0.0]))
        self.g1_high.set_position(1, np.array([0.0, 1.0, 0.0]))
        self.g1_high.set_position(2, np.array([1.0, 0.0, 0.0]))
        self.g1_high.add_edge(0, 1)
        self.g1_high.add_edge(1, 2)
Beispiel #4
0
def generate_bending(angle, radius):
    g1_vertices_N = 3
    g1 = g1_graph.G1(g1_vertices_N)
    g1.set_position(0, np.array([0.0, 0.0, 0.0]))
    g1.set_position(1, np.array([0.0, radius, 0.0]))

    x = np.sin(angle) * radius
    y = np.cos(angle) * radius
    g1.set_position(2, np.array([0.0, y, x]))
    g1.add_edge(0, 1)
    g1.add_edge(0, 2)

    orientation = np.array([1.0, 0.0, 0.0])

    for v in g1.get_vertex_iterator():
        g1.set_orientation(v, orientation)

    return g1, x, y
Beispiel #5
0
    def setUp(self):
        self.g1_vertices_N = 4
        
        self.g1 = g1_graph.G1(self.g1_vertices_N)
        
        self.g1.set_position(0, np.array([0.0, 0.0, 0.0]))
        self.g1.set_position(1, np.array([0.0, 1.0, 0.0]))
        self.g1.set_position(2, np.array([1.0, 1.0, 0.0]))
        self.g1.set_position(3, np.array([1.0, 0.0, 0.0]))

        self.g1.add_edge(0, 1)
        self.g1.add_edge(1, 2)
        self.g1.add_edge(2, 3)
        self.g1.add_edge(3, 0)

        self.orientation = np.array([1.0, 0.0, 0.0])

        for v in self.g1.get_vertex_iterator():
            self.g1.set_orientation(v, self.orientation)
    
        self.graph_converter = graph_converter.GraphConverter(self.g1)
        self.g2, self.index_maps = self.graph_converter.get_g2_graph()

        self.vertex_cost_params = {}
        self.edge_cost_params = {"orientation_factor": 10.0,
                                 "start_edge_prior": 0.0}
        self.edge_combination_cost_params = {"comb_angle_factor": 1.0}

        self.selection_cost = -100.0


        c_converter = cost_converter.CostConverter(self.g1,
                                                   self.vertex_cost_params,
                                                   self.edge_cost_params,
                                                   self.edge_combination_cost_params,
                                                   self.selection_cost)


        g2_cost = c_converter.get_g2_cost(self.g2, 
                                          self.index_maps)

        for v in self.g2.get_vertex_iterator():
            self.g2.set_cost(v, g2_cost[v])
Beispiel #6
0
def candidates_to_g1(candidates, voxel_size):
    g1 = g1_graph.G1(len(candidates))

    id = 0
    for candidate in candidates:
        assert (candidate.identifier == id)  # Partner derived from identifier

        position_phys = np.array(
            [candidate.position[j] * voxel_size[j] for j in range(3)])
        orientation_phys = np.array(
            [candidate.orientation[j] * voxel_size[j] for j in range(3)])
        partner = candidate.partner_identifier

        g1.set_position(id, position_phys)
        g1.set_orientation(id, orientation_phys)
        g1.set_partner(id, partner)

        id += 1

    return g1
Beispiel #7
0
 def setUp(self):
     self.g1_vertices_N = 10
     self.g1 = g1_graph.G1(self.g1_vertices_N)
Beispiel #8
0
def solve(g1,
          start_edge_prior,
          orientation_factor,
          comb_angle_factor,
          selection_cost,
          time_limit,
          output_dir=None,
          voxel_size=None,
          chunk_shift=np.array([0.,0.,0.]),
          backend="Gurobi"):

    """
    Base solver given a g1 graph.
    """

    vertex_cost_params = {}
    edge_cost_params = {"orientation_factor": orientation_factor,
                        "start_edge_prior": start_edge_prior}

    edge_combination_cost_params = {"comb_angle_factor": comb_angle_factor}

    

    if isinstance(g1, str):
        g1_tmp = g1_graph.G1(0) # initialize empty G1 graph
        g1_tmp.load(g1) # load from file
        g1 = g1_tmp

    if g1.get_number_of_edges() == 0:
        raise Warning("Graph has no edges.")


    logger.info("Get G2 graph...")
    g_converter = graph_converter.GraphConverter(g1)
    g2, index_maps = g_converter.get_g2_graph()

    logger.info("Get G2 costs...")
    c_converter = cost_converter.CostConverter(g1,
                                               vertex_cost_params,
                                               edge_cost_params,
                                               edge_combination_cost_params,
                                               selection_cost)
    g2_cost = c_converter.get_g2_cost(g2, index_maps)

    logger.info("Set G2 costs...")
    for v in g2.get_vertex_iterator():
        g2.set_cost(v, g2_cost[v])

    logger.info("Create ILP...")
    solver = g2_solver.G2Solver(g2, backend=backend)
    
    logger.info("Solve ILP...")
    g2_solution = solver.solve(time_limit=time_limit)

    if len(g2_solution) != solver.g2_vertices_N:
        raise ValueError("Model infeasible")

    logger.info("Get G1 solution...")
    g1_solution = g2_to_g1_solution(g2_solution, 
                                    g1, 
                                    g2, 
                                    index_maps, 
                                    chunk_shift=chunk_shift)
   

    if output_dir is not None:
        assert(voxel_size is not None)

        logger.info("Save solution...")
        if not os.path.exists(output_dir):
            os.makedirs(output_dir)
            
        g1_to_nml(g1_solution, 
                  output_dir + "g1s_kno.nml", 
                  knossos=True, 
                  voxel_size=voxel_size)

        g1_to_nml(g1_solution, 
                  output_dir + "g1s_phy.nml")

        g1_to_nml(g1_solution, 
                  output_dir + "g1s_vox.nml", 
                  voxel=True, 
                  voxel_size=voxel_size)

 
        g1_solution.save(output_dir + "g1s.gt")

        meta_data = {"tl": time_limit,
                     "vs": voxel_size,
                     "ec": edge_cost_params,
                     "ecc": edge_combination_cost_params,
                     "sc": selection_cost}

        with open(output_dir + "meta.json", "w+") as meta:
            json.dump(meta_data, meta)

    return g1_solution
Beispiel #9
0
    def runTest(self):
        cores = []

        line_range = self.line_end - self.line_start

        i = 0
        for z in range(int(self.line_start[2]),
                       int(self.line_end[2]) + 10, 10):
            x_lim = self.roi_x
            y_lim = self.roi_y
            z_lim = {"min": z - 5, "max": z + 5}

            core = Core(x_lim=x_lim,
                        y_lim=y_lim,
                        z_lim=z_lim,
                        context=[8, 8, 8],
                        core_id=i,
                        nbs=[i + 1, i - 1])
            cores.append(core)
            i += 1

        graph = self.db.get_client(self.name_db, self.collection)

        i = 0
        for core in cores:
            print "Solve Core {}".format(i)
            solve_core(core,
                       self.name_db,
                       self.collection,
                       cc_min_vertices=0,
                       start_edge_prior=10,
                       selection_cost=-10000,
                       distance_factor=0.0,
                       orientation_factor=100.0,
                       comb_angle_factor=100.0,
                       time_limit=None,
                       voxel_size=[1., 1., 1.],
                       backend="Gurobi")
            i += 1

            #graph.update_many({"solved": True}, {"$set": {"solved": False}})

        g1, index_map = self.db.get_g1(self.name_db, self.collection,
                                       self.roi_x, self.roi_y, self.roi_z)

        selected, index_map = self.db.get_selected(self.name_db,
                                                   self.collection,
                                                   x_lim=self.roi_x,
                                                   y_lim=self.roi_y,
                                                   z_lim=self.roi_z)

        for v in selected.get_vertex_iterator():
            self.assertTrue(len(selected.get_incident_edges(v)) <= 2)

        ccs_selected = selected.get_components(min_vertices=0,
                                               output_folder=None,
                                               return_graphs=True)

        g1_line = g1_graph.G1(len(self.line_points))
        i = 0
        for v in g1_line.get_vertex_iterator():
            g1_line.set_position(v, self.line_points[i])
            g1_line.set_orientation(v, self.line_orientations[i])
            g1_line.set_partner(v, -1)
            if i != 99:
                g1_line.add_edge(i, i + 1)
            i += 1

        self.assertTrue(selected.get_number_of_vertices() == 100)
        self.assertTrue(selected.get_number_of_edges() == 99)
        self.assertTrue(len(ccs_selected) == 1)
        self.assertTrue(g1_line == selected)