Esempio n. 1
0
def main(in_shp, out_shp, bool_error=False):
    """
    The main processing module for the Find Subnetworks tool.
    :param in_shp: Stream network polyline feature class.
    :param out_workspace: Directory where tool output will be stored
    """
    arcpy.AddMessage("FSN: Finding and labeling subnetworks...")
    # remove GNAT fields if present
    for f in arcpy.ListFields(in_shp):
        if f.name[:1] == "_" and f.name[1:] == "_":
            arcpy.AddMessage(
                "FSN: Deleting and replacing existing GNAT fields...")
            arcpy.DeleteField_management(in_shp, f.name)

    # calculate network ID
    theNetwork = net.Network(in_shp)
    list_SG = theNetwork.get_subgraphs()
    id_G = theNetwork.calc_network_id(list_SG)

    # find topology errors
    if bool_error:
        arcpy.AddMessage("FSN: Finding network topology errors...")
        error_G = find_errors(theNetwork, id_G)
        final_G = error_G
    else:
        final_G = id_G

    arcpy.AddMessage("FSN: Writing networkx graph to shapefile...")
    theNetwork._nx_to_shp(final_G, out_shp, bool_node=False)

    return
Esempio n. 2
0
    def classify(self):
        w = ['observation', 'prediction', 'target']
        window = Window(*[int(self.config['window'][x]) for x in w])

        #
        # Create the network of nodes (the source and its neighbors)
        #
        self.log.info('{0} + create network'.format(self.nid))
        n = self.config['neighbors']
        cluster = self.nhandler[n['selection']]
        self.network = nt.Network(self.nid, int(n['depth']), cluster)
        self.log.info('{0} - create network'.format(self.nid))

        depth = self.network.depth()
        if depth != int(n['depth']):
            msg = 'Depth not fully explored: {0} < {1}'
            raise ValueError(msg.format(depth, n['depth']))

        root = self.network.node
        assert (root.nid == self.nid)

        #
        # Clean up the nodes: All nodes are shifted, interpolated, and
        # time-aligned with the root. Missing values at the root are
        # maintained to determine which windows are valid to process.
        #
        s = root.readings.speed
        missing = s[s.isnull()].index

        for i in self.network:
            n = i.node
            n.readings.shift(i.lag)
            n.align(root, True)

        #
        # Build the observation matrix by looping over the root nodes
        # time periods
        #
        self.log.info('{0} + observe'.format(self.nid))
        observations = []
        for i in root.range(window):
            whole = [missing.intersection(x).size == 0 for x in i]
            if all(whole):
                (left, right) = i
                mid = left[-len(right):]
                assert (len(mid) == len(right))

                labels = self._labels(root, mid, right)
                features = self._features(self.network.nodes(), left)

                observations.append(features + labels)
        self.log.info('{0} - observe {1}'.format(self.nid, len(observations)))

        return observations
import lib.path as path
import lib.utils as utils
from datetime import datetime

if __name__ == '__main__':

    def save_loss(loss_arr, loss_type):
        save_dir = net.get_epoch_dir()
        np.save("{}/{}_loss.npy".format(save_dir, loss_type), loss_arr)
        plt.plot(np.array(loss_arr).flat)
        plt.savefig("{}/plot_{}_loss.png".format(save_dir, loss_type),
                    bbox_inches='tight')
        plt.close()

    # init network
    net = network.Network()
    net.init()

    # get preprocessed data
    data_all, label_all = dataset.get_preprocessed_dataset()

    # split dataset
    X_train, y_train, X_val, y_val, X_test, y_test = dataset.train_val_test_split(
        data_all, label_all)

    np.save("{}/X_test.npy".format(net.MODEL_DIR), X_test)
    np.save("{}/y_test.npy".format(net.MODEL_DIR), y_test)

    print("training loop")
    # train network
    train_loss, val_loss, test_loss = [], [], []
Esempio n. 4
0
def metrics_network():
    return network.Network().json
Esempio n. 5
0
def main(in_shp, field_name, out_shp, riverkm_bool=False):
    """
    Iterates through all identified subnetworks and generates network attributes
    which are added as new attribute fields.
    :param in_shp: input stream network shapefile
    :param out_workspace: directory where output files will be written
    """

    # attribute fields
    edgetype = "_edgetype_"
    netid = "_netid_"
    streamname = field_name

    arcpy.AddMessage("GNA: Generating network attributes...")
    arcpy.AddMessage("GNA: Converting shapefile to a NetworkX graph...")
    #prep_shp = prep_network(in_shp, temp_workspace)
    theNetwork = net.Network(in_shp)
    arcpy.AddMessage("GNA: Getting list of network IDs...")

    if theNetwork.check_attribute(theNetwork.G, netid):
        net_ids = theNetwork.attribute_as_list(theNetwork.G, netid)
    else:
        net_ids = []
        arcpy.AddError("ERROR: {} attribute field not found! The network should first be processed through"
                       "the Find Subnetworks Tool.".format(netid))

    if theNetwork.check_attribute(theNetwork.G, edgetype):
        theNetwork.delete_attribute(theNetwork.G, edgetype)

    # iterate through list of network IDs generate attributes, and produce a subnetwork graph
    list_subnets = []
    for id in net_ids:
        subnet_G = theNetwork.select_by_attribute(theNetwork.G, netid, id)
        theNetwork.add_attribute(subnet_G, edgetype, "connector")
        outflow_G = theNetwork.get_outflow_edges(subnet_G, edgetype, "outflow")
        headwater_G = theNetwork.get_headwater_edges(subnet_G, edgetype, "headwater")
        braid_complex_G = theNetwork.get_complex_braids(subnet_G, edgetype, "braid")
        braid_simple_G = theNetwork.get_simple_braids(subnet_G, edgetype, "braid")
        arcpy.AddMessage("GNA: Merging all edge types...")
        gnat_G = theNetwork.merge_subgraphs(subnet_G,
                                            outflow_G,
                                            headwater_G,
                                            braid_complex_G,
                                            braid_simple_G)
        stream_field = arcpy.ListFields(in_shp, streamname)
        if len(stream_field) != 0:
            theNetwork.set_mainflow(gnat_G, streamname)
        theNetwork.set_node_types(gnat_G)

        # River kilometers
        if riverkm_bool == True:
            arcpy.AddMessage("GNA: Calculating river kilometers...")
            theNetwork.calculate_river_km(gnat_G)

        list_subnets.append(gnat_G)
        arcpy.AddMessage("Network ID {0} processed...".format(id))

    # Union all subnetwork graphs
    arcpy.AddMessage("GNA: Merging all subnetworks...")
    theNetwork.G = nx.union_all(list_subnets)

    arcpy.AddMessage("GNA: Writing to shapefile...")
    theNetwork._nx_to_shp(theNetwork.G, out_shp, bool_node=True)

    return