-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.py
113 lines (84 loc) · 3.62 KB
/
utils.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
#!/usr/bin/env python
# coding=utf-8
# ==============================================================================
# title : utils.py
# description : helper functions
# author : Nicolas Coucke
# date : 2022-07-11
# version : 1
# usage : python utils.py
# notes : install the packages with "pip install -r requirements.txt"
# python_version : 3.9.2
# ==============================================================================
import torch
import numpy as np
def symmetric_matrix(value, size):
"""
Helper function to create coupling matrix with equal weights between oscillator
The "self-coupling weight" (the diagonal) is set to zero
Arguments:
---------
value: float
the coupling value between oscillators
size: int
the size of the matrix (size * size)
"""
matrix = value * torch.ones((size, size))
matrix.fill_diagonal_(0)
return matrix
def eucl_distance(location_1, location_2):
"""
Helper function to calculate the euclidian distance between two points
Arguments:
---------
location_1: tensor of length 2
the x and y coordinates of the first point
location_2: idem
Returns:
--------
Distance
"""
return torch.sqrt(torch.pow(location_1[0] - location_2[0],2) + torch.pow(location_1[1] - location_2[1],2))
def eucl_distance_np(location_1, location_2):
"""
Helper function to calculate the euclidian distance between two points
Arguments:
---------
location_1: tensor of length 2
the x and y coordinates of the first point
location_2: idem
Returns:
--------
Distance
"""
return np.sqrt((location_1[0] - location_2[0])**2 + (location_1[1] - location_2[1])**2)
def complementary_connection(connection_strength, asymmetry_degree):
return (1 - connection_strength) * asymmetry_degree + (1 - asymmetry_degree) * connection_strength
def initiate_coupling_weights(scale, asymmetry_degree, random_connections, n_oscillators, a_sens = 1, a_motor = 1, a_ips = 1, a_con = 1, a_soc_sens = 1, a_soc_motor = 1):
if random_connections:
a_sens = np.random.uniform()
a_motor = np.random.uniform()
a_ips_left = np.random.uniform()
a_con_left = np.random.uniform()
else:
a_ips_left = a_ips
a_con_left = a_con
# make random variables for connectivities:
a_ips_right = complementary_connection(a_ips_left, asymmetry_degree)
a_con_right = complementary_connection(a_con_left, asymmetry_degree)
if n_oscillators == 5:
# also determine connections to the 5th oscillator
if random_connections:
a_soc_sens_left = np.random.uniform()
a_soc_motor_left = np.random.uniform()
else:
a_soc_sens_left = a_soc_sens
a_soc_motor_left = a_soc_motor
a_soc_sens_right = complementary_connection(a_soc_sens_left, asymmetry_degree)
a_soc_motor_right = complementary_connection(a_soc_motor_left, asymmetry_degree)
coupling_weights = scale * np.array([ a_sens, a_ips_left, a_ips_right, a_con_left, a_con_right, a_motor,
a_soc_sens_left, a_soc_sens_right, a_soc_motor_left, a_soc_motor_right])
return coupling_weights, a_sens, a_ips_left, a_ips_right, a_con_left, a_con_right, a_motor, a_soc_sens_left, a_soc_sens_right, a_soc_motor_left, a_soc_motor_right
else:
coupling_weights = scale * np.array([ a_sens, a_ips_left, a_ips_right, a_con_left, a_con_right, a_motor])
return coupling_weights, a_sens, a_ips_left, a_ips_right, a_con_left, a_con_right, a_motor