Example #1
0
    def ComputeJacobianMatrix(self,
                              vec_x,
                              vec_xi=None,
                              rep_loc=None):  #need validation
        """
        Calcul l'inverse du Jacobien aux points de gauss dans le cas d'un élément isoparamétrique (c'est à dire que les mêmes fonctions de forme sont utilisées)
        vec_xi est un tableau dont les lignes donnent les coordonnées dans le repère de référence où on souhaite avoir le jacobien (en général pg)
        """
        if vec_xi is None: vec_xi = self.xi_pg
        self.ComputeDetJacobian(vec_x, vec_xi)

        if rep_loc != None:
            rep_pg = self.interpolateLocalFrame(
                rep_loc,
                vec_xi)  #interpolation du repère local aux points de gauss
            self.JacobianMatrix = sp.matmul(
                self.JacobianMatrix, sp.swapaxes(rep_pg, 2, 3)
            )  #to verify - sp.swapaxes(rep_pg,2,3) is equivalent to a transpose over the axis 2 and 3
#            for k,J in enumerate(self.JacobianMatrix): self.JacobianMatrix[k] = sp.dot(J, rep_pg[k].T)

        if self.JacobianMatrix.shape[-2] == self.JacobianMatrix.shape[-1]:
            self.inverseJacobian = linalg.inv(self.JacobianMatrix)
#            self.inverseJacobian = [linalg.inv(J) for J in self.JacobianMatrix]
        else:  #l'espace réel est dans une dimension plus grande que l'espace de l'élément de référence
            J = self.JacobianMatrix
            JT = sp.swapaxes(self.JacobianMatrix, 2, 3)
            self.inverseJacobian = sp.matmul(
                JT, linalg.inv(sp.matmul(J, JT))
            )  #inverseJacobian.shape = (Nel,len(vec_xi)=nb_pg, dim:vec_x.shape[-1], dim:vec_xi.shape[-1])
Example #2
0
    def _update_images(self):
        """ Updates the image data in self.plotdata to correspond to the 
        slices given.
        """
        cube = self.colorcube
        pd = self.plotdata
        pdVoxel = self.plotdataVoxel
        pdVoxelFFT = self.plotdataVoxelFFT
        pdPC = self.plotdataPC
        # These are transposed because img_plot() expects its data to be in 
        # row-major order
        pd.set_data("yz", sp.swapaxes(cube[self.slice_x, :, :],0,1))
        pd.set_data("xz", sp.swapaxes(cube[:, self.slice_y, :],0,1))
        pd.set_data("xy", cube[:,:,self.slice_z])
        
        pdVoxel.set_data("TimeVoxel", self.Voxel)
        aTime = num.zeros(self.num_figs)
        aTime[:] = num.nan
        aTime[self.pos_t]= self.Voxel[self.pos_t]
        pdVoxel.set_data("time", aTime)

        pcTime = num.zeros(self.num_figs)
        pcTime[:] = num.nan
        pcTime[self.pos_t]= self.principalComponent[self.pos_t]
        pdPC.set_data("Principal Component",self.principalComponent)
        pdPC.set_data("time", pcTime)

        pdVoxelFFT.set_data("FreqVoxel",self.FFTVoxel)
        aFreq = num.zeros(self.frqs.shape)
        aFreq[:] = num.nan
        aFreq[list(self.maxtab[:,0])]= self.fftPeaks
        pdVoxelFFT.set_data("peaks", aFreq)
Example #3
0
def align_image_with_openpnm(im):
    r"""
    Rotates an image to agree with the coordinates used in OpenPNM.  It is
    unclear why they are not in agreement to start with.  This is necessary
    for overlaying the image and the network in Paraview.

    Parameters
    ----------
    im : ND-array
        The image to be rotated.  Can be the Boolean image of the pore space or
        any other image of interest.

    Returns
    -------
    image : ND-array
        Returns a copy of ``im`` rotated accordingly.
    """
    im = sp.copy(im)
    if im.ndim == 2:
        im = (sp.swapaxes(im, 1, 0))
        im = im[-1::-1, :]
    elif im.ndim == 3:
        im = (sp.swapaxes(im, 2, 0))
        im = im[:, -1::-1, :]
    return im
Example #4
0
        def errfunc(pars, residuals=False):
            stat_tot = 0
            #for i in range(len(table.field('MAG_' + magtype + '-' + complist[0][0][0])[:100])):
            #print i
            #print 'MAG_' + magtype + '-' + a
            #print a,b
            #print table.field('MAG_ISO-' + a)
            #print magtype, 'MAG_' + magtype + '-' + a

            if 1:
                A_zp = scipy.swapaxes(
                    scipy.swapaxes(
                        scipy.array(loci * [
                            stars_good *
                            [[assign_zp(a[0][1], pars, zps) for a in complist]]
                        ]), 0, 1), 0, 0)
                B_zp = scipy.swapaxes(
                    scipy.swapaxes(
                        scipy.array(loci * [
                            stars_good *
                            [[assign_zp(a[1][1], pars, zps) for a in complist]]
                        ]), 0, 1), 0, 0)
                colors_zp = A_zp - B_zp
                #print colors_zp.shape
                #print locus_matrix.shape
                #print colors.shape
                #print colors_zp[0][:][0]

                #print colors[2][0], colors.shape
                #print locus_matrix[2][0], locus_matrix.shape
                print colors_zp.shape, colors.shape, locus_matrix.shape

                ds_prelim = ((colors - locus_matrix - colors_zp)**2.)
                ds_prelim[good == 0] = 0.
                #print ds_prelim[2][0], 'ds_prelim'
                ds = (ds_prelim.sum(axis=2))**0.5
                #print ds[2][0]
                ''' formula from High 2009 '''
                dotprod = abs((colors - locus_matrix - colors_zp) * colors_err)
                dotprod[
                    good ==
                    0] = 0.  # set error to zero for poor measurements not in fit
                dotprod_sum = dotprod.sum(axis=2)

                sum_diff = ds**2. / dotprod_sum
                #sum_diff = ds / colors_err
                #print sum_diff[2], 'sum_diff'
                #print c_diff[2][0], 'c_diff'
                dist = ds.min(axis=1)
                select_diff = sum_diff.min(axis=1)
                #print select_diff, 'select_diff'
                #select_diff_norm = select_diff #/star_mag_num
                #print select_diff_norm, 'select_diff_norm'
                stat_tot = select_diff.sum()
                #print stat_tot, 'stat_tot'

            print pars, stat_tot  #, zps
            print zps_list_full
            if residuals: return select_diff, dist
            else: return stat_tot
Example #5
0
        def plot(pars):

            A_zp = scipy.swapaxes(scipy.swapaxes(scipy.array(loci*[stars_good*[[assign_zp(a[0][1],pars,zps) for a in complist]]]),0,1),0,0)
            B_zp = scipy.swapaxes(scipy.swapaxes(scipy.array(loci*[stars_good*[[assign_zp(a[1][1],pars,zps) for a in complist]]]),0,1),0,0)
            colors_zp = A_zp- B_zp
            import pylab                                                           
            pylab.clf()
            for i in range(len(complist)):
                print complist[i], i


            x_color = scipy.array((colors - colors_zp)[:,0,2].tolist())
            y_color = (colors - colors_zp)[:,0,0]
            x_err = (colors_err)[:,0,2]
            y_err = (colors_err)[:,0,0]
                                                                                    
            x_color = x_color[x_err<100]
            y_color = y_color[x_err<100]
            y_err = y_err[x_err<100]
            x_err = x_err[x_err<100]
                                                                                    
            x_color = x_color[y_err<100]
            y_color = y_color[y_err<100]
            x_err = x_err[y_err<100]
            y_err = y_err[y_err<100]
                                                                                    
            print len(x_color), len(x_color) 
            #raw_input()
                                                                                    
                                                                                    
            pylab.scatter(x_color,y_color)
            pylab.errorbar(x_color,y_color,xerr=x_err,yerr=y_err,fmt=None)
            pylab.scatter(locus_matrix[0,:,2],locus_matrix[0,:,0],color='red')
                                                                                    
            pylab.show()
Example #6
0
def align_image_with_openpnm(im):
    r"""
    Rotates an image to agree with the coordinates used in OpenPNM.  It is
    unclear why they are not in agreement to start with.  This is necessary
    for overlaying the image and the network in Paraview.

    Parameters
    ----------
    im : ND-array
        The image to be rotated.  Can be the Boolean image of the pore space or
        any other image of interest.

    Returns
    -------
    image : ND-array
        Returns a copy of ``im`` rotated accordingly.
    """
    if im.ndim != im.squeeze().ndim:
        warnings.warn('Input image conains a singleton axis:' + str(im.shape) +
                      ' Reduce dimensionality with np.squeeze(im) to avoid' +
                      ' unexpected behavior.')
    im = sp.copy(im)
    if im.ndim == 2:
        im = (sp.swapaxes(im, 1, 0))
        im = im[-1::-1, :]
    elif im.ndim == 3:
        im = (sp.swapaxes(im, 2, 0))
        im = im[:, -1::-1, :]
    return im
Example #7
0
def watershed5(i_d, logger):
    # compute neighbourslocal_min = (filters.minimum_filter(arr, size=3)==arr)
    logger.info('Computing differences / edges...')
    nbs = compute_neighbours_max_border(i_d)

    # compute min altitude map
    logger.info('Computing minimal altitude map...')
    minaltitude = nbs[0]
    for nb in nbs[1:]:
        minaltitude = scipy.minimum(minaltitude, nb)

    logger.info('Swapping axes...')
    nbs = scipy.swapaxes(nbs, 0, 1)
    nbs = scipy.swapaxes(nbs, 1, 2)
    nbs = scipy.swapaxes(nbs, 2, 3)

    # compute relevant neighbours
    logger.info('Computing relevant neighbours...')
    neighbours = []
    for x in range(i_d.shape[0]):
        x_list = []
        neighbours.append(x_list)
        nbs_x = nbs[x]
        for y in range(i_d.shape[1]):
            y_list = []
            x_list.append(y_list)
            nbs_y = nbs_x[y]
            for z in range(i_d.shape[2]):
                z_set = set()
                y_list.append(z_set)
                nbs_z = nbs_y[z]
                nbs_idx = get_nbs_idx3((
                    x, y,
                    z))  # do not care about borders, their value is set to max
                min_value = minaltitude[x, y, z]
                for idx, nb in nbs_idx:
                    if nbs_z[idx] == min_value:
                        z_set.add(nb)

    # watershed
    logger.info(
        'Watershed \w minaltitude and relevant neighbours as list pre-computation...'
    )
    result = scipy.zeros(i_d.shape, dtype=scipy.uint16)
    nb_labs = 0
    for x in range(result.shape[0]):
        for y in range(result.shape[1]):
            for z in range(result.shape[2]):
                if result[x, y, z] != 0: continue  # 10%
                L, lab = stream_neighbours2_set(i_d, result, minaltitude,
                                                neighbours, (x, y, z))  # 90%
                if -1 == lab:
                    nb_labs += 1
                    for p in L:
                        result[p] = nb_labs
                else:
                    for p in L:
                        result[p] = lab

    return result
Example #8
0
    def ComputeJacobianMatrix(self, vec_x, vec_xi=None, rep_loc=None):
        if vec_xi is None: vec_xi == self.xi_pg
        self.ComputeDetJacobian(vec_x, vec_xi)

        if self.JacobianMatrix.shape[-2] == self.JacobianMatrix.shape[-1]:
            if rep_loc != None:
                rep_pg = self.interpolateLocalFrame(
                    rep_loc, vec_xi
                )  #interpolation du repère local aux points de gauss  -> shape = (len(vec_x),len(vec_xi),dim,dim)
                self.JacobianMatrix = sp.matmul(
                    self.JacobianMatrix, sp.swapaxes(rep_pg, 2, 3)
                )  #to verify - sp.swapaxes(rep_pg,2,3) is equivalent to a transpose over the axis 2 and 3
