__init__.py 4.24 KB
Newer Older
Tiago Peixoto's avatar
Tiago Peixoto committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
#! /usr/bin/env python
# -*- coding: utf-8 -*-
#
# Copyright (C) 2021 Tiago de Paula Peixoto <tiago@skewed.de>
#
# This program is free software: you can redistribute it and/or modify it under
# the terms of the GNU Affero General Public License as published by the Free
# Software Foundation, either version 3 of the License, or (at your option) any
# later version.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE.  See the GNU Affero General Public License for more
# details.
#
# You should have received a copy of the GNU Affero General Public License along
# with this program.  If not, see <http://www.gnu.org/licenses/>.

from .. import *

title = "Atlas of Economic Complexity export network"
description = """Two networks of economic products, where a pair of products are connected if they are exported at similar rates by the same countries. The data are a projection from a bipartite network of nations and the products they export. Edges weights represent a similarity score (called \"proximity\"). Data based on UN Comtrade worldwide trade patterns. SITC network based on the Standard International Trade Classification and HS network based on the Harmonized Commodity Description and Coding System.[^icon]
[^icon]: Description obtained from the [ICON](https://icon.colorado.edu) project."""
tags = ['Economic', 'Trade', 'Weighted', 'Projection', 'Metadata']
url = 'http://www.michelecoscia.com/?page_id=223'
citation = [('R. Hausmann et al., "The Atlas of Economic Complexity: Mapping paths to prosperity." MIT Press (2014)', 'https://doi.org/10.1126/science.1144581'),
            ('CA Hidalgo, R Hausmann, "The Building Blocks of Economic Complexity", Proc. Natl. Acad. Sci. 106(26):10570-10575 (2009)', "https://doi.org/10.1073/pnas.0900943106"),
            ('R Hausmann, CA Hidalgo, S Bustos, M Coscia, S Chung, J Jimenez, A Simoes, MA Yildirim, "The Atlas of Economic Complexity", Puritan Press (2011). ISBN-10: 0615546625', 'http://atlas.media.mit.edu/book/')]
icon_hash = '577e7bf458d31bd664bac0d9'
ustream_license = None
upstream_prefix = 'http://www.michelecoscia.com/wp-content/uploads/2012/09/'
files = [(('PS_HS.zip:PS_HS_nodes', 'PS_HS.zip:PS_HS_edges'), 'HS', None),
         (('PS_SITC.zip:PS_SITC_nodes', 'PS_SITC.zip:PS_SITC_edges'), 'SITC', None)]

def fetch_upstream(force=False):
    return fetch_upstream_files(__name__.split(".")[-1], upstream_prefix, files,
                                force)

@cache_network()
@coerce_props()
@annotate()
def parse(alts=None):
    global files
    name = __name__.split(".")[-1]
    for fnames, alt, fmt in files:
        if alts is not None and alt not in alts:
            continue
        if isinstance(fnames, str):
            fnames = [fnames]
        with ExitStack() as stack:
            fs = [stack.enter_context(open_upstream_file(name, fn, "r")) for fn in fnames]

            g = Graph(directed=False)

            g.vp.pid = g.new_vp("int")
            g.vp.community = g.new_vp("int")
            g.vp.size = g.new_vp("double")
            g.vp.pos = g.new_vp("vector<double>")
            g.vp.leamer = g.new_vp("string")
            g.vp.name = g.new_vp("string")
            if alt == "SITC":
                g.vp.color = g.new_vp("string")

            vertices = {}
            for line in fs[0]:
                v = g.add_vertex()
                if alt == "SITC":
                    pid, c, x, y, s, leamer, name, color = line.split("\t")
                    g.vp.color[v] = color
                else:
                    pid, c, x, y, s, leamer, name = line.split("\t")
                g.vp.pid[v] = int(pid)
                vertices[int(pid)] = v
                g.vp.pos[v] = [float(x), float(y)]
                g.vp.size[v] = s
                g.vp.name[v] = name
                g.vp.leamer[v] = leamer

            g.ep.width = g.new_ep("double")
            g.ep.color = g.new_ep("string")

            for line in fs[1]:
                s, x_s, y_s, t, x_t, y_t, w, c = line.split("\t")

                s = vertices[int(s)]
                t = vertices[int(t)]
                e = g.add_edge(s, t)
                g.ep.width[e] = w
                g.ep.color[e] = c

        yield alt, g