
###******************************************************************###
###******************************************************************###
###  "The Shape of Shared Attention: investigating the influence of ###
###  environmental variation on the compositional structure of       ### 
###  traditional decorative artworks"                                ###
###                                                                  ###
###  Feature Extraction and correlation code                         ###
###******************************************************************###
###******************************************************************###

#This code has been transfered from a Jupyter notebook and is organized as such

import torch
import torchvision.models as models
import torchvision.transforms as transforms
from PIL import Image
Image.MAX_IMAGE_PIXELS = 9933120000 # this ensures large photos will open
import numpy as np
import pandas as pd
import glob
import os
from sklearn.preprocessing import StandardScaler
import umap
import phate

from scipy.stats import spearmanr

print("DONE IMPORTS")

inny = open("px_cm_data_inclToraja.txt", "r")
fff = inny.readlines()
pxcmdict = {}
for i in range(len(fff)):
    rrr = fff[i].strip().split('\t')
    pxcmdict[rrr[1]] = rrr[4]

target_px_cm = 4.15568181818182

files = glob.glob('*_resize.png')
for item in files:
    ttt = item.strip().split('_')
    acc = ttt[1]

    if acc in pxcmdict.keys():
        img = Image.open(item)
        width, height = img.size

        scale_factor = target_px_cm / float(pxcmdict[acc])
        new_width  = round(width  * scale_factor)
        new_height = round(height * scale_factor)

        img_resized = img.resize((new_width, new_height), Image.NEAREST)
        
        # save scaled image
        out_name = item.replace('_resize.png', '_scaled.png')
        img_resized.save(out_name)
        print(f"Saved {out_name}: {width}x{height} -> {new_width}x{new_height}")

    else:
        print(f"Warning: {acc} not found in px/cm data, skipping {item}")

print("DONE")


### model setup and preprocessing

device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
model = models.vgg16(weights='IMAGENET1K_V1')
model = model.to(device)
model.eval()

# for hook outputs
activation = {}
def get_activation(name):
    def hook(model, input, output):
        activation[name] = output.detach()
    return hook

# hooks on early layers
model.features[14].register_forward_hook(get_activation('conv3')) # features[14] = end of conv3 block (mid-level textures)
model.features[21].register_forward_hook(get_activation('conv4')) # features[21] = end of conv4 block (complex textures/patterns)

# preprocessing
transform = transforms.Compose([
    transforms.Resize((224, 224)),
    transforms.ToTensor(),
    transforms.Normalize(mean=[0.485, 0.456, 0.406],
                         std=[0.229, 0.224, 0.225])
])

def extract_features(img_path):
    img = Image.open(img_path).convert('RGB')
    x = transform(img).unsqueeze(0).to(device)
    
    with torch.no_grad():
        _ = model(x)  
    
    # global avg pool for fixed-size vector
    conv3_feat = activation['conv3'].mean(dim=[2, 3]).cpu().numpy().flatten()
    conv4_feat = activation['conv4'].mean(dim=[2, 3]).cpu().numpy().flatten()
    
    # cat layers
    return np.concatenate([conv3_feat, conv4_feat]) 

# extract features from scaled images
files = glob.glob('*_scaled.png')
records = []

for img_path in files:
    print(f"Processing {img_path}...")
    try:
        features = extract_features(img_path)
        records.append({
            'filename': os.path.basename(img_path),
            'features': features
        })
    except Exception as e:
        print(f"  Failed: {e}")

print(f"Extracted features from {len(records)} images")
print(f"Feature vector size: {records[0]['features'].shape}")

feature_matrix = np.array([r['features'] for r in records])
filenames = [r['filename'] for r in records]

np.save('vgg16_texture_features.npy', feature_matrix)
pd.DataFrame({'filename': filenames}).to_csv('vgg16_texture_filenames.csv', index=False)


# align feature matrix to dataframe
feature_matrix_aligned = feature_matrix[merged.index]

# UMAP dim reduction
reducer = umap.UMAP(n_components=2, random_state=309) 
embedding = reducer.fit_transform(feature_matrix_aligned)

merged['umap1'] = embedding[:, 0]
merged['umap2'] = embedding[:, 1]

# PHATE
phate_operator = phate.PHATE(n_components=2, knn=5,random_state=139, verbose=True)
embedding = phate_operator.fit_transform(feature_matrix_aligned)

merged['phate1'] = embedding[:, 0]
merged['phate2'] = embedding[:, 1]

# PCA

scaler = StandardScaler()
features_scaled = scaler.fit_transform(feature_matrix_aligned)

pca = PCA(n_components=2)
pca_embedding = pca.fit_transform(features_scaled)

print(f"Variance explained: PC1={pca.explained_variance_ratio_[0]:.3f}, PC2={pca.explained_variance_ratio_[1]:.3f}")

merged['pc1'] = pca_embedding[:, 0]
merged['pc2'] = pca_embedding[:, 1]


# correlations
for dim in ['pc1', 'pc2']:
    r_H, p_H = spearmanr(merged[dim], merged['H'])
    r_C, p_C = spearmanr(merged[dim], merged['C'])
    print(f"{dim} vs H: r={r_H:.3f}, p={p_H:.4f}")
    print(f"{dim} vs C: r={r_C:.3f}, p={p_C:.4f}")
    
for dim in ['umap1', 'umap2']:
    r_H, p_H = spearmanr(merged[dim], merged['H'])
    r_C, p_C = spearmanr(merged[dim], merged['C'])
    print(f"{dim} vs H: r={r_H:.3f}, p={p_H:.4f}")
    print(f"{dim} vs C: r={r_C:.3f}, p={p_C:.4f}")
    
for dim in ['phate1', 'phate2']:
    r_H, p_H = spearmanr(merged[dim], merged['H'])
    r_C, p_C = spearmanr(merged[dim], merged['C'])
    print(f"{dim} vs H: r={r_H:.3f}, p={p_H:.4f}")
    print(f"{dim} vs C: r={r_C:.3f}, p={p_C:.4f}")
    
   