Species.py 3.6 KB
Newer Older
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
import pandas as pd
import os
import pathlib

import sys
sys.path.append('..')

class TabularSpecies:
    """
    Class holding pandas data frame and helper dicts on all MNP species
    """

    def __init__(self):
        filepath = pathlib.Path(__file__)

        # De volledige Species tabel als Pandas dataframe
        benb_dir = os.path.dirname(os.path.dirname(filepath))
        self.df = pd.read_csv(os.path.join(benb_dir, r'resources\mnp_species.csv'), sep=',', comment='#')

        # Dictionaries tussen code, lokale naam, wetenschappelijke naam
        self.code2local = dict(zip(self.df.Species_code, self.df.Local_name))
        self.local2code = dict(zip(self.df.Local_name, self.df.Species_code))

        self.code2scientific = dict(zip(self.df.Species_code, self.df.Scientific_name))
        self.scientific2code = dict(zip(self.df.Scientific_name, self.df.Species_code))

        self.local2scientific = dict(zip(self.df.Local_name, self.df.Scientific_name))
        self.scientific2local = dict(zip(self.df.Scientific_name, self.df.Local_name))

        self.code2taxon = dict(zip(self.df.Species_code, self.df.taxon))

        # Dictionaries to self, is usefull sometimes. Trust me...
        self.local2local = dict(zip(self.df.Local_name, self.df.Local_name))
        self.code2code = dict(zip(self.df.Species_code, self.df.Species_code))
        self.scientific2scientific = dict(zip(self.df.Scientific_name, self.df.Scientific_name))

        # Dict between species code and boolean species list
        self.code2sel468 = dict(zip(self.df.Species_code, self.df.sel468.map({1: True,  0: False})))
        self.code2selall = dict(zip(self.df.Species_code, self.df.selall.map({1: True,  0: False})))
        self.code2sel281 = dict(zip(self.df.Species_code, self.df.sel281.map({1: True,  0: False})))
        self.code2sel146 = dict(zip(self.df.Species_code, self.df.sel146.map({1: True,  0: False})))


class IndividualSpecies:
    """
    Class for holding properties of a single species instance.
    Hans Roelofsen, WEnR, 12 jan 2022
    """

    def __init__(self, x):
        """
        Initiate for a species.
        :param x: species code, local name or scientific name
        """

        species_tab = TabularSpecies()

        species_identifiers_full = ['Species_code', 'Local_name', 'Scientific_name']
        species_identifiers_brief = ['code', 'local', 'scientific']

        try:
            # Determine if X is a species code, local name, scientific name or not-existing
            isin = [x in getattr(species_tab.df, col).values for col
                    in species_identifiers_full].index(True)
            typer = species_identifiers_brief[isin]
        except ValueError:
            print('{} is not a recognized species'.format(x))
            raise

        self.taxondict = {'S02': 'V', 'S06': 'E', 'S09': 'P'}

        # Set all relevant attributes
        self.code = getattr(species_tab, '{}2code'.format(typer))[x]  # Species code
        self.local = getattr(species_tab, '{}2local'.format(typer))[x]  # Species local name
        self.scientific = getattr(species_tab, '{}2scientific'.format(typer))[x]  # Species Scientific name
        self.taxon = self.taxondict[self.code[:3]]
        self.sel281 = species_tab.code2sel281[self.code]  # boolean, species is part of 281 selection?
        self.sel468 = species_tab.code2sel468[self.code]  # idem for 468 selection
        self.sel146 = species_tab.code2sel146[self.code]  # idem foo 146 selection
        self.selall = species_tab.code2selall[self.code]  # catch all

        self.groupinfo = '146:{0} - 281:{1} - 468:{2}'.format(self.sel146, self.sel281, self.sel468)