#                rep_pg = self.interpolateLocalFrame(rep_loc, vec_xi) #interpolation du repère local aux points de gauss
#                for k,J in enumerate(self.JacobianMatrix): self.JacobianMatrix[k] = sp.dot(J, rep_pg[k].T)
        else:  #l'espace réel est dans une dimension plus grande que l'espace de l'élément de référence
            if rep_loc is None:
                J = self.JacobianMatrix
                JT = sp.swapaxes(self.JacobianMatrix, 2, 3)
                self.inverseJacobian = sp.matmul(
                    JT, linalg.inv(sp.matmul(J, JT))
                )  #inverseJacobian.shape = (Nel,len(vec_xi)=nb_pg, dim:vec_x.shape[-1], dim:vec_xi.shape[-1])
                #                self.inverseJacobian = [sp.dot(J.T , linalg.inv(sp.dot(J,J.T))) for J in self.JacobianMatrix]   #this line may have a high computational cost
                return
            else:
                rep_pg = self.repLocFromJac(rep_loc, vec_xi)[:, 0:2, :]
                for k, J in enumerate(self.JacobianMatrix):
                    self.JacobianMatrix[k] = sp.dot(J, rep_pg[k].T)
        self.inverseJacobian = linalg.inv(self.JacobianMatrix)
Example #9
0
    def _update_images(self):
        """ Updates the image data in self.plotdata to correspond to the 
        slices given.
        """
        cube = self.colorcube
        pd = self.plotdata
        pdVoxel = self.plotdataVoxel
        pdVoxelFFT = self.plotdataVoxelFFT
        pdPC = self.plotdataPC
        # These are transposed because img_plot() expects its data to be in
        # row-major order
        pd.set_data("yz", sp.swapaxes(cube[self.slice_x, :, :], 0, 1))
        pd.set_data("xz", sp.swapaxes(cube[:, self.slice_y, :], 0, 1))
        pd.set_data("xy", cube[:, :, self.slice_z])

        pdVoxel.set_data("TimeVoxel", self.Voxel)
        aTime = num.zeros(self.num_figs)
        aTime[:] = num.nan
        aTime[self.pos_t] = self.Voxel[self.pos_t]
        pdVoxel.set_data("time", aTime)

        pcTime = num.zeros(self.num_figs)
        pcTime[:] = num.nan
        pcTime[self.pos_t] = self.principalComponent[self.pos_t]
        pdPC.set_data("Principal Component", self.principalComponent)
        pdPC.set_data("time", pcTime)

        pdVoxelFFT.set_data("FreqVoxel", self.FFTVoxel)
        aFreq = num.zeros(self.frqs.shape)
        aFreq[:] = num.nan
        aFreq[list(self.maxtab[:, 0])] = self.fftPeaks
        pdVoxelFFT.set_data("peaks", aFreq)
def watershed5(i_d, logger):
    # compute neighbourslocal_min = (filters.minimum_filter(arr, size=3)==arr)
    logger.info('Computing differences / edges...')
    nbs = compute_neighbours_max_border(i_d)
    
        # compute min altitude map
    logger.info('Computing minimal altitude map...')
    minaltitude = nbs[0]
    for nb in nbs[1:]:
        minaltitude = scipy.minimum(minaltitude, nb)
        
    logger.info('Swapping axes...')   
    nbs = scipy.swapaxes(nbs, 0, 1)
    nbs = scipy.swapaxes(nbs, 1, 2)
    nbs = scipy.swapaxes(nbs, 2, 3)
        
    # compute relevant neighbours
    logger.info('Computing relevant neighbours...')
    neighbours = []
    for x in range(i_d.shape[0]):
        x_list = []
        neighbours.append(x_list)
        nbs_x = nbs[x]
        for y in range(i_d.shape[1]):
            y_list = []
            x_list.append(y_list)
            nbs_y = nbs_x[y]
            for z in range(i_d.shape[2]):
                z_set = set()
                y_list.append(z_set)
                nbs_z = nbs_y[z]
                nbs_idx = get_nbs_idx3((x,y,z)) # do not care about borders, their value is set to max
                min_value = minaltitude[x, y, z]
                for idx, nb in nbs_idx:
                    if nbs_z[idx] == min_value:
                        z_set.add(nb)
                
    # watershed
    logger.info('Watershed \w minaltitude and relevant neighbours as list pre-computation...')
    result = scipy.zeros(i_d.shape, dtype=scipy.uint16)
    nb_labs = 0
    for x in range(result.shape[0]):
        for y in range(result.shape[1]):
            for z in range(result.shape[2]):
                if result[x,y,z] != 0: continue # 10%
                L, lab = stream_neighbours2_set(i_d, result, minaltitude, neighbours, (x, y, z)) # 90%
                if -1 == lab:
                    nb_labs += 1
                    for p in L:
                        result[p] = nb_labs
                else:
                    for p in L: 
                        result[p] = lab
                        
    return result
Example #11
0
def test(path='lines_test_dat.sav', PEC=None, E_thresh=1.0, He_source='PEC'):
    sf = scipy.io.readsav(path)
    r = scipy.asarray(sf['rad'], dtype=float) / 100.0
    q_IDL = scipy.asarray(sf['emhead'][:, 1], dtype=int) - 1
    lam_IDL = scipy.asarray(sf['emhead'][:, 2], dtype=float) / 10.0
    E_IDL = h * c / (lam_IDL * 1e-9 * e * 1e3)
    ne = scipy.asarray(sf['nelec'], dtype=float) # already in cm^-3
    Te = scipy.asarray(sf['te'], dtype=float) # already in keV
    em_IDL = scipy.swapaxes(scipy.asarray(sf['lemist'], dtype=float), 1, 2) * 1e6 # convert to ph/s/m^3
    cs_den = scipy.swapaxes(scipy.asarray(sf['denst'], dtype=float), 1, 2) # already in cm^-3
    
    atdata_IDL = read_atdata(path='atdata.dat.original')[20]
    comment_IDL = [l.comment for l in atdata_IDL]
    # comment_IDL = [str(typ) for typ in sf['emhead'][:, 3]]
    
    em, lam, E, q, comment = compute_lines(
        20,
        cs_den,
        ne,
        Te,
        PEC=PEC,
        E_thresh=E_thresh,
        He_source=He_source,
        full_return=True
    )
    t = range(0, em.shape[0])
    
    plot_lines(em, E, q, comment, t, r)
    
    plot_lines(em_IDL, E_IDL, q_IDL, comment_IDL, t, r)
    
    atdata = read_atdata()
    sindat = read_sindat()
    filter_trans = read_filter_file('Be_filter_50_um.dat')(E)
    with open('Be_50_um_{src:s}.pkl'.format(src=He_source), 'wb') as f:
        pkl.dump(filter_trans, f)
    emiss = compute_SXR(cs_den, ne, Te, atdata, sindat, filter_trans, PEC, He_source=He_source)
    
    f = plt.figure()
    a_ne = f.add_subplot(3, 1, 1)
    a_ne.plot(r, ne)
    a_ne.set_ylabel('$n_e$ [cm^-3]')
    a_Te = f.add_subplot(3, 1, 2)
    a_Te.plot(r, Te)
    a_Te.set_ylabel('$T_e$ [keV]')
    a_emiss = f.add_subplot(3, 1, 3)
    a_emiss.plot(r, emiss[0, :])
    a_emiss.set_ylabel('$\epsilon$ [W/m^3]')
    a_emiss.set_xlabel('$r$ [m]')
    
    return em, E, q, comment, emiss
Example #12
0
def read_lsm(path,color=False):
    """ takes a path to a lsm file, reads the file with the tifffile lib and
    returns a np array

    final format is of the array dims: x y t - if color is True: x y t c
    """
    data = tifffile.imread(path)
    Data_cut = data[0,0,:,:,:] # empirical ...
    Data_cut_rot = sp.swapaxes(Data_cut,0,2)

    if color: # Thorough testing if xy order is preserved is missing!
        Data_cut = data[0,:,:,:]
        Data_cut_rot = sp.swapaxes(Data_cut,2,0)
        Data_cut_rot = sp.swapaxes(Data_cut_rot,1,3)

    return Data_cut_rot
Example #13
0
    def test_partial_dot_mat_mat(self):
        mat1 = sp.asarray(self.mat)
        mat1.shape = (4, 3, 2, 5)

        mat1 = matrix.make_mat(mat1,
                               axis_names=('time', 'x', 'y', 'z'),
                               row_axes=(0, ),
                               col_axes=(1, 2, 3))

        mat2 = sp.asarray(self.mat)
        mat2.shape = (4, 2, 3, 5)

        mat2 = matrix.make_mat(mat2,
                               axis_names=('w', 'y', 'x', 'freq'),
                               row_axes=(0, 1, 2),
                               col_axes=(3, ))

        result = dot_products.partial_dot(mat1, mat2)
        self.assertEqual(result.axes, ('time', 'w', 'z', 'freq'))
        self.assertEqual(result.rows, (0, 1))
        self.assertEqual(result.cols, (2, 3))
        self.assertEqual(result.shape, (4, 4, 5, 5))
        right_ans = sp.tensordot(mat1, mat2, ((1, 2), (2, 1)))
        right_ans = sp.swapaxes(right_ans, 1, 2)
        self.assertTrue(sp.allclose(right_ans, result))
Example #14
0
def read_lsm(path,color=False):
    """ takes a path to a lsm file, reads the file with the tifffile io and
    returns a np array

    final format is of the array dims: x y t - if color is True: x y t c
    """
    data = tifffile.imread(path)
    Data_cut = data[0,0,:,:,:] # empirical ...
    Data_cut_rot = sp.swapaxes(Data_cut,0,2)

    if color: # Thorough testing if xy order is preserved is missing!
        Data_cut = data[0,:,:,:]
        Data_cut_rot = sp.swapaxes(Data_cut,2,0)
        Data_cut_rot = sp.swapaxes(Data_cut_rot,1,3)

    return Data_cut_rot
Example #15
0
def interleave(lst):
    """Given a list of arrays, interleave the arrays in a way that the
    first dimension represents the first dimension of every array.
    This is useful for time series, where multiple time series should be
    processed in a single swipe."""
    arr = scipy.asarray(lst)
    return scipy.swapaxes(arr, 0, 1)
def main():
    args = getArguments(getParser())

    # prepare logger
    logger = Logger.getInstance()
    if args.debug: logger.setLevel(logging.DEBUG)
    elif args.verbose: logger.setLevel(logging.INFO)
    
    # load input image
    data_input, header_input = load(args.input)
    
    logger.debug('Original shape = {}.'.format(data_input.shape))
    
    # check if supplied dimension parameters is inside the images dimensions
    if args.dimension1 >= data_input.ndim or args.dimension1 < 0:
        raise ArgumentError('The first swap-dimension {} exceeds the number of input volume dimensions {}.'.format(args.dimension1, data_input.ndim))
    elif args.dimension2 >= data_input.ndim or args.dimension2 < 0:
        raise ArgumentError('The second swap-dimension {} exceeds the number of input volume dimensions {}.'.format(args.dimension2, data_input.ndim))
    
    # swap axes
    data_output = scipy.swapaxes(data_input, args.dimension1, args.dimension2)
    # swap pixel spacing and offset
    ps = list(header.get_pixel_spacing(header_input))
    ps[args.dimension1], ps[args.dimension2] = ps[args.dimension2], ps[args.dimension1]
    header.set_pixel_spacing(header_input, ps)
    os = list(header.get_offset(header_input))
    os[args.dimension1], os[args.dimension2] = os[args.dimension2], os[args.dimension1]
    header.set_offset(header_input, os)
    
    logger.debug('Resulting shape = {}.'.format(data_output.shape))
    
    # save resulting volume
    save(data_output, args.output, header_input, args.force)
    
    logger.info("Successfully terminated.")    
Example #17
0
 def f(self, x):
     N = self.xdim / 3
     coords = x.reshape((N, 3))
     distances = sqrt(
         scipy.sum((tile(coords,
                         (N, 1, 1)) - swapaxes(tile(coords,
                                                    (N, 1, 1)), 0, 1))**2,
                   axis=2)) + eye(N)
     return 2 * sum(ravel(distances**-12 - distances**-6))
