Ejemplo n.º 1
0
    def process_image(self, scanparams, pointparams, edf):
        delta, omega, alfa, beta, chi, phi, mon, transm = pointparams
        wavelength, UB = scanparams

        image = edf.GetData(0)
        header = edf.GetHeader(0)

        weights = numpy.ones_like(image)

        if not self.config.centralpixel:
            self.config.centralpixel = (int(header['y_beam']), int(header['x_beam']))
        if not self.config.sdd:
            self.config.sdd = float(header['det_sample_dist'])

        if self.config.background:
            data = image / mon
        else:
            data = image / mon / transm

        if mon == 0:
            raise errors.BackendError('Monitor is zero, this results in empty output. Scannumber = {0}, pointnumber = {1}. Did you forget to open the shutter?'.format(self.dbg_scanno, self.dbg_pointno)) 

        util.status('{4}| beta: {0:.3f}, delta: {1:.3f}, omega: {2:.3f}, alfa: {3:.3f}'.format(beta, delta, omega, alfa, time.ctime(time.time())))

        # pixels to angles
        pixelsize = numpy.array(self.config.pixelsize)
        sdd = self.config.sdd 

        app = numpy.arctan(pixelsize / sdd) * 180 / numpy.pi

        centralpixel = self.config.centralpixel # (column, row) = (delta, gamma)
        beta_range= -app[1] * (numpy.arange(data.shape[1]) - centralpixel[1]) + beta
        delta_range= app[0] * (numpy.arange(data.shape[0]) - centralpixel[0]) + delta

        # masking
        if self.config.maskmatrix is not None:
            if self.config.maskmatrix.shape != data.shape:
                raise errors.BackendError('The mask matrix does not have the same shape as the images')
            weights *= self.config.maskmatrix

        delta_range = delta_range[self.config.ymask]
        beta_range = beta_range[self.config.xmask]

        weights = self.apply_mask(weights, self.config.xmask, self.config.ymask)
        intensity = self.apply_mask(data, self.config.xmask, self.config.ymask)

        intensity = numpy.rot90(intensity)
        intensity = numpy.fliplr(intensity)
        intensity = numpy.flipud(intensity)

        weights = numpy.rot90(weights)
        weights = numpy.fliplr(weights)
        weights = numpy.flipud(weights)

        #polarisation correction
        delta_grid, beta_grid = numpy.meshgrid(delta_range, beta_range)
        Pver = 1 - numpy.sin(delta_grid * numpy.pi / 180.)**2 * numpy.cos(beta_grid * numpy.pi / 180.)**2
        #intensity /= Pver
 
        return intensity, weights, (wavelength, UB, beta_range, delta_range, omega, alfa, chi, phi)
Ejemplo n.º 2
0
def state_index_number(dims, index):
    """
    Return a quantum number representation given a state index, for a system
    of composite structure defined by dims.

    Example:

        >>> state_index_number([2, 2, 2], 6)
        [1, 1, 0]

    Parameters
    ----------
    dims : list or array
        The quantum state dimensions array, as it would appear in a Qobj.

    index : integer
        The index of the state in standard enumeration ordering.

    Returns
    -------
    state : list
        The state number array corresponding to index `index` in standard
        enumeration ordering.

    """
    state = np.empty_like(dims)

    D = np.concatenate([np.flipud(np.cumprod(np.flipud(dims[1:]))), [1]])

    for n in range(len(dims)):
        state[n] = index / D[n]
        index -= state[n] * D[n]

    return list(state)
Ejemplo n.º 3
0
def data_augmentation(image, mode):
    if mode == 0:
        # original
        return image
    elif mode == 1:
        # flip up and down
        return np.flipud(image)
    elif mode == 2:
        # rotate counterwise 90 degree
        return np.rot90(image)
    elif mode == 3:
        # rotate 90 degree and flip up and down
        image = np.rot90(image)
        return np.flipud(image)
    elif mode == 4:
        # rotate 180 degree
        return np.rot90(image, k=2)
    elif mode == 5:
        # rotate 180 degree and flip
        image = np.rot90(image, k=2)
        return np.flipud(image)
    elif mode == 6:
        # rotate 270 degree
        return np.rot90(image, k=3)
    elif mode == 7:
        # rotate 270 degree and flip
        image = np.rot90(image, k=3)
        return np.flipud(image)
Ejemplo n.º 4
0
def produce_heatmap(model, every = True, save = False):
    col_label = range(28)
    row_label = range(28)
    if every:
        for i in range(10):
            plt.pcolor(np.flipud(model[i]))
            plt.xticks(col_label)
            plt.yticks(row_label)
            plt.axis('off')
            plt.title("HeatMap for %d" % (i))
            cb = plt.colorbar()
            cb.set_label("Frequency")
            if save:
                plt.savefig('imgs/%d.png' % (i), bbox_inches='tight')
            else:
                plt.show()
            plt.close()
    else:
        plt.pcolor(np.flipud(model))
        plt.xticks(col_label)
        plt.yticks(row_label)
        plt.axis('off')
        cb = plt.colorbar()
        cb.set_label("Frequency")
        if save:
            plt.savefig('imgs/temp.png', bbox_inches='tight')
        else:
            plt.show()
        plt.close()
Ejemplo n.º 5
0
def rotate_data(bg, overlay, slices_list, axis_name, shape):
    # Rotate the data as required
    # Return the rotated data, and an updated slice list if necessary
    if axis_name == 'axial':
        # Align so that right is right
        overlay = np.rot90(overlay)
        overlay = np.fliplr(overlay)
        bg = np.rot90(bg)
        bg = np.fliplr(bg)
    
    elif axis_name == 'coronal':
        overlay = np.rot90(overlay)
        bg = np.rot90(bg)
        overlay = np.flipud(np.swapaxes(overlay, 0, 2))
        bg = np.flipud(np.swapaxes(bg, 0, 2))
        slices_list[1] = [ shape - n - 3 for n in slices_list[1] ] 
        
    elif axis_name == 'sagittal':
        overlay = np.flipud(np.swapaxes(overlay, 0, 2))
        bg = np.flipud(np.swapaxes(bg, 0, 2))
    
    else:
        print '\n************************'
        print 'ERROR: data could not be rotated\n'
        parser.print_help()
        sys.exit()
    
    return bg, overlay, slices_list
Ejemplo n.º 6
0
 def calculate_violin(self):
     
     # Get stats
     stats = self._stats
     
     # Get kernel density estimate
     nbins = stats.best_number_of_bins(8, 128)
     centers, values = stats.kde(nbins)
     
     # Normalize values
     values = values * (0.5 * self._width / values.max())
     
     # Create array with locations
     n = values.size
     points = np.zeros((n*2+1,3), np.float32)
     points[:n,0] = values
     points[:n,1] = centers
     points[n:2*n,0] = -np.flipud(values)
     points[n:2*n,1] = np.flipud(centers)
     points[2*n,0] = values[0]
     points[2*n,1] = centers[0]
     #
     self._points = points
     
     # Update limits
     self._limits = vv.Range(centers[0], centers[-1])
Ejemplo n.º 7
0
    def get_output(self, idx):
        img_id=idx/self.pertnum
        pert_id=idx%self.pertnum
        rot_id=pert_id%self.param['rotate']
        off_id=pert_id/self.param['rotate']
        [h, w]=self.output[img_id].shape
        [dy, dx]=self.get_offset(h, w, off_id)
        dy+=self.param['mrgsize']
        dx+=self.param['mrgsize']
        res=self.output[img_id][dy:dy+self.param['outsize'], dx:dx+self.param['outsize']]
        #res=np.rot90(res) #rotate 90
        if rot_id==1:
            res=np.fliplr(res)
        elif rot_id==2:
            res=np.flipud(res).T
        elif rot_id==3:
            res=res.T
        elif rot_id==4:
            res=np.fliplr(res).T
        elif rot_id==5:
            res=np.flipud(res)
        elif rot_id==6:
            res=np.rot90(res,2)
        elif rot_id==7:
            res=np.rot90(res,2).T

        return res
Ejemplo n.º 8
0
def put_image_quadrants (Q,odd_size=True):
    """
    Reassemble image from 4 quadrants Q = (Q0, Q1, Q2, Q3)
    The reverse process to get_image_quadrants()
    Qi defined in abel.hansenlaw.iabel_hansenlaw
    
    Parameters:
      - Q: tuple of numpy array quadrants
      - even_size: boolean, whether final image is even or odd pixel size
                   odd size requires trimming 1 row from Q1, Q0, and
                                              1 column from Q1, Q2

    Returns:  
      - rows x cols numpy array - the reassembled image
    """


    if not odd_size:
        Top    = np.concatenate((np.fliplr(Q[1]), Q[0]), axis=1)
        Bottom = np.flipud(np.concatenate((np.fliplr(Q[2]), Q[3]), axis=1))
    else:
        # odd size image remove extra row/column added in get_image_quadrant()
        Top    = np.concatenate((np.fliplr(Q[1][:-1,:-1]), Q[0][:-1,:]), axis=1)
        Bottom = np.flipud(np.concatenate((np.fliplr(Q[2][:,:-1]), Q[3]), axis=1))

    IM = np.concatenate((Top,Bottom), axis=0)

    return IM
Ejemplo n.º 9
0
def generateWedge(a, b, n, type, plotShape=False):
    # Define x and y coordinate
    xFrontTop_ = np.linspace(0, a / 2.0, n);                    xFrontTop = xFrontTop_
    xBackTop_ = np.linspace(a / 2.0, a, n);                     xBackTop = xBackTop_[1:]
    xBackBottom_ = np.flipud(np.linspace(a / 2.0, a, n));       xBackBottom = xBackBottom_[1:]
    xFrontBottom_ = np.flipud(np.linspace(0, a / 2.0, n));      xFrontBottom = xFrontBottom_[1:-1]

    yFrontTop_ = b / a * xFrontTop_;                            yFrontTop = yFrontTop_
    yBackTop_ = -b / a * (xBackTop_ - a / 2.0) + b / 2;         yBackTop = yBackTop_[1:]
    yBackBottom_ = b / a * (xBackBottom_ - a / 2.0) - b / 2;    yBackBottom = yBackBottom_[1:]
    yFrontBottom_ = -b / a * xFrontBottom_;                     yFrontBottom = yFrontBottom_[1:-1]

    if type == 'twoSided':
        # x = np.concatenate((xFrontTop, xBackTop, xBackBottom, xFrontBottom))
        # y = np.concatenate((yFrontTop, yBackTop, yBackBottom, yFrontBottom))
        xTop = np.concatenate((xFrontTop, xBackTop))
        xBottom = np.concatenate((xBackBottom, xFrontBottom))
        yTop = np.concatenate((yFrontTop, yBackTop))
        yBottom = np.concatenate((yBackBottom, yFrontBottom))
    elif type == 'oneSided':
        x = np.concatenate((xFrontTop, xBackTop))
        y = np.concatenate((yFrontTop, yBackTop))

    if plotShape:
        plt.figure()
        plt.plot(x, y, 'k')
        plt.xlim([-5, 14])
        plt.ylim([-5, 5])
        plt.show()

    return (x, y)
Ejemplo n.º 10
0
    def wrapper(*args):
        x = args[0]
        w = args[1]
        if x.ndim == 3:
            w = np.flipud(w)
            w = np.transpose(w, (1, 2, 0))
            if args[3] == 'channels_last':
                x = np.transpose(x, (0, 2, 1))
        elif x.ndim == 4:
            w = np.fliplr(np.flipud(w))
            w = np.transpose(w, (2, 3, 0, 1))
            if args[3] == 'channels_last':
                x = np.transpose(x, (0, 3, 1, 2))
        else:
            w = np.flip(np.fliplr(np.flipud(w)), axis=2)
            w = np.transpose(w, (3, 4, 0, 1, 2))
            if args[3] == 'channels_last':
                x = np.transpose(x, (0, 4, 1, 2, 3))

        y = func(x, w, args[2], args[3])

        if args[3] == 'channels_last':
            if y.ndim == 3:
                y = np.transpose(y, (0, 2, 1))
            elif y.ndim == 4:
                y = np.transpose(y, (0, 2, 3, 1))
            else:
                y = np.transpose(y, (0, 2, 3, 4, 1))

        return y
Ejemplo n.º 11
0
def get_image_quadrants(IM, reorient=False):
    """
    Given an image (m,n) return its 4 quadrants Q0, Q1, Q2, Q3
    as defined in abel.hansenlaw.iabel_hansenlaw

    Parameters:
      - IM: 1D or 2D array
      - reorient: reorient image as required by abel.hansenlaw.iabel_hansenlaw
    """
    IM = np.atleast_2d(IM)

    n, m = IM.shape

    n_c = n//2 + n%2
    m_c = m//2 + m%2

    # define 4 quadrants of the image
    # see definition in abel.hansenlaw.iabel_hansenlaw
    Q1 = IM[:n_c, :m_c]
    Q2 = IM[-n_c:, :m_c]
    Q0 = IM[:n_c, -m_c:]
    Q3 = IM[-n_c:, -m_c:]

    if reorient:
        Q1 = np.fliplr(Q1)
        Q3 = np.flipud(Q3)
        Q2 = np.fliplr(np.flipud(Q2))

    return Q0, Q1, Q2, Q3
def apply_Gumbel_original(gumbelFile, trgFolder, prefix, return_periods, cellArea, logger):
    # read gumbel file
    nc_src = nc4.Dataset(gumbelFile, 'r')
    # read axes and revert the y-axis
    x = nc_src.variables['lon'][:]
    y = np.flipud(nc_src.variables['lat'][:])
    
    # read different variables
    loc   = nc_src.variables['flvol_location'][0,:,:]#; loc    = gumbel_loc.data;  loc[loc==gumbel_loc._FillValue]       = np.nan
    scale = nc_src.variables['flvol_scale'][0,:,:]#;    scale  = gumbel_scale.data;scale[scale==gumbel_scale._FillValue] = np.nan
    p_zero    = nc_src.variables['flvol_zero_prob'][0,:,:]#;p_zero = zero_prob.data;   p_zero[p_zero==zero_prob._FillValue]  = np.nan
    # loop over all return periods
    for return_period in return_periods:
        logger.info('Preparing return period %05.f' % return_period)
        flvol = inv_gumbel(p_zero, loc, scale, return_period)
        # any area with cell > 0, fill in a zero. This may occur because:
            # a) dynRout produces missing values (occuring in some pixels in the Sahara)
            # b) the forcing data is not exactly overlapping the cell area mask (e.g. EU-WATCH land cells are slightly different from PCR-GLOBWB mask)
            # c) the probability of zero flooding is 100%. This causes a division by zero in the inv_gumbel function
        test = logical_and(flvol.mask, cellArea > 0)
        flvol[test] = 0.
        test = logical_and(np.isnan(flvol), cellArea > 0)
        flvol[test] = 0.
        # if any values become negative due to the statistical extrapolation, fix them to zero (may occur if the sample size for fitting was small and a small return period is requested)
        flvol = np.maximum(flvol, 0.)
        # write to a PCRaster file
        flvol_data = flvol.data
        # finally mask the real not-a-number cells
        flvol_data[flvol.mask] = -9999.
        fileName = os.path.join(trgFolder, '%s_RP_%05.f.map') % (prefix, return_period)
        writeMap(fileName, 'PCRaster', x, y, np.flipud(flvol_data), -9999.)
    nc_src.close()
 def setUp(self):
     """
     This generates a minimum data-set to be used for the regression.
     """
     # Test A: Generates a data set assuming b=1 and N(m=4.0)=10.0 events
     self.dmag = 0.1
     mext = np.arange(4.0, 7.01, 0.1)
     self.mval = mext[0:-1] + self.dmag / 2.0
     self.bval = 1.0
     self.numobs = np.flipud(
         np.diff(np.flipud(10.0 ** (-self.bval * mext + 8.0))))
     # Test B: Generate a completely artificial catalogue using the
     # Gutenberg-Richter distribution defined above
     numobs = np.around(self.numobs)
     size = int(np.sum(self.numobs))
     magnitude = np.zeros(size)
     lidx = 0
     for mag, nobs in zip(self.mval, numobs):
         uidx = int(lidx + nobs)
         magnitude[lidx:uidx] = mag + 0.01
         lidx = uidx
     year = np.ones(size) * 1999
     self.catalogue = Catalogue.make_from_dict(
         {'magnitude': magnitude, 'year': year})
     # Create the seismicity occurrence calculator
     self.aki_ml = AkiMaxLikelihood()
Ejemplo n.º 14
0
def polysmooth(x,y,z,NI,NJ):

    # size of the incoming array
    Nx, Ny = np.shape(z)
    x1d = x[:,0]
    y1d = y[0,:]

    # Get the C coefficients
    #NI = 7
    CIj = np.zeros((NI,Ny))
    for j in range (Ny):
        CIj[:,j] = np.flipud(np.polyfit(x1d,z[:,j],NI-1))

    # Get the D coefficients
    #NJ = 7
    DIJ = np.zeros((NI,NJ))
    for I in range (NI):
        DIJ[I,:] = np.flipud(np.polyfit(y1d,CIj[I,:],NJ-1))
    
    # Reconstruct the entire surface
    zsmooth = np.zeros((Nx,Ny))
    for I in range(NI):
        for J in range(NJ):
            zsmooth += DIJ[I,J]*x**I*y**J

    return zsmooth
Ejemplo n.º 15
0
    def update_lambda(self, sstats, word_list, opt_o):
        self.m_status_up_to_date = False
        # rhot will be between 0 and 1, and says how much to weight
        # the information we got from this mini-chunk.
        rhot = self.m_scale * pow(self.m_tau + self.m_updatect, -self.m_kappa)
        if rhot < rhot_bound:
            rhot = rhot_bound
        self.m_rhot = rhot

        # Update appropriate columns of lambda based on documents.
        self.m_lambda[:, word_list] = self.m_lambda[:, word_list] * (1 - rhot) + \
            rhot * self.m_D * sstats.m_var_beta_ss / sstats.m_chunksize
        self.m_lambda_sum = (1 - rhot) * self.m_lambda_sum + \
            rhot * self.m_D * np.sum(sstats.m_var_beta_ss, axis=1) / sstats.m_chunksize

        self.m_updatect += 1
        self.m_timestamp[word_list] = self.m_updatect
        self.m_r.append(self.m_r[-1] + np.log(1 - rhot))

        self.m_varphi_ss = (1.0 - rhot) * self.m_varphi_ss + rhot * \
            sstats.m_var_sticks_ss * self.m_D / sstats.m_chunksize

        if opt_o:
            self.optimal_ordering()

        ## update top level sticks
        self.m_var_sticks[0] = self.m_varphi_ss[:self.m_T - 1] + 1.0
        var_phi_sum = np.flipud(self.m_varphi_ss[1:])
        self.m_var_sticks[1] = np.flipud(np.cumsum(var_phi_sum)) + self.m_gamma
Ejemplo n.º 16
0
def generateBoundingBox(imap, reg, scale, t):
    # use heatmap to generate bounding boxes
    stride = 2
    cellsize = 12

    imap = np.transpose(imap)
    dx1 = np.transpose(reg[:, :, 0])
    dy1 = np.transpose(reg[:, :, 1])
    dx2 = np.transpose(reg[:, :, 2])
    dy2 = np.transpose(reg[:, :, 3])
    y, x = np.where(imap >= t)
    if y.shape[0] == 1:
        dx1 = np.flipud(dx1)
        dy1 = np.flipud(dy1)
        dx2 = np.flipud(dx2)
        dy2 = np.flipud(dy2)
    score = imap[(y, x)]
    reg = np.transpose(np.vstack([dx1[(y, x)], dy1[(y, x)], dx2[(y, x)], dy2[(y, x)]]))
    if reg.size == 0:
        reg = np.empty((0, 3))
    bb = np.transpose(np.vstack([y, x]))
    q1 = np.fix((stride * bb + 1) / scale)
    q2 = np.fix((stride * bb + cellsize - 1 + 1) / scale)
    boundingbox = np.hstack([q1, q2, np.expand_dims(score, 1), reg])
    return boundingbox, reg
Ejemplo n.º 17
0
	def _northup( self, latitude='lat' ):
		''' this works only for global grids to be downscaled flips it northup '''
		if self.ds[ latitude ][0].data < 0: # meaning that south is north globally
			self.ds[ latitude ] = np.flipud( self.ds[ latitude ] )
			# flip each slice of the array and make a new one
			flipped = np.array( [ np.flipud( arr ) for arr in self.ds[ self.variable ].data ] )
			self.ds[ self.variable ] = (('time', 'lat', 'lon' ), flipped )
Ejemplo n.º 18
0
def rkstab(A, b, xmin, xmax, ymax, n):
    """ Evaluate the stability region of a Runge-Kutta method
        given its coefficients A and b
    """

    d = b.size
    m = int(0.5 * n)
    x = np.linspace(xmin, xmax, n)
    y = np.linspace(0, ymax, m)
    X, Y = np.meshgrid(x, y)
    z = X + 1j * Y
    I = np.identity(d)
    e = np.ones(d)
    r = np.zeros((m, n), dtype=complex)

    for ix in xrange(n):
        for iy in xrange(m):
            s = solve(I - z[iy, ix] * A, e)
            r[iy, ix] = 1 + z[iy, ix] * np.dot(b, s)

    R = np.abs(r)
    X = np.vstack((np.flipud(X[1:, :]), X))
    Y = np.vstack((-np.flipud(Y[1:, :]), Y))
    R = np.vstack((np.flipud(R[1:, :]), R))
    return X, Y, R
Ejemplo n.º 19
0
  def _readDem(self):
    """
    Read coordinates defining DEM and create vectors of x, y, and z values.
    """

    # Load each coordinate as a numpy array.
    x, y, z = numpy.loadtxt(self.inputDem, dtype=numpy.float64, unpack=True)

    self.numZIn = len(z)
    if (y[0] == y[1]):
      # Ordered by rows.
      self.numXIn = max(numpy.argmax(x) + 1, numpy.argmin(x) + 1)
      self.xIn = x[0:self.numXIn]
      self.numYIn = self.numZIn/self.numXIn
      self.yIn = y[0:self.numZIn:self.numXIn]
      self.zIn = numpy.reshape(z, (self.numYIn, self.numXIn))
    else:
      # Ordered by columns.
      self.numYIn = max(numpy.argmax(y) + 1, numpy.argmin(y) + 1)
      self.yIn = y[0:self.numYIn]
      self.numXIn = self.numZIn/self.numYIn
      self.xIn = x[0:self.numZIn:self.numYIn]
      self.ZIn = numpy.transpose(numpy.reshape(z, (self.numXIn, self.numYIn)))

    if (self.xIn[0] > self.xIn[1]):
      self.xIn = numpy.flipud(self.xIn)
      self.zIn = numpy.fliplr(self.zIn)
    if (self.yIn[0] > self.yIn[1]):
      self.yIn = numpy.flipud(self.yIn)
      self.zIn = numpy.flipud(self.zIn)

    return
