-
Notifications
You must be signed in to change notification settings - Fork 169
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Save positions for reproducibility? #157
Comments
I have been looking for this information for a year. So far no luck! |
I assume that one could access them with |
My current solution is a bit clumsy, but it works for me to get the positions after I moved the nodes around. function download(content, fileName, contentType) {
var a = document.createElement("a");
var file = new Blob([content], {type: contentType});
a.href = URL.createObjectURL(file);
a.download = fileName;
a.click();
}
network.storePositions();
download(JSON.stringify(data.nodes.get()), 'positions.txt', 'text/plain'); That will download the information for all nodes, including their positions (x,y) which then can be added to the graph in the python code: import json
file = os.path.expanduser('~/Downloads/positions.txt')
d = json.load(open(file))
X = {x['node_name']:x['x'] for x in d}
Y = {x['node_name']:x['y'] for x in d}
nx.set_node_attributes(G, X, 'x')
nx.set_node_attributes(G, Y, 'y') |
Do you have a similar code in Python ? I am not familiar with much Java scripting. Thank you very much. |
This is so helpful. Thanks!! |
Here's a fully non-interactive follow-up of the walkaround that @tsp-kucbd proposed. from selenium import webdriver
from selenium.webdriver.chrome.service import Service as ChromiumService
from webdriver_manager.chrome import ChromeDriverManager
from webdriver_manager.core.os_manager import ChromeType
def get_nx_coordinates(file_path) -> dict[str, tuple[float, float]]:
drivermanager = ChromeDriverManager(chrome_type=ChromeType.CHROMIUM).install()
service = ChromiumService(drivermanager)
driver = webdriver.Chrome(service=service)
driver.get(file_path)
driver.implicitly_wait(0.1)
# Execute a command `data.nodes.get()` in the browser console and return the result
ans = driver.execute_script("network.storePositions();return data.nodes.get();")
driver.quit()
mean_x = sum(e["x"] for e in ans) / len(ans)
mean_y = sum(e["y"] for e in ans) / len(ans)
min_x = min(e["x"] for e in ans)
min_y = min(e["y"] for e in ans)
max_x = max(e["x"] for e in ans)
max_y = max(e["y"] for e in ans)
span_x = max_x - min_x
span_y = max_y - min_y
coords = {e["id"]: ((e["x"] - mean_x) / span_x, (e["y"] - mean_y) / span_y) for e in ans}
return coords It is also inspired by the Please note, that this solution requires For Ubuntu users: Selenium would not work with Chromium, if Chromium was installed via snap (which is the default). You may want to remove it, and then install it again, this time using |
Is it possible to save or dump the node positions once they have been drawn and physics is turned off?
It would be great to be able to store each nodes coordinates and then reuse it for subsequent drawings of the same graph.
The text was updated successfully, but these errors were encountered: