"""
Street View Imagery for Cities: hands-on lab, Part A (no images needed).
Uses the Singapore subset of NUS Global Streetscapes (CC BY-SA 4.0): 22,579 images with
labels already computed by ZenSVI-style models (segmentation, perception, scene, quality).
Needs: pandas, numpy, scikit-learn. Lab 6 also needs sg_places.csv and sg_reviews.csv
from the reviews guide (class sample).  Run:  python svi_lab.py
"""
import re
import numpy as np
import pandas as pd
from sklearn.linear_model import LinearRegression
from sklearn.ensemble import RandomForestRegressor
from sklearn.model_selection import cross_val_score, GroupKFold

svi = pd.read_csv('sg_svi.csv')
SEG = ['vegetation', 'terrain', 'sky', 'building', 'road', 'sidewalk', 'car', 'person', 'wall', 'fence', 'pole', 'bus', 'truck', 'motorcycle', 'bicycle', 'water']
PER = ['beautiful', 'lively', 'safe', 'wealthy', 'boring', 'depressing']

def km(lat1, lon1, lat2, lon2):
    """Small-distance approximation, good enough inside one city."""
    return np.hypot((lat1 - lat2) * 111.0, (lon1 - lon2) * 111.0 * np.cos(np.radians(1.35)))

def grid_groups(d, size=0.02):
    """Spatial blocks of about 2 km, so test images are not next door to training images."""
    return ((d.lat / size).astype(int).astype(str) + '_' + (d.lon / size).astype(int).astype(str))

if __name__ == '__main__':
    print('\n=== Lab 1: look at the images ===')
    print(f'images: {len(svi)} | sources: {svi.source.value_counts().to_dict()}')
    print('years:', svi.year.value_counts().sort_index().to_dict())
    print('platform:', svi.platform.value_counts().to_dict())
    print('weather (predicted by a model):', svi.weather.value_counts().to_dict())
    print('top scenes:', svi.scene.value_counts().head(6).to_dict())

    print('\n=== Lab 2: green view and sky view ===')
    print(f'green view index: mean {svi.green_view_index.mean():.2f} | median {svi.green_view_index.median():.2f} | sky view: mean {svi.sky_view_index.mean():.2f}')
    t = svi.groupby('scene').agg(n=('uuid', 'size'), green=('green_view_index', 'mean'), sky=('sky_view_index', 'mean'), building=('building', 'mean'))
    print(t[t.n >= 300].sort_values('green', ascending=False).round(2).to_string())
    w = km(svi.lat.min(), svi.lon.min(), svi.lat.min(), svi.lon.max()); hgt = km(svi.lat.min(), svi.lon.min(), svi.lat.max(), svi.lon.min())
    print(f'the Singapore images cover one patch of about {w:.1f} x {hgt:.1f} km: lat {svi.lat.min():.3f}-{svi.lat.max():.3f}, lon {svi.lon.min():.3f}-{svi.lon.max():.3f}')
    print('\n=== Lab 3: what makes a street look beautiful (to a model)? ===')
    print('correlation of each pixel share with perceived scores:')
    print(svi[SEG[:8] + PER].corr().loc[SEG[:8], PER].round(2).to_string())
    X, groups = svi[SEG], grid_groups(svi)
    for target in ['beautiful', 'safe', 'lively']:
        y = svi[target]
        lin = cross_val_score(LinearRegression(), X, y, cv=GroupKFold(5), groups=groups, scoring='r2').mean()
        rf = cross_val_score(RandomForestRegressor(n_estimators=120, min_samples_leaf=5, n_jobs=-1, random_state=0), X, y, cv=GroupKFold(5), groups=groups, scoring='r2').mean()
        print(f'{target:9s}: R2 from 16 pixel shares | linear {lin:.2f} | random forest {rf:.2f}')
    lr = LinearRegression().fit(X, svi.beautiful)
    co = pd.Series(lr.coef_, SEG).sort_values()
    print('linear weights for "beautiful" (score change if the share went from 0 to 1):', co.round(1).to_dict())

    print('\n=== Lab 4: check the labels before you trust them ===')
    print(f'"snowy" images in Singapore: {(svi.weather == "snowy").sum()} | "slightly poor" quality: {(svi.quality != "good").mean():.0%} | glare: {svi.glare.mean():.1%}')
    for name, keep in [('all images', svi.index == svi.index),
                       ('good quality only', svi.quality == 'good'),
                       ('good quality, front/back view', (svi.quality == 'good') & (svi.view_direction == 'front/back')),
                       ('driving surface only', svi.platform == 'driving surface'),
                       ('walking surface only', svi.platform == 'walking surface')]:
        s = svi[keep]
        print(f'  {name:32s} n={len(s):6d} | green {s.green_view_index.mean():.3f} | sky {s.sky_view_index.mean():.3f} | beautiful {s.beautiful.mean():.2f}')

    print('\n=== Lab 5: embeddings and similarity ===')
    # an "embedding" is a vector that describes an image; similar images get similar vectors.
    # Here we build a small one from the 16 pixel shares + 6 perception scores (22 numbers).
    from sklearn.preprocessing import StandardScaler
    from sklearn.decomposition import PCA
    good = svi[svi.quality == 'good'].reset_index(drop=True)
    Z = StandardScaler().fit_transform(good[SEG + PER])
    Zn = Z / np.linalg.norm(Z, axis=1, keepdims=True)          # unit length, so dot product = cosine similarity
    pca = PCA(n_components=2).fit(Z)
    print(f'images: {len(good)} | vector length: {Z.shape[1]} | 2 PCA axes keep {pca.explained_variance_ratio_.sum():.0%} of the variation')
    load = pd.DataFrame(pca.components_.T, index=SEG + PER, columns=['axis1', 'axis2'])
    for a in ['axis1', 'axis2']:
        o = load[a].sort_values()
        print(f'  {a}: low = {", ".join(o.index[:3])} | high = {", ".join(o.index[-3:])}')
    q = int(good.green_view_index.idxmax())                     # query: the greenest image
    sim = Zn @ Zn[q]
    top = np.argsort(-sim)[1:6]
    dist = km(good.lat.values[top], good.lon.values[top], good.lat[q], good.lon[q])
    print(f'query image {good.uuid[q][:8]} (green {good.green_view_index[q]:.2f}, scene {good.scene[q]}): 5 most similar images')
    for t, dd in zip(top, dist):
        print(f'  sim {sim[t]:.3f} | {dd*1000:5.0f} m away | green {good.green_view_index[t]:.2f} | scene {good.scene[t]}')
    rng = np.random.default_rng(0)
    i, j = rng.integers(0, len(good), 200000), rng.integers(0, len(good), 200000)
    s_ij = np.einsum('ij,ij->i', Zn[i], Zn[j]); d_ij = km(good.lat.values[i], good.lon.values[i], good.lat.values[j], good.lon.values[j])
    for lo, hi in [(0, .1), (.1, .3), (.3, 1), (1, 3)]:
        m = (d_ij >= lo) & (d_ij < hi)
        print(f'  random pairs {lo:>3}-{hi:<3} km apart: mean similarity {s_ij[m].mean():.2f} (n={m.sum()})')

    print('\n=== Lab 6: reviews meet streets ===')
    import os
    if os.path.exists('sg_places.csv') and os.path.exists('sg_reviews.csv'):
        places = pd.read_csv('sg_places.csv'); rev = pd.read_csv('sg_reviews.csv')
        good = svi[svi.quality == 'good'].reset_index(drop=True)
        # for every place, the street images within 200 m
        from sklearn.neighbors import BallTree
        tree = BallTree(np.radians(good[['lat', 'lon']].values), metric='haversine')
        idx = tree.query_radius(np.radians(places[['lat', 'lon']].values), r=0.2 / 6371.0)
        places['n_img'] = [len(i) for i in idx]
        for c in ['green_view_index', 'sky_view_index', 'beautiful', 'lively']:
            vals = good[c].values
            places[c] = [vals[i].mean() if len(i) else np.nan for i in idx]
        low = rev.text.str.lower()
        rev['heat'] = low.str.contains(r"\b(?:stuffy|humid|sweat\w*|sweltering|warm inside|hot inside|too warm|very warm|so warm|no air ?-?con\w*|non[- ]air ?-?con\w*|without air ?-?con\w*|poor ventilation|not well ventilated|no ventilation)")
        rev['quiet'] = low.str.contains(r"\b(?:quiet|peaceful|calm|serene|tranquil)")
        rev['noise'] = low.str.contains(r"\b(?:noisy|loud|noise|hard to hear|deafening|blasting)")
        g = rev.groupby('place_id').agg(n=('text', 'size'), heat=('heat', 'mean'), quiet=('quiet', 'mean'), noise=('noise', 'mean'), atm=('atmosphere', 'mean'))
        pj = places.join(g, on='place_id')
        pj = pj[(pj.n_img >= 3)]
        print(f'places with at least 3 good street images within 200 m: {len(pj)} of {len(places)}')
        pj['green_band'] = pd.qcut(pj.green_view_index, 3, labels=['least green third', 'middle third', 'greenest third'])
        w = pj.assign(rv=pj.n).groupby('green_band', observed=True).apply(
            lambda s: pd.Series({'places': len(s), 'green': s.green_view_index.mean(), 'reviews': s.n.sum(),
                                 'heat %': 100 * np.average(s.heat, weights=s.n), 'quiet %': 100 * np.average(s.quiet, weights=s.n),
                                 'noise %': 100 * np.average(s.noise, weights=s.n), 'atmosphere': s.atm.mean()}), include_groups=False)
        print(w.round(2).to_string())
        malls = pj.category.eq('Shopping mall')
        print(f'share of shopping malls: least green third {malls[pj.green_band == "least green third"].mean():.0%} | greenest third {malls[pj.green_band == "greenest third"].mean():.0%}')
    else:
        print('copy sg_places.csv and sg_reviews.csv from the reviews guide into this folder to run Lab 6')