Ejemplo n.º 20
0
    def MatForChannel(signals, picHeight, picWidth):
        # signals is a vector! not a mat

        newSignals = signals * ((picHeight - 1) / 2)
        width = len(newSignals)
        stride = int(np.ceil(float(width) / picWidth))

        # pad
        temp = np.zeros(picWidth * stride)
        temp[0:width] = newSignals

        # 1 - reshape according to stride
        maxVec = temp.reshape((picWidth, stride))
        # 2 - get max vector of each stride
        maxVec = maxVec.max(axis=1).astype(int)
        # 3 - get max mat
        maxMat = np.zeros((picWidth, picHeight / 2))
        maxMat[np.arange(picWidth), maxVec] = 1

        # flip it when it's little...
        # TODO: maybe we can cheat  this part (but it's really fast)
        maxMat = maxMat.T
        maxMat = np.flipud(maxMat)

        # 4 - create fill matrix
        # TODO: can do a static mat of this
        fillMat = np.ones((picHeight / 2, picHeight / 2))
        fillMat = np.tril(fillMat, 0)

        # result
        endMat = np.dot(fillMat, maxMat)
        endMat = np.vstack((endMat, np.flipud(endMat)))

        return endMat
def mirror_edges(X, nPixels):
    assert(nPixels>0)
    
    [s, height, width] = X.shape
    Xm = np.zeros([s, height+2*nPixels, width+2*nPixels], dtype=X.dtype)
    
    Xm[:, nPixels:height+nPixels, nPixels:width+nPixels] = X
    
    for i in range(s):
        # top left corner
        Xm[i, 0:nPixels, 0:nPixels] = np.fliplr(np.flipud(X[i, 0:nPixels, 0:nPixels]))
        # top right corner
        Xm[i, 0:nPixels, width+nPixels:width+2*nPixels] = np.fliplr(np.flipud(X[i, 0:nPixels, width-nPixels:width]))
        # bottom left corner
        Xm[i, height+nPixels:height+2*nPixels, 0:nPixels] = np.fliplr(np.flipud(X[i, height-nPixels:height, 0:nPixels]))
        # bottom right corner        
        Xm[i, height+nPixels:height+2*nPixels, width+nPixels:width+2*nPixels] = np.fliplr(np.flipud(X[i, height-nPixels:height, width-nPixels:width]))
        # top
        Xm[i, 0:nPixels, nPixels:width+nPixels] = np.flipud(X[i, 0:nPixels, 0:width])
        # bottom
        Xm[i, height+nPixels:height+2*nPixels, nPixels:width+nPixels] = np.flipud(X[i, height-nPixels:height, 0:width])
        # left
        Xm[i, nPixels:height+nPixels, 0:nPixels] = np.fliplr(X[i, 0:height, 0:nPixels])
        # right
        Xm[i, nPixels:height+nPixels, width+nPixels:width+2*nPixels] = np.fliplr(X[i, 0:height, width-nPixels:width])
    return Xm
Ejemplo n.º 22
0
    def zwriter(self, imageslice, fname):
        if "OpenEXR" in self.backends:
            imageslice = numpy.flipud(imageslice)
            exr.save_depth(imageslice, fname)

        elif "VTK" in self.backends:
            height = imageslice.shape[1]
            width = imageslice.shape[0]

            file = open(fname, mode='w')
            file.write("Image type: L 32F image\r\n")
            file.write("Name: A cinema depth image\r\n")
            file.write("Image size (x*y): "+str(height) + "*" + str(width) + "\r\n")
            file.write("File size (no of images): 1\r\n")
            file.write(chr(26))
            imageslice.tofile(file)
            file.close()

        elif "PIL" in self.backends:
            imageslice = numpy.flipud(imageslice)
            pimg = PIL.Image.fromarray(imageslice)
            #TODO:
            # don't let ImImagePlugin.py insert the Name: filename in line two
            # why? because ImImagePlugin.py reader has a 100 character limit
            pimg.save(fname)

        else:
            print "Warning: need OpenEXR or PIL or VTK to write to " + fname
Ejemplo n.º 23
0
def plot_kmeans(dist_m, shape_port, dat_port, dat_star, dat_kclass, ft, humfile, sonpath, base, p):

   Zdist = dist_m[shape_port[-1]*p:shape_port[-1]*(p+1)]
   extent = shape_port[1] 

   levels = [0.5,0.75,1.25,1.5,1.75,2,3]

   fig = plt.figure()
   plt.subplot(2,1,1)
   ax = plt.gca()
   plt.imshow(np.vstack((np.flipud(dat_port), dat_star)), cmap='gray',extent=[min(Zdist), max(Zdist), -extent*(1/ft), extent*(1/ft)],origin='upper')
   
   CS = plt.contourf(np.flipud(dat_kclass), levels, alpha=0.4, extent=[min(Zdist), max(Zdist), -extent*(1/ft), extent*(1/ft)],origin='upper', cmap='YlOrRd', vmin=0.5, vmax=3)   
   plt.ylabel('Horizontal distance (m)')
   plt.xlabel('Distance along track (m)')
   plt.axis('tight')

   try:
      divider = make_axes_locatable(ax)
      cax = divider.append_axes("right", size="5%", pad=0.05)
      plt.colorbar(CS, cax=cax)
   except:
      plt.colorbar()

   custom_save(sonpath,base+'class_kmeans'+str(p))
   del fig
Ejemplo n.º 24
0
    def to_unit(self, unit):
        """Return wavelength and transmission in new wavelength units.

        If the requested units are the same as the current units, self is
        returned.

        Parameters
        ----------
        unit : `~astropy.units.Unit` or str
            Target wavelength unit.

        Returns
        -------
        wave : `~numpy.ndarray`
        trans : `~numpy.ndarray`
        """

        if unit is u.AA:
            return self.wave, self.trans

        d = u.AA.to(unit, self.wave, u.spectral())
        t = self.trans
        if d[0] > d[-1]:
            d = np.flipud(d)
            t = np.flipud(t)
        return d, t
Ejemplo n.º 25
0
def schedule_jobs(job_matrix, mode='ratio'):
    job_weight = job_matrix[:, 0]
    job_length = job_matrix[:, 1]
    if mode == 'difference':
        job_metric = numpy.subtract(job_weight, job_length)
        job_matrix = numpy.vstack((job_weight, job_length, job_metric)).T
        job_matrix = job_matrix[job_matrix[:, 2].argsort()]
        job_matrix = numpy.flipud(job_matrix)

        metric_value_freq = Counter(job_matrix[:, 2])

        # Break ties between jobs using weight (i.e. higher weight goes first)
        i = 0
        for key in sorted(metric_value_freq.keys(), reverse=True):
            subarray = job_matrix[i:i + metric_value_freq[key]]
            sorted_subarray = subarray[subarray[:, 0].argsort()]
            sorted_subarray = numpy.flipud(sorted_subarray)
            job_matrix[i:i + metric_value_freq[key]] = sorted_subarray
            i += metric_value_freq[key]

    elif mode == 'ratio':
        job_metric = numpy.divide(job_weight, job_length)

        job_matrix = numpy.vstack((job_weight, job_length, job_metric)).T
        job_matrix = job_matrix[job_matrix[:, 2].argsort()]
        job_matrix = numpy.flipud(job_matrix)

    return job_matrix
Ejemplo n.º 26
0
Archivo: bem.py Proyecto: NREL/OpenWARP
def _interpolate_for_irf(w_orig, w_interp, mat_in):
  '''
  Interpolate matrices for the IRF calculations
  '''
  mat_interp = np.zeros([mat_in.shape[0], mat_in.shape[1], w_interp.size])

  flip = False

  if w_orig[0] > w_orig[1]:

    w_tmp = np.flipud(w_orig)
    flip = True

  else:

    w_tmp = w_orig

  for i in xrange(mat_in.shape[0]):

    for j in xrange(mat_in.shape[1]):

      if flip is True:

        rdTmp = np.flipud(mat_in[i, j, :])

      else:
        rdTmp = mat_in[i, j, :]

      f = interpolate.interp1d(x=w_tmp, y=rdTmp)
      mat_interp[i, j, :] = f(w_interp)

  return mat_interp
    def testCircular(self):
        g = Graph()

        op = OpLazyCC(graph=g)
        op.ChunkShape.setValue((3, 3, 1))

        vol = np.asarray(
            [
                [0, 0, 0, 0, 0, 0, 0, 0, 0],
                [0, 1, 1, 1, 0, 1, 1, 1, 0],
                [0, 1, 0, 0, 0, 0, 0, 1, 0],
                [0, 1, 0, 0, 0, 0, 0, 1, 0],
                [0, 1, 0, 0, 0, 0, 0, 1, 0],
                [0, 1, 0, 0, 0, 0, 0, 1, 0],
                [0, 1, 0, 0, 0, 0, 0, 1, 0],
                [0, 1, 1, 1, 1, 1, 1, 1, 0],
                [0, 0, 0, 0, 0, 0, 0, 0, 0],
            ],
            dtype=np.uint8,
        )
        vol1 = vigra.taggedView(vol, axistags="yx")
        vol2 = vigra.taggedView(vol, axistags="xy")
        vol3 = vigra.taggedView(np.flipud(vol), axistags="yx")
        vol4 = vigra.taggedView(np.flipud(vol), axistags="xy")

        for v in (vol1, vol2, vol3, vol4):
            op.Input.setValue(v)
            for x in [0, 3, 6]:
                for y in [0, 3, 6]:
                    if x == 3 and y == 3:
                        continue
                    op.Input.setDirty(slice(None))
                    out = op.Output[x : x + 3, y : y + 3].wait()
                    print(out.squeeze())
                    assert out.max() == 1
Ejemplo n.º 28
0
def data_reorient_udrot(d, pl, ud=None, rot90=None):
    """
    """
    print >>sys.stderr, "Reorienting...",
    if (not ud is None) and (not rot90 is None):
        if ud > 0:
            d = np.flipud(d)
        if rot90 > 0:
            d = np.rot90(d, rot90)
        print >>sys.stderr, "done."
        return d
        
    ud = pl.get_val('REORIENT_CCD_FLIPUD')
    if ud == None:
        ud = pl.get_val('CORRECTION_FLIPUD')
    ud = int(ud)
    rot90 = pl.get_val('REORIENT_CCD_ROT90')
    if rot90 == None:
        rot90 = pl.get_val('CORRECTION_ROTATION')
    rot90 = int(rot90)
    if ud > 0:
        d = np.flipud(d)
    if rot90 > 0:
        d = np.rot90(d, rot90)
    print >>sys.stderr, "done."
    return d
Ejemplo n.º 29
0
Archivo: calc.py Proyecto: jcmt/okean
def var_border(v, di=1, dj=1):
    """
  Border of 2d numpy array
  di,dj is the interval between points along columns and lines
  Corner points are kept even with di and dj not 1
  """

    j, i = v.shape
    if (di, dj) == (1, 1):
        xb = np.arange(2 * i + 2 * j, dtype=v.dtype)
        yb = np.arange(2 * i + 2 * j, dtype=v.dtype)

        xb[0:j] = v[:, 0]
        xb[j : j + i] = v[-1, :]
        xb[j + i : j + i + j] = np.flipud(v[:, -1])
        xb[j + i + j :] = np.flipud(v[0, :])
    else:
        # ensure corner points are kept!!
        tmp1 = v[::dj, 0]
        tmp2 = v[-1, ::di]
        tmp3 = np.flipud(v[:, -1])[::dj]
        tmp4 = np.flipud(v[0, :])[::di]
        xb = np.concatenate((tmp1, tmp2, tmp3, tmp4))

    return xb
Ejemplo n.º 30
0
def filtfilt(b,a,x):
    """
    What does this function do?  Alberto??
    """
    
    #For now only accepting 1d arrays
    ntaps=max(len(a),len(b))
    edge=ntaps*3
        
    if x.ndim != 1:
        raise ValueError, "Filiflit is only accepting 1 dimension arrays."

    #x must be bigger than edge
    if x.size < edge:
        raise ValueError, "Input vector needs to be bigger than 3 * max(len(a),len(b)."
        
    if len(a) < ntaps:
	a=np.r_[a,zeros(len(b)-len(a))]

    if len(b) < ntaps:
	b=np.r_[b,zeros(len(a)-len(b))]         
    
    zi=lfilter_zi(b,a)
    
    #Grow the signal to have edges for stabilizing 
    #the filter with inverted replicas of the signal
    s=np.r_[2*x[0]-x[edge:1:-1],x,2*x[-1]-x[-1:-edge:-1]]
    #in the case of one go we only need one of the extrems 
    # both are needed for filtfilt
    
    (y,zf)=lfilter(b,a,s,-1,zi*s[0])

    (y,zf)=lfilter(b,a,np.flipud(y),-1,zi*y[-1])
    
    return np.flipud(y[edge-1:-edge+1])
Ejemplo n.º 31
0
# In[12]:

# exposure_time = 0.1 * u.s
# camera_model_str = 'Canon 70D'
# author_str = 'Julius Berkowski'
print(str(time))

time = time  #datetime(2017, 8, 21, 17, 27, 13) # don't forget to convert your time to UTC!
#gps = [44.37197, -116.87393] * u.deg # latitude, longitude of Mann Creek, Idaho

# ## Read in the image data

# In[14]:

# read in the image and flip it so that it's correct
im_rgb = np.flipud(matplotlib.image.imread(f))

#raw = rawpy.imread(f)
#im_rgb = np.flipud(raw.postprocess())

# remove color info
im = np.average(im_rgb, axis=2)

# In[15]:

plt.imshow(im, origin='lower')

# # Get info from the image

# We need the following information from the image
# * the location of the center of the Sun/Moon and
Ejemplo n.º 32
0
maitempif8=ma.filled(maitempif8, fill_value=0.)

maizetrof8=maitropf8+maitropif8
maizetemf8=maitempf8+maitempif8
maizetorf8=maitropf8+maitempf8
maizetoif8=maitropif8+maitempif8
maizetof8 = maitropf8+maitempf8+maitropif8+maitempif8

clmtropf=N.zeros((10,360,720))
clmtempf=N.zeros((10,360,720))
clmtropfi=N.zeros((10,360,720))
clmtempfi=N.zeros((10,360,720))

for j in range(0,10):
	clm=NetCDFFile('/scratch2/scratchdirs/tslin2/plot/globalcrop/data/clm/clm45rcp45/soytrop_rcp45_constco2_rf_nofert_0.5x0.5.nc','r')
	aa= N.flipud(clm.variables['yield'][84+j,:,:])
	clmtropf[j,:,:] = aa
	
	clm1=NetCDFFile('/scratch2/scratchdirs/tslin2/plot/globalcrop/data/clm/clm45rcp45/soytemp_rcp45_constco2_rf_nofert_0.5x0.5.nc','r')
	bb = N.flipud(clm1.variables['yield'][84+j,:,:])
	clmtempf[j,:,:] = bb

	clm2=NetCDFFile('/scratch2/scratchdirs/tslin2/plot/globalcrop/data/clm/clm45rcp45/soytrop_rcp45_co2_rf_nofert_0.5x0.5.nc','r')
	cc = N.flipud(clm2.variables['yield'][84+j,:,:])
	clmtropfi[j,:,:] = cc

	clm3=NetCDFFile('/scratch2/scratchdirs/tslin2/plot/globalcrop/data/clm/clm45rcp45/soytemp_rcp45_co2_rf_nofert_0.5x0.5.nc','r')
	dd = N.flipud(clm3.variables['yield'][84+j,:,:])
	clmtempfi[j,:,:] = dd

Ejemplo n.º 33
0
def do(now, realtime=False):
    """ Generate for this timestep! """
    szx = 7000
    szy = 3500
    # Create the image data
    imgdata = np.zeros((szy, szx), 'u1')
    metadata = {
        'start_valid': now.strftime("%Y-%m-%dT%H:%M:%SZ"),
        'end_valid': now.strftime("%Y-%m-%dT%H:%M:%SZ"),
        'product': 'lcref',
        'units': '0.5 dBZ'
    }

    fn = now.strftime("SeamlessHSR_00.00_%Y%m%d-%H%M00.grib2.gz")
    uri = now.strftime(("http://mtarchive.geol.iastate.edu/%Y/%m/%d/"
                        "mrms/ncep/SeamlessHSR/" + fn))
    gribfn = "%s/%s" % (TMP, fn)
    res = requests.get(uri, timeout=30)
    if res.status_code != 200:
        print("mrms_lcref_comp.py MISSING %s" % (gribfn, ))
        return
    o = open(gribfn, 'wb')
    o.write(res.content)
    o.close()

    fp = gzip.GzipFile(gribfn, 'rb')
    (_, tmpfn) = tempfile.mkstemp()
    tmpfp = open(tmpfn, 'wb')
    tmpfp.write(fp.read())
    tmpfp.close()
    grbs = pygrib.open(tmpfn)
    grb = grbs[1]
    os.unlink(tmpfn)
    os.unlink(gribfn)

    val = grb['values']

    # -999 is no coverage, go to 0
    # -99 is missing , go to 255

    val = np.where(val >= -32, (val + 32) * 2.0, val)
    # val = np.where(val < -990., 0., val)
    # val = np.where(val < -90., 255., val)
    # This is an upstream BUG
    val = np.where(val < 0., 0., val)
    imgdata[:, :] = np.flipud(val.astype('int'))

    (tmpfp, tmpfn) = tempfile.mkstemp()

    # Create Image
    png = Image.fromarray(np.flipud(imgdata))
    png.putpalette(make_colorramp())
    png.save('%s.png' % (tmpfn, ))

    mrms.write_worldfile('%s.wld' % (tmpfn, ))
    # Inject WLD file
    prefix = 'lcref'
    pqstr = ("/home/ldm/bin/pqinsert -i -p 'plot ac %s "
             "gis/images/4326/mrms/%s.wld GIS/mrms/%s_%s.wld wld' %s.wld") % (
                 now.strftime("%Y%m%d%H%M"), prefix, prefix,
                 now.strftime("%Y%m%d%H%M"), tmpfn)
    subprocess.call(pqstr, shell=True)
    # Now we inject into LDM
    pqstr = ("/home/ldm/bin/pqinsert -i -p 'plot ac %s "
             "gis/images/4326/mrms/%s.png GIS/mrms/%s_%s.png png' %s.png") % (
                 now.strftime("%Y%m%d%H%M"), prefix, prefix,
                 now.strftime("%Y%m%d%H%M"), tmpfn)
    subprocess.call(pqstr, shell=True)
    # Create 900913 image
    cmd = ("gdalwarp -s_srs EPSG:4326 -t_srs EPSG:3857 -q -of GTiff "
           "-tr 1000.0 1000.0 %s.png %s.tif") % (tmpfn, tmpfn)
    subprocess.call(cmd, shell=True)
    # Insert into LDM
    pqstr = ("/home/ldm/bin/pqinsert -i -p 'plot c %s "
             "gis/images/900913/mrms/%s.tif GIS/mrms/%s_%s.tif tif' %s.tif"
             ) % (now.strftime("%Y%m%d%H%M"), prefix, prefix,
                  now.strftime("%Y%m%d%H%M"), tmpfn)
    subprocess.call(pqstr, shell=True)

    j = open("%s.json" % (tmpfn, ), 'w')
    j.write(json.dumps(dict(meta=metadata)))
    j.close()

    # Insert into LDM
    pqstr = ("/home/ldm/bin/pqinsert -p 'plot c %s "
             "gis/images/4326/mrms/%s.json GIS/mrms/%s_%s.json json' %s.json"
             ) % (now.strftime("%Y%m%d%H%M"), prefix, prefix,
                  now.strftime("%Y%m%d%H%M"), tmpfn)
    subprocess.call(pqstr, shell=True)

    for suffix in ['tif', 'json', 'png', 'wld']:
        os.unlink('%s.%s' % (tmpfn, suffix))

    os.close(tmpfp)
    os.unlink(tmpfn)
