Esempio n. 1
0
def index(request):

    timezone.activate(get_tzone(request.user))

    ipaddr = get_ip(request)
    if ipaddr:
        mkdir_on_absent(rs_filepath_info(client_ip_log_file)["foldername"])
        ip_log = TimeStamp_HashFile(client_ip_log_file)
        ip_log.stamp(ipaddr)

    contxt = {
        "NichoNode":
        NichoNode,
        "NichoEdge":
        NichoEdge,
        "settings":
        settings,
        "apache_user":
        getpass.getuser(),
        "acount_minus_wk3":
        len(ip_log.get_keys_timerange(timedelta(days=21), timedelta(days=14))),
        "acount_minus_wk2":
        len(ip_log.get_keys_timerange(timedelta(days=14), timedelta(days=7))),
        "acount_minus_wk1":
        len(ip_log.get_keys_timerange(timedelta(days=7), timedelta(days=0))),
    }  # {{ message|safe }}

    return render(request, "appNichoAnu/map_Anurag1_3.html", contxt)
# import networkx as nx
from rs_Python_Pack3_copied.Graph_Packages.NetworkX.rsNetworkX_DiGraph1 import rsNetworkX_DiGraph
from rs_Python_Pack3_copied.Graph_Packages.Graph_Draw.DrawNetworkX_simple1_9 \
    import KEY_NODE_COORD_XY, \
        KEY_DISP_NODE_LABEL, KEY_NODE_LABEL_OFFSET_XY, \
        KEY_EDGE_LABEL_LIST, KEY_EDGE_DIRECTION, KEY_EDGE_RELAY_POSS
from appNichoAnu.models import *

from rs_Python_Pack3_copied.General_Packages.FileDirPath.mkdir_on_absent import mkdir_on_absent
from rs_Python_Pack3_copied.General_Packages.FileDirPath.File_Path1 import rs_filepath_info

networkx_pkl_file = os.path.join(settings.UCSD_IAB_WORKDIR, "Media",
                                 "appNichoAnu", "Network",
                                 "NichoAnu_networkx_intermed1_2.pkl")
mkdir_on_absent(rs_filepath_info(networkx_pkl_file)["foldername"])


def del_networkx_rec():

    if os.path.isfile(networkx_pkl_file):
        os.remove(networkx_pkl_file)


def to_networkx_from_rec(update=False, focus_edge=None):

    if not os.path.isfile(networkx_pkl_file) or update:
        ogrf = to_networkx()
        with open(networkx_pkl_file, "wb") as fw:
            pickle.dump(ogrf, fw)
    else:
def del_edge(request, edge_id, outer_percent=300):

    timezone.activate(get_tzone(request.user))

    # edges_objs = Nichoedge.objects.filter(edge_id = edge_id,
    #                                       obsolete = False)

    edge = NichoEdge.objects.get(id=edge_id)
    edge_oldinfo = edge.generate_obsolete()
    edge_oldinfo.save()

    node_src_x = edge.node_src.pos_x_on_map
    node_src_y = edge.node_src.pos_y_on_map
    node_tgt_x = edge.node_tgt.pos_x_on_map
    node_tgt_y = edge.node_tgt.pos_y_on_map
    center_x = (node_src_x + node_tgt_x) / 2
    center_y = (node_src_y + node_tgt_y) / 2
    x_diff_abs = abs(node_tgt_x - node_src_x)
    y_diff_abs = abs(node_tgt_y - node_src_y)
    if y_diff_abs >= x_diff_abs * HHEIGHT_DEFAULT / HWIDTH_DEFAULT:
        y_width = y_diff_abs
        x_width = y_diff_abs * HWIDTH_DEFAULT / HHEIGHT_DEFAULT
    else:
        x_width = x_diff_abs
        y_width = x_diff_abs * HHEIGHT_DEFAULT / HWIDTH_DEFAULT

    range_x = [
        center_x - x_width / 2 * outer_percent / 100,
        center_x + x_width / 2 * outer_percent / 100
    ]
    range_y = [
        center_y - y_width / 2 * outer_percent / 100,
        center_y + y_width / 2 * outer_percent / 100
    ]

    if (range_x[1] - range_x[0] == HWIDTH_DEFAULT
            and range_y[1] - range_y[0] == HHEIGHT_DEFAULT):
        fig_face_color = (0.8, 0.8, 0.7)
    else:
        fig_face_color = (0.8, 0.8, 0.8)

    node_vis_id_src = edge.node_src.node_vis_id
    node_vis_id_tgt = edge.node_tgt.node_vis_id

    edge.delete()

    grf = to_networkx_from_rec(update=True)
    image_file = os.path.join(settings.UCSD_IMM_WORKDIR, "Media",
                              "appNichoAnu", "Images", "edges",
                              "%s.png" % edge_id)
    mkdir_on_absent(rs_filepath_info(image_file)["foldername"])
    # http://127.0.0.1:8000/UCSD_IMM_media/appNichoAnu/Images/tmp1.png
    image_url = '/'.join([
        settings.MEDIA_URL.rstrip('/'), "appNichoAnu", "Images", "edges",
        "%s.png" % edge_id
    ])

    drawgrf = DrawNetworkX_simple(grf)
    drawgrf.output_fig_around_region_simple(
        range_x,
        range_y,
        nodes_central=[node_vis_id_src, node_vis_id_tgt],
        node_label_off_num_nodes=1000,
        figsize=(6, 6),
        fig_face_color=fig_face_color,
        title="Edge %s - %s deleted" % (node_vis_id_src, node_vis_id_tgt),
        outfile=image_file)

    contxt = RequestContext(request, {"edge": edge, "image_url": image_url})
    templt = loader.get_template("appNichoAnu/map_Anurag_del_edge1.html")

    return HttpResponse(templt.render(contxt))
