Exemplo n.º 1
0
    def _subSampleByAveraging(self, var, timeVar, flagVar, samplingRate, flagsToUse):
        """
        Returns a new variable which is 'var' sub-sampled by averaging
        at the given samplingRate with data selected according to flagVar
        including the flag values specified in flagsToUse (defaults are 0 and 1).
        """
        maskedArray=self._getMaskedArray(var, flagVar, flagsToUse)
        shape=var.shape
        if shape[1]==1:
            newNumArray=MV.ravel(maskedArray)
            newArrayMask=MV.ravel(maskedArray.mask())
            newArray=MA.masked_array(newNumArray, mask=newArrayMask, fill_value=maskedArray.fill_value())
        else:
            newArray=Numeric.zeros(shape[0], 'f')
            for t0 in range(shape[0]):
                # Set as missing if less than half are valid                 
                t1Array=maskedArray[t0]

                if samplingRate==1:
                    # If half or more are good values then calculate the mean
                    if t1Array.count()>=(shape[1]/2.):
                        newArray[t0]=MA.average(t1Array)
                    # otherwise set as missing value
                    else:
                        newArray[t0]=maskedArray.fill_value()
        
                else:
                    raise "Averaging for non 1Hz sampling rates not yet supported!"

        # Now re-construct variable axes etc
        newTimeAxis=self._flatten2DTimeAxis(timeVar, samplingRate)
        newVar=self._recreateVariable(var, newArray, newTimeAxis, flagVar, max(flagsToUse), missingValue=maskedArray.fill_value(), sampleBy="averaging") 
        return newVar
Exemplo n.º 2
0
 def __init__ (self, 
               name: str, 
               pos_x: float, pos_y: float, pos_z: float, 
               i_r:float, i_g: float, i_b: float
             ):
     self.name = name
     self.pos = MV.vec3(pos_x, pos_y, pos_z)
     self.intensity = MV.vec3(i_r, i_g, i_b)
Exemplo n.º 3
0
    def _getMaskedArray(self, var, flagVar=None, flagValues=(0, 1), missingValuesToTest=(-9999.0, -32767.0)):
        """
        Returns a masked array that has the values only present where the flag
        values are acceptable and the data itself is not missing.
        """
        if flagVar:
            flagFillValue = -1
            flagGTValue = max(flagValues)

            flagMaskWhereUnacceptable = MA.masked_greater(flagVar, flagGTValue).mask()
            flagMaskWhereMissing = MA.masked_equal(flagVar, flagFillValue).mask()
            flagMask = flagMaskWhereUnacceptable + flagMaskWhereMissing

        for fv in missingValuesToTest:
            if fv in MV.ravel(var):
                print "Setting missing value for '%s' as: %s" % (var.id, fv)
                varFillValue = fv
        else:
            varFillValue = missingValuesToTest[0]

        if flagVar:
            varMask = MA.masked_array(var, mask=MA.equal(var, varFillValue), fill_value=varFillValue).mask()
            fullmask = MA.bitwise_or(varMask, flagMask)
            maskedArray = MA.masked_array(var, mask=fullmask, fill_value=varFillValue)
        else:
            maskedArray = MA.masked_array(var, mask=MA.equal(var, varFillValue), fill_value=varFillValue)

        # maskedArray=cdms.createVariable(maskedArray, id=var.id, fill_value=varFillValue)
        # maskedArray.missing_value=varFillValue
        return maskedArray
Exemplo n.º 4
0
    def _getMaskedArray(self, var, flagVar=None, flagValues=(0,1), missingValuesToTest=(-9999., -32767.)):
        """
        Returns a masked array that has the values only present where the flag
        values are acceptable and the data itself is not missing.
        """
        if flagVar:
            flagFillValue=-1
            flagGTValue=max(flagValues)

            flagMaskWhereUnacceptable=MA.masked_greater(flagVar, flagGTValue).mask()
            flagMaskWhereMissing=MA.masked_equal(flagVar, flagFillValue).mask()
            flagMask=flagMaskWhereUnacceptable+flagMaskWhereMissing

        for fv in missingValuesToTest:
            if fv in MV.ravel(var):
                print "Setting missing value for '%s' as: %s" % (var.id, fv)
                varFillValue=fv
        else:
            varFillValue=missingValuesToTest[0]

        if flagVar:
            varMask=MA.masked_array(var, mask=MA.equal(var, varFillValue), fill_value=varFillValue).mask()
            fullmask=MA.bitwise_or(varMask, flagMask)
            maskedArray=MA.masked_array(var, mask=fullmask, fill_value=varFillValue)
        else:
            maskedArray=MA.masked_array(var, mask=MA.equal(var, varFillValue), fill_value=varFillValue)

        #maskedArray=cdms.createVariable(maskedArray, id=var.id, fill_value=varFillValue)
        #maskedArray.missing_value=varFillValue
        return maskedArray 