Example #18
0
def main():
    # parse cmd arguments
    parser = getParser()
    parser.parse_args()
    args = getArguments(parser)

    # prepare logger
    logger = Logger.getInstance()
    if args.debug: logger.setLevel(logging.DEBUG)
    elif args.verbose: logger.setLevel(logging.INFO)

    # load first input image as example
    example_data, example_header = load(args.inputs[0])

    # test if the supplied position is valid
    if args.position > example_data.ndim or args.position < 0:
        raise ArgumentError(
            'The supplied position for the new dimension is invalid. It has to be between 0 and {}.'
            .format(example_data.ndim))

    # prepare empty output volume
    output_data = scipy.zeros([len(args.inputs)] + list(example_data.shape),
                              dtype=example_data.dtype)

    # add first image to output volume
    output_data[0] = example_data

    # load input images and add to output volume
    for idx, image in enumerate(args.inputs[1:]):
        image_data, _ = load(image)
        if not args.ignore and image_data.dtype != example_data.dtype:
            raise ArgumentError(
                'The dtype {} of image {} differs from the one of the first image {}, which is {}.'
                .format(image_data.dtype, image, args.inputs[0],
                        example_data.dtype))
        if image_data.shape != example_data.shape:
            raise ArgumentError(
                'The shape {} of image {} differs from the one of the first image {}, which is {}.'
                .format(image_data.shape, image, args.inputs[0],
                        example_data.shape))
        output_data[idx + 1] = image_data

    # move new dimension to the end or to target position
    for dim in range(output_data.ndim - 1):
        if dim >= args.position: break
        output_data = scipy.swapaxes(output_data, dim, dim + 1)

    # set pixel spacing
    spacing = list(header.get_pixel_spacing(example_header))
    spacing = tuple(spacing[:args.position] + [args.spacing] +
                    spacing[args.position:])
    example_header.set_voxel_spacing(spacing)

    # save created volume
    save(output_data, args.output, example_header, args.force)

    logger.info("Successfully terminated.")
Example #19
0
def sliceEpsArr(EpsArr,NX,NY,NZ,figNum):
    """ Visualizing object with slices 
    """
    vxyR=sci.real(EpsArr[:,:,round(NZ/2.0)])
    vxzR=sci.real(EpsArr[:,round(NY/2.0),:])
    vyzR=sci.real(EpsArr[round(NX/2.0),:,:])
    vxyI=sci.imag(EpsArr[:,:,round(NZ/2.0)])
    vxzI=sci.imag(EpsArr[:,round(NY/2.0),:])
    vyzI=sci.imag(EpsArr[round(NX/2.0),:,:])
    
    plt.ion()
    fig=plt.figure(figNum)
    fig.clear()
    
    plt.subplot(231)
    plt.imshow(vxyR)
    plt.title('real($\\varepsilon$), xy-plane')
    plt.colorbar()
    plt.subplot(232)
    plt.imshow(sci.swapaxes(vxzR,0,1))
    plt.title('real($\\varepsilon$), xz-plane')
    plt.colorbar()
    plt.subplot(233)
    plt.imshow(sci.swapaxes(vyzR,0,1))
    plt.title('real($\\varepsilon$), yz-plane')
    plt.colorbar()
    plt.subplot(234)
    plt.imshow(vxyI)
    plt.title('imag($\\varepsilon$), xy-plane')
    plt.colorbar()
    plt.subplot(235)
    plt.imshow(sci.swapaxes(vxzI,0,1))
    plt.title('imag($\\varepsilon$), xz-plane')
    plt.colorbar()
    plt.subplot(236)
    plt.imshow(sci.swapaxes(vyzI,0,1))
    plt.title('imag($\\varepsilon$), yz-plane')
    plt.colorbar()
    
    fig.canvas.draw()
    plt.ioff()
    
    return
Example #20
0
def sliceEpsArr(EpsArr, NX, NY, NZ, figNum):
    """ Visualizing object with slices 
    """
    vxyR = sci.real(EpsArr[:, :, round(NZ / 2.0)])
    vxzR = sci.real(EpsArr[:, round(NY / 2.0), :])
    vyzR = sci.real(EpsArr[round(NX / 2.0), :, :])
    vxyI = sci.imag(EpsArr[:, :, round(NZ / 2.0)])
    vxzI = sci.imag(EpsArr[:, round(NY / 2.0), :])
    vyzI = sci.imag(EpsArr[round(NX / 2.0), :, :])

    plt.ion()
    fig = plt.figure(figNum)
    fig.clear()

    plt.subplot(231)
    plt.imshow(vxyR)
    plt.title('real($\\varepsilon$), xy-plane')
    plt.colorbar()
    plt.subplot(232)
    plt.imshow(sci.swapaxes(vxzR, 0, 1))
    plt.title('real($\\varepsilon$), xz-plane')
    plt.colorbar()
    plt.subplot(233)
    plt.imshow(sci.swapaxes(vyzR, 0, 1))
    plt.title('real($\\varepsilon$), yz-plane')
    plt.colorbar()
    plt.subplot(234)
    plt.imshow(vxyI)
    plt.title('imag($\\varepsilon$), xy-plane')
    plt.colorbar()
    plt.subplot(235)
    plt.imshow(sci.swapaxes(vxzI, 0, 1))
    plt.title('imag($\\varepsilon$), xz-plane')
    plt.colorbar()
    plt.subplot(236)
    plt.imshow(sci.swapaxes(vyzI, 0, 1))
    plt.title('imag($\\varepsilon$), yz-plane')
    plt.colorbar()

    fig.canvas.draw()
    plt.ioff()

    return
Example #21
0
def main():
    args = getArguments(getParser())

    # prepare logger
    logger = Logger.getInstance()
    if args.debug: logger.setLevel(logging.DEBUG)
    elif args.verbose: logger.setLevel(logging.INFO)

    # load dicom slices
    [series] = pydicom_series.read_files(
        args.input, False,
        True)  # second to not show progress bar, third to retrieve data
    #print series.sampling # Note: The first value is the mean of all differences between ImagePositionPatient-values of the DICOM slices - of course total bullshit
    data_3d = series.get_pixel_array()

    # check parameters
    if args.dimension >= data_3d.ndim or args.dimension < 0:
        raise ArgumentError(
            'The image has only {} dimensions. The supplied target dimension {} exceeds this number.'
            .format(data_3d.ndim, args.dimension))
    if not 0 == data_3d.shape[args.dimension] % args.offset:
        raise ArgumentError(
            'The number of slices {} in the target dimension {} of the image shape {} is not dividable by the supplied number of consecutive slices {}.'
            .format(data_3d.shape[args.dimension], args.dimension,
                    data_3d.shape, args.offset))

    # prepare empty target volume
    volumes_3d = data_3d.shape[args.dimension] / args.offset
    shape_4d = list(data_3d.shape)
    shape_4d[args.dimension] = volumes_3d
    data_4d = scipy.zeros([args.offset] + shape_4d, dtype=data_3d.dtype)

    logger.debug(
        'Separating {} slices into {} 3D volumes of thickness {}.'.format(
            data_3d.shape[args.dimension], volumes_3d, args.offset))

    # iterate over 3D image and create sub volumes which are then added to the 4d volume
    for idx in range(args.offset):
        # collect the slices
        for sl in range(volumes_3d):
            idx_from = [slice(None), slice(None), slice(None)]
            idx_from[args.dimension] = slice(idx + sl * args.offset,
                                             idx + sl * args.offset + 1)
            idx_to = [slice(None), slice(None), slice(None)]
            idx_to[args.dimension] = slice(sl, sl + 1)
            #print 'Slice {} to {}.'.format(idx_from, idx_to)
            data_4d[idx][idx_to] = data_3d[idx_from]

    # flip dimensions such that the newly created is the last
    data_4d = scipy.swapaxes(data_4d, 0, 3)

    # save resulting 4D volume
    save(data_4d, args.output, False, args.force)

    logger.info("Successfully terminated.")
def main():
    # parse cmd arguments
    parser = getParser()
    parser.parse_args()
    args = getArguments(parser)
    
    # prepare logger
    logger = Logger.getInstance()
    if args.debug: logger.setLevel(logging.DEBUG)
    elif args.verbose: logger.setLevel(logging.INFO)
    
    # load first input image as example 
    example_data, example_header = load(args.inputs[0])
    
    # test if the supplied position is valid
    if args.position > example_data.ndim or args.position < 0:
        raise ArgumentError('The supplied position for the new dimension is invalid. It has to be between 0 and {}.'.format(example_data.ndim))
    
    # prepare empty output volume
    output_data = scipy.zeros([len(args.inputs)] + list(example_data.shape), dtype=example_data.dtype)
    
    # add first image to output volume
    output_data[0] = example_data
    
    # load input images and add to output volume
    for idx, image in enumerate(args.inputs[1:]):
        image_data, _ = load(image)
        if not args.ignore and image_data.dtype != example_data.dtype:
            raise ArgumentError('The dtype {} of image {} differs from the one of the first image {}, which is {}.'.format(image_data.dtype, image, args.inputs[0], example_data.dtype))
        if image_data.shape != example_data.shape:
            raise ArgumentError('The shape {} of image {} differs from the one of the first image {}, which is {}.'.format(image_data.shape, image, args.inputs[0], example_data.shape))
        output_data[idx + 1] = image_data
        
    # move new dimension to the end or to target position
    for dim in range(output_data.ndim - 1):
        if dim >= args.position: break
        output_data = scipy.swapaxes(output_data, dim, dim + 1)
        
    # set pixel spacing
    spacing = list(header.get_pixel_spacing(example_header))
    spacing = tuple(spacing[:args.position] + [args.spacing] + spacing[args.position:])
    
    # !TODO: Find a way to enable this also for PyDicom and ITK images
    if __is_header_nibabel(example_header):
        __update_header_from_array_nibabel(example_header, output_data)
        header.set_pixel_spacing(example_header, spacing)
    else:
        raise ArgumentError("Sorry. Setting the voxel spacing of the new dimension only works with NIfTI images. See the description of this program for more details.")
    
    # save created volume
    save(output_data, args.output, example_header, args.force)
        
    logger.info("Successfully terminated.")
def main():
    args = getArguments(getParser())

    # prepare logger
    logger = Logger.getInstance()
    if args.debug: logger.setLevel(logging.DEBUG)
    elif args.verbose: logger.setLevel(logging.INFO)

    # load 3d image
    data_3d, header_3d = load(args.input)

    # check if supplied dimension parameter is inside the images dimensions
    if args.dimension >= data_3d.ndim or args.dimension < 0:
        raise ArgumentError(
            'The supplied cut-dimension {} exceeds the number of input volume dimensions {}.'
            .format(args.dimension, data_3d.ndim))

    # check if the supplied offset parameter is a divider of the cut-dimensions slice number
    if not 0 == data_3d.shape[args.dimension] % args.offset:
        raise ArgumentError(
            'The offset is not a divider of the number of slices in cut dimension ({} / {}).'
            .format(data_3d.shape[args.dimension], args.offset))

    # prepare empty target volume
    volumes_3d = data_3d.shape[args.dimension] / args.offset
    shape_4d = list(data_3d.shape)
    shape_4d[args.dimension] = volumes_3d
    data_4d = scipy.zeros([args.offset] + shape_4d, dtype=data_3d.dtype)

    logger.debug(
        'Separating {} slices into {} 3D volumes of thickness {}.'.format(
            data_3d.shape[args.dimension], volumes_3d, args.offset))

    # iterate over 3D image and create sub volumes which are then added to the 4d volume
    for idx in range(args.offset):
        # collect the slices
        for sl in range(volumes_3d):
            idx_from = [slice(None), slice(None), slice(None)]
            idx_from[args.dimension] = slice(idx + sl * args.offset,
                                             idx + sl * args.offset + 1)
            idx_to = [slice(None), slice(None), slice(None)]
            idx_to[args.dimension] = slice(sl, sl + 1)
            #print 'Slice {} to {}.'.format(idx_from, idx_to)
            data_4d[idx][idx_to] = data_3d[idx_from]

    # flip dimensions such that the newly created is the last
    data_4d = scipy.swapaxes(data_4d, 0, args.dimension + 1)
    data_4d = scipy.rollaxis(data_4d, 0, 4)

    # save resulting 4D volume
    save(data_4d, args.output, header_3d, args.force)

    logger.info("Successfully terminated.")
