def init_frontend(): nl = NeurolangPDL() nl.add_symbol( np.log, name="log", type_=Callable[[float], float], ) return nl
def init_frontend(): nl = NeurolangPDL() nl.add_symbol( np.log, name="log", type_=Callable[[float], float], ) @nl.add_symbol def one_way_chi2(llr: float) -> float: """ 2 * log(LR) follows a one-way chi2 distribution """ return scipy.special.chdtrc(1, 2 * llr) return nl
def init_frontend(): nl = NeurolangPDL() nl.add_symbol( np.log, name="log", type_=Callable[[float], float], ) nl.add_symbol( lambda it: float(sum(it)), name="agg_sum", type_=Callable[[Iterable], float], ) @nl.add_symbol def agg_count(*iterables) -> int: return len(next(iter(iterables))) return nl
# ############################################################################## # Data preparation # ---------------- # ############################################################################## # Load the MNI atlas and resample it to 4mm voxels mni_t1 = nibabel.load(nilearn.datasets.fetch_icbm152_2009()["t1"]) mni_t1_4mm = nilearn.image.resample_img(mni_t1, np.eye(3) * 2) # ############################################################################## # Probabilistic Logic Programming in NeuroLang # -------------------------------------------- nl = NeurolangPDL() # ############################################################################## # Adding new aggregation function to build a region overlay # ---------------------------------- @nl.add_symbol def agg_create_region_overlay( i: Iterable, j: Iterable, k: Iterable, p: Iterable ) -> ExplicitVBR: voxels = np.c_[i, j, k] return ExplicitVBROverlay( voxels, mni_t1_4mm.affine, p, image_dim=mni_t1_4mm.shape )
if label == 0: continue voxels = np.transpose((image_data == label).nonzero()) if voxels.shape[0] == 0: continue r = ExplicitVBR(voxels, image.affine, image_dim=image.shape) region_table.append((str(name.decode('utf8')), r)) ################################################## # Initialise the atlas to the Neurolang Engine # add two symbols to split left and right structures # in the Destrieux atlas and add the atlas. nl = NeurolangPDL() @nl.add_symbol def lh(x: str) -> bool: return x.startswith('L ') @nl.add_symbol def rh(x: str) -> bool: return x.startswith('R ') destrieux = nl.add_tuple_set(region_table, name='destrieux') with nl.environment as e:
Uploading the Destrieux regions NeuroLang and executing a simple query. ''' from matplotlib import pyplot as plt import nibabel as nib from nilearn import datasets, plotting from neurolang.frontend import NeurolangPDL ################################################## # Initialise the NeuroLang probabilistic engine. nl = NeurolangPDL() ############################################################################### # Load the Destrieux example from nilearn as a fact list atlas_destrieux = datasets.fetch_atlas_destrieux_2009() atlas_labels = { label: str(name.decode('utf8')) for label, name in atlas_destrieux['labels'] } nl.add_atlas_set('destrieux', atlas_labels, nib.load(atlas_destrieux['maps'])) ############################################################################### # Add utility functions to separate hemispheric regions
def init_frontend(): nl = NeurolangPDL() return nl
# ---------------- data_dir = Path.home() / "neurolang_data" # ############################################################################## # Load the MNI atlas and resample it to 4mm voxels mni_t1 = nibabel.load( nilearn.datasets.fetch_icbm152_2009(data_dir=str(data_dir / "icbm"))["t1"]) mni_t1_2mm = nilearn.image.resample_img(mni_t1, np.eye(3) * 2) # ############################################################################## # Probabilistic Logic Programming in NeuroLang # -------------------------------------------- nl = NeurolangPDL() # ############################################################################## # Adding new aggregation function to build a region overlay # ---------------------------------- @nl.add_symbol def agg_create_region_overlay(i: Iterable, j: Iterable, k: Iterable, p: Iterable) -> ExplicitVBR: voxels = np.c_[i, j, k] return ExplicitVBROverlay(voxels, mni_t1_2mm.affine, p, image_dim=mni_t1_2mm.shape)