Skip to content

Commit 983eaff

Browse files
committed
Merge branch 'rename-fixes' into main-master
* rename-fixes: better figures for Bayesian structural analysis example debugged and improved the examples some cleaning in mask a nd statistical mapping (+pep8) renames and moved emp_null cleaning and renaming of the emp_null module moved utils.fast_distance to algorithms.routines removed a bad dependency fixed tiny bugs added a word of caution on clustering updated nibabel.gifti io updated the example in clustering comparison moved the clusting stuff from labs to algorithms moved algorithmic examples to algo subdir corrected mask-related bugs
2 parents 706536c + cc86fdf commit 983eaff

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

50 files changed

+349
-382
lines changed

doc/labs/plots/enn_demo.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
x = np.c_[np.random.normal(size=1e4),
55
np.random.normal(scale=4, size=1e4)]
66

7-
from nipy.labs.utils.emp_null import ENN
8-
enn = ENN(x)
7+
from nipy.algorithms.statistics.empirical_pvalue import NormalEmpiricalNull
8+
enn = NormalEmpiricalNull(x)
99
enn.threshold(verbose=True)
1010

examples/labs/bayesian_gaussian_mixtures.py renamed to examples/algorithms/bayesian_gaussian_mixtures.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@
1616
import numpy.random as nr
1717
import pylab as pl
1818

19-
import nipy.labs.clustering.bgmm as bgmm
20-
from nipy.labs.clustering.gmm import plot2D
19+
import nipy.algorithms.clustering.bgmm as bgmm
20+
from nipy.algorithms.clustering.gmm import plot2D
2121

2222

2323
dim = 2

examples/labs/clustering_comparisons.py renamed to examples/algorithms/clustering_comparisons.py

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,21 +10,18 @@
1010

1111
import numpy as np
1212
import numpy.random as nr
13-
import nipy.labs.graph.field as ff
14-
13+
from nipy.labs.graph.field import Field
14+
from scipy.ndimage import gaussian_filter
1515

1616
dx = 50
1717
dy = 50
1818
dz = 1
1919
nbseeds = 10
20-
F = ff.Field(dx * dy * dz)
20+
data = gaussian_filter( np.random.randn(dx, dy), 2)
21+
F = Field(dx * dy * dz)
2122
xyz = np.reshape(np.indices((dx, dy, dz)), (3, dx * dy * dz)).T.astype(np.int)
2223
F.from_3d_grid(xyz, 18)
23-
data = nr.randn(dx * dy * dz, 1)
24-
F.set_weights(F.get_weights() / 18)
2524
F.set_field(data)
26-
F.diffusion(5)
27-
data = F.get_field()
2825

2926
seeds = np.argsort(nr.rand(F.V))[:nbseeds]
3027
seeds, label, J0 = F.geodesic_kmeans(seeds)

examples/labs/gaussian_mixture_models.py renamed to examples/algorithms/gaussian_mixture_models.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
import numpy as np
1313

14-
import nipy.labs.clustering.gmm as gmm
14+
import nipy.algorithms.clustering.gmm as gmm
1515

1616

1717
dim = 2

examples/labs/ward_clustering.py renamed to examples/algorithms/ward_clustering.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
import matplotlib.pylab as mp
1212

1313
from nipy.labs import graph
14-
from nipy.labs.clustering.hierarchical_clustering import ward
14+
from nipy.algorithms.clustering.hierarchical_clustering import ward
1515

1616
# n = number of points, k = number of nearest neighbours
1717
n = 100
@@ -27,17 +27,17 @@
2727
threshold = .5 * n
2828
u = tree.partition(threshold)
2929

30-
mp.figure()
31-
mp.subplot(1, 2, 1)
30+
mp.figure(figsize=(12, 6))
31+
mp.subplot(1, 3, 1)
3232
for i in range(u.max()+1):
3333
mp.plot(X[u == i, 0], X[u == i, 1], 'o', color=(rand(), rand(), rand()))
3434

3535
mp.axis('tight')
3636
mp.axis('off')
37-
mp.title('clustering into clusters of inertia<%f' % threshold)
37+
mp.title('clustering into clusters \n of inertia < %g' % threshold)
3838

3939
u = tree.split(k)
40-
mp.subplot(1, 2, 2)
40+
mp.subplot(1, 3, 2)
4141
for e in range(G.E):
4242
mp.plot([X[G.edges[e, 0], 0], X[G.edges[e, 1], 0]],
4343
[X[G.edges[e, 0], 1], X[G.edges[e, 1], 1]], 'k')
@@ -61,7 +61,9 @@
6161
valid[tree.parents[v]]=1
6262
nv = np.sum(valid)
6363