def main():
    args = getArguments(getParser())

    # prepare logger
    logger = Logger.getInstance()
    if args.debug: logger.setLevel(logging.DEBUG)
    elif args.verbose: logger.setLevel(logging.INFO)
    
    # load dicom slices
    [series] = pydicom_series.read_files(args.input, False, True) # second to not show progress bar, third to retrieve data
    #print series.sampling # Note: The first value is the mean of all differences between ImagePositionPatient-values of the DICOM slices - of course total bullshit
    data_3d = series.get_pixel_array()
    
    # check parameters
    if args.dimension >= data_3d.ndim or args.dimension < 0:
        raise ArgumentError('The image has only {} dimensions. The supplied target dimension {} exceeds this number.'.format(
                    data_3d.ndim,
                    args.dimension))
    if not 0 == data_3d.shape[args.dimension] % args.offset:
        raise ArgumentError('The number of slices {} in the target dimension {} of the image shape {} is not dividable by the supplied number of consecutive slices {}.'.format(
                    data_3d.shape[args.dimension],
                    args.dimension,
                    data_3d.shape,
                    args.offset))
    
    # prepare empty target volume
    volumes_3d = data_3d.shape[args.dimension] / args.offset
    shape_4d = list(data_3d.shape)
    shape_4d[args.dimension] = volumes_3d
    data_4d = scipy.zeros([args.offset] + shape_4d, dtype=data_3d.dtype)
    
    logger.debug('Separating {} slices into {} 3D volumes of thickness {}.'.format(data_3d.shape[args.dimension], volumes_3d, args.offset))
        
    # iterate over 3D image and create sub volumes which are then added to the 4d volume
    for idx in range(args.offset):
        # collect the slices
        for sl in range(volumes_3d):
            idx_from = [slice(None), slice(None), slice(None)]
            idx_from[args.dimension] = slice(idx + sl * args.offset, idx + sl * args.offset + 1)
            idx_to = [slice(None), slice(None), slice(None)]
            idx_to[args.dimension] = slice(sl, sl+1)
            #print 'Slice {} to {}.'.format(idx_from, idx_to)
            data_4d[idx][idx_to] = data_3d[idx_from]
        
    # flip dimensions such that the newly created is the last
    data_4d = scipy.swapaxes(data_4d, 0, 3)
        
    # save resulting 4D volume
    save(data_4d, args.output, False, args.force)
    
    logger.info("Successfully terminated.")
Example #25
0
def sliceFieldArr(Ein, NX, NY, NZ, SliceX, SliceY, SliceZ, figNum, fieldName):
    """ Visualizing field with slices 
    """
    vxy1 = Ein[:, :, SliceZ, 0]
    vxy2 = Ein[:, :, SliceZ, 1]
    vxy3 = Ein[:, :, SliceZ, 2]
    vyz1 = Ein[SliceX, :, :, 0]
    vyz2 = Ein[SliceX, :, :, 1]
    vyz3 = Ein[SliceX, :, :, 2]
    vxz1 = Ein[:, SliceY, :, 0]
    vxz2 = Ein[:, SliceY, :, 1]
    vxz3 = Ein[:, SliceY, :, 2]
    plt.ion()
    fig = plt.figure(figNum)
    fig.clear()
    plt.subplot(331)
    plt.imshow(sci.real(vxy1))
    plt.title('real($' + fieldName + '_{x}$), xy-plane')
    plt.colorbar()
    plt.subplot(332)
    plt.imshow(sci.real(sci.swapaxes(vxz1, 0, 1)))
    plt.title('real($' + fieldName + '_{x}$), xz-plane')
    plt.colorbar()
    plt.subplot(333)
    plt.imshow(sci.real(sci.swapaxes(vyz1, 0, 1)))
    plt.title('real($' + fieldName + '_{x}$), yz-plane')
    plt.colorbar()
    plt.subplot(334)
    plt.imshow(sci.real(vxy2))
    plt.title('real($' + fieldName + '_{y}$), xy-plane')
    plt.colorbar()
    plt.subplot(335)
    plt.imshow(sci.real(sci.swapaxes(vxz2, 0, 1)))
    plt.title('real($' + fieldName + '_{y}$), xz-plane')
    plt.colorbar()
    plt.subplot(336)
    plt.imshow(sci.real(sci.swapaxes(vyz2, 0, 1)))
    plt.title('real($' + fieldName + '_{y}$), yz-plane')
    plt.colorbar()
    plt.subplot(337)
    plt.imshow(sci.real(vxy3))
    plt.title('real($' + fieldName + '_{z}$), xy-plane')
    plt.colorbar()
    plt.subplot(338)
    plt.imshow(sci.real(sci.swapaxes(vxz3, 0, 1)))
    plt.title('real($' + fieldName + '_{z}$), xz-plane')
    plt.colorbar()
    plt.subplot(339)
    plt.imshow(sci.real(sci.swapaxes(vyz3, 0, 1)))
    plt.title('real($' + fieldName + '_{z}$), yz-plane')
    plt.colorbar()

    fig.canvas.draw()
    plt.ioff()

    return
Example #26
0
def sliceFieldArr(Ein,NX,NY,NZ,SliceX,SliceY,SliceZ,figNum,fieldName):
    """ Visualizing field with slices 
    """
    vxy1=Ein[:,:,SliceZ,0]
    vxy2=Ein[:,:,SliceZ,1]
    vxy3=Ein[:,:,SliceZ,2]
    vyz1=Ein[SliceX,:,:,0]
    vyz2=Ein[SliceX,:,:,1]
    vyz3=Ein[SliceX,:,:,2]
    vxz1=Ein[:,SliceY,:,0]
    vxz2=Ein[:,SliceY,:,1]
    vxz3=Ein[:,SliceY,:,2]
    plt.ion()
    fig=plt.figure(figNum)
    fig.clear()
    plt.subplot(331)
    plt.imshow(sci.real(vxy1))
    plt.title('real($'+fieldName+'_{x}$), xy-plane')
    plt.colorbar()
    plt.subplot(332)
    plt.imshow(sci.real(sci.swapaxes(vxz1,0,1)))
    plt.title('real($'+fieldName+'_{x}$), xz-plane')
    plt.colorbar()
    plt.subplot(333)
    plt.imshow(sci.real(sci.swapaxes(vyz1,0,1)))
    plt.title('real($'+fieldName+'_{x}$), yz-plane')
    plt.colorbar()
    plt.subplot(334)
    plt.imshow(sci.real(vxy2))
    plt.title('real($'+fieldName+'_{y}$), xy-plane')
    plt.colorbar()
    plt.subplot(335)
    plt.imshow(sci.real(sci.swapaxes(vxz2,0,1)))
    plt.title('real($'+fieldName+'_{y}$), xz-plane')
    plt.colorbar()
    plt.subplot(336)
    plt.imshow(sci.real(sci.swapaxes(vyz2,0,1)))
    plt.title('real($'+fieldName+'_{y}$), yz-plane')
    plt.colorbar()
    plt.subplot(337)
    plt.imshow(sci.real(vxy3))
    plt.title('real($'+fieldName+'_{z}$), xy-plane')
    plt.colorbar()
    plt.subplot(338)
    plt.imshow(sci.real(sci.swapaxes(vxz3,0,1)))
    plt.title('real($'+fieldName+'_{z}$), xz-plane')
    plt.colorbar()
    plt.subplot(339)
    plt.imshow(sci.real(sci.swapaxes(vyz3,0,1)))
    plt.title('real($'+fieldName+'_{z}$), yz-plane')
    plt.colorbar()
    
    fig.canvas.draw()
    plt.ioff()
    
    return
Example #27
0
def main():
    args = getArguments(getParser())

    # prepare logger
    logger = Logger.getInstance()
    if args.debug: logger.setLevel(logging.DEBUG)
    elif args.verbose: logger.setLevel(logging.INFO)
    
    data_3d, _ = load(args.input)
    
    # check parameters
    if args.dimension >= data_3d.ndim or args.dimension < 0:
        raise ArgumentError('The image has only {} dimensions. The supplied target dimension {} exceeds this number.'.format(
                    data_3d.ndim,
                    args.dimension))
    if not 0 == data_3d.shape[args.dimension] % args.offset:
        raise ArgumentError('The number of slices {} in the target dimension {} of the image shape {} is not dividable by the supplied number of consecutive slices {}.'.format(
                    data_3d.shape[args.dimension],
                    args.dimension,
                    data_3d.shape,
                    args.offset))
    
    # prepare empty target volume
    volumes_3d = data_3d.shape[args.dimension] / args.offset
    shape_4d = list(data_3d.shape)
    shape_4d[args.dimension] = volumes_3d
    data_4d = scipy.zeros([args.offset] + shape_4d, dtype=data_3d.dtype)
    
    logger.debug('Separating {} slices into {} 3D volumes of thickness {}.'.format(data_3d.shape[args.dimension], volumes_3d, args.offset))
        
    # iterate over 3D image and create sub volumes which are then added to the 4d volume
    for idx in range(args.offset):
        # collect the slices
        for sl in range(volumes_3d):
            idx_from = [slice(None), slice(None), slice(None)]
            idx_from[args.dimension] = slice(idx + sl * args.offset, idx + sl * args.offset + 1)
            idx_to = [slice(None), slice(None), slice(None)]
            idx_to[args.dimension] = slice(sl, sl+1)
            #print 'Slice {} to {}.'.format(idx_from, idx_to)
            data_4d[idx][idx_to] = data_3d[idx_from]
        
    # flip dimensions such that the newly created is the last
    data_4d = scipy.swapaxes(data_4d, 0, 3)
        
    # save resulting 4D volume
    save(data_4d, args.output, False, args.force)
    
    logger.info("Successfully terminated.")
Example #28
0
def rescaleImage(self):
    '''Rescales image according to parameters in accompanying text file. File
    is the filename(without extension), data is the data matrix and nbvals is the
    amount of b-values. Does not return anything because this modifies the array.'''

    if self.dim == 3:
        for i in range(self.shape[3]):
            self.pdata[:, :, :, i] = self.pdata[:, :, :, i] * self.slopes[i]
    elif self.dim == 2:
        rslopes = np.reshape(self.slopes, self.shape[:-3:-1])
        rslopes = scipy.swapaxes(rslopes, 0, 1)
        for j in range(self.shape[2]):
            for i in range(self.shape[3]):
                self.pdata[:, :, j, i] = self.pdata[:, :, j, i] * rslopes[j, i]
    else:
        print "No b-values found or cannot process slope formatting."
Example #29
0
def main():
    args = getArguments(getParser())

    # prepare logger
    logger = Logger.getInstance()
    if args.debug: logger.setLevel(logging.DEBUG)
    elif args.verbose: logger.setLevel(logging.INFO)
    
    # load 3d image
    data_3d, header_3d = load(args.input)
    
    # check if supplied dimension parameter is inside the images dimensions
    if args.dimension >= data_3d.ndim or args.dimension < 0:
        raise ArgumentError('The supplied cut-dimension {} exceeds the number of input volume dimensions {}.'.format(args.dimension, data_3d.ndim))
    
    # check if the supplied offset parameter is a divider of the cut-dimensions slice number
    if not 0 == data_3d.shape[args.dimension] % args.offset:
        raise ArgumentError('The offset is not a divider of the number of slices in cut dimension ({} / {}).'.format(data_3d.shape[args.dimension], args.offset))
        
    # prepare empty target volume
    volumes_3d = data_3d.shape[args.dimension] / args.offset
    shape_4d = list(data_3d.shape)
    shape_4d[args.dimension] = volumes_3d
    data_4d = scipy.zeros([args.offset] + shape_4d, dtype=data_3d.dtype)
    
    logger.debug('Separating {} slices into {} 3D volumes of thickness {}.'.format(data_3d.shape[args.dimension], volumes_3d, args.offset))
        
    # iterate over 3D image and create sub volumes which are then added to the 4d volume
    for idx in range(args.offset):
        # collect the slices
        for sl in range(volumes_3d):
            idx_from = [slice(None), slice(None), slice(None)]
            idx_from[args.dimension] = slice(idx + sl * args.offset, idx + sl * args.offset + 1)
            idx_to = [slice(None), slice(None), slice(None)]
            idx_to[args.dimension] = slice(sl, sl+1)
            #print 'Slice {} to {}.'.format(idx_from, idx_to)
            data_4d[idx][idx_to] = data_3d[idx_from]
        
    # flip dimensions such that the newly created is the last
    data_4d = scipy.swapaxes(data_4d, 0, args.dimension + 1)
    data_4d = scipy.rollaxis(data_4d, 0, 4)
        
    # save resulting 4D volume
    save(data_4d, args.output, header_3d, args.force)
    
    logger.info("Successfully terminated.")
