1 | { |
---|
2 | "cells": [ |
---|
3 | { |
---|
4 | "cell_type": "code", |
---|
5 | "execution_count": 2, |
---|
6 | "id": "95d338ee-e31f-400e-9dae-c395cd19307c", |
---|
7 | "metadata": { |
---|
8 | "tags": [] |
---|
9 | }, |
---|
10 | "outputs": [ |
---|
11 | { |
---|
12 | "name": "stdout", |
---|
13 | "output_type": "stream", |
---|
14 | "text": [ |
---|
15 | "Using Framsticks version: 5.0rc25\n", |
---|
16 | "Home (writable) dir : /home/oriona/framsticks/framspy/Framsticks50rc25/data\n", |
---|
17 | "Resources dir : /home/oriona/framsticks/framspy/Framsticks50rc25/data\n", |
---|
18 | "\n" |
---|
19 | ] |
---|
20 | } |
---|
21 | ], |
---|
22 | "source": [ |
---|
23 | "import sys\n", |
---|
24 | "sys.path.append('..')\n", |
---|
25 | "import frams\n", |
---|
26 | "import numpy as np\n", |
---|
27 | "import importlib\n", |
---|
28 | "density = importlib.import_module(\"density-distribution\")\n", |
---|
29 | "from scipy import stats\n", |
---|
30 | "import gc\n", |
---|
31 | "import matplotlib.pyplot as plt\n", |
---|
32 | "\n", |
---|
33 | "\n", |
---|
34 | "DIR = 'genos/'\n", |
---|
35 | "files = [[\"swim.gen\", \"velocity\"], [\"walk.gen\", \"velocity\"], [\"vposa.gen\", \"velocity\"], [\"vposp.gen\", \"velocity\"]]\n", |
---|
36 | "\n", |
---|
37 | "def parse_fit(fname, goal):\n", |
---|
38 | " values = []\n", |
---|
39 | " with open(DIR+fname) as f:\n", |
---|
40 | " for line in f:\n", |
---|
41 | " if goal in line:\n", |
---|
42 | " splt = line.split(\":\")\n", |
---|
43 | " values.append(float(splt[1]))\n", |
---|
44 | " return values\n", |
---|
45 | "\n", |
---|
46 | "def get_dist(matrix, fits):\n", |
---|
47 | " n = len(matrix[0])\n", |
---|
48 | " dists = []\n", |
---|
49 | " for geno in range(n):\n", |
---|
50 | " better = []\n", |
---|
51 | " for other in range(n):\n", |
---|
52 | " if fits[other] >= fits[geno]:\n", |
---|
53 | " better.append(matrix[geno][other])\n", |
---|
54 | " sum_ = sum(better)\n", |
---|
55 | " if sum_ > 0:\n", |
---|
56 | " dists.append(sum_/len(better))\n", |
---|
57 | " else:\n", |
---|
58 | " dists.append(0)\n", |
---|
59 | " return dists\n", |
---|
60 | "\n", |
---|
61 | "path = '/home/oriona/framsticks/framspy/Framsticks50rc25'\n", |
---|
62 | "frams.init(path)\n", |
---|
63 | "\n", |
---|
64 | "measures = [[\"distribution\", False], [\"frequency\", True]]\n", |
---|
65 | "metrics = [\"l1\", \"l2\", \"emd\"]\n", |
---|
66 | "\n", |
---|
67 | "def plot_fdc(file):\n", |
---|
68 | " frams.Simulator.ximport(file[0])\n", |
---|
69 | " fits = parse_fit(file[0], file[1])\n", |
---|
70 | "\n", |
---|
71 | " fig, ax = plt.subplots(nrows=2, ncols=3, figsize=(15, 10))\n", |
---|
72 | " for i, measure in enumerate(measures):\n", |
---|
73 | " labels = []\n", |
---|
74 | " for j, metric in enumerate(metrics):\n", |
---|
75 | " print(measure[0], metric)\n", |
---|
76 | " dissim = density.DensityDistribution(path, frequency=measure[1], metric=metric)\n", |
---|
77 | " matrix = dissim.getDissimilarityMatrix([g.genotype for g in frams.GenePools[0]])\n", |
---|
78 | " dists = get_dist(matrix, fits)\n", |
---|
79 | " cor = stats.spearmanr(fits, dists)\n", |
---|
80 | " print(\"r=\", round(cor.correlation,2))\n", |
---|
81 | " ax[i, j].scatter(dists, fits)\n", |
---|
82 | " ax[i, j].set_xlabel(\"distance\")\n", |
---|
83 | " ax[i, j].set_ylabel(\"fitness\")\n", |
---|
84 | " ax[i,j].set_title(measure[0]+\" \"+metric+\" \"+str(round(cor.correlation, 2)))\n", |
---|
85 | " plt.show()\n", |
---|
86 | " plt.savefig(file[0][:-4]+\".pdf\")\n", |
---|
87 | " plt.cla()\n", |
---|
88 | " plt.clf()\n", |
---|
89 | " plt.close(fig)\n", |
---|
90 | " gc.collect()\n", |
---|
91 | "\n", |
---|
92 | " frams.GenePools[0].clear() \n", |
---|
93 | "\n", |
---|
94 | "#for file in [files[0]]:\n", |
---|
95 | "\n" |
---|
96 | ] |
---|
97 | }, |
---|
98 | { |
---|
99 | "cell_type": "code", |
---|
100 | "execution_count": null, |
---|
101 | "id": "f8e0932c-11e4-4884-8d14-86b3590d93d6", |
---|
102 | "metadata": { |
---|
103 | "tags": [] |
---|
104 | }, |
---|
105 | "outputs": [ |
---|
106 | { |
---|
107 | "name": "stdout", |
---|
108 | "output_type": "stream", |
---|
109 | "text": [ |
---|
110 | "distribution l1\n", |
---|
111 | "Using Framsticks version: 5.0rc25\n", |
---|
112 | "Home (writable) dir : /home/oriona/framsticks/framspy/Framsticks50rc25/data\n", |
---|
113 | "Resources dir : /home/oriona/framsticks/framspy/Framsticks50rc25/data\n", |
---|
114 | "\n", |
---|
115 | "r= -0.52\n", |
---|
116 | "distribution l2\n", |
---|
117 | "Using Framsticks version: 5.0rc25\n", |
---|
118 | "Home (writable) dir : /home/oriona/framsticks/framspy/Framsticks50rc25/data\n", |
---|
119 | "Resources dir : /home/oriona/framsticks/framspy/Framsticks50rc25/data\n", |
---|
120 | "\n", |
---|
121 | "r= -0.54\n", |
---|
122 | "distribution emd\n", |
---|
123 | "Using Framsticks version: 5.0rc25\n", |
---|
124 | "Home (writable) dir : /home/oriona/framsticks/framspy/Framsticks50rc25/data\n", |
---|
125 | "Resources dir : /home/oriona/framsticks/framspy/Framsticks50rc25/data\n", |
---|
126 | "\n", |
---|
127 | "r= -0.65\n", |
---|
128 | "frequency l1\n", |
---|
129 | "Using Framsticks version: 5.0rc25\n", |
---|
130 | "Home (writable) dir : /home/oriona/framsticks/framspy/Framsticks50rc25/data\n", |
---|
131 | "Resources dir : /home/oriona/framsticks/framspy/Framsticks50rc25/data\n", |
---|
132 | "\n", |
---|
133 | "r= -0.54\n", |
---|
134 | "frequency l2\n", |
---|
135 | "Using Framsticks version: 5.0rc25\n", |
---|
136 | "Home (writable) dir : /home/oriona/framsticks/framspy/Framsticks50rc25/data\n", |
---|
137 | "Resources dir : /home/oriona/framsticks/framspy/Framsticks50rc25/data\n", |
---|
138 | "\n", |
---|
139 | "r= -0.53\n", |
---|
140 | "frequency emd\n", |
---|
141 | "Using Framsticks version: 5.0rc25\n", |
---|
142 | "Home (writable) dir : /home/oriona/framsticks/framspy/Framsticks50rc25/data\n", |
---|
143 | "Resources dir : /home/oriona/framsticks/framspy/Framsticks50rc25/data\n", |
---|
144 | "\n" |
---|
145 | ] |
---|
146 | } |
---|
147 | ], |
---|
148 | "source": [ |
---|
149 | "plot_fdc(files[0])" |
---|
150 | ] |
---|
151 | }, |
---|
152 | { |
---|
153 | "cell_type": "code", |
---|
154 | "execution_count": null, |
---|
155 | "id": "bba87fc1-454e-4aad-b71e-bc52096d864e", |
---|
156 | "metadata": {}, |
---|
157 | "outputs": [], |
---|
158 | "source": [] |
---|
159 | } |
---|
160 | ], |
---|
161 | "metadata": { |
---|
162 | "kernelspec": { |
---|
163 | "display_name": "Python 3 (ipykernel)", |
---|
164 | "language": "python", |
---|
165 | "name": "python3" |
---|
166 | }, |
---|
167 | "language_info": { |
---|
168 | "codemirror_mode": { |
---|
169 | "name": "ipython", |
---|
170 | "version": 3 |
---|
171 | }, |
---|
172 | "file_extension": ".py", |
---|
173 | "mimetype": "text/x-python", |
---|
174 | "name": "python", |
---|
175 | "nbconvert_exporter": "python", |
---|
176 | "pygments_lexer": "ipython3", |
---|
177 | "version": "3.10.6" |
---|
178 | } |
---|
179 | }, |
---|
180 | "nbformat": 4, |
---|
181 | "nbformat_minor": 5 |
---|
182 | } |
---|