64-
ax = tree.plot()
64+
ax = mp.subplot(1, 3, 3)
65+
ax = tree.plot(ax)
66+
ax.set_title('Dendrogram')
6567
ax.set_visible(True)
6668
mp.show()
6769

examples/labs/bayesian_structural_analysis.py

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -88,31 +88,36 @@ def make_bsa_2d(betas, theta=3., dmax=5., ths=0, thq=0.5, smin=0,
8888
AF.show()
8989

9090
group_map.shape = ref_dim
91-
mp.figure()
92-
mp.subplot(1, 3, 1)
91+
mp.figure(figsize=(8, 3))
92+
ax = mp.subplot(1, 3, 1)
9393
mp.imshow(group_map, interpolation='nearest', vmin=-1, vmax=lmax)
94-
mp.title('Blob separation map')
95-
mp.colorbar()
94+
mp.title('Blob separation map', fontsize=10)
95+
mp.axis('off')
96+
plop = mp.colorbar(shrink=.8)
9697

9798
if AF != None:
9899
group_map = AF.map_label(coord, 0.95, dmax)
99100
group_map.shape = ref_dim
100101

101102
mp.subplot(1, 3, 2)
102103
mp.imshow(group_map, interpolation='nearest', vmin=-1, vmax=lmax)
103-
mp.title('group-level position 95% \n confidence regions')
104-
mp.colorbar()
104+
mp.title('group-level position 95% \n confidence regions', fontsize=10)
105+
mp.axis('off')
106+
mp.colorbar(shrink=.8)
105107

106108
mp.subplot(1, 3, 3)
107109
likelihood.shape = ref_dim
108110
mp.imshow(likelihood, interpolation='nearest')
109-
mp.title('Spatial density under h1')
110-
mp.colorbar()
111+
mp.title('Spatial density under h1', fontsize=10)
112+
mp.axis('off')
113+
mp.colorbar(shrink=.8)
111114

112115

113-
mp.figure()
116+
fig_output = mp.figure(figsize=(8, 3.5))
117+
fig_output.text(.5, .9, "Individual landmark regions", ha="center")
114118
for s in range(nsubj):
115-
mp.subplot(nsubj / 5, 5, s + 1)
119+
ax = mp.subplot(nsubj / 5, 5, s + 1)
120+
#ax.set_position([.02, .02, .96, .96])
116121
lw = - np.ones(ref_dim)
117122
if BF[s] is not None:
118123
nls = BF[s].get_roi_feature('label')
@@ -123,9 +128,11 @@ def make_bsa_2d(betas, theta=3., dmax=5., ths=0, thq=0.5, smin=0,
123128
mp.imshow(lw, interpolation='nearest', vmin=-1, vmax=lmax)
124129
mp.axis('off')
125130

126-
mp.figure()
131+
fig_input = mp.figure(figsize=(8, 3.5))
132+
fig_input.text(.5,.9, "Input activation maps", ha='center')
127133
for s in range(nsubj):
128-
mp.subplot(nsubj / 5, 5, s + 1)
134+
ax = mp.subplot(nsubj / 5, 5, s + 1)
135+
#ax.set_position([.02, .02, .96, .96])
129136
mp.imshow(betas[s], interpolation='nearest', vmin=betas.min(),
130137
vmax=betas.max())
131138
mp.axis('off')

examples/labs/demo_dmtx.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -47,16 +47,15 @@
4747
fir_delays=range(1, 6))
4848

4949
# plot the results
50-
fig = mp.figure()
50+
fig = mp.figure(figsize=(10, 6))
5151
ax = mp.subplot(1, 3, 1)
5252
X1.show(ax=ax)
53-
ax.set_title('example of event-related design matrix')
53+
ax.set_title('Event-related design matrix', fontsize=12)
5454
ax = mp.subplot(1, 3, 2)
5555
X2.show(ax=ax)
56-
ax.set_title('example of block design matrix')
56+
ax.set_title('Block design matrix', fontsize=12)
5757
ax = mp.subplot(1, 3, 3)
5858
X3.show(ax=ax)
59-
ax.set_title('example of FIR design matrix')
59+
ax.set_title('FIR design matrix', fontsize=12)
6060
mp.subplots_adjust(top=0.9, bottom=0.25)
61-
fig.set_size_inches((12, 6))
6261
mp.show()

examples/labs/histogram_fits.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
import numpy as np
1818

1919
import nipy.labs.utils.simul_multisubject_fmri_dataset as simul
20-
import nipy.labs.utils.emp_null as en
20+
import nipy.algorithms.statistics.empirical_pvalue as en
2121

2222
###############################################################################
2323
# simulate the data
@@ -82,7 +82,7 @@
8282
###############################################################################
8383
# Fit the null mode of Beta with an empirical normal null
8484

85-
efdr = en.ENN(Beta)
85+
efdr = en.NormalEmpiricalNull(Beta)
8686
emp_null_fdr = efdr.fdr(Beta)
8787
emp_null_fdr = emp_null_fdr.reshape((dimx, dimy))
8888

examples/labs/multi_subject_parcellation.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,14 +41,14 @@
4141
for s in range(nsubj)])
4242

