<jupyter_start><jupyter_text>Masking Example===============This tutorial illustrates methods to help with masking data. Load Data---------First, let's load the pain data for this example.<jupyter_code>from nltools.datasets import fetch_pain

data = fetch_pain()<jupyter_output><empty_output><jupyter_text>Apply_Mask----------Spherical masks can be created using the create_sphere function.It requires specifying a center voxel and the radius of the sphere.<jupyter_code>from nltools.mask import create_sphere

mask = create_sphere([0, 0, 0], radius=30)
masked_data = data.apply_mask(mask)
masked_data.mean().plot()<jupyter_output><empty_output><jupyter_text>Extract Mean Within ROI-----------------------We can easily calculate the mean within an ROI for each image within a Brain_Data() instance using the extract_roi() method.<jupyter_code>import matplotlib.pyplot as plt

mean = data.extract_roi(mask)
plt.plot(mean)<jupyter_output><empty_output><jupyter_text>Expand and Contract ROIs------------------------Some masks have many ROIs indicated by a unique ID.  It is possible toexpand these masks into separate ROIs and also collapse them into a singleimage again.  Here we will demonstrate on a k=50 parcellation hosted onhttp://neurovault.org.<jupyter_code>from nltools.mask import expand_mask, collapse_mask
from nltools.data import Brain_Data

mask = Brain_Data('http://neurovault.org/media/images/2099/Neurosynth%20Parcellation_0.nii.gz')
mask.plot()<jupyter_output><empty_output><jupyter_text>We can expand this mask into 50 separate regions<jupyter_code>mask_x = expand_mask(mask)
mask_x.plot()<jupyter_output><empty_output><jupyter_text>We can collapse these 50 separate regions as unique values in a single image<jupyter_code>mask_c = collapse_mask(mask_x)
mask_c.plot()<jupyter_output><empty_output><jupyter_text>Threshold and Regions---------------------Images can be thresholded using an arbitrary cutoff or a percentile using the threshold method.  Here we calculate the mean of the high pain images andthreshold using the 95 percentile.<jupyter_code>import numpy as np

high = data[np.where(data.X['PainLevel']==3)[0]]
high.mean().threshold(threshold='95%').plot()<jupyter_output><empty_output><jupyter_text>We might be interested in creating a binary mask from this threshold.<jupyter_code>mask = high.mean().threshold(threshold='95%',binarize=True)
mask.plot()<jupyter_output><empty_output><jupyter_text>We might also want to create separate images from each contiguous ROI.<jupyter_code>region = high.mean().threshold(threshold='95%').regions()
region.plot()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.125, 'fraction_numerical': 0.010736196319018405, 'mean_word_length': 5.957333333333334, 'pattern_counts': {'":': 0, '<': 37, '<?xml version=': 0, '>': 37, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '9', 'dir': 'jupyter-structured-clean-dedup', 'id': '291903', 'license': 'MIT', 'n_tokens_mistral': 821, 'n_tokens_neox': 781, 'n_words': 221, 'path': 'Tutorials/Decoding/nltools_mask.ipynb', 'repo_name': 'smburns47/mind_2017', 'size': '2608'}