Exemplo n.º 5
0
def flatten2DTimeData(var, time_var):
    """
    Returns a flattened 2D array variable with a recalculated time axies.
    """
    data = MV.ravel(var)
    missing = var.getMissing()
    time_values = time_var._data
    time_unit = time_var.units
    sampling_rate = var.getAxis(1)
    rate = int(sampling_rate.id[3:])
    newtime_values = []

    for t in time_values:
        for i in range(rate):
            tvalue = t + ((1./rate)*i)
            newtime_values.append(tvalue)

    new_time_ax = cdms.createAxis(newtime_values)
    new_time_ax.units = time_unit
    new_time_ax.designateTime()
    new_time_ax.id = new_time_ax.long_name = new_time_ax.standard_name = "time"

    atts = var.attributes
    newAtts = {}
    for att,value in atts.items():
        if type(value) in (type((1,1)), type([1,2]), type(Numeric.array([1.]))) and len(value) == 1:
            value = value[0]
        if type(value) in (type(1), type(1.), type(long(1))):
            newAtts[att] = value
        elif type(value) == type("string"):
            newAtts[att] = value.strip()

    # Now create the variable
    newvar = cdms.createVariable(data, id=var.id, fill_value=missing, axes=[new_time_ax], attributes=newAtts)
    return newvar
Exemplo n.º 6
0
    def intersectRay (self, ray: Ray, isEyeRay:bool=False) -> np.ndarray:
        rayLength = MV.dot(ray.direction, ray.direction) ** 0.5
        ray.direction = MV.normalize(ray.direction)

        rayOrigin_SphereCoord = MV.transformPoint(self.inverseMat, ray.origin)
        rayDir_SphereCoord = MV.transformVector(self.inverseMat, ray.direction)
        a = MV.dot(rayDir_SphereCoord, rayDir_SphereCoord)
        b = MV.dot(rayDir_SphereCoord, rayOrigin_SphereCoord)
        c = MV.dot(rayOrigin_SphereCoord, rayOrigin_SphereCoord) - 1**2
        
        d = b * b - a * c
        
        if d < 0:
            return np.inf
        if d >= 0:
            t1 = (-b + np.sqrt(d)) / a
            t2 = (-b - np.sqrt(d)) / a
            if (isEyeRay):
                tMin = min (t1, t2)
                tMax = max (t1, t2)
                if (tMin > rayLength):
                    return tMin
                elif (tMax > rayLength):
                    return tMax
                else:
                    return np.inf
            answer = min(t1, t2)
            if answer < 0:
                return np.inf
            else:
                return answer
Exemplo n.º 7
0
    def _subSampleByAveraging(self, var, timeVar, flagVar, samplingRate, flagsToUse):
        """
        Returns a new variable which is 'var' sub-sampled by averaging
        at the given samplingRate with data selected according to flagVar
        including the flag values specified in flagsToUse (defaults are 0 and 1).
        """
        maskedArray = self._getMaskedArray(var, flagVar, flagsToUse)
        shape = var.shape
        if shape[1] == 1:
            newNumArray = MV.ravel(maskedArray)
            newArrayMask = MV.ravel(maskedArray.mask())
            newArray = MA.masked_array(newNumArray, mask=newArrayMask, fill_value=maskedArray.fill_value())
        else:
            newArray = Numeric.zeros(shape[0], "f")
            for t0 in range(shape[0]):
                # Set as missing if less than half are valid
                t1Array = maskedArray[t0]

                if samplingRate == 1:
                    # If half or more are good values then calculate the mean
                    if t1Array.count() >= (shape[1] / 2.0):
                        newArray[t0] = MA.average(t1Array)
                    # otherwise set as missing value
                    else:
                        newArray[t0] = maskedArray.fill_value()

                else:
                    raise "Averaging for non 1Hz sampling rates not yet supported!"

        # Now re-construct variable axes etc
        newTimeAxis = self._flatten2DTimeAxis(timeVar, samplingRate)
        newVar = self._recreateVariable(
            var,
            newArray,
            newTimeAxis,
            flagVar,
            max(flagsToUse),
            missingValue=maskedArray.fill_value(),
            sampleBy="averaging",
        )
        return newVar