Ejemplo n.º 34
0
def steric(Tb, Sb, dT, dS, deta, eta):
    ogrid = ModelGrid(fname='../wrkdir/grid_spec.nc')
    p = np.ones(np.shape(Sb))
    h = np.ones(np.shape(Sb))
    for index in range(0, np.shape(p)[0]):
        p[index, :, :] = ogrid.zt[index]
        if (index == 0):
            h[index, :, :] = ogrid.zb[index]  #-eta
        else:
            h[index, :, :] = abs(ogrid.zb[index] - ogrid.zb[index - 1])
    rho_b = eos.density(Sb, Tb, p)
    rho_a = eos.density(Sb + dS, Tb + dT, p)
    rho_a_halo = eos.density(Sb + dS, Tb, p)
    rho_a_thermo = eos.density(Sb, Tb + dT, p)

    dsteric = -np.nansum(h * (rho_a - rho_b) / rho_b, axis=0)
    dhalosteric = -np.nansum(h * (rho_a_halo - rho_b) / rho_b, axis=0)
    dthermosteric = -np.nansum(h * (rho_a_thermo - rho_b) / rho_b, axis=0)

    return dsteric, dhalosteric, dthermosteric
    '''
    print np.shape(dsteric), np.shape(Sb)

    dsteric[dsteric==0.0]=np.nan
    deta[deta==0.0]=np.nan

    print 'deta-dsteric=',np.nansum(np.abs(deta-dsteric))    

    fig = plt.figure(num=1, figsize=(19,12), facecolor='w')
    #grid = AxesGrid(fig, 111, nrows_ncols = (2, 3), axes_pad = 0.5,
    #                cbar_location="bottom",
    #                cbar_mode="each",
    #                cbar_size="7%",
    #                cbar_pad="2%")

    
    vmax=0.1
    vmin=-vmax

    #plt.sca(grid[0])

    plt.subplot(231)
    ch=plt2d(ogrid.x, ogrid.y, dsteric, minval=vmin, maxval=vmax, colmap=cm.jet)
    #grid.cbar_axes[0].colorbar(ch)
    plt.title('Steric height infered increment [m]')

    #plt.sca(grid[1])
    plt.subplot(232)
    incr_s=np.flipud(dhalosteric)
    incr_s[incr_s==0.0]=np.nan
    ch=plt2d(ogrid.x, ogrid.y, dhalosteric, minval=vmin, maxval=vmax, colmap=cm.jet)
    #grid.cbar_axes[1].colorbar(ch)
    plt.title('Halo Steric height infered increment [m]')

    #plt.sca(grid[2])
    plt.subplot(233)
    incr_t=np.flipud(dthermosteric)
    incr_t[incr_t==0.0]=np.nan
    ch=plt2d(ogrid.x, ogrid.y, dthermosteric, minval=vmin, maxval=vmax, colmap=cm.jet)
    #grid.cbar_axes[2].colorbar(ch)
    plt.title('Thermo Steric height infered increment [m]')

    #plt.sca(grid[3])
    plt.subplot(234)
    ch=plt2d(ogrid.x, ogrid.y, deta, minval=vmin, maxval=vmax, colmap=cm.jet)
    #grid.cbar_axes[3].colorbar(ch)
    plt.title('SSH increment [m]')

    vmin=-0.1
    vmax=0.1
    #plt.sca(grid[4])
    plt.subplot(235)
    err=deta-dsteric
    #err[np.abs(err)<0.05]=np.nan
    #err=np.abs(deta/dsteric)
    ch=plt2d(ogrid.x, ogrid.y, err, minval=vmin, maxval=vmax, colmap=cm.bwr)
    #grid.cbar_axes[4].colorbar(ch)
    plt.title('SSH - Steric [m]')

    #plt.sca(grid[5])
    plt.subplot(236)
    err=deta-dthermosteric
    #err=np.abs(deta/dthermosteric)
    #err[np.abs(err)<0.05]=np.nan
    ch=plt2d(ogrid.x, ogrid.y, err, minval=vmin, maxval=vmax, colmap=cm.bwr)
    #grid.cbar_axes[5].colorbar(ch)
    plt.title('SSH - Thermosteric [m]')


    '''
    plt.subplot(235)
    incr_t = np.flipud(dthermosteric)
    incr_t[incr_t == 0.0] = np.nan
    plt.imshow(incr_t, vmin=vmin, vmax=vmax)
    #plt.imshow(np.flipud(dthermosteric),vmin=vmin,vmax=vmax)

    plt.colorbar()

    plt.subplot(231)
    plt.imshow(np.flipud(deta), vmin=vmin, vmax=vmax)
    plt.title('SSH increment [m]')
    plt.colorbar()

    plt.subplot(233)
    err = np.flipud(deta - dsteric)
    err[np.abs(err) < 0.02] = np.nan
    plt.imshow(err, vmin=vmin, vmax=vmax)
    #plt.imshow(np.flipud(deta-dsteric),vmin=vmin,vmax=vmax)
    plt.title('SSH - Steric height [m]')
    plt.colorbar()
    '''
Ejemplo n.º 35
0
COLORS = ["#ffffff", "#000000"]
RASTERIZE_COLOR_FIELD = "__color__"


class fcycle(object):
    def __init__(self, iterable):
        """Call functions from the iterable each time it is called."""
        self.funcs = cycle(iterable)

    def __call__(self, *args, **kwargs):
        f = next(self.funcs)
        return f(*args, **kwargs)


# SI and IS operators for 2D and 3D.
_P2 = [np.eye(3), np.array([[0, 1, 0]] * 3), np.flipud(np.eye(3)), np.rot90([[0, 1, 0]] * 3)]
_P3 = [np.zeros((3, 3, 3)) for i in range(9)]

_P3[0][:, :, 1] = 1
_P3[1][:, 1, :] = 1
_P3[2][1, :, :] = 1
_P3[3][:, [0, 1, 2], [0, 1, 2]] = 1
_P3[4][:, [0, 1, 2], [2, 1, 0]] = 1
_P3[5][[0, 1, 2], :, [0, 1, 2]] = 1
_P3[6][[0, 1, 2], :, [2, 1, 0]] = 1
_P3[7][[0, 1, 2], [0, 1, 2], :] = 1
_P3[8][[0, 1, 2], [2, 1, 0], :] = 1

_aux = np.zeros(0)

Ejemplo n.º 36
0
def process(frame):
    # steps
    # 1. select region of intrest
    # 2. undistort the image
    # 3. compute the prepective transform and warp image
    # 4. find the thresholded image
    # 5. create histogram and then find line pixels by anaylsing peaks and sliding window technique


    roi = utils.region_of_intrest(frame)
    undistorted_img = cv2.undistort(roi,cam_mat, dist_coff, None, cam_mat)
    img_size = (undistorted_img.shape[1], undistorted_img.shape[0])
    trans_mat = utils.get_prespective(undistorted_img)
    inv_trans_mat = utils.get_prespective(undistorted_img, unwrapped = True)
    warped_img = cv2.warpPerspective(undistorted_img, trans_mat,img_size,cv2.INTER_LINEAR )
    thresh_img = utils.image_threshold(warped_img)



    # histogram to find peaks  for staring points of lane detection

    histogram = np.sum(thresh_img[int(thresh_img.shape[0] / 2):, :], axis=0)
    out_img = np.dstack((thresh_img, thresh_img, thresh_img)) * 255

    # Find the peak of the left and right halves of the histogram
    # These will be the starting point for the left and right lines
    midpoint = np.int(histogram.shape[0] / 2)
    leftx_base = np.argmax(histogram[:midpoint])
    rightx_base = np.argmax(histogram[midpoint:]) + midpoint

    # Choose the number of sliding windows
    nwindows = 9
    window_height = np.int(thresh_img.shape[0] / nwindows)
    nonzero = thresh_img.nonzero()
    nonzeroy = np.array(nonzero[0])
    nonzerox = np.array(nonzero[1])
    leftx_current = leftx_base
    rightx_current = rightx_base
    margin = 100
    minpix = 50
    left_lane_inds = []
    right_lane_inds = []

    for window in range(nwindows):
        # Identify window boundaries in x and y (and right and left)
        win_y_low = thresh_img.shape[0] - (window + 1) * window_height
        win_y_high = thresh_img.shape[0] - window * window_height
        win_xleft_low = leftx_current - margin
        win_xleft_high = leftx_current + margin
        win_xright_low = rightx_current - margin
        win_xright_high = rightx_current + margin
        # Draw the windows on the visualization image
        cv2.rectangle(out_img, (win_xleft_low, win_y_low), (win_xleft_high, win_y_high), (0, 255, 0), 2)
        cv2.rectangle(out_img, (win_xright_low, win_y_low), (win_xright_high, win_y_high), (0, 255, 0), 2)
        # Identify the nonzero pixels in x and y within the window
        good_left_inds = ((nonzeroy >= win_y_low) & (nonzeroy < win_y_high) & (nonzerox >= win_xleft_low) & (
        nonzerox < win_xleft_high)).nonzero()[0]
        good_right_inds = ((nonzeroy >= win_y_low) & (nonzeroy < win_y_high) & (nonzerox >= win_xright_low) & (
        nonzerox < win_xright_high)).nonzero()[0]
        # Append these indices to the lists
        left_lane_inds.append(good_left_inds)
        right_lane_inds.append(good_right_inds)
        # If you found > minpix pixels, recenter next window on their mean position
        if len(good_left_inds) > minpix:
            leftx_current = np.int(np.mean(nonzerox[good_left_inds]))
        if len(good_right_inds) > minpix:
            rightx_current = np.int(np.mean(nonzerox[good_right_inds]))

    # Concatenate the arrays of indices
    left_lane_inds = np.concatenate(left_lane_inds)
    right_lane_inds = np.concatenate(right_lane_inds)

    # Extract left and right line pixel positions
    leftx = nonzerox[left_lane_inds]
    lefty = nonzeroy[left_lane_inds]
    rightx = nonzerox[right_lane_inds]
    righty = nonzeroy[right_lane_inds]

    left_fit = np.polyfit(lefty, leftx, 2)
    right_fit = np.polyfit(righty, rightx, 2)

    ploty = np.linspace(0, thresh_img.shape[0] - 1, thresh_img.shape[0])
    left_fitx = left_fit[0] * ploty ** 2 + left_fit[1] * ploty + left_fit[2]
    right_fitx = right_fit[0] * ploty ** 2 + right_fit[1] * ploty + right_fit[2]

    # out_img[nonzeroy[left_lane_inds], nonzerox[left_lane_inds]] = [255, 0, 0]
    # out_img[nonzeroy[right_lane_inds], nonzerox[right_lane_inds]] = [0, 0, 255]
    # plt.imshow(out_img)
    # plt.plot(left_fitx, ploty, color='yellow')
    # plt.plot(right_fitx, ploty, color='yellow')
    # plt.xlim(0, 1280)
    # plt.ylim(720, 0)
    # plt.show()

    # calulation the lane curvature and distance from center

    radius_of_curvature = lane_curvature(left_fit, right_fit)
    center_calc = dist_from_center(frame, left_fitx, right_fitx)

    warp_zero = np.zeros_like(thresh_img).astype(np.uint8)
    color_warp = np.dstack((warp_zero, warp_zero, warp_zero))
    pts_left = np.array([np.transpose(np.vstack([left_fitx, ploty]))])
    pts_right = np.array([np.flipud(np.transpose(np.vstack([right_fitx, ploty])))])
    pts = np.hstack((pts_left, pts_right))
    cv2.fillPoly(color_warp, np.int_([pts]), (0, 255, 0))

    # this is a hamfisted solution to replace curves with extreme values with the previous frame's curve

    newwarp = cv2.warpPerspective(color_warp, inv_trans_mat, (frame.shape[1], frame.shape[0]))

    result = cv2.addWeighted(frame, 1, newwarp, 0.3, 0)
    cv2.putText(result, 'Radius of Curvature: %.2fm' % radius_of_curvature, (20, 40), cv2.FONT_HERSHEY_SIMPLEX, 1,
                (255, 255, 255), 2)
    if center_calc < 0:
        text = 'left'
    else:
        text = 'right'
    cv2.putText(result, 'Distance From Center: %.2fm %s' % (np.absolute(center_calc), text), (20, 80),
                cv2.FONT_HERSHEY_SIMPLEX, 1, (255, 255, 255), 2)

    return result
def create_output_images(Rover):

    # Create a scaled map for plotting and clean up obs/nav pixels a bit
    if np.max(Rover.worldmap[:, :, 2]) > 0:
        nav_pix = Rover.worldmap[:, :, 2] > 0
        navigable = Rover.worldmap[:, :, 2] * (
            255 / np.mean(Rover.worldmap[nav_pix, 2]))
    else:
        navigable = Rover.worldmap[:, :, 2]
    if np.max(Rover.worldmap[:, :, 0]) > 0:
        obs_pix = Rover.worldmap[:, :, 0] > 0
        obstacle = Rover.worldmap[:, :, 0] * (
            255 / np.mean(Rover.worldmap[obs_pix, 0]))
    else:
        obstacle = Rover.worldmap[:, :, 0]

    likely_nav = navigable >= obstacle
    obstacle[likely_nav] = 0
    plotmap = np.zeros_like(Rover.worldmap)
    plotmap[:, :, 0] = obstacle
    plotmap[:, :, 2] = navigable
    plotmap = plotmap.clip(0, 255)
    # Overlay obstacle and navigable terrain map with ground truth map
    map_add = cv2.addWeighted(plotmap, 1, Rover.ground_truth, 0.5, 0)

    # Check whether any rock detections are present in worldmap
    rock_world_pos = Rover.worldmap[:, :, 1].nonzero()
    # If there are, we'll step through the known sample positions
    # to confirm whether detections are real
    samples_located = 0
    if rock_world_pos[0].any():

        rock_size = 2
        for idx in range(len(Rover.samples_pos[0])):
            test_rock_x = Rover.samples_pos[0][idx]
            test_rock_y = Rover.samples_pos[1][idx]
            rock_sample_dists = np.sqrt((test_rock_x - rock_world_pos[1])**2 + \
                                  (test_rock_y - rock_world_pos[0])**2)
            # If rocks were detected within 3 meters of known sample positions
            # consider it a success and plot the location of the known
            # sample on the map
            if np.min(rock_sample_dists) < 3:
                samples_located += 1
                map_add[test_rock_y - rock_size:test_rock_y + rock_size,
                        test_rock_x - rock_size:test_rock_x +
                        rock_size, :] = 255

    # Calculate some statistics on the map results
    # First get the total number of pixels in the navigable terrain map
    tot_nav_pix = np.float(len((plotmap[:, :, 2].nonzero()[0])))
    # Next figure out how many of those correspond to ground truth pixels
    good_nav_pix = np.float(
        len(((plotmap[:, :, 2] > 0) &
             (Rover.ground_truth[:, :, 1] > 0)).nonzero()[0]))
    # Next find how many do not correspond to ground truth pixels
    bad_nav_pix = np.float(
        len(((plotmap[:, :, 2] > 0) &
             (Rover.ground_truth[:, :, 1] == 0)).nonzero()[0]))
    # Grab the total number of map pixels
    tot_map_pix = np.float(len((Rover.ground_truth[:, :, 1].nonzero()[0])))
    # Calculate the percentage of ground truth map that has been successfully found
    perc_mapped = round(100 * good_nav_pix / tot_map_pix, 1)
    # Calculate the number of good map pixel detections divided by total pixels
    # found to be navigable terrain
    if tot_nav_pix > 0:
        fidelity = round(100 * good_nav_pix / (tot_nav_pix), 1)
    else:
        fidelity = 0
    # Flip the map for plotting so that the y-axis points upward in the display
    map_add = np.flipud(map_add).astype(np.float32)
    # Add some text about map and rock sample detection results
    cv2.putText(map_add, "Time: " + str(np.round(Rover.total_time, 1)) + ' s',
                (0, 10), cv2.FONT_HERSHEY_COMPLEX, 0.4, (255, 255, 255), 1)
    cv2.putText(map_add, "Mapped: " + str(perc_mapped) + '%', (0, 25),
                cv2.FONT_HERSHEY_COMPLEX, 0.4, (255, 255, 255), 1)
    cv2.putText(map_add, "Fidelity: " + str(fidelity) + '%', (0, 40),
                cv2.FONT_HERSHEY_COMPLEX, 0.4, (255, 255, 255), 1)
    cv2.putText(map_add, "Rocks", (0, 55), cv2.FONT_HERSHEY_COMPLEX, 0.4,
                (255, 255, 255), 1)
    cv2.putText(map_add, "  Located: " + str(samples_located), (0, 70),
                cv2.FONT_HERSHEY_COMPLEX, 0.4, (255, 255, 255), 1)
    cv2.putText(map_add, "  Collected: " + str(Rover.samples_collected),
                (0, 85), cv2.FONT_HERSHEY_COMPLEX, 0.4, (255, 255, 255), 1)
    # Convert map and vision image to base64 strings for sending to server
    pil_img = Image.fromarray(map_add.astype(np.uint8))
    buff = BytesIO()
    pil_img.save(buff, format="JPEG")
    encoded_string1 = base64.b64encode(buff.getvalue()).decode("utf-8")

    pil_img = Image.fromarray(Rover.vision_image.astype(np.uint8))
    buff = BytesIO()
    pil_img.save(buff, format="JPEG")
    encoded_string2 = base64.b64encode(buff.getvalue()).decode("utf-8")

    return encoded_string1, encoded_string2
Ejemplo n.º 38
0
def spec(X, y=None, mode='rank', **kwargs):
    """
    This function implements the SPEC feature selection
    Input
    -----
    X: {numpy array}, shape (n_samples, n_features)
        input data
    kwargs: {dictionary}
        style: {int}
            style == -1, the first feature ranking function, use all eigenvalues
            style == 0, the second feature ranking function, use all except the 1st eigenvalue
            style >= 2, the third feature ranking function, use the first k except 1st eigenvalue
        W: {sparse matrix}, shape (n_samples, n_samples}
            input affinity matrix
    Output
    ------
    w_fea: {numpy array}, shape (n_features,)
        SPEC feature score for each feature
    Reference
    ---------
    Zhao, Zheng and Liu, Huan. "Spectral Feature Selection for Supervised and Unsupervised Learning." ICML 2007.
    """
    def feature_ranking(score, **kwargs):
        if 'style' not in kwargs:
            kwargs['style'] = 0
        style = kwargs['style']

        # if style = -1 or 0, ranking features in descending order, the higher the score, the more important the feature is
        if style == -1 or style == 0:
            idx = np.argsort(score, 0)
            return idx[::-1]
        # if style != -1 and 0, ranking features in ascending order, the lower the score, the more important the feature is
        elif style != -1 and style != 0:
            idx = np.argsort(score, 0)
            return idx
    
    if 'style' not in kwargs:
        kwargs['style'] = 0
    if 'is_classification' not in kwargs:
        # if y is available then we do supervised SPEC algo.
        kwargs['is_classification'] = True    
    if 'W' not in kwargs:
        print("W not in kwargs")
        print(kwargs)
        if y is None:
            kwargs['W'] = rbf_kernel(X, gamma=1)
        elif kwargs['is_classification']:
            kwargs['W'] = similiarity_classification(X, y)
        else:
            kwargs['W'] = similarity_regression(X, y, kwargs.get('n_neighbors', None))
    
    style = kwargs['style']
    W = kwargs['W']
    if type(W) is numpy.ndarray:
        W = csc_matrix(W)

    n_samples, n_features = X.shape

    # build the degree matrix
    X_sum = np.array(W.sum(axis=1))
    D = np.zeros((n_samples, n_samples))
    for i in range(n_samples):
        D[i, i] = X_sum[i]

    # build the laplacian matrix
    L = D - W
    d1 = np.power(np.array(W.sum(axis=1)), -0.5)
    d1[np.isinf(d1)] = 0
    d2 = np.power(np.array(W.sum(axis=1)), 0.5)
    v = np.dot(np.diag(d2[:, 0]), np.ones(n_samples))
    v = v/LA.norm(v)

    # build the normalized laplacian matrix
    L_hat = (np.matlib.repmat(d1, 1, n_samples)) * np.array(L) * np.matlib.repmat(np.transpose(d1), n_samples, 1)

    # calculate and construct spectral information
    s, U = np.linalg.eigh(L_hat)
    s = np.flipud(s)
    U = np.fliplr(U)

    # begin to select features
    w_fea = np.ones(n_features)*1000

    for i in range(n_features):
        f = X[:, i]
        F_hat = np.dot(np.diag(d2[:, 0]), f)
        l = LA.norm(F_hat)
        if l < 100*np.spacing(1):
            w_fea[i] = 1000
            continue
        else:
            F_hat = F_hat/l
        a = np.array(np.dot(np.transpose(F_hat), U))
        a = np.multiply(a, a)
        a = np.transpose(a)

        # use f'Lf formulation
        if style == -1:
            w_fea[i] = np.sum(a * s)
        # using all eigenvalues except the 1st
        elif style == 0:
            a1 = a[0:n_samples-1]
            w_fea[i] = np.sum(a1 * s[0:n_samples-1])/(1-np.power(np.dot(np.transpose(F_hat), v), 2))
        # use first k except the 1st
        else:
            a1 = a[n_samples-style:n_samples-1]
            w_fea[i] = np.sum(a1 * (2-s[n_samples-style: n_samples-1]))

    if style != -1 and style != 0:
        w_fea[w_fea == 1000] = -1000

    if mode == 'raw':
        return w_fea
    return feature_ranking(w_fea)
    def evaluate_data(self):

        self.depthcam_midpixel2 = [
            self.new_K_kin[1, 2] - HORIZ_CUT,
            (960 - self.new_K_kin[0, 2]) - pre_VERT_CUT
        ]

        #function_input = np.array(function_input)*np.array([10, 10, 10, 10, 10, 10, 0.1, 0.1, 0.1, 0.1, 1])
        #function_input += np.array([2.2, 32, -1, 1.2, 32, -5, 1.0, 1.0, 0.96, 0.95, 0.8])
        function_input = np.array(self.calibration_optim_values) * np.array(
            [10, 10, 10, 0.1, 0.1, 0.1, 0.1])
        function_input += np.array([1.2, 32, -5, 1.0, 1.0, 0.96, 0.95])

        kinect_rotate_angle = function_input[3 - 3]
        kinect_shift_up = int(function_input[4 - 3])
        kinect_shift_right = int(function_input[5 - 3])
        camera_alpha_vert = function_input[6 - 3]
        camera_alpha_horiz = function_input[7 - 3]

        blah = True

        file_dir = "/media/henry/multimodal_data_2/CVPR2020_study/" + PARTICIPANT + "/data_checked_" + PARTICIPANT + "-" + POSE_TYPE
        file_dirs = [file_dir]

        init_time = time.time()

        RESAVE_DICT = {}
        RESAVE_DICT['images'] = []
        RESAVE_DICT['RGB'] = []
        RESAVE_DICT['depth'] = []
        RESAVE_DICT['pc'] = []
        RESAVE_DICT['pmat_corners'] = []
        RESAVE_DICT['pose_type'] = []

        for file_dir in file_dirs:
            V3D.load_next_file(file_dir)

            start_num = 0
            #print self.color_all.shape

            #for im_num in range(29, 100):
            for im_num in range(start_num, self.color_all.shape[0]):

                #For P188: skip 5. 13 good cross legs

                if PARTICIPANT == "S114" and POSE_TYPE == "2" and im_num in [
                        26, 29
                ]:
                    continue  #these don't have point clouds
                if PARTICIPANT == "S165" and POSE_TYPE == "2" and im_num in [
                        1, 3, 15
                ]:
                    continue  #these don't have point clouds
                if PARTICIPANT == "S188" and POSE_TYPE == "2" and im_num in [
                        5, 17, 21
                ]:
                    continue  #these don't have point clouds

                print "NEXT IM!", im_num, " ", time.time(
                ) - init_time, self.pose_type_2_list[im_num]

                if POSE_TYPE == "2":
                    RESAVE_DICT['pose_type'].append(
                        self.pose_type_2_list[im_num])
                elif POSE_TYPE == "1":
                    if im_num == 0:
                        if PARTICIPANT == "S145":
                            RESAVE_DICT['pose_type'].append('p_sel_sup')
                        elif PARTICIPANT == "S188":
                            RESAVE_DICT['pose_type'].append('p_sel_ll')
                        else:
                            RESAVE_DICT['pose_type'].append('p_sel_any')
                    if im_num == 1:
                        if PARTICIPANT == "S140" or PARTICIPANT == "S145":
                            RESAVE_DICT['pose_type'].append('p_sel_ll')
                        elif PARTICIPANT == "S188":
                            RESAVE_DICT['pose_type'].append('p_sel_rl')
                        else:
                            RESAVE_DICT['pose_type'].append('p_sel_sup')
                    if im_num == 2:
                        if PARTICIPANT == "S140" or PARTICIPANT == "S145":
                            RESAVE_DICT['pose_type'].append('p_sel_rl')
                        elif PARTICIPANT == "S188":
                            RESAVE_DICT['pose_type'].append('p_sel_prn')
                        else:
                            RESAVE_DICT['pose_type'].append('p_sel_ll')
                    if im_num == 3:
                        if PARTICIPANT == "S140" or PARTICIPANT == "S145":
                            RESAVE_DICT['pose_type'].append('p_sel_prn')
                        elif PARTICIPANT == "S188":
                            RESAVE_DICT['pose_type'].append('p_sel_any')
                        else:
                            RESAVE_DICT['pose_type'].append('p_sel_rl')
                    if im_num == 4:
                        if PARTICIPANT == "S140" or PARTICIPANT == "S188":
                            RESAVE_DICT['pose_type'].append('p_sel_sup')
                        else:
                            RESAVE_DICT['pose_type'].append('p_sel_prn')

                print RESAVE_DICT['pose_type'][-1]

                self.overall_image_scale_amount = 0.85

                half_w_half_l = [0.4, 0.4, 1.1, 1.1]

                all_image_list = []
                self.label_single_image = []

                self.label_index = 0

                self.color = self.color_all[im_num]
                self.depth_r = self.depth_r_all[im_num]
                self.pressure = self.pressure_all[im_num]
                self.bed_state = self.bedstate_all[im_num]

                if self.point_cloud_autofil_all[im_num].shape[0] == 0:
                    self.point_cloud_autofil_all[im_num] = np.array(
                        [[0.0, 0.0, 0.0]])
                self.point_cloud_autofil = self.point_cloud_autofil_all[
                    im_num] + self.markers_all[im_num][
                        2]  #[0.0, 0.0, 0.0]#0.1]
                #print self.markers_all[im_num]
                #print self.point_cloud_autofil.shape, 'PC AUTOFIL ORIG'

                self.bed_state[
                    0] = self.bed_state[0] * 0.0  #*head_angle_multiplier
                self.bed_state *= 0
                #self.bed_state += 60.
                #print self.bed_state, np.shape(self.pressure)

                if im_num == start_num and blah == True:
                    markers_c = []
                    markers_c.append(self.markers_all[im_num][0])
                    markers_c.append(self.markers_all[im_num][1])
                    markers_c.append(self.markers_all[im_num][2])
                    markers_c.append(self.markers_all[im_num][3])
                    #for idx in range(4):
                    #if markers_c[idx] is not None:
                    #markers_c[idx] = np.array(markers_c[idx])*213./228.
                blah = False

                #print markers_c, 'Markers C'

                # Get the marker points in 2D on the color image
                u_c, v_c = ArTagLib().color_2D_markers(markers_c,
                                                       self.new_K_kin)

                # Get the marker points dropped to the height of the pressure mat
                u_c_drop, v_c_drop, markers_c_drop = ArTagLib(
                ).color_2D_markers_drop(markers_c, self.new_K_kin)

                #print markers_c_drop, self.new_K_kin, self.pressure_im_size_required, self.bed_state, half_w_half_l
                # Get the geometry for sizing the pressure mat
                pmat_ArTagLib = ArTagLib()
                self.pressure_im_size_required, u_c_pmat, v_c_pmat, u_p_bend, v_p_bend, half_w_half_l = \
                    pmat_ArTagLib.p_mat_geom(markers_c_drop, self.new_K_kin, self.pressure_im_size_required, self.bed_state, half_w_half_l)

                tf_corners = np.zeros((8, 2))
                tf_corners[0:8, :] = np.copy(self.tf_corners)

                #COLOR
                color_reshaped, color_size = VizLib().color_image(
                    self.color, self.kcam, self.new_K_kin, u_c, v_c, u_c_drop,
                    v_c_drop, u_c_pmat, v_c_pmat, camera_alpha_vert,
                    camera_alpha_horiz)
                color_reshaped = imutils.rotate(color_reshaped,
                                                kinect_rotate_angle)
                color_reshaped = color_reshaped[pre_VERT_CUT +
                                                kinect_shift_up:-pre_VERT_CUT +
                                                kinect_shift_up, HORIZ_CUT +
                                                kinect_shift_right:540 -
                                                HORIZ_CUT +
                                                kinect_shift_right, :]

                #all_image_list.append(color_reshaped)

                tf_corners[0:4, :], _ = self.transform_selected_points(
                    color_reshaped, camera_alpha_vert, camera_alpha_horiz,
                    kinect_rotate_angle, kinect_shift_right, kinect_shift_up,
                    [1.0, 0], [1.0, 0], np.copy(self.tf_corners[0:4][:]))

                #should blur face here
                #color_reshaped = self.blur_face(color_reshaped)
                RESAVE_DICT['RGB'].append(color_reshaped)
                RESAVE_DICT['pmat_corners'].append(tf_corners[0:4, :])

                #SAVE CALIBRATED COLOR HERE, color_reshaped
                #SAVE CALIBRATED TF CORNERS HERE, tf_corners[0:4, :]
                all_image_list.append(color_reshaped)

                #DEPTH
                h = VizLib().get_new_K_kin_homography(camera_alpha_vert,
                                                      camera_alpha_horiz,
                                                      self.new_K_kin)
                depth_r_orig = cv2.warpPerspective(
                    self.depth_r, h,
                    (self.depth_r.shape[1], self.depth_r.shape[0]))
                depth_r_orig = imutils.rotate(depth_r_orig,
                                              kinect_rotate_angle)
                depth_r_orig = depth_r_orig[HORIZ_CUT +
                                            kinect_shift_right:540 -
                                            HORIZ_CUT + kinect_shift_right,
                                            pre_VERT_CUT -
                                            kinect_shift_up:-pre_VERT_CUT -
                                            kinect_shift_up]
                #SAVE CALIBRATED DEPTH HERE, depth_r_orig
                RESAVE_DICT['depth'].append(depth_r_orig)
                depth_r_reshaped, depth_r_size, depth_r_orig = self.depth_image(
                    depth_r_orig)
                all_image_list.append(depth_r_reshaped)

                #PRESSURE
                self.pressure = np.clip(self.pressure * 1, 0, 100)
                pressure_reshaped, pressure_size = self.pressure_image(
                    self.pressure, color_size, tf_corners)

                pressure_reshaped = pressure_reshaped[
                    pre_VERT_CUT:-pre_VERT_CUT, HORIZ_CUT:540 - HORIZ_CUT, :]
                all_image_list.append(pressure_reshaped)

                self.all_images = np.zeros(
                    (960 - np.abs(pre_VERT_CUT) * 2, 1, 3)).astype(np.uint8)
                for image in all_image_list:
                    #print image.shape
                    self.all_images = np.concatenate((self.all_images, image),
                                                     axis=1)

                self.all_images = self.all_images[VERT_CUT:960 -
                                                  VERT_CUT, :, :]

                is_not_mult_4 = True
                while is_not_mult_4 == True:
                    is_not_mult_4 = cv2.resize(
                        self.all_images, (0, 0),
                        fx=self.overall_image_scale_amount,
                        fy=self.overall_image_scale_amount).shape[1] % 4
                    self.overall_image_scale_amount += 0.001

                self.all_images = cv2.resize(
                    self.all_images, (0, 0),
                    fx=self.overall_image_scale_amount,
                    fy=self.overall_image_scale_amount)
                self.cursor_shift = self.all_images.shape[1] / 4

                self.all_images_clone = self.all_images.copy()

                cv2.imshow('all_images', self.all_images)
                k = cv2.waitKey(1)
                #cv2.waitKey(0)

                #now do 3D rendering
                pmat = np.fliplr(
                    np.flipud(
                        np.clip(self.pressure.reshape(MAT_SIZE) * float(1),
                                a_min=0,
                                a_max=100)))

                #SAVE PRESSURE HERE, self.pressure
                RESAVE_DICT['images'].append(pmat)

                pc_autofil_red = self.trim_pc_sides(
                    camera_alpha_vert, camera_alpha_horiz, h,
                    kinect_rotate_angle)  #this is the point cloud

                #SAVE POINT CLOUD HERE, pc_autofil_red
                RESAVE_DICT['pc'].append(pc_autofil_red)

                camera_point = [
                    1.09898028, 0.46441343, -CAM_BED_DIST
                ]  #[dist from foot of bed, dist from left side of mat, dist normal]

                #
                self.pyRender.render_3D_data(camera_point,
                                             pmat=pmat,
                                             pc=pc_autofil_red)

                self.point_cloud_array = None

                if POSE_TYPE == "2":
                    save_name = '/prescribed'
                elif POSE_TYPE == "1":
                    save_name = '/p_select'

            pkl.dump(RESAVE_DICT,
                     open(participant_directory + save_name + '.p', 'wb'))
            print "SAVED."
def search_around_poly(binary_warped):
    # HYPERPARAMETER
    # Choose the width of the margin around the previous polynomial to search
    # The quiz grader expects 100 here, but feel free to tune on your own!
    margin = 100

    # Grab activated pixels
    nonzero = binary_warped.nonzero()
    nonzeroy = np.array(nonzero[0])
    nonzerox = np.array(nonzero[1])
    
    ### TO-DO: Set the area of search based on activated x-values ###
    ### within the +/- margin of our polynomial function ###
    ### Hint: consider the window areas for the similarly named variables ###
    ### in the previous quiz, but change the windows to our new search area ###
    left_lane_inds = ((nonzerox > (left_fit[0]*(nonzeroy**2) + left_fit[1]*nonzeroy + 
                    left_fit[2] - margin)) & (nonzerox < (left_fit[0]*(nonzeroy**2) + 
                    left_fit[1]*nonzeroy + left_fit[2] + margin)))
    right_lane_inds = ((nonzerox > (right_fit[0]*(nonzeroy**2) + right_fit[1]*nonzeroy + 
                    right_fit[2] - margin)) & (nonzerox < (right_fit[0]*(nonzeroy**2) + 
                    right_fit[1]*nonzeroy + right_fit[2] + margin)))
    
    # Again, extract left and right line pixel positions
    leftx = nonzerox[left_lane_inds]
    lefty = nonzeroy[left_lane_inds] 
    rightx = nonzerox[right_lane_inds]
    righty = nonzeroy[right_lane_inds]

    # Fit new polynomials
    left_fitx, right_fitx, ploty = fit_poly(binary_warped.shape, leftx, lefty, rightx, righty)
    
    ## Visualization ##
    # Create an image to draw on and an image to show the selection window
    out_img = np.dstack((binary_warped, binary_warped, binary_warped))*255
    window_img = np.zeros_like(out_img)
    # Color in left and right line pixels
    out_img[nonzeroy[left_lane_inds], nonzerox[left_lane_inds]] = [255, 0, 0]
    out_img[nonzeroy[right_lane_inds], nonzerox[right_lane_inds]] = [0, 0, 255]

    # Generate a polygon to illustrate the search window area
    # And recast the x and y points into usable format for cv2.fillPoly()
    left_line_window1 = np.array([np.transpose(np.vstack([left_fitx-margin, ploty]))])
    left_line_window2 = np.array([np.flipud(np.transpose(np.vstack([left_fitx+margin, 
                              ploty])))])
    left_line_pts = np.hstack((left_line_window1, left_line_window2))
    right_line_window1 = np.array([np.transpose(np.vstack([right_fitx-margin, ploty]))])
    right_line_window2 = np.array([np.flipud(np.transpose(np.vstack([right_fitx+margin, 
                              ploty])))])
    right_line_pts = np.hstack((right_line_window1, right_line_window2))

    # Draw the lane onto the warped blank image
    cv2.fillPoly(window_img, np.int_([left_line_pts]), (0,255, 0))
    cv2.fillPoly(window_img, np.int_([right_line_pts]), (0,255, 0))
    result = cv2.addWeighted(out_img, 1, window_img, 0.3, 0)
    
    # Plot the polynomial lines onto the image
    plt.plot(left_fitx, ploty, color='yellow')
    plt.plot(right_fitx, ploty, color='yellow')
    ## End visualization steps ##
    
    return result
def detect_similar_lines(img, line_fits=None, return_img=False):
    if line_fits is None:
        return detect_lines(img, return_img)

    left_fit = line_fits[0]
    right_fit = line_fits[1]

    nonzero = img.nonzero()
    nonzeroy = np.array(nonzero[0])
    nonzerox = np.array(nonzero[1])
    margin = 100
    left_lane_inds = (
        (nonzerox >
         (left_fit[0] *
          (nonzeroy**2) + left_fit[1] * nonzeroy + left_fit[2] - margin)) &
        (nonzerox <
         (left_fit[0] *
          (nonzeroy**2) + left_fit[1] * nonzeroy + left_fit[2] + margin)))

    right_lane_inds = (
        (nonzerox >
         (right_fit[0] *
          (nonzeroy**2) + right_fit[1] * nonzeroy + right_fit[2] - margin)) &
        (nonzerox <
         (right_fit[0] *
          (nonzeroy**2) + right_fit[1] * nonzeroy + right_fit[2] + margin)))

    # Again, extract left and right line pixel positions
    leftx = nonzerox[left_lane_inds]
    lefty = nonzeroy[left_lane_inds]
    rightx = nonzerox[right_lane_inds]
    righty = nonzeroy[right_lane_inds]

    # If any of the lines could not be found,
    # perform a more exhaustive search
    if (leftx.size == 0 or rightx.size == 0):
        return detect_lines(img, return_img)

    # Fit a second order polynomial to each
    left_fit = np.polyfit(lefty, leftx, 2)
    right_fit = np.polyfit(righty, rightx, 2)

    # Generate x and y values for plotting
    ploty = np.linspace(0, img.shape[0] - 1, img.shape[0])
    left_fitx = left_fit[0] * ploty**2 + left_fit[1] * ploty + left_fit[2]
    right_fitx = right_fit[0] * ploty**2 + right_fit[1] * ploty + right_fit[2]

    if return_img:
        # Create an image to draw on and an image to show the selection window
        out_img = np.dstack((img, img, img)) * 255
        window_img = np.zeros_like(out_img)

        # Generate a polygon to illustrate the search window area
        # And recast the x and y points into usable format for cv2.fillPoly()
        left_line_window1 = np.array(
            [np.transpose(np.vstack([left_fitx - margin, ploty]))])
        left_line_window2 = np.array(
            [np.flipud(np.transpose(np.vstack([left_fitx + margin, ploty])))])
        left_line_pts = np.hstack((left_line_window1, left_line_window2))
        right_line_window1 = np.array(
            [np.transpose(np.vstack([right_fitx - margin, ploty]))])
        right_line_window2 = np.array(
            [np.flipud(np.transpose(np.vstack([right_fitx + margin, ploty])))])
        right_line_pts = np.hstack((right_line_window1, right_line_window2))

        # Draw the lane onto the warped blank image
        cv2.fillPoly(window_img, np.int_([left_line_pts]), (0, 255, 0))
        cv2.fillPoly(window_img, np.int_([right_line_pts]), (0, 255, 0))
        out_img = cv2.addWeighted(out_img, 1, window_img, 0.3, 0)

        # Color in left and right line pixels
        out_img[nonzeroy[left_lane_inds],
                nonzerox[left_lane_inds]] = [255, 0, 0]
        out_img[nonzeroy[right_lane_inds],
                nonzerox[right_lane_inds]] = [0, 0, 255]

        for index in range(img.shape[0]):
            cv2.circle(out_img, (int(left_fitx[index]), int(ploty[index])), 3,
                       (255, 255, 0))
            cv2.circle(out_img, (int(right_fitx[index]), int(ploty[index])), 3,
                       (255, 255, 0))

        return (left_fit, right_fit), (left_fitx,
                                       ploty), (right_fitx,
                                                ploty), out_img.astype(int)

    return (left_fit, right_fit), (left_fitx, ploty), (right_fitx, ploty)
Ejemplo n.º 42
0
        def get(self):
            self.application.close_figures()
            try:
                filename1 = self.get_argument("fitsfile1")
                filename2 = self.get_argument("fitsfile2")
                log.info(f"Analyzing {filename1} and {filename2}...")
            except Exception as e:
                log.warning(
                    f"No or not enough CWFS files specified. ({e.__class__})")

            connect = self.get_argument("connect", default=True)
            spher = self.get_argument("spher", default=False)
            thresh = self.get_argument("thresh", default=150.0)
            thresh = float(thresh) * u.nm
            focoff = self.get_argument("focoff", default=1000.0)
            focoff = float(focoff)

            tel = self.application.wfs.telescope

            if spher == "true":
                spher_mask = ['Z11', 'Z22']
                log.info(f"Ignoring the spherical terms {spher_mask}...")
            else:
                spher_mask = []

            if os.path.isfile(filename1) and os.path.isfile(
                    filename2) and not self.application.busy:
                self.application.busy = True
                if connect == "true":
                    self.application.wfs.connect()
                else:
                    self.application.wfs.disconnect()

                # get rotator and focus values from the headers, if available
                rots = []
                focusvals = []
                images = [filename1, filename2]
                arrays = []
                for image in images:
                    h = fits.open(image)
                    hdr = h[-1].header
                    if 'ROT' in hdr:
                        rots.append(hdr['ROT'])
                        rot = hdr['ROT']
                    else:
                        rot = 0.0
                    if 'FOCUS' in hdr:
                        focusvals.append(hdr['FOCUS'])
                    data = h[-1].data
                    orig_shape = data.shape
                    pup_mask = tel.pupil_mask(rotation=rot, size=120)
                    x, y, fig = center_pupil(data,
                                             pup_mask,
                                             threshold=0.8,
                                             plot=False)
                    cenx = int(np.round(x))
                    ceny = int(np.round(y))
                    data = data[ceny - 96:ceny + 96, cenx - 96:cenx + 96]
                    if len(h) > 1 or orig_shape == (
                            516,
                            532):  # check if we're raw binospec SOG images
                        log.info(
                            f"Found raw Binospec SOG image. Flipping {image} up/down."
                        )
                        data = np.flipud(data)
                    arrays.append(data)

                if len(rots) > 0:
                    rot = np.array(rots).mean() * u.deg
                    log.info(f"Using rotator angle of {rot.round(2)}.")
                else:
                    log.warning(
                        "WARNING: No rotator information in headers. Assuming rotator angle of 0.0 degrees."
                    )
                    rot = 0.0 * u.deg

                if len(focusvals) == 2:
                    focusvals = np.array(focusvals)
                    I1 = Image(arrays[np.argmin(focusvals)], [0, 0],
                               Image.INTRA)
                    I2 = Image(arrays[np.argmax(focusvals)], [0, 0],
                               Image.EXTRA)
                    intra_file = pathlib.Path(
                        images[np.argmin(focusvals)]).name
                    extra_file = pathlib.Path(
                        images[np.argmax(focusvals)]).name
                    focoff = focusvals.max() - focusvals.mean()
                    log.info(f"Using an M2 focus offset of +/- {focoff} um.")
                    log.info(
                        f"Intra-focal image: {images[np.argmin(focusvals)]}")
                    log.info(
                        f"Extra-focal image: {images[np.argmax(focusvals)]}")
                else:
                    I1 = Image(readFile(images[0]), [0, 0], Image.INTRA)
                    I2 = Image(readFile(images[1]), [0, 0], Image.EXTRA)
                    intra_file = pathlib.Path(images[0]).name
                    extra_file = pathlib.Path(images[1]).name
                    log.warning(
                        f"WARNING: No focus information in image headers. Assuming M2 focus offset of +/- {focoff} um."
                    )
                    log.warning(
                        f"WARNING: Assuming intra-focal image is {image[0]}")
                    log.warning(
                        f"WARNING: Assuming extra-focal image is {image[1]}")

                # use second image filename as the root for output files
                output = filename2

                # The pupil rotation in the single-object guider on binospec was determined to be 0 deg.
                rotation = 0 * u.deg - rot
                log.info(f"Total pupil rotation: {rotation.round(2)}")

                # load instrument and algorithm parameters
                inst = Instrument('mmto', I1.sizeinPix)

                # this is a MMTO hack. 0.0 doesn't work, but this will yield an annular zernike solution that is very close
                # to circular. the MMTO wfs code currently doesn't support annular zernikes for calculating corrections.
                inst.obscuration = 0.01

                # convert M2 focus offset in microns to meters of focus shift at the instrument focal plane
                inst.offset = focoff * 1.0e-6 * 18.8

                # set up fitting algorithm
                algo = Algorithm("exp", inst, 0)

                log.info(
                    "Fitting wavefront using 'exp' algorithm and the 'onAxis' model."
                )

                # run it
                algo.runIt(inst, I1, I2, 'onAxis')

                log.info("Wavefront fit complete.")

                # output parameters
                outParam(output + ".param", algo, inst, I1, I2, 'onAxis')

                # set up ZernikeVector, denormalize it, and then derotate it
                zvec = ZernikeVector()
                zvec.from_array(algo.zer4UpNm, modestart=4, normalized=True)
                zvec.denormalize()
                zvec.rotate(angle=-rotation)

                log.debug("\n" + repr(zvec))

                # output Zernikes 4 and up
                outZer4Up(algo.zer4UpNm, 'nm', output + ".raw.lsst.zernikes")
                zvec.save(filename=output + ".rot.zernikes")

                m1gain = self.application.wfs.m1_gain

                # this is the total if we try to correct everything as fit
                totforces, totm1focus, zv_totmasked = tel.calculate_primary_corrections(
                    zvec, gain=m1gain)
                figures = {}
                ax = {}

                # show intra-focal image
                figures['intra'], ax['intra'] = plt.subplots()
                figures['intra'].set_label("Intra-focal Image")
                im1 = ax['intra'].imshow(I1.image,
                                         cmap='Greys',
                                         origin='lower',
                                         interpolation='None')
                ax['intra'].set_title(intra_file)
                figures['intra'].colorbar(im1)

                # show extra-focal image
                figures['extra'], ax['extra'] = plt.subplots()
                figures['extra'].set_label("Extra-focal Image")
                im2 = ax['extra'].imshow(I2.image,
                                         cmap='Greys',
                                         origin='lower',
                                         interpolation='None')
                ax['extra'].set_title(extra_file)
                figures['extra'].colorbar(im2)

                # show wavefront map
                figures['wavefront'], ax['wavefront'] = plt.subplots()
                wfim = ax['wavefront'].imshow(algo.Wconverge * 1.0e9,
                                              origin="lower",
                                              cmap='RdBu')
                cbar_wf = figures['wavefront'].colorbar(wfim)
                cbar_wf.set_label(zvec.units.name, rotation=0)

                # show RMS bar chart
                zernike_rms = zvec.rms
                rms_asec = zernike_rms.value / self.application.wfs.tiltfactor * u.arcsec
                log.info(f"Total wavefront RMS: {zernike_rms.round(2)}")
                figures['barchart'] = zvec.bar_chart(
                    title=
                    f"Total Wavefront RMS: {zernike_rms.round(1)} ({rms_asec.round(2)})"
                )

                # show total forces and PSF
                figures['totalforces'] = tel.plot_forces(totforces, totm1focus)
                figures['totalforces'].set_label("Total M1 Actuator Forces")
                psf, figures['psf'] = tel.psf(zv=zvec.copy())

                self.application.wavefront_fit = zvec
                self.application.pending_focus = self.application.wfs.calculate_focus(
                    zvec)
                self.application.pending_cc_x, self.application.pending_cc_y = self.application.wfs.calculate_cc(
                    zvec)

                self.application.pending_forces, self.application.pending_m1focus, zv_masked = \
                    self.application.wfs.calculate_primary(zvec, threshold=thresh, mask=spher_mask)

                self.application.pending_forcefile = output + ".forces"
                zvec_masked_file = output + ".masked.zernike"
                zv_masked.save(filename=zvec_masked_file)

                limit = np.round(
                    np.abs(self.application.pending_forces['force']).max())
                figures['forces'] = tel.plot_forces(
                    self.application.pending_forces,
                    self.application.pending_m1focus,
                    limit=limit)
                figures['forces'].set_label("Requested M1 Actuator Forces")

                figures['fringebarchart'] = zvec.fringe_bar_chart(
                    title="Focus: {0:0.1f}  CC_X: {1:0.1f}  CC_Y: {2:0.1f}".
                    format(
                        self.application.pending_focus,
                        self.application.pending_cc_x,
                        self.application.pending_cc_y,
                    ))
                self.application.has_pending_m1 = True
                self.application.has_pending_focus = True
                self.application.has_pending_coma = True

                self.application.refresh_figures(figures=figures)
            else:
                log.error(f"No such files: {filename1} {filename2}")

            self.application.wavefront_fit.denormalize()
            self.write(json.dumps(repr(self.application.wavefront_fit)))
            self.application.busy = False
            self.finish()
def figure_4_2_val_iter():
    # value iteration for final policy and values in one sweep
    # determinstic policies
    value = np.zeros((L1_CAR_MAX + 1, L2_CAR_MAX + 1))
    policy = np.zeros(value.shape, dtype=np.int)

    iterations = 0
    _, axes = plt.subplots(
        2, 1, figsize=(40, 20))  # a fig of policy and a fig of optimal values
    plt.subplots_adjust(wspace=0.1, hspace=0.2)
    axes = axes.flatten()

    while True:
        # value iteration, similar to policy improvement
        max_diff = 0
        for L1_cars in range(L1_CAR_MAX + 1):
            for L2_cars in range(L2_CAR_MAX + 1):
                old_value = value[L1_cars, L2_cars]
                action_values = []
                for action in ACTION_LIST:
                    if 0 <= action <= L1_cars or (-1 * L2_cars) <= action <= 0:
                        # action_values.append(Expected_Return([L1_cars, L2_cars], action, value))
                        action_values.append(
                            Expected_Return_with_Mem([L1_cars, L2_cars],
                                                     action, value))
                    else:
                        # not available actions
                        action_values.append(-np.inf)
                new_value = np.max(action_values)
                diff = abs(old_value - new_value)
                value[L1_cars, L2_cars] = new_value
                if max_diff < diff:
                    max_diff = diff
        iterations += 1
        print('Iter {}: max diff: {:.6f}'.format(iterations, max_diff))
        if max_diff < 1e-4:
            break

    # draw optimal values
    fig = sns.heatmap(np.flipud(value), cmap="YlGnBu", ax=axes[-1])
    fig.set_ylabel('# cars at first location', fontsize=30)
    fig.set_yticks(list(reversed(range(MAX_CARS + 1))))
    fig.set_xlabel('# cars at second location', fontsize=30)
    fig.set_title('optimal value', fontsize=30)

    # final optimal policy
    for L1_cars in range(L1_CAR_MAX + 1):
        for L2_cars in range(L2_CAR_MAX + 1):
            action_values = []
            for action in ACTION_LIST:
                if 0 <= action <= L1_cars or (-1 * L2_cars) <= action <= 0:
                    # action_values.append(Expected_Return([L1_cars, L2_cars], action, value))
                    action_values.append(
                        Expected_Return_with_Mem([L1_cars, L2_cars], action,
                                                 value))
                else:
                    # not available actions
                    action_values.append(-np.inf)
            new_action = ACTION_LIST[np.argmax(action_values)]
            policy[L1_cars, L2_cars] = new_action

    # draw optimal policy
    fig = sns.heatmap(np.flipud(policy), cmap="YlGnBu", ax=axes[0])
    fig.set_ylabel('# cars at first location', fontsize=30)
    fig.set_yticks(list(reversed(range(MAX_CARS + 1))))
    fig.set_xlabel('# cars at second location', fontsize=30)
    fig.set_title('optimal policy'.format(0), fontsize=30)

    plt.savefig('./images/mine/exe_4_7_val_iter.png')
    plt.close()
Ejemplo n.º 44
0
def main():

	parser = argparse.ArgumentParser(description='This script post-process the F_out file that SSAGES print out, and create a FES from the estimation of the thermodynamic force')
	parser.add_argument('-i','--input',default='F_out',help='name of the file containing the force')
	parser.add_argument('-o','--output',default='G',help='file containing the free energy surface')
	parser.add_argument('-p','--periodicity',nargs='*',default=[False,False,False],help='periodicity of the CVs (True is periodic, False is not)')
	parser.add_argument('-n','--npoints',nargs='*',type=int,default=[200,200,200],help='number of points for the interpolation')
	parser.add_argument('-s','--scale',type=float,default=1,help='scale for the interpolation')
	parser.add_argument('-c','--contours',type=int,default=10,help='number of contours on final plot')

	args=parser.parse_args()

	inputfile=args.input
	outputname=args.output
	periodic=args.periodicity
	interpolate=args.npoints
	scale=args.scale
	ncontours=args.contours

	if interpolate == [0]:
		interpolate = 0

	periodic = [False if x == 'False' else x for x in periodic]	

	if not (os.path.isfile(inputfile)):
		print('Input file is missing. Aborting! Please use -h to ask for help!')
		exit()

	print('Input file is :\n', inputfile, '\n and output file is : \n',outputname)
	print('CV1 periodicity is set to \n',periodic[0],' \n')
	if(len(periodic)>1):
		print('CV2 periodicity is set to (if it exists) \n',periodic[1],' \n')
	if(len(periodic)>2):
		print('CV3 periodicity is set to (if it exists) \n',periodic[2],' \n')
	print('Free energy will be interpolated with \n',interpolate,'\n points \n')
	print('Free energy will be plotted with \n',ncontours,'\n contours')
	f = open(outputname+'_integrated.txt','w')	

	vfield = []

	with open(inputfile,"r") as infile:
		for line in reversed(infile.readlines()):
			if "The columns" in line:
				headerinfo=re.findall(r'\d+',line)
				break
			elif not (line.strip()==""):
				vfield.append(list(map(float,line.split())))
	

	vfield = np.flipud(np.asarray(vfield))
	shapeinfo = vfield.shape
	headerfound = 0
	try:
		headerinfo = [int(i) for i in headerinfo]
		headerfound = 1
	except NameError:
		print("Header not found. Proceeding with analysis, but the file read in is likely not created by ABF directly. Proceed with caution.")
	
	if shapeinfo[1] == 2:
		print("Two columns detected in input, which translates to a 1-dimensional grid of "+str(shapeinfo[0])+ " points.")
		
		if interpolate == 0:
			A=np.zeros((vfield.shape[0],vfield.shape[0]))
			dx = vfield[2,0]-vfield[1,0]
			X = vfield[:,0]
			b = vfield[:,1]
			
		else:
			A=np.zeros((interpolate[0],interpolate[0]))
			dx = (vfield[-1,0]-vfield[0,0])/interpolate[0]
			X = np.linspace(vfield[0,0],vfield[-1,0],interpolate[0])
			b = np.interp(X,vfield[:,0],vfield[:,1])
			
		for i in range (1,A.shape[0]-1):
			A[i][i-1]=-1
			A[i][i]=1
			
			if(periodic[0]):
				A[0][-1] = -2
				A[0][0] = 2
				A[-1][-1] = 1
				A[-1][-2] = -1
			else:
				A[0][0] = -1
				A[0][1] = 1
				A[-1][-1] = 1
				A[-1][-2] = -1
				
				A[1][1] = 1
				A[1][0] = 0
				b[1] = 0
		A=A/dx			
		
		Asurf,c,d,e = npl.lstsq(A,b)
		Asurf = -Asurf*scale
		
		dx = vfield[2,0]-vfield[1,0]
		vfieldy = vfield[:,1]
		
		for k in range(0,A.shape[0]):
			f.write("{0:4f} {1:4f} \n".format(X[k],Asurf[k]))
		minimum = min(Asurf[:])
		plt.plot(X,Asurf-minimum)

#		plt.show()
		plt.savefig(outputname+'_integrated.png')

	elif shapeinfo[1] == 4:	 
		if(len(periodic)<2):
			print('Please enter periodicity for each dimension.')
			exit()
		
		unique,counts=np.unique(vfield[:,0],return_counts=True)
		nx = len(unique)
		unique,counts=np.unique(vfield[:,1],return_counts=True)
		ny = len(unique)
	
		boundx = [vfield[0,0],vfield[-1,0]]
		boundy = [vfield[0,1],vfield[-1,1]]

		plot1 = plt.figure()
		plt.quiver(vfield[:,0],vfield[:,1],vfield[:,2],vfield[:,3],width = (0.0002*(vfield[nx*ny-1,0]-vfield[0,0])), headwidth=3)
		plt.axis([boundx[0],boundx[1],boundy[0],boundy[1]])
		plt.title('Gradient Field of the Free Energy')
		plot1.savefig(outputname+'_GradientField.png')

		if interpolate != 0:

			grid_x, grid_y = np.mgrid[boundx[0]:boundx[1]:interpolate[0]*1j,boundy[0]:boundy[1]:interpolate[1]*1j]
		
			dx = (boundx[1]-boundx[0])/interpolate[0]
			dy = (boundy[1]-boundy[0])/interpolate[1]
	
			nx = interpolate[0]
			ny = interpolate[1]

			fx=interp.griddata(vfield[:,0:2], vfield[:,2],(grid_x,grid_y), method='cubic')
			fy=interp.griddata(vfield[:,0:2], vfield[:,3],(grid_x,grid_y), method='cubic')

			fx=fx.T
			fy=fy.T
	
		else:			

			fy = vfield[:,3]
			fx = vfield[:,2]		

			dx = vfield[1,0] - vfield[0,0]
			dy = vfield[nx,1] - vfield[0,1]

			grid_x = vfield[:,0].reshape((nx,ny),order='F')
			grid_y = vfield[:,1].reshape((nx,ny),order='F')		
			

		fhat = intgrad2(fx,fy,nx,ny,dx,dy,1,periodic[0],periodic[1])
		fhat = fhat*scale

		zmin = min(fhat)
		zmax = max(fhat)
		fhat = fhat.reshape((nx,ny),order='F')
		
		X = grid_x
		Y = grid_y

		for k in range(0,ny):
			for n in range(0,nx): 
				f.write("{0:4f} {1:4f} {2:4f} \n".format(X[n,k],Y[n,k],-fhat[n,k]))


		plot3 = plt.figure()	
		plt.pcolormesh(X,Y,-fhat)
		plt.colorbar()
		plt.axis([min(vfield[:,0]),max(vfield[:,0]),min(vfield[:,1]),max(vfield[:,1])])
		plot3.savefig(outputname+'_EnergySurface.png')
	
		plot4=plt.figure()
		CS=plt.contour(X,Y,-fhat,antialiased=True,levels=np.linspace(np.amin(-fhat),np.amax(-fhat),30))

		plot4.savefig(outputname+'_contourmap.png')
		
		plot5 = plt.figure()
		black_contours=np.linspace(0,np.amax(-fhat-np.amin(-fhat)),ncontours+1)
		gray_contours=black_contours+np.amax(-fhat-np.amin(-fhat))/(2*(ncontours+1))
		plt.pcolormesh(X,Y,-fhat-np.amin(-fhat))
		plt.colorbar(label='kJ/mol')
		plt.axis([min(vfield[:,0]),max(vfield[:,0]),min(vfield[:,1]),max(vfield[:,1])])
		plt.contour(X,Y,(-fhat-np.amin(-fhat)),black_contours,colors='black',linewidths=0.75)
		plt.contour(X,Y,(-fhat-np.amin(-fhat)),gray_contours,colors='grey',linewidths=0.75)
		plt.tight_layout()
		plot5.savefig(outputname+'_merged.png')

	elif shapeinfo[1] == 6:
		if(len(periodic)<3):
			print('Please enter periodicity for each dimension.')
			exit()

		unique,counts=np.unique(vfield[:,0],return_counts=True)
		nx = len(unique)
		unique,counts=np.unique(vfield[:,1],return_counts=True)
		ny = len(unique)
		unique,counts=np.unique(vfield[:,2],return_counts=True)
		nz = len(unique)

		boundx = [vfield[0,0],vfield[-1,0]]
		boundy = [vfield[0,1],vfield[-1,1]]
		boundz = [vfield[0,2],vfield[-1,2]]
		
		

		if interpolate != 0:

			grid_x, grid_y, grid_z = np.mgrid[0:nx:interpolate[0]*1j,0:ny:interpolate[1]*1j,0:nz:interpolate[2]*1j]

			X, Y, Z = np.mgrid[boundx[0]:boundx[1]:interpolate[0]*1j,boundy[0]:boundy[1]:interpolate[1]*1j,boundz[0]:boundz[1]:interpolate[2]*1j]

			dx = X[1,0,0]-X[0,0,0]
			dy = Y[0,1,0]-Y[0,0,0]
			dz = Z[0,0,1]-Z[0,0,0]

			x = np.linspace(boundx[0],boundx[1],nx)
			y = np.linspace(boundy[0],boundy[1],ny)
			z = np.linspace(boundz[0],boundz[1],nz)

			datax = vfield[:,3].reshape((nx,ny,nz))
			datay = vfield[:,4].reshape((nx,ny,nz))
			dataz = vfield[:,5].reshape((nx,ny,nz))
	
			nx = interpolate[0]
			ny = interpolate[1]
			nz = interpolate[2]
		
			fx= interp2.map_coordinates(datax,np.vstack((np.ravel(grid_x),np.ravel(grid_y),np.ravel(grid_z))),order=3,mode='nearest')
			fy= interp2.map_coordinates(datay,np.vstack((np.ravel(grid_x),np.ravel(grid_y),np.ravel(grid_z))),order=3,mode='nearest')
			fz= interp2.map_coordinates(dataz,np.vstack((np.ravel(grid_x),np.ravel(grid_y),np.ravel(grid_z))),order=3,mode='nearest')

		else:			
		

			fx = vfield[:,3]
			fy = vfield[:,4]
			fz = vfield[:,5]		

			dx = vfield[1,0] - vfield[0,0]
			dy = vfield[nx,1] - vfield[0,1]
			dz = vfield[ny*nx,2] - vfield[0,2]

			grid_x = vfield[:,0].reshape((nx,ny,nz),order='F')
			grid_y = vfield[:,1].reshape((nx,ny,nz),order='F')
			grid_z = vfield[:,2].reshape((nx,ny,nz),order='F')
		
		fhat = intgrad3double(fx,fy,fz,nx,ny,nz,dx,dy,dz,1,periodic[0],periodic[1],periodic[2])
		fhat = fhat*scale

		zmin = min(fhat)
		zmax = max(fhat)
		fhat = fhat.reshape((nx,ny,nz),order='F')
		
		X = grid_x
		Y = grid_y
		Z = grid_z


		for l in range(0,nz):
			for k in range(0,ny):
				for j in range(0,nx): 
					f.write("{0:4f} {1:4f} {2:4f} {3:4f} \n".format(X[j,k,l],Y[j,k,l],Z[j,k,l],-fhat[j,k,l]))
	
	else:
		print("Input file does not contain the corrent number of dimensions. This code only supports 1D, 2D and 3D currently.")
		f.close()
		exit()
			
	f.close()
Ejemplo n.º 45
0
def plot_total_importance(axs, mfd, weights):
    axs = np.flipud(axs)
    c = plt.cm.tab20(range(weights.shape[1]))
    for ax, m, w in zip(axs.flatten(), mfd, weights):
        ax.scatter(m, w, c=c)
Ejemplo n.º 46
0
    def read(self, dfs3file, item_numbers=None, layers=None, coordinates=None):
        """ Function: Read data from a dfs3 file

        Usage:
            [data,time,name] = read( filename, item_numbers, layers=None, coordinates=None)

        item_numbers
            list of indices (base 0) to read from. If None then all the items.
        layers
            list of layer indices (base 0) to read
        coordinates
            list of list (x,y,layer) integers ( 0,0 at Bottom Left of Grid !! )
            example coordinates = [[2,5,1], [11,41,2]]

        Returns
            1) the data contained in a dfs3 file in a list of numpy matrices
            2) time index
            3) name of the items

        NOTE
            Returns Dataset with data (t, z, y, x)

            1) If coordinates is selected, then only return data at those coordinates
            2) coordinates specified overules layers.
            3) layer counts from the bottom
        """

        # Open the dfs file for reading
        dfs = DfsFileFactory.DfsGenericOpen(dfs3file)

        # Determine the size of the grid
        axis = dfs.ItemInfo[0].SpatialAxis
        zNum = axis.ZCount
        yNum = axis.YCount
        xNum = axis.XCount
        nt = dfs.FileInfo.TimeAxis.NumberOfTimeSteps

        deleteValue = dfs.FileInfo.DeleteValueFloat

        if item_numbers is None:
            n_items = safe_length(dfs.ItemInfo)
            item_numbers = list(range(n_items))

        n_items = len(item_numbers)
        data_list = []

        if coordinates is None:
            # if nt is 0, then the dfs is 'static' and must be handled differently
            if nt != 0:
                for item in range(n_items):
                    if layers is None:
                        # Initialize an empty data block
                        data = np.ndarray(shape=(nt, zNum, yNum, xNum),
                                          dtype=float)
                        data_list.append(data)
                    else:
                        data = np.ndarray(shape=(nt, len(layers), yNum, xNum),
                                          dtype=float)
                        data_list.append(data)

            else:
                raise ValueError(
                    "Static dfs3 files (with no time steps) are not supported."
                )

        else:
            ncoordinates = len(coordinates)
            for item in range(n_items):
                # Initialize an empty data block
                data = np.ndarray(shape=(nt, ncoordinates), dtype=float)
                data_list.append(data)

        t = []
        startTime = dfs.FileInfo.TimeAxis.StartDateTime

        if coordinates is None:
            for it in range(nt):
                for item in range(n_items):
                    itemdata = dfs.ReadItemTimeStep(item_numbers[item] + 1, it)

                    src = itemdata.Data
                    d = to_numpy(src)

                    # DO a direct copy instead of eleement by elment
                    d = d.reshape(zNum, yNum,
                                  xNum)  # .swapaxes(0, 2).swapaxes(0, 1)
                    d = np.flipud(d)
                    d[d == deleteValue] = np.nan
                    if layers is None:
                        data_list[item][it, :, :, :] = d
                    else:
                        for l in range(len(layers)):
                            data_list[item][it, l, :, :] = d[layers[l], :, :]

                t.append(
                    startTime.AddSeconds(
                        itemdata.Time).ToString("yyyy-MM-dd HH:mm:ss"))
        else:
            indices = [
                self.__calculate_index(xNum, yNum, zNum, x, y, z)
                for x, y, z in coordinates
            ]
            for it in range(nt):
                for item in range(n_items):
                    itemdata = dfs.ReadItemTimeStep(item_numbers[item] + 1, it)
                    d = np.array([itemdata.Data[i] for i in indices])
                    d[d == deleteValue] = np.nan
                    data_list[item][it, :] = d

                t.append(
                    startTime.AddSeconds(
                        itemdata.Time).ToString("yyyy-MM-dd HH:mm:ss"))

        # time = pd.DatetimeIndex(t)
        time = [datetime.strptime(x, "%Y-%m-%d %H:%M:%S") for x in t]
        names = []
        for item in range(n_items):
            name = dfs.ItemInfo[item_numbers[item]].Name
            names.append(name)

        dfs.Close()

        return Dataset(data_list, time, names)
Ejemplo n.º 47
0
    def get_rawimage(self, raw_file, det):
        """
        Read raw images and generate a few other bits and pieces
        that are key for image processing.

        Parameters
        ----------
        raw_file : :obj:`str`
            File to read
        det : :obj:`int`
            1-indexed detector to read

        Returns
        -------
        detector_par : :class:`pypeit.images.detector_container.DetectorContainer`
            Detector metadata parameters.
        raw_img : `numpy.ndarray`_
            Raw image for this detector.
        hdu : `astropy.io.fits.HDUList`_
            Opened fits file
        exptime : :obj:`float`
            Exposure time read from the file header
        rawdatasec_img : `numpy.ndarray`_
            Data (Science) section of the detector as provided by setting the
            (1-indexed) number of the amplifier used to read each detector
            pixel. Pixels unassociated with any amplifier are set to 0.
        oscansec_img : `numpy.ndarray`_
            Overscan section of the detector as provided by setting the
            (1-indexed) number of the amplifier used to read each detector
            pixel. Pixels unassociated with any amplifier are set to 0.
        """
        # Check for file; allow for extra .gz, etc. suffix
        fil = glob.glob(raw_file + '*')
        if len(fil) != 1:
            msgs.error("Found {:d} files matching {:s}".format(len(fil)))

        # Read
        msgs.info("Reading MMIRS file: {:s}".format(fil[0]))
        hdu = io.fits_open(fil[0])
        head1 = fits.getheader(fil[0], 1)

        detector_par = self.get_detector_par(det if det is not None else 1,
                                             hdu=hdu)

        # get the x and y binning factors...
        binning = head1['CCDSUM']
        xbin, ybin = [int(ibin) for ibin in binning.split(' ')]

        # First read over the header info to determine the size of the output array...
        datasec = head1['DATASEC']
        x1, x2, y1, y2 = np.array(parse.load_sections(
            datasec, fmt_iraf=False)).flatten()

        # ToDo: I am currently using the standard double correlated frame, that is a difference between
        # the first and final read-outs. In the future need to explore up-the-ramp fitting.
        if len(hdu) > 2:
            data = mmirs_read_amp(
                hdu[1].data.astype('float64')) - mmirs_read_amp(
                    hdu[2].data.astype('float64'))
        else:
            data = mmirs_read_amp(hdu[1].data.astype('float64'))
        array = data[x1 - 1:x2, y1 - 1:y2]

        ## ToDo: This is a hack. Need to solve this issue. I cut at 998 due to the HK zero order contaminating
        ## the blue part of the zJ+HK spectrum. For other setup, you do not need to cut the detector.
        if (head1['FILTER'] == 'zJ') and (head1['DISPERSE'] == 'HK'):
            array = array[:int(998 / ybin), :]
        rawdatasec_img = np.ones_like(array, dtype='int')
        # NOTE: If there is no overscan, must be set to 0s
        oscansec_img = np.zeros_like(array, dtype='int')

        # Need the exposure time
        exptime = hdu[self.meta['exptime']['ext']].header[self.meta['exptime']
                                                          ['card']]
        # Return, transposing array back to orient the overscan properly
        return detector_par, np.flipud(array), hdu, exptime, np.flipud(rawdatasec_img),\
               np.flipud(np.flipud(oscansec_img))
    def constant_q(self, workspace, peak):
        """
            Compute reflectivity using constant-Q binning
        """
        # Extract the x-pixel vs TOF data
        signal = workspace.extractY()
        signal_err = workspace.extractE()
        signal=np.flipud(signal)
        signal_err=np.flipud(signal_err)

        theta = self.calculate_scattering_angle(workspace)
        two_theta_degrees = 2.0*theta*180.0/math.pi
        AddSampleLog(Workspace=workspace, LogName='two_theta', LogText=str(two_theta_degrees),
                     LogType='Number', LogUnit='degree')

        # Get an array with the center wavelength value for each bin
        wl_values = workspace.readX(0)

        AddSampleLog(Workspace=workspace, LogName='lambda_min', LogText=str(wl_values[0]),
                     LogType='Number', LogUnit='Angstrom')
        AddSampleLog(Workspace=workspace, LogName='lambda_max', LogText=str(wl_values[-1]),
                     LogType='Number', LogUnit='Angstrom')

        x_pixel_map = np.mgrid[peak[0]:peak[1]+1, 0:len(wl_values)]
        x_pixel_map = x_pixel_map[0,:,:]

        pixel_width = float(workspace.getInstrument().getNumberParameter("pixel-width")[0]) / 1000.0
        det_distance = workspace.getRun()['SampleDetDis'].getStatistics().mean / 1000.0
        round_up = self.getProperty("RoundUpPixel").value
        ref_pix = self.getProperty("SpecularPixel").value
        if round_up:
            x_distance=(x_pixel_map-np.round(ref_pix)) * pixel_width
        else:
            # We offset by 0.5 pixels because the reference pixel is given as
            # a fractional position relative to the start of a pixel, whereas the pixel map
            # assumes the center of each pixel.
            x_distance=(x_pixel_map-ref_pix-0.5) * pixel_width

        theta_f = np.arcsin(x_distance/det_distance)/2.0

        # Calculate Qx, Qz for each pixel
        LL,TT = np.meshgrid(wl_values, theta_f[:,0])

        qz=4*math.pi/LL * np.sin(theta + TT) * np.cos(TT)
        qz = qz.T

        AddSampleLog(Workspace=workspace, LogName='q_min', LogText=str(np.min(qz)),
                     LogType='Number', LogUnit='1/Angstrom')
        AddSampleLog(Workspace=workspace, LogName='q_max', LogText=str(np.max(qz)),
                     LogType='Number', LogUnit='1/Angstrom')

        # Transform q values to q indices
        q_min_input = self.getProperty("QMin").value
        q_step = self.getProperty("QStep").value

        # We use np.digitize to bin. The output of digitize is a bin
        # number for each entry, starting at 1. The first bin (0) is
        # for underflow entries, and the last bin is for overflow entries.
        if q_step > 0:
            n_q_values = int((np.max(qz) - q_min_input) / q_step)
            axis_z = np.linspace(q_min_input, np.max(qz), n_q_values)
        else:
            n_q_values = int((np.log10(np.max(qz))-np.log10(q_min_input)) / np.log10(1.0-q_step))
            axis_z = np.array([q_min_input * (1.0-q_step)**i for i in range(n_q_values)])

        refl = np.zeros(len(axis_z)-1)
        refl_err = np.zeros(len(axis_z)-1)
        signal_n = np.zeros(len(axis_z)-1)

        err_count = 0
        for tof in range(qz.shape[0]):
            z_inds = np.digitize(qz[tof], axis_z)

            # Move the indices so the valid bin numbers start at zero,
            # since this is how we are going to address the output array
            z_inds -= 1

            for ix in range(signal.shape[0]):
                if z_inds[ix]<len(axis_z)-1 and z_inds[ix]>=0 and not np.isnan(signal[ix][tof]):
                    refl[z_inds[ix]] += signal[ix][tof]
                    refl_err[z_inds[ix]] += signal_err[ix][tof] * signal_err[ix][tof]
                    signal_n[z_inds[ix]] += 1.0
                elif signal[ix][tof]>0:
                    err_count += 1
        logger.notice("Ignored pixels: %s" % err_count)

        signal_n = np.where(signal_n > 0, signal_n, 1)
        refl = float(signal.shape[0]) * refl / signal_n
        refl_err = float(signal.shape[0]) * np.sqrt(refl_err) / signal_n

        # Trim Qz bins with fewer than a certain number of wavelength bins
        # contributing to them,
        trim_factor = self.getProperty("ConstQTrim").value
        for i in range(len(refl)):
            if signal_n[i] < np.max(signal_n) * trim_factor:
                refl[i] = 0.0
                refl_err[i] = 0.0

        q_rebin = CreateWorkspace(DataX=axis_z, DataY=refl, DataE=refl_err, ParentWorkspace=workspace)
        # At this point we still have a histogram, and we need to convert to point data
        q_rebin = ConvertToPointData(InputWorkspace=q_rebin)
        return q_rebin
Ejemplo n.º 49
0
    def run_with_adaptive_time_step_solver(self, dt=1.0):
        """Run step with CHILD-like solver that adjusts time steps to prevent
        slope flattening.

        Examples
        --------
        >>> from landlab import RasterModelGrid
        >>> from landlab.components import FlowAccumulator
        >>> import numpy as np

        >>> rg = RasterModelGrid((3, 4))
        >>> z = rg.add_zeros('topographic__elevation', at='node')
        >>> z[:] = 0.1 * rg.x_of_node
        >>> H = rg.add_zeros('soil__depth', at='node')
        >>> H += 0.1
        >>> br = rg.add_zeros('bedrock__elevation', at='node')
        >>> br[:] = z - H

        >>> fa = FlowAccumulator(rg, flow_director='FlowDirectorSteepest')
        >>> fa.run_one_step()
        >>> sp = Space(rg, K_sed=1.0, K_br=0.1,
        ...            F_f=0.5, phi=0.0, H_star=1., v_s=1.0,
        ...            m_sp=0.5, n_sp = 1.0, sp_crit_sed=0,
        ...            sp_crit_br=0, solver='adaptive')
        >>> sp.run_one_step(dt=10.0)

        >>> np.round(sp.Es[5:7], 4)
        array([ 0.0029,  0.0074])
        >>> np.round(sp.Er[5:7], 4)
        array([ 0.0032,  0.0085])
        >>> np.round(H[5:7], 3)
        array([ 0.088,  0.078])
        """

        # Initialize remaining_time, which records how much of the global time
        # step we have yet to use up.
        remaining_time = dt

        z = self._grid.at_node["topographic__elevation"]
        br = self._grid.at_node["bedrock__elevation"]
        H = self._grid.at_node["soil__depth"]
        r = self._flow_receivers
        time_to_flat = np.zeros(len(z))
        time_to_zero_alluv = np.zeros(len(z))
        dzdt = np.zeros(len(z))
        cores = self._grid.core_nodes

        first_iteration = True

        if not self._erode_flooded_nodes:
            flood_status = self._grid.at_node["flood_status_code"]
            flooded_nodes = np.nonzero(flood_status == _FLOODED)[0]
        else:
            flooded_nodes = []

        flooded = np.full(self._grid.number_of_nodes, False, dtype=bool)
        flooded[flooded_nodes] = True

        # Outer WHILE loop: keep going until time is used up
        while remaining_time > 0.0:

            # Update all the flow-link slopes.
            #
            # For the first iteration, we assume this has already been done
            # outside the component (e.g., by flow router), but we need to do
            # it ourselves on subsequent iterations.
            if not first_iteration:
                # update the link slopes
                self._update_flow_link_slopes()
                # update where nodes are flooded. This shouuldn't happen because
                # of the dynamic timestepper, but just in case, we update here.
                new_flooded_nodes = np.where(self._slope < 0)[0]
                flooded_nodes = np.asarray(
                    np.unique(
                        np.concatenate((flooded_nodes, new_flooded_nodes))),
                    dtype=np.int64,
                )
            else:
                first_iteration = False

            # Calculate rates of entrainment
            self._calc_hydrology()
            self._calc_erosion_rates()

            # CORRECTION HERE?
            self._Es[flooded_nodes] = 0.0
            self._Er[flooded_nodes] = 0.0

            # Zero out sediment influx for new iteration
            self._qs_in[:] = 0

            calculate_qs_in(
                np.flipud(self._stack),
                self._flow_receivers,
                self._cell_area_at_node,
                self._q,
                self._qs,
                self._qs_in,
                self._Es,
                self._Er,
                self._v_s,
                self._F_f,
            )

            self._depo_rate[self._q > 0] = self._qs[self._q > 0] * (
                self._v_s / self._q[self._q > 0])
            # TODO handle flooded nodes in the above fn

            # Now look at upstream-downstream node pairs, and recording the
            # time it would take for each pair to flatten. Take the minimum.
            dzdt[cores] = self._depo_rate[cores] - (self._Es[cores] +
                                                    self._Er[cores])
            rocdif = dzdt - dzdt[r]
            zdif = z - z[r]
            time_to_flat[:] = remaining_time

            converging = np.where(rocdif < 0.0)[0]
            time_to_flat[converging] = -(TIME_STEP_FACTOR * zdif[converging] /
                                         rocdif[converging])
            time_to_flat[np.where(zdif <= 0.0)[0]] = remaining_time

            # From this, find the maximum stable time step with regard to slope
            # evolution.
            dt_max1 = np.amin(time_to_flat)

            # Next we consider time to exhaust regolith
            time_to_zero_alluv[:] = remaining_time
            dHdt = self._porosity_factor * (self._depo_rate - self._Es)
            decreasing_H = np.where(dHdt < 0.0)[0]
            time_to_zero_alluv[decreasing_H] = -(
                TIME_STEP_FACTOR * H[decreasing_H] / dHdt[decreasing_H])

            # Now find the smallest time that would lead to near-empty alluv
            dt_max2 = np.amin(time_to_zero_alluv)

            # Take the smaller of the limits
            dt_max = max(self._dt_min, min(dt_max1, dt_max2))

            # Now a vector operation: apply dzdt and dhdt to all nodes
            br[cores] -= self._Er[cores] * dt_max
            H[cores] += dHdt[cores] * dt_max
            z[cores] = br[cores] + H[cores]

            # Update remaining time and continue
            remaining_time -= dt_max
Ejemplo n.º 50
0
    def run(self):
        if not self.tello.connect():
            print("Tello not connected")
            return
        if not self.tello.set_speed(self.speed):
            print("Not set speed to lowest possible")
            return

        self.tello.streamoff()
        self.tello.streamon()
        cap = self.tello.get_frame_read()

        should_stop = False
        # print('loop started')

        while not should_stop:
            frame = cap.frame  # 摄像头读取一帧
            for event in pygame.event.get():
                if event.type == USEREVENT + 1:
                    self.update()
                elif event.type == QUIT:
                    should_stop = True
                elif event.type == KEYDOWN:
                    if event.key == K_ESCAPE:
                        should_stop = True
                    else:
                        self.keydown(event.key)
                elif event.type == KEYUP:
                    self.keyup(event.key)

            self.screen.fill([0, 0, 0])

            hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
            green_mask = cv2.inRange(
                hsv, greenLower, greenUpper
            )  # 将低于lower_red和高于upper_red的部分分别变成0,lower_red~upper_red之间的值变成255
            kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (10, 10))
            green_mask = cv2.morphologyEx(green_mask, cv2.MORPH_OPEN,
                                          kernel)  # 开操作
            green_mask = cv2.morphologyEx(green_mask, cv2.MORPH_CLOSE,
                                          kernel)  # 闭操作
            cnts, hie = cv2.findContours(green_mask, cv2.RETR_TREE,
                                         cv2.CHAIN_APPROX_SIMPLE)

            if len(cnts) > 3:
                cnts.sort(key=cv2.contourArea, reverse=True)
                x1, y1, w1, h1 = cv2.boundingRect(cnts[0])
                x2, y2, w2, h2 = cv2.boundingRect(cnts[1])
                x3, y3, w3, h3 = cv2.boundingRect(cnts[2])
                x4, y4, w4, h4 = cv2.boundingRect(cnts[3])

                center1 = (int(x1 + w1 / 2), int(y1 + h1 / 2))
                center2 = (int(x2 + w2 / 2), int(y2 + h2 / 2))
                center3 = (int(x3 + w3 / 2), int(y3 + h3 / 2))
                center4 = (int(x4 + w4 / 2), int(y4 + h4 / 2))

                center = (
                    int((center1[0] + center2[0] + center3[0] + center4[0]) /
                        4),
                    int((center1[1] + center2[1] + center3[1] + center4[1]) /
                        4))
                w = max(abs(center1[0] - center2[0]),
                        abs(center1[0] - center3[0]),
                        abs(center1[0] - center4[0]))
                h = max(abs(center1[1] - center2[1]),
                        abs(center1[1] - center3[1]),
                        abs(center1[1] - center4[1]))
                # delta = min(abs(center2[1]-center1[1]), abs(center3[1]-center1[1]), abs(center4[1]-center1[1]))
                temp_a = center2[1] - center1[1]
                temp_b = center3[1] - center1[1]
                temp_c = center4[1] - center1[1]
                delta = min(abs(temp_a), abs(temp_b), abs(temp_c))
                flag = bool(temp_a * temp_b * temp_c > 0)  # 左偏

                percent = (w * h / 691200.) * 100.0
                cnt = torch.Tensor(center)
                actions = self.actor(cnt)
                actions = actions.cpu().data.numpy()
                # actions = speedControlLinear(center)

                done = bool(
                    get_dist(center[0], center[1]) < 60 and
                    (percent > 40)) and (delta < 10)
                # done = bool(get_dist(center[0], center[1]) < 60 and (percent > 40))
                if not done:
                    self.yaw_velocity = -int(actions[0])  # 强化学习需要加"-"
                    self.up_down_velocity = int(actions[1])
                    if percent < 40:
                        self.for_back_velocity = 15
                        if delta > 10:
                            # self.left_right_velocity = 10
                            # self.yaw_velocity = -10
                            if flag:
                                self.left_right_velocity = 10
                                self.yaw_velocity = -10
                            else:
                                self.left_right_velocity = -10
                                self.yaw_velocity = 10
                    cv2.circle(frame, center, 5, (255, 0, 0), -1)
                else:
                    self.yaw_velocity = 0
                    self.up_down_velocity = 0
                    self.for_back_velocity = 0
            else:
                self.for_back_velocity = -15
                # self.yaw_velocity = -self.yaw_velocity
                # self.up_down_velocity = -self.up_down_velocity

            frame = cv2.circle(frame, (480, 360), 5, (0, 0, 255), -1)
            cv2.imshow("xx", green_mask)
            k = cv2.waitKey(1)
            if k == 27:
                break

            frame = np.rot90(frame)
            frame = np.flipud(frame)
            frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
            frame = pygame.surfarray.make_surface(frame)
            self.screen.blit(frame, (0, 0))
            pygame.display.update()

            # time.sleep(0.1)

        # Call it always before finishing. I deallocate resources.
        self.tello.end()
Ejemplo n.º 51
0
 def flip_vert(self):
     "flip image along vertical axis (up/down)"
     self.data = np.flipud(self.data)
     if self.ydata is not None:
         self.ydata = self.ydata[::-1]
     self.flip_ud = not self.flip_ud
Ejemplo n.º 52
0
def main(_):
    tf_flags = tf.app.flags.FLAGS
    # gpu config.
    config = tf.ConfigProto()
    config.gpu_options.per_process_gpu_memory_fraction = 0.9

    if tf_flags.phase == "train":
        with tf.Session(config=config) as sess: 
            # sess = tf.Session(config=config) # when use queue to load data, not use with to define sess
            train_model = MemNet_rgb.MemNet(sess, tf_flags)
            train_model.train(tf_flags.training_steps, tf_flags.summary_steps, 
                tf_flags.checkpoint_steps, tf_flags.save_steps)
    else:
        with tf.Session(config=config) as sess:
            test_model = MemNet_rgb.MemNet(sess, tf_flags)
            test_model.load(tf_flags.checkpoint)
            # test Set12
            # get psnr and ssim outside
            sigma =10/255.
            save_path = "./test_output/memnet_10_rgb_no_augs_urban100/"
            aug_mean = False
            if not os.path.exists(save_path):
                os.makedirs(save_path)
            for image_file in glob.glob(tf_flags.testing_set):
                print("testing {}...".format(image_file))
                # testing_set is path/*.jpg.
                # c_image = np.reshape(cv2.resize(cv2.imread(image_file, 0), (tf_flags.img_size, tf_flags.img_size)),
                #     (1, tf_flags.img_size, tf_flags.img_size, 1)) / 255.
                # c_image = np.reshape(cv2.resize(cv2.imread(image_file, 0), (256, 256)), (1, 256, 256, 1)) / 255.
                if aug_mean:
                    c_image = cv2.imread(image_file)/255.
                    c_image = c_image.transpose(2, 0, 1)
                    noise = np.random.normal(0.0, 1.0, size=c_image.shape)*sigma
                    noise2 = np.rot90(noise, 1, (1, 2))
                    c_image_aug = create_augmentations(c_image)
                    test_out = []
                    for aug_, c_image_aug_ in enumerate(c_image_aug):
                        if aug_%2==0:
                            c_image_aug_ = np.clip(c_image_aug_ + noise, 0, 1).transpose(1, 2, 0)
                            c_image_aug_ = c_image_aug_[np.newaxis, :, :, :]
                        elif aug_%2==1:
                            c_image_aug_ = np.clip(c_image_aug_ + noise2, 0, 1).transpose(1, 2, 0)
                            c_image_aug_ = c_image_aug_[np.newaxis, :, :, :]
                        recovery_image_ = test_model.test(c_image_aug_)
                        test_out.append(recovery_image_[0])
                    test_out[0] = test_out[0]
                    for aug in range(1, 8):
                        if aug < 4:
                            test_out[aug] = np.rot90(test_out[aug], 4 - aug)
                        else:
                            test_out[aug] = np.flipud(np.rot90(test_out[aug], 8 - aug))
                    recovery_image = np.mean(test_out, 0)
                    cv2.imwrite(os.path.join(save_path, image_file.split("/")[3]), np.uint8(recovery_image.clip(0., 1.) * 255.))
                else:
                    c_image = cv2.imread(image_file)/255.
                    noise = np.random.normal(0.0, 1.0, size=c_image.shape) * sigma
                    c_image = np.clip(c_image+noise,0, 1)
                    c_image = c_image[np.newaxis,:,:,:]

                    # In Caffe, Tensorflow, might must divide 255.?
                    recovery_image = test_model.test(c_image)

                    # save image
                    cv2.imwrite(os.path.join(save_path, image_file.split("/")[3]),
                        np.uint8(recovery_image[0, :].clip(0., 1.) * 255.))
                # recovery_image[0, :], 3D array.
            print("Testing done.")
Ejemplo n.º 53
0
yieldfa = N.zeros((10, 360, 720))
yieldf2a = N.zeros((10, 360, 720))
yieldf = N.zeros((10, 360, 720))
yieldf2 = N.zeros((10, 360, 720))
years2 = range(2090, 2100)

clmtropfin = N.zeros((10, 360, 720))
clmtempfin = N.zeros((10, 360, 720))

for j in range(0, 10):

    clm2n = NetCDFFile(
        '/scratch2/scratchdirs/tslin2/plot/globalcrop/data/clm/clm45rcp45/maizetrop_rcp45_co2_rf_fert_0.5x0.5.nc',
        'r')
    cc = N.flipud(clm2n.variables['yield'][84 + j, :, :])
    clmtropfin[j, :, :] = cc

    clm3n = NetCDFFile(
        '/scratch2/scratchdirs/tslin2/plot/globalcrop/data/clm/clm45rcp45/maizetemp_rcp45_co2_rf_fert_0.5x0.5.nc',
        'r')
    dd = N.flipud(clm3n.variables['yield'][84 + j, :, :])
    clmtempfin[j, :, :] = dd

yield_new2a = N.average(clmtropfin, axis=0)
yield_new2 = N.average(clmtempfin, axis=0)

yield_new2 = ma.masked_where(yield_new2 <= 0., yield_new2)
yield_new2a = ma.masked_where(yield_new2a <= 0., yield_new2a)
yield_new2 = ma.masked_where(maitoatemp <= 0, yield_new2)
yield_new2 = ma.filled(yield_new2, fill_value=0.)
Ejemplo n.º 54
0
    def run_one_step_basic(self, dt=1.0):
        """Calculate change in rock and alluvium thickness for a time period
        'dt'.

        Parameters
        ----------
        dt : float
            Model timestep [T]
        """
        # Choose a method for calculating erosion:
        self._calc_hydrology()
        self._calc_erosion_rates()

        if not self._erode_flooded_nodes:
            flood_status = self._grid.at_node["flood_status_code"]
            flooded_nodes = np.nonzero(flood_status == _FLOODED)[0]
        else:
            flooded_nodes = []

        flooded = np.full(self._grid.number_of_nodes, False, dtype=bool)
        flooded[flooded_nodes] = True

        self._qs_in[:] = 0

        # iterate top to bottom through the stack, calculate qs
        # cythonized version of calculating qs_in
        calculate_qs_in(
            np.flipud(self._stack),
            self._flow_receivers,
            self._cell_area_at_node,
            self._q,
            self._qs,
            self._qs_in,
            self._Es,
            self._Er,
            self._v_s,
            self._F_f,
        )

        self._depo_rate[self._q > 0] = self._qs[self._q > 0] * (
            self._v_s / self._q[self._q > 0])

        # now, the analytical solution to soil thickness in time:
        # need to distinguish D=kqS from all other cases to save from blowup!

        # distinguish cases:
        blowup = self._depo_rate == self._K_sed * self._Q_to_the_m * self._slope

        # first, potential blowup case:
        # positive slopes, not flooded
        pos_not_flood = (self._q > 0) & (blowup) & (self._slope >
                                                    0) & (~flooded)
        self._soil__depth[pos_not_flood] = self._H_star * np.log(
            ((self._sed_erosion_term[pos_not_flood] /
              (1 - self._phi)) / self._H_star) * dt +
            np.exp(self._soil__depth[pos_not_flood] / self._H_star))
        # positive slopes, flooded
        pos_flood = (self._q > 0) & (blowup) & (self._slope > 0) & (flooded)
        self._soil__depth[pos_flood] = (self._depo_rate[pos_flood] /
                                        (1 - self._phi)) * dt

        # non-positive slopes, not flooded
        non_pos_not_flood = (self._q > 0) & (blowup) & (self._slope <=
                                                        0) & (~flooded)
        self._soil__depth[non_pos_not_flood] += (
            self._depo_rate[non_pos_not_flood] / (1 - self._phi) * dt)

        # more general case:
        pos_not_flood = (self._q > 0) & (~blowup) & (self._slope >
                                                     0) & (~flooded)

        self._soil__depth[pos_not_flood] = self._H_star * np.log(
            (1 / ((self._depo_rate[pos_not_flood] / (1 - self._phi)) /
                  (self._sed_erosion_term[pos_not_flood] /
                   (1 - self._phi)) - 1)) *
            (np.exp((self._depo_rate[pos_not_flood] / (1 - self._phi) -
                     (self._sed_erosion_term[pos_not_flood] /
                      (1 - self._phi))) * (dt / self._H_star)) *
             (((self._depo_rate[pos_not_flood] / (1 - self._phi) /
                (self._sed_erosion_term[pos_not_flood] /
                 (1 - self._phi))) - 1) *
              np.exp(self._soil__depth[pos_not_flood] / self._H_star) + 1) -
             1))

        # places where slope <= 0 but not flooded:
        neg_slope_not_flooded = ((self._q > 0) & (~blowup) & (self._slope <= 0)
                                 & (~flooded))
        self._soil__depth[neg_slope_not_flooded] += (
            self._depo_rate[neg_slope_not_flooded] / (1 - self._phi) * dt)

        # flooded nodes:
        flooded_nodes = (self._q > 0) & (~blowup) & (flooded)
        self._soil__depth[flooded_nodes] += (self._depo_rate[flooded_nodes] /
                                             (1 - self._phi) * dt)

        # where discharge exists
        discharge_exists = self._q > 0
        self._bedrock__elevation[discharge_exists] += dt * (
            -self._br_erosion_term[discharge_exists] *
            (np.exp(-self._soil__depth[discharge_exists] / self._H_star)))

        # finally, determine topography by summing bedrock and soil
        cores = self._grid.core_nodes
        self._topographic__elevation[cores] = (
            self._bedrock__elevation[cores] + self._soil__depth[cores])
Ejemplo n.º 55
0
def flow_read(src_file):
    """Read optical flow stored in a .flo, .pfm, or .png file
    Args:
        src_file: Path to flow file
    Returns:
        flow: optical flow in [h, w, 2] format
    Refs:
        - Interpret bytes as packed binary data
        Per https://docs.python.org/3/library/struct.html#format-characters:
        format: f -> C Type: float, Python type: float, Standard size: 4
        format: d -> C Type: double, Python type: float, Standard size: 8
    Based on:
        - To read optical flow data from 16-bit PNG file:
        https://github.com/ClementPinard/FlowNetPytorch/blob/master/datasets/KITTI.py
        Written by Clément Pinard, Copyright (c) 2017 Clément Pinard
        MIT License
        - To read optical flow data from PFM file:
        https://github.com/liruoteng/OpticalFlowToolkit/blob/master/lib/pfm.py
        Written by Ruoteng Li, Copyright (c) 2017 Ruoteng Li
        License Unknown
        - To read optical flow data from FLO file:
        https://github.com/daigo0927/PWC-Net_tf/blob/master/flow_utils.py
        Written by Daigo Hirooka, Copyright (c) 2018 Daigo Hirooka
        MIT License
    """
    # Read in the entire file, if it exists
    assert (os.path.exists(src_file))

    if src_file.lower().endswith('.flo'):

        with open(src_file, 'rb') as f:

            # Parse .flo file header
            tag = float(np.fromfile(f, np.float32, count=1)[0])
            assert (tag == TAG_FLOAT)
            w = np.fromfile(f, np.int32, count=1)[0]
            h = np.fromfile(f, np.int32, count=1)[0]

            # Read in flow data and reshape it
            flow = np.fromfile(f, np.float32, count=h * w * 2)
            flow.resize((h, w, 2))

    elif src_file.lower().endswith('.png'):

        # Read in .png file
        flow_raw = cv2.imread(src_file, -1)

        # Convert from [H,W,1] 16bit to [H,W,2] float formet
        flow = flow_raw[:, :, 2:0:-1].astype(np.float32)
        flow = flow - 32768
        flow = flow / 64

        # Clip flow values
        flow[np.abs(flow) < 1e-10] = 1e-10

        # Remove invalid flow values
        invalid = (flow_raw[:, :, 0] == 0)
        flow[invalid, :] = 0

    elif src_file.lower().endswith('.pfm'):

        with open(src_file, 'rb') as f:

            # Parse .pfm file header
            tag = f.readline().rstrip().decode("utf-8")
            assert (tag == 'PF')
            dims = f.readline().rstrip().decode("utf-8")
            w, h = map(int, dims.split(' '))
            scale = float(f.readline().rstrip().decode("utf-8"))

            # Read in flow data and reshape it
            flow = np.fromfile(f, '<f') if scale < 0 else np.fromfile(f, '>f')
            flow = np.reshape(flow, (h, w, 3))[:, :, 0:2]
            flow = np.flipud(flow)

    elif src_file.lower().endswith('.npy'):
        H, W = (376, 1241)
        flow_raw = np.load(src_file)
        u = cv2.resize(flow_raw[..., 0], (W, H))
        v = cv2.resize(flow_raw[..., 1], (W, H))
        flow = np.dstack((u, v))
        flow[..., 0] *= W
        flow[..., 1] *= H

    else:
        raise IOError

    return flow
Ejemplo n.º 56
0
def cal_ext_paras(ind_ls = (np.arange(1, params['poses_num']+1)).tolist()):
    #ind_ls: Indexes of pairs used for optimization
	
    ls = ind_ls
    # res_ls = []
    # pnp_ls = []

    corners_in_image_all = np.array([]).reshape(0, 2)
    corners_in_pcd_all = np.array([]).reshape(0, 3)

    if params['camera_type'] == "panoramic":
        print "Optimize the extrinsic parameters with panoramic model."
        for i in ls:
            imgfile = "img/" + str(i).zfill(params['file_name_digits']) + "." + params['image_format']
            cords_file = params['base_dir']+"/output/img_corners/" + str(i).zfill(params['file_name_digits']) + "_img_corners.txt"
            corners_in_img_arr = np.genfromtxt(cords_file, delimiter=",").astype(np.int32)
            # make sure the corners are counted start from left lower
            if np.linalg.norm(np.array(corners_in_img_arr[0]) - np.array([0, H])) > np.linalg.norm(
                    np.array(corners_in_img_arr[-1]) - np.array([0, H])):
                print imgfile + " is counted in reversed order"
                corners_in_img_arr = np.flipud(corners_in_img_arr)

            pcd_result_file = params['base_dir']+"/output/pcd_seg/" + str(i).zfill(params['file_name_digits']) + "_pcd_result.pkl"
            # print imgfile
            with open(os.path.abspath(pcd_result_file), "r") as f:
                pcd_result_ls = cPickle.load(f)
            assert pcd_result_ls is not None

            corner_arr = pcd_result_ls[4].reshape(-1, 2)

            num = corner_arr.shape[0]
            corner_arr = np.hstack([corner_arr, np.zeros(num).reshape(num, 1)])
            # print corner_arr.shape
            rot1 = pcd_result_ls[0]
            t1 = pcd_result_ls[1].reshape(1, 3)
            rot2 = pcd_result_ls[2]
            t2 = pcd_result_ls[3].reshape(1, 3)

            corners_in_pcd_arr = np.dot(np.dot(rot2.T, corner_arr.T).T - t2 + t1, rot1)

            corners_in_image_all = np.vstack([corners_in_image_all, corners_in_img_arr])
            corners_in_pcd_all = np.vstack([corners_in_pcd_all, corners_in_pcd_arr])
        # print "corners_in_pcd_all num of rows: ", corners_in_pcd_all.shape

        try:
            initial_guess = calc_inintial_guess(corners_in_image_all.copy(), corners_in_pcd_all.copy(),
                                                method="UPNP")
        except:
            upnp_cost = np.inf
            print "upnp can not be applied"

        res = opt_r_t(corners_in_image_all, corners_in_pcd_all, initial_guess=initial_guess,
                      save_backproj=False, )  # initial_guess=initial_guess,
        # res_ls.append(convert2_ang_cm(res.x))
        # pnp_ls.append(initial_guess)

        print "initial guess by UPnP:", initial_guess
        print
        print "final extrinsic parameters:"
        cal_file_name = time.strftime("%Y%m%d_%H%M%S_cali_result.txt")
        np.savetxt(cal_file_name, res.x, delimiter=',')
        print "refined by LM : ", res.x, " unit: [rad,rad,rad,m,m,m]. The result is Saved to ", cal_file_name
        print "unit converted : ", convert2_ang_cm(res.x), "unit: [deg,deg,deg,cm,cm,cm]"
        print
        print
        print
        # print "The difference of relative_pose - res.x: ", initial_guess - res.x
        # np.savetxt('intes_result', res_ls, delimiter=',')
        # np.savetxt('pnp_result', pnp_ls, delimiter=',')

        # back_proj = True
        if params['back_proj_corners']:
            # for i in range(start, end):
            for i in ls:
                imgfile = os.path.join(params['base_dir'], "img/") + str(i).zfill(params['file_name_digits']) + "." + \
                          params['image_format']
                cords_file = os.path.join(params['base_dir'], "output/img_corners/") + str(i).zfill(
                    params['file_name_digits']) + "_img_corners.txt"
                corners_in_img_arr = np.genfromtxt(cords_file, delimiter=",").astype(np.int32)
                # make sure the corners are counted start from left lower
                if np.linalg.norm(np.array(corners_in_img_arr[0]) - np.array([0, H])) > np.linalg.norm(
                        np.array(corners_in_img_arr[-1]) - np.array([0, H])):
                    corners_in_img_arr = np.flipud(corners_in_img_arr)

                pcd_result_file = params['base_dir']+"/output/pcd_seg/" + str(i).zfill(params['file_name_digits']) + "_pcd_result.pkl"
                # print imgfile
                with open(os.path.abspath(pcd_result_file), "r") as f:
                    pcd_result_ls = cPickle.load(f)
                assert pcd_result_ls is not None

                corner_arr = pcd_result_ls[4].reshape(-1, 2)

                num = corner_arr.shape[0]
                corner_arr = np.hstack([corner_arr, np.zeros(num).reshape(num, 1)])
                rot1 = pcd_result_ls[0]
                t1 = pcd_result_ls[1].reshape(1, 3)
                rot2 = pcd_result_ls[2]
                t2 = pcd_result_ls[3].reshape(1, 3)
                corners_in_pcd_arr = np.dot(np.dot(rot2.T, corner_arr.T).T - t2 + t1, rot1)
                ret = back_project(res.x, cv2.imread(imgfile), corners_in_img_arr, corners_in_pcd_arr)
                save_file = params['base_dir']+"/output/" + str(i).zfill(params['file_name_digits']) + "_cal_backproj." + params[
                    'image_format']
                cv2.imwrite(save_file, ret)
    elif params['camera_type'] == "perspective":
        print "Optimize the extrinsic parameters with perspective model."
        for i in ls:
            imgfile = os.path.join(params['base_dir'], "img/") + str(i).zfill(params['file_name_digits']) + "." + \
                      params['image_format']
            cords_file = os.path.join(params['base_dir'], "output/img_corners/") + str(i).zfill(
                params['file_name_digits']) + "_img_corners.txt"
            corners_in_img_arr = np.genfromtxt(cords_file, delimiter=",").astype(np.int32)
            # make sure the corners are counted start from left lower
            if np.linalg.norm(np.array(corners_in_img_arr[0]) - np.array([0, H])) > np.linalg.norm(
                    np.array(corners_in_img_arr[-1]) - np.array([0, H])):
                print imgfile + " is counted in reversed order"
                corners_in_img_arr = np.flipud(corners_in_img_arr)

            pcd_result_file = os.path.join(params['base_dir'], "output/pcd_seg/") + str(i).zfill(
                params['file_name_digits']) + "_pcd_result.pkl"
            # print imgfile
            with open(os.path.abspath(pcd_result_file), "r") as f:
                pcd_result_ls = cPickle.load(f)
            assert pcd_result_ls is not None

            corner_arr = pcd_result_ls[4].reshape(-1, 2)

            num = corner_arr.shape[0]
            corner_arr = np.hstack([corner_arr, np.zeros(num).reshape(num, 1)])
            # print corner_arr.shape
            rot1 = pcd_result_ls[0]
            t1 = pcd_result_ls[1].reshape(1, 3)
            rot2 = pcd_result_ls[2]
            t2 = pcd_result_ls[3].reshape(1, 3)

            corners_in_pcd_arr = np.dot(np.dot(rot2.T, corner_arr.T).T - t2 + t1, rot1)

            corners_in_image_all = np.vstack([corners_in_image_all, corners_in_img_arr])
            corners_in_pcd_all = np.vstack([corners_in_pcd_all, corners_in_pcd_arr])
        # print "corners_in_pcd_all num of rows: ", corners_in_pcd_all.shape

        try:
            initial_guess = calc_inintial_guess(corners_in_image_all.copy(), corners_in_pcd_all.copy(),
                                                method="UPNP")
        except:
            upnp_cost = np.inf
            initial_guess = np.zeros(6).tolist()
            print "upnp can not be applied"

        res = opt_r_t(corners_in_image_all, corners_in_pcd_all, initial_guess=initial_guess,
                      save_backproj=False, )  # initial_guess=initial_guess,
        print res.cost
        # res_ls.append(convert2_ang_cm(res.x))
        # pnp_ls.append(initial_guess)

        print "initial guess by UPnP:", initial_guess
        print
        print "final extrinsic parameters:"
        cal_file_name = os.path.join(params['base_dir'], time.strftime("%Y%m%d_%H%M%S_cali_result.txt"))
        np.savetxt(cal_file_name, res.x, delimiter=',')
        print "refined by LM : ", res.x, " unit: [rad,rad,rad,m,m,m]. The result is Saved to ", cal_file_name
        print "unit converted : ", convert2_ang_cm(res.x), "unit: [deg,deg,deg,cm,cm,cm]"
        print
        print
        print
        # print "The difference of relative_pose - res.x: ", initial_guess - res.x
        # np.savetxt('intes_result', res_ls, delimiter=',')
        # np.savetxt('pnp_result', pnp_ls, delimiter=',')

        # back_proj = True
        if params['back_proj_corners']:
            # for i in range(start, end):
            for i in ls:
                imgfile = os.path.join(params['base_dir'], "img/") + str(i).zfill(params['file_name_digits']) + "." + \
                          params['image_format']
                cords_file = os.path.join(params['base_dir'], "output/img_corners/") + str(i).zfill(
                    params['file_name_digits']) + "_img_corners.txt"
                corners_in_img_arr = np.genfromtxt(cords_file, delimiter=",").astype(np.int32)
                # make sure the corners are counted start from left lower
                if np.linalg.norm(np.array(corners_in_img_arr[0]) - np.array([0, H])) > np.linalg.norm(
                        np.array(corners_in_img_arr[-1]) - np.array([0, H])):
                    corners_in_img_arr = np.flipud(corners_in_img_arr)

                pcd_result_file = os.path.join(params['base_dir'], "output/pcd_seg/") + str(i).zfill(
                    params['file_name_digits']) + "_pcd_result.pkl"
                # print imgfile
                with open(os.path.abspath(pcd_result_file), "r") as f:
                    pcd_result_ls = cPickle.load(f)
                assert pcd_result_ls is not None

                corner_arr = pcd_result_ls[4].reshape(-1, 2)

                num = corner_arr.shape[0]
                corner_arr = np.hstack([corner_arr, np.zeros(num).reshape(num, 1)])
                rot1 = pcd_result_ls[0]
                t1 = pcd_result_ls[1].reshape(1, 3)
                rot2 = pcd_result_ls[2]
                t2 = pcd_result_ls[3].reshape(1, 3)
                corners_in_pcd_arr = np.dot(np.dot(rot2.T, corner_arr.T).T - t2 + t1, rot1)
                ret = back_project(res.x, cv2.imread(imgfile), corners_in_img_arr, corners_in_pcd_arr)
                # ret = back_project(initial_guess, cv2.imread(imgfile), corners_in_img_arr, corners_in_pcd_arr)
                save_file = os.path.join(params['base_dir'], "output/") + str(i).zfill(
                    params['file_name_digits']) + "_cal_backproj." + params[
                                'image_format']
                cv2.imwrite(save_file, ret)

    else:
        raise "The input camera type is not implemented yet!"
Ejemplo n.º 57
0
    def __getitem__(self, index):
        if self.image_weights:
            index = self.indices[index]

        hyp = self.hyp
        if self.mosaic:
            # Load mosaic
            img, labels = load_mosaic(self, index)
            shapes = None

        else:
            # Load image
            img, (h0, w0), (h, w) = load_image(self, index)

            # Letterbox
            shape = self.batch_shapes[self.batch[
                index]] if self.rect else self.img_size  # final letterboxed shape
            img, ratio, pad = letterbox(img,
                                        shape,
                                        auto=False,
                                        scaleup=self.augment)
            shapes = (h0, w0), (
                (h / h0, w / w0), pad)  # for COCO mAP rescaling

            # Load labels
            labels = []
            x = self.labels[index]
            if x.size > 0:
                # Normalized xywh to pixel xyxy format
                labels = x.copy()
                labels[:,
                       1] = ratio[0] * w * (x[:, 1] -
                                            x[:, 3] / 2) + pad[0]  # pad width
                labels[:,
                       2] = ratio[1] * h * (x[:, 2] -
                                            x[:, 4] / 2) + pad[1]  # pad height
                labels[:, 3] = ratio[0] * w * (x[:, 1] + x[:, 3] / 2) + pad[0]
                labels[:, 4] = ratio[1] * h * (x[:, 2] + x[:, 4] / 2) + pad[1]

        if self.augment:
            # Augment imagespace
            if not self.mosaic:
                img, labels = random_affine(img,
                                            labels,
                                            degrees=hyp['degrees'],
                                            translate=hyp['translate'],
                                            scale=hyp['scale'],
                                            shear=hyp['shear'])

            # Augment colorspace
            augment_hsv(img,
                        hgain=hyp['hsv_h'],
                        sgain=hyp['hsv_s'],
                        vgain=hyp['hsv_v'])

            # Apply cutouts
            # if random.random() < 0.9:
            #     labels = cutout(img, labels)

        nL = len(labels)  # number of labels
        if nL:
            # convert xyxy to xywh
            labels[:, 1:5] = xyxy2xywh(labels[:, 1:5])
            labels[:, 5:9] = xyxy2xywh(labels[:, 5:9])

            # Normalize coordinates 0 - 1
            labels[:, [2, 4, 6, 8]] /= img.shape[0]  # height
            labels[:, [1, 3, 5, 7]] /= img.shape[1]  # width

        if self.augment:
            # random left-right flip
            lr_flip = True
            if lr_flip and random.random() < 0.5:
                img = np.fliplr(img)
                if nL:
                    labels[:, 1] = 1 - labels[:, 1]
                    labels[:, 5] = 1 - labels[:, 5]

            # random up-down flip
            ud_flip = False
            if ud_flip and random.random() < 0.5:
                img = np.flipud(img)
                if nL:
                    labels[:, 2] = 1 - labels[:, 2]
                    labels[:, 6] = 1 - labels[:, 6]

        labels_out = torch.zeros((nL, 10))
        if nL:
            labels_out[:, 1:] = torch.from_numpy(labels)

        # Convert
        img = img[:, :, ::-1].transpose(2, 0, 1)  # BGR to RGB, to 3x416x416
        img = np.ascontiguousarray(img)

        return torch.from_numpy(img), labels_out, self.img_files[index], shapes
Ejemplo n.º 58
0
def plot_fields(sim,
                ax=None,
                fields=None,
                output_plane=None,
                field_parameters=None):
    if not sim._is_initialized:
        sim.init_sim()

    if fields is None:
        return ax

    if field_parameters is None:
        field_parameters = default_field_parameters
    else:
        field_parameters = dict(default_field_parameters, **field_parameters)

    # user specifies a field component
    if fields in [
            mp.Ex, mp.Ey, mp.Ez, mp.Er, mp.Ep, mp.Dx, mp.Dy, mp.Dz, mp.Hx,
            mp.Hy, mp.Hz
    ]:
        # Get domain measurements
        sim_center, sim_size = get_2D_dimensions(sim, output_plane)

        xmin, xmax, ymin, ymax, zmin, zmax = box_vertices(sim_center, sim_size)

        if sim_size.x == 0:
            # Plot y on x axis, z on y axis (YZ plane)
            extent = [ymin, ymax, zmin, zmax]
            xlabel = 'Y'
            ylabel = 'Z'
        elif sim_size.y == 0:
            # Plot x on x axis, z on y axis (XZ plane)
            extent = [xmin, xmax, zmin, zmax]
            if (sim.dimensions == mp.CYLINDRICAL) or sim.is_cylindrical:
                xlabel = 'R'
            else:
                xlabel = "X"
            ylabel = 'Z'
        elif sim_size.z == 0:
            # Plot x on x axis, y on y axis (XY plane)
            extent = [xmin, xmax, ymin, ymax]
            xlabel = 'X'
            ylabel = 'Y'
        fields = sim.get_array(center=sim_center,
                               size=sim_size,
                               component=fields)
    else:
        raise ValueError(
            'Please specify a valid field component (mp.Ex, mp.Ey, ...')

    fields = field_parameters['post_process'](fields)
    if (sim.dimensions == mp.CYLINDRICAL) or sim.is_cylindrical:
        fields = np.flipud(fields)
    else:
        fields = np.rot90(fields)

    # Either plot the field, or return the array
    if ax:
        if mp.am_master():
            ax.imshow(fields,
                      extent=extent,
                      **filter_dict(field_parameters, ax.imshow))
        return ax
    else:
        return fields
    return ax
Ejemplo n.º 59
0
def train(
    batch_size=128,
    num_epochs=500,
    sagittal_only=False,
    num_frames=1,
    with_maps=False,
    model_new=True,
    learning_rate=0.002,
    lr_decay=False,
):

    run_str = ''
    run_str += 'NewUNet_' if model_new else 'TBMEUNet_'
    run_str += 'WMaps_' if with_maps else ''
    run_str += 'SagittalOnly_' if sagittal_only else 'AllImages_'
    run_str += 'SingleFrame' if num_frames == 1 else str(num_frames) + 'Frames'

    local_data_folder = "/home/zbaum/Baum/aigt/data"

    data_arrays_folder = "DataArrays"
    results_save_folder = "SavedResults"
    models_save_folder = "SavedModels"
    val_data_folder = "PredictionsValidation"
    data_arrays_fullpath = os.path.join(local_data_folder, data_arrays_folder)
    results_save_fullpath = os.path.join(local_data_folder,
                                         results_save_folder)
    models_save_fullpath = os.path.join(local_data_folder, models_save_folder)
    val_data_fullpath = os.path.join(local_data_folder, val_data_folder)
    if not os.path.exists(data_arrays_fullpath):
        os.makedirs(data_arrays_fullpath)
        print("Created folder: {}".format(data_arrays_fullpath))

    if not os.path.exists(results_save_fullpath):
        os.makedirs(results_save_fullpath)
        print("Created folder: {}".format(results_save_fullpath))

    if not os.path.exists(models_save_fullpath):
        os.makedirs(models_save_fullpath)
        print("Created folder: {}".format(models_save_fullpath))

    if not os.path.exists(val_data_fullpath):
        os.makedirs(val_data_fullpath)
        print("Created folder: {}".format(val_data_fullpath))

    # Learning parameters

    ultrasound_size = 128
    num_classes = 2
    min_learning_rate = 0.00001
    regularization_rate = 0.0001
    filter_multiplier = 14
    class_weights = np.array([0.1, 0.9])
    learning_rate_decay = (learning_rate -
                           min_learning_rate) / num_epochs if lr_decay else 0

    if with_maps:
        loss_func = weighted_categorical_crossentropy_with_maps(class_weights)
        preprocess_func = train_preprocess_with_maps
    else:
        loss_func = weighted_categorical_crossentropy(class_weights)
        preprocess_func = train_preprocess

    # Evaluation parameters

    acceptable_margin_mm = 1.0
    mm_per_pixel = 1.0

    roc_thresholds = [
        0.9,
        0.8,
        0.7,
        0.65,
        0.6,
        0.55,
        0.5,
        0.45,
        0.4,
        0.35,
        0.3,
        0.25,
        0.2,
        0.15,
        0.1,
        0.08,
        0.06,
        0.04,
        0.02,
        0.01,
        0.008,
        0.006,
        0.004,
        0.002,
        0.001,
    ]
    """
    Provide NxM numpy array to schedule cross validation
    N rounds of validation will be performed, leaving out M patients in each for validation data
    All values should be valid patient IDs, or negative. Negative values are ignored.

    Example 1: a leave-one-out cross validation with 3 patients would look like this:
    validation_schedule_patient = np.array([[0],[1],[2]])

    Example 2: a leave-two-out cross validation on 10 patients would look like this:
    validation_schedule_patient = np.array([[0,1],[2,3],[4,5],[6,7],[8,9]])

    Example 3: leave-one-out cross validation with 3 patients, then training on all available data (no validation):
    validation_schedule_patient = np.array([[0],[1],[2],[-1]])
    """
    # validation_schedule_patient = np.array([[-1]])
    validation_schedule_patient = np.array([[0]])

    # Define what data to download

    girder_url = "https://pocus.cs.queensu.ca/api/v1"
    girder_key = "nwv5qqqrDYn9DVakp1XnYDqjrNsowxaXisawPNRR"

    queens_sagittal_csv = "QueensSagittal.csv"
    verdure_axial_csv = "VerdureAxial.csv"
    verdure_sagittal_csv = "VerdureSagittal.csv"
    childrens_axial_csv = "Childrens1Axial.csv"

    queens_ultrasound_arrays, queens_segmentation_arrays = utils.load_girder_data(
        queens_sagittal_csv,
        data_arrays_fullpath,
        girder_url,
        girder_key=girder_key)

    verdure_axial_ultrasound_arrays, verdure_axial_segmentation_arrays = utils.load_girder_data(
        verdure_axial_csv,
        data_arrays_fullpath,
        girder_url,
        girder_key=girder_key)

    verdure_sagittal_ultrasound_arrays, verdure_sagittal_segmentation_arrays = utils.load_girder_data(
        verdure_sagittal_csv,
        data_arrays_fullpath,
        girder_url,
        girder_key=girder_key)

    childrens_ultrasound_arrays, childrens_segmentation_arrays = utils.load_girder_data(
        childrens_axial_csv,
        data_arrays_fullpath,
        girder_url,
        girder_key=girder_key)

    if not sagittal_only:
        ultrasound_arrays_by_patients = (queens_ultrasound_arrays +
                                         verdure_axial_ultrasound_arrays +
                                         verdure_sagittal_ultrasound_arrays +
                                         childrens_ultrasound_arrays)

        segmentation_arrays_by_patients = (
            queens_segmentation_arrays + verdure_axial_segmentation_arrays +
            verdure_sagittal_segmentation_arrays +
            childrens_segmentation_arrays)

    else:
        ultrasound_arrays_by_patients = (queens_ultrasound_arrays +
                                         verdure_sagittal_ultrasound_arrays)

        segmentation_arrays_by_patients = (
            queens_segmentation_arrays + verdure_sagittal_segmentation_arrays)

    if num_frames > 1:
        multiframe_ultrasound_arrays_by_patients = []

        for patient_series in ultrasound_arrays_by_patients:
            multiframe_patient_series = np.zeros((
                patient_series.shape[0],
                patient_series.shape[1],
                patient_series.shape[2],
                patient_series.shape[3] * num_frames,
            ))

            for frame_idx in range(len(patient_series)):
                multiframe_frame = np.zeros((
                    patient_series[frame_idx].shape[0],
                    patient_series[frame_idx].shape[1],
                    patient_series[frame_idx].shape[2] * num_frames,
                ))

                for frame in range(num_frames):
                    if frame_idx - frame >= 0:
                        multiframe_frame[:, :, frame] = np.squeeze(
                            patient_series[frame_idx - frame])
                    else:
                        multiframe_frame[:, :, frame] = np.zeros((
                            patient_series[frame_idx].shape[0],
                            patient_series[frame_idx].shape[1],
                        ))

                multiframe_patient_series[frame_idx] = multiframe_frame
            multiframe_ultrasound_arrays_by_patients.append(
                multiframe_patient_series)

        ultrasound_arrays_by_patients = multiframe_ultrasound_arrays_by_patients

    n_patients = len(ultrasound_arrays_by_patients)

    for i in range(n_patients):
        print("Patient {} has {} ultrasounds and {} segmentations".format(
            i,
            ultrasound_arrays_by_patients[i].shape[0],
            segmentation_arrays_by_patients[i].shape[0],
        ))

    # Prepare validation rounds

    if np.max(validation_schedule_patient) > (n_patients - 1):
        raise Exception(
            "Patient ID cannot be greater than {}".format(n_patients - 1))

    num_validation_rounds = len(validation_schedule_patient)
    print("Planning {} rounds of validation".format(num_validation_rounds))
    for i in range(num_validation_rounds):
        print("Validation on patients {} in round {}".format(
            validation_schedule_patient[i], i))

    # Print training parameters, to archive them together with the notebook output.

    time_sequence_start = datetime.datetime.now()

    print("Name for saved files: {}".format(run_str))
    print("\nTraining parameters")
    print("Number of epochs:    {}".format(num_epochs))
    print("LR:                  {}".format(learning_rate))
    print("LR decay:            {}".format(learning_rate_decay))
    print("Batch size:          {}".format(batch_size))
    print("Regularization rate: {}".format(regularization_rate))
    print("")
    print("Saving validation predictions in: {}".format(val_data_fullpath))
    print("Saving models in:                 {}".format(models_save_fullpath))

    # ROC data will be saved in these containers

    val_best_metrics = dict()
    val_fuzzy_metrics = dict()
    val_aurocs = np.zeros(num_validation_rounds)
    val_best_thresholds = np.zeros(num_validation_rounds)

    # Perform validation rounds

    for val_round_index in range(num_validation_rounds):

        # Prepare data arrays

        train_ultrasound_data = np.zeros([
            0,
            ultrasound_arrays_by_patients[0].shape[1],
            ultrasound_arrays_by_patients[0].shape[2],
            ultrasound_arrays_by_patients[0].shape[3],
        ])

        train_segmentation_data = np.zeros([
            0,
            segmentation_arrays_by_patients[0].shape[1],
            segmentation_arrays_by_patients[0].shape[2],
            segmentation_arrays_by_patients[0].shape[3],
        ])

        val_ultrasound_data = np.zeros([
            0,
            ultrasound_arrays_by_patients[0].shape[1],
            ultrasound_arrays_by_patients[0].shape[2],
            ultrasound_arrays_by_patients[0].shape[3],
        ])

        val_segmentation_data = np.zeros([
            0,
            segmentation_arrays_by_patients[0].shape[1],
            segmentation_arrays_by_patients[0].shape[2],
            segmentation_arrays_by_patients[0].shape[3],
        ])

        for patient_index in range(n_patients):
            if patient_index not in validation_schedule_patient[
                    val_round_index]:
                train_ultrasound_data = np.concatenate(
                    (train_ultrasound_data,
                     ultrasound_arrays_by_patients[patient_index]))
                train_segmentation_data = np.concatenate((
                    train_segmentation_data,
                    segmentation_arrays_by_patients[patient_index],
                ))
            else:
                val_ultrasound_data = np.concatenate(
                    (val_ultrasound_data,
                     ultrasound_arrays_by_patients[patient_index]))
                val_segmentation_data = np.concatenate(
                    (val_segmentation_data,
                     segmentation_arrays_by_patients[patient_index]))

        n_train = train_ultrasound_data.shape[0]
        n_val = val_ultrasound_data.shape[0]

        print("\n*** Leave-one-out round # {}".format(val_round_index))
        print("    Training on {} images, validating on {} images...".format(
            n_train, n_val))

        train_segmentation_data_onehot = tf.keras.utils.to_categorical(
            train_segmentation_data, num_classes)
        val_segmentation_data_onehot = tf.keras.utils.to_categorical(
            val_segmentation_data, num_classes)

        # Create and train model
        if not model_new:
            model = old_unet(ultrasound_size, num_classes, num_frames,
                             filter_multiplier, regularization_rate)
        else:
            model = new_unet(
                ultrasound_size,
                num_classes=num_classes,
                num_channels=num_frames,
                use_batch_norm=True,
                upsample_mode="deconv",  # 'deconv' or 'simple'
                dropout=0.0,
                dropout_type="spatial",
                use_attention=True,
                filters=16,
                num_layers=5,
                output_activation="softmax",
            )

        model.compile(
            optimizer=tf.keras.optimizers.Adam(lr=learning_rate,
                                               decay=learning_rate_decay),
            loss=loss_func,
            metrics=[
                "accuracy", evaluation_metrics.jaccard_coef,
                evaluation_metrics.dice_coef
            ],
        )

        dataset = tf.data.Dataset.from_tensor_slices(
            ((train_ultrasound_data, train_segmentation_data_onehot)))
        dataset = dataset.map(preprocess_func, num_parallel_calls=4)
        dataset = dataset.shuffle(buffer_size=1024).batch(batch_size)
        dataset = dataset.prefetch(1)

        val_dataset = tf.data.Dataset.from_tensor_slices(
            ((val_ultrasound_data, val_segmentation_data_onehot)))
        if with_maps:
            val_dataset = val_dataset.map(generate_weight_maps,
                                          num_parallel_calls=4)
        val_dataset = val_dataset.batch(batch_size)
        val_dataset = val_dataset.prefetch(1)
        '''
        fig = plt.figure(figsize=(30, 20 * 5))
        i = 0
        for im, lb in dataset:

            a1 = fig.add_subplot(25, 6, i * 6 + 1)
            img1 = a1.imshow(np.squeeze(np.flipud(im[i, :, :, 1])), cmap='gray')

            a2 = fig.add_subplot(25, 6, i * 6 + 2)
            img2 = a2.imshow(np.squeeze(np.flipud(im[i, :, :, 0])), cmap='gray')

            a3 = fig.add_subplot(25, 6, i * 6 + 3)
            img3 = a3.imshow(np.squeeze(np.flipud(lb[i, :, :, 0])))
            c = fig.colorbar(img3, fraction=0.046, pad=0.04)

            a4 = fig.add_subplot(25, 6, i * 6 + 4)
            img4 = a4.imshow(np.squeeze(np.flipud(lb[i, :, :, 1])))
            c = fig.colorbar(img4, fraction=0.046, pad=0.04)

            a5 = fig.add_subplot(25, 6, i * 6 + 5)
            img5 = a5.imshow(np.squeeze(np.flipud(lb[i, :, :, -2])))
            c = fig.colorbar(img5, fraction=0.046, pad=0.04)

            a6 = fig.add_subplot(25, 6, i * 6 + 6)
            img6 = a6.imshow(np.squeeze(np.flipud(lb[i, :, :, -1])))
            c = fig.colorbar(img6, fraction=0.046, pad=0.04)

            i += 1
            if i >= 20:
                break

        plt.savefig("us_seg.png")
        '''

        training_time_start = datetime.datetime.now()

        if n_val > 0:
            training_log = model.fit(
                dataset,
                validation_data=val_dataset,
                epochs=num_epochs,
                verbose=2,
            )

        else:
            training_log = model.fit(
                training_generator,
                epochs=num_epochs,
                verbose=2,
            )

        training_time_stop = datetime.datetime.now()

        # Print training log

        print("  Training time: {}".format(training_time_stop -
                                           training_time_start))

        # Plot training loss and metrics

        fig, axes = plt.subplots(nrows=1, ncols=2, figsize=(12, 4))

        axes[0].plot(training_log.history["loss"], "bo--")
        if n_val > 0:
            axes[0].plot(training_log.history["val_loss"], "ro-")
        axes[0].set(xlabel="Epochs (n)", ylabel="Loss")
        if n_val > 0:
            axes[0].legend(["Training loss", "Validation loss"])

        axes[1].plot(training_log.history["accuracy"], "bo--")
        if n_val > 0:
            axes[1].plot(training_log.history["val_accuracy"], "ro-")
        axes[1].set(xlabel="Epochs (n)", ylabel="Accuracy")
        if n_val > 0:
            axes[1].legend(["Training accuracy", "Validation accuracy"])

        fig.tight_layout()
        plt.savefig(run_str + "_val-round_" + str(val_round_index) + ".png")

        # Archive trained model with unique filename based on notebook name and timestamp

        model_file_name = run_str + "_model-" + str(val_round_index) + ".h5"
        model_fullname = os.path.join(models_save_fullpath, model_file_name)
        model.save(model_fullname)

        # Predict on validation data

        if n_val > 0:
            y_pred_val = model.predict(val_ultrasound_data)

            # Saving predictions for further evaluation

            val_prediction_filename = (run_str + "_prediction_" +
                                       str(val_round_index) + ".npy")
            val_prediction_fullname = os.path.join(val_data_fullpath,
                                                   val_prediction_filename)
            np.save(val_prediction_fullname, y_pred_val)

            # Validation results
            vali_metrics_dicts, vali_best_threshold_index, vali_area = evaluation_metrics.compute_roc(
                roc_thresholds, y_pred_val, val_segmentation_data,
                acceptable_margin_mm, mm_per_pixel)

            val_fuzzy_metrics[
                val_round_index] = evaluation_metrics.compute_evaluation_metrics(
                    y_pred_val, val_segmentation_data, acceptable_margin_mm,
                    mm_per_pixel)

            val_best_metrics[val_round_index] = vali_metrics_dicts[
                vali_best_threshold_index]
            val_aurocs[val_round_index] = vali_area
            val_best_thresholds[val_round_index] = roc_thresholds[
                vali_best_threshold_index]

        # Display sample results

        num_vali = val_ultrasound_data.shape[0]
        num_show = 10
        if num_vali < num_show:
            num_show = 0
        num_col = 4

        indices = [i for i in range(num_vali)]
        sample_indices = sample(indices, num_show)

        threshold = 0.5

        # Uncomment for comparing the same images
        # sample_indices = [105, 195, 391, 133, 142]

        fig = plt.figure(figsize=(18, num_show * 5))
        for i in range(num_show):
            a0 = fig.add_subplot(num_show, num_col, i * num_col + 1)
            img0 = a0.imshow(
                np.flipud(val_ultrasound_data[sample_indices[i], :, :,
                                              0].astype(np.float32)))
            a0.set_title("Ultrasound #{}".format(sample_indices[i]))
            a1 = fig.add_subplot(num_show, num_col, i * num_col + 2)
            img1 = a1.imshow(
                np.flipud(val_segmentation_data[sample_indices[i], :, :, 0]),
                vmin=0.0,
                vmax=1.0,
            )
            a1.set_title("Segmentation #{}".format(sample_indices[i]))
            c = fig.colorbar(img1, fraction=0.046, pad=0.04)
            a2 = fig.add_subplot(num_show, num_col, i * num_col + 3)
            img2 = a2.imshow(np.flipud(y_pred_val[sample_indices[i], :, :, 1]),
                             vmin=0.0,
                             vmax=1.0)
            a2.set_title("Prediction #{}".format(sample_indices[i]))
            c = fig.colorbar(img2, fraction=0.046, pad=0.04)
            a3 = fig.add_subplot(num_show, num_col, i * num_col + 4)
            img3 = a3.imshow(
                (np.flipud(y_pred_val[sample_indices[i], :, :, 1]) >
                 threshold),
                vmin=0.0,
                vmax=1.0,
            )
            c = fig.colorbar(img3, fraction=0.046, pad=0.04)
            a3.set_title("Thresholded #{}".format(sample_indices[i]))
        plt.savefig(run_str + "-samples-" + str(val_round_index) + ".png")

        # Printing total time of this validation round

        print("\nTotal round time:  {}".format(datetime.datetime.now() -
                                               training_time_start))
        print("")

    time_sequence_stop = datetime.datetime.now()

    print("\nTotal training time:   {}".format(time_sequence_stop -
                                               time_sequence_start))

    # Arrange results in tables

    metric_labels = [
        "AUROC",
        "best thresh",
        "best TP",
        "best FP",
        "best recall",
        "best precis",
        "fuzzy recall",
        "fuzzy precis",
        "fuzzy Fscore",
    ]

    results_labels = []

    for label in metric_labels:
        results_labels.append("Vali " + label)

    results_df = pd.DataFrame(columns=results_labels)

    for i in range(num_validation_rounds):
        if i in val_best_metrics.keys():
            results_df.loc[i] = [
                val_aurocs[i],
                val_best_thresholds[i],
                val_best_metrics[i][evaluation_metrics.TRUE_POSITIVE_RATE],
                val_best_metrics[i][evaluation_metrics.FALSE_POSITIVE_RATE],
                val_best_metrics[i][evaluation_metrics.RECALL],
                val_best_metrics[i][evaluation_metrics.PRECISION],
                val_fuzzy_metrics[i][evaluation_metrics.RECALL],
                val_fuzzy_metrics[i][evaluation_metrics.PRECISION],
                val_fuzzy_metrics[i][evaluation_metrics.FSCORE],
            ]

    # Save results table

    csv_filename = run_str + ".csv"
    csv_fullname = os.path.join(results_save_fullpath, csv_filename)
    results_df.to_csv(csv_fullname)

    print("Results saved to: {}".format(csv_fullname))
Ejemplo n.º 60
0
    def _transform_samples(self, batch_index, image_index):
        x_bounding_boxes = numpy.zeros((self.batch_size, 0, 4))

        x_categories = numpy.zeros((self.batch_size, 0, self.n_categories))

        x_images = numpy.zeros(
            (self.batch_size, *self.target_size, self.channels))

        x_masks = numpy.zeros((self.batch_size, 0, *self.mask_size))

        x_metadata = numpy.zeros((self.batch_size, 3))

        horizontal_flip = False

        if self.generator.horizontal_flip:
            if numpy.random.random() < 0.5:
                horizontal_flip = True

        vertical_flip = False

        if self.generator.vertical_flip:
            if numpy.random.random() < 0.5:
                vertical_flip = True

        try:
            pathname = self.dictionary[image_index]["image"]["pathname"]

        except:
            raise MissingImageException

        target_image = numpy.zeros((*self.target_size, self.channels))

        image = skimage.io.imread(pathname)

        if self.data_format == "channels_last":
            image = image[..., :self.channels]
        else:
            image = image[:self.channels, ...]

        dimensions = numpy.array([0, 0, image.shape[0], image.shape[1]])

        if self.generator.crop_size:
            if image.shape[0] > self.generator.crop_size[0] and image.shape[
                    1] > self.generator.crop_size[1]:
                image, dimensions = self._crop_image(image)

        dimensions = dimensions.astype(numpy.float16)

        scale = self.find_scale(image)

        dimensions *= scale

        image = skimage.transform.rescale(image,
                                          scale,
                                          anti_aliasing=True,
                                          mode="reflect",
                                          multichannel=True)

        image = self.generator.standardize(image)

        if horizontal_flip:
            image = numpy.fliplr(image)

        if vertical_flip:
            image = numpy.flipud(image)

        image_r = image.shape[0]
        image_c = image.shape[1]

        target_image[:image_r, :image_c] = image

        x_images[batch_index] = target_image

        x_metadata[batch_index] = [*self.target_size, 1.0]

        bounding_boxes = self.dictionary[image_index]["objects"]

        n_objects = len(bounding_boxes)

        if n_objects == 0:
            return [
                numpy.zeros((self.batch_size, 0, 4)),
                numpy.zeros((self.batch_size, 0, self.n_categories)), x_images,
                numpy.zeros((self.batch_size, 0, self.mask_size[0],
                             self.mask_size[1])), x_metadata
            ]

        x_bounding_boxes = numpy.resize(x_bounding_boxes,
                                        (self.batch_size, n_objects, 4))

        x_masks = numpy.resize(x_masks,
                               (self.batch_size, n_objects, *self.mask_size))

        x_categories = numpy.resize(
            x_categories, (self.batch_size, n_objects, self.n_categories))

        for bounding_box_index, bounding_box in enumerate(bounding_boxes):
            if bounding_box["category"] not in self.categories:
                continue

            minimum_r = bounding_box["bounding_box"]["minimum"]["r"]
            minimum_c = bounding_box["bounding_box"]["minimum"]["c"]

            maximum_r = bounding_box["bounding_box"]["maximum"]["r"]
            maximum_c = bounding_box["bounding_box"]["maximum"]["c"]

            minimum_r *= scale
            minimum_c *= scale

            maximum_r *= scale
            maximum_c *= scale

            minimum_r = int(minimum_r)
            minimum_c = int(minimum_c)

            maximum_r = int(maximum_r)
            maximum_c = int(maximum_c)

            target_bounding_box = [minimum_r, minimum_c, maximum_r, maximum_c]

            x_bounding_boxes[batch_index,
                             bounding_box_index] = target_bounding_box

            if "mask" in bounding_box:
                target_mask = skimage.io.imread(
                    bounding_box["mask"]["pathname"])

                target_mask = skimage.transform.rescale(target_mask,
                                                        scale,
                                                        anti_aliasing=True,
                                                        mode="reflect",
                                                        multichannel=False)

                target_mask = target_mask[minimum_r:maximum_r + 1,
                                          minimum_c:maximum_c + 1]

                target_mask = skimage.transform.resize(target_mask,
                                                       self.mask_size,
                                                       order=0,
                                                       mode="reflect",
                                                       anti_aliasing=True)

                x_masks[batch_index, bounding_box_index] = target_mask

            target_category = numpy.zeros(self.n_categories)

            target_category[self.categories[bounding_box["category"]]] = 1

            x_categories[batch_index, bounding_box_index] = target_category

        x = self._shuffle_objects(x_bounding_boxes, x_categories, x_masks)

        x_bounding_boxes, x_categories, x_masks = x

        x_bounding_boxes = self._crop_bounding_boxes(x_bounding_boxes,
                                                     dimensions)

        cropped = self._cropped_objects(x_bounding_boxes)

        x_bounding_boxes = x_bounding_boxes[:, ~cropped]

        x_categories = x_categories[:, ~cropped]

        x_masks = x_masks[:, ~cropped]

        for bounding_box_index, bounding_box in enumerate(x_bounding_boxes[0]):
            mask = x_masks[0, bounding_box_index]

            if horizontal_flip:
                bounding_box = [
                    bounding_box[0], image.shape[1] - bounding_box[3],
                    bounding_box[2], image.shape[1] - bounding_box[1]
                ]
                mask = numpy.fliplr(mask)

            if vertical_flip:
                bounding_box = [
                    image.shape[0] - bounding_box[2], bounding_box[1],
                    image.shape[0] - bounding_box[0], bounding_box[3]
                ]
                mask = numpy.flipud(mask)

            x_bounding_boxes[batch_index, bounding_box_index] = bounding_box

            x_masks[batch_index, bounding_box_index] = mask

        if self.generator.clear_border:
            indices = self._clear_border(x_bounding_boxes)

            x_bounding_boxes = x_bounding_boxes[:, indices]

            x_categories = x_categories[:, indices]

            x_masks = x_masks[:, indices]

        if x_bounding_boxes.shape == (self.batch_size, 0, 4):
            raise BoundingBoxException

        x_masks[x_masks > 0.5] = 1.0
        x_masks[x_masks < 0.5] = 0.0

        return [x_bounding_boxes, x_categories, x_images, x_masks, x_metadata]