imsciences 0.9.4__py3-none-any.whl → 0.9.5.1__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.
Potentially problematic release.
This version of imsciences might be problematic. Click here for more details.
- imsciences/geo.py +81 -1
- {imsciences-0.9.4.dist-info → imsciences-0.9.5.1.dist-info}/METADATA +6 -1
- {imsciences-0.9.4.dist-info → imsciences-0.9.5.1.dist-info}/RECORD +7 -7
- {imsciences-0.9.4.dist-info → imsciences-0.9.5.1.dist-info}/LICENSE.txt +0 -0
- {imsciences-0.9.4.dist-info → imsciences-0.9.5.1.dist-info}/PKG-INFO-IMS-24Ltp-3 +0 -0
- {imsciences-0.9.4.dist-info → imsciences-0.9.5.1.dist-info}/WHEEL +0 -0
- {imsciences-0.9.4.dist-info → imsciences-0.9.5.1.dist-info}/top_level.txt +0 -0
imsciences/geo.py
CHANGED
|
@@ -30,6 +30,11 @@ class geoprocessing:
|
|
|
30
30
|
print(" - Usage: process_itv_analysis(self, raw_df, itv_path, cities_path, media_spend_path, output_path, group1, group2)")
|
|
31
31
|
print(" - Example:process_itv_analysis(df,'itv regional mapping.csv', 'Geo_Mappings_with_Coordinates.xlsx', 'IMS.xlsx', 'itv_for_test_analysis_itvx.csv', ['West', 'Westcountry', 'Tyne Tees'], ['Central Scotland', 'North Scotland'])")
|
|
32
32
|
|
|
33
|
+
print("\n3. process_city_analysis")
|
|
34
|
+
print(" - Description: Processes city-level data for geo experiments by grouping user metrics, merging with media spend data, and saving the result.")
|
|
35
|
+
print(" - Usage: process_city_analysis(raw_df, spend_df, output_path, group1, group2, response_column)")
|
|
36
|
+
print(" - Example:process_city_analysis(df, spend, output, ['Barnsley'], ['Aberdeen'], 'newUsers')")
|
|
37
|
+
|
|
33
38
|
def pull_ga(self, credentials_file, property_id, start_date, country, metrics):
|
|
34
39
|
"""
|
|
35
40
|
Pulls Google Analytics data using the BetaAnalyticsDataClient.
|
|
@@ -192,4 +197,79 @@ class geoprocessing:
|
|
|
192
197
|
# Save the final output
|
|
193
198
|
analysis_df.to_csv(output_path, index=False)
|
|
194
199
|
|
|
195
|
-
return analysis_df
|
|
200
|
+
return analysis_df
|
|
201
|
+
|
|
202
|
+
def process_city_analysis(self, raw_input_path, spend_input_path, output_path, group1, group2, response_column):
|
|
203
|
+
"""
|
|
204
|
+
Process city analysis by grouping data, analyzing user metrics, and merging with spend data.
|
|
205
|
+
|
|
206
|
+
Parameters:
|
|
207
|
+
raw_input_path (str): Path to the raw input data file (CSV or XLSX) containing at least 'date', 'city', and the specified response column.
|
|
208
|
+
spend_input_path (str): Path to the media spend data file (CSV or XLSX) with 'date', 'geo', and 'cost' columns. Costs should be numeric.
|
|
209
|
+
output_path (str): Path to save the final output file (CSV or XLSX).
|
|
210
|
+
group1 (list): List of city regions for group 1.
|
|
211
|
+
group2 (list): List of city regions for group 2.
|
|
212
|
+
response_column (str): Column name to be used as the response metric.
|
|
213
|
+
|
|
214
|
+
Returns:
|
|
215
|
+
pd.DataFrame: Processed DataFrame.
|
|
216
|
+
"""
|
|
217
|
+
import pandas as pd
|
|
218
|
+
import os
|
|
219
|
+
|
|
220
|
+
def read_file(file_path):
|
|
221
|
+
"""Helper function to read CSV or XLSX files."""
|
|
222
|
+
ext = os.path.splitext(file_path)[1].lower()
|
|
223
|
+
if ext == '.csv':
|
|
224
|
+
return pd.read_csv(file_path)
|
|
225
|
+
elif ext in ['.xlsx', '.xls']:
|
|
226
|
+
return pd.read_excel(file_path)
|
|
227
|
+
else:
|
|
228
|
+
raise ValueError("Unsupported file type. Please use a CSV or XLSX file.")
|
|
229
|
+
|
|
230
|
+
def write_file(df, file_path):
|
|
231
|
+
"""Helper function to write DataFrame to CSV or XLSX files."""
|
|
232
|
+
ext = os.path.splitext(file_path)[1].lower()
|
|
233
|
+
if ext == '.csv':
|
|
234
|
+
df.to_csv(file_path, index=False)
|
|
235
|
+
elif ext in ['.xlsx', '.xls']:
|
|
236
|
+
df.to_excel(file_path, index=False, engine='openpyxl')
|
|
237
|
+
else:
|
|
238
|
+
raise ValueError("Unsupported file type. Please use a CSV or XLSX file.")
|
|
239
|
+
|
|
240
|
+
# Read input files
|
|
241
|
+
raw_df = read_file(raw_input_path)
|
|
242
|
+
spend_df = read_file(spend_input_path)
|
|
243
|
+
|
|
244
|
+
# Ensure necessary columns are present
|
|
245
|
+
required_columns = {'date', 'city', response_column}
|
|
246
|
+
if not required_columns.issubset(raw_df.columns):
|
|
247
|
+
raise ValueError(f"Input DataFrame must contain the following columns: {required_columns}")
|
|
248
|
+
|
|
249
|
+
spend_required_columns = {'date', 'geo', 'cost'}
|
|
250
|
+
if not spend_required_columns.issubset(spend_df.columns):
|
|
251
|
+
raise ValueError(f"Spend DataFrame must contain the following columns: {spend_required_columns}")
|
|
252
|
+
|
|
253
|
+
# Convert cost column to numeric after stripping currency symbols and commas
|
|
254
|
+
spend_df['cost'] = spend_df['cost'].replace('[^\d.]', '', regex=True).astype(float)
|
|
255
|
+
|
|
256
|
+
# Rename and process input DataFrame
|
|
257
|
+
raw_df = raw_df.rename(columns={'city': 'geo', response_column: 'response'})
|
|
258
|
+
|
|
259
|
+
# Filter and group data
|
|
260
|
+
filtered_df = raw_df[raw_df['geo'].isin(group1 + group2)].copy()
|
|
261
|
+
|
|
262
|
+
grouped_df = filtered_df.groupby(['date', 'geo'], as_index=False).agg({'response': 'sum'})
|
|
263
|
+
|
|
264
|
+
assignment_map = {city: 1 for city in group1}
|
|
265
|
+
assignment_map.update({city: 2 for city in group2})
|
|
266
|
+
grouped_df['assignment'] = grouped_df['geo'].map(assignment_map)
|
|
267
|
+
|
|
268
|
+
# Merge with spend data
|
|
269
|
+
merged_df = pd.merge(grouped_df, spend_df, on=['date', 'geo'], how='left')
|
|
270
|
+
merged_df['cost'] = merged_df['cost'].fillna(0)
|
|
271
|
+
|
|
272
|
+
# Save the final output
|
|
273
|
+
write_file(merged_df, output_path)
|
|
274
|
+
|
|
275
|
+
return merged_df
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.1
|
|
2
2
|
Name: imsciences
|
|
3
|
-
Version: 0.9.
|
|
3
|
+
Version: 0.9.5.1
|
|
4
4
|
Summary: IMS Data Processing Package
|
|
5
5
|
Author: IMS
|
|
6
6
|
Author-email: cam@im-sciences.com
|
|
@@ -236,6 +236,11 @@ Table of Contents
|
|
|
236
236
|
- **Usage**: `process_itv_analysis(self, raw_df, itv_path, cities_path, media_spend_path, output_path, group1, group2)`
|
|
237
237
|
- **Example**: `process_itv_analysis(df, 'itv regional mapping.csv', 'Geo_Mappings_with_Coordinates.xlsx', 'IMS.xlsx', 'itv_for_test_analysis_itvx.csv', ['West', 'Westcountry', 'Tyne Tees'], ['Central Scotland', 'North Scotland'])`
|
|
238
238
|
|
|
239
|
+
## 3. `process_city_analysis`
|
|
240
|
+
- **Description**: Processes city-level data for geo experiments by grouping user metrics, merging with media spend data, and saving the result.
|
|
241
|
+
- **Usage**: `process_city_analysis(raw_df, spend_df, output_path, group1, group2, response_column)`
|
|
242
|
+
- **Example**: `process_city_analysis(df, spend, output, ['Barnsley'], ['Aberdeen'], 'newUsers')`
|
|
243
|
+
|
|
239
244
|
---
|
|
240
245
|
|
|
241
246
|
## Data Visualisations
|
|
@@ -5,7 +5,7 @@ imsciences/__init__.py,sha256=_HuYeLbDMTdt7GpKI4r6-d7yRPZgcAQ7yOW0-ydR2Yo,117
|
|
|
5
5
|
imsciences/datafunctions-IMS-24Ltp-3.py,sha256=3Snv-0iE_03StmyjtT-riOU9f4v8TaJWLoyZLJp6l8Y,141406
|
|
6
6
|
imsciences/datafunctions.py,sha256=WZrXNLO-SYrCuFt0pAbha74psMOZPY7meWJ7yWEbRpk,169953
|
|
7
7
|
imsciences/datapull.py,sha256=TPY0LDgOkcKTBk8OekbD0Grg5x0SomAK2dZ7MuT6X1E,19000
|
|
8
|
-
imsciences/geo.py,sha256=
|
|
8
|
+
imsciences/geo.py,sha256=kBFc4_DEyQDQlnGnCc09qcydoaW5Lium6kTSdz3li3c,13526
|
|
9
9
|
imsciences/mmm.py,sha256=M91Qs_ijtY4EytTo1rruWCOVGnV7DMVaYUhNpP1NVNc,73920
|
|
10
10
|
imsciences/pull.py,sha256=bGz8B7bBQ5b9hrx3ipCFTWl_eebEb7rPL4dANKiVWTY,74015
|
|
11
11
|
imsciences/unittesting.py,sha256=DYGqVCsZHrs_tZ-EXDW8q8CdlcsTnG8HsnmWjEE521c,45691
|
|
@@ -14,9 +14,9 @@ imsciencesdataprocessing/__init__.py,sha256=quSwsLs6IuLoA5Rzi0ZD40xZaQudwDteF7_a
|
|
|
14
14
|
imsciencesdataprocessing/datafunctions.py,sha256=vE1vsZ8xOSbR9Bwlp9SWXwEHXQ0nFydwGkvzHXf2f1Y,41
|
|
15
15
|
imsdataprocessing/__init__.py,sha256=quSwsLs6IuLoA5Rzi0ZD40xZaQudwDteF7_ai9JfTPk,32
|
|
16
16
|
imsdataprocessing/datafunctions.py,sha256=vE1vsZ8xOSbR9Bwlp9SWXwEHXQ0nFydwGkvzHXf2f1Y,41
|
|
17
|
-
imsciences-0.9.
|
|
18
|
-
imsciences-0.9.
|
|
19
|
-
imsciences-0.9.
|
|
20
|
-
imsciences-0.9.
|
|
21
|
-
imsciences-0.9.
|
|
22
|
-
imsciences-0.9.
|
|
17
|
+
imsciences-0.9.5.1.dist-info/LICENSE.txt,sha256=lVq2QwcExPX4Kl2DHeEkRrikuItcDB1Pr7yF7FQ8_z8,1108
|
|
18
|
+
imsciences-0.9.5.1.dist-info/METADATA,sha256=aTHbZ8Hr_JNbE_UQwbP5CSJtPM-BHNmHnRUn9wlbQXE,16994
|
|
19
|
+
imsciences-0.9.5.1.dist-info/PKG-INFO-IMS-24Ltp-3,sha256=yqZbigwHjnYoqyI81PGz_AeofRFfOrwH_Vyawyef-mg,854
|
|
20
|
+
imsciences-0.9.5.1.dist-info/WHEEL,sha256=ixB2d4u7mugx_bCBycvM9OzZ5yD7NmPXFRtKlORZS2Y,91
|
|
21
|
+
imsciences-0.9.5.1.dist-info/top_level.txt,sha256=hsENS-AlDVRh8tQJ6-426iUQlla9bPcGc0-UlFF0_iU,11
|
|
22
|
+
imsciences-0.9.5.1.dist-info/RECORD,,
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|