|
| 1 | +""" |
| 2 | +Explore Covariance Matrix |
| 3 | +========================= |
| 4 | +
|
| 5 | +Example showing how you can explore a covariance matrix with a selector tool. |
| 6 | +""" |
| 7 | + |
| 8 | +# test_example = false |
| 9 | +# sphinx_gallery_pygfx_docs = 'animate 10s' |
| 10 | + |
| 11 | + |
| 12 | +import fastplotlib as fpl |
| 13 | +from sklearn import datasets |
| 14 | +from sklearn.preprocessing import StandardScaler |
| 15 | + |
| 16 | +# load faces dataset |
| 17 | +faces = datasets.fetch_olivetti_faces() |
| 18 | +data = faces["data"] |
| 19 | + |
| 20 | +# sort the data so it's easier to understand the covariance matrix |
| 21 | +targets = faces["target"] |
| 22 | +sort_indices = targets.argsort() |
| 23 | +targets_sorted = targets[sort_indices] |
| 24 | + |
| 25 | +X = data[sort_indices] |
| 26 | + |
| 27 | +# scale the data w.r.t. mean and standard deviation |
| 28 | +X = StandardScaler().fit_transform(X) |
| 29 | + |
| 30 | +# compute covariance matrix |
| 31 | +X = X.T |
| 32 | +cov = X @ X.T / X.shape[1] |
| 33 | + |
| 34 | +# reshaped image for each sample wil be 64 x 64 pixels |
| 35 | +img = cov[0].reshape(64, 64) |
| 36 | + |
| 37 | +# figure kwargs for image widget |
| 38 | +# controller_ids = [[0, 1]] so we get independent controllers for each supblot |
| 39 | +# the covariance matrix is 4096 x 4096 and the reshaped image ix 64 x 64 |
| 40 | +figure_kwargs = {"size": (700, 400), "controller_ids": [[0, 1]]} |
| 41 | + |
| 42 | +# create image widget |
| 43 | +iw = fpl.ImageWidget( |
| 44 | + data=[cov, img], # display the covariance matrix and reshaped image of a row |
| 45 | + cmap="bwr", # diverging colormap |
| 46 | + names=["covariance", "row image"], |
| 47 | + figure_kwargs=figure_kwargs, |
| 48 | +) |
| 49 | + |
| 50 | +# graphic that corresponds to image widget data array 0 |
| 51 | +# 0 is the covariance matrix, 1 is the reshaped image of a row from the covariance matrix |
| 52 | + |
| 53 | +# add a linear selector to select y axis values so we can select rows of the cov matrix |
| 54 | +selector_cov = iw.managed_graphics[0].add_linear_selector(axis="y") |
| 55 | + |
| 56 | +# if you are exploring other types of matrices which are not-symmetric |
| 57 | +# you can also add a column selector by setting axis="x" |
| 58 | + |
| 59 | +# set vmin vmax |
| 60 | +for g in iw.managed_graphics: |
| 61 | + g.vmin, g.vmax = -1, 1 |
| 62 | + |
| 63 | + |
| 64 | +# event handler when the covariance matrix row changes |
| 65 | +@selector_cov.add_event_handler("selection") |
| 66 | +def update_img(ev): |
| 67 | + # get the row index |
| 68 | + ix = ev.get_selected_index() |
| 69 | + |
| 70 | + # get the image the corresponds to this row |
| 71 | + img = cov[ix].reshape(64, 64) |
| 72 | + |
| 73 | + # change the reshaped image graphic data |
| 74 | + iw.managed_graphics[1].data = img |
| 75 | + |
| 76 | + |
| 77 | +figure = iw.figure # not required, just for the docs gallery to pick it up |
| 78 | + |
| 79 | + |
| 80 | +# move the selector programmatically, this is mainly for the docs gallery |
| 81 | +# for real use you can interact with the selector with your mouse |
| 82 | +def animate(): |
| 83 | + selector_cov.selection += 1 |
| 84 | + |
| 85 | + |
| 86 | +iw.figure.add_animations(animate) |
| 87 | + |
| 88 | +iw.show() |
| 89 | + |
| 90 | +# NOTE: `if __name__ == "__main__"` is NOT how to use fastplotlib interactively |
| 91 | +# please see our docs for using fastplotlib interactively in ipython and jupyter |
| 92 | +if __name__ == "__main__": |
| 93 | + print(__doc__) |
| 94 | + fpl.run() |
0 commit comments