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 <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 *
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](https://icon.colorado.edu) project."""
tags = ['Biological', 'Food web', 'Weighted']
url = 'https://iwdb.nceas.ucsb.edu/html/kato_1990.html'
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)', 'http://www.researchgate.net/publication/236969168_Insect-flower_relationship_in_the_primary_beech_forest_of_Ashu_Kyoto_an_overview_of_the_flowering_phenology_and_the_seasonal_pattern_of_insect_visits')]
icon_hash = '56a85c8e26855e083a2f7900'
ustream_license = None
upstream_prefix = 'https://iwdb.nceas.ucsb.edu/data/plant_pollinator/excel'
files = [('kato_et_al_1990.xls', None, 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, "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)
subprocess.run(["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:
continue
if numpy.isnan(float(m[i,j])):
continue
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:
continue
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