本文整理汇总了Python中sklearn.decomposition.FastICA.components_方法的典型用法代码示例。如果您正苦于以下问题:Python FastICA.components_方法的具体用法?Python FastICA.components_怎么用?Python FastICA.components_使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类sklearn.decomposition.FastICA
的用法示例。
在下文中一共展示了FastICA.components_方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: generate_components
# 需要导入模块: from sklearn.decomposition import FastICA [as 别名]
# 或者: from sklearn.decomposition.FastICA import components_ [as 别名]
def generate_components(
images,
hemi,
term_scores=None,
n_components=20,
random_state=42,
out_dir=None,
memory=Memory(cachedir="nilearn_cache"),
):
"""
images: list
Can be nibabel images, can be file paths.
"""
# Create grey matter mask from mni template
target_img = datasets.load_mni152_template()
# Reshape & mask images
print("%s: Reshaping and masking images; may take time." % hemi)
if hemi == "wb":
masker = GreyMatterNiftiMasker(target_affine=target_img.affine, target_shape=target_img.shape, memory=memory)
else: # R and L maskers
masker = HemisphereMasker(
target_affine=target_img.affine, target_shape=target_img.shape, memory=memory, hemisphere=hemi
)
masker = masker.fit()
# Images may fail to be transformed, and are of different shapes,
# so we need to trasnform one-by-one and keep track of failures.
X = [] # noqa
xformable_idx = np.ones((len(images),), dtype=bool)
for ii, im in enumerate(images):
img = cast_img(im, dtype=np.float32)
img = clean_img(img)
try:
X.append(masker.transform(img))
except Exception as e:
print("Failed to mask/reshape image %d/%s: %s" % (im.get("collection_id", 0), op.basename(im), e))
xformable_idx[ii] = False
# Now reshape list into 2D matrix
X = np.vstack(X) # noqa
# Run ICA and map components to terms
print("%s: Running ICA; may take time..." % hemi)
fast_ica = FastICA(n_components=n_components, random_state=random_state)
fast_ica = memory.cache(fast_ica.fit)(X.T)
ica_maps = memory.cache(fast_ica.transform)(X.T).T
# Tomoki's suggestion to normalize components_
# X ~ ica_maps * fast_ica.components_
# = (ica_maps * f) * (fast_ica.components_ / f)
# = new_ica_map * new_components_
C = fast_ica.components_
factor = np.sqrt(np.multiply(C, C).sum(axis=1, keepdims=True)) # (n_components x 1)
ica_maps = np.multiply(ica_maps, factor)
fast_ica.components_ = np.multiply(C, 1.0 / (factor + 1e-12))
if term_scores is not None:
terms = term_scores.keys()
term_matrix = np.asarray(term_scores.values())
term_matrix[term_matrix < 0] = 0
term_matrix = term_matrix[:, xformable_idx] # terms x images
# Don't use the transform method as it centers the data
ica_terms = np.dot(term_matrix, fast_ica.components_.T).T
# 2015/12/26 - sign matters for comparison, so don't do this!
# 2016/02/01 - sign flipping is ok for R-L comparison, but RL concat
# may break this.
# Pretty up the results
for idx, ic in enumerate(ica_maps):
if -ic.min() > ic.max():
# Flip the map's sign for prettiness
ica_maps[idx] = -ic
if term_scores:
ica_terms[idx] = -ica_terms[idx]
# Create image from maps, save terms to the image directly
ica_image = NiftiImageWithTerms.from_image(masker.inverse_transform(ica_maps))
if term_scores:
ica_image.terms = dict(zip(terms, ica_terms.T))
# Write to disk
if out_dir is not None:
out_path = op.join(out_dir, "%s_ica_components.nii.gz" % hemi)
if not op.exists(op.dirname(out_path)):
os.makedirs(op.dirname(out_path))
ica_image.to_filename(out_path)
return ica_image