Esempio n. 4
0
def edge_info(request, edge_id, outer_percent=300):

    timezone.activate(get_tzone(request.user))

    # edges_objs = Nichoedge.objects.filter(edge_id = edge_id,
    #                                       obsolete = False)

    edge = NichoEdge.objects.get(id=edge_id)
    edge_oldinfo = edge.generate_obsolete()

    if request.method == "POST":
        # iform_h = dict([(ikey, request.POST[ikey]) for ikey in request.POST])
        # from pprint import pprint
        # pprint(iform_h)

        if request.POST.get("del_edge", "") == "on":
            return HttpResponseRedirect(
                reverse("appNichoAnu:del_edge", kwargs={"edge_id": edge_id}))

        prev_edge_reliab = edge.reliability
        edge_form = NichoEdge_Form(request.POST, instance=edge)
        # edge object is already affected at above line.
        # The object is returned with edgeform.save(commit = False)

        if edge_form.is_valid() and len(edge_form.changed_data):
            edge_newinfo = edge_form.save(commit=False)
            edge_newinfo.user = request.user
            edge_newinfo.timestamp = timezone.now()

            if not edge_newinfo.reliability:
                if prev_edge_reliab:
                    edge_newinfo.reliability = prev_edge_reliab
                else:
                    edge_newinfo.reliability = 1

            edge_newinfo.save()
            edge_oldinfo.save()
            to_networkx_from_rec(update=True)
            return HttpResponse("Modified!")
        else:
            from pprint import pprint
            pprint(edge_form.errors)
    else:
        edge_form = NichoEdge_Form(instance=edge)

    grf = to_networkx_from_rec(focus_edge=edge)
    image_file = os.path.join(settings.UCSD_IAB_WORKDIR, "Media",
                              "appNichoAnu", "Images", "edges",
                              "%s.png" % edge_id)
    mkdir_on_absent(rs_filepath_info(image_file)["foldername"])
    # http://127.0.0.1:8000/UCSD_IMM_media/appNichoAnu/Images/tmp1.png
    image_url = '/'.join([
        settings.MEDIA_URL.rstrip('/'), "appNichoAnu", "Images", "edges",
        "%s.png" % edge_id
    ])

    if True:  # not os.path.isfile(image_file):
        node_src_x = edge.node_src.pos_x_on_map
        node_src_y = edge.node_src.pos_y_on_map
        node_tgt_x = edge.node_tgt.pos_x_on_map
        node_tgt_y = edge.node_tgt.pos_y_on_map
        center_x = (node_src_x + node_tgt_x) / 2
        center_y = (node_src_y + node_tgt_y) / 2
        x_diff_abs = abs(node_tgt_x - node_src_x)
        y_diff_abs = abs(node_tgt_y - node_src_y)
        if y_diff_abs >= x_diff_abs * HHEIGHT_DEFAULT / HWIDTH_DEFAULT:
            y_width = y_diff_abs
            x_width = y_diff_abs * HWIDTH_DEFAULT / HHEIGHT_DEFAULT
        else:
            x_width = x_diff_abs
            y_width = x_diff_abs * HHEIGHT_DEFAULT / HWIDTH_DEFAULT

        range_x = [
            center_x - x_width / 2 * outer_percent / 100,
            center_x + x_width / 2 * outer_percent / 100
        ]
        range_y = [
            center_y - y_width / 2 * outer_percent / 100,
            center_y + y_width / 2 * outer_percent / 100
        ]

        if (range_x[1] - range_x[0] == HWIDTH_DEFAULT
                and range_y[1] - range_y[0] == HHEIGHT_DEFAULT):
            fig_face_color = (0.8, 0.8, 0.7)
        else:
            fig_face_color = (0.8, 0.8, 0.8)

        drawgrf = DrawNetworkX_simple(grf)
        drawgrf.output_fig_around_region_simple(
            range_x,
            range_y,
            nodes_central=[
                edge.node_src.node_vis_id, edge.node_tgt.node_vis_id
            ],
            edges_central=[
                (edge.node_src.node_vis_id, edge.node_tgt.node_vis_id),
                (edge.node_tgt.node_vis_id, edge.node_src.node_vis_id)
            ],
            node_label_off_num_nodes=1000,
            figsize=(6, 6),
            fig_face_color=fig_face_color,
            title="Edge %s - %s" %
            (edge.node_src.node_vis_id, edge.node_tgt.node_vis_id),
            outfile=image_file)

    contxt = {"edge": edge, "edge_form": edge_form, "image_url": image_url}

    return render(request, "appNichoAnu/map_Anurag_edge_info2.html", contxt)