Exemplo n.º 8
0
def compVarSlice(fileA, fileB, var, dim, tol=0.0, start=0, end=0):
    '''Compare a slice (of given dimension) through named variable'''

    # open files
    fpA = cdms.open(fileA)
    fpB = cdms.open(fileB)

    # check named variable present in both files
    varsA = Set(fpA.listvariables())
    varsB = Set(fpB.listvariables())
    commonVars = varsA & varsB
    if var not in commonVars:
        fpA.close()
        fpB.close()
        return (FALSE, var + ' not common', varsA, varsB)

    # ditto for named dimension
    dimsA = Set(fpA.listdimension())
    dimsB = Set(fpB.listdimension())
    commonDims = dimsA & dimsB
    if dim not in commonDims:
        fpA.close()
        fpB.close()
        return (FALSE, dim + ' not common', dimsA, dimsB)

    # get the slices
    sliceA = eval(r"fpA('" + var + "'," + dim + "=slice(" + str(start) + "," +
                  str(end) + "))")
    sliceB = eval(r"fpB('" + var + "'," + dim + "=slice(" + str(start) + "," +
                  str(end) + "))")

    # close files
    fpA.close()
    fpB.close()

    # ensure dimensions of slices correct
    if sliceA.shape != sliceB.shape:
        return (FALSE, 'different shapes', sliceA.shape, sliceB.shape)
    if sliceA.shape[0] != end - start:
        return (FALSE, 'slice size wrong', str(sliceA.shape[0]),
                str(end - start))
    if sliceA.shape[0] == 0:
        return (FALSE, 'slice size zero', str(sliceA.shape[0]),
                str(end - start))

    # make actual comparison
    maxDelta = MV.maximum(abs(sliceA - sliceB))
    if maxDelta > tol:
        return (FALSE, 'max diff > ' + str(tol), '', '')
    else:
        return (TRUE, '', '', '')
Exemplo n.º 9
0
def compVarSlice(fileA, fileB, var, dim, tol=0.0, start=0, end=0):
    '''Compare a slice (of given dimension) through named variable'''

    # open files
    fpA = cdms.open(fileA)
    fpB = cdms.open(fileB)
    
    # check named variable present in both files
    varsA = Set(fpA.listvariables())
    varsB = Set(fpB.listvariables())
    commonVars = varsA & varsB
    if var not in commonVars:
        fpA.close()
        fpB.close()
        return (FALSE,var+' not common',varsA,varsB)

    # ditto for named dimension
    dimsA = Set(fpA.listdimension())
    dimsB = Set(fpB.listdimension())
    commonDims = dimsA & dimsB
    if dim not in commonDims:
        fpA.close()
        fpB.close()
        return (FALSE,dim+' not common',dimsA,dimsB)

    # get the slices
    sliceA = eval(r"fpA('"+var+"',"+dim+"=slice("+str(start)+","+str(end)+"))")
    sliceB = eval(r"fpB('"+var+"',"+dim+"=slice("+str(start)+","+str(end)+"))")

    # close files
    fpA.close()
    fpB.close()

    # ensure dimensions of slices correct
    if sliceA.shape != sliceB.shape:
        return (FALSE,'different shapes',sliceA.shape,sliceB.shape)
    if sliceA.shape[0] != end - start:
        return (FALSE,'slice size wrong',str(sliceA.shape[0]),str(end-start))
    if sliceA.shape[0] == 0:
        return (FALSE,'slice size zero',str(sliceA.shape[0]),str(end-start))

    # make actual comparison
    maxDelta = MV.maximum(abs(sliceA - sliceB))
    if maxDelta > tol:
        return (FALSE,'max diff > '+str(tol),'','')
    else:
        return (TRUE,'','','')
Exemplo n.º 10
0
def flatten2DTimeData(var, time_var):
    """
    Returns a flattened 2D array variable with a recalculated time axies.
    """
    data = MV.ravel(var)
    missing = var.getMissing()
    time_values = time_var._data
    time_unit = time_var.units
    sampling_rate = var.getAxis(1)
    rate = int(sampling_rate.id[3:])
    newtime_values = []

    for t in time_values:
        for i in range(rate):
            tvalue = t + ((1. / rate) * i)
            newtime_values.append(tvalue)

    new_time_ax = cdms.createAxis(newtime_values)
    new_time_ax.units = time_unit
    new_time_ax.designateTime()
    new_time_ax.id = new_time_ax.long_name = new_time_ax.standard_name = "time"

    atts = var.attributes
    newAtts = {}
    for att, value in atts.items():
        if type(value) in (type((1, 1)), type([1, 2]), type(Numeric.array(
            [1.]))) and len(value) == 1:
            value = value[0]
        if type(value) in (type(1), type(1.), type(long(1))):
            newAtts[att] = value
        elif type(value) == type("string"):
            newAtts[att] = value.strip()

    # Now create the variable
    newvar = cdms.createVariable(data,
                                 id=var.id,
                                 fill_value=missing,
                                 axes=[new_time_ax],
                                 attributes=newAtts)
    return newvar
