Datasets:
Upload 10 files (#1)
Browse files- Upload 10 files (58e4e812f57cbe57b3d3ded45c9001af6114566e)
Co-authored-by: Tom Freeman <TomFreeman3@users.noreply.huggingface.co>
Files changed (11)
- .gitattributes+9
- np_extract_part_1.csvAdded+3
- np_extract_part_2.csvAdded+3
- np_extract_part_3.csvAdded+3
- np_extract_part_4.csvAdded+3
- np_extract_part_5.csvAdded+3
- np_extract_part_6.csvAdded+3
- np_extract_part_7.csvAdded+3
- np_extract_part_8.csvAdded+3
- np_extract_part_9.csvAdded+3
- obfs_np.pyAdded+248
56 unmodified lines57585956 unmodified lines# Video files - compressed*.mp4 filter=lfs diff=lfs merge=lfs -text*.webm filter=lfs diff=lfs merge=lfs -text56 unmodified lines57585960616263646566676856 unmodified lines# Video files - compressed*.mp4 filter=lfs diff=lfs merge=lfs -text*.webm filter=lfs diff=lfs merge=lfs -textnp_extract_part_1.csv filter=lfs diff=lfs merge=lfs -textnp_extract_part_2.csv filter=lfs diff=lfs merge=lfs -textnp_extract_part_3.csv filter=lfs diff=lfs merge=lfs -textnp_extract_part_4.csv filter=lfs diff=lfs merge=lfs -textnp_extract_part_5.csv filter=lfs diff=lfs merge=lfs -textnp_extract_part_6.csv filter=lfs diff=lfs merge=lfs -textnp_extract_part_7.csv filter=lfs diff=lfs merge=lfs -textnp_extract_part_8.csv filter=lfs diff=lfs merge=lfs -textnp_extract_part_9.csv filter=lfs diff=lfs merge=lfs -text
123version https://git-lfs.github.com/spec/v1oid sha256:8641a28bc92616fff4c43f26328176c0729be2fa14fd9b35b5c9dd3f81f348bfsize 522828567123version https://git-lfs.github.com/spec/v1oid sha256:e540e7761621dc8daf57ecb1fc1cff66fa379f3ce8c4bf024fab60d021d5f840size 515660719123version https://git-lfs.github.com/spec/v1oid sha256:f0a7fc7e45b5f825560149b561bf4297c3b2fb15c63936d6043a6ca08438408fsize 512904154123version https://git-lfs.github.com/spec/v1oid sha256:9562da5857e85317e04df9240f76f6318675e9f0e52cdfa70c2ec3eb31d0f02bsize 481844515123version https://git-lfs.github.com/spec/v1oid sha256:3da3eab2b40c8288fb404397cfd33d5144b82adec45db29035f7818fba41d1bfsize 476040385123version https://git-lfs.github.com/spec/v1oid sha256:ddc420ff844d1f6a92f27dc77fa482cd103af678f70626359095195af78d4f22size 512294051123version https://git-lfs.github.com/spec/v1oid sha256:7ba56e238855a8cb4a734d83f4c43ed659220a1b426451ebd838fe2277118fe8size 502468054123version https://git-lfs.github.com/spec/v1oid sha256:14cd711447a98e5dc5235c8b14022933838137fdeda05fe3f078f0b47cb76ca6size 526161322123version https://git-lfs.github.com/spec/v1oid sha256:1f9cc9627a0094a33c30aa18428a3f8aec15169e8421c2245a0556028f6eeba0size 364490746123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248import gcimport loggingimport timefrom pathlib import Path
import numpy as npimport pandas as pdfrom gomez_cloud.utils.date_utils import iterate_days
logging.basicConfig( level=logging.INFO, format="%(asctime)s | %(levelname)s | %(name)s | %(message)s",)log = logging.getLogger(__name__)
def _haversine_m(lat1, lon1, lat2, lon2): """Vectorised haversine distance in meters.""" R = 6371000.0 phi1 = np.radians(lat1); phi2 = np.radians(lat2) dphi = np.radians(lat2 - lat1); dl = np.radians(lon2 - lon1) a = np.sin(dphi/2)**2 + np.cos(phi1)*np.cos(phi2)*np.sin(dl/2)**2 return 2*R*np.arcsin(np.sqrt(a))
def _meters_to_deg(lat_deg, dx_m, dy_m): """Convert local meter offsets (east=dx, north=dy) to lon/lat degrees at given latitude.""" lat_rad = np.radians(lat_deg) m_per_deg_lat = 111_320.0 m_per_deg_lon = 111_320.0 * np.cos(lat_rad) dlat = dy_m / m_per_deg_lat dlon = dx_m / m_per_deg_lon return dlat, dlon
def jitter_points(df, lat_col, lon_col, radius_m=50, seed=42, shuffle=True): """ Add uniform random jitter within a circle of radius_m meters. Returns a new DataFrame with columns <lat_col>_jit, <lon_col>_jit. """ rng = np.random.default_rng(seed) n = len(df) # uniform in disk: r = R*sqrt(u), theta ~ U[0,2pi) u = rng.random(n) r = radius_m * np.sqrt(u) theta = rng.random(n) * 2*np.pi dx = r * np.cos(theta) dy = r * np.sin(theta)
lat = df[lat_col].to_numpy(dtype=float, copy=False) dlat, dlon = _meters_to_deg(lat, dx, dy)
out = df.copy() out[f"{lat_col}_jit"] = lat + dlat out[f"{lon_col}_jit"] = df[lon_col].to_numpy(dtype=float, copy=False) + dlon
if shuffle: out = out.sample(frac=1.0, random_state=seed).reset_index(drop=True) return out
def jitter_signal(series, sigma_db=2.0, seed=42, clip=(-120, -20)): """ Add small Gaussian noise (dBm) to signal, robust to strings like '-083'. """ rng = np.random.default_rng(seed) sig = pd.to_numeric(series, errors="coerce") # "-083" -> -83 noise = rng.normal(0.0, sigma_db, size=len(sig)) out = (sig + noise).clip(clip[0], clip[1]) return out
# ----- difference metrics -----def displacement_stats(orig_lat, orig_lon, jit_lat, jit_lon): d = _haversine_m(orig_lat, orig_lon, jit_lat, jit_lon) return { "n": d.size, "mean_m": float(np.nanmean(d)), "p50_m": float(np.nanpercentile(d, 50)), "p90_m": float(np.nanpercentile(d, 90)), "p95_m": float(np.nanpercentile(d, 95)), "max_m": float(np.nanmax(d)), }
def js_distance_2d(orig_lat, orig_lon, jit_lat, jit_lon, bins=100, eps=1e-12): """ Jensen–Shannon distance between 2D (lat,lon) distributions via hist2d. Range: 0 identical … 1 very different (we return sqrt(JS divergence)). """ lat_all = np.concatenate([orig_lat, jit_lat]) lon_all = np.concatenate([orig_lon, jit_lon]) lat_edges = np.linspace(lat_all.min(), lat_all.max(), bins+1) lon_edges = np.linspace(lon_all.min(), lon_all.max(), bins+1)
H1, _, _ = np.histogram2d(orig_lat, orig_lon, bins=[lat_edges, lon_edges]) H2, _, _ = np.histogram2d(jit_lat, jit_lon, bins=[lat_edges, lon_edges])
P = (H1.ravel() + eps); P /= P.sum() Q = (H2.ravel() + eps); Q /= Q.sum() M = 0.5*(P+Q)
def kl(p, q): # both already have eps return np.sum(p * np.log(p/q)) js_div = 0.5*kl(P, M) + 0.5*kl(Q, M) return float(np.sqrt(js_div))
def ks_1d_marginals(orig, jit): """Kolmogorov–Smirnov D for 1D arrays (simple numpy implementation).""" x = np.sort(np.asarray(orig)) y = np.sort(np.asarray(jit)) # empirical CDFs on merged support grid = np.sort(np.unique(np.concatenate([x, y]))) Fx = np.searchsorted(x, grid, side='right') / x.size Fy = np.searchsorted(y, grid, side='right') / y.size return float(np.max(np.abs(Fx - Fy)))
def main() -> None: start_all = time.time()
partitions = iterate_days(first="2025-03-01", last="2025-06-30")
output_dir = Path(...) output_dir.mkdir(parents=True, exist_ok=True)
base_uri = ... storage_opts = {"token": "cloud"} # ADC via gcsfs
week_buffers = [] week_idx = 1 day_idx = 0 total_rows_written = 0 total_days_processed = 0
for day in partitions: t0 = time.time() uri = f"{base_uri}/day={day}"
try: df = pd.read_parquet(uri, storage_options=storage_opts) except FileNotFoundError: log.warning("Partition not found (skipping): %s", uri) continue except Exception as e: log.exception("Failed to read partition %s: %s", uri, e) continue
log.info("Loaded %s rows x %s cols from %s", len(df), len(df.columns), uri)
try: # Jitter location pings_jit = jitter_points( df, lat_col="latitude", lon_col="longitude", radius_m=20, seed=456, shuffle=True, ) # Jitter signal pings_jit["signal_level_jit"] = jitter_signal( df["signal_level"], sigma_db=2.0, seed=456 )
# Replace originals with jittered pings_jit = ( pings_jit.drop(columns=["latitude", "longitude", "signal_level"]) .rename( columns={ "latitude_jit": "latitude", "longitude_jit": "longitude", "signal_level_jit": "signal_level", } ) )
# Keep only 'Full Service Loss (>120s)', else set to None if "measurement_type_name" in pings_jit.columns: pings_jit["measurement_type_name"] = pings_jit["measurement_type_name"].apply( lambda x: x if x == "Full Service Loss (>120s)" else None ) else: log.warning("Column 'measurement_type_name' missing in partition %s", day)
log.info( "Transformed day=%s → %s rows", day, len(pings_jit) )
week_buffers.append(pings_jit) total_days_processed += 1 day_idx += 1
except Exception as e: log.exception("Transform failed for day=%s: %s", day, e) # Drop heavy refs before moving on del df gc.collect() continue finally: # free the original df ASAP del df gc.collect()
# Flush every 14 days if day_idx % 14 == 0: try: week_df = pd.concat(week_buffers, ignore_index=True) out_path = output_dir / f"np_extract_part_{week_idx}.csv" week_df.to_csv(out_path, index=False) total_rows_written += len(week_df) log.info( "Wrote week %d: %s rows to %s (elapsed %.2fs)", week_idx, len(week_df), out_path, time.time() - t0 ) finally: week_buffers.clear() week_idx += 1 # encourage memory to return del week_df gc.collect()
log.info("Processed day=%s in %.2fs", day, time.time() - t0)
# Final partial week flush if week_buffers: week_df = pd.concat(week_buffers, ignore_index=True)
week_df.to_csv(f"/home/tom_freeman_vodafone_com/tom-foolery/data/np_extractions/part_{week_idx}.csv", index=False)
total_rows_written += len(week_df) log.info( "Wrote FINAL part %d: %s rows to %s", week_idx, len(week_df), out_path ) week_buffers.clear() del week_df gc.collect()
log.info( "Done. Days processed: %d | Rows written: %d | Total time: %.2fs", total_days_processed, total_rows_written, time.time() - start_all )
if __name__ == "__main__": main().gitattributes+9
np_extract_part_1.csvAdded+3
np_extract_part_2.csvAdded+3
np_extract_part_3.csvAdded+3
np_extract_part_4.csvAdded+3
np_extract_part_5.csvAdded+3
np_extract_part_6.csvAdded+3
np_extract_part_7.csvAdded+3
np_extract_part_8.csvAdded+3
np_extract_part_9.csvAdded+3
obfs_np.pyAdded+248