Esempio n. 5
0
    imed_hmdb_to_partialinfo_dfrm = \
        pd.io.parsers.read_table(ihmdb_tsv_file,
                                 sep = '\t', header = 0, index_col = 0,
                                 encoding = "latin1")

    imed_dfrm = \
        imed_hmdb_to_partialinfo_dfrm.loc[:, ("KEGG ID",
                                              "CAS number",
                                              "Wikipidia",
                                              "Name")]
    odfrm = imed_dfrm.applymap(lambda ielem: str(ielem).strip())

    return odfrm


pkl_file = os.path.join(settings.UCSD_IAB_WORKDIR, "InterMed", "PubData",
                        "hmdb3_3_partial_dfrm.pkl")
mkdir_on_absent(rs_filepath_info(pkl_file)["foldername"])

pmI2 = Pickle_Manager(source_files=[hmdb_tsv_file],
                      version_info_h={
                          "Ver major": "0.54",
                      },
                      pkl_file=pkl_file,
                      verbose=True,
                      ifunc=generate_hmdb_partialinfo_dfrm,
                      args=[hmdb_tsv_file],
                      kwargs={})

hmdb_partial_info_dfrm = pmI2.get_obj()
Esempio n. 6
0
def node_vis_id_info(request,
                     node_vis_id,
                     hwidth=HWIDTH_DEFAULT,
                     hheight=HHEIGHT_DEFAULT,
                     offx=0,
                     offy=0):
    hwidth = int(hwidth)
    hheight = int(hheight)
    offx = int(offx)
    offy = int(offy)

    timezone.activate(get_tzone(request.user))
    timezone_now = timezone.now()
    node = NichoNode.objects.get(node_vis_id=node_vis_id)
    node_oldinfo = node.generate_obsolete(timezone_now)

    node_pos_x = node.pos_x_on_map
    node_pos_y = node.pos_y_on_map

    if request.method == "POST":

        if request.POST.get("del_node", "") == "on":
            return HttpResponseRedirect(
                reverse("appNichoAnu:del_node",
                        kwargs={"node_vis_id": node_vis_id}))

        iform_h = dict([(ikey, request.POST[ikey]) for ikey in request.POST])
        iform_h["node_vis_id"] = node.node_vis_id
        iform_h["reliability"] = node.reliability
        node_form = NichoNode_Form(iform_h, instance=node)
        # node object is already affected at above line.
        # The object is returned with nodeform.save(commit = False)

        from pprint import pprint
        pprint(iform_h)
        print(".....")

        if node_form.is_valid() and len(node_form.changed_data):
            node_newinfo = node_form.save(commit=False)
            node_newinfo.user = request.user
            node_newinfo.timestamp = timezone.now()
            # New time stamp!
            node_newinfo.save()
            node_oldinfo.save()
            to_networkx_from_rec(update=True)
            return HttpResponse("Modified!")
        else:
            from pprint import pprint
            pprint(node_form.errors)
    else:
        node_form = NichoNode_Form(instance=node)

    grf = to_networkx_from_rec()
    image_file = os.path.join(settings.UCSD_IAB_WORKDIR, "Media",
                              "appNichoAnu", "Images", "nodes",
                              "%s.png" % node_vis_id)
    mkdir_on_absent(rs_filepath_info(image_file)["foldername"])
    # http://127.0.0.1:8000/UCSD_IMM_media/appNichoAnu/Images/tmp1.png
    image_url = '/'.join([
        settings.MEDIA_URL.rstrip('/'), "appNichoAnu", "Images", "nodes",
        "%s.png" % node_vis_id
    ])

    if True:  # not os.path.isfile(image_file):
        drawgrf = DrawNetworkX_simple(grf)
        if hwidth == HWIDTH_DEFAULT and hheight == HHEIGHT_DEFAULT:
            fig_face_color = (0.8, 0.8, 0.7)
        else:
            fig_face_color = (0.8, 0.8, 0.8)
        nodes_drawn, edges_drawn = \
            drawgrf.output_fig_around_region_simple(
                [node_pos_x - hwidth  + offx, node_pos_x + hwidth  + offx],
                [node_pos_y - hheight + offy, node_pos_y + hheight + offy],
                nodes_central = [ node_vis_id ],
                node_label_off_num_nodes = 1000,
                figsize = (6, 6),
                fig_face_color = fig_face_color,
                title = "Network around %s" % node_vis_id,
                outfile = image_file)

        # for nodenam in nodes_drawn:
        #     print("Getting", nodenam)
        #     print(NichoNode.objects.get(node_vis_id = nodenam))

        nodes_drawn = [
            NichoNode.objects.get(node_vis_id=nodenam)
            for nodenam in nodes_drawn
        ]
        print("Nodes drawn:", nodes_drawn)
    else:
        nodes_drawn = []

    hwidth_zi = int(hwidth / 1.5)
    if hwidth_zi < 2:
        hwidth_zi = 2
    hheight_zi = int(hheight / 1.5)
    if hheight_zi < 2:
        hheight_zi = 2

    zoom_in_param = {
        "hwidth": hwidth_zi,
        "hheight": hheight_zi,
        "offx": offx,
        "offy": offy
    }

    zoom_out_param = {
        "hwidth": int(hwidth * 1.5),
        "hheight": int(hheight * 1.5),
        "offx": offx,
        "offy": offy
    }

    shift_left_param = {
        "hwidth": hwidth,
        "hheight": hheight,
        "offx": int(offx - hwidth * 0.5),
        "offy": offy
    }

    shift_right_param = {
        "hwidth": hwidth,
        "hheight": hheight,
        "offx": int(offx + hwidth * 0.5),
        "offy": offy
    }

    shift_down_param = {
        "hwidth": hwidth,
        "hheight": hheight,
        "offx": offx,
        "offy": int(offy - hheight * 0.5)
    }

    shift_up_param = {
        "hwidth": hwidth,
        "hheight": hheight,
        "offx": offx,
        "offy": int(offy + hheight * 0.5)
    }

    contxt = {
        "node": node,
        "node_form": node_form,
        "image_url": image_url,
        "nodes_drawn": nodes_drawn,
        "zoom_in_param": zoom_in_param,
        "zoom_out_param": zoom_out_param,
        "shift_left_param": shift_left_param,
        "shift_right_param": shift_right_param,
        "shift_up_param": shift_up_param,
        "shift_down_param": shift_down_param,
    }

    return render(request, "appNichoAnu/map_Anurag_node_info4.html", contxt)
