Nishimoto et al. (2011) from CRCNS vim-2: color information¶
Paper. Nishimoto, Vu, Naselaris, Benjamini, Yu & Gallant (2011), Current Biology 21, 1641-1646, doi:10.1016/j.cub.2011.08.031. Claims file. claims/nishimoto-2011-reconstructing.yaml. Dataset. CRCNS vim-2, doi:10.6080/K00Z715X, version unversioned (CRCNS data sets are not versioned; docs/archives.md), streamed from the bucket crcnsarchive as in the first notebook, reproduce_nishimoto_2011.ipynb, which tests the other claims and describes the data.
This notebook tests one claim, discussion-color-no-gain. It is kept apart because it filters the movies three times (L*, A* and B*), which with the first notebook's analyses would pass the hour a notebook may take.
The claims file's reading of the color model: the directional model of fig2de-prediction-accuracy, with the same 6,555-filter bank applied as well to the A* and B* channels of CIE LAB* (19,665 filters, 78,660 regressors with the four delays), fit and scored the same way. Ridge regression is used, as in the first notebook (an assumption there), with the penalty of each voxel chosen on the held-out 10% of the training data (penalty reading a) or by five-fold cross-validation (penalty reading b). As a check of the single shared penalty, the color model is also fit with a separate weight for each color channel (banded ridge). Each channel's features are z-scored with their own training statistics, so the three channels enter the ridge penalty on the same scale (standard choice).
import time, resource, collections, urllib.request
import numpy as np
from scipy import stats
import vim2
T0 = time.time()
SEED = 2011 # standard choice; the same held-out split as the first notebook
ALPHAS = np.logspace(0, 9, 19).astype(np.float32)
SUBJECTS = [1, 2, 3]
def mem_gb():
return resource.getrusage(resource.RUSAGE_SELF).ru_maxrss / 1e6
t = time.time()
filters = vim2.pyramid().filters
S = vim2.stimulus_motion_energy(filters, ("L", "a", "b"))
ME = {c: (S["st"][c], S["sv"][c]) for c in ("L", "a", "b")}
stim_info = {k: S[k] for k in ("member", "member_bytes", "layout", "st_range", "sv_range", "st_L_range", "sv_L_range")}
del S
print(f"stimuli streamed, converted and filtered in three channels in {time.time() - t:.0f} s; peak memory {mem_gb():.1f} GB")
stimuli streamed, converted and filtered in three channels in 1897 s; peak memory 6.9 GB
md5 = urllib.request.urlopen(vim2.BASE + "checksums.md5", timeout=60).read().decode()
print("MD5 sums of the files read (checksums.md5):")
for line in md5.splitlines():
if "Stimuli" in line or "VoxelResponses" in line:
print(" ", line)
print("Stored arrays:", stim_info["layout"], "; pixel range (uint8)", stim_info["st_range"], stim_info["sv_range"])
print("L* range, training %.1f to %.1f, test %.1f to %.1f" % tuple(stim_info["st_L_range"] + stim_info["sv_L_range"]))
for c in ("L", "a", "b"):
tr, te = ME[c]
print(f"channel {c}: motion energy training {tr.shape}, test {te.shape}, range {tr.min():.2f} to {tr.max():.2f} (log units), "
f"filters with no variance over training {(tr.std(0) == 0).sum()}")
print("Filters per channel:", len(filters))
ktr = vim2.keep_mask(7200, vim2.TRAIN_BLOCK)
kte = vim2.keep_mask(540, vim2.TEST_BLOCK)
print("Samples kept: training", ktr.sum(), "(paper 7,128), test", kte.sum(), "(paper 486)")
MD5 sums of the files read (checksums.md5):
d06069177dd2ccad303009b7003c67b9 Stimuli.tar.gz
0e389bfa0b2235d1763d8a0d3bebf0af VoxelResponses_subject1.tar.gz
92f3e621677ac1c8c226c87d77977c88 VoxelResponses_subject2.tar.gz
3ea1084138f70583228ad5649c935921 VoxelResponses_subject3.tar.gz
Stored arrays: {'st': ((108000, 3, 128, 128), 'uint8'), 'sv': ((8100, 3, 128, 128), 'uint8')} ; pixel range (uint8) [0, 255] [0, 255]
L* range, training 0.0 to 100.0, test 0.0 to 100.0
channel L: motion energy training (7200, 6555), test (540, 6555), range -11.51 to 14.81 (log units), filters with no variance over training 0
channel a: motion energy training (7200, 6555), test (540, 6555), range -11.51 to 14.21 (log units), filters with no variance over training 0 channel b: motion energy training (7200, 6555), test (540, 6555), range -11.51 to 14.70 (log units), filters with no variance over training 0 Filters per channel: 6555 Samples kept: training 7128 (paper 7,128), test 486 (paper 486)
held = vim2.holdout_blocks(int(ktr.sum()), np.random.default_rng([SEED, 0]))
fold = vim2.cv_folds(int(ktr.sum()), np.random.default_rng([SEED, 1]))
cols = vim2.model_columns(filters, "directional")
KCH = {}
for c in ("L", "a", "b"):
Xtr, Xte = vim2.build_features(*ME[c], cols)
X = vim2.delayed(Xtr, vim2.DELAYS, 600)[ktr]
Xe = vim2.delayed(Xte, vim2.DELAYS, 60)[kte]
KCH[c] = (X @ X.T, Xe @ X.T)
del X, Xe, Xtr, Xte
KER = {"luminance": KCH["L"], "color": tuple(sum(KCH[c][i] for c in ("L", "a", "b")) for i in (0, 1))}
del ME
print("regressors: luminance", len(cols) * 4, "; color", 3 * len(cols) * 4, f"; held-out samples {held.sum()}; peak memory {mem_gb():.1f} GB")
DATA = {}
for s in SUBJECTS:
t = time.time()
d = vim2.load_subject(s)
good = ~(np.isnan(d["rt"]).any(0) | np.isnan(d["rva"]).any((0, 1)) | np.isnan(d["rv"]).any(0))
rva = d["rva"][:, :, good]
rva = (rva - rva.mean(1, keepdims=True)) / rva.std(1, keepdims=True)
Yrep = rva[:, kte]
DATA[s] = dict(Y=d["rt"][ktr][:, good], Yrep=Yrep, Yte=Yrep.mean(0), n_drop=int((~good).sum()), n=int(good.sum()))
del d, rva
print(f"subject {s}: {DATA[s]['n']} V1-V3B voxels kept, {DATA[s]['n_drop']} dropped for missing values; loaded in {time.time() - t:.0f} s")
regressors: luminance 26220 ; color 78660 ; held-out samples 728; peak memory 6.9 GB
subject 1: 4004 V1-V3B voxels kept, 179 dropped for missing values; loaded in 96 s
subject 2: 4622 V1-V3B voxels kept, 46 dropped for missing values; loaded in 92 s
subject 3: 4381 V1-V3B voxels kept, 29 dropped for missing values; loaded in 95 s
FIT = {s: {} for s in SUBJECTS}
idx_all = np.arange(int(ktr.sum()))
tr, ev = idx_all[~held], idx_all[held]
for m in ("luminance", "color"):
t = time.time()
K, Kte = KER[m]
w, U = vim2.eig_split(K, tr)
Kev = K[np.ix_(ev, tr)]
for s in SUBJECTS:
Y = DATA[s]["Y"]
rh = vim2.ridge_path_corr(w, U, Kev, Y[tr], Y[ev], ALPHAS)
FIT[s][m] = dict(ai=rh.argmax(0), held_acc=rh.max(0))
if s == 1:
C90 = vim2.ridge_dual(w, U, Y[tr], FIT[s][m]["ai"], ALPHAS)
FIT[s][m]["Pheld"] = Kev @ C90
FIT[s][m]["R90"] = Y[tr] - K[np.ix_(tr, tr)] @ C90
del C90
del w, U, Kev
cv = {s: 0 for s in SUBJECTS}
for k in range(5): # penalty reading (b): five folds, whole decades of the grid, as in the first notebook
tr_k, ev_k = idx_all[fold != k], idx_all[fold == k]
wk, Uk = vim2.eig_split(K, tr_k)
Kk = K[np.ix_(ev_k, tr_k)]
for s in SUBJECTS:
Y = DATA[s]["Y"]
cv[s] = cv[s] + vim2.ridge_path_corr(wk, Uk, Kk, Y[tr_k], Y[ev_k], ALPHAS[::2]) / 5
del wk, Uk, Kk
for s in SUBJECTS:
FIT[s][m]["ai_b"] = 2 * cv[s].argmax(0)
w, U = vim2.eig_split(K, idx_all)
for s in SUBJECTS:
Y, f = DATA[s]["Y"], FIT[s][m]
for rd in ("", "_b"):
C = vim2.ridge_dual(w, U, Y, f["ai" + rd], ALPHAS)
P = Kte @ C
f["avg" + rd] = vim2.corr_cols(P, DATA[s]["Yte"])
f["rep" + rd] = np.mean([vim2.corr_cols(P, DATA[s]["Yrep"][k]) for k in range(10)], 0)
if s == 1 and rd == "":
f["Pte"], f["R"] = P, Y - K @ C
del C, P
print(f"{m}: fitted in {time.time() - t:.0f} s; voxels per chosen penalty (S1) {np.bincount(FIT[1][m]['ai'], minlength=len(ALPHAS)).tolist()}")
luminance: fitted in 138 s; voxels per chosen penalty (S1) [147, 1, 2, 14, 14, 40, 37, 48, 116, 315, 898, 980, 544, 301, 154, 67, 26, 13, 287]
color: fitted in 139 s; voxels per chosen penalty (S1) [155, 0, 0, 3, 6, 15, 24, 31, 73, 132, 501, 1052, 770, 456, 262, 129, 56, 25, 314]
discussion-color-no-gain¶
In preliminary work, encoding models with explicit color information did not predict or identify better than models using luminance alone.
Criterion: reproduced when the color model's mean accuracy is not higher by the paired Wilcoxon signed-rank test over voxels at p < 0.0001; the difference is reported either way. The identification count of each model (as fig3ab-identification-s1, model reading a, S1) is reported but does not decide the outcome. Readings of the test signal as in fig2de-prediction-accuracy: (a) the repeat mean, (b) the mean over single repeats; subjects (i) pooled or (ii) the mean of subject means; the ridge penalty chosen on (a) the held-out 10% or (b) five folds. Below, the color model is also fit with a separate weight for each color channel (banded ridge), as a check of the shared penalty.
def pooled(m, rd):
return np.concatenate([FIT[s][m][rd] for s in SUBJECTS])
for rd in ("avg", "rep", "avg_b", "rep_b"):
L, C = pooled("luminance", rd), pooled("color", rd)
p2 = stats.wilcoxon(C, L).pvalue
p1 = stats.wilcoxon(C, L, alternative="greater").pvalue
sm = {m: np.mean([FIT[s][m][rd].mean() for s in SUBJECTS]) for m in ("luminance", "color")}
higher = C.mean() > L.mean() and p2 < 1e-4
print(f"signal {rd[:3]}, penalty reading ({'b' if rd.endswith('_b') else 'a'}): (i) pooled over {len(L)} voxels, luminance {L.mean():.4f}, color {C.mean():.4f}, difference {C.mean() - L.mean():+.4f}; "
f"(ii) subject means {sm['luminance']:.4f} and {sm['color']:.4f}; Wilcoxon two-sided p = {p2:.1e}, one-sided (color greater) p = {p1:.1e}; "
f"color better than luminance in {np.mean(C > L):.2f} of voxels; color higher by the test: {higher}")
for s in SUBJECTS:
l, c = FIT[s]["luminance"][rd], FIT[s]["color"][rd]
print(f" S{s}: luminance {l.mean():.4f}, color {c.mean():.4f}, difference {c.mean() - l.mean():+.4f}, Wilcoxon two-sided p = {stats.wilcoxon(c, l).pvalue:.1e}")
D1 = DATA[1]
th = np.arange(len(held))[held]
tt = np.arange(540)[kte]
LAMS = np.round(np.logspace(-1, 2, 13), 3).tolist()
def within1(choice, times):
return int(np.sum(np.abs(times[choice] - times) <= 1))
for m in ("luminance", "color"):
f = FIT[1][m]
sel = np.argsort(f["held_acc"])[::-1][:2000]
cov90 = (f["R90"][:, sel].T @ f["R90"][:, sel]) / f["R90"].shape[0]
hc = [within1(vim2.identify(D1["Y"][held][:, sel], f["Pheld"][:, sel], cov90, l), th) for l in LAMS]
lam = LAMS[int(np.argmax(hc))]
cov = (f["R"][:, sel].T @ f["R"][:, sel]) / f["R"].shape[0]
ca = within1(vim2.identify(D1["Yte"][:, sel], f["Pte"][:, sel], cov, lam), tt)
cr = [within1(vim2.identify(D1["Yrep"][k][:, sel], f["Pte"][:, sel], cov, lam), tt) for k in range(10)]
print(f"identification, S1, {m}: Tikhonov penalty {lam} (held-out counts {hc} of {held.sum()}); "
f"(i) repeat mean {ca} of 486; (ii) single repeats mean {np.mean(cr):.1f} (range {min(cr)} to {max(cr)})")
signal avg, penalty reading (a): (i) pooled over 13007 voxels, luminance 0.3734, color 0.3639, difference -0.0095; (ii) subject means 0.3731 and 0.3636; Wilcoxon two-sided p = 0.0e+00, one-sided (color greater) p = 1.0e+00; color better than luminance in 0.38 of voxels; color higher by the test: False S1: luminance 0.3763, color 0.3691, difference -0.0072, Wilcoxon two-sided p = 0.0e+00 S2: luminance 0.3975, color 0.3893, difference -0.0082, Wilcoxon two-sided p = 3.1e-38 S3: luminance 0.3455, color 0.3326, difference -0.0129, Wilcoxon two-sided p = 0.0e+00 signal rep, penalty reading (a): (i) pooled over 13007 voxels, luminance 0.2069, color 0.2022, difference -0.0047; (ii) subject means 0.2066 and 0.2020; Wilcoxon two-sided p = 0.0e+00, one-sided (color greater) p = 1.0e+00; color better than luminance in 0.38 of voxels; color higher by the test: False S1: luminance 0.2128, color 0.2082, difference -0.0046, Wilcoxon two-sided p = 0.0e+00 S2: luminance 0.2296, color 0.2261, difference -0.0035, Wilcoxon two-sided p = 4.2e-35 S3: luminance 0.1775, color 0.1716, difference -0.0059, Wilcoxon two-sided p = 0.0e+00 signal avg, penalty reading (b): (i) pooled over 13007 voxels, luminance 0.3861, color 0.3756, difference -0.0105; (ii) subject means 0.3859 and 0.3753; Wilcoxon two-sided p = 0.0e+00, one-sided (color greater) p = 1.0e+00; color better than luminance in 0.36 of voxels; color higher by the test: False S1: luminance 0.3945, color 0.3821, difference -0.0124, Wilcoxon two-sided p = 0.0e+00 S2: luminance 0.4087, color 0.4009, difference -0.0078, Wilcoxon two-sided p = 0.0e+00 S3: luminance 0.3546, color 0.3430, difference -0.0116, Wilcoxon two-sided p = 0.0e+00 signal rep, penalty reading (b): (i) pooled over 13007 voxels, luminance 0.2121, color 0.2069, difference -0.0052; (ii) subject means 0.2119 and 0.2066; Wilcoxon two-sided p = 0.0e+00, one-sided (color greater) p = 1.0e+00; color better than luminance in 0.36 of voxels; color higher by the test: False S1: luminance 0.2202, color 0.2136, difference -0.0067, Wilcoxon two-sided p = 0.0e+00 S2: luminance 0.2346, color 0.2307, difference -0.0040, Wilcoxon two-sided p = 0.0e+00 S3: luminance 0.1810, color 0.1757, difference -0.0052, Wilcoxon two-sided p = 0.0e+00
identification, S1, luminance: Tikhonov penalty 3.162 (held-out counts [296, 359, 406, 448, 492, 510, 523, 519, 503, 467, 419, 368, 303] of 728); (i) repeat mean 409 of 486; (ii) single repeats mean 338.1 (range 321 to 364)
identification, S1, color: Tikhonov penalty 5.623 (held-out counts [242, 301, 346, 395, 447, 474, 484, 486, 456, 438, 401, 355, 300] of 728); (i) repeat mean 378 of 486; (ii) single repeats mean 309.2 (range 297 to 323)
Check: a separate weight per color channel (banded ridge)¶
With one ridge penalty for all 78,660 regressors, the A* and B* features are penalized as much as the luminance features, which may handicap the color model. Here each color channel enters the kernel with its own weight g (K = K_L + g_A K_A + g_B K_B, which is ridge with the penalty of that channel divided by g), and each voxel chooses the pair (g_A, g_B) and the penalty that best predict the held-out 10% of the training data. The pairs are (0, 0), the luminance model; (1, 1), the color model above; (0.1, 0.1) and (0.3, 0.3); and each channel alone at 0.3 and 1 ((0.3, 0), (1, 0), (0, 0.3), (0, 1)). Since the luminance model is one of the choices, the banded model can only gain from color on the held-out data; the test data then show whether that gain is real. Only penalty reading (a) is run here, for time.
t = time.time()
PAIRS = [(0.0, 0.0), (1.0, 1.0), (0.1, 0.1), (0.3, 0.3), (0.3, 0.0), (1.0, 0.0), (0.0, 0.3), (0.0, 1.0)]
best = {s: (np.full(DATA[s]["n"], -np.inf), np.zeros(DATA[s]["n"], int), np.zeros(DATA[s]["n"], int)) for s in SUBJECTS}
for j, (ga, gb) in enumerate(PAIRS):
K = KCH["L"][0] + ga * KCH["a"][0] + gb * KCH["b"][0]
w, U = vim2.eig_split(K, tr)
Kev = K[np.ix_(ev, tr)]
for s in SUBJECTS:
rh = vim2.ridge_path_corr(w, U, Kev, DATA[s]["Y"][tr], DATA[s]["Y"][ev], ALPHAS)
v, ai, pj = best[s]
upd = rh.max(0) > v
v[upd], ai[upd], pj[upd] = rh.max(0)[upd], rh.argmax(0)[upd], j
del w, U, Kev
banded = {s: np.zeros(DATA[s]["n"]) for s in SUBJECTS}
for j, (ga, gb) in enumerate(PAIRS):
if not any((best[s][2] == j).any() for s in SUBJECTS):
continue
K = KCH["L"][0] + ga * KCH["a"][0] + gb * KCH["b"][0]
Kte = KCH["L"][1] + ga * KCH["a"][1] + gb * KCH["b"][1]
w, U = vim2.eig_split(K, idx_all)
for s in SUBJECTS:
sel = best[s][2] == j
if sel.any():
C = vim2.ridge_dual(w, U, DATA[s]["Y"][:, sel], best[s][1][sel], ALPHAS)
banded[s][sel] = vim2.corr_cols(Kte @ C, DATA[s]["Yte"][:, sel])
del w, U
for s in SUBJECTS:
print(f"S{s}: voxels per chosen (g_A, g_B):", {f"{PAIRS[j]}": int((best[s][2] == j).sum()) for j in range(len(PAIRS))})
L, B = pooled("luminance", "avg"), np.concatenate([banded[s] for s in SUBJECTS])
print(f"banded color model, signal (a), (i) pooled over {len(L)} voxels: luminance {L.mean():.4f}, banded color {B.mean():.4f}, difference {B.mean() - L.mean():+.4f}; "
f"(ii) subject means {np.mean([FIT[s]['luminance']['avg'].mean() for s in SUBJECTS]):.4f} and {np.mean([banded[s].mean() for s in SUBJECTS]):.4f}; "
f"Wilcoxon two-sided p = {stats.wilcoxon(B, L).pvalue:.1e}, one-sided (color greater) p = {stats.wilcoxon(B, L, alternative='greater').pvalue:.1e}; "
f"banded better in {np.mean(B > L):.2f} of voxels, equal (to 1e-5) in {np.mean(np.isclose(B, L, rtol=0, atol=1e-5)):.2f}")
for s in SUBJECTS:
l, b = FIT[s]["luminance"]["avg"], banded[s]
ch = best[s][2] != 0
print(f" S{s}: luminance {l.mean():.4f}, banded {b.mean():.4f}, difference {b.mean() - l.mean():+.4f}; "
f"in the {ch.sum()} voxels that chose some color: luminance {l[ch].mean():.4f}, banded {b[ch].mean():.4f}")
print(f"banded fits in {time.time() - t:.0f} s")
print(f"total run time {(time.time() - T0) / 60:.1f} min; peak memory {mem_gb():.1f} GB")
S1: voxels per chosen (g_A, g_B): {'(0.0, 0.0)': 1331, '(1.0, 1.0)': 503, '(0.1, 0.1)': 78, '(0.3, 0.3)': 89, '(0.3, 0.0)': 315, '(1.0, 0.0)': 635, '(0.0, 0.3)': 272, '(0.0, 1.0)': 781}
S2: voxels per chosen (g_A, g_B): {'(0.0, 0.0)': 1327, '(1.0, 1.0)': 824, '(0.1, 0.1)': 98, '(0.3, 0.3)': 151, '(0.3, 0.0)': 278, '(1.0, 0.0)': 847, '(0.0, 0.3)': 292, '(0.0, 1.0)': 805}
S3: voxels per chosen (g_A, g_B): {'(0.0, 0.0)': 1158, '(1.0, 1.0)': 884, '(0.1, 0.1)': 125, '(0.3, 0.3)': 105, '(0.3, 0.0)': 285, '(1.0, 0.0)': 622, '(0.0, 0.3)': 328, '(0.0, 1.0)': 874}
banded color model, signal (a), (i) pooled over 13007 voxels: luminance 0.3734, banded color 0.3668, difference -0.0066; (ii) subject means 0.3731 and 0.3665; Wilcoxon two-sided p = 1.4e-113, one-sided (color greater) p = 1.0e+00; banded better in 0.39 of voxels, equal (to 1e-5) in 0.29
S1: luminance 0.3763, banded 0.3701, difference -0.0062; in the 2673 voxels that chose some color: luminance 0.3774, banded 0.3681
S2: luminance 0.3975, banded 0.3916, difference -0.0059; in the 3295 voxels that chose some color: luminance 0.3998, banded 0.3915
S3: luminance 0.3455, banded 0.3378, difference -0.0077; in the 3223 voxels that chose some color: luminance 0.3449, banded 0.3345
banded fits in 267 s
total run time 45.9 min; peak memory 6.9 GB
Result: reproduced. The color model (the 6,555-filter bank on L*, A* and B*) predicts worse than the luminance model, not better. Pooled over 13,007 voxels, with penalty reading (a) and the repeat-averaged signal, the mean accuracy is 0.3640 against 0.3735 (difference -0.0095), lower in every subject (-0.0073, -0.0082, -0.0128), and better in only 38% of voxels; the one-sided Wilcoxon test of color greater gives p = 1.0. Single repeats give the same (-0.0047). With penalty reading (b) the difference is -0.0105 (0.3756 against 0.3861; single repeats -0.0052), lower in every subject, one-sided p = 1.0.
With a separate weight for each color channel (banded ridge), chosen per voxel on the held-out data, 2,674, 3,295 and 3,222 voxels of S1, S2 and S3 choose some color weight, but the banded model still predicts worse on the test data: 0.3669 against 0.3735 (difference -0.0066; lower in every subject, -0.0062, -0.0059, -0.0077), one-sided p = 1.0. In the voxels that chose some color, the banded model is lower than luminance in each subject (for example S1 0.3679 against 0.3773). So the shared penalty is not what makes the color model worse here: color features chosen on the training data do not carry over to the test data.
In S1 identification the color model identifies 378 of 486 test volumes within one volume against 408 for luminance, which the criterion reports without a test. The outcome rests on the claims file's reading of the color model and on ridge regression.
Summary¶
| Claim | Outcome |
|---|---|
| discussion-color-no-gain | reproduced (color 0.3640 against luminance 0.3735 under penalty reading a, 0.3756 against 0.3861 under reading b; banded color 0.3669; identification 378 against 408 of 486) |
The other claims are in reproduce_nishimoto_2011.ipynb, with the data check and the differences from the paper's method. Here, the color model follows the claims file's reading (the paper does not describe its color models), and is fit by ridge regression with the penalty chosen on the held-out 10% of the training data or by five-fold cross-validation, and, as a check, with a separate weight for each color channel.