4343
import matplotlib.pylab as mp
44-
mp.figure()
44+
mp.figure(figsize=(8, 4))
4545
mp.title('Input data')
4646
for s in range(nsubj):
4747
mp.subplot(2, 5, s + 1)
4848
mp.imshow(dataset[s], interpolation='nearest')
4949
mp.axis('off')
5050

51-
mp.figure()
51+
mp.figure(figsize=(8, 4))
5252
mp.title('Resulting parcels')
5353
for s in range(nsubj):
5454
mp.subplot(2, 5, s+1)

examples/labs/need_data/bayesian_structural_analysis.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,12 +40,12 @@
4040
subj_id = ['%04d' %i for i in range(12)]
4141
theta = float(stats.t.isf(0.01, 100))
4242
dmax = 4.
43-
ths = 0 #2# or nbsubj/4
43+
ths = 0
4444
thq = 0.95
4545
verbose = 1
4646
smin = 5
4747
swd = tempfile.mkdtemp()
48-
method = 'simple'
48+
method = 'quick'
4949
print 'method used:', method
5050

5151
# call the function

examples/labs/need_data/example_localizer_glm.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,10 @@
6969
design_matrix = dm.DesignMatrix( frametimes, paradigm, hrf_model=hrf_model,
7070
drift_model=drift_model, hfcut=hfcut)
7171

72-
design_matrix.show()
72+
ax = design_matrix.show()
73+
ax.set_position([.05, .25, .9, .65])
74+
ax.set_title('Design matrix')
75+
7376
pylab.savefig(op.join(swd, 'design_matrix.png'))
7477
# design_matrix.save(...)
7578

examples/labs/need_data/histogram_fits.py

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,17 +14,17 @@
1414
import numpy as np
1515
import matplotlib.pylab as mp
1616
import scipy.stats as st
17-
import nipy.labs.utils.emp_null as en
18-
import get_data_light
1917

2018
from nibabel import load
19+
import nipy.algorithms.statistics.empirical_pvalue as en
20+
import get_data_light
21+
2122

2223
# parameters
2324
verbose = 1
24-
theta = float(st.t.isf(0.01,100))
25+
theta = float(st.t.isf(0.01, 100))
2526

2627
# paths
27-
#data_dir = get_data_light.get_it()
2828
data_dir = os.path.expanduser( os.path.join( '~', '.nipy', 'tests', 'data'))
2929
mask_image = os.path.join(data_dir, 'mask.nii.gz')
3030
input_image = os.path.join(data_dir, 'spmT_0029.nii.gz')
@@ -38,27 +38,29 @@
3838
# read the functional image
3939
rbeta = load(input_image)
4040
beta = rbeta.get_data()
41-
beta = beta[mask>0]
41+
beta = beta[mask > 0]
4242

43-
mf = mp.figure()
43+
mf = mp.figure(figsize=(13,5))
4444
a1 = mp.subplot(1, 3, 1)
4545
a2 = mp.subplot(1, 3, 2)
4646
a3 = mp.subplot(1, 3, 3)
4747

4848
# fit beta's histogram with a Gamma-Gaussian mixture
4949
bfm = np.array([2.5, 3.0, 3.5, 4.0, 4.5])
50-
bfp = en.Gamma_Gaussian_fit(beta, bfm, verbose=2, mpaxes=a1)
50+
bfp = en.Gamma_Gaussian_fit(beta, bfm, verbose=1, mpaxes=a1)
5151

5252
# fit beta's histogram with a mixture of Gaussians
5353
alpha = 0.01
5454
pstrength = 100
5555
bfq = en.three_classes_GMM_fit(beta, bfm, alpha, pstrength,
56-
verbose=2, mpaxes=a2)
56+
verbose=1, mpaxes=a2)
5757

5858
# fit the null mode of beta with the robust method
59-
efdr = en.ENN(beta)
59+
efdr = en.NormalEmpiricalNull(beta)
6060
efdr.learn()
6161
efdr.plot(bar=0, mpaxes=a3)
6262

63-
mf.set_size_inches(15, 5)
63+
a1.set_title('Fit of the density with \n a Gamma-Gaussian mixture')
64+
a2.set_title('Fit of the density with \n a mixture of Gaussians')
65+
a3.set_title('Robust fit of the density \n with a single Gaussian')
6466
mp.show()