def del_node(request,
             node_vis_id,
             hwidth=HWIDTH_DEFAULT,
             hheight=HHEIGHT_DEFAULT,
             offx=0,
             offy=0):

    hwidth = int(hwidth)
    hheight = int(hheight)
    offx = int(offx)
    offy = int(offy)

    timezone.activate(get_tzone(request.user))
    timezone_now = timezone.now()
    node = NichoNode.objects.get(node_vis_id=node_vis_id)
    node_pos_x = node.pos_x_on_map
    node_pos_y = node.pos_y_on_map

    node_oldinfo = node.generate_obsolete(timezone_now)
    node_oldinfo.save()
    edges_deleted_str = []
    for edge in node.assoc_edges():
        edge_oldinfo = edge.generate_obsolete(timezone_now)
        edge_oldinfo.save()
        edges_deleted_str = str(edge)
        edge.delete()

    node.delete()

    grf = to_networkx_from_rec(update=True)
    image_file = os.path.join(settings.UCSD_IMM_WORKDIR, "Media",
                              "appNichoAnu", "Images", "nodes",
                              "%s.png" % node_vis_id)
    mkdir_on_absent(rs_filepath_info(image_file)["foldername"])
    # http://127.0.0.1:8000/UCSD_IMM_media/appNichoAnu/Images/tmp1.png
    image_url = '/'.join([
        settings.MEDIA_URL.rstrip('/'), "appNichoAnu", "Images", "nodes",
        "%s.png" % node_vis_id
    ])

    drawgrf = DrawNetworkX_simple(grf)
    if hwidth == HWIDTH_DEFAULT and hheight == HHEIGHT_DEFAULT:
        fig_face_color = (0.8, 0.8, 0.7)
    else:
        fig_face_color = (0.8, 0.8, 0.8)
    nodes_drawn, edges_drawn = \
        drawgrf.output_fig_around_region_simple(
            [node_pos_x - hwidth  + offx, node_pos_x + hwidth  + offx],
            [node_pos_y - hheight + offy, node_pos_y + hheight + offy],
            nodes_central = [ node_vis_id ],
            node_label_off_num_nodes = 1000,
            figsize = (6, 6),
            fig_face_color = fig_face_color,
            title = "Network around %s" % node_vis_id,
            outfile = image_file)

    nodes_drawn = [
        NichoNode.objects.get(node_vis_id=nodenam) for nodenam in nodes_drawn
    ]
    print("Nodes drawn:", nodes_drawn)

    hwidth_zi = int(hwidth / 1.5)
    if hwidth_zi < 2:
        hwidth_zi = 2
    hheight_zi = int(hheight / 1.5)
    if hheight_zi < 2:
        hheight_zi = 2

    zoom_in_param = {
        "hwidth": hwidth_zi,
        "hheight": hheight_zi,
        "offx": offx,
        "offy": offy
    }

    zoom_out_param = {
        "hwidth": int(hwidth * 1.5),
        "hheight": int(hheight * 1.5),
        "offx": offx,
        "offy": offy
    }

    shift_left_param = {
        "hwidth": hwidth,
        "hheight": hheight,
        "offx": int(offx - hwidth * 0.5),
        "offy": offy
    }

    shift_right_param = {
        "hwidth": hwidth,
        "hheight": hheight,
        "offx": int(offx + hwidth * 0.5),
        "offy": offy
    }

    shift_down_param = {
        "hwidth": hwidth,
        "hheight": hheight,
        "offx": offx,
        "offy": int(offy - hheight * 0.5)
    }

    shift_up_param = {
        "hwidth": hwidth,
        "hheight": hheight,
        "offx": offx,
        "offy": int(offy + hheight * 0.5)
    }

    contxt = RequestContext(
        request, {
            "image_url": image_url,
            "nodes_drawn": nodes_drawn,
            "zoom_in_param": zoom_in_param,
            "zoom_out_param": zoom_out_param,
            "shift_left_param": shift_left_param,
            "shift_right_param": shift_right_param,
            "shift_up_param": shift_up_param,
            "shift_down_param": shift_down_param,
        })
    templt = loader.get_template("appNichoAnu/map_Anurag_del_node1.html")

    return HttpResponse(templt.render(contxt))