Exemplo n.º 11
0
    # Now re-mask if necessary
    if real_mask:
        array = MA.masked_array(array, mask=real_mask, fill_value=cdms_var.missing_value)
        print "With mask back on:", array

    # Now re-make into variable
    new_var = cdms.createVariable(array, axes=cdms_var.getAxisList(),
                   id=cdms_var.id, attributes=cdms_var.attributes)

    return new_var


if __name__ == "__main__":

    print "Running test for bringWithinBounds function:"
    x = N.array([0,1,2,3,4,999])
    print "x is:", x
    print "we want to treat 999 as missing_value and then mask 0 to lower_bound of 1"
    import MV
    ma = MV.masked_array(x, mask=[0,0,0,0,0,1], fill_value=999)
    ax = cdms.createAxis([3,5,7,9,11,13])
    ax.id = ax.units = "super_axis"
    v = cdms.createVariable(ma, id="myvar", axes=[ax],
                            mask=ma.mask(), fill_value=ma.fill_value(), attributes={"cabbage":1})
    print "V:", v, type(v), v.shape, v.attributes
    lb = 1
    ub = 3
    nv = restrictToBounds(v, lower_bound=lb, upper_bound=ub, set_as="bound")
    print nv
Exemplo n.º 12
0
def simpleComp(fileA, fileB, tol=0.0):
    '''A simple comparison function.

       Attribute names and values are compared first.
       Then the data arrays are compared within a
       tolerance on a cell by cell basis.'''
    
    # open files
    fpA = cdms.open(fileA)
    fpB = cdms.open(fileB)

    # == global attributes ==
    # compare attribute names
    message = 'global attributes: '
    globalNamesA = Set(fpA.listglobal())
    globalNamesB = Set(fpB.listglobal())
    symmetricDiff = globalNamesA ^ globalNamesB
    if len(symmetricDiff) != 0:
        (detailA, detailB) = setDiff(globalNamesA,globalNamesB,symmetricDiff)
        return (FALSE,message,detailA,detailB)
    # compare values
    for globalName in globalNamesA:
        # limit our checks to attributes with string values
        if isinstance(eval(r'fpA.'+globalName),types.StringType):
            globalValueA = eval(r'fpA.'+globalName)
            globalValueB = eval(r'fpB.'+globalName)
            if globalValueA != globalValueB:
                message += globalName + ' values'
                return(FALSE,message,globalValueA,globalValueB)

    # == dimensions ==
    # compare dimension names
    dimNamesA = Set(fpA.listdimension())
    dimNamesB = Set(fpB.listdimension())
    symmetricDiff = dimNamesA ^ dimNamesB
    if len(symmetricDiff) != 0:
        message = 'dimensions:'
        (detailA, detailB) = setDiff(dimNamesA,dimNamesB,symmetricDiff)
        return (FALSE,message,detailA,detailB)
    # loop over dimensions
    for dimName in dimNamesA:
        message = 'dimensions: '+ dimName
        # compare attribute names
        dimAttNamesA = Set(fpA[dimName].attributes.keys())
        dimAttNamesB = Set(fpA[dimName].attributes.keys())
        symmetricDiff = dimAttNamesA ^ dimAttNamesB
        if len(symmetricDiff) != 0:
            (detailA, detailB) = setDiff(dimAttNamesA,dimAttNamesB,symmetricDiff)
            return (FALSE,message,detailA,detailB)
        # compare attribute values
        for dimAttName in dimAttNamesA:
            # assuming objects we can compare
            dimAttValueA = eval(r"fpA['"+dimName+r"']."+dimAttName)
            dimAttValueB = eval(r"fpB['"+dimName+r"']."+dimAttName)
            if dimAttValueA != dimAttValueB:
                message += ': '+dimAttName
                return (FALSE,message,dimAttValueA,dimAttValueB)
        # compare data
        dimDataShapeA = MV.shape(fpA[dimName])
        dimDataShapeB = MV.shape(fpB[dimName])
        if dimDataShapeA != dimDataShapeB:
            message += ': data array shape'
            return (FALSE,message,str(dimDataShapeA),str(dimDataShapeB))
        maxDelta = MV.maximum(abs(fpA[dimName][:] - fpB[dimName][:]))
        if maxDelta > tol:
            message += ': delta: '+str(maxDelta)+' > '+str(tol)
            return (FALSE,message,'','')

    # == variables ==
    # compare variable names
    varNamesA = Set(fpA.listvariables())
    varNamesB = Set(fpB.listvariables())
    symmetricDiff = varNamesA ^ varNamesB
    if len(symmetricDiff) != 0:
        message = 'variables:'
        (detailA, detailB) = setDiff(varNamesA,varNamesB,symmetricDiff)
        return (FALSE,message,detailA,detailB)
    # loop over variables
    for varName in varNamesA:
        message = 'variables: '+varName
        # compare attribute names
        varAttNamesA = Set(fpA[varName].attributes.keys())
        varAttNamesB = Set(fpA[varName].attributes.keys())
        symmetricDiff = varAttNamesA ^ varAttNamesB
        if len(symmetricDiff) != 0:
            (detailA, detailB) = setDiff(varAttNamesA,varAttNamesB,symmetricDiff)
            return (FALSE,message,detailA,detailB)
        # compare attribute values
        for varAttName in varAttNamesA:
            # assuming objects we can compare
            varAttValueA = eval(r"fpA['"+varName+r"']."+varAttName)
            varAttValueB = eval(r"fpB['"+varName+r"']."+varAttName)
            if varAttValueA != varAttValueB:
                message += ': '+varAttName
                return (FALSE,message,varAttValueA,varAttValueB)
        # compare data
        varDataShapeA = MV.shape(fpA[varName])
        varDataShapeB = MV.shape(fpB[varName])
        if varDataShapeA != varDataShapeB:
            message += ': data array shape'
            return (FALSE,message,str(varDataShapeA),str(varDataShapeB))
        maxDelta = MV.maximum(abs(fpA[varName][:] - fpB[varName][:]))
        if maxDelta > tol:
            message += ': delta: '+str(maxDelta)+' > '+str(tol)
            return (FALSE,message,'','')

    # close files
    fpA.close()
    fpB.close()
    return (TRUE,'','','')
