-
Notifications
You must be signed in to change notification settings - Fork 0
/
pair.py
146 lines (132 loc) · 4.21 KB
/
pair.py
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
'''
Pairs one or more gorillas with suitable mate(s)
'''
# Third party imports
import sys
import traceback
# Local imports
from db import (
get_gorilla,
get_relations,
get_non_relations
)
def get_relation_percetages(relation_type):
relation_percentages = {
'siblings': 50,
'children': 50,
'parents': 50,
'grand_children': 25,
'grand_parents': 25,
'aunts_uncles': 25,
'nephews': 25,
'half_siblings': 25,
'cousins': [12.5, 3.13, 0.78, 0.20, 0.05, 0.01],
'cousins_once_removed': [6.25, 1.5]
}
if relation_type in relation_percentages:
return relation_percentages[relation_type]
else:
return 0
def assign_percentages_to_mates(mates):
return_obj = {}
for key, value in mates.items():
if key != "cousins" and key != "cousins_once_removed":
for relation in value:
return_obj[relation] = get_relation_percetages(key)
else:
for idx, cousins in enumerate(value):
for cousin in cousins:
return_obj[cousin] = get_relation_percetages(key)[idx]
return return_obj
def find_best_mates(gorilla):
'''
1. Check if gorilla is alive, if no quit.
2. Check sex, and for every of the sex:
a. identify the:
a. Twin
b. Sibling
c. Grandparent / Grandchild Aunt / Uncle Niece / Nephew / Half Sibling
d. 1st Cousin
e. 1st Cousin once removed
f. 2nd Cousin
g. 2nd Cousin once removed
h. 3rd Cousin
i. 4th Cousin
j. 5th Cousin
k. 6th Cousin
b. for all the identified relatives, assign the percentages to them.
c. for all the non relatives, add them to a list.
'''
mate_sex = 'F' if gorilla.sex == 'M' else 'M'
relations = {
'siblings': gorilla.siblings,
'children': gorilla.offsprings,
'parents': [gorilla.sire, gorilla.dam],
'grand_children': [],
'grand_parents': [],
'aunts_uncles': [],
'nephews': [],
'half_siblings': [],
'cousins': [],
'cousins_once_removed': []
}
for key, value in relations.items():
if value == []:
relations[key] = get_relations(
mate_sex, key, gorilla.identifier
)
print('''
Relations: {}
'''.format(relations))
relations = assign_percentages_to_mates(relations)
print('Number of relations: {}'.format(len(relations)))
non_relations = get_non_relations(
mate_sex,
gorilla.identifier,
list(relations.keys())
)
print('Number of non-relations: {}'.format(len(non_relations)))
for non_relation in non_relations:
relations[non_relation] = 0
relations.pop(gorilla.identifier, None)
print('''
Selected mates with percentages: {}
'''.format(relations))
return relations
GENERAL_ERROR = '''
Error:
Unrecognized argument supplied, please supply
gorilla identifier or link or "all" to run the pairing
for all gorillas in the database.
'''
if __name__ == '__main__':
try:
args = sys.argv
identifier_or_key = args[1]
if len(args) < 1 or identifier_or_key == __file__:
print(GENERAL_ERROR)
quit()
if 'all' in args:
# Run for all gorillas
pass
elif identifier_or_key is not None:
gorilla = get_gorilla(
identifier_or_key,
with_siblings_and_offsprings=True
)
if gorilla is not None:
if gorilla.alive is False:
print('''
Gorilla {} is not alive.
'''.format(gorilla.identifier))
quit()
print('''
Finding mates for {}
'''.format(gorilla.identifier))
mates = find_best_mates(gorilla)
else:
print(GENERAL_ERROR)
except Exception as ex:
print(ex)
traceback.print_exc()
print(GENERAL_ERROR)