Example #30
0
 def test_partial_dot_mat_mat(self):
     mat1 = sp.asarray(self.mat)
     mat1.shape = (4, 3, 2, 5)
     mat1 = algebra.make_mat(mat1, axis_names=('time', 'x', 'y', 'z'),
                             row_axes=(0,), col_axes=(1, 2, 3))
     mat2 = sp.asarray(self.mat)
     mat2.shape = (4, 2, 3, 5)
     mat2 = algebra.make_mat(mat2, axis_names=('w', 'y', 'x', 'freq'), 
                             row_axes=(0, 1, 2), col_axes=(3,))
     result = algebra.partial_dot(mat1, mat2)
     self.assertEqual(result.axes, ('time', 'w', 'z', 'freq'))
     self.assertEqual(result.rows, (0, 1))
     self.assertEqual(result.cols, (2, 3))
     self.assertEqual(result.shape, (4, 4, 5, 5))
     right_ans = sp.tensordot(mat1, mat2, ((1, 2), (2, 1)))
     right_ans = sp.swapaxes(right_ans, 1, 2)
     self.assertTrue(sp.allclose(right_ans, result))
Example #31
0
def rescaleImage(file, data, nbvals, dims):
    '''Rescales image according to parameters in accompanying text file. File
    is the filename(without extension), data is the data matrix and nbvals is the
    amount of b-values. Does not return anything because this modifies the array.'''
    slopes = np.array(list_values(read_line('VisuCoreDataSlope=', file)))

    if len(dims) == 3:
        for i in range(data.shape[3]):
            data[:, :, :, i] = data[:, :, :, i] * slopes[i]
    elif len(dims) == 2:
        rslopes = np.reshape(slopes, data.shape[:-3:-1])
        rslopes = scipy.swapaxes(rslopes, 0, 1)
        for j in range(data.shape[2]):
            for i in range(data.shape[3]):
                data[:, :, j, i] = data[:, :, j, i] * rslopes[j, i]
    else:
        print "No b-values found or cannot process slope formatting."
def main():
    args = getArguments(getParser())

    # prepare logger
    logger = Logger.getInstance()
    if args.debug: logger.setLevel(logging.DEBUG)
    elif args.verbose: logger.setLevel(logging.INFO)

    # load input image
    data_input, header_input = load(args.input)

    logger.debug('Original shape = {}.'.format(data_input.shape))

    # check if supplied dimension parameters is inside the images dimensions
    if args.dimension1 >= data_input.ndim or args.dimension1 < 0:
        raise ArgumentError(
            'The first swap-dimension {} exceeds the number of input volume dimensions {}.'
            .format(args.dimension1, data_input.ndim))
    elif args.dimension2 >= data_input.ndim or args.dimension2 < 0:
        raise ArgumentError(
            'The second swap-dimension {} exceeds the number of input volume dimensions {}.'
            .format(args.dimension2, data_input.ndim))

    # swap axes
    data_output = scipy.swapaxes(data_input, args.dimension1, args.dimension2)
    # swap pixel spacing and offset
    ps = list(header.get_pixel_spacing(header_input))
    ps[args.dimension1], ps[args.dimension2] = ps[args.dimension2], ps[
        args.dimension1]
    header.set_pixel_spacing(header_input, ps)
    os = list(header.get_offset(header_input))
    os[args.dimension1], os[args.dimension2] = os[args.dimension2], os[
        args.dimension1]
    header.set_offset(header_input, os)

    logger.debug('Resulting shape = {}.'.format(data_output.shape))

    # save resulting volume
    save(data_output, args.output, header_input, args.force)

    logger.info("Successfully terminated.")
Example #33
0
def read(file_name, map_inds=None, feedback=2):
    """Read a map from an image fits file.
    """

    fname_abbr = ku.abbreviate_file_path(file_name)
    if feedback > 0:
        print "Opening file: " + fname_abbr
    # Open the fits file.
    hdulist = pyfits.open(file_name)
    history = bf.get_history_header(hdulist[0].header)
    history.add("Read from file.", "File name: " + fname_abbr)
    map_list = []

    if map_inds is None:
        map_inds = range(1, len(hdulist))
    elif not hasattr(map_inds, "__iter__"):
        map_inds = (map_inds,)
    elif len(scans) == 0:
        map_inds = range(1, len(hdulist))

    for ii in map_inds:
        data = hdulist[ii].data

        # Set the data attriute
        Map = data_map.DataMap()
        Map.set_data(sp.swapaxes(data, 0, 2))
        # Masked data is stored in FITS files as float('nan')
        Map.data[sp.logical_not(sp.isfinite(Map.data))] = ma.masked
        Map.history = history

        # Set the other fields.
        for field_name in fields:
            if not field_name in hdulist[1].header.keys():
                continue
            value = hdulist[1].header[field_name]
            Map.set_field(field_name, value)
        map_list.append(Map)
    if len(map_list) == 1:
        map_list = map_list[0]

    return map_list
Example #34
0
def read(file_name, map_inds=None, feedback=2):
    """Read a map from an image fits file.
    """

    fname_abbr = ku.abbreviate_file_path(file_name)
    if feedback > 0:
        print 'Opening file: ' + fname_abbr
    # Open the fits file.
    hdulist = pyfits.open(file_name)
    history = bf.get_history_header(hdulist[0].header)
    history.add('Read from file.', 'File name: ' + fname_abbr)
    map_list = []

    if map_inds is None:
        map_inds = range(1, len(hdulist))
    elif not hasattr(map_inds, '__iter__'):
        map_inds = (map_inds, )
    elif len(scans) == 0:
        map_inds = range(1, len(hdulist))

    for ii in map_inds:
        data = hdulist[ii].data

        # Set the data attriute
        Map = data_map.DataMap()
        Map.set_data(sp.swapaxes(data, 0, 2))
        # Masked data is stored in FITS files as float('nan')
        Map.data[sp.logical_not(sp.isfinite(Map.data))] = ma.masked
        Map.history = history

        # Set the other fields.
        for field_name in fields:
            if not field_name in hdulist[1].header.keys():
                continue
            value = hdulist[1].header[field_name]
            Map.set_field(field_name, value)
        map_list.append(Map)
    if len(map_list) == 1:
        map_list = map_list[0]

    return map_list
Example #35
0
def write(Maps, file_name, feedback=2):
    """Write a map to fits file.

    Map should be a map_data.MapData object.
    """

    # If only a single Map was passed, make it iterable.
    if not hasattr(Maps, '__iter__'):
        Maps = (Maps, )
    # First create a primary with the history and such:
    prihdu = pyfits.PrimaryHDU()
    # Add history to the primary.
    fname_abbr = ku.abbreviate_file_path(file_name)
    # Add final history entry and store in the primary header.
    history = base_data.merge_histories(*Maps)
    history.add('Written to file.', 'File name: ' + fname_abbr)
    bf.write_history_header(prihdu.header, history)
    list_of_hdus = [prihdu]

    for ii, Map in enumerate(Maps):
        # Creat an image HDU.
        map = sp.array(ma.filled(Map.data, float('nan')))
        imhdu = pyfits.ImageHDU(sp.swapaxes(map, 0, 2), name='MAP%d' % ii)

        # Add extra data to the HDU
        for key in Map.field.iterkeys():
            if Map.field_axes[key] != ():
                raise ce.DataError('Only 0D data can be written to a Fits Map '
                                   'Header.')
            card = pyfits.Card(key, Map.field[key].item())
            imhdu.header.ascardlist().append(card)
        list_of_hdus.append(imhdu)

    # Creat the HDU list and write to file.
    hdulist = pyfits.HDUList(list_of_hdus)
    hdulist.writeto(file_name, clobber=True)
    if feedback > 0:
        print 'Wrote data to file: ' + fname_abbr
Example #36
0
def write(Maps, file_name, feedback=2):
    """Write a map to fits file.

    Map should be a map_data.MapData object.
    """

    # If only a single Map was passed, make it iterable.
    if not hasattr(Maps, "__iter__"):
        Maps = (Maps,)
    # First create a primary with the history and such:
    prihdu = pyfits.PrimaryHDU()
    # Add history to the primary.
    fname_abbr = ku.abbreviate_file_path(file_name)
    # Add final history entry and store in the primary header.
    history = base_data.merge_histories(*Maps)
    history.add("Written to file.", "File name: " + fname_abbr)
    bf.write_history_header(prihdu.header, history)
    list_of_hdus = [prihdu]

    for ii, Map in enumerate(Maps):
        # Creat an image HDU.
        map = sp.array(ma.filled(Map.data, float("nan")))
        imhdu = pyfits.ImageHDU(sp.swapaxes(map, 0, 2), name="MAP%d" % ii)

        # Add extra data to the HDU
        for key in Map.field.iterkeys():
            if Map.field_axes[key] != ():
                raise ce.DataError("Only 0D data can be written to a Fits Map " "Header.")
            card = pyfits.Card(key, Map.field[key].item())
            imhdu.header.ascardlist().append(card)
        list_of_hdus.append(imhdu)

    # Creat the HDU list and write to file.
    hdulist = pyfits.HDUList(list_of_hdus)
    hdulist.writeto(file_name, clobber=True)
    if feedback > 0:
        print "Wrote data to file: " + fname_abbr
Example #37
0
def plot_people(ifig,
                dom,
                people,
                contacts,
                U,
                colors,
                time=-1,
                axis=None,
                plot_people=True,
                plot_contacts=True,
                plot_velocities=True,
                plot_paths=False,
                paths=None,
                plot_sensors=False,
                sensors=[],
                savefig=False,
                filename='fig.png',
                cmap='winter'):
    """
    This function draws spheres for the individuals, \
    lines for the active contacts and arrows for the \
    (desired or real) velocities.

    Parameters
    ----------
    ifig: int
        figure number
    dom: Domain
        contains everything for managing the domain
    people: numpy array
        people coordinates and radius : x,y,r
    contacts: numpy array
        all the contacts : i,j,dij,eij_x,eij_y
    U: numpy array
        people velocities
    colors: numpy array
        scalar field used to define people colors
    time: float
        time in seconds
    axis: numpy array
        matplotlib axis : [xmin, xmax, ymin, ymax]
    plot_people: boolean
        draws spheres for people if true
    plot_paths: boolean
        draws people paths if true
    paths: numpy array
        coordinates of the people paths
    plot_sensors: boolean
        draws sensor lines if true
    sensors: numpy array
        sensor line coordinates (see also the sensor function below)
    savefig: boolean
        writes the figure as a png file if true
    filename: string
        png filename used to write the figure
    cmap: string
        matplotlib colormap name
    """
    fig = plt.figure(ifig)
    plt.clf()
    ax1 = fig.add_subplot(111)
    # Domain
    ax1.imshow(dom.image,
               interpolation='nearest',
               extent=[dom.xmin, dom.xmax, dom.ymin, dom.ymax],
               origin='lower')
    if (plot_people):
        # People
        #offsets = list(zip(people[:,:2])) ## for older versions of matplotlib...
        offsets = people[:, :2]
        ec = EllipseCollection(widths=2 * people[:, 2],
                               heights=2 * people[:, 2],
                               angles=0,
                               units='xy',
                               cmap=plt.get_cmap(cmap),
                               offsets=offsets,
                               transOffset=ax1.transData)
        ec.set_array(colors)
        ax1.add_collection(ec)
    if (plot_contacts):
        # Contacts
        Nc = contacts.shape[0]
        if (Nc > 0):
            for ic in sp.arange(Nc):
                i = sp.int64(contacts[ic, 0])
                j = sp.int64(contacts[ic, 1])
                if (j != -1):
                    line = Line2D([people[i, 0], people[j, 0]],
                                  [people[i, 1], people[j, 1]],
                                  lw=1.,
                                  alpha=0.6,
                                  color='k')
                else:
                    line = Line2D([
                        people[i, 0], people[i, 0] -
                        (people[i, 2] + contacts[ic, 2]) * contacts[ic, 3]
                    ], [
                        people[i, 1], people[i, 1] -
                        (people[i, 2] + contacts[ic, 2]) * contacts[ic, 4]
                    ],
                                  lw=1.,
                                  alpha=0.6,
                                  color='k')
                ax1.add_line(line)
    if (plot_velocities):
        # Velocities
        Np = people.shape[0]
        for ip in sp.arange(Np):
            arrow = Arrow(people[ip, 0],
                          people[ip, 1],
                          U[ip, 0],
                          U[ip, 1],
                          width=0.3)
            ax1.add_patch(arrow)
    if ((plot_paths) and (paths is not None)):
        mpaths = sp.ma.masked_values(paths, 1e99)
        pathlines = []
        for id in sp.arange(paths.shape[0]):
            pathlines.append(sp.swapaxes(mpaths[id, :, :], 0, 1))
        pathlinecoll = LineCollection(pathlines,
                                      linewidths=0.5,
                                      linestyle="solid",
                                      cmap=plt.get_cmap(cmap))
        pathlinecoll.set_array(colors)
        ax1.add_collection(pathlinecoll)
    if (plot_sensors):
        # Sensors
        for ss in sensors:
            line = Line2D([ss[0], ss[2]], [ss[1], ss[3]],
                          lw=1.,
                          alpha=0.6,
                          color='g')
            ax1.add_line(line)
    if (axis):
        ax1.set_xlim(axis[0], axis[1])
        ax1.set_ylim(axis[2], axis[3])
    ax1.set_xticks([])
    ax1.set_yticks([])
    ax1.axis('off')
    if (time >= 0):
        ax1.set_title('time = {:.2F}'.format(time) + ' s')
    fig.canvas.draw()
    if (savefig):
        fig.savefig(filename, dpi=600, bbox_inches='tight', pad_inches=0)
