Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 01f23eb

Browse files
committed
added analysis files so far and resultant figures
1 parent f246691 commit 01f23eb

6 files changed

Lines changed: 357 additions & 0 deletions

File tree

perceptions/analysis.py

Lines changed: 171 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,171 @@
1+
from skimage import io, color
2+
import numpy as np
3+
import matplotlib.pyplot as plt
4+
from matplotlib import cm
5+
import matplotlib as mpl
6+
import pdb
7+
from scipy.optimize import curve_fit
8+
9+
mpl.rcParams.update({'font.size': 14})
10+
mpl.rcParams['font.sans-serif'] = 'Arev Sans, Bitstream Vera Sans, Lucida Grande, Verdana, Geneva, Lucid, Helvetica, Avant Garde, sans-serif'
11+
mpl.rcParams['mathtext.fontset'] = 'custom'
12+
mpl.rcParams['mathtext.cal'] = 'cursive'
13+
mpl.rcParams['mathtext.rm'] = 'sans'
14+
mpl.rcParams['mathtext.tt'] = 'monospace'
15+
mpl.rcParams['mathtext.it'] = 'sans:italic'
16+
mpl.rcParams['mathtext.bf'] = 'sans:bold'
17+
mpl.rcParams['mathtext.sf'] = 'sans'
18+
mpl.rcParams['mathtext.fallback_to_cm'] = 'True'
19+
20+
21+
## Get colormaps, from http://matplotlib.org/1.2.1/examples/pylab_examples/show_colormaps.html
22+
23+
# Get a list of the colormaps in matplotlib. Ignore the ones that end with
24+
# '_r' because these are simply reversed versions of ones that don't end
25+
# with '_r'
26+
maps = sorted(m for m in plt.cm.datad if not m.endswith("_r"))
27+
nmaps = len(maps) + 1
28+
29+
# OR, to have colormaps separated into categories: http://matplotlib.org/examples/color/colormaps_reference.html
30+
31+
cmaps = [('Sequential', ['binary', 'Blues', 'BuGn', 'BuPu', 'gist_yarg',
32+
'GnBu', 'Greens', 'Greys', 'Oranges', 'OrRd',
33+
'PuBu', 'PuBuGn', 'PuRd', 'Purples', 'RdPu',
34+
'Reds', 'YlGn', 'YlGnBu', 'YlOrBr', 'YlOrRd']),
35+
('Sequential (2)', ['afmhot', 'autumn', 'bone', 'cool', 'copper',
36+
'gist_gray', 'gist_heat', 'gray', 'hot', 'pink',
37+
'spring', 'summer', 'winter']),
38+
('Diverging', ['BrBG', 'bwr', 'coolwarm', 'PiYG', 'PRGn', 'PuOr',
39+
'RdBu', 'RdGy', 'RdYlBu', 'RdYlGn', 'seismic']),
40+
('Qualitative', ['Accent', 'Dark2', 'hsv', 'Paired', 'Pastel1',
41+
'Pastel2', 'Set1', 'Set2', 'Set3', 'spectral']),
42+
('Miscellaneous', ['gist_earth', 'gist_ncar', 'gist_rainbow',
43+
'gist_stern', 'jet', 'brg', 'CMRmap', 'cubehelix',
44+
'gnuplot', 'gnuplot2', 'ocean', 'rainbow',
45+
'terrain', 'flag', 'prism'])]
46+
47+
ncmaps = len(cmaps)
48+
49+
x = np.linspace(0.0, 1.0, 100)
50+
51+
## Sequential colormaps
52+
53+
fig = plt.figure(figsize=(18,8))
54+
55+
# loop through maps
56+
# for i,m in enumerate(maps):
57+
i = 1
58+
for cmap_category, cmap_list in cmaps:
59+
60+
if 'Sequential' not in cmap_category:
61+
continue
62+
63+
flag = True
64+
65+
ax = fig.add_subplot(2, 1, i)
66+
if i==1:
67+
ax.set_title('Lightness $L^*$ along colormap index', fontsize=18)
68+
69+
for j, cmap in enumerate(cmap_list):
70+
71+
# Get rgb values for colormap
72+
rgb = cm.get_cmap(cmap)(x)[np.newaxis,:,:3]
73+
# hsv = matplotlib.colors.rgb_to_hsv(rgb).squeeze()
74+
75+
# Get colormap in CIE LAB. We want the L here.
76+
lab = color.rgb2lab(rgb)
77+
78+
# if cmap=='binary':
79+
# pdb.set_trace()
80+
81+
# Plot colormap L values
82+
if '(2)' in cmap_category:
83+
ax.scatter(x+j*0.83, lab[0,:,0], c=x, cmap=cmap, s=300, linewidths=0.1)
84+
ax.set_ylabel('Sequential MatLab', fontsize=18)
85+
ax.axis([0,11,0,100])
86+
else:
87+
ax.scatter(x+j*0.5, lab[0,::-1,0], c=x, cmap=cmap + '_r', s=300, linewidths=0.1)
88+
ax.set_ylabel('Sequential', fontsize=18)
89+
ax.axis([0,10.5,0,100])
90+
91+
ax.get_xaxis().set_ticks([])
92+
93+
i += 1
94+
95+
fig.subplots_adjust(left=0.04, right=0.99, bottom=0.05, top=0.93, hspace=0.1)
96+
fig.show()
97+
98+
fig.savefig('figures/lightness-sequential.png')
99+
100+
101+
## Non-sequential colormaps
102+
103+
fig = plt.figure(figsize=(18,10))
104+
105+
# loop through maps
106+
# for i,m in enumerate(maps):
107+
i = 1
108+
for cmap_category, cmap_list in cmaps:
109+
110+
if 'Sequential' in cmap_category:
111+
continue
112+
113+
flag = True
114+
115+
ax = fig.add_subplot(3, 1, i)
116+
if i==1:
117+
ax.set_title('Lightness $L^*$ along colormap index', fontsize=18)
118+
119+
for j, cmap in enumerate(cmap_list):
120+
121+
# Get rgb values for colormap
122+
rgb = cm.get_cmap(cmap)(x)[np.newaxis,:,:3]
123+
# hsv = matplotlib.colors.rgb_to_hsv(rgb).squeeze()
124+
125+
# Get colormap in CIE LAB. We want the L here.
126+
lab = color.rgb2lab(rgb)
127+
128+
# Plot colormap L values
129+
if 'Qualitative' in cmap_category:
130+
ax.scatter(x+j*1.3, lab[0,:,0], c=x, cmap=cmap, s=300, linewidths=0.1)
131+
ax.axis([0,13,0,100])
132+
elif 'Diverging' in cmap_category:
133+
ax.scatter(x+j*1.2, lab[0,::-1,0], c=x, cmap=cmap + '_r', s=300, linewidths=0.1)
134+
ax.axis([0,13,0,100])
135+
elif 'Miscellaneous' in cmap_category:
136+
ax.scatter(x+j*1.5, lab[0,::-1,0], c=x, cmap=cmap + '_r', s=300, linewidths=0.1)
137+
ax.axis([0,22,0,100])
138+
139+
ax.get_xaxis().set_ticks([])
140+
ax.set_ylabel(cmap_category, fontsize=18)
141+
142+
i += 1
143+
144+
fig.subplots_adjust(left=0.04, right=0.99, bottom=0.03, top=0.95, hspace=0.1)
145+
fig.show()
146+
fig.savefig('figures/lightness-rest.png')
147+
148+
# can't figure out this part. Curve fitting isn't looking right.
149+
150+
# ## Sequential colormap L* curve fitting
151+
152+
# def func(x, a, b, c):
153+
# return c + a * x ** b
154+
155+
# # xdata = np.linspace(0, 4, 50)
156+
157+
# # Loop through sequential colormaps and calculate the curve fit
158+
# # Then paste the exponent on the plot for each.
159+
# for j, cmap in enumerate(cmaps[0][1]): # Sequential colormaps
160+
161+
# # Get rgb values for colormap
162+
# rgb = cm.get_cmap(cmap + '_r')(x)[np.newaxis,:,:3]
163+
164+
# # Get colormap in CIE LAB. We want the L here.
165+
# lab = color.rgb2lab(rgb)
166+
167+
# popt, pcov = curve_fit(func, x, lab[0,:,0])
168+
169+
# print cmap, popt[1]
170+
171+
# # pdb.set_trace()