nipy/labs/clustering/__init__.py renamed to nipy/algorithms/clustering/__init__.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
# emacs: -*- mode: python; py-indent-offset: 4; indent-tabs-mode: nil -*-
22
# vi: set ft=python sts=4 ts=4 sw=4 et:
3-
#from clustering import *
3+
"""
4+
This sub-package contains functions for clustering.
5+
It might be removed in the future, and replaced
6+
by an optional dependence on scikit learn.
7+
"""
48

59
from nipy.testing import Tester
610

File renamed without changes.

nipy/labs/clustering/ggmixture.py renamed to nipy/algorithms/clustering/ggmixture.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
import numpy.random as nr
1616

1717

18-
1918
#############################################################################
2019
# Auxiliary functions #######################################################
2120
#############################################################################
@@ -445,15 +444,14 @@ def init_fdr(self, x, dof=-1, copy=True):
445444
x = x.copy()
446445
# positive gamma
447446
i = np.ravel(np.nonzero(x > 0))
448-
from ..utils import emp_null as en
449-
lfdr = en.FDR(x)
447+
from ..statistics.empirical_pvalue import FDR
450448

451449
if np.size(i) > 0:
452450
if dof < 0:
453451
pvals = st.norm.sf(x)
454452
else:
455453
pvals = st.t.sf(x, dof)
456-
q = lfdr.all_fdr_from_pvals(pvals)
454+
q = FDR().all_fdr(pvals)
457455
z = 1 - q[i]
458456
self.mixt[2] = np.maximum(0.5, z.sum()) / np.size(x)
459457
self.shape_p, self.scale_p = _gam_param(x[i], z)
@@ -467,7 +465,7 @@ def init_fdr(self, x, dof=-1, copy=True):
467465
pvals = st.norm.cdf(x)
468466
else:
469467
pvals = st.t.cdf(x, dof)
470-
q = lfdr.all_fdr_from_pvals(pvals)
468+
q = FDR().all_fdr(pvals)
471469
z = 1 - q[i]
472470
self.shape_n, self.scale_n = _gam_param( - x[i], z)
473471
self.mixt[0] = np.maximum(0.5, z.sum()) / np.size(x)

nipy/labs/clustering/gmm.py renamed to nipy/algorithms/clustering/gmm.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -883,7 +883,8 @@ def show(self, x, gd, density=None, axes=None):
883883
axes = pylab.figure()
884884

885885
if gd.dim == 1:
886-
from ..utils.emp_null import smoothed_histogram_from_samples
886+
from ..statistics.empirical_pvalue import \
887+
smoothed_histogram_from_samples
887888
h, c = smoothed_histogram_from_samples(x, normalized=True)
888889
offset = (c.max() - c.min()) / (2 * c.size)
889890
grid = gd.make_grid()

nipy/labs/clustering/hierarchical_clustering.py renamed to nipy/algorithms/clustering/hierarchical_clustering.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,9 @@
2626

2727
import numpy as np
2828

29-
from ..graph.graph import WeightedGraph
30-
from ..graph.forest import Forest
31-
from ..utils.fast_distance import euclidean_distance
29+
from nipy.labs.graph.graph import WeightedGraph
30+
from nipy.labs.graph.forest import Forest
31+
from ..routines.fast_distance import euclidean_distance
3232

3333

3434
class WeightedForest(Forest):
@@ -131,6 +131,7 @@ def plot(self, ax=None):
131131
-------
132132
ax, the axis handle
133133
"""
134+
import matplotlib.pylab as mp
134135
if self.check_compatible_height() == False:
135136
raise ValueError('cannot plot myself in my current state')
136137

@@ -155,7 +156,6 @@ def plot(self, ax=None):
155156

156157
# 3. plot
157158
if ax == None:
158-
import matplotlib.pylab as mp
159159
mp.figure()
160160
ax = mp.subplot(1, 1, 1)
161161

File renamed without changes.
File renamed without changes.

nipy/labs/clustering/tests/test_hierarchical_clustering.py renamed to nipy/algorithms/clustering/tests/test_hierarchical_clustering.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@
1919
ward, ward_simple, ward_quick,
2020
ward_segment, ward_field_segment,
2121
ward_quick_segment)
22-
from ...graph.graph import WeightedGraph
23-
from ...graph.field import Field
22+
from nipy.labs.graph.graph import WeightedGraph
23+
from nipy.labs.graph.field import Field
2424

2525
def alc_test_basic():
2626
np.random.seed(0)

nipy/algorithms/routines/__init__.py

Whitespace-only changes.

0 commit comments

Comments
 (0)