{"id":547,"date":"2014-07-24T11:09:41","date_gmt":"2014-07-24T11:09:41","guid":{"rendered":"https:\/\/block.arch.ethz.ch\/blog\/?p=547"},"modified":"2014-07-24T11:15:50","modified_gmt":"2014-07-24T11:15:50","slug":"reading-network-data-from-a-file","status":"publish","type":"post","link":"https:\/\/block.arch.ethz.ch\/blog\/2014\/07\/reading-network-data-from-a-file\/","title":{"rendered":"Reading network data from a file"},"content":{"rendered":"<p>The data describing large networks can obviously not be written out manually, as in a previous post. Here we will take a look at getting network data from an obj file.<\/p>\n<p><!--more--><\/p>\n<pre class=\"brush: py\">\r\nimport urllib2\r\n\r\nfilepath = 'http:\/\/block.arch.ethz.ch\/labs\/samples\/saddle.obj'\r\nresponse = urllib2.urlopen(filepath)\r\ndata     = iter(response.readlines())\r\n\r\nxyz      = [] # xyz coordinates\r\nvertices = [] # unique xyz coordinates\r\npoints   = [] # references to vertices\r\nlines    = [] # pairs of references to vertices\r\n \r\ndeg = None\r\nfor line in data:\r\n    parts = line.split()\r\n    if not parts: \r\n        continue\r\n    head = parts[0]\r\n    tail = parts[1:]\r\n    if 'deg' == head:\r\n        deg = int(tail[0])\r\n    elif 'end' == head:\r\n        deg = None \r\n    elif 'v' == head: \r\n        xyz.append(map(float, tail))\r\n        deg = None\r\n    elif 'p' == head:\r\n        points.append(int(tail[0]) - 1)\r\n        deg = None\r\n    elif 'curv' == head:\r\n        if deg != 1: \r\n            continue\r\n        if len(tail[2:]) != 2: \r\n            continue\r\n        lines.append((int(tail[2]) - 1, int(tail[3]) - 1))\r\n        deg = None\r\n\r\nx2v = {}\r\ntol = 0.001**2\r\nfor i, x in enumerate(iter(xyz)):\r\n    found = False\r\n    for j, v in enumerate(iter(vertices)):\r\n        if (x[0] - v[0])**2 < tol \\\r\n           and (x[1] - v[1])**2 < tol \\\r\n           and (x[2] - v[2])**2 < tol:\r\n            found = True\r\n            x2v[i] = j\r\n            break\r\n    if not found:\r\n        x2v[i] = len(vertices)\r\n        vertices.append(x)\r\n\r\nedges     = [(x2v[u], x2v[v]) for u, v in lines]\r\npoints[:] = [x2v[index] for index in points]\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>The data describing large networks can obviously not be written out manually, as in a previous post. Here we will take a look at getting network data from an obj file.<\/p>\n","protected":false},"author":11,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-547","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/block.arch.ethz.ch\/blog\/wp-json\/wp\/v2\/posts\/547","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/block.arch.ethz.ch\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/block.arch.ethz.ch\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/block.arch.ethz.ch\/blog\/wp-json\/wp\/v2\/users\/11"}],"replies":[{"embeddable":true,"href":"https:\/\/block.arch.ethz.ch\/blog\/wp-json\/wp\/v2\/comments?post=547"}],"version-history":[{"count":5,"href":"https:\/\/block.arch.ethz.ch\/blog\/wp-json\/wp\/v2\/posts\/547\/revisions"}],"predecessor-version":[{"id":556,"href":"https:\/\/block.arch.ethz.ch\/blog\/wp-json\/wp\/v2\/posts\/547\/revisions\/556"}],"wp:attachment":[{"href":"https:\/\/block.arch.ethz.ch\/blog\/wp-json\/wp\/v2\/media?parent=547"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/block.arch.ethz.ch\/blog\/wp-json\/wp\/v2\/categories?post=547"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/block.arch.ethz.ch\/blog\/wp-json\/wp\/v2\/tags?post=547"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}