<commit_before>import numpy as np
from scipy.ndimage import map_coordinates
from dipy.reconst.recspeed import pdf_to_odf
from scipy.fftpack import fftn, fftshift
from .odf import OdfModel, OdfFit
from .cache import Cache


"""
###############################################
# MODULE TEMPORARILY DISABLED FOR REFACTORING #
###############################################

import nose
class UnderConstruction(nose.SkipTest):
    pass

raise UnderConstruction()

"""

class DiffusionSpectrumFit(OdfFit):
    """ DSI directions and ODF class 
    """ 

    def __init__(self, model, data):
        self.model = model
        self.data = data
        self.qgrid_sz = self.model.qgrid_size
        self.dn = self.model.dn 
    def precompute_interp_coords(self, vertices):
        interp_coords = []
        for m in range(self.odfn):
            xi = self.model.origin + self.model.qradius * vertices[m, 0]
            yi = self.model.origin + self.model.qradius * vertices[m, 1]
            zi = self.model.origin + self.model.qradius * vertices[m, 2]
            interp_coords.append(np.vstack((xi, yi, zi)).T)
        return np.concatenate(interp_coords).T

    def pdf(self, data):
        values = data * self.model.filter
        #create the signal volume
        Sq = np.zeros((self.qgrid_sz, self.qgrid_sz, self.qgrid_sz))
        #fill q-space
        for i in range(self.dn):
            qx, qy, qz = self.model.qgrid[i]
            Sq[qx, qy, qz] += values[i]
        #apply fourier transform
        Pr=fftshift(np.abs(np.real(fftn(fftshift(Sq),
                                        (self.qgrid_sz, self.qgrid_sz, self.qgrid_sz)))))
        return Pr

    def pdf_odf(self, Pr):
        odf = np.zeros(self.odfn)
        ''' 
        for m in range(self.odfn):
            xi=self.origin+self.radius*self.odf_vertices[m,0]
            yi=self.origin+self.radius*self.odf_vertices[m,1]
            zi=self.origin+self.radius*self.odf_vertices[m,2]
            PrI=map_coordinates(Pr,np.vstack((xi,yi,zi)),order=1)
            for i in range(self.radiusn):
                odf[m]=odf[m]+PrI[i]*self.radius[i]**2
        '''
        PrIs = map_coordinates(Pr, self.interp_coords, order=1)
        ''' 
        for m in range(self.odfn):
            for i in range(self.radiusn):
                odf[m]=odf[m]+PrIs[m*self.radiusn+i]*self.radius[i]**2
        '''
        pdf_to_odf(odf, PrIs, self.model.qradius, self.odfn, self.model.qradiusn)
        return odf

    def odf(self, sphere):
        self.odfn = len(sphere.vertices)
        self.interp_coords = self.model.cache_get('interpolated coords',
                                             key='interp_coords')
        if self.interp_coords is None:
            self.interp_coords = self.precompute_interp_coords(sphere.vertices)
            self.model.cache_set('interpolated coords', 'interp_coords', self.interp_coords)
        Pr = self.pdf(self.data)
        #calculate the orientation distribution function
        odf = self.pdf_odf(Pr)
        return odf


class DiffusionSpectrumModel(OdfModel, Cache):
    """ DSI Assumptions """

    def __init__(self, gtab):
        self.bvals = gtab.bvals<commit_msg>Cache needed the sphere object from the odf as the key parameter<commit_after>import numpy as np
from scipy.ndimage import map_coordinates
from dipy.reconst.recspeed import pdf_to_odf
from scipy.fftpack import fftn, fftshift
from .odf import OdfModel, OdfFit
from .cache import Cache


"""
###############################################
# MODULE TEMPORARILY DISABLED FOR REFACTORING #
###############################################

import nose
class UnderConstruction(nose.SkipTest):
    pass

raise UnderConstruction()

"""

class DiffusionSpectrumFit(OdfFit):
    """ DSI directions and ODF class 
    """ 

    def __init__(self, model, data):
        self.model = model
        self.data = data
        self.qgrid_sz = self.model.qgrid_size
        self.dn = self.model.dn 
    def precompute_interp_coords(self, vertices):
        interp_coords = []
        for m in range(self.odfn):
            xi = self.model.origin + self.model.qradius * vertices[m, 0]
            yi = self.model.origin + self.model.qradius * vertices[m, 1]
            zi = self.model.origin + self.model.qradius * vertices[m, 2]
            interp_coords.append(np.vstack((xi, yi, zi)).T)
        return np.concatenate(interp_coords).T

    def pdf(self, data):
        values = data * self.model.filter
        #create the signal volume
        Sq = np.zeros((self.qgrid_sz, self.qgrid_sz, self.qgrid_sz))
        #fill q-space
        for i in range(self.dn):
            qx, qy, qz = self.model.qgrid[i]
            Sq[qx, qy, qz] += values[i]
        #apply fourier transform
        Pr=fftshift(np.abs(np.real(fftn(fftshift(Sq),
                                        (self.qgrid_sz, self.qgrid_sz, self.qgrid_sz)))))
        return Pr

    def pdf_odf(self, Pr):
        odf = np.zeros(self.odfn)
        ''' 
        for m in range(self.odfn):
            xi=self.origin+self.radius*self.odf_vertices[m,0]
            yi=self.origin+self.radius*self.odf_vertices[m,1]
            zi=self.origin+self.radius*self.odf_vertices[m,2]
            PrI=map_coordinates(Pr,np.vstack((xi,yi,zi)),order=1)
            for i in range(self.radiusn):
                odf[m]=odf[m]+PrI[i]*self.radius[i]**2
        '''
        PrIs = map_coordinates(Pr, self.interp_coords, order=1)
        ''' 
        for m in range(self.odfn):
            for i in range(self.radiusn):
                odf[m]=odf[m]+PrIs[m*self.radiusn+i]*self.radius[i]**2
        '''
        pdf_to_odf(odf, PrIs, self.model.qradius, self.odfn, self.model.qradiusn)
        return odf

    def odf(self, sphere):
        self.odfn = len(sphere.vertices)
        self.interp_coords = self.model.cache_get('interpolated coords',
                                             key=sphere)
        if self.interp_coords is None:
            self.interp_coords = self.precompute_interp_coords(sphere.vertices)
            self.model.cache_set('interpolated coords', sphere, self.interp_coords)
        Pr = self.pdf(self.data)
        #calculate the orientation distribution function
        odf = self.pdf_odf(Pr)
        return odf


class DiffusionSpectrumModel(OdfModel, Cache):
    """ DSI Assumptions """

    def __init__(self, gtab):
        self.bvals = gtab.bvals
{'fraction_non_alphanumeric': 0.14225418164764733, 'fraction_numerical': 0.003126465530717524, 'mean_word_length': 2.3887711864406778, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 6, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5730556', 'n_tokens_mistral': 2197, 'n_tokens_neox': 2119, 'n_words': 410}