Exemplo n.º 13
0
ac1=cdutil.ANNUALCYCLE.climatology(data1(time=(start_time, end_time, 'cob')))
ac2=cdutil.ANNUALCYCLE.climatology(data2(time=(start_time, end_time, 'cob')))
print ac1

data1=cdutil.ANNUALCYCLE.departures(data1,ref=ac1)
data2=cdutil.ANNUALCYCLE.departures(data2,ref=ac2)

print data1.shape,data2.shape

tim = data2.getTime()
lat=data2.getLatitude()
lon=data2.getLongitude()

data1=cdms.createVariable(data1,axes=[tim,lat,lon],typecode='f',id='tas')

diff=MV.subtract(data1,data2)
# zonal differences
z_diff=MV.average(diff,2)
print 'Zonal data shape (before): ',z_diff.shape

z_diff=MV.transpose(z_diff,(1,0))

# add id to data
z_diff.id='zonal_diff'
print 'Zonal data shape (after): ',z_diff.shape

# global differences
gl_diff=cdutil.averager(diff,axis='xy')

x=vcs.init()
x.setcolormap('default')
Exemplo n.º 14
0
def simpleComp(fileA, fileB, tol=0.0):
    '''A simple comparison function.

       Attribute names and values are compared first.
       Then the data arrays are compared within a
       tolerance on a cell by cell basis.'''

    # open files
    fpA = cdms.open(fileA)
    fpB = cdms.open(fileB)

    # == global attributes ==
    # compare attribute names
    message = 'global attributes: '
    globalNamesA = Set(fpA.listglobal())
    globalNamesB = Set(fpB.listglobal())
    symmetricDiff = globalNamesA ^ globalNamesB
    if len(symmetricDiff) != 0:
        (detailA, detailB) = setDiff(globalNamesA, globalNamesB, symmetricDiff)
        return (FALSE, message, detailA, detailB)
    # compare values
    for globalName in globalNamesA:
        # limit our checks to attributes with string values
        if isinstance(eval(r'fpA.' + globalName), types.StringType):
            globalValueA = eval(r'fpA.' + globalName)
            globalValueB = eval(r'fpB.' + globalName)
            if globalValueA != globalValueB:
                message += globalName + ' values'
                return (FALSE, message, globalValueA, globalValueB)

    # == dimensions ==
    # compare dimension names
    dimNamesA = Set(fpA.listdimension())
    dimNamesB = Set(fpB.listdimension())
    symmetricDiff = dimNamesA ^ dimNamesB
    if len(symmetricDiff) != 0:
        message = 'dimensions:'
        (detailA, detailB) = setDiff(dimNamesA, dimNamesB, symmetricDiff)
        return (FALSE, message, detailA, detailB)
    # loop over dimensions
    for dimName in dimNamesA:
        message = 'dimensions: ' + dimName
        # compare attribute names
        dimAttNamesA = Set(fpA[dimName].attributes.keys())
        dimAttNamesB = Set(fpA[dimName].attributes.keys())
        symmetricDiff = dimAttNamesA ^ dimAttNamesB
        if len(symmetricDiff) != 0:
            (detailA, detailB) = setDiff(dimAttNamesA, dimAttNamesB,
                                         symmetricDiff)
            return (FALSE, message, detailA, detailB)
        # compare attribute values
        for dimAttName in dimAttNamesA:
            # assuming objects we can compare
            dimAttValueA = eval(r"fpA['" + dimName + r"']." + dimAttName)
            dimAttValueB = eval(r"fpB['" + dimName + r"']." + dimAttName)
            if dimAttValueA != dimAttValueB:
                message += ': ' + dimAttName
                return (FALSE, message, dimAttValueA, dimAttValueB)
        # compare data
        dimDataShapeA = MV.shape(fpA[dimName])
        dimDataShapeB = MV.shape(fpB[dimName])
        if dimDataShapeA != dimDataShapeB:
            message += ': data array shape'
            return (FALSE, message, str(dimDataShapeA), str(dimDataShapeB))
        maxDelta = MV.maximum(abs(fpA[dimName][:] - fpB[dimName][:]))
        if maxDelta > tol:
            message += ': delta: ' + str(maxDelta) + ' > ' + str(tol)
            return (FALSE, message, '', '')

    # == variables ==
    # compare variable names
    varNamesA = Set(fpA.listvariables())
    varNamesB = Set(fpB.listvariables())
    symmetricDiff = varNamesA ^ varNamesB
    if len(symmetricDiff) != 0:
        message = 'variables:'
        (detailA, detailB) = setDiff(varNamesA, varNamesB, symmetricDiff)
        return (FALSE, message, detailA, detailB)
    # loop over variables
    for varName in varNamesA:
        message = 'variables: ' + varName
        # compare attribute names
        varAttNamesA = Set(fpA[varName].attributes.keys())
        varAttNamesB = Set(fpA[varName].attributes.keys())
        symmetricDiff = varAttNamesA ^ varAttNamesB
        if len(symmetricDiff) != 0:
            (detailA, detailB) = setDiff(varAttNamesA, varAttNamesB,
                                         symmetricDiff)
            return (FALSE, message, detailA, detailB)
        # compare attribute values
        for varAttName in varAttNamesA:
            # assuming objects we can compare
            varAttValueA = eval(r"fpA['" + varName + r"']." + varAttName)
            varAttValueB = eval(r"fpB['" + varName + r"']." + varAttName)
            if varAttValueA != varAttValueB:
                message += ': ' + varAttName
                return (FALSE, message, varAttValueA, varAttValueB)
        # compare data
        varDataShapeA = MV.shape(fpA[varName])
        varDataShapeB = MV.shape(fpB[varName])
        if varDataShapeA != varDataShapeB:
            message += ': data array shape'
            return (FALSE, message, str(varDataShapeA), str(varDataShapeB))
        maxDelta = MV.maximum(abs(fpA[varName][:] - fpB[varName][:]))
        if maxDelta > tol:
            message += ': delta: ' + str(maxDelta) + ' > ' + str(tol)
            return (FALSE, message, '', '')

    # close files
    fpA.close()
    fpB.close()
    return (TRUE, '', '', '')
