Commit 5dec248c authored by Tiago Peixoto's avatar Tiago Peixoto
Browse files

Add plant_pol_kato

parent 6b5087e0
#! /usr/bin/env python
# -*- coding: utf-8 -*-
# Copyright (C) 2020 Tiago de Paula Peixoto <>
# 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 <>.
from .. import *
from openpyxl import load_workbook
import pandas
import shutil
import subprocess
import tempfile
title = "Kato plant-pollinator web"
description = """A bipartite network of plants and pollinators from Kyoto University Forest of Ashu, Japan, from 1984 to 1987. Edge weights represent frequency of interactions[^icon]
[^icon]: Description obtained from the [ICON]( project."""
tags = ['Biological', 'Food web', 'Weighted']
url = ''
citation = [('M. Kato et al., "Insect-flower relationship in the primary beech forest of Ashu, Kyoto: an overview of the flowering phenology and seasonal pattern of insect visits." Contr. Biol. Lab. Kyoto Univ. 27, 309-375 (1990)', '')]
icon_hash = '56a85c8e26855e083a2f7900'
ustream_license = None
upstream_prefix = ''
files = [('kato_et_al_1990.xls', None, None)]
def fetch_upstream(force=False):
return fetch_upstream_files(__name__.split(".")[-1], upstream_prefix, files,
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:
if isinstance(fnames, str):
fnames = [fnames]
with ExitStack() as stack:
fs = [stack.enter_context(open_upstream_file(name, fn, "rb")) for fn in fnames]
with tempfile.TemporaryDirectory(dir="./cache/temp") as tdir:
with open(f"{tdir}/dump.xls", "wb") as fw:
shutil.copyfileobj(fs[0], fw)["localc", "--convert-to", "xlsx",
f"dump.xls"], cwd=tdir, check=True)
wb = load_workbook(f"{tdir}/dump.xlsx", read_only=True)
g = Graph(directed=False)
g.vp.idx = g.new_vp("int")
g.vp.pollinator = g.new_vp("bool")
g.ep.count = g.new_ep("int")
def add_bip_edges(m):
plts = {}
pols = {}
for i in range(1, m.shape[0]):
v = g.add_vertex()
g.vp.pollinator[v] = True
pols[m[i,0]] = v
g.vp.idx[v] = m[i,0]
for j in range(1, m.shape[1]):
u = plts.get(m[0, j], None)
if u is None:
u = g.add_vertex()
g.vp.pollinator[u] = False
plts[m[0, j]] = u
g.vp.idx[u] = m[0, j]
if m[i,j] is None:
if numpy.isnan(float(m[i,j])):
x = int(m[i, j])
if x > 0:
e = g.add_edge(v, u)
g.ep.count[e] = x
return plts, pols
m = pandas.DataFrame(wb["matr"].values).values
plts, pols = add_bip_edges(m[2:,1:])
for ti, (idxs, vals) in enumerate([(m[2:,1], m[2:,0]),
(m[2,1:].T, m[0,1:].T),
(m[2,1:].T, m[1,1:].T)]):
vs = pols if ti == 0 else plts
p = vals[0]
if p not in g.vp:
g.vp[p] = g.new_vp("string")
for i in range(1, len(vals)):
idx = idxs[i]
if idx is None:
g.vp[p][vs[idx]] = vals[i]
yield alt, g
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment