synthetic
predspot.synthetic
¶
Synthetic Data Module¶
Generates realistic-looking synthetic crime events inside a study area, so that Predspot can be tried, demonstrated and tested without real data.
The generator is a simple inhomogeneous space-time point process:
- Space — a mixture of
n_hotspotsGaussian hotspots (centres drawn uniformly inside the study area) plus a uniform background. The fraction of events that belong to hotspots ishotspot_share. - Time — an intensity built from a linear trend, an annual cycle, a day-of-week profile and an hour-of-day profile. Timestamps are drawn by thinning uniform candidates, which is exact for a fixed number of events.
Example::
from predspot.synthetic import generate_crimes
crimes = generate_crimes(study_area, n_events=5000, seed=0)
dataset = Dataset(crimes, study_area)
sample_points_in_polygon
¶
Draw n points uniformly inside a polygon by rejection sampling.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
polygon
|
Geometry
|
Polygon in WGS84. |
required |
n
|
int
|
Number of points. |
required |
rng
|
Generator
|
Random generator. |
required |
max_iterations
|
int
|
Safety cap on rejection rounds. |
1000
|
Returns:
| Type | Description |
|---|---|
tuple
|
|
Source code in src/predspot/synthetic.py
sample_points_around
¶
Draw points from Gaussian clouds around hotspot centres, kept inside the polygon.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
polygon
|
Geometry
|
Study area in WGS84. |
required |
centers
|
ndarray
|
|
required |
sd_km
|
float or array
|
Standard deviation of each cloud in km. |
required |
n_per_center
|
array
|
Number of points to draw per centre. |
required |
rng
|
Generator
|
Random generator. |
required |
max_iterations
|
int
|
Safety cap on rejection rounds. |
1000
|
Returns:
| Type | Description |
|---|---|
tuple
|
|
Source code in src/predspot/synthetic.py
temporal_intensity
¶
temporal_intensity(timestamps, start, end, trend=0.0, annual_amplitude=0.0, annual_peak_month=1, weekly_profile=None, hourly_profile=None)
Relative event intensity at each timestamp (mean around 1).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
timestamps
|
DatetimeIndex
|
Times to evaluate. |
required |
start
|
Timestamp
|
Start of the simulation, used for the trend. |
required |
end
|
Timestamp
|
End of the simulation, used for the trend. |
required |
trend
|
float
|
Relative change of the intensity from |
0.0
|
annual_amplitude
|
float
|
Amplitude of the annual cosine (0-1). |
0.0
|
annual_peak_month
|
int
|
Month (1-12) where the annual cycle peaks. |
1
|
weekly_profile
|
sequence
|
7 relative weights, Monday to Sunday. |
None
|
hourly_profile
|
sequence
|
24 relative weights, hour 0 to 23. |
None
|
Returns:
| Type | Description |
|---|---|
ndarray
|
Intensity values, one per timestamp. |
Source code in src/predspot/synthetic.py
sample_timestamps
¶
Draw n timestamps from an inhomogeneous process by thinning.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n
|
int
|
Number of timestamps. |
required |
start
|
Timestamp
|
Start of the simulation. |
required |
end
|
Timestamp
|
End of the simulation. |
required |
rng
|
Generator
|
Random generator. |
required |
max_iterations
|
int
|
Safety cap on thinning rounds. |
1000
|
**intensity_kwargs
|
dict
|
Forwarded to
|
{}
|
Returns:
| Type | Description |
|---|---|
DatetimeIndex
|
|
Source code in src/predspot/synthetic.py
generate_crimes
¶
generate_crimes(study_area, n_events=5000, start='2019-01-01', end='2020-12-31', n_hotspots=3, hotspot_share=0.7, hotspot_sd_km=0.5, tags=None, trend=0.0, annual_amplitude=0.2, annual_peak_month=1, weekly_profile=DEFAULT_WEEKLY_PROFILE, hourly_profile=DEFAULT_HOURLY_PROFILE, seed=None, return_hotspots=False)
Generate synthetic crime events inside a study area.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
study_area
|
GeoDataFrame
|
Boundary of the study area (any CRS). See
|
required |
n_events
|
int
|
Number of events to generate. |
5000
|
start
|
str or Timestamp
|
First possible timestamp. |
'2019-01-01'
|
end
|
str or Timestamp
|
Last possible timestamp. |
'2020-12-31'
|
n_hotspots
|
int
|
Number of Gaussian hotspots (0 for a uniform map). |
3
|
hotspot_share
|
float
|
Fraction of events that belong to hotspots; the rest is uniform background (0-1). |
0.7
|
hotspot_sd_km
|
float or sequence
|
Standard deviation of the hotspot clouds in km (one value or one per hotspot). |
0.5
|
tags
|
dict or sequence
|
Crime types. A dict maps type to relative
weight; a sequence gives equal weights. Defaults to
|
None
|
trend
|
float
|
Relative change of the event rate from |
0.0
|
annual_amplitude
|
float
|
Amplitude of the annual cycle (0 disables). |
0.2
|
annual_peak_month
|
int
|
Month (1-12) where the annual cycle peaks. |
1
|
weekly_profile
|
sequence or None
|
7 weights Monday..Sunday
( |
DEFAULT_WEEKLY_PROFILE
|
hourly_profile
|
sequence or None
|
24 weights, hour 0..23
( |
DEFAULT_HOURLY_PROFILE
|
seed
|
int
|
Seed for reproducibility. |
None
|
return_hotspots
|
bool
|
Also return the hotspot centres. |
False
|
Returns:
| Type | Description |
|---|---|
DataFrame
|
Events with |
Source code in src/predspot/synthetic.py
223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 | |