Exemplo n.º 15
0
  parser.add_argument('-n', action='store_true', help='Runs the naive algorithm')
  parser.add_argument('-m', action='store_true', help='Computes Majority Vote')
  args = parser.parse_args()

  alphabet = "a b c"
  types = alphabet.split()
  numCharacters = len(types)

  accuracies = []
  cross_entropies = []

  for sim in range(100): # Run 100 simulations
    data = init_from_file("Tests/RareClass/data/%d.txt" % sim, 0, not args.r, True)

    if args.m:
      acc = MV(data)
      print "Simulation %d: %.2f" % (sim, acc)

    elif args.n:
      acc = naive(data)
      print "Simulation %d: %.2f" % (sim, acc)

    else:
      EM(data)
      acc = data.best_percent_correct()
      ce = data.best_cross_entropy()
      print "Simulation %d: %.2f % | %.2f CE" % (sim, acc, ce)
      cross_entropies.append(ce)

    accuracies.append(acc)
Exemplo n.º 16
0
cdms.axis.latitude_aliases.append('Y')
cdms.axis.longitude_aliases.append('X')
cdms.axis.time_aliases.append('T')

f = cdms.open('../example/data2.cdf')

s2 = f('ssta', latitude=(-10, 10), longitude=(110, 180))
s1 = f('ssta', latitude=(-15, 15), longitude=(210, 250))

