Giter Site home page Giter Site logo

Comments (8)

benmaier avatar benmaier commented on June 12, 2024 1

Ah, I see! Sounds reasonable. Also, I suggest once this is implemented and tests run smoothly we bump the whole thing up to v0.1

from netwulf.

ulfaslak avatar ulfaslak commented on June 12, 2024

Actually, the problem is in the else, where, if nodes have x and y properties but the graph has no xlim and ylim properties, node-positions are recomputed such as to use the entire canvas space available. It's nice if someone gives their node position in obscure coordinate ranges (like GPS coordinates, grid positions in nanometers, etc). I think the proper way to fix this is:

  1. In bind_positions_to_network, add xlim and ylim attributes to the G.graph object (graph is a dictionary attribute that an nx.Graph has dedicated for storing graph attributes) such that, in netwulf.visualize, nx.node_link_data(G) produces a dictionary that has an entry 'graph': {'xlim': [lowerx, upperx], 'ymin': [lowery, uppery]}, then
  2. In the vis.js every lookup in graph or masterGraph for xlim and ylim get changed from graph['xlim'] to `graph['graph']['xlim'].

I'll try this and report back.

from netwulf.

ulfaslak avatar ulfaslak commented on June 12, 2024

Changes to netwulf (a305733):

  • In tools.py: bind_positions_to_network -> bind_properties_to_network. The updated function defaults to binding all properties to the nx.Graph object, but has an argument for untoggling binding for each property. xlim and ylim are now stored in the G.graph dict.
  • In interactive.py: I made a small update so the dictionary resulting from nx.node_link_data(G) is flattened, before it is saved. Thus, it ends up almost identical to the posted_network_properties that visualize returns.

There's still a problem then config['zoom'] is changed. This needs to be fixed in https://github.com/ulfaslak/network_styling_with_d3.

from netwulf.

ulfaslak avatar ulfaslak commented on June 12, 2024

Alright, I thought about this a bit now. From a user's perspective it makes sense that setting config['zoom'] should be equivalent to pulling the slider... But it gets a little complicated due to the way zoom is implemented in the visualization. Nodes have d3 simulation coordinates, but using the zoomScaler nodes are rendered elsewhere. For example, a node with coordinates x, y is rendered is in zoomScaler(x), zoomScaler(y). So the zoomScaler is basically a (two-way) may between simulation coordinates and canvas coordinates (and vice versa). Now, when we return the node coordinates to Python, it is the canvas coordinates we return, simply because it simplifies re-rendering the graph in Matplotlib. This is all fine. The problem, however, is when we overwrite config['zoom'] the program suddenly no longer knows what the simulation coordinates were. Even worse, as-is it will assume that the simulation coordinates are those obtained with a zoomScaler initiated with a different config['zoom'] value.

So if we e.g. lower config['zoom'] from 1.5 to 0.66, it's like saying "actually these canvas coordinates are contracted compared to the simulation coordinates". So the simulation coordinates are now different (more spread out), and the rendered network is contracted 0.66 in relation to that. Effectively, the nodes are going to appear to be in the same position on the canvas, only with smaller node sizes.

A way to enable the "user-intuitive" behavior would be to:

  1. Return to Python, not the canvas coordinates, but the simulation coordinates
  2. Change the draw_netwulf function so it also takes the config as input and then uses config['zoom'] to put the nodes in the "canvas positions".

Alternatively, we return to Python the simulation node-positions in the original 'x' and 'y' keys and then add extra keys called 'x_canvas', and 'y_canvas', that only matplotlib uses.

from netwulf.

ulfaslak avatar ulfaslak commented on June 12, 2024

Btw, no need to wrap your head around all of that, I mostly wrote it out to understand it myself. I'll go for the alternative solution. For reproduction, it's much more elegant that we return the simulation coordinates.

b4bef4a

from netwulf.

ulfaslak avatar ulfaslak commented on June 12, 2024

Now you can try this:

from netwulf.tools import bind_properties_to_network
from netwulf import visualize
import networkx as nx

G = nx.Graph()
G.add_nodes_from(range(10))
G.add_nodes_from("abcde")
G.add_edges_from([("a","b")])

props, config = visualize(G)
config['zoom'] = 0.666

bind_properties_to_network(G, props)
visualize(G, config=config)

from netwulf.

benmaier avatar benmaier commented on June 12, 2024

works flawlessly now, thanks!

from netwulf.

benmaier avatar benmaier commented on June 12, 2024

even though config['zoom'] = 0.666 is not necessary anymore now

from netwulf.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.