proxdecay 0.1.1__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- proxdecay-0.1.1/PKG-INFO +194 -0
- proxdecay-0.1.1/README.md +182 -0
- proxdecay-0.1.1/pyproject.toml +19 -0
- proxdecay-0.1.1/setup.cfg +4 -0
- proxdecay-0.1.1/src/proxdecay/__init__.py +16 -0
- proxdecay-0.1.1/src/proxdecay/core.py +116 -0
- proxdecay-0.1.1/src/proxdecay.egg-info/PKG-INFO +194 -0
- proxdecay-0.1.1/src/proxdecay.egg-info/SOURCES.txt +9 -0
- proxdecay-0.1.1/src/proxdecay.egg-info/dependency_links.txt +1 -0
- proxdecay-0.1.1/src/proxdecay.egg-info/requires.txt +5 -0
- proxdecay-0.1.1/src/proxdecay.egg-info/top_level.txt +1 -0
proxdecay-0.1.1/PKG-INFO
ADDED
|
@@ -0,0 +1,194 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: proxdecay
|
|
3
|
+
Version: 0.1.1
|
|
4
|
+
Summary: Exponential distance-decay proximity score for geographic coordinates
|
|
5
|
+
License: MIT
|
|
6
|
+
Requires-Python: >=3.9
|
|
7
|
+
Description-Content-Type: text/markdown
|
|
8
|
+
Provides-Extra: dev
|
|
9
|
+
Requires-Dist: pytest>=7.0; extra == "dev"
|
|
10
|
+
Requires-Dist: build; extra == "dev"
|
|
11
|
+
Requires-Dist: twine; extra == "dev"
|
|
12
|
+
|
|
13
|
+
# `proxdecay`-ES
|
|
14
|
+
|
|
15
|
+
## INTRODUCCIÓN
|
|
16
|
+
|
|
17
|
+
Esta librería de Python funciona como un medidor de concentración entre una serie de lugares. Para ello, utiliza una lista de coordenadas (latitud, longitud), el método de Haversine y una fórmula de decaimiento exponencial.
|
|
18
|
+
|
|
19
|
+
La fórmula empleada para el cálculo es la siguiente:
|
|
20
|
+
|
|
21
|
+
$$S = \sum_{i=1}^{n} e^{-d_i / \lambda}$$
|
|
22
|
+
|
|
23
|
+
donde:
|
|
24
|
+
- $d_i$ es la distancia calculada mediante Haversine entre el punto de referencia y uno de sus vecinos.
|
|
25
|
+
- $\lambda$ es el parámetro de escala o decaimiento aplicado. A mayor $\lambda$, mayor influencia tienen los vecinos más alejados. A la inversa, un $\lambda$ menor reduce la influencia de los vecinos lejanos y da mayor peso a los más cercanos.
|
|
26
|
+
|
|
27
|
+
## Supuestos y unidades
|
|
28
|
+
|
|
29
|
+
- **Punto de referencia excluido:** si la coordenada de referencia aparece en la lista de vecinos, no se contabiliza a sí misma.
|
|
30
|
+
- **Unidades:** por defecto, tanto la distancia como $\lambda$ se expresan en kilómetros (`km`). Sin embargo, es posible adaptar la librería para trabajar en metros (`m`), convirtiendo tanto las distancias como los parámetros de $\lambda$.
|
|
31
|
+
|
|
32
|
+
## Métodos y funciones dentro de `proxdecay`
|
|
33
|
+
|
|
34
|
+
- `haversine`: a partir de dos puntos definidos por sus coordenadas de latitud y longitud, y una unidad de distancia, calcula la distancia entre ellos mediante el método de Haversine.
|
|
35
|
+
|
|
36
|
+
**Entradas de la función:**
|
|
37
|
+
```python
|
|
38
|
+
(
|
|
39
|
+
lat1: float,
|
|
40
|
+
lon1: float,
|
|
41
|
+
lat2: float,
|
|
42
|
+
lon2: float,
|
|
43
|
+
unit: str
|
|
44
|
+
)
|
|
45
|
+
```
|
|
46
|
+
**Salida:** `float`
|
|
47
|
+
|
|
48
|
+
- `proximity_score`: calcula el índice de proximidad a partir de un punto de referencia y una lista de coordenadas.
|
|
49
|
+
|
|
50
|
+
**Entradas de la función:**
|
|
51
|
+
```python
|
|
52
|
+
(
|
|
53
|
+
origin: tuple[float, float],
|
|
54
|
+
neighbours: list[tuple[float, float]],
|
|
55
|
+
decay_length: float,
|
|
56
|
+
unit: str
|
|
57
|
+
)
|
|
58
|
+
```
|
|
59
|
+
**Salida:** `float`
|
|
60
|
+
|
|
61
|
+
- `batch_proximity_score`: similar a la función anterior, pero calcula el índice de proximidad para cada uno de los puntos de la lista.
|
|
62
|
+
|
|
63
|
+
**Entradas de la función:**
|
|
64
|
+
```python
|
|
65
|
+
(
|
|
66
|
+
places: list[tuple[float, float]],
|
|
67
|
+
decay_length: float,
|
|
68
|
+
unit: str
|
|
69
|
+
)
|
|
70
|
+
```
|
|
71
|
+
**Salida:** `list`
|
|
72
|
+
|
|
73
|
+
- `neighbours_within_radius`: utiliza un radio determinado como límite para filtrar y devolver los lugares que se encuentran dentro de esa distancia.
|
|
74
|
+
|
|
75
|
+
**Entradas de la función:**
|
|
76
|
+
```python
|
|
77
|
+
(
|
|
78
|
+
origin: tuple[float, float],
|
|
79
|
+
candidates: list[tuple[float, float]],
|
|
80
|
+
radius: float,
|
|
81
|
+
unit: str,
|
|
82
|
+
include_origin: bool
|
|
83
|
+
)
|
|
84
|
+
```
|
|
85
|
+
**Salida:** `list`
|
|
86
|
+
|
|
87
|
+
- `weighted_proximity_score`: utiliza la misma fórmula, pero con una ligera modificación: el peso asignado a cada establecimiento también influye en el resultado.
|
|
88
|
+
|
|
89
|
+
$$S_w = \sum_{i=1}^{n} w_i \cdot e^{-d_i / \lambda}$$
|
|
90
|
+
|
|
91
|
+
En este caso, $w_i$ representa el peso asignado a cada establecimiento. Este peso puede representar una valoración, popularidad u otra métrica relevante. De esta manera, se tiene en cuenta la importancia relativa de cada negocio, dando mayor prioridad a aquellos con pesos más elevados.
|
|
92
|
+
|
|
93
|
+
**Entradas de la función:**
|
|
94
|
+
```python
|
|
95
|
+
(
|
|
96
|
+
origin: tuple[float, float],
|
|
97
|
+
weighted_neighbours: list[tuple[float, float, float]],
|
|
98
|
+
decay_length: float,
|
|
99
|
+
unit: str
|
|
100
|
+
)
|
|
101
|
+
```
|
|
102
|
+
**Salida:** `float`
|
|
103
|
+
|
|
104
|
+
|
|
105
|
+
# `proxdecay`-EN
|
|
106
|
+
|
|
107
|
+
## INTRODUCTION
|
|
108
|
+
|
|
109
|
+
This Python library functions as a concentration measurement tool for a set of locations. To achieve this, it uses a list of coordinates (latitude, longitude), the Haversine method, and an exponential decay formula.
|
|
110
|
+
|
|
111
|
+
The formula used for the calculation is:
|
|
112
|
+
|
|
113
|
+
$$S = \sum_{i=1}^{n} e^{-d_i / \lambda}$$
|
|
114
|
+
|
|
115
|
+
where:
|
|
116
|
+
- $d_i$ is the distance calculated using the Haversine method between the reference point and one of its neighboring points.
|
|
117
|
+
- $\lambda$ is the applied scale or decay parameter. The higher the value of $\lambda$, the greater the influence of more distant neighboring points. Conversely, a lower $\lambda$ reduces the influence of distant neighbors and gives greater weight to nearby ones.
|
|
118
|
+
|
|
119
|
+
## Assumptions and Units
|
|
120
|
+
|
|
121
|
+
- **Reference point exclusion:** If the reference coordinates are present in the list of neighboring points, they are excluded from the calculation to prevent the reference point from being counted as its own neighbor.
|
|
122
|
+
- **Units:** By default, both distance and $\lambda$ are expressed in kilometers (`km`). However, the library can be adapted to work with meters (`m`) by converting both distances and $\lambda$ parameters accordingly.
|
|
123
|
+
|
|
124
|
+
## Methods and Functions within `proxdecay`
|
|
125
|
+
|
|
126
|
+
- `haversine`: Takes two points defined by their latitude and longitude coordinates, along with a distance unit, and calculates the distance between them using the Haversine method.
|
|
127
|
+
|
|
128
|
+
**Function inputs:**
|
|
129
|
+
```python
|
|
130
|
+
(
|
|
131
|
+
lat1: float,
|
|
132
|
+
lon1: float,
|
|
133
|
+
lat2: float,
|
|
134
|
+
lon2: float,
|
|
135
|
+
unit: str
|
|
136
|
+
)
|
|
137
|
+
```
|
|
138
|
+
**Output:** `float`
|
|
139
|
+
|
|
140
|
+
- `proximity_score`: Calculates the proximity score based on a reference point and a list of coordinates.
|
|
141
|
+
|
|
142
|
+
**Function inputs:**
|
|
143
|
+
```python
|
|
144
|
+
(
|
|
145
|
+
origin: tuple[float, float],
|
|
146
|
+
neighbours: list[tuple[float, float]],
|
|
147
|
+
decay_length: float,
|
|
148
|
+
unit: str
|
|
149
|
+
)
|
|
150
|
+
```
|
|
151
|
+
**Output:** `float`
|
|
152
|
+
|
|
153
|
+
- `batch_proximity_score`: Similar to the previous function, but calculates the proximity score for every point in the list.
|
|
154
|
+
|
|
155
|
+
**Function inputs:**
|
|
156
|
+
```python
|
|
157
|
+
(
|
|
158
|
+
places: list[tuple[float, float]],
|
|
159
|
+
decay_length: float,
|
|
160
|
+
unit: str
|
|
161
|
+
)
|
|
162
|
+
```
|
|
163
|
+
**Output:** `list`
|
|
164
|
+
|
|
165
|
+
- `neighbours_within_radius`: Uses a specified radius as a boundary to filter and return locations within that distance.
|
|
166
|
+
|
|
167
|
+
**Function inputs:**
|
|
168
|
+
```python
|
|
169
|
+
(
|
|
170
|
+
origin: tuple[float, float],
|
|
171
|
+
candidates: list[tuple[float, float]],
|
|
172
|
+
radius: float,
|
|
173
|
+
unit: str,
|
|
174
|
+
include_origin: bool
|
|
175
|
+
)
|
|
176
|
+
```
|
|
177
|
+
**Output:** `list`
|
|
178
|
+
|
|
179
|
+
- `weighted_proximity_score`: Uses the same formula, with one slight modification: the weight assigned to each location also contributes to the final result.
|
|
180
|
+
|
|
181
|
+
$$S_w = \sum_{i=1}^{n} w_i \cdot e^{-d_i / \lambda}$$
|
|
182
|
+
|
|
183
|
+
In this case, $w_i$ represents the weight assigned to each location. This weight could represent a review score, popularity, or another relevant metric. This allows the relative importance of each business to be taken into account, giving higher priority to those with greater weights.
|
|
184
|
+
|
|
185
|
+
**Function inputs:**
|
|
186
|
+
```python
|
|
187
|
+
(
|
|
188
|
+
origin: tuple[float, float],
|
|
189
|
+
weighted_neighbours: list[tuple[float, float, float]],
|
|
190
|
+
decay_length: float,
|
|
191
|
+
unit: str
|
|
192
|
+
)
|
|
193
|
+
```
|
|
194
|
+
**Output:** `float`
|
|
@@ -0,0 +1,182 @@
|
|
|
1
|
+
# `proxdecay`-ES
|
|
2
|
+
|
|
3
|
+
## INTRODUCCIÓN
|
|
4
|
+
|
|
5
|
+
Esta librería de Python funciona como un medidor de concentración entre una serie de lugares. Para ello, utiliza una lista de coordenadas (latitud, longitud), el método de Haversine y una fórmula de decaimiento exponencial.
|
|
6
|
+
|
|
7
|
+
La fórmula empleada para el cálculo es la siguiente:
|
|
8
|
+
|
|
9
|
+
$$S = \sum_{i=1}^{n} e^{-d_i / \lambda}$$
|
|
10
|
+
|
|
11
|
+
donde:
|
|
12
|
+
- $d_i$ es la distancia calculada mediante Haversine entre el punto de referencia y uno de sus vecinos.
|
|
13
|
+
- $\lambda$ es el parámetro de escala o decaimiento aplicado. A mayor $\lambda$, mayor influencia tienen los vecinos más alejados. A la inversa, un $\lambda$ menor reduce la influencia de los vecinos lejanos y da mayor peso a los más cercanos.
|
|
14
|
+
|
|
15
|
+
## Supuestos y unidades
|
|
16
|
+
|
|
17
|
+
- **Punto de referencia excluido:** si la coordenada de referencia aparece en la lista de vecinos, no se contabiliza a sí misma.
|
|
18
|
+
- **Unidades:** por defecto, tanto la distancia como $\lambda$ se expresan en kilómetros (`km`). Sin embargo, es posible adaptar la librería para trabajar en metros (`m`), convirtiendo tanto las distancias como los parámetros de $\lambda$.
|
|
19
|
+
|
|
20
|
+
## Métodos y funciones dentro de `proxdecay`
|
|
21
|
+
|
|
22
|
+
- `haversine`: a partir de dos puntos definidos por sus coordenadas de latitud y longitud, y una unidad de distancia, calcula la distancia entre ellos mediante el método de Haversine.
|
|
23
|
+
|
|
24
|
+
**Entradas de la función:**
|
|
25
|
+
```python
|
|
26
|
+
(
|
|
27
|
+
lat1: float,
|
|
28
|
+
lon1: float,
|
|
29
|
+
lat2: float,
|
|
30
|
+
lon2: float,
|
|
31
|
+
unit: str
|
|
32
|
+
)
|
|
33
|
+
```
|
|
34
|
+
**Salida:** `float`
|
|
35
|
+
|
|
36
|
+
- `proximity_score`: calcula el índice de proximidad a partir de un punto de referencia y una lista de coordenadas.
|
|
37
|
+
|
|
38
|
+
**Entradas de la función:**
|
|
39
|
+
```python
|
|
40
|
+
(
|
|
41
|
+
origin: tuple[float, float],
|
|
42
|
+
neighbours: list[tuple[float, float]],
|
|
43
|
+
decay_length: float,
|
|
44
|
+
unit: str
|
|
45
|
+
)
|
|
46
|
+
```
|
|
47
|
+
**Salida:** `float`
|
|
48
|
+
|
|
49
|
+
- `batch_proximity_score`: similar a la función anterior, pero calcula el índice de proximidad para cada uno de los puntos de la lista.
|
|
50
|
+
|
|
51
|
+
**Entradas de la función:**
|
|
52
|
+
```python
|
|
53
|
+
(
|
|
54
|
+
places: list[tuple[float, float]],
|
|
55
|
+
decay_length: float,
|
|
56
|
+
unit: str
|
|
57
|
+
)
|
|
58
|
+
```
|
|
59
|
+
**Salida:** `list`
|
|
60
|
+
|
|
61
|
+
- `neighbours_within_radius`: utiliza un radio determinado como límite para filtrar y devolver los lugares que se encuentran dentro de esa distancia.
|
|
62
|
+
|
|
63
|
+
**Entradas de la función:**
|
|
64
|
+
```python
|
|
65
|
+
(
|
|
66
|
+
origin: tuple[float, float],
|
|
67
|
+
candidates: list[tuple[float, float]],
|
|
68
|
+
radius: float,
|
|
69
|
+
unit: str,
|
|
70
|
+
include_origin: bool
|
|
71
|
+
)
|
|
72
|
+
```
|
|
73
|
+
**Salida:** `list`
|
|
74
|
+
|
|
75
|
+
- `weighted_proximity_score`: utiliza la misma fórmula, pero con una ligera modificación: el peso asignado a cada establecimiento también influye en el resultado.
|
|
76
|
+
|
|
77
|
+
$$S_w = \sum_{i=1}^{n} w_i \cdot e^{-d_i / \lambda}$$
|
|
78
|
+
|
|
79
|
+
En este caso, $w_i$ representa el peso asignado a cada establecimiento. Este peso puede representar una valoración, popularidad u otra métrica relevante. De esta manera, se tiene en cuenta la importancia relativa de cada negocio, dando mayor prioridad a aquellos con pesos más elevados.
|
|
80
|
+
|
|
81
|
+
**Entradas de la función:**
|
|
82
|
+
```python
|
|
83
|
+
(
|
|
84
|
+
origin: tuple[float, float],
|
|
85
|
+
weighted_neighbours: list[tuple[float, float, float]],
|
|
86
|
+
decay_length: float,
|
|
87
|
+
unit: str
|
|
88
|
+
)
|
|
89
|
+
```
|
|
90
|
+
**Salida:** `float`
|
|
91
|
+
|
|
92
|
+
|
|
93
|
+
# `proxdecay`-EN
|
|
94
|
+
|
|
95
|
+
## INTRODUCTION
|
|
96
|
+
|
|
97
|
+
This Python library functions as a concentration measurement tool for a set of locations. To achieve this, it uses a list of coordinates (latitude, longitude), the Haversine method, and an exponential decay formula.
|
|
98
|
+
|
|
99
|
+
The formula used for the calculation is:
|
|
100
|
+
|
|
101
|
+
$$S = \sum_{i=1}^{n} e^{-d_i / \lambda}$$
|
|
102
|
+
|
|
103
|
+
where:
|
|
104
|
+
- $d_i$ is the distance calculated using the Haversine method between the reference point and one of its neighboring points.
|
|
105
|
+
- $\lambda$ is the applied scale or decay parameter. The higher the value of $\lambda$, the greater the influence of more distant neighboring points. Conversely, a lower $\lambda$ reduces the influence of distant neighbors and gives greater weight to nearby ones.
|
|
106
|
+
|
|
107
|
+
## Assumptions and Units
|
|
108
|
+
|
|
109
|
+
- **Reference point exclusion:** If the reference coordinates are present in the list of neighboring points, they are excluded from the calculation to prevent the reference point from being counted as its own neighbor.
|
|
110
|
+
- **Units:** By default, both distance and $\lambda$ are expressed in kilometers (`km`). However, the library can be adapted to work with meters (`m`) by converting both distances and $\lambda$ parameters accordingly.
|
|
111
|
+
|
|
112
|
+
## Methods and Functions within `proxdecay`
|
|
113
|
+
|
|
114
|
+
- `haversine`: Takes two points defined by their latitude and longitude coordinates, along with a distance unit, and calculates the distance between them using the Haversine method.
|
|
115
|
+
|
|
116
|
+
**Function inputs:**
|
|
117
|
+
```python
|
|
118
|
+
(
|
|
119
|
+
lat1: float,
|
|
120
|
+
lon1: float,
|
|
121
|
+
lat2: float,
|
|
122
|
+
lon2: float,
|
|
123
|
+
unit: str
|
|
124
|
+
)
|
|
125
|
+
```
|
|
126
|
+
**Output:** `float`
|
|
127
|
+
|
|
128
|
+
- `proximity_score`: Calculates the proximity score based on a reference point and a list of coordinates.
|
|
129
|
+
|
|
130
|
+
**Function inputs:**
|
|
131
|
+
```python
|
|
132
|
+
(
|
|
133
|
+
origin: tuple[float, float],
|
|
134
|
+
neighbours: list[tuple[float, float]],
|
|
135
|
+
decay_length: float,
|
|
136
|
+
unit: str
|
|
137
|
+
)
|
|
138
|
+
```
|
|
139
|
+
**Output:** `float`
|
|
140
|
+
|
|
141
|
+
- `batch_proximity_score`: Similar to the previous function, but calculates the proximity score for every point in the list.
|
|
142
|
+
|
|
143
|
+
**Function inputs:**
|
|
144
|
+
```python
|
|
145
|
+
(
|
|
146
|
+
places: list[tuple[float, float]],
|
|
147
|
+
decay_length: float,
|
|
148
|
+
unit: str
|
|
149
|
+
)
|
|
150
|
+
```
|
|
151
|
+
**Output:** `list`
|
|
152
|
+
|
|
153
|
+
- `neighbours_within_radius`: Uses a specified radius as a boundary to filter and return locations within that distance.
|
|
154
|
+
|
|
155
|
+
**Function inputs:**
|
|
156
|
+
```python
|
|
157
|
+
(
|
|
158
|
+
origin: tuple[float, float],
|
|
159
|
+
candidates: list[tuple[float, float]],
|
|
160
|
+
radius: float,
|
|
161
|
+
unit: str,
|
|
162
|
+
include_origin: bool
|
|
163
|
+
)
|
|
164
|
+
```
|
|
165
|
+
**Output:** `list`
|
|
166
|
+
|
|
167
|
+
- `weighted_proximity_score`: Uses the same formula, with one slight modification: the weight assigned to each location also contributes to the final result.
|
|
168
|
+
|
|
169
|
+
$$S_w = \sum_{i=1}^{n} w_i \cdot e^{-d_i / \lambda}$$
|
|
170
|
+
|
|
171
|
+
In this case, $w_i$ represents the weight assigned to each location. This weight could represent a review score, popularity, or another relevant metric. This allows the relative importance of each business to be taken into account, giving higher priority to those with greater weights.
|
|
172
|
+
|
|
173
|
+
**Function inputs:**
|
|
174
|
+
```python
|
|
175
|
+
(
|
|
176
|
+
origin: tuple[float, float],
|
|
177
|
+
weighted_neighbours: list[tuple[float, float, float]],
|
|
178
|
+
decay_length: float,
|
|
179
|
+
unit: str
|
|
180
|
+
)
|
|
181
|
+
```
|
|
182
|
+
**Output:** `float`
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
[build-system]
|
|
2
|
+
requires = ["setuptools>=61.0"]
|
|
3
|
+
build-backend = "setuptools.build_meta"
|
|
4
|
+
|
|
5
|
+
[project]
|
|
6
|
+
name = "proxdecay"
|
|
7
|
+
version = "0.1.1"
|
|
8
|
+
description = "Exponential distance-decay proximity score for geographic coordinates"
|
|
9
|
+
readme = "README.md"
|
|
10
|
+
requires-python = ">=3.9"
|
|
11
|
+
license = { text = "MIT" }
|
|
12
|
+
dependencies = []
|
|
13
|
+
|
|
14
|
+
[project.optional-dependencies]
|
|
15
|
+
dev = ["pytest>=7.0", "build", "twine"]
|
|
16
|
+
|
|
17
|
+
[tool.setuptools.packages.find]
|
|
18
|
+
where = ["src"]
|
|
19
|
+
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
from .core import (
|
|
2
|
+
batch_proximity_score,
|
|
3
|
+
haversine,
|
|
4
|
+
neighbors_within_radius,
|
|
5
|
+
proximity_score,
|
|
6
|
+
weighted_proximity_score,
|
|
7
|
+
)
|
|
8
|
+
|
|
9
|
+
__all__ = [
|
|
10
|
+
"haversine",
|
|
11
|
+
"proximity_score",
|
|
12
|
+
"batch_proximity_score",
|
|
13
|
+
"neighbors_within_radius",
|
|
14
|
+
"weighted_proximity_score",
|
|
15
|
+
]
|
|
16
|
+
__version__ = "0.1.0"
|
|
@@ -0,0 +1,116 @@
|
|
|
1
|
+
import math
|
|
2
|
+
|
|
3
|
+
Coordinate = tuple[float, float]
|
|
4
|
+
WeightedCoordinate = tuple[float, float, float]
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
def haversine(
|
|
8
|
+
lat1: float,
|
|
9
|
+
lon1: float,
|
|
10
|
+
lat2: float,
|
|
11
|
+
lon2: float,
|
|
12
|
+
unit: str = "km",
|
|
13
|
+
) -> float:
|
|
14
|
+
|
|
15
|
+
scale = 1.0 if unit == "km" else 1000.0
|
|
16
|
+
earth_radius = 6371.0 * scale
|
|
17
|
+
|
|
18
|
+
phi1, phi2 = math.radians(lat1), math.radians(lat2)
|
|
19
|
+
dphi = math.radians(lat2 - lat1)
|
|
20
|
+
dlambda = math.radians(lon2 - lon1)
|
|
21
|
+
|
|
22
|
+
a = (
|
|
23
|
+
math.sin(dphi / 2) ** 2
|
|
24
|
+
+ math.cos(phi1) * math.cos(phi2) * math.sin(dlambda / 2) ** 2
|
|
25
|
+
)
|
|
26
|
+
return 2.0 * earth_radius * math.atan2(math.sqrt(a), math.sqrt(1.0 - a))
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
def proximity_score(
|
|
30
|
+
origin: Coordinate,
|
|
31
|
+
neighbors: list[Coordinate],
|
|
32
|
+
decay_length: float = 1.0,
|
|
33
|
+
unit: str = "km",
|
|
34
|
+
) -> float:
|
|
35
|
+
|
|
36
|
+
if decay_length <= 0:
|
|
37
|
+
raise ValueError("decay_length must be greater than 0.")
|
|
38
|
+
|
|
39
|
+
lat, lon = origin
|
|
40
|
+
score = 0.0
|
|
41
|
+
|
|
42
|
+
for rlat, rlon in neighbors:
|
|
43
|
+
distance = haversine(lat, lon, rlat, rlon, unit=unit)
|
|
44
|
+
if distance > 0:
|
|
45
|
+
score += math.exp(-distance / decay_length)
|
|
46
|
+
|
|
47
|
+
return score
|
|
48
|
+
|
|
49
|
+
|
|
50
|
+
def batch_proximity_score(
|
|
51
|
+
places: list[Coordinate],
|
|
52
|
+
decay_length: float = 1.0,
|
|
53
|
+
unit: str = "km",
|
|
54
|
+
) -> list[float]:
|
|
55
|
+
|
|
56
|
+
if decay_length <= 0:
|
|
57
|
+
raise ValueError("decay_length must be greater than 0.")
|
|
58
|
+
|
|
59
|
+
scores = []
|
|
60
|
+
for place in places:
|
|
61
|
+
scores.append(
|
|
62
|
+
proximity_score(
|
|
63
|
+
origin=place,
|
|
64
|
+
neighbors=places,
|
|
65
|
+
decay_length=decay_length,
|
|
66
|
+
unit=unit,
|
|
67
|
+
)
|
|
68
|
+
)
|
|
69
|
+
return scores
|
|
70
|
+
|
|
71
|
+
|
|
72
|
+
def neighbors_within_radius(
|
|
73
|
+
origin: Coordinate,
|
|
74
|
+
candidates: list[Coordinate],
|
|
75
|
+
radius: float,
|
|
76
|
+
unit: str = "km",
|
|
77
|
+
include_origin: bool = False,
|
|
78
|
+
) -> list[Coordinate]:
|
|
79
|
+
|
|
80
|
+
if radius < 0:
|
|
81
|
+
raise ValueError("radius must be greater than or equal to 0.")
|
|
82
|
+
|
|
83
|
+
lat, lon = origin
|
|
84
|
+
filtered: list[Coordinate] = []
|
|
85
|
+
|
|
86
|
+
for clat, clon in candidates:
|
|
87
|
+
distance = haversine(lat, lon, clat, clon, unit=unit)
|
|
88
|
+
if not include_origin and distance == 0:
|
|
89
|
+
continue
|
|
90
|
+
if distance <= radius:
|
|
91
|
+
filtered.append((clat, clon))
|
|
92
|
+
|
|
93
|
+
return filtered
|
|
94
|
+
|
|
95
|
+
|
|
96
|
+
def weighted_proximity_score(
|
|
97
|
+
origin: Coordinate,
|
|
98
|
+
weighted_neighbors: list[WeightedCoordinate],
|
|
99
|
+
decay_length: float = 1.0,
|
|
100
|
+
unit: str = "km",
|
|
101
|
+
) -> float:
|
|
102
|
+
|
|
103
|
+
if decay_length <= 0:
|
|
104
|
+
raise ValueError("decay_length must be greater than 0.")
|
|
105
|
+
|
|
106
|
+
lat, lon = origin
|
|
107
|
+
score = 0.0
|
|
108
|
+
|
|
109
|
+
for nlat, nlon, weight in weighted_neighbors:
|
|
110
|
+
if weight < 0:
|
|
111
|
+
raise ValueError("weight must be greater than or equal to 0.")
|
|
112
|
+
distance = haversine(lat, lon, nlat, nlon, unit=unit)
|
|
113
|
+
if distance > 0:
|
|
114
|
+
score += weight * math.exp(-distance / decay_length)
|
|
115
|
+
|
|
116
|
+
return score
|
|
@@ -0,0 +1,194 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: proxdecay
|
|
3
|
+
Version: 0.1.1
|
|
4
|
+
Summary: Exponential distance-decay proximity score for geographic coordinates
|
|
5
|
+
License: MIT
|
|
6
|
+
Requires-Python: >=3.9
|
|
7
|
+
Description-Content-Type: text/markdown
|
|
8
|
+
Provides-Extra: dev
|
|
9
|
+
Requires-Dist: pytest>=7.0; extra == "dev"
|
|
10
|
+
Requires-Dist: build; extra == "dev"
|
|
11
|
+
Requires-Dist: twine; extra == "dev"
|
|
12
|
+
|
|
13
|
+
# `proxdecay`-ES
|
|
14
|
+
|
|
15
|
+
## INTRODUCCIÓN
|
|
16
|
+
|
|
17
|
+
Esta librería de Python funciona como un medidor de concentración entre una serie de lugares. Para ello, utiliza una lista de coordenadas (latitud, longitud), el método de Haversine y una fórmula de decaimiento exponencial.
|
|
18
|
+
|
|
19
|
+
La fórmula empleada para el cálculo es la siguiente:
|
|
20
|
+
|
|
21
|
+
$$S = \sum_{i=1}^{n} e^{-d_i / \lambda}$$
|
|
22
|
+
|
|
23
|
+
donde:
|
|
24
|
+
- $d_i$ es la distancia calculada mediante Haversine entre el punto de referencia y uno de sus vecinos.
|
|
25
|
+
- $\lambda$ es el parámetro de escala o decaimiento aplicado. A mayor $\lambda$, mayor influencia tienen los vecinos más alejados. A la inversa, un $\lambda$ menor reduce la influencia de los vecinos lejanos y da mayor peso a los más cercanos.
|
|
26
|
+
|
|
27
|
+
## Supuestos y unidades
|
|
28
|
+
|
|
29
|
+
- **Punto de referencia excluido:** si la coordenada de referencia aparece en la lista de vecinos, no se contabiliza a sí misma.
|
|
30
|
+
- **Unidades:** por defecto, tanto la distancia como $\lambda$ se expresan en kilómetros (`km`). Sin embargo, es posible adaptar la librería para trabajar en metros (`m`), convirtiendo tanto las distancias como los parámetros de $\lambda$.
|
|
31
|
+
|
|
32
|
+
## Métodos y funciones dentro de `proxdecay`
|
|
33
|
+
|
|
34
|
+
- `haversine`: a partir de dos puntos definidos por sus coordenadas de latitud y longitud, y una unidad de distancia, calcula la distancia entre ellos mediante el método de Haversine.
|
|
35
|
+
|
|
36
|
+
**Entradas de la función:**
|
|
37
|
+
```python
|
|
38
|
+
(
|
|
39
|
+
lat1: float,
|
|
40
|
+
lon1: float,
|
|
41
|
+
lat2: float,
|
|
42
|
+
lon2: float,
|
|
43
|
+
unit: str
|
|
44
|
+
)
|
|
45
|
+
```
|
|
46
|
+
**Salida:** `float`
|
|
47
|
+
|
|
48
|
+
- `proximity_score`: calcula el índice de proximidad a partir de un punto de referencia y una lista de coordenadas.
|
|
49
|
+
|
|
50
|
+
**Entradas de la función:**
|
|
51
|
+
```python
|
|
52
|
+
(
|
|
53
|
+
origin: tuple[float, float],
|
|
54
|
+
neighbours: list[tuple[float, float]],
|
|
55
|
+
decay_length: float,
|
|
56
|
+
unit: str
|
|
57
|
+
)
|
|
58
|
+
```
|
|
59
|
+
**Salida:** `float`
|
|
60
|
+
|
|
61
|
+
- `batch_proximity_score`: similar a la función anterior, pero calcula el índice de proximidad para cada uno de los puntos de la lista.
|
|
62
|
+
|
|
63
|
+
**Entradas de la función:**
|
|
64
|
+
```python
|
|
65
|
+
(
|
|
66
|
+
places: list[tuple[float, float]],
|
|
67
|
+
decay_length: float,
|
|
68
|
+
unit: str
|
|
69
|
+
)
|
|
70
|
+
```
|
|
71
|
+
**Salida:** `list`
|
|
72
|
+
|
|
73
|
+
- `neighbours_within_radius`: utiliza un radio determinado como límite para filtrar y devolver los lugares que se encuentran dentro de esa distancia.
|
|
74
|
+
|
|
75
|
+
**Entradas de la función:**
|
|
76
|
+
```python
|
|
77
|
+
(
|
|
78
|
+
origin: tuple[float, float],
|
|
79
|
+
candidates: list[tuple[float, float]],
|
|
80
|
+
radius: float,
|
|
81
|
+
unit: str,
|
|
82
|
+
include_origin: bool
|
|
83
|
+
)
|
|
84
|
+
```
|
|
85
|
+
**Salida:** `list`
|
|
86
|
+
|
|
87
|
+
- `weighted_proximity_score`: utiliza la misma fórmula, pero con una ligera modificación: el peso asignado a cada establecimiento también influye en el resultado.
|
|
88
|
+
|
|
89
|
+
$$S_w = \sum_{i=1}^{n} w_i \cdot e^{-d_i / \lambda}$$
|
|
90
|
+
|
|
91
|
+
En este caso, $w_i$ representa el peso asignado a cada establecimiento. Este peso puede representar una valoración, popularidad u otra métrica relevante. De esta manera, se tiene en cuenta la importancia relativa de cada negocio, dando mayor prioridad a aquellos con pesos más elevados.
|
|
92
|
+
|
|
93
|
+
**Entradas de la función:**
|
|
94
|
+
```python
|
|
95
|
+
(
|
|
96
|
+
origin: tuple[float, float],
|
|
97
|
+
weighted_neighbours: list[tuple[float, float, float]],
|
|
98
|
+
decay_length: float,
|
|
99
|
+
unit: str
|
|
100
|
+
)
|
|
101
|
+
```
|
|
102
|
+
**Salida:** `float`
|
|
103
|
+
|
|
104
|
+
|
|
105
|
+
# `proxdecay`-EN
|
|
106
|
+
|
|
107
|
+
## INTRODUCTION
|
|
108
|
+
|
|
109
|
+
This Python library functions as a concentration measurement tool for a set of locations. To achieve this, it uses a list of coordinates (latitude, longitude), the Haversine method, and an exponential decay formula.
|
|
110
|
+
|
|
111
|
+
The formula used for the calculation is:
|
|
112
|
+
|
|
113
|
+
$$S = \sum_{i=1}^{n} e^{-d_i / \lambda}$$
|
|
114
|
+
|
|
115
|
+
where:
|
|
116
|
+
- $d_i$ is the distance calculated using the Haversine method between the reference point and one of its neighboring points.
|
|
117
|
+
- $\lambda$ is the applied scale or decay parameter. The higher the value of $\lambda$, the greater the influence of more distant neighboring points. Conversely, a lower $\lambda$ reduces the influence of distant neighbors and gives greater weight to nearby ones.
|
|
118
|
+
|
|
119
|
+
## Assumptions and Units
|
|
120
|
+
|
|
121
|
+
- **Reference point exclusion:** If the reference coordinates are present in the list of neighboring points, they are excluded from the calculation to prevent the reference point from being counted as its own neighbor.
|
|
122
|
+
- **Units:** By default, both distance and $\lambda$ are expressed in kilometers (`km`). However, the library can be adapted to work with meters (`m`) by converting both distances and $\lambda$ parameters accordingly.
|
|
123
|
+
|
|
124
|
+
## Methods and Functions within `proxdecay`
|
|
125
|
+
|
|
126
|
+
- `haversine`: Takes two points defined by their latitude and longitude coordinates, along with a distance unit, and calculates the distance between them using the Haversine method.
|
|
127
|
+
|
|
128
|
+
**Function inputs:**
|
|
129
|
+
```python
|
|
130
|
+
(
|
|
131
|
+
lat1: float,
|
|
132
|
+
lon1: float,
|
|
133
|
+
lat2: float,
|
|
134
|
+
lon2: float,
|
|
135
|
+
unit: str
|
|
136
|
+
)
|
|
137
|
+
```
|
|
138
|
+
**Output:** `float`
|
|
139
|
+
|
|
140
|
+
- `proximity_score`: Calculates the proximity score based on a reference point and a list of coordinates.
|
|
141
|
+
|
|
142
|
+
**Function inputs:**
|
|
143
|
+
```python
|
|
144
|
+
(
|
|
145
|
+
origin: tuple[float, float],
|
|
146
|
+
neighbours: list[tuple[float, float]],
|
|
147
|
+
decay_length: float,
|
|
148
|
+
unit: str
|
|
149
|
+
)
|
|
150
|
+
```
|
|
151
|
+
**Output:** `float`
|
|
152
|
+
|
|
153
|
+
- `batch_proximity_score`: Similar to the previous function, but calculates the proximity score for every point in the list.
|
|
154
|
+
|
|
155
|
+
**Function inputs:**
|
|
156
|
+
```python
|
|
157
|
+
(
|
|
158
|
+
places: list[tuple[float, float]],
|
|
159
|
+
decay_length: float,
|
|
160
|
+
unit: str
|
|
161
|
+
)
|
|
162
|
+
```
|
|
163
|
+
**Output:** `list`
|
|
164
|
+
|
|
165
|
+
- `neighbours_within_radius`: Uses a specified radius as a boundary to filter and return locations within that distance.
|
|
166
|
+
|
|
167
|
+
**Function inputs:**
|
|
168
|
+
```python
|
|
169
|
+
(
|
|
170
|
+
origin: tuple[float, float],
|
|
171
|
+
candidates: list[tuple[float, float]],
|
|
172
|
+
radius: float,
|
|
173
|
+
unit: str,
|
|
174
|
+
include_origin: bool
|
|
175
|
+
)
|
|
176
|
+
```
|
|
177
|
+
**Output:** `list`
|
|
178
|
+
|
|
179
|
+
- `weighted_proximity_score`: Uses the same formula, with one slight modification: the weight assigned to each location also contributes to the final result.
|
|
180
|
+
|
|
181
|
+
$$S_w = \sum_{i=1}^{n} w_i \cdot e^{-d_i / \lambda}$$
|
|
182
|
+
|
|
183
|
+
In this case, $w_i$ represents the weight assigned to each location. This weight could represent a review score, popularity, or another relevant metric. This allows the relative importance of each business to be taken into account, giving higher priority to those with greater weights.
|
|
184
|
+
|
|
185
|
+
**Function inputs:**
|
|
186
|
+
```python
|
|
187
|
+
(
|
|
188
|
+
origin: tuple[float, float],
|
|
189
|
+
weighted_neighbours: list[tuple[float, float, float]],
|
|
190
|
+
decay_length: float,
|
|
191
|
+
unit: str
|
|
192
|
+
)
|
|
193
|
+
```
|
|
194
|
+
**Output:** `float`
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
README.md
|
|
2
|
+
pyproject.toml
|
|
3
|
+
src/proxdecay/__init__.py
|
|
4
|
+
src/proxdecay/core.py
|
|
5
|
+
src/proxdecay.egg-info/PKG-INFO
|
|
6
|
+
src/proxdecay.egg-info/SOURCES.txt
|
|
7
|
+
src/proxdecay.egg-info/dependency_links.txt
|
|
8
|
+
src/proxdecay.egg-info/requires.txt
|
|
9
|
+
src/proxdecay.egg-info/top_level.txt
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
proxdecay
|