crimepy-cdc 0.1.0__py3-none-any.whl

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.
Binary file
Binary file
Binary file
@@ -0,0 +1,49 @@
1
+ Source,Target
2
+ 20,1
3
+ 3,2
4
+ 2,3
5
+ 18,4
6
+ 6,5
7
+ 5,6
8
+ 7,6
9
+ 21,6
10
+ 6,7
11
+ 21,7
12
+ 10,8
13
+ 14,8
14
+ 15,8
15
+ 21,8
16
+ 17,9
17
+ 8,10
18
+ 18,10
19
+ 20,10
20
+ 21,10
21
+ 22,11
22
+ 16,12
23
+ 17,13
24
+ 8,14
25
+ 16,14
26
+ 8,15
27
+ 20,15
28
+ 21,15
29
+ 12,16
30
+ 14,16
31
+ 9,17
32
+ 13,17
33
+ 4,18
34
+ 10,18
35
+ 20,18
36
+ 21,19
37
+ 1,20
38
+ 10,20
39
+ 15,20
40
+ 18,20
41
+ 21,20
42
+ 6,21
43
+ 7,21
44
+ 8,21
45
+ 10,21
46
+ 15,21
47
+ 19,21
48
+ 20,21
49
+ 11,22
@@ -0,0 +1,23 @@
1
+ Id,TotalInc,Attended,impact,Prob_or_Par
2
+ 1,2,0,0,0
3
+ 2,2,0,1,1
4
+ 3,5,0,1,0
5
+ 4,5,1,1,0
6
+ 5,4,1,1,1
7
+ 6,4,0,1,0
8
+ 7,4,0,1,1
9
+ 8,11,0,1,0
10
+ 9,7,0,1,0
11
+ 10,7,0,1,0
12
+ 11,4,1,1,0
13
+ 12,12,0,0,1
14
+ 13,4,0,1,0
15
+ 14,13,0,0,0
16
+ 15,9,0,0,0
17
+ 16,3,0,0,0
18
+ 17,8,0,1,0
19
+ 18,4,0,1,0
20
+ 19,2,0,1,0
21
+ 20,19,0,1,1
22
+ 21,16,0,1,0
23
+ 22,7,1,1,0
crimepy/__init__.py ADDED
File without changes
crimepy/_optional.py ADDED
@@ -0,0 +1,34 @@
1
+ """Import optional dependencies the first time they are used."""
2
+
3
+ from __future__ import annotations
4
+
5
+ import importlib
6
+ from types import ModuleType
7
+
8
+
9
+ class optional_dependency:
10
+ """Attribute access imports *module_name* and caches the module.
11
+
12
+ A missing install raises ``ImportError`` naming the extra that provides it.
13
+ """
14
+
15
+ def __init__(self, module_name: str, extra: str, feature: str) -> None:
16
+ self._module_name = module_name
17
+ self._extra = extra
18
+ self._feature = feature
19
+ self._module: ModuleType | None = None
20
+
21
+ def _load(self) -> ModuleType:
22
+ if self._module is None:
23
+ try:
24
+ self._module = importlib.import_module(self._module_name)
25
+ except ImportError as exc:
26
+ raise ImportError(
27
+ f"{self._feature} requires the optional package "
28
+ f"{self._module_name!r}. Install it with: "
29
+ f"pip install 'crimepy-cdc[{self._extra}]'"
30
+ ) from exc
31
+ return self._module
32
+
33
+ def __getattr__(self, name: str):
34
+ return getattr(self._load(), name)
crimepy/aoristic.py ADDED
@@ -0,0 +1,412 @@
1
+ '''
2
+ Functions to conduct aoristic
3
+ analysis using python
4
+
5
+ Claude rewrite to make more efficient
6
+ '''
7
+
8
+ import pandas as pd
9
+ import numpy as np
10
+ import matplotlib.pyplot as plt
11
+
12
+ # Constants - precompute for efficiency
13
+ WEEK_HOUR_TUPLES = [(wd, hr) for wd in range(7) for hr in range(24)]
14
+ WEEK_HOUR_DF = pd.DataFrame(WEEK_HOUR_TUPLES, columns=['weekday', 'hour'])
15
+ WEEK_EQUAL = np.full(168, 1.0/168) # Use numpy array for speed
16
+ WD_LABELS = ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun']
17
+ WD_DICT = {i: w for i, w in enumerate(WD_LABELS)}
18
+ WEEK_HOUR_LABELS = [f'{wd}_{hr}' for wd in WD_LABELS for hr in range(24)]
19
+
20
+ # Precompute weekday-hour index mapping for O(1) lookups
21
+ WEEKDAY_HOUR_INDEX = {(wd, hr): idx for idx, (wd, hr) in enumerate(WEEK_HOUR_TUPLES)}
22
+
23
+ # Constants needed
24
+ week_hour = [(wd,hr) for wd in range(7) for hr in range(24)]
25
+ week_hour_df = pd.DataFrame(week_hour,columns=['weekday','hour'])
26
+ week_equal = [1.0/len(week_hour)]*len(week_hour)
27
+ week_missing = [None]*len(week_hour)
28
+ wd_lab = ['Mon','Tue','Wed','Thu','Fri','Sat','Sun']
29
+ wd_di = {i:w for i,w in enumerate(wd_lab)}
30
+ week_hour_labs = [f'{wd}_{hr}' for wd in wd_lab for hr in range(24)]
31
+ week_color = ['#BABABA', '#878787', '#3F0001', '#7F0103', '#D6604D', '#F4A582', '#FDDBC7']
32
+
33
+ # Claude rewrite to make more efficient
34
+ def weekhour_func_vectorized(begin_series, end_series):
35
+ '''
36
+ Vectorized version of weekhour_func that processes entire series at once
37
+
38
+ Parameters:
39
+ -----------
40
+ begin_series : pd.Series of datetime
41
+ end_series : pd.Series of datetime
42
+
43
+ Returns:
44
+ --------
45
+ np.ndarray of shape (n_rows, 168) with weights for each weekday-hour combination
46
+ '''
47
+ n_rows = len(begin_series)
48
+ result = np.zeros((n_rows, 168))
49
+
50
+ # Handle missing data vectorized
51
+ begin_valid = begin_series.notna()
52
+ end_valid = end_series.notna()
53
+
54
+ # Where we have begin but no end, use begin as end
55
+ end_filled = end_series.where(end_valid, begin_series)
56
+
57
+ # Only process rows with valid begin times
58
+ valid_mask = begin_valid
59
+ if not valid_mask.any():
60
+ return result
61
+
62
+ # Work with valid data only
63
+ begin_valid_data = begin_series[valid_mask]
64
+ end_valid_data = end_filled[valid_mask]
65
+ valid_indices = np.where(valid_mask)[0]
66
+
67
+ # Handle swapped times
68
+ swapped = begin_valid_data > end_valid_data
69
+ begin_corrected = np.where(swapped, end_valid_data, begin_valid_data)
70
+ end_corrected = np.where(swapped, begin_valid_data, end_valid_data)
71
+
72
+ # Floor to hour
73
+ begin_floor = pd.Series(begin_corrected).dt.floor('h')
74
+ end_floor = pd.Series(end_corrected).dt.floor('h')
75
+
76
+ # Calculate duration in minutes
77
+ duration_minutes = (end_corrected - begin_corrected) / pd.Timedelta(minutes=1)
78
+
79
+ # For very long durations (>1 week), use equal distribution
80
+ long_duration_mask = duration_minutes > 10080
81
+ if long_duration_mask.any():
82
+ long_indices = valid_indices[long_duration_mask]
83
+ result[long_indices] = WEEK_EQUAL
84
+
85
+ # Process normal durations
86
+ normal_mask = ~long_duration_mask
87
+ if normal_mask.any():
88
+ normal_indices = valid_indices[normal_mask]
89
+
90
+ begin_normal = begin_corrected[normal_mask]
91
+ end_normal = end_corrected[normal_mask]
92
+ begin_floor_normal = begin_floor[normal_mask]
93
+ end_floor_normal = end_floor[normal_mask]
94
+ duration_normal = duration_minutes[normal_mask]
95
+
96
+ # Vectorized processing for each case
97
+ for i, (idx, b_orig, e_orig, b_floor, e_floor, dur_min) in enumerate(
98
+ zip(normal_indices, begin_normal, end_normal, begin_floor_normal, end_floor_normal, duration_normal)
99
+ ):
100
+ if pd.isna(b_orig) or pd.isna(e_orig):
101
+ continue
102
+
103
+ # Single hour case
104
+ if b_floor == e_floor:
105
+ wd = b_floor.weekday()
106
+ hr = b_floor.hour
107
+ result[idx, WEEKDAY_HOUR_INDEX[(wd, hr)]] = 1.0
108
+ continue
109
+
110
+ # Multi-hour case - calculate proportions
111
+ total_minutes = (e_orig - b_orig) / pd.Timedelta(minutes=1)
112
+ if total_minutes <= 0:
113
+ continue
114
+
115
+ # Generate hour range
116
+ hour_range = pd.date_range(b_floor, e_floor, freq='1h')
117
+
118
+ # Calculate weights for each hour
119
+ weights = []
120
+ for j, hour_dt in enumerate(hour_range):
121
+ if j == 0: # First hour
122
+ minutes_in_hour = 60 - (b_orig - b_floor) / pd.Timedelta(minutes=1)
123
+ elif j == len(hour_range) - 1: # Last hour
124
+ minutes_in_hour = (e_orig - e_floor) / pd.Timedelta(minutes=1)
125
+ else: # Middle hours
126
+ minutes_in_hour = 60
127
+
128
+ weight = minutes_in_hour / total_minutes
129
+ weights.append((hour_dt.weekday(), hour_dt.hour, weight))
130
+
131
+ # Aggregate weights by weekday-hour
132
+ for wd, hr, weight in weights:
133
+ if (wd, hr) in WEEKDAY_HOUR_INDEX:
134
+ result[idx, WEEKDAY_HOUR_INDEX[(wd, hr)]] += weight
135
+
136
+ return result
137
+
138
+ def agg_weekhour(data, begin, end, group=None):
139
+ '''
140
+ Optimized version of agg_weekhour using vectorized operations
141
+
142
+ Parameters:
143
+ -----------
144
+ data : pd.DataFrame
145
+ begin : str, column name for begin datetime
146
+ end : str, column name for end datetime
147
+ group : str or list, optional grouping column(s)
148
+
149
+ Returns:
150
+ --------
151
+ pd.DataFrame with aggregated weights by weekday-hour
152
+ '''
153
+ # Get weight matrix (n_rows x 168)
154
+ weights_matrix = weekhour_func_vectorized(data[begin], data[end])
155
+
156
+ if group is None:
157
+ # Simple case: sum across all rows
158
+ total_weights = weights_matrix.sum(axis=0)
159
+ result_df = WEEK_HOUR_DF.copy()
160
+ result_df['weight'] = total_weights
161
+ return result_df
162
+
163
+ # Group case: need to aggregate by group
164
+ if isinstance(group, str):
165
+ group = [group]
166
+
167
+ # Create DataFrame with weights and group columns
168
+ group_data = data[group].copy()
169
+
170
+ # Handle case where we have grouping variables
171
+ unique_groups = group_data.drop_duplicates().reset_index(drop=True)
172
+ results = []
173
+
174
+ for _, group_vals in unique_groups.iterrows():
175
+ # Find matching rows
176
+ mask = np.ones(len(data), dtype=bool)
177
+ for col in group:
178
+ mask &= (data[col] == group_vals[col])
179
+
180
+ if mask.any():
181
+ # Sum weights for this group
182
+ group_weights = weights_matrix[mask].sum(axis=0)
183
+
184
+ # Create result DataFrame for this group
185
+ group_df = WEEK_HOUR_DF.copy()
186
+ for col in group:
187
+ group_df[col] = group_vals[col]
188
+ group_df['weight'] = group_weights
189
+
190
+ results.append(group_df)
191
+
192
+ if results:
193
+ final_result = pd.concat(results, ignore_index=True)
194
+ final_result = final_result[group + ['weekday', 'hour', 'weight']]
195
+ return final_result.sort_values(group + ['weekday', 'hour']).reset_index(drop=True)
196
+ else:
197
+ # No valid groups, return empty result with correct structure
198
+ result_df = WEEK_HOUR_DF.copy()
199
+ for col in group:
200
+ result_df[col] = None
201
+ result_df['weight'] = 0.0
202
+ return result_df[group + ['weekday', 'hour', 'weight']]
203
+
204
+ # Alternative even faster version for simple cases
205
+ def weekhour_func_simple(begin_series, end_series):
206
+ '''
207
+ Simplified version for when you don't need all the edge case handling
208
+ Much faster for clean data
209
+ '''
210
+ n_rows = len(begin_series)
211
+ result = np.zeros((n_rows, 168))
212
+
213
+ # Basic validation
214
+ valid = begin_series.notna() & end_series.notna()
215
+ if not valid.any():
216
+ return result
217
+
218
+ begin_clean = begin_series[valid]
219
+ end_clean = end_series[valid]
220
+ valid_idx = np.where(valid)[0]
221
+
222
+ # Vectorized weekday/hour extraction
223
+ begin_weekday = begin_clean.dt.weekday.values
224
+ begin_hour = begin_clean.dt.hour.values
225
+ end_weekday = end_clean.dt.weekday.values
226
+ end_hour = end_clean.dt.hour.values
227
+
228
+ # Calculate duration in hours
229
+ duration_hours = (end_clean - begin_clean).dt.total_seconds() / 3600
230
+
231
+ # Simple case: same weekday and hour
232
+ same_hour_mask = (begin_weekday == end_weekday) & (begin_hour == end_hour)
233
+ if same_hour_mask.any():
234
+ same_hour_indices = valid_idx[same_hour_mask]
235
+ for i, (idx, wd, hr) in enumerate(zip(same_hour_indices,
236
+ begin_weekday[same_hour_mask],
237
+ begin_hour[same_hour_mask])):
238
+ result[idx, wd * 24 + hr] = 1.0
239
+
240
+ # Multi-hour cases would need more complex logic...
241
+ # For now, fall back to original method for complex cases
242
+ complex_mask = ~same_hour_mask
243
+ if complex_mask.any():
244
+ # Use original logic for complex cases
245
+ complex_indices = valid_idx[complex_mask]
246
+ for i in complex_indices:
247
+ # Fall back to single-row processing for complex cases
248
+ row_result = weekhour_func_single_row(begin_series.iloc[i], end_series.iloc[i])
249
+ if row_result is not None:
250
+ result[i] = row_result
251
+
252
+ return result
253
+
254
+ def weekhour_func_single_row(begin_val, end_val):
255
+ '''Helper function for single row processing'''
256
+ if pd.isna(begin_val):
257
+ return None
258
+
259
+ if pd.isna(end_val):
260
+ end_val = begin_val
261
+
262
+ if begin_val > end_val:
263
+ begin_val, end_val = end_val, begin_val
264
+
265
+ beg = begin_val.floor('h')
266
+ end = end_val.floor('h')
267
+
268
+ tot_min = (end_val - begin_val).total_seconds() / 60
269
+ if tot_min > 10080:
270
+ return WEEK_EQUAL.copy()
271
+
272
+ rv = pd.date_range(beg, end, freq='1h')
273
+ if len(rv) == 1:
274
+ wd, hr = rv[0].weekday(), rv[0].hour()
275
+ result = np.zeros(168)
276
+ result[WEEKDAY_HOUR_INDEX[(wd, hr)]] = 1.0
277
+ return result
278
+
279
+ # Calculate proportional weights
280
+ beg_sub = (3600 - (begin_val - beg).total_seconds()) / 60
281
+ end_sub = (end_val - end).total_seconds() / 60
282
+ mid_sub = tot_min - beg_sub - end_sub
283
+
284
+ weights = [beg_sub] + [mid_sub] * (len(rv) - 2) + [end_sub]
285
+ weights = np.array(weights)
286
+ weights = weights / weights.sum()
287
+
288
+ result = np.zeros(168)
289
+ for i, (dt, weight) in enumerate(zip(rv, weights)):
290
+ wd, hr = dt.weekday(), dt.hour()
291
+ result[WEEKDAY_HOUR_INDEX[(wd, hr)]] += weight
292
+
293
+ return result
294
+
295
+
296
+ def agg_hour(data,begin,end,group=None):
297
+ """
298
+ Aggregate aoristic weights by hour only (ignoring weekday).
299
+
300
+ data : pandas.DataFrame
301
+ DataFrame with datetime columns
302
+ begin : str
303
+ Column name for begin datetime
304
+ end : str
305
+ Column name for end datetime
306
+ group : str or list, optional
307
+ Grouping column(s)
308
+
309
+ Returns
310
+ -------
311
+ pandas.DataFrame
312
+ DataFrame with hour and aggregated weights
313
+ """
314
+ res_week = agg_weekhour(data,begin,end,group)
315
+ if group:
316
+ res_day = res_week.groupby([group] + ['hour'],as_index=False)['weight'].sum()
317
+ else:
318
+ res_day = res_week.groupby('hour',as_index=False)['weight'].sum()
319
+ return res_day
320
+
321
+
322
+ def plt_super(data,
323
+ color=week_color,
324
+ ax=None,
325
+ figsize=(8,4),
326
+ show=True,
327
+ legend=True,
328
+ leg_kwargs={'bbox_to_anchor':(1.0,1.0)},
329
+ save=None,
330
+ save_kwarges={'dpi':500,'bbox_inches':'tight'},
331
+ title=None):
332
+ """
333
+ Plot aoristic weights by hour with separate lines for each weekday.
334
+
335
+ data : pandas.DataFrame
336
+ DataFrame with 'weekday', 'hour', and 'weight' columns
337
+ color : list, default week_color
338
+ List of colors for each weekday line
339
+ ax : matplotlib.axes.Axes, optional
340
+ Axes to plot on. If None, creates new figure.
341
+ figsize : tuple, default (8, 4)
342
+ Figure size if creating new figure
343
+ show : bool, default True
344
+ Whether to display the plot
345
+ legend : bool, default True
346
+ Whether to show legend
347
+ leg_kwargs : dict
348
+ Keyword arguments for legend
349
+ save : str, optional
350
+ File path to save the figure
351
+ save_kwarges : dict
352
+ Keyword arguments for savefig
353
+ title : str, optional
354
+ Plot title
355
+ """
356
+ if ax is None:
357
+ fig, ax = plt.subplots(figsize=figsize)
358
+ for w in range(7):
359
+ sub = data[data['weekday'] == w].copy()
360
+ ax.plot(sub['hour'],sub['weight'],c=week_color[w],label=wd_di[w])
361
+ ax.set_xticks(list(range(0,24,2)))
362
+ if title:
363
+ ax.set_title(title)
364
+ if legend:
365
+ ax.legend(**leg_kwargs)
366
+ if save:
367
+ fig.savefig(save,**save_kwargs)
368
+ if show:
369
+ fig.show()
370
+
371
+
372
+ def plt_basic(data,
373
+ color='k',
374
+ ax=None,
375
+ figsize=(8,4),
376
+ show=True,
377
+ label=None,
378
+ save=None,
379
+ save_kwargs={'dpi':500,'bbox_inches':'tight'},
380
+ title=None):
381
+ """
382
+ Plot aoristic weights by hour as a simple line chart.
383
+
384
+ data : pandas.DataFrame
385
+ DataFrame with 'hour' and 'weight' columns
386
+ color : str, default 'k'
387
+ Line color
388
+ ax : matplotlib.axes.Axes, optional
389
+ Axes to plot on. If None, creates new figure.
390
+ figsize : tuple, default (8, 4)
391
+ Figure size if creating new figure
392
+ show : bool, default True
393
+ Whether to display the plot
394
+ label : str, optional
395
+ Label for legend
396
+ save : str, optional
397
+ File path to save the figure
398
+ save_kwargs : dict
399
+ Keyword arguments for savefig
400
+ title : str, optional
401
+ Plot title
402
+ """
403
+ if ax is None:
404
+ fig, ax = plt.subplots(figsize=figsize)
405
+ ax.plot(data['hour'],data['weight'],marker='o',c=color,markeredgecolor='white',label=label)
406
+ ax.set_xticks(list(range(0,24,2)))
407
+ if title:
408
+ ax.set_title(title)
409
+ if save:
410
+ fig.savefig(save,**save_kwargs)
411
+ if show:
412
+ fig.show()