print 'Data  in:', s1.shape, s2.shape

res = spanlib.stackData(s1, s2)

print res[0].shape

SP = spanlib.SpAn(MV.array(res[0]), weights=MV.array(res[1]))
eof, pc, ev = SP.pca()

## for ax in res[3]:
##     ax[0]=eof.getAxis(0)

## res2 = spanlib.unStackData(eof,res[1],res[2],res[3])

res3 = steof, stpc, stev = SP.mssa(pca=True)

ffrec = SP.reconstruct()
res4 = spanlib.unStackData(ffrec, res[1], res[2], res[3])

print ffrec

import vcs
Exemplo n.º 17
0
 def isInside (self, point: np.ndarray) -> bool:
     point_SphereCoord = MV.transformPoint(self.inverseMat, point)
     return (MV.dot(point_SphereCoord, point_SphereCoord) <= 1)
Exemplo n.º 18
0
 def getInverseMat (self) -> np.ndarray:
     return (MV.mult(MV.inverseScale(self.scale[0], self.scale[1], self.scale[2]), 
                     MV.inverseTranslate(self.pos[0], self.pos[1], self.pos[2])))
Exemplo n.º 19
0
 def getTransformationMatrix (self) -> np.ndarray:
     scaleMat = MV.scale(self.scale[0], self.scale[1], self.scale[2])
     translateMat = MV.translate(self.pos[0], self.pos[1], self.pos[2])
     # translate then scale
     return MV.mult(scaleMat, translateMat)
Exemplo n.º 20
0
def rayTracer (ray: Ray, scene: Scene):
    # find the intersection of the ray with all the object
    t = np.inf
    for i, obj in enumerate(scene.objects):
        # if the ray is eye-ray, check whether t < 1, 
        # if yes, omit it.
        if (np.array_equal(ray.origin, scene.camPoint)):
            tObj = obj.intersectRay(ray, True)
        else:
            tObj = obj.intersectRay(ray)
        if tObj < t:
            t, obj_idx = tObj, i
    # if there is not any intersection, return
    if t == np.inf:
        return
    
    obj = scene.objects[obj_idx]

    # converse the intersection point to Sphere Coordinate Sytem
    # then calculate the normal vector at the intersection point
    # finally converse it back to the World Coordinate System
    P = ray.origin + ray.direction * t
    P_SphereCoord = MV.transformPoint(obj.inverseMat, P)

    N_SphereCoord = P_SphereCoord - np.zeros(3) # sphere origin is (0, 0, 0) in sphere coordinate system

    # N = P - obj.pos
    # N_SphereCoord = MV.transformVector(obj.inverseMat, N)

    N = MV.transformVector(obj.transposeInverseMat, N_SphereCoord)
    N = MV.normalize(N)

    V = MV.normalize(-P)
    color = obj.color.copy()

    '''
    PIXEL_COLOR[c] = Ka*Ia[c]*O[c] +

    for each point light (p) { Kd*Ip[c]*(N dot L)*O[c]+Ks*Ip[c]*(R dot V)n } 
    
    + Kr*(Color returned from reflection ray)

    O is the object color (<r> <g> <b>)
    '''
    Ka = scene.ambient * obj.k_a 
    Kd = np.zeros(3)
    Ks = np.zeros(3)
    for light in scene.lightSources:
        L = MV.normalize(light.pos - P)
        # Shadow: find if the point is shadowed or not.
        ray = Ray(P + N * .000001, L)
        l = [sphere.intersectRay(ray) 
        for k, sphere in enumerate(scene.objects) if k != obj_idx]
        if l and min(l) < np.inf:
            continue
        if (obj.isInside(scene.atVector)):
            if (obj.isInside(light.pos)):
                N = -N
            else:
                continue
        
        R = MV.normalize(2 * MV.dot(N, L) * N - L)

        # Lambert shading (diffuse).
        Kd += obj.k_d * max(MV.dot(N, L), 0.)  *  ( light.intensity )
        # Blinn-Phong shading (specular)
        Ks += obj.k_s * ( max(np.dot(R, V), 0.) ** obj.specular ) * ( light.intensity )
    pixelColor = Ka * color + Kd * color + Ks
    return obj, P + N * .000001, N, pixelColor 
Exemplo n.º 21
0
cdms.axis.longitude_aliases.append("X")
cdms.axis.time_aliases.append("T")

f = cdms.open("../example/data2.cdf")

s2 = f("ssta", latitude=(-10, 10), longitude=(110, 180))
s1 = f("ssta", latitude=(-15, 15), longitude=(210, 250))