perceptions/figures/albers-bw.pdf

10.2 KB
Binary file not shown.

perceptions/figures/albers.pdf

8.95 KB
Binary file not shown.
701 KB
Loading
621 KB
Loading

perceptions/weber-fechner.py

Lines changed: 186 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,186 @@
1+
import numpy as np
2+
import matplotlib.pyplot as plt
3+
from skimage import io, color
4+
import pdb
5+
import matplotlib as mpl
6+
from matplotlib import cm
7+
8+
mpl.rcParams.update({'font.size': 20})
9+
mpl.rcParams['font.sans-serif'] = 'Arev Sans, Bitstream Vera Sans, Lucida Grande, Verdana, Geneva, Lucid, Helvetica, Avant Garde, sans-serif'
10+
mpl.rcParams['mathtext.fontset'] = 'custom'
11+
mpl.rcParams['mathtext.cal'] = 'cursive'
12+
mpl.rcParams['mathtext.rm'] = 'sans'
13+
mpl.rcParams['mathtext.tt'] = 'monospace'
14+
mpl.rcParams['mathtext.it'] = 'sans:italic'
15+
mpl.rcParams['mathtext.bf'] = 'sans:bold'
16+
mpl.rcParams['mathtext.sf'] = 'sans'
17+
mpl.rcParams['mathtext.fallback_to_cm'] = 'True'
18+
19+
20+
### Red, original Albers plot
21+
22+
nrows = 5
23+
24+
# Start with red
25+
red = np.array([np.hstack([np.ones((nrows,1)), np.zeros((nrows,2))])])
26+
27+
# Get basic red in LAB
28+
lab_add = color.rgb2lab(red)
29+
lab_geometric = lab_add.copy()
30+
31+
# Alter successive rows with more black
32+
k = 1
33+
for i in xrange(red.shape[1]):
34+
# more blackness is closer to 0 than one, and in first column of LAB
35+
lab_add[0,i,0] = lab_add[0,i,0] - 10*i
36+
print i,k
37+
if i != 0:
38+
lab_geometric[0,i,0] = lab_geometric[0,i,0] - 10*k
39+
k *= 2
40+
41+
# Change LAB back to RGB for plotting
42+
rgb_add = red.copy() # only change red values
43+
temp = color.lab2rgb(lab_add)
44+
rgb_add[0,:,0] = temp[0,:,0]
45+
rgb_geometric = red.copy() # only change red values
46+
temp = color.lab2rgb(lab_geometric)
47+
rgb_geometric[0,:,0] = temp[0,:,0]
48+
49+
fig = plt.figure()
50+
k = 1
51+
for i in xrange(red.shape[1]):
52+
53+
# LHS: additive
54+
ax1 = fig.add_subplot(nrows,2,i*2+1, axisbg=tuple(rgb_add[0,i,:]))
55+
print tuple(lab_add[0,i,:])#, tuple(rgb_add[0,i,:])
56+
57+
# RHS: multiplicative
58+
ax2 = fig.add_subplot(nrows,2,i*2+2, axisbg=tuple(rgb_geometric[0,i,:]))
59+
print tuple(lab_geometric[0,i,:])#, tuple(rgb_geometric[0,i,:])
60+
61+
# ylabels
62+
if i!=0:
63+
ax1.set_ylabel(str(1*i))
64+
ax2.set_ylabel(str(k))
65+
k *= 2
66+
67+
# Turn off ticks
68+
ax1.get_xaxis().set_ticks([])
69+
ax2.get_xaxis().set_ticks([])
70+
ax1.get_yaxis().set_ticks([])
71+
ax2.get_yaxis().set_ticks([])
72+
73+
# Turn off black edges
74+
ax1.spines['right'].set_visible(False)
75+
ax1.spines['top'].set_visible(False)
76+
ax1.spines['bottom'].set_visible(False)
77+
ax1.spines['left'].set_visible(False)
78+
ax2.spines['right'].set_visible(False)
79+
ax2.spines['top'].set_visible(False)
80+
ax2.spines['bottom'].set_visible(False)
81+
ax2.spines['left'].set_visible(False)
82+
83+
84+
# common ylabel
85+
ax1.text(-0.3, 3.8, 'Additional Parts Black',
86+
rotation=90, transform=ax1.transAxes)
87+
88+
89+
fig.subplots_adjust(hspace=0.0)
90+
plt.show()
91+
92+
fig.savefig('figures/albers.pdf')
93+
fig.savefig('figures/albers.png')
94+
95+
96+
97+
### Albers plot with linear scale black and white
98+
99+
nrows = 5
100+
ncols = 2#3
101+
102+
x = np.linspace(0.0, 1.0, 100)
103+
cmap = 'binary'
104+
105+
# Get binary colormap entries for full 100 entries
106+
rgb = cm.get_cmap(cmap)(x)[np.newaxis,:,:3]
107+
108+
# Sample 100-entry rgb additively and geometrically
109+
rgb_add = np.empty((1,nrows,3))
110+
rgb_geometric = np.empty((1,nrows,3))
111+
# rgb_cubic = np.empty((1,nrows,3))
112+
k = 1
113+
di = 8
114+
I0 = 5
115+
for i in xrange(nrows):
116+
# Do more blackness via increasing indices
117+
rgb_add[:,i,:] = rgb[:,i*di+I0,:]
118+
# rgb_cubic[:,i,:] = rgb[:,(I0**(1./3)+i*di**(1./3))**3,:]
119+
if i != 0:
120+
print i*di+I0, di*k+I0, (I0**(1./3)+i*di**(1./3))**3
121+
rgb_geometric[:,i,:] = rgb[:,I0+di*k,:]
122+
k *= 2
123+
elif i==0:
124+
print i*di+I0, I0, (I0**(1./3)+i*di**(1./3))**3
125+
rgb_geometric[:,i,:] = rgb[:,I0,:]
126+
127+
lab_add = color.rgb2lab(rgb_add)
128+
lab_geometric = color.rgb2lab(rgb_geometric)
129+
# lab_cubic = color.rgb2lab(rgb_cubic)
130+
131+
fig = plt.figure()
132+
k = 1
133+
for i in xrange(nrows):
134+
135+
# LHS: additive
136+
ax1 = fig.add_subplot(nrows,ncols,i*2+1, axisbg=tuple(rgb_add[0,i,:]))
137+
print tuple(lab_add[0,i,:])#, tuple(rgb_add[0,i,:])
138+
139+
# middle: multiplicative
140+
ax2 = fig.add_subplot(nrows,ncols,i*2+2, axisbg=tuple(rgb_geometric[0,i,:]))
141+
print tuple(lab_geometric[0,i,:])#, tuple(rgb_geometric[0,i,:])
142+
143+
# # RHS: cubic
144+
# ax3 = fig.add_subplot(nrows,ncols,i*2+3, axisbg=tuple(rgb_cubic[0,i,:]))
145+
146+
# ylabels
147+
if i!=0:
148+
ax1.set_ylabel(str(1*i))
149+
ax2.set_ylabel(str(k))
150+
k *= 2
151+
152+
# Turn off ticks
153+
ax1.get_xaxis().set_ticks([])
154+
ax2.get_xaxis().set_ticks([])
155+
ax1.get_yaxis().set_ticks([])
156+
ax2.get_yaxis().set_ticks([])
157+
158+
# Turn off black edges
159+
ax1.spines['right'].set_visible(False)
160+
ax1.spines['top'].set_visible(False)
161+
ax1.spines['bottom'].set_visible(False)
162+
ax1.spines['left'].set_visible(False)
163+
ax2.spines['right'].set_visible(False)
164+
ax2.spines['top'].set_visible(False)
165+
ax2.spines['bottom'].set_visible(False)
166+
ax2.spines['left'].set_visible(False)
167+
168+
169+
# common ylabel
170+
ax1.text(-0.3, 4.0, 'Steps through map indices',
171+
rotation=90, transform=ax1.transAxes)
172+
173+
174+
fig.subplots_adjust(hspace=0.0)
175+
plt.show()
176+
177+
fig.savefig('figures/albers-bw.pdf')
178+
fig.savefig('figures/albers-bw.png')
179+
180+
181+
## Make a binary colormap with geometric relationship
182+
183+
x = np.linspace(0.0, 1.0, 100)
184+
185+
L = np.zeros(100)
186+
L[1:] = 2**(x-1)

0 commit comments

Comments
 (0)