CalBlocks = ()

# Loop over IFs and read in the appropriate cal data.
for Data in Blocks :
    CalT = data_block.DataBlock()

    for field in required_fields :
        CalT.set_field(field, Data.field[field], Data.field_axes[field],
                       Data.field_formats[field])

    CalT.set_field('CAL', ['R'], ('cal',), '1A')
    CalT.set_field('CRVAL4', [-5, -6], ('pol',), '1I')
    CalT.set_field('LST', [3546.2], ('time',), '1D')

    freq_centre = int(round(Data.field['CRVAL1']/1.0e6)) # MHz

    indata_xx = sp.loadtxt(input_root + str(freq_centre) + '_xx.txt')
    indata_yy = sp.loadtxt(input_root + str(freq_centre) + '_yy.txt')

    cal_data = sp.concatenate((indata_xx[:,[1]], indata_yy[:,[1]]), axis=1)
    cal_data = sp.swapaxes(cal_data, 0, 1)
    CalT.set_data(cal_data[sp.newaxis,:,sp.newaxis,:])
    CalT.verify()

    CalBlocks = CalBlocks + (CalT,)

BigData = stitch_windows_crude.stitch(CalBlocks)

Writer.add_data(BigData)
Writer.write(out_file)
Example #39
0
 def f(self, x):
     N = self.xdim / 3
     coords = x.reshape((N, 3))
     distances = sqrt(scipy.sum((tile(coords, (N, 1, 1)) - swapaxes(tile(coords, (N, 1, 1)), 0, 1)) ** 2, axis=2)) + eye(N)
     return 2 * sum(ravel(distances ** -12 - distances ** -6))
Example #40
0
def Example1():
    a = sp.array([[1,2,3],[4,5,6]])
    b = sp.array([i*i for i in range(100) if i%2==1])
    c = b.tolist()
    print a,b,c

    a = sp.zeros(100)                      # a 100-element array of float zeros
    b = sp.zeros((2,8), int)               # a 2x8 array of int zeros
    c = sp.zeros((2,2,2), complex)         # a NxMxL array of complex zeros
    print a,b,c

    a = sp.ones(10, int)                   # a 10-element array of int ones
    b = sp.pi * sp.ones((5,5))          # a useful way to fill up an array with a specified value
    print a,b

    id = sp.eye(10,10, dtype=int)                           # 10x10 identity matrix (1's on diagonal)
    offdiag = sp.eye(10,10,1)+sp.eye(10,10,-1)     # off diagonal elements = 1
    print id,offdiag

    a = sp.array([[1,2,3],[4,5,6]])
    b = sp.transpose(a)                    # reverse dimensions of a (even for dim > 2)
    b = a.T                                   # equivalent to scipy.transpose(a)
    c = sp.swapaxes(a, 0, 1)       # swap specified axes
    print a,b,c

    a = sp.arange(1, 10, 1)      # like Python range, but with (potentially) real-valued arrays
    b = sp.linspace(1, 10, 11) # create array of equally-spaced points based on specifed number of points
    print a,b

    a = sp.random.random((100,100))        # 100x100 array of floats uniform on [0.,1.)
    b = sp.random.randint(0,10, (100,))    # 100 random ints uniform on [0, 10), i.e., not including the upper bound 10
    c = sp.random.standard_normal((5,5,5)) # zero-mean, unit-variance Gaussian random numbers in a 5x5x5 array
    print a,b,c

    elem = c[1,1,1]       # equiv. to a[i][j][k] but presumably more efficient
    print elem

    i = sp.array([0,1,2,1])                     # array of indices for the first axis
    j = sp.array([1,2,3,4])                     # array of indices for the second axis
    print a[i,j]                                         # return array([a[0,1], a[1,2], a[2,3], a[1,4]])
    c = sp.linspace(1, 10, 11)
    b = sp.array([True, False, True, False])
    print c[b]    
    last_elem = c[-1]     # the last element of the array
    print last_elem

    section = a[10:20, 30:40]         # 10x10 subblock starting at [10,30]
    print section

    asection = a[10:, 30:]            # missing stop index implies until end of array
    bsection = a[:10, :30]            # missing start index implies until start of array
    print asection,bsection

    x = a[:, 0]                       # get everything in the 0th column (missing start and stop)
    y = a[:, 1]                       # get everything in the 1st column
    print x,y

    s  = sp.sum(a)             # sum all elements in a, returning a scalar
    s0 = sp.sum(a, axis=0)     # sum elements along specified axis (=0), returning an array of remaining shape, e.g.,
    a  = sp.ones((10,20,30))
    a0 = sp.sum(a, axis=0)     # s0 has shape (20,30)
    print s,s0,a,a0

    a = sp.array([[1,2,3],[4,5,6]])
    m = sp.mean(a, axis=0)       # compute mean along the specified axis (over entire array if axis=None)
    s = sp.std(a, axis=0)        # compute standard deviation along the specified axis (over entire array if axis=None)
    print m,s

    s0 = sp.cumsum(a, axis=0)  # cumulatively sum over 0 axis, returning array with same shape as a
    s1 = sp.cumsum(a)          # cumulatively sum over 0 axis, returning 1D array of length shape[0]*shape[1]*...*shape[dim-1]
    print s0,s1
Example #41
0
    if len(ls)!=5:
        continue
    i = ls[0]
    j = ls[1]
    fit = ls[2]
    Temperature[i,j] = fit[0]
    Denscol[i,j] = fit[2]
    Turbvel[i,j] = fit[1]
    tau0[i,j] = ls[-1]
    model[i,j] = ls[-2]


# Parameter error (confidence intervals)
errmodel= sp.array(errmodel)
model = sp.array(model)
model = sp.swapaxes(model, 0,1)
model = sp.swapaxes(model,0,2)
print('Calculated Fits')
model = sp.nan_to_num(model)
dust = sp.nan_to_num(dust)
Denscol = sp.nan_to_num(Denscol)
Turbvel = sp.nan_to_num(Turbvel)
Temperature = sp.nan_to_num(Temperature)
tau0 = sp.nan_to_num(tau0)

if Convolve:
    Denscol = Convolv(Denscol, head)
    Temperature = Convolv(Temperature, head)
    Turbvel = Convolv(Turbvel, head)
    mom2 = Convolv(mom2, head)
import lmdb
import argparse
import sys
sys.path.insert(0, "/home/ogaki/Workspace/caffe/python")
import caffe
import cv2
import scipy

parser = argparse.ArgumentParser()
parser.add_argument("db")
parser.add_argument("kvfile")
parser.add_argument("imgdir")
args = parser.parse_args()

db = lmdb.open(path=args.db, map_size=1024 * 1024 * 1024)
db.open_db()

with db.begin(write=True) as buf:
    for i, line in enumerate(open(args.kvfile)):
        if i % 10000 == 0: print >> sys.stderr, i
        key = line.split()[0]
        imgfilepath = line.split()[1]
        #img = cv2.imread(args.imgdir+"/"+imgfilepath).transpose([2,0,1])
        img = caffe.io.load_image(args.imgdir + "/" +
                                  imgfilepath).astype(float)
        img = scipy.swapaxes(img, 0, 2)

        datum = caffe.io.array_to_datum(img)
        buf.put(key, datum.SerializeToString())