print "Data  in:", s1.shape, s2.shape

res = spanlib.stackData(s1, s2)

print res[0].shape

SP = spanlib.SpAn(MV.array(res[0]), weights=MV.array(res[1]))
eof, pc, ev = SP.pca()


## for ax in res[3]:
##     ax[0]=eof.getAxis(0)

## res2 = spanlib.unStackData(eof,res[1],res[2],res[3])

res3 = steof, stpc, stev = SP.mssa(pca=True)

ffrec = SP.reconstruct()
res4 = spanlib.unStackData(ffrec, res[1], res[2], res[3])


print ffrec
Exemplo n.º 22
0
     accurate3(truth, day, time, rate)
     print('acc计算完毕')
 elif (run_name == "CATD"):
     #CATD
     print("%s Day %d:" % (run_name, day))
     e2wl, w2el, label_set = CATD.gete2wlandw2el("./data/"+now_work+"/%s/answers/%d/answer_%d.csv" % (rate, time, day))
     print('读入数据完毕')
     truth, weight = CATD.Conf_Aware(e2wl, w2el, 'categorical').Run(0.05,100)
     print('EM完毕')
     accurate3(truth, day, time, rate)
     #accurate2(e2wl, w2el, label_set, truth, day, time, rate)
     print('acc计算完毕')
 elif (run_name == "MV"):
     #MV
     print("%s Day %d:" % (run_name, day))
     e2wl, w2el, label_set = MV.gete2wlandw2el("./data/"+now_work+"/%s/answers/%d/answer_%d.csv" % (rate, time, day))
     print('读入数据完毕')
     e2lpd = MV.MV(e2wl, w2el, label_set).Run()
     print('EM完毕')
     truth = get_truth(e2lpd)
     print('get真相完毕')
     accurate3(truth, day, time, rate)
     #accurate2(e2wl, w2el, label_set, truth, day, time, rate)
     print('acc计算完毕')
 elif (run_name == "PM"):
     # PM
     print("%s Day %d:" % (run_name, day))
     e2wl, w2el, label_set = PM.gete2wlandw2el("./data/"+now_work+"/%s/answers/%d/answer_%d.csv" % (rate, time, day))
     print('读入数据完毕')
     e2lpd, weight = PM.CRH(e2wl, w2el, label_set, 'categorical', r'0/1 loss').Run(10)
     print('EM完毕')
Exemplo n.º 23
0
# We tell cdms that we have longitude, latitude and time
cdms.axis.latitude_aliases.append('Y')
cdms.axis.longitude_aliases.append('X')
cdms.axis.time_aliases.append('T')

# Simply open the netcdf file
print "Open file"
f=cdms.open('data2.cdf')

# Get our two datasets
print "Read two different regions"
s2=f('ssta',latitude=(-10,10),longitude=(110,180))
s1=f('ssta',latitude=(-15,15),longitude=(210,250))
s1[:,0:10,0:4]=MV.masked
s2[:,0:5,0:6]=MV.masked
print MV.average(s1.flat),MV.average(s2.flat)

# Stack the two dataset to have only one dataset
print "Stacking data"
res = spanlib.stackData(s1,s2)

# Create the analysis object
print "Creating SpAn object"
SP=spanlib.SpAn(MV.array(res[0]),weights=MV.array(res[1]))

# Perform a preliminary PCA
# (optional step since done by default with mssa)
print "PCA+MSSA..."
steof,stpc,stev = SP.mssa(pca=True)

# Recontructed the filtered field
Exemplo n.º 24
0
'''
parser = argparse.ArgumentParser()
parser.add_argument('infile', type=str, help="input file")

args = parser.parse_args()
if not args.infile:
    print("Need an infile...")
    exit()

lights, spheres, backgroundColor, ambient, near, left, right, bottom, top, res, output_filename = parse_data(args.infile)

h = res[0]
w = res[1]
img = np.full((h, w, 3), backgroundColor)

atVector = MV.vec3((right+left)/2, (top+bottom)/2 , -near) 
camPoint = MV.vec3(0., 0., 0.) 

MAX_DEPTH = 6

scene = Scene(ambient, backgroundColor, camPoint, atVector, spheres, lights)
r = float(w) / h
# Screen coordinates: x0, y0, x1, y1.
S = (-1., -1. / r, 1., 1. / r )
for i, x in enumerate(np.linspace(S[0], S[2], w)):
    for j, y in enumerate(np.linspace(S[1], S[3], h)):
        col = np.zeros(3)
        scene.atVector[:2] = (x, y)
        D = scene.atVector - scene.camPoint
        depth = 0
        ray = Ray(scene.camPoint, D)