Example #43
0
def fit(combo, mapping, magtype, startingzps=None):

    filt_combo = [[mapping[c],c] for c in combo]
    good_fs = dict(filt_combo) 
    filt_combo = [[mapping[c],'yes'] for c in combo]
    l_fs = dict(filt_combo)
    filt_combo = [[mapping[c],[c]] for c in combo]
    locus_dict_obj = dict(filt_combo) 








    #good_fs = {'USDSS':'u','GSDSS':'g','RSDSS':'r','ISDSS':'i','ZSDSS':'z','JTMASS':'J'}
    #l_fs = {'USDSS':'yes','GSDSS':'yes','RSDSS':'yes','ISDSS':'yes','ZSDSS':'yes','JTMASS':'yes'}
    #locus_dict_obj = {'USDSS':['u'],'GSDSS':['g'],'RSDSS':['r'],'ISDSS':['i'],'ZSDSS':['z'],'JTMASS':['J']}

    #zp_dict = {'USDSS':0.,'GSDSS':0.,'RSDSS':0.,'ISDSS':0.,'ZSDSS':0.,'JTMASS':0.}

    #good_fs = {'JTMASS':'J','RSDSS':'r','ISDSS':'i',}
    #l_fs = {'JTMASS':'yes','RSDSS':'yes','ISDSS':'yes'}
    #locus_dict_obj = {'JTMASS':['J'],'RSDSS':['r'],'ISDSS':['i'],}

    import cutout_bpz, string
    locus_c = cutout_bpz.locus()

    print filter(lambda x:string.find(x.split('_')[0],'SDSS')!=-1 and string.find(x.split('_')[1],'SDSS')!=-1,locus_c.keys())
    print filter(lambda x:string.find(x,'MASS')!=-1 and string.find(x,'SDSS')!=-1 ,locus_c.keys())


    import re                                                    
    good_fs = []
    for k1 in locus_c.keys():
        res = re.split('_',k1)
        print l_fs
        print res
        if l_fs.has_key(res[0]) and l_fs.has_key(res[1]):
            good_fs.append([res[0],res[1]])

    print l_fs

    print locus_dict_obj, good_fs

    print good_fs

    zps_dict_obj = {} 

    list = ['MPUSUBARU','VJOHN','RJOHN'] #,'RJOHN','IJOHN','WSZSUBARU','WSISUBARU'] 

    print good_fs

    complist = []
    for f1A,f1B in good_fs:         
        if f1A != 'MPUSUBARU' and f1B != 'MPUSUBARU': # True: #filter(lambda x: x==f1A, list) and filter(lambda x: x==f1B, list): 
            zps_dict_obj[f1A] = 0                          
            zps_dict_obj[f1B] = 0
            import random
            for a in locus_dict_obj[f1A]:
                for b in locus_dict_obj[f1B]:
                    complist.append([[a,f1A],[b,f1B]])

    print complist
    print good_fs
    print zps_dict_obj

    print complist, 'complist'

    zps_list_full = combo #zps_dict_obj.keys()
    #if 'JTMASS' in zps_list_full:        
    #    zps_list_full = ['JTMASS'] + filter(lambda x: x!='JTMASS',zps_list_full)
        
    zps_list = zps_list_full[1:]
    zps ={} 
    zps_rev ={} 
    for i in range(len(zps_list)):
        zps[zps_list[i]] = i
        zps_rev[i] = zps_list[i] 

    import pyfits
    p = pyfits.open('stars.fits')[1].data
   
    table = p                                                                            
    loci = len(locus_c['GSDSS_RSDSS'])

    print loci

    stars = len(table.field('MAG_' + magtype + '_' + complist[0][0][0]))

    locus_list = []
    for j in range(len(locus_c['GSDSS_RSDSS'])):
        o = []
        for c in complist:
            #print c, len(locus_c[c[0][1] + '_' +  c[1][1]]), j
            o.append(locus_c[c[0][1] + '_' +  c[1][1]][j])
        locus_list.append(o)

    import scipy

    results = {} 
    for iteration in ['full','bootstrap1','bootstrap2','bootstrap3','bootstrap4']:       

        ''' make matrix with a locus for each star '''                                                                                           
        locus_matrix = scipy.array(stars*[locus_list])
        print locus_matrix.shape

        ''' assemble matricies to make colors '''
        A_band = scipy.swapaxes(scipy.swapaxes(scipy.array(loci*[[table.field('MAG_' + magtype + '_' + a[0][0]) for a in complist]]),0,2),1,2)
        B_band = scipy.swapaxes(scipy.swapaxes(scipy.array(loci*[[table.field('MAG_' + magtype + '_' + a[1][0]) for a in complist]]),0,2),1,2)
        A_err = scipy.swapaxes(scipy.swapaxes(scipy.array(loci*[[table.field('MAGERR_' + magtype + '_' + a[0][0]) for a in complist]]),0,2),1,2)
        B_err = scipy.swapaxes(scipy.swapaxes(scipy.array(loci*[[table.field('MAGERR_' + magtype + '_' + a[1][0]) for a in complist]]),0,2),1,2)
                                                                                                                                                 
        print A_err.shape
        A_band[A_err > 1.] = -99
        B_band[B_err > 1.] = -99

        ''' make matrix specifying good values '''
        good = scipy.ones(A_band.shape)
        good[A_band == -99] = 0
        good[B_band == -99] = 0
        good = good[:,0,:]
        good_test = good.sum(axis=1) # sum all of the good measurements for any given star
        
        print sorted(good_test)
        print good_test
                                                                                                                                                 
        ''' figure out the cut-off '''
        cut_off = 2 #sorted(good_test)[-20] -1 
        print cut_off
        
                                                                                                                                                 
        A_band = A_band[good_test>cut_off]
        B_band = B_band[good_test>cut_off]
        A_err = A_err[good_test>cut_off]
        B_err = B_err[good_test>cut_off]

        A_err[A_err<0.05] = 0.05
        B_err[B_err<0.05] = 0.05


        locus_matrix = locus_matrix[good_test>cut_off]

        if string.find(iteration,'bootstrap') != -1:

            length = len(A_band)
            randvec = scipy.array([random.random() for ww in range(length)])
            fraction = 0.5
            mask = randvec < (fraction) 

            A_band = A_band[mask]             
            B_band = B_band[mask]
            A_err = A_err[mask]
            B_err = B_err[mask]
            locus_matrix = locus_matrix[mask]
        
        colors = A_band - B_band
        colors_err = (A_err**2. + B_err**2.)**0.5
                                                                                                                                                 
        colors_err[A_band == -99] = 1000000.   
        colors_err[B_band == -99] = 1000000.   
        colors_err[A_band == 99] = 1000000.   
        colors_err[B_band == 99] = 1000000.   
        colors[A_band == -99] = 0.   
        colors[B_band == -99] = 0.   
        colors[A_band == 99] = 0.   
        colors[B_band == 99] = 0.   
                                                                                                                                                 
        print colors.shape, locus_matrix.shape
                                                                                                                                                 
        from copy import copy
                                                                                                                                                 
        print good_test
                                                                                                                                                 
        #colors = colors[good_test > 1]
        #colors_err = colors_err[good_test > 1]
        #locus_matrix = locus_matrix[good_test > 1]
        stars_good = len(locus_matrix)
        good = scipy.ones(A_band.shape) 
        good[A_band == -99] = 0
        good[B_band == -99] = 0
        print good.sum(axis=2).sum(axis=1).sum(axis=0)
        #good = good[:,0,:]
        good_test = good[:,0,:].sum(axis=1)
        good = good[good_test > 1]
        star_mag_num = good[:,0,:].sum(axis=1)

        itr = 0

        
        def errfunc(pars,residuals=False):
            stat_tot = 0
            #for i in range(len(table.field('MAG_' + magtype + '-' + complist[0][0][0])[:100])):    
                #print i
                #print 'MAG_' + magtype + '-' + a                                 
                #print a,b                                           
                #print table.field('MAG_ISO-' + a)                    
                #print magtype, 'MAG_' + magtype + '-' + a
                                                                                                                                                 
            if 1:                
                A_zp = scipy.swapaxes(scipy.swapaxes(scipy.array(loci*[stars_good*[[assign_zp(a[0][1],pars,zps) for a in complist]]]),0,1),0,0)
                B_zp = scipy.swapaxes(scipy.swapaxes(scipy.array(loci*[stars_good*[[assign_zp(a[1][1],pars,zps) for a in complist]]]),0,1),0,0)
                colors_zp = A_zp- B_zp
                #print colors_zp.shape
                #print locus_matrix.shape
                #print colors.shape
                #print colors_zp[0][:][0]
                                                                                                                                                 
                #print colors[2][0], colors.shape
                #print locus_matrix[2][0], locus_matrix.shape
                print colors_zp.shape, colors.shape, locus_matrix.shape
                                                                                                                                                 
                ds_prelim = ((colors - locus_matrix - colors_zp)**2.)
                ds_prelim[good == 0] = 0.
                #print ds_prelim[2][0], 'ds_prelim'
                ds = (ds_prelim.sum(axis=2))**0.5
                #print ds[2][0]
                                                                                                                                                 
                ''' formula from High 2009 '''
                dotprod = abs((colors - locus_matrix - colors_zp) * colors_err)
                dotprod[good == 0] = 0. # set error to zero for poor measurements not in fit
                dotprod_sum = dotprod.sum(axis=2)
                                                                                                                                                 
                sum_diff = ds**2./dotprod_sum 
                #sum_diff = ds / colors_err
                #print sum_diff[2], 'sum_diff'
                #print c_diff[2][0], 'c_diff'
                dist = ds.min(axis=1)
                select_diff = sum_diff.min(axis=1)
                #print select_diff, 'select_diff'
                #select_diff_norm = select_diff #/star_mag_num         
                #print select_diff_norm, 'select_diff_norm'
                stat_tot = select_diff.sum()
                #print stat_tot, 'stat_tot'

            print pars, stat_tot#, zps
            print zps_list_full
            if residuals: return select_diff, dist
            else: return stat_tot
                                                                                                                                                 
        import pylab
        #pylab.ion()


        def plot(pars):

            A_zp = scipy.swapaxes(scipy.swapaxes(scipy.array(loci*[stars_good*[[assign_zp(a[0][1],pars,zps) for a in complist]]]),0,1),0,0)
            B_zp = scipy.swapaxes(scipy.swapaxes(scipy.array(loci*[stars_good*[[assign_zp(a[1][1],pars,zps) for a in complist]]]),0,1),0,0)
            colors_zp = A_zp- B_zp
            import pylab                                                           
            pylab.clf()
            for i in range(len(complist)):
                print complist[i], i


            x_color = scipy.array((colors - colors_zp)[:,0,2].tolist())
            y_color = (colors - colors_zp)[:,0,0]
            x_err = (colors_err)[:,0,2]
            y_err = (colors_err)[:,0,0]
                                                                                    
            x_color = x_color[x_err<100]
            y_color = y_color[x_err<100]
            y_err = y_err[x_err<100]
            x_err = x_err[x_err<100]
                                                                                    
            x_color = x_color[y_err<100]
            y_color = y_color[y_err<100]
            x_err = x_err[y_err<100]
            y_err = y_err[y_err<100]
                                                                                    
            print len(x_color), len(x_color) 
            #raw_input()
                                                                                    
                                                                                    
            pylab.scatter(x_color,y_color)
            pylab.errorbar(x_color,y_color,xerr=x_err,yerr=y_err,fmt=None)
            pylab.scatter(locus_matrix[0,:,2],locus_matrix[0,:,0],color='red')
                                                                                    
            pylab.show()

        ''' now rerun after cutting outliers '''        
        if True:
            
            if iteration == 'full':
                if startingzps is None:
                    pinit = scipy.zeros(len(zps_list))
                else:
                    pinit = [startingzps[key] for key in zps_list]
            else:
                import random
                ''' add random offset of 1.0 mag '''
                pinit = [results['full'][key] + random.random()*1.0 for key in zps_list]

            #plot(pinit)
            from scipy import optimize
            out = scipy.optimize.fmin(errfunc,pinit,maxiter=100000,args=()) 
            print out

            #plot(out)
                                                                                                                                                 
            import scipy                                                              
            print zps_list        
            print 'starting'        
            print out
            residuals,dist  = errfunc(pars=[0.] + out,residuals=True)


            print dist.shape, residuals.shape
            raw_input()
                                                                                                                                                 
            print dist 
            print 'finished'
            print 'colors' , len(colors)
                                                                                      
            ''' first filter on distance '''
            colors = colors[dist < 1]
            colors_err = colors_err[dist < 1]
            locus_matrix = locus_matrix[dist < 1]
            good = good[dist < 1]
            residuals = residuals[dist < 1]
                                                                                      
            ''' filter on residuals '''
            colors = colors[residuals < 6]
            colors_err = colors_err[residuals < 6]
            locus_matrix = locus_matrix[residuals < 6]
            good = good[residuals < 6]
                                                                                      
            stars_good = len(locus_matrix)
            star_mag_num = good[:,0,:].sum(axis=1)
                                                                                                                                                 
            print 'colors' , len(colors)

        pinit = out #scipy.zeros(len(zps_list))                         
        from scipy import optimize
        out = scipy.optimize.fmin(errfunc,pinit,args=()) 
        print out

        #plot(out)
        results[iteration] = dict(zip(zps_list_full,([0.] + out.tolist())))

                                                                  
                                                                  
    print results    

    errors = {}
                                                              
    import scipy
    print 'BOOTSTRAPPING ERRORS:'
    for key in zps_list_full:
        l = []
        for r in results.keys():
            if r != 'full':
                l.append(results[r][key])
        print key+':', scipy.std(l), 'mag'
        errors[key] = scipy.std(l)

    if False:
        def save_results(save_file,results,errors):                                               
            f = open(save_file,'w')
            for key in results['full'].keys():
                f.write(key + ' ' + str(results['full'][key]) + ' +- ' + str(errors[key]) + '\n')
            f.close()
                                                                                                  
            import pickle 
            f = open(save_file + '.pickle','w')
            m = pickle.Pickler(f)
            pickle.dump({'results':results,'errors':errors},m)
            f.close()

        if results.has_key('full') and save_results is not None: save_results(save_file,results, errors)
                                                              
    return results
Example #44
0
def plot_people_paths(ifig,
                      dt,
                      pixel_size,
                      people,
                      domain,
                      results,
                      axis=None,
                      savefig=False,
                      filename='fig.png'):
    """
    To draw all the individual paths from intial time to final time

    Parameters
    ----------

    ifig: int
        figure number
    dt: float
        time step
    pixel_size: float
        size of one pixel in meters
    people: numpy masked arrays
        equal to 1 if the cell (i,j) is occupied, 0 elsewhere
    domain: Domain
        contains everything for managing the domain
    results: numpy array
        positions at each time
    axis: list
        matplotlib axis
    savefig: boolean
        writes the figure as a png file if true
    filename: string
        png filename used to write the figure
    """
    Np = results.shape[0]
    fig = plt.figure(ifig)
    fig.clf()
    ax1 = fig.add_subplot(111)
    ax1.imshow(domain.image,
               interpolation='nearest',
               extent=[domain.xmin, domain.xmax, domain.ymin, domain.ymax],
               origin='lower')
    I0 = results[:, 0, 0]
    J0 = results[:, 1, 0]
    exit_times = compute_exit_times(dt, people, results)
    tmp = domain.door_distance.copy()
    tmp[I0, J0] = exit_times
    people_init = sp.ma.MaskedArray(sp.zeros(people.shape, dtype=int),
                                    mask=domain.mask)
    people_init.data[I0, J0] = 1
    vmin = (tmp * (people_init == 1)).min()
    vmax = (tmp * (people_init == 1)).max()
    cmap = cm.Greys
    ax1.imshow(tmp * (people_init == 1),
               interpolation='nearest',
               extent=[domain.xmin, domain.xmax, domain.ymin, domain.ymax],
               origin='lower',
               alpha=1.0,
               cmap=cmap,
               norm=colors.BoundaryNorm(boundaries=sp.linspace(
                   vmin, vmax, cmap.N + 1),
                                        ncolors=cmap.N))
    mask = (results == -1)
    traj = sp.swapaxes(sp.ma.MaskedArray(results, dtype=int, mask=mask), 1, 2)
    traj = traj[:, :, [1, 0]]
    col = sp.array([])
    for id in sp.arange(Np):
        ns = sp.where(results[id, 0, :] != -1)[0].shape[0] - 1
        col = sp.hstack((col, sp.ones(ns) * exit_times[id]))
    paths = LineCollection(0.5*pixel_size+pixel_size*traj, linewidths=2,
                           linestyle="solid", cmap=cmap,
                           norm=colors.BoundaryNorm( \
                                boundaries=sp.linspace(vmin,vmax,cmap.N+1), \
                                ncolors=cmap.N) \
                          )
    paths.set_array(exit_times)
    ax1.add_collection(paths)
    if (axis):
        ax1.set_xlim(axis[0], axis[1])
        ax1.set_ylim(axis[2], axis[3])
    ax1.set_xticks([])
    ax1.set_yticks([])
    ax1.axis('off')
    # ax1.set_xlabel('x',color='white')
    # ax1.set_ylabel('y',color='white')
    # ax1.set_title('t',color='white')
    # ax1.spines['left'].set_color('white')
    # ax1.spines['right'].set_color('white')
    # ax1.spines['bottom'].set_color('white')
    # ax1.spines['top'].set_color('white')
    #ax1.axis('off')
    fig.canvas.draw()
    if (savefig):
        fig.savefig(filename, dpi=150, bbox_inches='tight', pad_inches=0)
    plt.pause(0.01)
Example #45
0
# -*- coding: utf-8 -*-
"""
Created on Sun Oct 06 12:37:36 2013

@author: Gebruiker
"""

from ProcessCEST.CESTimports import *
from ProcessImages.ReadInput import *
import math

import scipy

fieldmap = Bruker2AnalyzeImg('TD_GlutCEST_t02_lZ1_9_1')
fieldmap.data = scipy.swapaxes(
    fieldmap.data, 1, 2
)  #fieldmap array is ordered differently. This puts it in the same order as e.g. the voxel size

filelist = [x.replace('.img', '') for x in glob.glob('*.img')
            ]  #cool list comprehension that gets all files
cestimg = Bruker2AnalyzeImg('TD_GlutCEST_t02_lZ1_15_1')
cestarray = scansToArray(filelist, start=15)
cestimg.data = cestarray

fieldmap.resampleImage(
    cestimg)  #resamples fieldmap to match CEST image resolution
fieldmap.data = fillEdges(fieldmap, cestimg)
fieldmapslice = getCorrespondingSlice(fieldmap, cestimg)

offsimg1, pseudofieldmap1, indexerrors1 = correctB0(cestimg.data,
                                                    ranges,
Example #46
0
def apply_chords(im, spacing=1, axis=0, trim_edges=True, label=False):
    r"""
    Adds chords to the void space in the specified direction.  The chords are
    separated by 1 voxel plus the provided spacing.

    Parameters
    ----------
    im : ND-array
        An image of the porous material with void marked as ``True``.

    spacing : int
        Separation between chords.  The default is 1 voxel.  This can be
        decreased to 0, meaning that the chords all touch each other, which
        automatically sets to the ``label`` argument to ``True``.

    axis : int (default = 0)
        The axis along which the chords are drawn.

    trim_edges : bool (default = ``True``)
        Whether or not to remove chords that touch the edges of the image.
        These chords are artifically shortened, so skew the chord length
        distribution.

    label : bool (default is ``False``)
        If ``True`` the chords in the returned image are each given a unique
        label, such that all voxels lying on the same chord have the same
        value.  This is automatically set to ``True`` if spacing is 0, but is
        ``False`` otherwise.

    Returns
    -------
    image : ND-array
        A copy of ``im`` with non-zero values indicating the chords.

    See Also
    --------
    apply_chords_3D

    """
    if spacing < 0:
        raise Exception('Spacing cannot be less than 0')
    if spacing == 0:
        label = True
    result = sp.zeros(im.shape, dtype=int)  # Will receive chords at end
    slxyz = [slice(None, None, spacing * (axis != i) + 1) for i in [0, 1, 2]]
    slices = tuple(slxyz[:im.ndim])
    s = [[0, 1, 0], [0, 1, 0], [0, 1, 0]]  # Straight-line structuring element
    if im.ndim == 3:  # Make structuring element 3D if necessary
        s = sp.pad(sp.atleast_3d(s),
                   pad_width=((0, 0), (0, 0), (1, 1)),
                   mode='constant',
                   constant_values=0)
    im = im[slices]
    s = sp.swapaxes(s, 0, axis)
    chords = spim.label(im, structure=s)[0]
    if trim_edges:  # Label on border chords will be set to 0
        chords = clear_border(chords)
    result[slices] = chords  # Place chords into empty image created at top
    if label is False:  # Remove label if not requested
        result = result > 0
    return result
import lmdb
import argparse
import sys
sys.path.insert(0, "/home/ogaki/Workspace/caffe/python")
import caffe
import cv2
import scipy


parser = argparse.ArgumentParser()
parser.add_argument("db")
parser.add_argument("kvfile")
parser.add_argument("imgdir")
args = parser.parse_args()

db = lmdb.open(path=args.db, map_size=1024*1024*1024)
db.open_db()

with db.begin(write=True) as buf:
    for i, line in enumerate(open(args.kvfile)):
        if i % 10000 == 0: print >> sys.stderr, i
        key = line.split()[0]
        imgfilepath = line.split()[1]
        #img = cv2.imread(args.imgdir+"/"+imgfilepath).transpose([2,0,1])
        img = caffe.io.load_image(args.imgdir+"/"+imgfilepath).astype(float)
        img = scipy.swapaxes(img, 0, 2)
        
        datum = caffe.io.array_to_datum(img)
        buf.put(key, datum.SerializeToString())
Example #48
0
def window(x, windowname = 'hanning', normalize = False):
    """returns w
    Create a  window of length :math:`x`, or a hanning window sized to match :math:`x`
    such that x*w is the windowed result.
    
    Parameters
 
    x:                 1) Integer. Number of points in desired hanning windows.
                       2) Array to which window needs to be applied. 
    windowname:        One of: hanning, hamming, blackman, flatwin, boxwin
    normalize (False): Adjust power level (for use in ASD) to 1

    Returns
 
    w: 1) hanning window array of size x
       2) windowing array. Windowed array is then x*w

    :Example:

    >>> import numpy as np
    >>> import vibrationtesting as vt
    >>> import matplotlib.pyplot as plt
    >>> sample_freq = 1e3
    >>> tfinal = 5
    >>> fs = 100
    >>> A = 10
    >>> freq = 5
    >>> noise_power = 0.001 * sample_freq / 2
    >>> time = np.reshape(np.arange(0, tfinal, 1/sample_freq),(1,-1))
    >>> xsin = A*np.sin(2*np.pi*freq*time)
    >>> xcos = A*np.cos(2*np.pi*freq*time)
    # assembling individual records. vstack assembles channels
    >>> x=np.dstack((xsin,xcos)) # assembling individual records. vstack
    >>> xw=vt.hanning(x)*x
    >>> plt.subplot(2, 1, 1)
    <matplotlib.axes._subplots.AxesSubplot object at ...>
    >>> plt.plot(time.T,x.T)
    [<matplotlib.lines.Line2D object at ...>]
    >>> plt.ylim([-20, 20])
    (-20, 20)
    >>> plt.title('Unwindowed data, 2 records.')
    <matplotlib.text.Text object at ...>
    >>> plt.ylabel('$x(t)$')
    <matplotlib.text.Text object at ...>
    >>> plt.subplot(2, 1, 2)
    <matplotlib.axes._subplots.AxesSubplot object at ...>
    >>> plt.title('Original (raw) data.')
    <matplotlib.text.Text object at ...>
    >>> plt.plot(time[0,:],xw[0,:],time[0,:],vt.hanning(x)[0,:]*A,'--',time[0,:],-vt.hanning(x)[0,:]*A,'--')
    [<matplotlib.lines.Line2D object at ...>]
    >>> plt.ylabel('Hanning windowed $x(t)$')
    <matplotlib.text.Text object at ...>
    >>> plt.xlabel('time')
    <matplotlib.text.Text object at ...>
    >>> plt.title('Effect of window. Note the scaling to conserve ASD amplitude')
    <matplotlib.text.Text object at ...>
    >>> plt.show()
    
    """
    
    if isinstance(x,(list, tuple, np.ndarray)):
        # Create Hanning windowing array of dimension n by N by nr
        # where N is number of data points and n is the number of number of inputs or outputs.
        # and nr is the number of records.
        
        swap = 0
        if len(x.shape) == 1:
            # We have either a scalar or 1D array
            if x.shape[0] == 1:
                print(" x is a scalar... and shouldn\'t have entered this part of the loop.")
            else:
                N = len(x)
            
            f = self.window(N , windowname = windowname)
            
        elif len(x.shape)==3:
            
            if x.shape[0]>x.shape[1]:
                x = sp.swapaxes(x,0,1)
                swap = 1
                print('Swapping axes temporarily to be compliant with expectations. I\'ll fix them in your result')

            f=window(N , windowname = windowname)
            f,_, _=np.meshgrid(f, np.arange(x.shape[0]),np.arange(x.shape[2]))
            if swap == 1:
                f = sp.swapaxes(f,0,1)
            
        elif len(x.shape)==2:
            #f,_=np.meshgrid(f[0,:],np.arange(x.shape[0]))
            #print('b')

            if x.shape[0]>x.shape[1]:
                x = sp.swapaxes(x,0,1)
                swap = 1
                print('Swapping axes temporarily to be compliant with expectations. I\'ll fix them in your result')

            f = window(x.shape[1] , windowname = windowname)
            f, _ = np.meshgrid(f, np.arange(x.shape[0]))
            if swap == 1:
                f = sp.swapaxes(f,0,1)
            
    else:
        #print(x)
        # Create hanning window of length x
        N = x
        #print(N)
        if windowname is 'hanning':
            f = np.sin(np.pi * np.arange(N)/(N-1))**2 * np.sqrt(8/3)
        elif windowname is 'hamming':
            f=(0.54-0.46*np.cos(2*np.pi*(np.arange(N))/(N-1)))*np.sqrt(5000/1987)
        elif windowname is 'blackman':
            print('blackman')
            f=(0.42-0.5*np.cos(2*np.pi*(np.arange(N)+.5)/(N))+.08*np.cos(4*np.pi*(np.arange(N)+.5)/(N)))*np.sqrt(5000/1523)
        elif windowname is 'flatwin':
            f=1.0-1.933*np.cos(2*np.pi*(np.arange(N))/(N-1))+1.286*np.cos(4*np.pi*(np.arange(N))/(N-1))-0.338*np.cos(6*np.pi*(np.arange(N))/(N-1))+0.032*np.cos(8*np.pi*(np.arange(N))/(N-1))
        elif windowname is 'boxwin':
            f=np.ones((1,N))
        else:
            f = np.ones((1,N))
            print("I don't recognize that window name. Sorry")
            
        if normalize is True:
            f = f/np.linalg.norm(f) * np.sqrt(N)
    return f