Example #1
0
def main():
    # объект dtype=float32
    f = np.float32(1.0)
    print('Объект: {}\nТип данных: {}'.format(f, type(f)))

    # объект np.ndarray, полученный из python списка, с автоматическим определеникм dtype
    ar = np.array([1, 2, 3])
    print('Массив: {}\nТип данных массива (dtype): {}\nТип данных элемента массива: {}'.format(ar, type(ar), type(ar[0])))

    # объект np.ndarray, полученный из python списка
    ar_int32 = np.array([1, 2, 3], dtype=np.int32)
    print('Массив: {}\nТип данных массива (dtype): {}\nТип данных элемента массива: {}'.format(ar_int32, type(ar_int32), type(ar_int32[0])))

    # объект np.ndarray, полученный при помощи конструктора типа dtype
    i_int = np.int_(10)
    print('Объект: {}\nТип данных: {}'.format(i_int, type(i_int)))

    ar_int = np.int_([10, 20, 30])
    print('Массив: {}\nТип данных масива (dtype: {}\nТип данных элемента массива: {}'.format(ar_int, type(ar_int), type(ar_int[0])))

    ar_bool = np.bool_([0, 1, 0, 0, 1, 1, 1])
    print('Тип данных массива: {}'.format(ar_bool.dtype))

    ar_int64 = np.array(range(100), dtype=np.int_)
    print('Тип данных массива: {}'.format(ar_int64.dtype))

    ar_float = np.array([1.03, 1, -5.9, 4.6], dtype=np.float16)
    print('Массив ndarray: {}'.format(ar_float))
    print('Тип данных массива: {}'.format(type(ar_float)))

    ar_scalar = ar_float[3]
    print('Значение скаляра массива: {}'.format(ar_scalar))
    print('Тип данных скаляра массива: {}'.format(ar_scalar.dtype))
Example #2
0
def getSuperPixelColorHistogram(superpixels, image):
    colors = []
    #newIm = image
    numSuperpixels = np.max(superpixels)+1
    for i in xrange(0,numSuperpixels):
        temp = np.zeros((1,64),dtype = float)
        indices = np.where(superpixels==i)
        color = image[indices]
        for j in xrange(0,color.shape[0]):
            r = np.int_(color[j][0]/0.25)
            g = np.int_(color[j][1]/0.25)
            b = np.int_(color[j][2]/0.25)
            if r ==4:
                r = 3
            if g == 4:
                g = 3
            if b == 4:
                b = 3
            x = 16*r+4*g+b*1
            temp[0][x] = temp[0][x]+1
        #min_max_scaler = preprocessing.MinMaxScaler()
        #t = min_max_scaler.fit_transform(temp[0])
        #print t
        colors.append(temp[0])
    #showPlots(newIm, numSuperpixels, superpixels)
    return np.array(colors)
Example #3
0
def project_lane_lines(img,left_fitx,right_fitx,yvals):
    
    # Create an image to draw the lines on
    color_warp = np.zeros_like(img).astype(np.uint8)

    # Recast the x and y points into usable format for cv2.fillPoly()
    pts_left = np.array([np.transpose(np.vstack([left_fitx, yvals]))])
    pts_right = np.array([np.flipud(np.transpose(np.vstack([right_fitx, yvals])))])
    pts = np.hstack((pts_left, pts_right))

    # Draw the lane onto the warped blank image
    cv2.polylines(color_warp, np.int_([pts]), isClosed=False, color=(255,0,0), thickness=20)
    cv2.fillPoly(color_warp, np.int_([pts]), (0,255, 0))
    
    
        
    undist = undistort(img)  
    #sp = (550, 310) 
    #ep = (700, 460)
    #for i in range(4):
        #center = ((ep[0] + sp[0])/2 , )
        #cv2.rectangle(undist, (550, 310), (700, 460), (0,0,255), 4)
    unwarp,Minv = warp(img,bird_view=False)

    

    # Warp the blank back to original image space using inverse perspective matrix (Minv)
    newwarp = cv2.warpPerspective(color_warp, Minv, (img.shape[1], img.shape[0])) 
    # Combine the result with the original image
    result = cv2.addWeighted(undist, 1, newwarp, 0.3, 0)
    return result
Example #4
0
    def test_contains(self):
        d = Domain((age, gender, income), metas=(ssn,))
        self.assertTrue("AGE" in d)
        self.assertTrue(age in d)
        self.assertTrue(0 in d)
        self.assertTrue(np.int_(0) in d)
        self.assertTrue("income" in d)
        self.assertTrue(income in d)
        self.assertTrue(2 in d)
        self.assertTrue(np.int_(2) in d)
        self.assertTrue("SSN" in d)
        self.assertTrue(ssn in d)
        self.assertTrue(-1 in d)
        self.assertTrue(np.int_(-1) in d)

        self.assertFalse("no_such_thing" in d)
        self.assertFalse(race in d)
        self.assertFalse(3 in d)
        self.assertFalse(np.int_(3) in d)
        self.assertFalse(-2 in d)
        self.assertFalse(np.int_(-2) in d)

        with self.assertRaises(TypeError):
            {} in d
        with self.assertRaises(TypeError):
            [] in d
Example #5
0
File: sc.py Project: iagapov/ocelot
 def el_field(self, X, Q, gamma, nxyz):
     if self.random_seed != None:
         np.random.seed(self.random_seed)
     N = X.shape[0]
     X[:, 2] = X[:, 2]*gamma
     XX = np.max(X, axis=0)-np.min(X, axis=0)
     #XX = XX*np.random.uniform(low=1.0, high=1.1)
     if self.debug: print( 'mesh steps:', XX)
     # here we use a fast 3D "near-point" interpolation
     # we need a stand-alone module with 1D,2D,3D parricles-to-grid functions
     steps = XX/(nxyz-3)
     X = X/steps
     X_min = np.min(X, axis=0)
     X_mid = np.dot(Q, X)/np.sum(Q)
     X_off = np.floor(X_min-X_mid) + X_mid
     X = X - X_off
     nx = nxyz[0]
     ny = nxyz[1]
     nz = nxyz[2]
     nzny = nz*ny
     Xi = np.int_(np.floor(X)+1)
     inds = np.int_(Xi[:, 0]*nzny+Xi[:, 1]*nz+Xi[:, 2])  # 3d -> 1d
     q = np.bincount(inds, Q, nzny*nx).reshape(nxyz)
     p = self.potential(q, steps)
     Ex = np.zeros(p.shape)
     Ey = np.zeros(p.shape)
     Ez = np.zeros(p.shape)
     Ex[:nx-1, :, :] = (p[:nx-1, :, :] - p[1:nx, :, :])/steps[0]
     Ey[:, :ny-1, :] = (p[:, :ny-1, :] - p[:, 1:ny, :])/steps[1]
     Ez[:, :, :nz-1] = (p[:, :, :nz-1] - p[:, :, 1:nz])/steps[2]
     Exyz = np.zeros((N, 3))
     Exyz[:, 0] = ndimage.map_coordinates(Ex, np.c_[X[:, 0], X[:, 1]+0.5, X[:, 2]+0.5].T, order=1)*gamma
     Exyz[:, 1] = ndimage.map_coordinates(Ey, np.c_[X[:, 0]+0.5, X[:, 1], X[:, 2]+0.5].T, order=1)*gamma
     Exyz[:, 2] = ndimage.map_coordinates(Ez, np.c_[X[:, 0]+0.5, X[:, 1]+0.5, X[:, 2]].T, order=1)
     return Exyz
Example #6
0
def _find_nearest_node_ndarray(rmg, coords, mode='raise'):
    column_indices = np.int_(
        np.around((coords[0] - rmg.node_x[0]) / rmg.node_spacing))
    row_indices = np.int_(
        np.around((coords[1] - rmg.node_y[0]) / rmg.node_spacing))

    return rmg.grid_coords_to_node_id(row_indices, column_indices, mode=mode)
Example #7
0
def mkpDict(period0,nperiod,f,N,ds):
    
    periods = [period0]
    for ip in range(nperiod):
        periods.append(periods[-1]*(1+f/2)/(1-f/2))
    
    periods = num.array(periods)
    tdur_vec = num.vectorize(tdur)
    q = num.int_(num.floor(tdur_vec(ds['rho_s'],ds['b'],periods)/ds['dt']))
    tmin = num.floor(periods*(1e0-f/2e0))
    tmax = num.ceil(periods*(1e0+f/2e0))
    mmin= num.int_(num.floor((N+q-1L)/tmax))
    mmax= num.int_(num.floor((N-q)/tmin)+1L)
    
    pDdict = {}
    for ip in range(nperiod):
        pDdict[ip] = {'q':q[ip],
                      'tmin':tmin[ip],
                      'tmax':tmax[ip],
                      'mmin':mmin[ip],
                      'mmax':mmax[ip],
                      'period':periods[ip]
                     }
                     
    return pDdict, periods, q
		def voigtking(v,a):
			oneonsqrtpi=0.56418958354775630
			h0 = np.array([ 1.0e0, 0.9975031223974601240368798e0, 0.9900498337491680535739060e0, 0.9777512371933363639286036e0, 0.9607894391523232094392107e0, 0.9394130628134757861197108e0, 0.9139311852712281867473535e0, 0.8847059049434835594929548e0, 0.8521437889662113384563470e0, 0.8166864825981108401538061e0, 0.7788007830714048682451703e0, 0.7389684882589442416058206e0, 0.6976763260710310572091293e0, 0.6554062543268405127576690e0, 0.6126263941844160689885800e0, 0.5697828247309230097666297e0, 0.5272924240430485572436946e0, 0.4855368951540794399916001e0, 0.4448580662229411344814454e0, 0.4055545050633205516443034e0, 0.3678794411714423215955238e0, 0.3320399453446606420249195e0, 0.2981972794298873779316010e0, 0.2664682978135241116965901e0, 0.2369277586821217567233665e0, 0.2096113871510978225241101e0, 0.1845195239929892676298138e0, 0.1616211924653392539324509e0, 0.1408584209210449961479715e0, 0.1221506695399900084151679e0, 0.1053992245618643367832177e0, 0.9049144166369591062935159e-1, 0.7730474044329974599046566e-1, 0.6571027322750286139200605e-1, 0.5557621261148306865356766e-1, 0.4677062238395898365276137e-1, 0.3916389509898707373977109e-1, 0.3263075599289603180381419e-1, 0.2705184686635041108596167e-1, 0.2231491477696640649487920e-1, 0.1831563888873418029371802e-1, 0.1495813470057748930092482e-1, 0.1215517832991493721502629e-1, 0.9828194835379685936011149e-2, 0.7907054051593440493635646e-2, 0.6329715427485746576865117e-2, 0.5041760259690979102410257e-2, 0.3995845830084632413030896e-2, 0.3151111598444440557819106e-2, 0.2472563035874193226953048e-2, 0.1930454136227709242213512e-2, 0.1499685289329846120368399e-2, 0.1159229173904591150012118e-2, 0.8915937199952195568639939e-3, 0.6823280527563766163014506e-3, 0.5195746821548384817648154e-3, 0.3936690406550782109805393e-3, 0.2967857677932108344855019e-3, 0.2226298569188890101840659e-3, 0.1661698666072774484528398e-3, 0.1234098040866795494976367e-3, 0.9119595636226606575873788e-4, 0.6705482430281108867614262e-4, 0.4905835745620769579106241e-4, 0.3571284964163521691234528e-4, 0.2586810022265412127035909e-4, 0.1864374233151683041526522e-4, 0.1336996212084380475632834e-4, 0.9540162873079234841590110e-5, 0.6773449997703748098370991e-5, 0.4785117392129009089609771e-5, 0.3363595724825637829225185e-5, 0.2352575200009772922652510e-5, 0.1637237807196195233271403e-5, 0.1133727138747965652009438e-5, 0.7811489408304490795473004e-6, 0.5355347802793106157479094e-6, 0.3653171341207511214363159e-6, 0.2479596018045029629499234e-6, 0.1674635703137489046698250e-6, 0.1125351747192591145137752e-6, 0.7524623257644829651017174e-7, 0.5006218020767042215644986e-7, 0.3314082270898834287088712e-7, 0.2182957795125479209083827e-7, 0.1430724191856768833467676e-7, 0.9330287574504991120387842e-8, 0.6054282282484886644264747e-8, 0.3908938434264861859681131e-8, 0.2511212833271291589987176e-8, 0.1605228055185611608653934e-8, 0.1020982947159334870301705e-8, 0.6461431773106108989429857e-9, 0.4068811450655793356678124e-9, 0.2549381880391968872012880e-9, 0.1589391009451636652873474e-9, 0.9859505575991508240729766e-10, 0.6085665105518337082108266e-10, 0.3737571327944262032923964e-10, 0.2284017657993705413027994e-10, 0.1388794386496402059466176e-10, 0.8402431396484308187150245e-11, 0.5058252742843793235026422e-11, 0.3029874246723653849216172e-11, 0.1805831437513215621913785e-11, 0.1070923238250807645586450e-11, 0.6319285885175366663984108e-12, 0.3710275783094727281418983e-12, 0.2167568882618961942307398e-12, 0.1259993054847742150188394e-12, 0.7287724095819692419343177e-13, 0.4194152536192217185131208e-13, 0.2401734781620959445230543e-13, 0.1368467228126496785536523e-13, 0.7758402075696070467242451e-14, 0.4376618502870849893821267e-14, 0.2456595368792144453705261e-14, 0.1372009419645128473380053e-14, 0.7624459905389739760616425e-15, 0.4215893238174252040735029e-15, 0.2319522830243569388312264e-15, 0.1269802641377875575018264e-15, 0.6916753975541448863883054e-16, 0.3748840457745443581785685e-16, 0.2021715848695342027119482e-16, 0.1084855264042937802512215e-16, 0.5792312885394857923477507e-17, 0.3077235638152508657901574e-17, 0.1626664621453244338034305e-17, 0.8555862896902856300749061e-18, 0.4477732441718301199042103e-18, 0.2331744656246116743545942e-18, 0.1208182019899973571654094e-18, 0.6228913128535643653088166e-19, 0.3195366717748344275120932e-19, 0.1631013922670185678641901e-19, 0.8283677007682876110228791e-20, 0.4186173006145967657832773e-20, 0.2104939978339734445589080e-20, 0.1053151347744013743766989e-20, 0.5242885663363463937171805e-21, 0.2597039249246848208769072e-21, 0.1280015319051641983953037e-21, 0.6277407889747195099574399e-22, 0.3063190864577440373821128e-22, 0.1487292181651270619154227e-22, 0.7185335635902193010046941e-23, 0.3454031957013868448981675e-23, 0.1652091782314268593068387e-23, 0.7862678502984538622254116e-24, 0.3723363121750510429289070e-24, 0.1754400713566556605465117e-24, 0.8225280651606668501925640e-25, 0.3837082905344536379879530e-25, 0.1781066634757091357021587e-25, 0.8225980595143903024275237e-26, 0.3780277844776084635218009e-26, 0.1728575244037268289032505e-26, 0.7864685935766448441713277e-27, 0.3560434556451067378310069e-27, 0.1603810890548637852976087e-27, 0.7188393394953158727447087e-28, 0.3205819323394999444158648e-28, 0.1422573701362478490703169e-28, 0.6281148147605989215436687e-29, 0.2759509067522042024589005e-29, 0.1206293927781149203841840e-29, 0.5246902396795390138796640e-30, 0.2270812922026396509517690e-30, 0.9778860615814667663870901e-31, 0.4190093194494397377123780e-31, 0.1786436718517518413888050e-31, 0.7578445267618382646037748e-32, 0.3198903416725805416294188e-32, 0.1343540197758737662452134e-32, 0.5614728092387934579799402e-33, 0.2334722783487267408869808e-33, 0.9659851300583384710233199e-34, 0.3976803097901655265751816e-34, 0.1629019426220514693169818e-34, 0.6639677199580734400702255e-35, 0.2692751000456178970430831e-35, 0.1086610640745980532852592e-35, 0.4362950029268711046345153e-36, 0.1743070896645292498913954e-36, 0.6929124938815710000577778e-37, 0.2740755284722598699701951e-37, 0.1078675105373929991550997e-37, 0.4224152406206200437573993e-38, 0.1645951484063258284098658e-38, 0.6381503448060790393554118e-39, 0.2461826907787885454919214e-39, 0.9449754976491185028813549e-40, 0.3609209642415355020302235e-40, 0.1371614910949353618952282e-40, 0.5186576811908572940413120e-41, 0.1951452380295377748121319e-41, 0.7305730197111493885868359e-42, 0.2721434140093713884466599e-42, 0.1008696596314342558322441e-42, 0.3720075976020835962959696e-43, 0.1365122395620087240477630e-43 ], dtype=np.float64)
			h1 = np.array([ -1.128379167095512573896159e0, -1.122746665023313894112994e0, -1.105961434222613497822717e0, -1.078356949458362356972974e0, -1.040477963566390226869037e0, -0.9930644092865188274925694e0, -0.9370297574325730524254160e0, -0.8734346738611667009559691e0, -0.8034569860177944012914767e0, -0.7283590897795191457635390e0, -0.6494539941944691013512214e0, -0.5680712138345335512208471e0, -0.4855236771153186839197872e0, -0.4030767281964792012404736e0, -0.3219201665209207840831093e0, -0.2431441002236951675148354e0, -0.1677191974661332963609891e0, -0.9648171389061105293546881e-1, -0.3012346558870770535102483e-1, 0.3081328457047809980986685e-1, 0.8593624458727488433391777e-1, 0.1349991935349749351748713e0, 0.1778942744880748462232135e0, 0.2146410885736963723412265e0, 0.2453732617833523433216744e0, 0.2703231847626659615037426e0, 0.2898056218155761132507312e0, 0.3042008523837261147222841e0, 0.3139379509747736418513567e0, 0.3194787353320834397089635e0, 0.3213028233267945998845488e0, 0.3198941423604233541674753e0, 0.3157291364070343763776039e0, 0.3092668200208504802085382e0, 0.3009407397271468294117335e0, 0.2911528243392948676821857e0, 0.2802690390913659378360681e0, 0.2686167052981096351368975e0, 0.2564833079412283848897372e0, 0.2441165877658165024921633e0, 0.2317257011687522312257119e0, 0.2194832289213470945135105e0, 0.2075278218310246553881156e0, 0.1959672858880207128215797e0, 0.1848819293094190730287360e0, 0.1743280173110208640535652e0, 0.1643412057011470302647273e0, 0.1549398500207542791790132e0, 0.1461281117364874603340094e0, 0.1378988059908943461128856e0, 0.1302359559637753421977543e0, 0.1231170365911391556632533e0, 0.1165149050377156668055896e0, 0.1103994269264874144398788e0, 0.1047388160423518894772002e0, 0.9950071130235648759030670e-1, 0.9465301854781620910441970e-1, 0.9016454652735125189272609e-1, 0.8600546667768981700419079e-1, 0.8214762533231104047151097e-1, 0.7856473513008974607178765e-1, 0.7523246995193424459351750e-1, 0.7212848493340500348466924e-1, 0.6923238018945846374255513e-1, 0.6652562400245432725286132e-1, 0.6399144848312167544450556e-1, 0.6161472819590847810012464e-1, 0.5938184999317344054777048e-1, 0.5728058034957269600588669e-1, 0.5529993483145627029203620e-1, 0.5343005296426139233134751e-1, 0.5166208065197234887486323e-1, 0.4998806142885727821214551e-1, 0.4840083715410895783485349e-1, 0.4689395826338997495993764e-1, 0.4546160333748704598916335e-1, 0.4409850750954268216573793e-1, 0.4279989908392569899980027e-1, 0.4156144366035708515282858e-1, 0.4037919502845779134315796e-1, 0.3924955210570969222557380e-1, 0.3816922122416471946490538e-1, 0.3713518311895684989765586e-1, 0.3614466402785612590311943e-1, 0.3519511037069617482332004e-1, 0.3428416653694949866994660e-1, 0.3340965536664229903158673e-1, 0.3256956096272257612903376e-1, 0.3176201352112533673779090e-1, 0.3098527590780517228496903e-1, 0.3023773174995156695256252e-1, 0.2951787484170619418302355e-1, 0.2882429969333463230632146e-1, 0.2815569307740452259166926e-1, 0.2751082644654734935368337e-1, 0.2688854911528297388431485e-1, 0.2628778211358937241904422e-1, 0.2570751263279204975253415e-1, 0.2514678899527364475073049e-1, 0.2460471608876676259183765e-1, 0.2408045121385331090696902e-1, 0.2357320029997478838776359e-1, 0.2308221445094914570064896e-1, 0.2260678678585010840991674e-1, 0.2214624954526743636682309e-1, 0.2169997143654264861646818e-1, 0.2126735519465680897241377e-1, 0.2084783533811200664569883e-1, 0.2044087610146017752978434e-1, 0.2004596952814515567227767e-1, 0.1966263370908071277476715e-1, 0.1929041115392591487587378e-1, 0.1892886728337045173071115e-1, 0.1857758903193275942486415e-1, 0.1823618355182474294515453e-1, 0.1790427700936730343669473e-1, 0.1758151346626646308038721e-1, 0.1726755383879409857500321e-1, 0.1696207492857163038741910e-1, 0.1666476851923932358834102e-1, 0.1637534053381661837450139e-1, 0.1609351024802744708797459e-1, 0.1581900955528515170398058e-1, 0.1555158227940989996039230e-1, 0.1529098353149220739767610e-1, 0.1503697910762349625920090e-1, 0.1478934492449222808347731e-1, 0.1454786649009525295887101e-1, 0.1431233840704145462214254e-1, 0.1408256390613103046576229e-1, 0.1385835440808103075999097e-1, 0.1363952911143803959964144e-1, 0.1342591460487383719630737e-1, 0.1321734450220107129175951e-1, 0.1301365909857474699723209e-1, 0.1281470504646293252049926e-1, 0.1262033505007755515762735e-1, 0.1243040757705449418533892e-1, 0.1224478658626222948827240e-1, 0.1206334127070085131071308e-1, 0.1188594581452897199141430e-1, 0.1171247916332562864755594e-1, 0.1154282480675818732553606e-1, 0.1137687057288605896976939e-1, 0.1121450843338417065773542e-1, 0.1105563431902001242285305e-1, 0.1090014794476407143162512e-1, 0.1074795264395590657657700e-1, 0.1059895521098731117021612e-1, 0.1045306575200023435008377e-1, 0.1031019754313063242129945e-1, 0.1017026689586042607609242e-1, 0.1003319302906845397201302e-1, 0.9898897947397924639729408e-2, 0.9767306325582547468180475e-2, 0.9638345398396424782187982e-2, 0.9511944855914052317394595e-2, 0.9388036743786533882143785e-2, 0.9266555368258485665416943e-2, 0.9147437205667194364984339e-2, 0.9030620816181499749829423e-2, 0.8916046761552686783940876e-2, 0.8803657526663477808232965e-2, 0.8693397444674087410976982e-2, 0.8585212625576311168220303e-2, 0.8479050887977828363904268e-2, 0.8374861693949366877024963e-2, 0.8272596086777159693185345e-2, 0.8172206631472266686907249e-2, 0.8073647357896888215194357e-2, 0.7976873706375800846399120e-2, 0.7881842475668539112571351e-2, 0.7788511773184966394916599e-2, 0.7696840967333456047851643e-2, 0.7606790641897071224649652e-2, 0.7518322552338916854888971e-2, 0.7431399583943265980411531e-2, 0.7345985711704159367477213e-2, 0.7262045961877964368036759e-2, 0.7179546375120877141317720e-2, 0.7098453971136580788416864e-2, 0.7018736714763248519923831e-2, 0.6940363483432822204243367e-2, 0.6863304035939017881037086e-2, 0.6787528982453825324020280e-2, 0.6713009755735391745310971e-2, 0.6639718583473122562606414e-2, 0.6567628461718606252976457e-2, 0.6496713129353586350126915e-2, 0.6426947043548671526978323e-2, 0.6358305356168803683625031e-2, 0.6290763891083702643557758e-2, 0.6224299122343582476647260e-2, 0.6158888153182396103862750e-2, 0.6094508695812718682782931e-2, 0.6031139051978132847456608e-2, 0.5968758094230636272231571e-2, 0.5907345247902159938278185e-2, 0.5846880473740769223255677e-2, 0.5787344251183524483318654e-2, 0.5728717562239307805652498e-2, 0.5670981875956182433959706e-2 ], dtype=np.float64)
			h2 = np.array([ 1.0e0, 0.9925156067854728234166954e0, 0.9702488370741846925024279e0, 0.9337524315196362275518164e0, 0.8839262840201373526840738e0, 0.8219864299617913128547470e0, 0.7494235719224071131328299e0, 0.6679529582323300874171809e0, 0.5794577764970237101503160e0, 0.4859284571458759498915146e0, 0.3894003915357024341225852e0, 0.2918925528622829754342991e0, 0.1953493712998886960185562e0, 0.1015879694206602794774387e0, 0.1225252788368832137977160e-1, -0.7122285309136537622082871e-1, -0.1476418787320535960282345e0, -0.2160639183435653507962620e0, -0.2758120010582235033784961e0, -0.3264713765759730440736642e0, -0.3678794411714423215955238e0, -0.4001081341403160736400280e0, -0.4234401367904400766628734e0, -0.4383403499032471637408907e0, -0.4454241863223889026399290e0, -0.4454241976960828728637340e0, -0.4391564671033144569589568e0, -0.4274880540708223266513326e0, -0.4113065890894513887520768e0, -0.3914928958756679769706131e0, -0.3688972859665251787412620e0, -0.3443199355303629399446828e0, -0.3184955306263949534807185e0, -0.2920821644962502188874669e0, -0.2656542962828890681640534e0, -0.2396994397177897912204020e0, -0.2146181451424491640939456e0, -0.1907267687784773058932939e0, -0.1682624875086995569546816e0, -0.1473900121018631148986771e0, -0.1282094722211392620560261e0, -0.1107649874577763082733483e0, -0.9505349453993480902150559e-1, -0.8103346641770551054241192e-1, -0.6863322916783106348475741e-1, -0.5775865327580743751389419e-1, -0.4830006328783957980109026e-1, -0.4013827136320013258889535e-1, -0.3314969401563551466825700e-1, -0.2721055620979549646261829e-1, -0.2220022256661865628545539e-1, -0.1800372189840480267502263e-1, -0.1451354925728548119815172e-1, -0.1163084007733763911929080e-1, -0.9266014956431594449373699e-2, -0.7338992385437093554928018e-2, -0.5779061516816548137194317e-2, -0.4524499030007499171731476e-2, -0.3522004336456824141111923e-2, -0.2726016661692386541868837e-2, -0.2097966669473552341459824e-2, -0.1605504811757694087682580e-2, -0.1221738898797218035679319e-2, -0.9245047462622340271825711e-3, -0.6956863110190540254524861e-3, -0.5205955169809141905659767e-3, -0.3874169656489197360292113e-3, -0.2867188376814953929994613e-3, -0.2110284027525126746959732e-3, -0.1544685271976339753833504e-3, -0.1124502587150317136058296e-3, -0.8141583451940456365639560e-4, -0.5862617398424354123250055e-4, -0.4198696356554642675724513e-4, -0.2990772192017133390000897e-4, -0.2118866502002593128272052e-4, -0.1493070967418717996705171e-4, -0.1046450930688891587354327e-4, -0.7294971485088477169986746e-5, -0.5058237141326785665552064e-5, -0.3488590416297032549927031e-5, -0.2393206427093938070506012e-5, -0.1633028318374209170743394e-5, -0.1108394815502115127316820e-5, -0.7483179321690142728739359e-6, -0.5025418723896900527555212e-6, -0.3357037469306895805115546e-6, -0.2230700306981556484079346e-6, -0.1474451577404705893471723e-6, -0.9694537142843821183145493e-7, -0.6340650817983165854183039e-7, -0.4125281597997292543454039e-7, -0.2669863608647444234432417e-7, -0.1718869397329539903528673e-7, -0.1100823095953252158935162e-7, -0.7013187829205346730804204e-8, -0.4444665113656971914920979e-8, -0.2802144497835918309456751e-8, -0.1757406038399392007880848e-8, -0.1096442676719878283524089e-8, -0.6805092493832370091384262e-9, -0.4201635819811978308984480e-9, -0.2580720549398903308510481e-9, -0.1576898051707325645824557e-9, -0.9585353270320148521118371e-10, -0.5796372027032496381736661e-10, -0.3486981951439767325186431e-10, -0.2086844614201629359434107e-10, -0.1242450483517188985330601e-10, -0.7358989436838238028175315e-11, -0.4336195837012716989509190e-11, -0.2541866144559293225048769e-11, -0.1482350707216456169596291e-11, -0.8600132295160969048704279e-12, -0.4963825648030345884941720e-12, -0.2850272799994640993351100e-12, -0.1628231410435433343915847e-12, -0.9253517530796568988711767e-13, -0.5231904387078439423734991e-13, -0.2942904274907536637035087e-13, -0.1646861209472934265701707e-13, -0.9168609972068950589419375e-14, -0.5078280768842531755862938e-14, -0.2798321959684086361623925e-14, -0.1534077985990025530178263e-14, -0.8366946223931157801875458e-15, -0.4540014839572489640421670e-15, -0.2450864324006565520585709e-15, -0.1316297011679965318337360e-15, -0.7033347094398993022030766e-16, -0.3738906588834781501200156e-16, -0.1977436055729519304364136e-16, -0.1040486355537857239908506e-16, -0.5446873085247993592442947e-17, -0.2836846572016980047452363e-17, -0.1469951297806504842876013e-17, -0.7577907726628295065637298e-18, -0.3886652327556223671914838e-18, -0.1983274447591697794634031e-18, -0.1006865346010664339728430e-18, -0.5085599093462560019056651e-19, -0.2555616473221360979839205e-19, -0.1277711291477349028381922e-19, -0.6355561617974547678564100e-20, -0.3145284379748115775839534e-20, -0.1548642984144385532194339e-20, -0.7586277364385535380007560e-21, -0.3697368508385495481212434e-21, -0.1792850002167444277197814e-21, -0.8649339487208141711410640e-22, -0.4151549880751819128657313e-22, -0.1982560526365887292005855e-22, -0.9419591402219956768405243e-23, -0.4452742857507067242031201e-23, -0.2094178149147388017585982e-23, -0.9799199383965174477667876e-24, -0.4562039303075778937781093e-24, -0.2113096807073358619927786e-24, -0.9738054125666016460529380e-25, -0.4464962955517461045769742e-25, -0.2036839830996770073279630e-25, -0.9244633325579509781433326e-26, -0.4174617922924968276183391e-26, -0.1875592296561359766067593e-26, -0.8384076547424474404764890e-27, -0.3728786627489159285725893e-27, -0.1649968834419055881014869e-27, -0.7264074023243377877657008e-28, -0.3181863066343386789136187e-28, -0.1386691329625598948075213e-28, -0.6012783734099460236172624e-29, -0.2593995437123362612886143e-29, -0.1113425178718492778355866e-29, -0.4755009983792073461050496e-30, -0.2020415749389589696795519e-30, -0.8541405110545145479519840e-31, -0.3592671419230207088768861e-31, -0.1503507555679300913224246e-31, -0.6260283436716785719346509e-32, -0.2593480377514370417261009e-32, -0.1068988029132498238513063e-32, -0.4383933266292682172809914e-33, -0.1788778436796033153181937e-33, -0.7261912176216306101089190e-34, -0.2933239704874698217172402e-34, -0.1178817380216022663848294e-34, -0.4713550938665925243747415e-35, -0.1875222736937308593811831e-35, -0.7422680608185535408905020e-36, -0.2923292133270549875473422e-36, -0.1145479868926911875642964e-36, -0.4465877102072613609496200e-37, -0.1732329082290364039482100e-37, -0.6685880402092324407358875e-38, -0.2567388790315000103954881e-38, -0.9809113395522088573556313e-39, -0.3728835208268407801110216e-39, -0.1410334685901388337197457e-39, -0.5307340860010760817486761e-40, -0.1987182729569070557023125e-40, -0.7402951192281463566289795e-41, -0.2743964271316156357722060e-41 ], dtype=np.float64)
			h3 = np.array([ -0.7522527780636750492641059e0, -0.7447490315497708463240858e0, -0.7224619689626252165385118e0, -0.6860552061846493969863268e0, -0.6366054955061156295204758e0, -0.5755603365344096850483262e0, -0.5046815829547811446478382e0, -0.4259777864640005624125117e0, -0.3416285184773921405216660e0, -0.2539042236274465364534081e0, -0.1650852727968867264939651e0, -0.7738379667939842709258988e-1, 0.7128394424195324853014844e-2, 0.8658293927736663174097951e-1, 0.1593668102410841966827594e0, 0.2241613263920280449352809e0, 0.2799673824845877680517527e0, 0.3261167006652041288605015e0, 0.3622695948610319801705815e0, 0.3884003473857446343896496e0, 0.4047718038942624860766923e0, 0.4119011753186058824533937e0, 0.4105192820995319949018743e0, 0.4015255845130582620257648e0, 0.3859413195031716183649201e0, 0.3648629230000597762360636e0, 0.3394176769351978836202936e0, 0.3107232057693364099667621e0, 0.2798520840662402744643034e0, 0.2478024303401173430156194e0, 0.2154749773684402246897790e0, 0.1836567467116494732079552e0, 0.1530111326375332319918793e0, 0.1240739307148443832620940e0, 0.9725463688468146271051371e-1, 0.7284219701173870412977577e-1, 0.5101430368585303674221369e-1, 0.3184931174142700893159512e-1, 0.1533986919450959655382290e-1, 0.1407426811309193306581366e-2, -0.1008311291608286074413380e-1, -0.1930922840812282398312132e-1, -0.2647758532035030682089135e-1, -0.3181217775839225922486926e-1, -0.3554404023046894464526427e-1, -0.3790265208183702749516685e-1, -0.3910905737306063850349279e-1, -0.3937064210715186736633504e-1, -0.3887744829978686271653342e-1, -0.3779986028416367095012508e-1, -0.3628747152011772566083547e-1, -0.3446892799961155950489723e-1, -0.3245254463375208029954651e-1, -0.3032750110251363953076864e-1, -0.2816544089164076874994184e-1, -0.2602231914851994604543481e-1, -0.2394036936359898584929537e-1, -0.2195008388641825247433045e-1, -0.2007212746338689903391700e-1, -0.1831912527214265469865516e-1, -0.1669728661861120572688442e-1, -0.1520784216814043766189564e-1, -0.1384828617477219420839203e-1, -0.1261342573197174928239427e-1, -0.1149624682246302216128454e-1, -0.1048861222035593117850278e-1, -0.9581809474549548274726564e-2, -0.8766968673914992518412266e-2, -0.8035369845963356580758239e-2, -0.7378659024311709843220737e-2, -0.6788990545369120409265684e-2, -0.6259111260511144290061333e-2, -0.5782400284632080908741386e-2, -0.5352875804464578036313191e-2, -0.4965178455311671875710459e-2, -0.4614538919616485527188256e-2, -0.4296735750484013517713710e-2, -0.4008047998562558651176877e-2, -0.3745206023826233664801882e-2, -0.3505342894046476979204381e-2, -0.3285947990022833548498951e-2, -0.3084823830238963251792028e-2, -0.2900046668982056656687612e-2, -0.2729931086807768375811907e-2, -0.2572998556853316466871207e-2, -0.2427949813646523181953355e-2, -0.2293640754330915732383722e-2, -0.2169061550185197106672818e-2, -0.2053318626361484792588433e-2, -0.1945619169898585865047079e-2, -0.1845257842557062274985012e-2, -0.1751605400071271234291063e-2, -0.1664098948801722796139379e-2, -0.1582233601544145935191528e-2, -0.1505555324435757496173641e-2, -0.1433654795260900326865144e-2, -0.1366162119305863305428748e-2, -0.1302742271937752484738341e-2, -0.1243091157235637593921277e-2, -0.1186932189400779713774489e-2, -0.1134013318531012910469058e-2, -0.1084104434925714219487149e-2, -0.1036995096671516116549004e-2, -0.9924925341187004927105684e-3, -0.9504198922493585737226438e-3, -0.9106146780909950790852145e-3, -0.8729273854455090856168734e-3, -0.8372202734577421999252958e-3, -0.8033662790881490171689315e-3, -0.7712480465049772662387464e-3, -0.7407570588761368843162862e-3, -0.7117928601052224681383490e-3, -0.6842623557902678206459998e-3, -0.6580791841453911032352388e-3, -0.6331631488616646148452257e-3, -0.6094397069328185150662371e-3, -0.5868395053652243037188589e-3, -0.5652979614557357816469240e-3, -0.5447548819764808379193485e-3, -0.5251541171699206704315699e-3, -0.5064432459446979814905582e-3, -0.4885732890847829717111949e-3, -0.4714984476509869340551945e-3, -0.4551758640732029088500233e-3, -0.4395654037105695480727411e-3, -0.4246294549008608587718018e-3, -0.4103327457346023732872108e-3, -0.3966421759777806984761265e-3, -0.3835266627330082944382909e-3, -0.3709569985755748701109446e-3, -0.3589057210304776810509891e-3, -0.3473469923714317173865229e-3, -0.3362564888248703524021643e-3, -0.3256112983526542094353014e-3, -0.3153898262679901745636708e-3, -0.3055717080111181576022737e-3, -0.2961377284756872027881530e-3, -0.2870697473343167903391904e-3, -0.2783506298634098374691914e-3, -0.2699641828135376810549337e-3, -0.2618950949132550960609061e-3, -0.2541288816315519571599965e-3, -0.2466518338577700959600751e-3, -0.2394509701881161861915701e-3, -0.2325139925352426415436720e-3, -0.2258292448020649292499711e-3, -0.2193856743833149971866920e-3, -0.2131727962785441468085678e-3, -0.2071806596186039850962257e-3, -0.2013998164242456949121338e-3, -0.1958212924305587453675523e-3, -0.1904365598246742268269672e-3, -0.1852375117566223449189077e-3, -0.1802164384945805472907308e-3, -0.1753660051060874920343825e-3, -0.1706792305562262391906103e-3, -0.1661494681223850440721571e-3, -0.1617703870330642541013594e-3, -0.1575359552453832883093564e-3, -0.1534404232825155716084045e-3, -0.1494783090582982775062280e-3, -0.1456443836217787948749789e-3, -0.1419336577595168918308957e-3, -0.1383413693981019940302776e-3, -0.1348629717536061671270311e-3, -0.1314941221786090162018978e-3, -0.1282306716610312631043834e-3, -0.1250686549323268134999138e-3, -0.1220042811456336331711188e-3, -0.1190339250872943430156140e-3, -0.1161541188877486230913414e-3, -0.1133615442001899065679247e-3, -0.1106530248175853517419676e-3, -0.1080255197006960803598953e-3, -0.1054761163916181391183883e-3, -0.1030020247891063146774180e-3, -0.1006005712635544029343839e-3, -0.9826919309099737327798045e-4, -0.9600543318688272806740460e-4, -0.9380693512163903486436983e-4, -0.9167143840125715403094134e-4, -0.8959677399720145006388879e-4, -0.8758086011099098595144745e-4, -0.8562169815974051700802759e-4, -0.8371736896983366064768422e-4, -0.8186602916672109476829247e-4, -0.8006590774959976520266573e-4, -0.7831530284043921555152064e-4, -0.7661257859748228262605498e-4, -0.7495616228396319961002592e-4, -0.7334454148335998246272097e-4, -0.7177626145303295708079228e-4, -0.7024992260860025649833230e-4, -0.6876417813186671874001603e-4, -0.6731773169555726054493046e-4, -0.6590933529851172806481185e-4, -0.6453778720537748581672358e-4, -0.6320192998519050404738797e-4, -0.6190064864356719221383246e-4, -0.6063286884353932444622322e-4, -0.5939755521035460581086281e-4, -0.5819370971583712468698264e-4 ], dtype=np.float64)

			# Voigt function is symmetric, so -v = v
			if len(np.argwhere(v<0.0)) != 0: v[np.argwhere(v<0.0)] *= -1.0
			# if a is exactly zero go to 3 for exact expression
			if (a == 0.0):
				return np.exp(-(v*v))
			# Scale up v for ease with lookup tables
			v0 = v*10.0
			n=np.array(v0,dtype=np.int_)
			voigt_prof = np.zeros(np.size(v))
			nl=np.argwhere(n<100)
			nh=np.argwhere(n>=100)
			if len(nh) != 0:
				r=1.0/v[nh]**2
				voigt_prof[nh] = a*r*oneonsqrtpi*(1.0 + r*(1.5 + r*(3.75 + r*(13.125 + 59.0625*r))) - a*a*r*(1.0 + r*(5.0 +26.25*r)))
			if len(nl) != 0:
				v0[nl] = 2.0*v[nl]*10.0
				p=np.int_(v0[nl])
				p1=p+1
				p2=p+2
				x=0.5*np.int_(v0[nl])
				y=x+0.5
				z=x+1.0
				v1 = v0[nl] * 0.5
				voigt_prof[nl] = 2.0*((v1-y)*(v1-z)*(h0[p]+a*(h1[p]+a*(h2[p]+a*h3[p]))) - (v1-x)*(v1-z)*2.0*(h0[p1] + a*(h1[p1]+a*(h2[p1]+a*h3[p1]))) + (v1-x)*(v1-y)*(h0[p2] + a*(h1[p2]+a*(h2[p2]+a*h3[p2]))))
			del nl, nh
			return voigt_prof
Example #9
0
def translate_pix(xpix_rot, ypix_rot, xpos, ypos, scale): 
    # TODO:
    # Apply a scaling and a translation
    xpix_translated = np.int_(xpos + xpix_rot / scale)
    ypix_translated = np.int_(ypos + ypix_rot / scale)
    # Return the result  
    return xpix_translated, ypix_translated
Example #10
0
        def Init(self):
                #boundary and domain condition
                self.lat  = io.read_PETSc_vec(self.config["-Metos3DBoundaryConditionInputDirectory"][0] + self.config["-Metos3DLatitudeFileFormat"][0])
                dz        = io.read_PETSc_vec(self.config["-Metos3DDomainConditionInputDirectory"][0] + self.config["-Metos3DLayerHeightFileFormat"][0])
                z         = io.read_PETSc_vec(self.config["-Metos3DDomainConditionInputDirectory"][0] + self.config["-Metos3DLayerDepthFileFormat"][0])
                self.lsm  = io.read_PETSc_mat(self.config["-Metos3DProfileInputDirectory"][0] + self.config["-Metos3DProfileMaskFile"][0])
                self.fice = np.zeros((self.profiles,np.int_(self.config["-Metos3DIceCoverCount"][0])),dtype=np.float_)
                for i in range(np.int_(self.config["-Metos3DIceCoverCount"][0])):
                        self.fice[:,i] = io.read_PETSc_vec(self.config["-Metos3DBoundaryConditionInputDirectory"][0] + (self.config["-Metos3DIceCoverFileFormat"][0] % i))

                self.bc         = np.zeros(2,dtype=np.float_)
                self.dc         = np.zeros((self.ny,2),dtype=np.float_)
                self.dc[:,0]    = z
                self.dc[:,1]    = dz

                self.u          = np.array(self.config["-Metos3DParameterValue"],dtype=np.float_)
                self.dt         = np.float_(self.config["-Metos3DTimeStep"][0])
                self.nspinup    = np.int_(self.config["-Metos3DSpinupCount"][0])
                self.ntimestep  = np.int_(self.config["-Metos3DTimeStepCount"][0])


                self.matrixCount  = np.int_(self.config["-Metos3DMatrixCount"][0])
                self.U_PODN        = np.load(self.config["-Metos3DMatrixInputDirectory"][0] +'N/'+ self.config["-Metos3DMatrixPODFileFormat"][0])
                self.U_PODDOP       = np.load(self.config["-Metos3DMatrixInputDirectory"][0] +'DOP/'+ self.config["-Metos3DMatrixPODFileFormat"][0])
                self.U_DEIMN       = np.load(self.config["-Metos3DMatrixInputDirectory"][0] +'N/'+ self.config["-Metos3DMatrixDEIMFileFormat"][0])
                self.U_DEIMDOP       = np.load(self.config["-Metos3DMatrixInputDirectory"][0] +'DOP/'+ self.config["-Metos3DMatrixDEIMFileFormat"][0])
                self.DEIM_IndicesN = np.load(self.config["-Metos3DMatrixInputDirectory"][0] +'N/'+ self.config["-Metos3DDEIMIndicesFileFormat"][0])
                self.DEIM_IndicesDOP = np.load(self.config["-Metos3DMatrixInputDirectory"][0] +'DOP/'+ self.config["-Metos3DDEIMIndicesFileFormat"][0])

                

                self.AN = np.ndarray(shape=(self.matrixCount,self.U_PODN.shape[1],self.U_PODN.shape[1]), dtype=np.float_, order='C')
                self.ADOP = np.ndarray(shape=(self.matrixCount,self.U_PODDOP.shape[1],self.U_PODDOP.shape[1]), dtype=np.float_, order='C')

                for i in range(0,self.matrixCount):
                        self.AN[i] = np.load(self.config["-Metos3DMatrixInputDirectory"][0] +'N/'+ self.config["-Metos3DMatrixReducedFileFormat"][0] % i)
                        self.ADOP[i] = np.load(self.config["-Metos3DMatrixInputDirectory"][0] +'DOP/'+ self.config["-Metos3DMatrixReducedFileFormat"][0] % i)
        
                self.PN = np.ndarray(shape=(self.matrixCount,self.U_PODN.shape[1],self.U_DEIMN.shape[1]), dtype=np.float_, order='C')
                self.PDOP = np.ndarray(shape=(self.matrixCount,self.U_PODDOP.shape[1],self.U_DEIMDOP.shape[1]), dtype=np.float_, order='C')
                for i in range(0,self.matrixCount):
                        self.PN[i] = np.load(self.config["-Metos3DMatrixInputDirectory"][0] +'N/'+ self.config["-Metos3DMatrixReducedDEINFileFormat"][0] % i)
                        self.PDOP[i] = np.load(self.config["-Metos3DMatrixInputDirectory"][0] +'DOP/'+ self.config["-Metos3DMatrixReducedDEINFileFormat"][0] % i)

                #precomputin the interplaton indices for a year         
                [self.interpolation_a,self.interpolation_b,self.interpolation_j,self.interpolation_k] = util.linearinterpolation(2880,12,0.0003472222222222)

                self.yN     = np.ones(self.ny,dtype=np.float_) * np.float_(self.config["-Metos3DTracerInitValue"])[0]
                self.yDOP     = np.ones(self.ny,dtype=np.float_) * np.float_(self.config["-Metos3DTracerInitValue"])[1]
                self.y_redN = np.dot(self.U_PODN.T,self.yN)
                self.y_redDOP = np.dot(self.U_PODDOP.T,self.yDOP)

                self.qN     = np.zeros(self.DEIM_IndicesN.shape[0],dtype=np.float_)
                self.qDOP     = np.zeros(self.DEIM_IndicesDOP.shape[0],dtype=np.float_)

                self.J,self.PJ = util.generateIndicesForNonlinearFunction(self.lsm,self.profiles,self.ny)

                self.out_pathN     = self.config["-Metos3DTracerOutputDirectory"][0] +self.config["-Metos3DSpinupMonitorFileFormatPrefix"][0] + self.config["-Metos3DSpinupMonitorFileFormatPrefix"][1] +self.config["-Metos3DTracerOutputFile"][0]
                self.out_pathDOP     = self.config["-Metos3DTracerOutputDirectory"][0] +self.config["-Metos3DSpinupMonitorFileFormatPrefix"][0] + self.config["-Metos3DSpinupMonitorFileFormatPrefix"][1] +self.config["-Metos3DTracerOutputFile"][1]
                self.monitor_path = self.config["-Metos3DTracerMointorDirectory"][0] +self.config["-Metos3DSpinupMonitorFileFormatPrefix"][0] + self.config["-Metos3DSpinupMonitorFileFormatPrefix"][1] +self.config["-Metos3DTracerOutputFile"][0]
Example #11
0
    def indices(self, x, y, clip=False):
        """
        Return the grid pixel indices (i_x, i_y) corresponding to the
        given arrays of grid coordinates. Arrays x and y must have the
        same size. Also return a boolean array of the same length that
        is True where the pixels are within the grid bounds and False
        elsewhere.
		
        If clip is False, a ValueError is raised if any of the pixel
        centers are outside the grid bounds, and array within will be
        all True. If clip is True, then the i_x and i_y values where
        within is False will be nonsense; the safe thing is to use
        only i_x[within] and i_y[within].
        """
        if x.size != y.size:
            raise ValueError("Arrays x and y must have the same length.")
        # This is a workaround for the behavior of int_: when given an
        # array of size 1 it returns an int instead of an array.
        if x.size == 1:
            i_x = np.array([np.int(np.round((x[0] - self.x[0]) / self.dx()))])
            i_y = np.array([np.int(np.round((y[0] - self.y[0]) / self.dy()))])
        else:
            i_x = np.int_(np.round_((x - self.x[0]) / self.dx()))
            i_y = np.int_(np.round_((y - self.y[0]) / self.dy()))
        within = ((0 <= i_x) & (i_x < self.x.size) & (0 <= i_y) & (i_y < self.y.size))
        if not clip and not all(within):
            raise ValueError("Not all points are inside the grid bounds, and clipping is not allowed.")
        return i_x, i_y, within
Example #12
0
def initialize(video_capture,rot_angle, pt1, pt2, ppl_width):
    #read image
    ret, image = video_capture.read()    
    
    (hh, ww) = image.shape[:2]
    
    #rotate
    M = None;
    if (rot_angle != 0):
        center = (ww / 2, hh / 2)
        M = cv2.getRotationMatrix2D(center, rot_angle, 1.0)    
    
    image = imutils.resize(image, width=min(400, image.shape[1]))
    
    ##mask after resize
    resize_ratio = image.shape[1] / float(ww) 
    
    #max_min_ppl_size  
    ppl_size=[50,100]
    ppl_size[0] = np.ceil(ppl_width * resize_ratio * 1.4)
    ppl_size[1] = np.ceil(ppl_width * resize_ratio * 0.8)
    #print max_ppl_size
    
    ROI_1 = np.int_(np.dot(pt1,resize_ratio))   
    ROI_2 = np.int_(np.dot(pt2,resize_ratio))
    
    
    return [ww, hh, M, ppl_size, ROI_1, ROI_2]
def get_outliers(art_outliers,motion):
    import numpy as np
    import os
    def try_import(fname):
        try:
            a = np.genfromtxt(fname)
            return a
        except:
            return np.array([])

    mot = np.genfromtxt(motion)
    print mot.shape
    len = mot.shape[0]
    outliers = try_import(art_outliers)
    if outliers.shape == ():  # 1 outlier
        art = np.zeros((len, 1))
        art[np.int_(outliers), 0] = 1 #  art outputs 0 based indices

    elif outliers.shape[0] == 0:  # empty art file
        art = np.zeros((len, 1))

    else:  # >1 outlier
        art = np.zeros((len, outliers.shape[0]))
        for j, t in enumerate(outliers):
            art[np.int_(t), j] = 1 #  art outputs 0 based indices

    out_file = os.path.abspath('outliers.txt')
    np.savetxt(out_file,art)

    return out_file
Example #14
0
def add_pbc_jncol(data,rand):
   '''If the input is a periodic box and los is along z axis then jacknife region is simply equal area region in the x-y space which can be done in using this function and not needed to be supplied with data file make sure that njn is a perfect square'''

   #adding jacknife regions
   if(args.njn>0 and args.los==1):
      POS_min,POS_max, blen=getminmax(data,rand=rand)
      NJNx=np.int(np.sqrt(args.njn))
      NJNy=np.int(args.njn/NJNx)
      for ii in (0,2):
	 if(ii==0): mat=data
	 else: mat=rand
	 
         #get the x and y indx as integers
	 indx=np.int_(NJNx*(mat[:,0]-POS_min[0])/blen[0])
	 indy=np.int_(NJNy*(mat[:,1]-POS_min[1])/blen[1])
         #apply modulo operation on x an y index
	 indx=np.mod(indx,NJNx)
	 indy=np.mod(indy,NJNy)

	 #convert index to integers
	 #indx.astype(np.int64); indy.astype(np.int64);
	 jnreg=NJNy*indx+indy
	 mat=np.column_stack([mat,jnreg])

         if(ii==0): data=mat
	 else: rand=mat

      return data,rand
   else:
      print('not appropriate input to add jacknife internally')
      sys.exit()
      return 0
Example #15
0
def DepositDataToGrid(data, coords, N, hsml, gridres, rmax, griddata):
    norm = 1.8189136353359467 # 40 / (7 pi) for 2D
    grid_dx = 2*rmax/(gridres-1)
    shift_coords = coords[:] + rmax
    
    gxmin = np.int_((shift_coords[:,0] - hsml[:])/grid_dx + 0.5)
    gxmax = np.int_((shift_coords[:,0] + hsml[:])/grid_dx)
    gymin = np.int_((shift_coords[:,1] - hsml[:])/grid_dx + 0.5)
    gymax = np.int_((shift_coords[:,1] + hsml[:])/grid_dx)
    for i in xrange(N):
        h = hsml[i]
        mh2 = data[i,:]/h**2
    
        if gxmin[i] < 0:
            gxmin[i] = 0
        if gxmax[i] > gridres - 1:
            gxmax[i] = gridres - 1
        if gymin[i] < 0:
            gymin[i] = 0
        if gymax[i] > gridres - 1:
            gymax[i] = gridres - 1

        for gx in xrange(gxmin[i], gxmax[i]+1):
            for gy in xrange(gymin[i], gymax[i]+1):
                q = np.sqrt((shift_coords[i,0] - gx*grid_dx)**2 + (shift_coords[i,1] - gy*grid_dx)**2)/h
                if q <= 0.5:
                    griddata[gy, gx,:] += (1 - 6*q**2 + 6*q**3) * mh2
                elif q <= 1.0:
                    griddata[gy, gx,:] += (2*(1-q)**3) * mh2

    griddata[:] = norm*griddata[:]
Example #16
0
def get_many_patches(image, patch_shape, centers,
                     flat=True, step=1, force_pure_python=False):
    """Return the patches at given centers"""
    
    patch_shape = tuple(patch_shape)
    centers = np.reshape(np.asarray(centers, dtype=np.int_), (-1, len(patch_shape)))
    
    ndims = len(patch_shape)
    if ndims in [2,3] and "_get_many_patches" in globals() and not force_pure_python:
        # 3d version (efficient Cython implementation)
        patches = _get_many_patches(ndims, image, patch_shape, centers, step)
    else:
        # Extract patches (pure Python version)
        grid_slices = tuple(slice(-(i//2), i-i//2, step) for i in patch_shape)
        grid = np.reshape(np.mgrid[grid_slices], (len(patch_shape), -1))
        points = tuple(np.int_(centers.T[:,:,np.newaxis]) + np.int_(grid[:,np.newaxis,:]))
        patches = image[points]
    
    # Compute the final patch shape taking into acount the step
    final_shape = tuple((sh - 1)/step + 1 for sh in patch_shape)
    
    channels = image.shape[len(patch_shape):]
    if not flat:
        patches = np.reshape(patches, (-1,) + tuple(final_shape) + channels)
    else:
        patches = np.reshape(patches, (len(patches), np.prod(final_shape + channels)))
    return patches
Example #17
0
    def draw(self, dt):
        if self._mixer.is_onset():
            self._offset_z += self.parameter('beat-color-boost').get()

        angle = self.parameter('angle').get()
        #self._offset_x += dt * self.parameter('speed').get() * math.cos(angle) * 2 * math.pi
        #self._offset_y += dt * self.parameter('speed').get() * math.sin(angle) * 2 * math.pi
        self._offset_x += dt * self.parameter('speed').get()
        self._offset_z += dt * self.parameter('color-speed').get()
        posterization = self.parameter('resolution').get()

        rotMatrix = np.array([(math.cos(angle), -math.sin(angle)), (math.sin(angle),  math.cos(angle))])
        x,y = rotMatrix.T.dot(self.pixel_locations.T)
        x *= self.parameter('stretch').get()
        x += self._offset_x
        y += self._offset_y
        locations = np.asarray([x,y]).T

        hues = np.asarray([snoise3(self.scale * location[0], \
                                   self.scale * location[1], \
                                   self._offset_z, 1, 0.5, 0.5) for location in locations])
        hues = (1.0 + hues) / 2
        hues = self.hue_min + ((np.int_(hues * posterization) / float(posterization)) * (self.hue_max - self.hue_min))
        brights = np.asarray([snoise3(self.luminance_scale * location[0], self.luminance_scale * location[1], self._offset_z, 1, 0.5, 0.5) for location in locations])
        brights = (1.0 + brights) / 2
        brights *= self._luminance_steps
        luminances = self.lum_fader.color_cache[np.int_(brights)].T[1]

        self.setAllHLS(hues, luminances, 1.0)
Example #18
0
def DepositDataToGrid3D(data, coords, N, hsml, gridres, rmax, griddata):
    norm =  2.5464790894703255 #8/np.pi for 3D
    grid_dx = 2*rmax/(gridres-1)
    zSqr = coords[:,2]*coords[:,2]
    hsml_plane = np.sqrt(hsml[:]*hsml[:] - zSqr)
    shift_coords = coords[:,:2] + rmax
    
    gxmin = np.int_((shift_coords[:,0] - hsml_plane[:])/grid_dx + 0.5)
    gxmax = np.int_((shift_coords[:,0] + hsml_plane[:])/grid_dx)
    gymin = np.int_((shift_coords[:,1] - hsml_plane[:])/grid_dx + 0.5)
    gymax = np.int_((shift_coords[:,1] + hsml_plane[:])/grid_dx)
    
    for i in xrange(N):
        h = hsml[i]
        mh3 = data[i,:]/h**3
        z2 = zSqr[i]
    
        if gxmin[i] < 0:
            gxmin[i] = 0
        if gxmax[i] > gridres - 1:
            gxmax[i] = gridres - 1
        if gymin[i] < 0:
            gymin[i] = 0
        if gymax[i] > gridres - 1:
            gymax[i] = gridres - 1
        for gx in xrange(gxmin[i], gxmax[i]+1):
            for gy in xrange(gymin[i], gymax[i]+1):
                q = np.sqrt((shift_coords[i,0] - gx*grid_dx)**2 + (shift_coords[i,1] - gy*grid_dx)**2 + z2)/h
                if q <= 0.5:
                    griddata[gy, gx,:] += (1 - 6*q**2 + 6*q**3) * mh3
                elif q <= 1.0:
                    griddata[gy, gx,:] += (2*(1-q)**3) * mh3

    griddata[:] = norm*griddata[:]
Example #19
0
 def K(self, X, X2=None):
     #This way is not working, indexes are lost after using k._slice_X
     #index = np.asarray(X, dtype=np.int)
     #index = index.reshape(index.size,)
     if hasattr(X, 'values'):
         X = X.values
     index = np.int_(X[:, 1])
     index = index.reshape(index.size,)
     X_flag = index[0] >= self.output_dim
     if X2 is None:
         if X_flag:
             #Calculate covariance function for the latent functions
             index -= self.output_dim
             return self._Kuu(X, index)
         else:
             raise NotImplementedError
     else:
         #This way is not working, indexes are lost after using k._slice_X
         #index2 = np.asarray(X2, dtype=np.int)
         #index2 = index2.reshape(index2.size,)
         if hasattr(X2, 'values'):
             X2 = X2.values
         index2 = np.int_(X2[:, 1])
         index2 = index2.reshape(index2.size,)
         X2_flag = index2[0] >= self.output_dim
         #Calculate cross-covariance function
         if not X_flag and X2_flag:
             index2 -= self.output_dim
             return self._Kfu(X, index, X2, index2) #Kfu
         else:
             index -= self.output_dim
             return self._Kfu(X2, index2, X, index).T #Kuf
Example #20
0
def get_data_set():
    data_set = [] 
    categories = {}
    next_cat_index = 1
    first_line = True
    with open ('original_data/numerai_training_data.csv', 'r') as csvfile:
        spamreader = csv.reader(csvfile)
        for row in spamreader:
            if not first_line: #Skip first line
                data_set_item = np.int_(row[0:14]).astype(np.int)
                category = row[14]
                try:
                    #if KeyError add a new category
                    cat_index = categories[category] 
                except KeyError as e:
                    categories[category] = next_cat_index
                    next_cat_index += 1
                    cat_index = categories[category]
                data_set_item = np.append(data_set_item, np.int_([cat_index]))
                data_set_item = np.append(data_set_item, np.int_(row[15:]).astype(np.int))
                data_set.append(data_set_item)
            else:
                first_line = False 
    data_set = np.int_(data_set)
    return (data_set, categories)
Example #21
0
File: kk.py Project: pyIPP/pykk
    def rhopol_to_rhotor(self, time, rhopol):
        N = numpy.size(rhopol)
        if self.__status:
            error = ctypes.c_int(0)
            _error = ctypes.byref(error)
            _shotnumber = ctypes.byref(self.__shotnumber)
            _edition = ctypes.byref(self.__edition)
            t = ctypes.c_float( time )
            _t = ctypes.byref( t )
            
            if N == 1:
                rhopf = ctypes.c_float(rhopol) if isinstance(rhopol, float) else ctypes.c_float(rhopol[0]) 
                rhot = ctypes.c_float(0)
                pf = ctypes.c_float(0)
                fpf = ctypes.c_float(0)
                ftf = ctypes.c_float(0)
            else:
                rhopf = (ctypes.c_float*N)()
                rhot = (ctypes.c_float*N)()
                pf = (ctypes.c_float*N)()
                fpf = (ctypes.c_float*N)()
                ftf = (ctypes.c_float*N)()
                for i in range(N):
                    rhopf[i] = rhopol[i]
                    rhot[i] = 0
                    pf[i] = 0
                    fpf[i] = 0
                    ftf[i] = 0
            _rhopf = ctypes.byref(rhopf)
            _rhot = ctypes.byref(rhot)
            _pf = ctypes.byref(pf)
            lrho = ctypes.c_int(N)
            _lrho = ctypes.byref(lrho)
            _fpf = ctypes.byref(fpf)
            _ftf = ctypes.byref(ftf)
                    
            lexper = ctypes.c_long(len(self.experiment))
            ldiag = ctypes.c_long(3)
            
            libkk.kkrhopto_(_error, self.__exper,self.__diag,_shotnumber,_edition,_t,\
            _rhopf, _lrho,\
            _rhot,_fpf,_ftf,\
            lexper, ldiag)

            #kkrhopto (&error,exp,diag,&shot,&edition, &time,
            #rhopf, &lrho,
            #rhopf, fpf, ftf , strlen(exp), strlen(diag) );
                    
            if N == 1:
                return {'error' : numpy.int_(error),\
                'time'  : numpy.float32(t),\
                'rhotor'     : numpy.float32(rhot),\
                'fpf'     : numpy.float32(fpf),\
                'ftf': numpy.float32(ftf)}
            else:
                return {'error' : numpy.int_(error),\
                'time'  : numpy.float32(t),\
                'rhotor'     : numpy.array(rhot, dtype=float),\
                'fpf'     : numpy.array(fpf, dtype=float),\
                'ftf': numpy.array(ftf, dtype=float)}    
Example #22
0
def create_BP_mask(dataset, num_MP, feat_dim):
    '''
    Input: list of 2D array of size timesteps x input_dim
    Output: list of 2D array of size timesteps x output_dim, which only takes entry from part_index. For each frame t, [..., x(t-sampling_rate), x(t), x(t+sampling_rate),...], which contains window_size frames, is computed as output for frame t.
    '''
    
    file_info = os.path.expanduser("~/work/Data/{}/{}_info.mat".format(dataset,dataset))    
    contents = sio.loadmat(file_info)
    BP_info = contents['config'][0]
    dim_per_frame=60 # 3(xyz) * 20joints
    if dataset=='MHAD':
        dim_per_frame=105 # 3(xyz) * 35 joints
    elif dataset =="HDM05":
        dim_per_frame =93
    elif dataset =="CAD120":
        dim_per_frame=45
    n_frame = feat_dim/dim_per_frame
    mask_all = []
    for part_index in BP_info: 

        part_index = np.int_(part_index[0])
        entry_index = np.concatenate([(part_index-1)*3, (part_index-1)*3+1, (part_index-1)*3+2])
        entry_index = np.sort(entry_index)
        entry_index_all=[]
        for i in range(n_frame):
            entry_index_all = np.append(entry_index_all, [entry_index+dim_per_frame*i])
        entry_index_all=np.int_(np.array(entry_index_all))    
        mask = np.zeros((feat_dim,num_MP),dtype='float32')
        mask[entry_index_all,:]=1.0
        mask_all.append(mask)
    mask_all = np.concatenate(mask_all, axis=1)
    
    return mask_all
Example #23
0
def draw_hough_line(image, dist, theta, color=0):
    """
    Draws a line described by the hough transform to an image

    :param image: Image to draw on
    :param dist: Hough transform distance
    :param theta: Hough transform angle
    :param color: intensity to draw line
    """

    rows, cols = image.shape

    if abs(theta) < pi/4:
        # Find the x (col) intercepts
        x0 = int_(dist/cos(theta))
        x1 = int_(x0 - rows * sin(theta))
        intercepts = (0, x0, rows, x1)

    else:
        # Find the y (row) intercepts
        y0 = int_(dist/sin(theta))
        y1 = int_(y0 + cols * cos(theta))
        intercepts = (y0, 0, y1, cols)

    r, c = line(*intercepts)

    # Check to make sure each point stays in the image bounds and draw it
    for n in range(r.size):
        if r[n] >= 0 and c[n] >= 0:
            if r[n] < rows and c[n] < cols:
                image[r[n], c[n]] = color
Example #24
0
 def gradients_X(self, dL_dK, X, X2=None):
     #index = np.asarray(X, dtype=np.int)
     #index = index.reshape(index.size,)
     if hasattr(X, 'values'):
         X = X.values
     index = np.int_(X[:, 1])
     index = index.reshape(index.size,)
     X_flag = index[0] >= self.output_dim
     #If input_dim == 1, use this
     #gX = np.zeros((X.shape[0], 1))
     #Cheat to allow gradient for input_dim==2
     gX = np.zeros(X.shape)
     if X2 is None: #Kuu or Kmm
         if X_flag:
             index -= self.output_dim
             gX[:, 0] = 2.*(dL_dK*self._gkuu_X(X, index)).sum(0)
             return gX
         else:
             raise NotImplementedError
     else: #Kuf or Kmn
         #index2 = np.asarray(X2, dtype=np.int)
         #index2 = index2.reshape(index2.size,)
         if hasattr(X2, 'values'):
             X2 = X2.values
         index2 = np.int_(X2[:, 1])
         index2 = index2.reshape(index2.size,)
         X2_flag = index2[0] >= self.output_dim
         if X_flag and not X2_flag: #gradient of Kuf(Z, X) wrt Z
             index -= self.output_dim
             gX[:, 0] = (dL_dK*self._gkfu_z(X2, index2, X, index).T).sum(1)
             return gX
         else:
             raise NotImplementedError
Example #25
0
def EField(X,Q,gamma,nxyz):
    N=X.shape[0];
    X[:,2]=X[:,2]*gamma
    XX=np.max(X,axis=0)-np.min(X,axis=0)
    XX=XX*np.random.uniform(low=1.0,high=1.1)
    print 'mesh steps:', XX
    steps=XX/(nxyz-3)
    X=X/steps
    X_min=np.min(X,axis=0)
    X_mid=np.dot(Q,X)/np.sum(Q);
    X_off=np.floor(X_min-X_mid)+X_mid;
    X=X-X_off  
    nx=nxyz[0];ny=nxyz[1];nz=nxyz[2];nzny=nz*ny
    Xi=np.int_(np.floor(X)+1)
    inds=np.int_(Xi[:,0]*nzny+Xi[:,1]*nz+Xi[:,2]) # 3d -> 1d
    print inds.shape, nxyz
    q=np.bincount(inds,Q,nzny*nx).reshape(nxyz)
    p=Phi(q,steps)
    Ex=np.zeros(p.shape);Ey=np.zeros(p.shape);Ez=np.zeros(p.shape);
    Ex[:nx-1,:,:]=(p[:nx-1,:,:]-p[1:nx,:,:])/steps[0]
    Ey[:,:ny-1,:]=(p[:,:ny-1,:]-p[:,1:ny,:])/steps[1]
    Ez[:,:,:nz-1]=(p[:,:,:nz-1]-p[:,:,1:nz])/steps[2]
    Exyz=np.zeros((N,3))
    Exyz[:,0]=ndimage.map_coordinates(Ex,np.c_[X[:,0],X[:,1]+0.5,X[:,2]+0.5].T,order=1)*gamma
    Exyz[:,1]=ndimage.map_coordinates(Ey,np.c_[X[:,0]+0.5,X[:,1],X[:,2]+0.5].T,order=1)*gamma
    Exyz[:,2]=ndimage.map_coordinates(Ez,np.c_[X[:,0]+0.5,X[:,1]+0.5,X[:,2]].T,order=1)
    return Exyz
Example #26
0
def EField(X,Q,gamma,kern,steps):
    N=X.shape[0];
    X[:,2]=X[:,2]*gamma
    X=X/steps
    X_min=np.min(X,axis=0)
    X_mid=np.dot(Q,X)/np.sum(Q);
    X_off=np.floor(X_min-X_mid)+X_mid;
    X=X-X_off  
    nx,ny,nz=np.int_(3+np.floor(np.max(X,axis=0)))
    nzny=nz*ny
    Xi=np.int_(np.floor(X)+1)
    inds=np.int_(Xi[:,0]*nzny+Xi[:,1]*nz+Xi[:,2]) # 3d -> 1d
    q=np.bincount(inds,Q,nzny*nx)
    print len(q), nx*ny*nz
    
    q=q.reshape(nx,ny,nz)
    #t0=time.time()   
    print q.shape, steps
    p,kern=Phi(q,kern,steps)
    #t1=time.time(); print t1-t0
    Ex=np.zeros(p.shape);Ey=np.zeros(p.shape);Ez=np.zeros(p.shape);
    Ex[:nx-1,:,:]=(p[:nx-1,:,:]-p[1:nx,:,:])/steps[0]
    Ey[:,:ny-1,:]=(p[:,:ny-1,:]-p[:,1:ny,:])/steps[1]
    Ez[:,:,:nz-1]=(p[:,:,:nz-1]-p[:,:,1:nz])/steps[2]
    Exyz=np.zeros((N,3))
    Exyz[:,0]=ndimage.map_coordinates(Ex,np.c_[X[:,0],X[:,1]+0.5,X[:,2]+0.5].T,order=1)*gamma
    Exyz[:,1]=ndimage.map_coordinates(Ey,np.c_[X[:,0]+0.5,X[:,1],X[:,2]+0.5].T,order=1)*gamma
    Exyz[:,2]=ndimage.map_coordinates(Ez,np.c_[X[:,0]+0.5,X[:,1]+0.5,X[:,2]].T,order=1)
    #t1=time.time();    print t1-t0
    return Exyz
Example #27
0
def _find_nearest_node_ndarray(rmg, coords, mode='raise'):
    """Find the node nearest to a point.

    Parameters
    ----------
    rmg : RasterModelGrid
        A RasterModelGrid.
    coords : tuple of float
        Coordinates of test points as *x*, then *y*.
    mode : {'raise', 'wrap', 'clip'}, optional
        What to do with out-of-bounds indices (as with
        numpy.ravel_multi_index).

    Returns
    -------
    ndarray
        Nodes that are closest to the points.

    Examples
    --------
    >>> from landlab.grid.raster_funcs import _find_nearest_node_ndarray
    >>> from landlab import RasterModelGrid
    >>> import numpy as np
    >>> grid = RasterModelGrid((4, 5))
    >>> _find_nearest_node_ndarray(grid, (.25, 1.25))
    5
    >>> _find_nearest_node_ndarray(grid, (.75, 2.25))
    11
    """
    column_indices = np.int_(
        np.around((coords[0] - rmg.node_x[0]) / rmg.node_spacing))
    row_indices = np.int_(
        np.around((coords[1] - rmg.node_y[0]) / rmg.node_spacing))

    return rmg.grid_coords_to_node_id(row_indices, column_indices, mode=mode)
Example #28
0
    def azimToBeam(self, azim):
        ''' Get azimuth of given beam.  Return a negative beam number (offset by
        one instead of zero) if the azimuth corresponds to the back lobe.
        Return np.nan if the azimuth is not covered by any beam.

        **Args**: 
            * **azim** (float): beam azimuth [deg. East]
        **Returns**:
            * **beam** (int): beam number
        '''
        import numpy as np

        # Assume the azimuth comes from the front lobe
        phi = np.radians(azim - self.boresite)
        delta = np.degrees(np.arctan2(np.sin(phi), np.cos(phi)))
        beam = np.round(delta / self.bmsep + (self.maxbeam - 1) / 2.)

        if beam < 0.0 or beam > self.maxbeam:
            # This azimuth lies outside the front lobe
            phi = np.radians(self.boresite - azim - 180.0)
            delta = np.degrees(np.arctan2(np.sin(phi), np.cos(phi)))
            beam = np.round(delta / self.bmsep + (self.maxbeam - 1) / 2.)

            # Seperate back lobe azimuths from azimuths outside of either
            # field-of-view
            if beam >= 0 and beam < self.maxbeam:
                beam = -np.int_(beam + 1)
            else:
                beam = np.nan
        else:
            beam = np.int_(beam)

        return beam
Example #29
0
    def check_numpy_scalar_argument_return_generic(self):
        f = PyCFunction('foo')
        f += Variable('a1', numpy.int_, 'in, out')
        f += Variable('a2', numpy.float_, 'in, out')
        f += Variable('a3', numpy.complex_, 'in, out')
        foo = f.build()
        args = 2, 1.2, 1+2j
        results = numpy.int_(2), numpy.float_(1.2), numpy.complex(1+2j)
        assert_equal(foo(*args),results)
        args = [2], [1.2], [1+2j]
        assert_equal(foo(*args),results)
        args = [2], [1.2], [1,2]
        assert_equal(foo(*args),results)

        f = PyCFunction('foo')
        f += Variable('a1', 'npy_int', 'in, out')
        f += Variable('a2', 'npy_float', 'in, out')
        f += Variable('a3', 'npy_complex', 'in, out')
        foo = f.build()
        args = 2, 1.2, 1+2j
        results = numpy.int_(2), numpy.float_(1.2), numpy.complex(1+2j)
        assert_equal(foo(*args),results)
        args = [2], [1.2], [1+2j]
        assert_equal(foo(*args),results)
        args = [2], [1.2], [1,2]
        assert_equal(foo(*args),results)
Example #30
0
def totalPower(latitude, timeTuple):
	global shell_normal
	global shell_faceO
	global shell_vertO
	matrixImport()
	month = timeTuple[1]
	day = timeTuple[2]
	hour = timeTuple[3]
	heading = 85 # Moving SSE
	shell_heading = heading
	shell_azimuths = 180/math.pi*numpy.arctan2(-shell_normal[:,1] ,shell_normal[:,0]) + heading
	shell_tilts = 90 - 180/math.pi*numpy.arcsin(shell_normal[:,2])
	a = shell_vertO[numpy.int_(shell_faceO[:,0]),:]
	b = shell_vertO[numpy.int_(shell_faceO[:,1]),:]
	c = shell_vertO[numpy.int_(shell_faceO[:,2]),:]
	v1 = b - a
	v2 = c - a
	temp = numpy.cross(v1,v2)**2
	temp = numpy.sum(temp, 1)
	shell_Area = 0.5*temp**0.5
	#shell_area = numpy.sum(shell_Area)
	shell_flux = incident_radiation(month, day, hour, shell_tilts, shell_azimuths, latitude)
	shell_power = numpy.dot(shell_flux,shell_Area)
	#shell_fluxavg = shell_power/shell_area
	#return shell_fluxavg
	return shell_power
Example #31
0
def get_pic(filename):
    """
    Open image file and convert to numpy array.
    Array is 3D containing [r,g,b] at each [x,y] coordinate.
    """
    return int_(asarray(Image.open(filename,'r')))
def pipeline(img, s_thresh=(190, 245), sx_thresh=(50, 170)):
    global brute_force
    global left_fit_hist
    global right_fit_hist

    # un distort the image
    img_undist = cv2.undistort(img, mtx, dist, None, mtx)
    # perspective transform the image
    img_warped = cv2.warpPerspective(img_undist,
                                     M,
                                     img_size,
                                     flags=cv2.INTER_LINEAR)
    # Convert to HLS color space and separate the V channel
    hls = cv2.cvtColor(img_warped, cv2.COLOR_BGR2HLS)
    l_channel = hls[:, :, 1]
    s_channel = hls[:, :, 2]
    # Sobel x
    sobelx = cv2.Sobel(l_channel, cv2.CV_64F, 1, 0)  # Take the derivative in x
    abs_sobelx = np.absolute(
        sobelx
    )  # Absolute x derivative to accentuate lines away from horizontal
    scaled_sobel = np.uint8(255 * abs_sobelx / np.max(abs_sobelx))

    # Threshold x gradient
    sxbinary = np.zeros_like(scaled_sobel)
    sxbinary[(scaled_sobel >= sx_thresh[0])
             & (scaled_sobel <= sx_thresh[1])] = 1

    # Threshold color channel
    s_binary = np.zeros_like(s_channel)
    s_binary[(s_channel >= s_thresh[0]) & (s_channel <= s_thresh[1])] = 1
    # Stack each channel
    color_binary = np.dstack(
        (np.zeros_like(sxbinary), sxbinary, s_binary)) * 255
    #cv2.imwrite('color_binary.jpg',color_binary)

    # Detect lane lines using sliding window
    leftx, lefty, rightx, righty, out_img1 = find_lane_pixels(
        color_binary, brute_force)

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

    ###### Using a forgetting factor to average over old lane line fits:
    ff = 0.7  #Forgetting factor

    # Generate x and y values for plotting
    ploty = np.linspace(0, color_binary.shape[0] - 1, color_binary.shape[0])
    try:
        # Check if left and right fit are none or incorrect
        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 left and right fit have worked, then set brute_force flag to false
        brute_force = False

    except TypeError:
        # Avoids an error if `left` and `right_fit` are still none or incorrect
        print('The function failed to fit a line!')
        # if lane lines weren't detected, set the lane lines to historic fit data for smooth results
        left_fitx = left_fit_hist[0] * ploty**2 + left_fit_hist[
            1] * ploty + left_fit_hist[2]
        right_fitx = right_fit_hist[0] * ploty**2 + right_fit_hist[
            1] * ploty + right_fit_hist[2]
        # set brute force flag to true for next lane line detection
        brute_force = True

    #### Sanity check if new lines found can be tursted.
    if brute_force == False:  #new line has been found, lets check for validity of new line found
        #check if lines are parallel by comparing the diff between the constant term
        if (right_fit[2] - left_fit[2]) < 550 or (
                right_fit[2] - left_fit[2]) > 850:  #Not parallel
            left_fitx = left_fit_hist[0] * ploty**2 + left_fit_hist[
                1] * ploty + left_fit_hist[2]
            right_fitx = right_fit_hist[0] * ploty**2 + right_fit_hist[
                1] * ploty + right_fit_hist[2]
            brute_force = True
        else:  #Lane line fit are parallel
            # Average the left and right fit using weighted avg with historic line data to smooth results
            if left_fit_hist.all() != 0 and right_fit_hist.all() != 0:
                left_fit = (left_fit + left_fit_hist * ff) / (1 + ff)
                right_fit = (right_fit + right_fit_hist * ff) / (1 + ff)

            left_fit_hist = left_fit
            right_fit_hist = right_fit

            #Calcualte the smoothed left_fitx and right fitx
            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]

    # Calculate radius of curvature
    left_curverad = curvature(left_fitx, ploty)
    right_curverad = curvature(right_fitx, ploty)

    curverad = (left_curverad + right_curverad) / 2

    #Calculate lane position
    xm_per_pix = 3.7 / 370  # m per pixel in the x direction
    lane_pos = ((right_fitx[-1] + left_fitx[-1]) / 2 -
                color_binary.shape[1] / 2) * xm_per_pix

    # Create an image to draw the lines on
    color_warp = np.zeros_like(color_binary).astype(np.uint8)
    #color_warp = np.dstack((warp_zero, warp_zero, warp_zero))

    # Recast the x and y points into usable format for cv2.fillPoly()
    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))

    # Draw the lane onto the warped blank image
    cv2.fillPoly(color_warp, np.int_([pts]), (0, 255, 0))

    # Warp the blank back to original image space using inverse perspective matrix (Minv)
    newwarp = cv2.warpPerspective(color_warp, Minv,
                                  (img.shape[1], img.shape[0]))
    # Combine the result with the original image
    result = cv2.addWeighted(img, 1, newwarp, 0.3, 0)
    cv2.putText(result, 'Radius of Curvature: {:.0f} m'.format(curverad),
                (50, 50), cv2.FONT_HERSHEY_SIMPLEX, 1, (255, 255, 255), 2)
    cv2.putText(result, 'Lane Position- off center: {:.2f} m'.format(lane_pos),
                (50, 100), cv2.FONT_HERSHEY_SIMPLEX, 1, (255, 255, 255), 2)
    #plt.imshow(result)

    return result
Example #33
0
def asr_calibrate(X,
                  sfreq,
                  cutoff=5,
                  blocksize=10,
                  win_len=0.5,
                  win_overlap=0.66,
                  max_dropout_fraction=0.1,
                  min_clean_fraction=0.25,
                  method='euclid'):
    """Calibration function for the Artifact Subspace Reconstruction method.

    The input to this data is a multi-channel time series of calibration data.
    In typical uses the calibration data is clean resting EEG data of ca. 1
    minute duration (can also be longer). One can also use on-task data if the
    fraction of artifact content is below the breakdown point of the robust
    statistics used for estimation (50% theoretical, ~30% practical). If the
    data has a proportion of more than 30-50% artifacts then bad time windows
    should be removed beforehand. This data is used to estimate the thresholds
    that are used by the ASR processing function to identify and remove
    artifact components.

    The calibration data must have been recorded for the same cap design from
    which data for cleanup will be recorded, and ideally should be from the
    same session and same subject, but it is possible to reuse the calibration
    data from a previous session and montage to the extent that the cap is
    placed in the same location (where loss in accuracy is more or less
    proportional to the mismatch in cap placement).

    The calibration data should have been high-pass filtered (for example at
    0.5Hz or 1Hz using a Butterworth IIR filter).

    Parameters
    ----------
    X : array, shape=([n_trials, ]n_channels, n_samples)
        *zero-mean* (e.g., high-pass filtered) and reasonably clean EEG of not
        much less than 30 seconds (this method is typically used with 1 minute
        or more).
    sfreq : float
        Sampling rate of the data, in Hz.

    The following are optional parameters (the key parameter of the method is
    the ``cutoff``):

    cutoff: float
        Standard deviation cutoff for rejection. X portions whose variance
        is larger than this threshold relative to the calibration data are
        considered missing data and will be removed. The most aggressive value
        that can be used without losing too much EEG is 2.5. A quite
        conservative value would be 5 (default=5).
    blocksize : int
        Block size for calculating the robust data covariance and thresholds,
        in samples; allows to reduce the memory and time requirements of the
        robust estimators by this factor (down to Channels x Channels x Samples
        x 16 / Blocksize bytes) (default=10).
    win_len : float
        Window length that is used to check the data for artifact content. This
        is ideally as long as the expected time scale of the artifacts but
        short enough to allow for several 1000 windows to compute statistics
        over (default=0.5).
    win_overlap : float
        Window overlap fraction. The fraction of two successive windows that
        overlaps. Higher overlap ensures that fewer artifact portions are going
        to be missed, but is slower (default=0.66).
    max_dropout_fraction : float
        Maximum fraction of windows that can be subject to signal dropouts
        (e.g., sensor unplugged), used for threshold estimation (default=0.1).
    min_clean_fraction : float
        Minimum fraction of windows that need to be clean, used for threshold
        estimation (default=0.25).
    method : {'euclid', 'riemann'}
        Metric to compute the covariance matric average.

    Returns
    -------
    M : array
        Mixing matrix.
    T : array
        Threshold matrix.

    """
    logging.debug('[ASR] Calibrating...')

    [nc, ns] = X.shape

    # window length for calculating thresholds
    N = int(np.round(win_len * sfreq))

    if method == 'euclid':
        U = np.zeros((blocksize, nc, nc))
        for k in range(blocksize):
            rangevect = np.minimum(ns - 1, np.arange(k, ns + k, blocksize))
            x = X[:, rangevect]
            U[k, ...] = x @ x.T
        Uavg = block_geometric_median(U.reshape((-1, nc * nc)) / blocksize, 2)
        Uavg = Uavg.reshape((nc, nc))
    elif method == 'riemann':
        blocksize = int(ns // blocksize)
        U = block_covariance(X, window=blocksize, overlap=win_overlap)
        Uavg = pyriemann.utils.mean.mean_covariance(U, metric='riemann')

    # get the mixing matrix M
    M = linalg.sqrtm(np.real(Uavg))
    D, Vtmp = linalg.eig(M)
    # D, Vtmp = nonlinear_eigenspace(M, nc)
    V = Vtmp[:, np.argsort(D)]

    # get the threshold matrix T
    x = np.abs(np.dot(V, X))
    offsets = np.int_(np.arange(0, ns - N, np.round(N * (1 - win_overlap))))

    mu = np.zeros(nc)
    sig = np.zeros(nc)
    for ichan in range(nc):
        rms = x[ichan, :]**2
        Y = []
        for o in offsets:
            Y.append(np.sqrt(np.sum(rms[o:o + N]) / N))

        mu[ichan], sig[ichan], alpha, beta = fit_eeg_distribution(
            Y, min_clean_fraction, max_dropout_fraction)

    T = np.dot(np.diag(mu + cutoff * sig), V.T)
    logging.debug('[ASR] Calibration done.')
    return M, T
Example #34
0
def age_consistency(data):
    """
    Construct age_head from agerange if available; otherwise use CPS value.
    Construct age_spouse as a normally-distributed agediff from age_head.
    """
    # set random-number-generator seed so that always get same random numbers
    np.random.seed(seed=123456789)
    # generate random integers to smooth age distribution in agerange
    shape = data['age_head'].shape
    agefuzz8 = np.random.randint(0, 9, size=shape)
    agefuzz9 = np.random.randint(0, 10, size=shape)
    agefuzz10 = np.random.randint(0, 11, size=shape)
    agefuzz15 = np.random.randint(0, 16, size=shape)

    # assign age_head using agerange midpoint or CPS age if agerange absent
    data['age_head'] = np.where(data['agerange'] == 0, data['age_head'],
                                (data['agerange'] + 1 - data['dsi']) * 10)

    # smooth the agerange-based age_head within each agerange
    data['age_head'] = np.where(
        np.logical_and(data['agerange'] == 1, data['dsi'] == 0),
        data['age_head'] - 3 + agefuzz9, data['age_head'])
    data['age_head'] = np.where(
        np.logical_and(data['agerange'] == 2, data['dsi'] == 0),
        data['age_head'] - 4 + agefuzz9, data['age_head'])
    data['age_head'] = np.where(
        np.logical_and(data['agerange'] == 3, data['dsi'] == 0),
        data['age_head'] - 5 + agefuzz10, data['age_head'])
    data['age_head'] = np.where(
        np.logical_and(data['agerange'] == 4, data['dsi'] == 0),
        data['age_head'] - 5 + agefuzz10, data['age_head'])
    data['age_head'] = np.where(
        np.logical_and(data['agerange'] == 5, data['dsi'] == 0),
        data['age_head'] - 5 + agefuzz10, data['age_head'])
    data['age_head'] = np.where(
        np.logical_and(data['agerange'] == 6, data['dsi'] == 0),
        data['age_head'] - 5 + agefuzz15, data['age_head'])
    data['age_head'] = np.where(
        np.logical_and(data['agerange'] == 1, data['dsi'] == 1),
        data['age_head'] - 0 + agefuzz8, data['age_head'])
    data['age_head'] = np.where(
        np.logical_and(data['agerange'] == 2, data['dsi'] == 1),
        data['age_head'] - 2 + agefuzz8, data['age_head'])
    data['age_head'] = np.where(
        np.logical_and(data['agerange'] == 3, data['dsi'] == 1),
        data['age_head'] - 4 + agefuzz10, data['age_head'])

    # convert zero age_head to one
    data['age_head'] = np.where(data['age_head'] == 0, 1, data['age_head'])

    # assign age_spouse relative to age_head if married;
    # if head is not married, set age_spouse to zero;
    # if head is married but has unknown age, set age_spouse to one;
    # do not specify age_spouse values below 15
    adiff = np.random.normal(0.0, 4.0, size=shape)
    agediff = np.int_(adiff.round())
    age_sp = data['age_head'] + agediff
    age_spouse = np.where(age_sp < 15, 15, age_sp)
    data['age_spouse'] = np.where(
        data['mars'] == 2, np.where(data['age_head'] == 1, 1, age_spouse), 0)
    return data
Example #35
0
def calculate_mean_face(database):
    # Calculating mean of all faces
    return np.int_(database.mean(0))
def main(argv):
    # print ('Number of arguments:', len(argv), 'arguments.')
    # print ('Argument List:', str(argv))
    contours_dir = "./data/panel/"
    rooftop_img_dir = "./panel/"
    rooftop_csv_path = './data/rooftop_solar_array_outlines_new.csv'
    rooftop_iou_csv_path = './rooftop_iou.csv'
    with open(rooftop_iou_csv_path, 'a') as csvfile:
        myFields = [
            'id', 'location_id', 'label', 'solar_list', 'contour_num', 'iou'
        ]
        writer = csv.DictWriter(csvfile, fieldnames=myFields)
        writer.writeheader()
    with open(rooftop_csv_path, newline='') as rooftop_csv_file:
        reader = csv.DictReader(rooftop_csv_file)
        for row in reader:
            roof = {}
            roof = row
            contour_mask = eval(row['contour_num'])
            # print(contour_mask)
            contour_img = np.zeros((800, 800, 3), np.uint8)
            for contour in contour_mask:
                contour_path = contours_dir + contour + '.png'
                # print(contour_path )
                img = cv2.imread(contour_path)
                # cv2.imshow('img', img)
                # cv2.waitKey(0)
                excluded_color = [0, 0, 0]
                indices_list = np.where(np.all(img != excluded_color, axis=-1))
                contour_img[indices_list] = [255, 255, 255]
            # cv2.imshow('img',contour_img)
            # cv2.waitKey(0)

            solar_mask = np.zeros((800, 800, 3), np.uint8)
            outline_list = eval(row['solar_list'])
            for outline in outline_list:
                # print(outline)
                pts = np.asarray(outline)
                cv2.fillPoly(solar_mask, np.int_([pts]), (255, 255, 255))
                # cv2.polylines(solar_mask, [pts], True, (0, 0, 255),  2)
            # cv2.imshow('img', solar_mask)
            # cv2.waitKey(0)
            # cv2.fillPoly(img_to_show, np.int_([pts]), (198, 133, 61))
            # cv2.fillPoly(img_to_show, np.int_([pts]), (255, 255, 255))
            #
            predict_gray_mask = cv2.cvtColor(contour_img, cv2.COLOR_BGR2GRAY)
            label_gray_mask = cv2.cvtColor(solar_mask, cv2.COLOR_BGR2GRAY)
            #
            # # rooftop_mask_size = cv2.countNonZero(rooftop_gray_mask)
            # # solar_mask_size = cv2.countNonZero(solar_gray_mask)
            # # size_ration = solar_mask_size / rooftop_mask_size
            # # print(rooftop_mask_size)
            # # print(solar_mask_size)
            # # print(size_ration)
            #
            # # IOU Score
            intersection = np.logical_and(predict_gray_mask, label_gray_mask)
            union = np.logical_or(predict_gray_mask, label_gray_mask)
            iou_score = np.sum(intersection) / np.sum(union)
            # print(iou_score)
            #
            # print(iou_score)
            #
            # # print(size_ration/iou_score)

            # cv2.imshow(row['id'], img_to_show)

            # hotkey()
            roof['iou'] = iou_score
            with open(rooftop_iou_csv_path, 'a') as csvfile_new:
                writer = csv.writer(csvfile_new)
                writer.writerow([
                    roof['id'], roof['location_id'], roof['label'],
                    roof['solar_list'], roof['contour_num'], roof['iou']
                ])
            csvfile_new.close()

    rooftop_csv_file.close()
def calculate_precip_timeseries(avg_interval, precip_varname, season,
                                file_time_resolution,
                                precip_data_input_directory):
    '''
    This function takes raw AOSMET precip data from GoAmazon site.
    It currently calculates an average according to avg_interval.
    The extract dates from filename section will need to be ammended based on the raw data files being extracted.
    Need to find a way to automate this more seamlessly among different ARM sites/datasets
    '''
    # timesteps_in_day
    timesteps_in_day = (1. / avg_interval) * 24

    #Load each individual file
    f = sorted(glob(precip_data_input_directory + '*.cdf'))  # Get file list
    length_fnames = len(f)

    # Create date file from filenames
    dts = []  # Create an empty list to hold dates
    dts_yymmdd = []

    # Extract datetimes from the filenames
    for i in f:
        if i[-10:-4] == 'custom':
            dts.append(dt.datetime.strptime(
                i[-26:-11], '%Y%m%d.%H%M%S'))  # Extract dates from filename
            dts_yymmdd.append(dt.datetime.strptime(
                i[-26:-18],
                '%Y%m%d'))  # Extract dates from filename in yymmdd format
        else:
            dts.append(dt.datetime.strptime(
                i[-19:-4], '%Y%m%d.%H%M%S'))  # Extract dates from filename
            dts_yymmdd.append(dt.datetime.strptime(
                i[-19:-11],
                '%Y%m%d'))  # Extract dates from filename in yymmdd format

    # Ensure that there no multiple files on the same day
    for i, j in enumerate(dts_yymmdd[:-1]):
        td = dts_yymmdd[i + 1] - dts_yymmdd[i]
        if td == 0:
            print(dts[i])
            print(
                'There are multiple files for the above date. Need to delete or combine files.'
            )
            return

    # Get start and end dates
    strt_date = dts_yymmdd[0]
    temp_date = strt_date
    end_date = dts_yymmdd[-1]

    # Find missing days. This includes days from the beginning of the year (if the series does not start at Jan 1st)
    # and days at the end of the year (if the series does not end on Dec. 31st)
    missing_dates = []  # Storing missing dates in this list
    continuous_dates = [
    ]  # Storing continuous dates in this list (dates available + missing dates)
    ctr = 0  # counter for debugging reasons

    # Fill in days beginning at Jan 1st to the start date
    if (temp_date.month, temp_date.day) != (1, 1):
        pretemp_date = dt.datetime(temp_date.year, 1, 1, 0, 0)
        while pretemp_date < temp_date:
            continuous_dates.append(pretemp_date)
            if (pretemp_date not in dts_yymmdd):
                missing_dates.append(pretemp_date)
            pretemp_date += dt.timedelta(days=1)
            ctr += 1

    # Make continuous dates and find missing dates from what is included in the files
    while temp_date <= end_date:
        continuous_dates.append(temp_date)
        if (temp_date not in dts_yymmdd):
            missing_dates.append(temp_date)
        temp_date += dt.timedelta(days=1)
        ctr += 1

    # Fill in days from end date to Dec 31st
    if (end_date.month, end_date.day) != (12, 31):
        post_end_date = dt.datetime(end_date.year, 12, 31, 0, 0)
        end_temp_date = end_date
        while end_temp_date < post_end_date:
            continuous_dates.append(end_temp_date)
            missing_dates.append(end_temp_date)
            end_temp_date += dt.timedelta(days=1)
            ctr += 1

    # file_time_resolution is inluced to indicate whether the .cdf file is for daily data or for an enitre year
    # we define a time factor here to help construct the timeseries
    if file_time_resolution == 'daily':
        time_factor = 1
        daily = True
    else:
        time_factor = 366  # 366 in case of leap yr
        daily = False

    # create array, H, that is either of the form (day, timestep) or (year, timestep), in the latter case
    # the array will be much longer

    H = np.zeros((int(len(f)), int(timesteps_in_day) * time_factor)) - 1

    # now loop through and average data between points in the days. Ex: if you choose 3 hr avg interval,
    # we look for data points between time bounds evenly spaced by 3 hr, starting at 00 hr, so points
    # between 0 and 3 hrs will be averaged as will 3 to 6 hrs, etc.
    for a, i in enumerate(f):

        f1 = Dataset(i, 'r')  # read in data

        if daily == False:
            tot_seconds = f1.variables['time'][-1]
            time_factor = np.ceil(
                tot_seconds / 60 / 60 /
                24)  # this will return eith either 365 or 366

        precip = f1.variables[precip_varname][:]  # precipitation in mm hr-1
        time = f1.variables[
            'time_offset'][:]  # seconds since yy-mm-dd 00:00:00 0:00
        step = 86400. / timesteps_in_day
        array_loop = np.arange(step, 86400 * time_factor + step, step)

        # now loop through and average data between points in the days. Ex: if you choose 3 hr avg interval,
        # we look for data points between time bounds evenly spaced by 3 hr, starting at 00 hr, so points
        # between 0 and 3 hrs will be averaged as will 3 to 6 hrs, etc.
        for j, k in enumerate(array_loop):
            if k == step:
                ind = np.where(time < k + 1)

            elif (k > step):
                ind = np.where(
                    np.logical_and(time > k + 1 - step, time < k + 1))

            ############ QUALITY CONTROL SHOULD BE PLACED HERE ################
            precip = np.array(precip)
            Q2 = precip[
                ind]  # find data points between the timestpes to average
            Q2[np.logical_or(Q2 < 0, Q2 > 100)] = np.nan
            H[a, j] = np.nanmean(Q2)  # store average value in the array

    #if season == 'annual':
    if (daily == True):
        Hnew = np.zeros(
            (int(len(continuous_dates)), int(timesteps_in_day) *
             time_factor))  # Create new array to fill in missing dates
        Hnew[:] = np.nan  # Initialize to nan

        #Fill in missing days with NaN by defining new array Hnew (Hnew = H + missing days)
        if len(missing_dates) > 0:
            for i, j in enumerate(
                    continuous_dates):  # loop through continuous dates
                if j in dts_yymmdd:
                    ind = np.int_([
                        a for a, b in enumerate(dts_yymmdd) if b == j
                    ])[0]  # find index of where j is in dts_yymmdd
                    Hnew[i, :] = H[ind, :]
                else:
                    Hnew[i, :] = np.nan

        elif len(missing_dates) == 0:
            Hnew = np.copy(H)

        precip_timeseries = Hnew.flatten()
    else:
        precip_timeseries = []  # store precip in here
        for i in np.arange(H.shape[0]):
            if np.sum(H[i, -int(timesteps_in_day):]) < 0:  # we initialized H
                # to have values of -1, if those values are still -1, then
                #the yr is not a leap year, so we store the first 365*steps_in_day points
                precip_timeseries = np.append(precip_timeseries,
                                              H[i, :-int(timesteps_in_day)])

            else:  # else this is a leap year and we store all points
                precip_timeseries = np.append(precip_timeseries, H[i, :])

#    else:
#        if (daily==True):
#
#            season_ind=np.zeros(H.shape[0],dtype='bool') # logical array for season indices
#            # define seasons
#            if season == 'DJF':
#                season_month = [12,1,2]
#            elif season == 'MAM':
#                season_month = [3,4,5]
#            elif season == 'JJA':
#                season_month = [6,7,8]
#            elif season == 'SON':
#                season_month = [9,10,11]
#
#
#            # loop through the dts_yymmdd and if the date is within the selected season, the logical array value for that index becomes true
#            for i in np.arange(len(dts_yymmdd)):
#                if dts_yymmdd[i].month in season_month:
#                    season_ind[i] = True
#
#            H[season_ind==False,:] =np.nan # change the matrix to nan where the values are not within the season
#            # loop through as before
#
#
#
#            Hnew=np.zeros((int(len(f)+len(missing_dates)),int(timesteps_in_day)*time_factor)) # Create new array to fill in missing dates
#            Hnew[:]=np.nan # Initialize to nan
#
#
#            #Fill in missing days with NaN by defining new array Hnew (Hnew = H + missing days)
#            if len(missing_dates)>0:
#                for i,j in enumerate(continuous_dates):
#
#                    if j in dts_yymmdd:
#                        ind=np.int_([a for a,b in enumerate(dts_yymmdd) if b == j])[0]
#                        Hnew[i,:]=H[ind,:]
#                    else:
#                        Hnew[i,:]=np.nan
#
#            elif len(missing_dates)==0:
#                Hnew=np.copy(H)
#
#            #Reshape Hnew into timeseries with length = timesteps_in_day*(length_fnames+length(missing_days)
#
#
#            precip_timeseries=Hnew.flatten()
#
#        else:
#            #seasonal filter in days
#            if season == 'DJF':
#                strt_ind = 0
#                end_ind = 31+31+28-1
#            elif season == 'MAM':
#                strt_ind = 31+31+28-1
#                end_ind = strt_ind + 31+30+31
#            elif season == 'JJA':
#                strt_ind = 31+31+28+31+30+31-1
#                end_ind = strt_ind + 30+31+31
#            elif season == 'SON':
#                strt_ind = 31+31+28+31+30+31+30+31+31-1
#                end_ind = strt_ind + 30+31+30
#
#            strt_ind    =int(timesteps_in_day*strt_ind)
#            end_ind     =int(timesteps_in_day*end_ind)
#
#
#           #seasonal filter, since rows of H are years, we use the indices above to filter each column, then store it
#
#            precip_timeseries = []
#            for i in np.arange(H.shape[0]):
#                if np.sum(H[i,-int(timesteps_in_day):])<0:
#                    if season == 'DJF':
#                        x = np.zeros(366*int(timesteps_in_day),dtype='bool')
#                        x[strt_ind:end_ind+1] = True
#                        H[i,x==False] = np.nan
#                        precip_timeseries = np.append(precip_timeseries,H[i,:])
#                    else:
#                        x = np.zeros(366*int(timesteps_in_day),dtype='bool')
#                        x[strt_ind+1:end_ind] = True
#                        H[i,x==False] = np.nan
#                        precip_timeseries = np.append(precip_timeseries,H[i,:])
#
#                else:
#                    x = np.zeros(366*int(timesteps_in_day),dtype='bool')
#                    x[strt_ind:end_ind] = True
#                    H[i,x==False] = np.nan
#                    precip_timeseries = np.append(precip_timeseries,H[i,:-int(timesteps_in_day)])

# create time bounds
    date_list = [
        continuous_dates[0] + dt.timedelta(hours=x) for x in np.arange(
            0,
            timesteps_in_day * len(continuous_dates) *
            avg_interval, avg_interval)
    ]
    time_uppr_bnd = np.roll(date_list, -1)
    time_uppr_bnd[-1] = time_uppr_bnd[-2] + dt.timedelta(seconds=step)
    time_bnds = np.stack((np.asarray(time_uppr_bnd), np.asarray(date_list)))

    if season != 'annual':
        season_ind = np.zeros(precip_timeseries.shape[0],
                              dtype='bool')  # logical array for season indices
        # define seasons
        if season == 'DJF':
            season_month = [12, 1, 2]
        elif season == 'MAM':
            season_month = [3, 4, 5]
        elif season == 'JJA':
            season_month = [6, 7, 8]
        elif season == 'SON':
            season_month = [9, 10, 11]

        # loop through the dts_yymmdd and if the date is within the selected season, the logical array value for that index becomes true
        for i in np.arange(len(date_list)):
            if date_list[i].month in season_month:
                season_ind[i] = True
        precip_timeseries[season_ind == False] = np.nan

    return precip_timeseries, time_bnds
def calculate_MWR_interp(avg_interval, cwv_varname_mwr, window_length,
                         hours_minus_GMT, season, cwv_data_input_directory):
    '''
    This function takes raw MWR column integrated water vapor (CWV) data.
    It calculates an average according to avg_interval and linearly
    interpolates over missing data periods less than those specified by
    window_length (hours)
    '''

    # timesteps_in_day
    timesteps_in_day = (1. / avg_interval) * 24

    # window_length is the length (in hours) of the window over which one
    # wishes to linearly interpolate the CWV data

    window = timesteps_in_day / (24. / window_length)

    #Load each individual file
    f = glob(cwv_data_input_directory + '*.cdf')  # Get file list
    length_fnames = len(f)

    # Create date file from filenames
    dts = []  # Create an empty list to hold dates
    dts_yymmdd = []
    #     f=iter(f) # Make list iterable

    for i in f:
        if i[-10:-4] == 'custom':
            dts.append(dt.datetime.strptime(
                i[-26:-11], '%Y%m%d.%H%M%S'))  # Extract dates from filename
            dts_yymmdd.append(dt.datetime.strptime(
                i[-26:-18],
                '%Y%m%d'))  # Extract dates from filename in yymmdd format
        else:
            dts.append(dt.datetime.strptime(
                i[-19:-4], '%Y%m%d.%H%M%S'))  # Extract dates from filename
            dts_yymmdd.append(dt.datetime.strptime(
                i[-19:-11],
                '%Y%m%d'))  # Extract dates from filename in yymmdd format

    ## Ensure that there no multiple files on the same day
    for i, j in enumerate(dts_yymmdd[:-1]):
        td = dts_yymmdd[i + 1] - dts_yymmdd[i]
        if td == 0:
            print(dts[i])
            print(
                'There are multiple files for the above date. Need to delete or combine files.'
            )
            return

    # Get start and end dates
    strt_date = dts_yymmdd[0]
    temp_date = strt_date
    end_date = dts_yymmdd[-1]

    # Find missing days
    missing_dates = []  # Storing missing dates in this list
    continuous_dates = []
    ctr = 0
    while temp_date <= end_date:
        continuous_dates.append(temp_date)
        if (temp_date not in dts_yymmdd):
            missing_dates.append(temp_date)
        temp_date += dt.timedelta(days=1)
        ctr += 1

    #Define days to ignore afternoon data due to problems around equinox
    #Ignore data in the 30 days surrounding equinox

    yy_strt = dts_yymmdd[0].timetuple().tm_year
    yy_end = dts_yymmdd[-1].timetuple().tm_year
    year_diff = yy_end - yy_strt
    equinox = []

    while yy_strt <= yy_end:

        d01, d02 = dt.datetime(yy_strt, 3, 6), dt.datetime(yy_strt, 4,
                                                           4)  # Spring equinox
        d11, d12 = dt.datetime(yy_strt, 9, 6), dt.datetime(yy_strt, 10,
                                                           5)  # Autumn equinox

        while d01 <= d02:
            equinox.append(d01)
            d01 += dt.timedelta(days=1)

        while d11 <= d12:
            equinox.append(d11)
            d11 += dt.timedelta(days=1)
        yy_strt += 1

    H = np.zeros((int(len(f)), int(timesteps_in_day)))
    H[:] = np.nan  # Initialize to nan

    #Read in time and cwv
    for a, i in enumerate(f):

        f1 = Dataset(i, 'r')
        cwv = f1.variables[cwv_varname_mwr][:]  # column vapor in cm
        time = f1.variables[
            'time_offset'][:]  # seconds since yy-mm-dd 00:00:00 0:00
        TB23 = f1.variables['tbsky23'][:]  # sky brightness temperature in K
        step = 86400. / timesteps_in_day
        array_loop = np.arange(step, 86400 + step, step)

        for j, k in enumerate(array_loop):

            if k == step:
                ind = np.where(time < k + 1)

            elif (k > step):
                ind = np.where(
                    np.logical_and(time > k + 1 - step, time < k + 1))

            Q = cwv[ind]
            TB23_Q = TB23[ind]

            #Ridding of erroneous data
            #Rids of -9999 (missing data)
            #Q[Q==-9999]=np.nan
            #Q[Q==0]=np.nan
            #Q[TB23_Q>100]=np.nan

            #Rids of erroneous data that occur surrounding solstices and
            #equinoxes from 11 am to 2 pm local time

            # If the day is within +/- 30 days of equinox
            if dts_yymmdd[a] in equinox:
                # Get local time
                dates_day = [
                    dts_yymmdd[a] + dt.timedelta(seconds=z) -
                    dt.timedelta(hours=hours_minus_GMT) for z in time[ind]
                ]
                ind_eq = np.int_([
                    y for y, z in enumerate(dates_day)
                    if (z.timetuple().tm_hour >= 11)
                    & (z.timetuple().tm_hour <= 14)
                ])
                Q[ind_eq] = np.nan

            #H[a,j]=np.nanmean(Q)

            if H[a, j] <= 0:
                H[a, j] = np.nan

    Hnew = np.zeros(
        (int(len(f) + len(missing_dates)),
         int(timesteps_in_day)))  # Create new array to fill in missing dates
    Hnew[:] = np.nan  # Initialize to nan

    #Fill in missing days with NaN by defining new array Hnew (Hnew = H + missing days)

    if len(missing_dates) > 0:
        for i, j in enumerate(continuous_dates):
            if j in dts_yymmdd:
                ind = np.int_([a for a, b in enumerate(dts_yymmdd)
                               if b == j])[0]
                Hnew[i, :] = H[ind, :]
            else:
                Hnew[i, :] = np.nan

    elif len(missing_dates) == 0:
        Hnew = np.copy(H)

    #Reshape Hnew into timeseries with length = timesteps_in_day*(length_fnames+length(missing_days)
    cwv_timeseries_raw = Hnew.flatten()

    #Do linear interpolation over pre-defined window of NaN (defined in the
    #first part of the code)
    nan_idx = np.isnan(cwv_timeseries_raw)
    nan_ind = np.where(np.isnan(cwv_timeseries_raw))[0]  #
    nan_window = []

    # Get the lengths of NaN blocks. Ignore if length > specified window length
    for l, m in groupby(enumerate(nan_ind), lambda x: x[1] - x[0]):
        temp = (list(map(itemgetter(1), m)))

        if len(temp) <= window_length / avg_interval:
            nan_window += temp

    nan_window = (np.asarray(nan_window, dtype=int))
    cwv_timeseries_interp = np.copy(cwv_timeseries_raw)
    indices = np.arange(0, cwv_timeseries_raw.size)
    not_nan = np.logical_not(np.isnan(cwv_timeseries_raw))
    cwv_timeseries_interp[nan_window] = np.interp(indices[nan_window],
                                                  indices[not_nan],
                                                  cwv_timeseries_raw[not_nan])

    return cwv_timeseries_interp
def calculate_MWRRET_interp(avg_interval, window_length, season,
                            cwv_data_input_directory):
    '''
    This function takes raw MWRRET column integrated water vapor (CWV) data.
    It calculates an average according to avg_interval and linearly
    interpolates over missing data periods less than those specified by
    window_length (hours)
    '''

    # timesteps_in_day
    timesteps_in_day = (1. / avg_interval) * 24

    # window_length is the length (in hours) of the window over which one
    # wishes to linearly interpolate the CWV data
    window = timesteps_in_day / (24. / window_length)

    #Load each individual file
    f = sorted(glob(cwv_data_input_directory + '*.cdf'))  # Get file list
    length_fnames = len(f)

    # Create date file from filenames
    dts = []  # Create an empty list to hold dates
    dts_yymmdd = []

    # Extract datetimes from the filenames
    for i in f:
        if i[-10:-4] == 'custom':
            dts.append(dt.datetime.strptime(
                i[-26:-11], '%Y%m%d.%H%M%S'))  # Extract dates from filename
            dts_yymmdd.append(dt.datetime.strptime(
                i[-26:-18],
                '%Y%m%d'))  # Extract dates from filename in yymmdd format
        else:
            dts.append(dt.datetime.strptime(
                i[-19:-4], '%Y%m%d.%H%M%S'))  # Extract dates from filename
            dts_yymmdd.append(dt.datetime.strptime(
                i[-19:-11],
                '%Y%m%d'))  # Extract dates from filename in yymmdd format

    # Ensure that there no multiple files on the same day
    for i, j in enumerate(dts_yymmdd[:-1]):
        td = dts_yymmdd[i + 1] - dts_yymmdd[i]
        if td == 0:
            print(dts[i])
            print(
                'There are multiple files for the above date. Need to delete or combine files.'
            )
            return

    # Get start and end dates
    strt_date = dts_yymmdd[0]
    temp_date = strt_date
    end_date = dts_yymmdd[-1]

    # Find missing days. This includes days from the beginning of the year (if the series does not start at Jan 1st)
    # and days at the end of the year (if the series does not end on Dec. 31st)
    missing_dates = []  # Storing missing dates in this list
    continuous_dates = [
    ]  # Storing continuous dates in this list (dates available + missing dates)
    ctr = 0  # counter for debugging reasons

    # Fill in days beginning at Jan 1st to the start date
    if (temp_date.month, temp_date.day) != (1, 1):
        pretemp_date = dt.datetime(temp_date.year, 1, 1, 0, 0)
        while pretemp_date < temp_date:
            continuous_dates.append(pretemp_date)
            if (pretemp_date not in dts_yymmdd):
                missing_dates.append(pretemp_date)
            pretemp_date += dt.timedelta(days=1)
            ctr += 1

    # Make continuous dates and find missing dates from what is included in the files
    while temp_date <= end_date:
        continuous_dates.append(temp_date)
        if (temp_date not in dts_yymmdd):
            missing_dates.append(temp_date)
        temp_date += dt.timedelta(days=1)
        ctr += 1

    # Fill in days from end date to Dec 31st
    if (end_date.month, end_date.day) != (12, 31):
        post_end_date = dt.datetime(end_date.year, 12, 31, 0, 0)
        end_temp_date = end_date
        while end_temp_date < post_end_date:
            continuous_dates.append(end_temp_date)
            missing_dates.append(end_temp_date)
            end_temp_date += dt.timedelta(days=1)
            ctr += 1

    # define some matrix H, which we will fill in with the cwv data
    H = np.zeros((int(len(f)), int(timesteps_in_day)))
    H[:] = np.nan  # Initialize to nan

    #Read in time and cwv
    for a, i in enumerate(f):

        f1 = Dataset(i, 'r')  # read in file data
        cwv = f1.variables['be_pwv'][:]  # column vapor in cm
        time = f1.variables[
            'time_offset'][:]  # seconds since yy-mm-dd 00:00:00 0:00

        step = 86400. / timesteps_in_day  # timestep
        array_loop = np.arange(step, 86400 + step,
                               step)  # the timestep intervals in the day

        # now loop through and average data between points in the days. Ex: if you choose 3 hr avg interval,
        # we look for data points between time bounds evenly spaced by 3 hr, starting at 00 hr, so points
        # between 0 and 3 hrs will be averaged as will 3 to 6 hrs, etc.
        for j, k in enumerate(array_loop):

            if k == step:
                ind = np.where(time < k + 1)

            elif (k > step):
                ind = np.where(
                    np.logical_and(time > k + 1 - step, time < k + 1))

            ############ QUALITY CONTROL SHOULD BE PLACED HERE ################
            cwv = np.array(cwv)  # cwv as array
            Q1 = cwv[
                ind]  # indices defined above, points between the timesteps
            Q1 = np.array(Q1)
            Q1[Q1 <= 0] = np.nan  # no negative cwv, nan it
            H[a, j] = np.nanmean(
                Q1)  # average the points together and store them in H

    Hnew = np.zeros(
        (int(len(f) + len(missing_dates)),
         int(timesteps_in_day)))  # Create new array to fill in missing dates
    Hnew[:] = np.nan  # Initialize to nan

    # if we filter by season
    if season != 'annual':
        season_ind = np.zeros(H.shape[0],
                              dtype='bool')  # logical array for season indices
        # define seasons
        if season == 'DJF':
            season_month = [12, 1, 2]
        elif season == 'MAM':
            season_month = [3, 4, 5]
        elif season == 'JJA':
            season_month = [6, 7, 8]
        elif season == 'SON':
            season_month = [9, 10, 11]

        # loop through the dts_yymmdd and if the date is within the selected season, the logical array value for that index becomes true
        for i in np.arange(len(dts_yymmdd)):
            if dts_yymmdd[i].month in season_month:
                season_ind[i] = True

        H[season_ind ==
          False, :] = np.nan  # change the matrix to nan where the values are not within the season
        # loop through as before

    if len(missing_dates) > 0:
        for i, j in enumerate(
                continuous_dates):  # loop through continuous dates

            if j in dts_yymmdd:  # if continuous date is in a data file:
                ind = np.int_([a for a, b in enumerate(dts_yymmdd) if b == j
                               ])[0]  # find index of where j is in dts_yymmdd
                Hnew[i, :] = H[ind, :]  # fill in the new matrix with the value
            else:
                Hnew[
                    i, :] = np.nan  # else the new matrix has a nan value where the continuous date does not exist in the files

    elif len(missing_dates) == 0:
        Hnew = np.copy(H)

    cwv_timeseries_raw = Hnew.flatten()

    #Do linear interpolation over pre-defined window of NaN (defined in the
    #first part of the code)
    nan_idx = np.isnan(cwv_timeseries_raw)
    nan_ind = np.where(np.isnan(cwv_timeseries_raw))[0]  #
    nan_window = []

    # Get the lengths of NaN blocks. Ignore if length > specified window length
    for l, m in groupby(enumerate(nan_ind), lambda x: x[1] - x[0]):
        temp = (list(map(itemgetter(1), m)))

        if len(temp) <= window_length / avg_interval:
            nan_window += temp

    nan_window = (np.asarray(nan_window, dtype=int))
    cwv_timeseries_interp = np.copy(cwv_timeseries_raw)
    indices = np.arange(0, cwv_timeseries_raw.size)
    not_nan = np.logical_not(np.isnan(cwv_timeseries_raw))
    cwv_timeseries_interp[nan_window] = np.interp(indices[nan_window],
                                                  indices[not_nan],
                                                  cwv_timeseries_raw[not_nan])
    cwv_timeseries_interp = cwv_timeseries_interp * 10

    # create time bnds
    date_list = [
        continuous_dates[0] + dt.timedelta(hours=x) for x in np.arange(
            0,
            timesteps_in_day * len(continuous_dates) *
            avg_interval, avg_interval)
    ]
    time_uppr_bnd = np.roll(date_list, -1)
    time_uppr_bnd[-1] = time_uppr_bnd[-2] + dt.timedelta(seconds=step)
    time_bnds = np.stack((np.asarray(time_uppr_bnd), np.asarray(date_list)))

    return cwv_timeseries_interp, time_bnds
#timepp=endepp-startpp
timel=endel-startl

#timecp=timet+timepp
timetot=timet+timel

print ('Time for the clustering process: %.2f s' %(timet))

# Determine number of clusters and clustersize

Cs=np.zeros((2,N))

for a in range(0,N):
    Ca=Clust[a]
    if np.int_(np.shape(Ca)) > 1:
        Cs[0,a]=np.int_(np.shape(Ca))
        Cs[1,a]=a
        
(a1,Nc)=np.int_(np.shape(np.nonzero(Cs[0])))
Cmax=np.int_(np.max(Cs[0]))

Ncl= np.count_nonzero(np.extract(Cs[0,:]>=Nl,Cs))

# Percentage of the largest cluster

Pc=float(Cmax)/float(N)

# Determine number of noise points

Nn=float(N)-np.sum(Cs[0,:])
Example #41
0
def gen_matrix(x,y):
    array=np.int_(np.random.rand(x,y)*10)
    print (array)
    return array
Example #42
0
def upload_hasil():
    if request.method == 'POST':
        a = request.files['file']  # variabel untuk nyimpan file
        ta = request.form['ta']
        sem = request.form['semester']
        a.save(os.path.join('app/upload_data', 'DATA.csv')
               )  # wadah untuk setiap kita nge-load, hasilnya disimpan disitu
        dataku = pd.read_csv('app/upload_data/DATA.csv')
        load_vectorizer = pickle.load(open("app/pickle_load/vectorizer.b",
                                           "rb"),
                                      encoding='latin1')
        load_naivebayes = pickle.load(open("app/pickle_load/nb.b", "rb"),
                                      encoding='latin1')
        pegawai = [str(x) for x in list(dataku["pegawai_id_pegawai"])]
        ampu = [str(x) for x in list(dataku["ampu_id_ampu"])]
        baku = [x for x in katabaku["kata_baku"]]

        new_data = []
        save_index = []

        def unique(listq):
            unique_list = []
            for x in listq:
                unique_list.append(x)
            return unique_list

        listStemUji = []
        for low in dataku.answer:
            lowerku = low.lower()
            textStemmed = stemmer.stem(lowerku)
            textClean = remover.remove(textStemmed)
            n = 0
            for i in katabaku["vocabulary"]:
                if textClean == i:
                    textClean = baku[n]
                n += 1
            listStemUji.append(textClean)

        data_sentimen = []

        tfidf = load_vectorizer.transform(listStemUji)
        for i in tfidf:
            sentiment = load_naivebayes.predict(i)
            data_sentimen.append(sentiment)
        data_sentimen
        for i in range(0, len(dataku.answer)):
            index = pegawai[i] + ',' + ampu[i]
            save_index.append(index)
            new_data.append([index, [data_sentimen[i]]])
        new_data = unique(new_data)

        clean_index = list(set(save_index))
        total = 0
        y = 0
        data_gue = []
        sumPositive = 0
        sumNegative = 0
        sumNetral = 0
        sumAll = 0
        totalPrecentPositive = 0
        totalPrecentNetral = 0
        totalPrecentNegative = 0
        for i in clean_index:
            for j in new_data:
                if str(i) == j[0]:
                    if np.int_(j[1]) == 0:
                        sumNegative += 1
                    elif np.int_(j[1]) == 2:
                        sumPositive += 1
                    elif np.int_(j[1]) == 1:
                        sumNetral += 1
                    sumAll += 1
            precentNegative = round((float(sumNegative) / float(sumAll)) * 100,
                                    2)
            precentPositive = round((float(sumPositive) / float(sumAll)) * 100,
                                    2)
            precentNetral = round((float(sumNetral) / float(sumAll)) * 100, 2)
            totalPrecentPositive += precentPositive
            totalPrecentNetral += precentNetral
            totalPrecentNegative += precentNegative
            data_gue.append(
                [i, precentPositive, precentNetral, precentNegative])
        rataPOS = round(float(totalPrecentPositive) / len(clean_index), 2)
        rataNET = round(float(totalPrecentNetral) / len(clean_index), 2)
        rataNEG = round(float(totalPrecentNegative) / len(clean_index), 2)

        data_final = []
        for i in data_gue:
            n = i[0].split(',')
            data_final.append([n[0], n[1], i[1], i[2], i[3]])
        print(data_final)
        framegue = pd.DataFrame.from_dict(data_final)
        framegue.columns = [
            'Id Dosen', 'Id Mata Kuliah', 'Sentimen Positif (%)',
            'Sentimen Netral (%)', 'Sentimen Negatif (%)'
        ]

        tahun_ajaran = str(ta) + '_' + str(sem)
        namaFile = 'app/db/' + str(ta) + '_' + str(sem) + '.csv'
        try:
            sentiment_obj = Sentiment(tahun_ajaran=tahun_ajaran,
                                      positive=rataPOS,
                                      neutral=rataNET,
                                      negative=rataNEG)
            sentiment_obj.save()
        except Exception as e:
            print(e)
        framegue.to_csv(namaFile,
                        quoting=csv.QUOTE_ALL,
                        sep=',',
                        escapechar='"',
                        mode='w',
                        header=True,
                        index=False)
    return render_template(
        'hasil_upload.html',
        tables=[framegue.to_html(classes='table table-bordered')])
def find_lines(img):
    # Threshold x gradient
    sxbinary = abs_sobel_thresh(img, orient='x', thresh_min=50, thresh_max=100)

    # sbinary = color_transform_thresh(img, thresh_min=100, thresh_max=255)

    # Threshold the L-channel of HLS
    hls_l = cv2.cvtColor(img, cv2.COLOR_RGB2HLS)[:,:,1]
    binary_hls_l = np.zeros_like(hls_l)
    binary_hls_l[(hls_l > 180) & (hls_l <= 255)] = 1

    # Threshold the S-channel of HSV
    hsv_s = cv2.cvtColor(img, cv2.COLOR_RGB2HSV)[:,:,1]
    binary_hsv_s = np.zeros_like(hsv_s)
    binary_hsv_s[(hsv_s >= 180) & (hsv_s <= 255)] = 1

    # Threshold the V-channel of HSV
    hsv_v = cv2.cvtColor(img, cv2.COLOR_RGB2HSV)[:,:,2]
    binary_hsv_v = np.zeros_like(hsv_v)
    binary_hsv_v[(hsv_v >= 180) & (hsv_v <= 255)] = 1

    # Thresholds the B-channel of LAB
    lab_b = cv2.cvtColor(img, cv2.COLOR_RGB2Lab)[:,:,2]
    binary_lab_b = np.zeros_like(lab_b)
    binary_lab_b[(lab_b > 160) & (lab_b <= 255)] = 1

    # Thresholds the L-channel of LUV
    luv_l = cv2.cvtColor(img, cv2.COLOR_RGB2Luv)[:, :, 0]
    binary_luv_l = np.zeros_like(luv_l)
    binary_luv_l[(luv_l > 200) & (luv_l <= 255)] = 1

    # # Stack each channel to view their individual contributions in green and blue respectively
    # # This returns a stack of the two binary images, whose components you can see as different colors
    # # color_binary = np.dstack(( np.zeros_like(sxbinary), sxbinary, sbinary, binary_hls_l, binary_lab_b )) * 255
    #
    combined_binary = np.zeros_like(sxbinary)
    combined_binary[(sxbinary == 1) | (binary_lab_b == 1) | (binary_luv_l == 1)] = 1

    # Uncomment to view binary image
    # plt.imshow(combined_binary, cmap='gray')
    # plt.show()

    # Perform sliding window search, returns binary_warped image (bird's eye view)
    binary_warped, Minv = get_birds_eye(combined_binary)

    # Plotting thresholded images
    f, (ax1, ax2) = plt.subplots(1, 2, figsize=(20,10))
    ax1.set_title('Original Image')
    ax1.imshow(img)

    ax2.set_title('Birds-eye-view')
    ax2.imshow(binary_warped, cmap='gray')
    plt.show()

    # Perform sliding window search to identify lane lines
    left_fitx, right_fitx, \
    ploty, left_curverad, \
    right_curverad, lane_deviation = sliding_window_search(binary_warped)

    # Load camera calibration data
    pickle_data = pickle.load(open('wide_dist_pickle.p', 'rb'))
    mtx = pickle_data["mtx"]
    dist = pickle_data["dist"]

    # Undistort image with calibration data
    undist = cv2.undistort(img, mtx, dist, None, mtx)

    # Create an image to draw the lines on
    warp_zero = np.zeros_like(binary_warped).astype(np.uint8)
    color_warp = np.dstack((warp_zero, warp_zero, warp_zero))

    # Recast the x and y points into usable format for cv2.fillPoly()
    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))

    # Draw the lane onto the warped blank image
    cv2.fillPoly(color_warp, np.int_([pts]), (255,0, 0))

    # Warp the blank back to original image space using inverse perspective matrix (Minv)
    newwarp = cv2.warpPerspective(color_warp, Minv, (img.shape[1], img.shape[0]))
    # Combine the result with the original image
    result = cv2.addWeighted(undist, 1, newwarp, 0.3, 0)
    font = cv2.FONT_HERSHEY_SIMPLEX
    cv2.putText(result,"Radius of Curvature = ",(20,40), font, 1, (255,255,255), 2, cv2.LINE_AA)
    cv2.putText(result,str(int(left_curverad)) + "m",(400,40), font, 1, (255,255,255), 2, cv2.LINE_AA)
    cv2.putText(result,"Vehicle is ",(20,80), font, 1, (255,255,255), 2, cv2.LINE_AA)
    cv2.putText(result,str(round(lane_deviation, 3)) + "m left of center",(180,80), font, 1, (255,255,255), 2, cv2.LINE_AA)

    plt.imshow(result)
    plt.show()
    return result
Example #44
0
def makeBVFeature(PointCloud_, BoundaryCond, Discretization):
    # 1024 x 1024 x 3
    Height = 1024 + 1
    Width = 1024 + 1
    # print(Discretization)
    # Discretize Feature Map
    PointCloud = np.copy(PointCloud_)
    PointCloud[:, 0] = np.int_(np.floor(PointCloud[:, 0] / Discretization))
    PointCloud[:, 1] = np.int_(
        np.floor(PointCloud[:, 1] / Discretization) + Width / 2)

    # sort-3times
    indices = np.lexsort((-PointCloud[:, 2], PointCloud[:, 1], PointCloud[:,
                                                                          0]))
    PointCloud = PointCloud[indices]

    # Height Map
    heightMap = np.zeros((Height, Width))

    _, indices = np.unique(PointCloud[:, 0:2], axis=0, return_index=True)
    PointCloud_frac = PointCloud[indices]
    #some important problem is image coordinate is (y,x), not (x,y)
    heightMap[np.int_(PointCloud_frac[:, 0]),
              np.int_(PointCloud_frac[:, 1])] = PointCloud_frac[:, 2]

    # Intensity Map & DensityMap
    intensityMap = np.zeros((Height, Width))
    densityMap = np.zeros((Height, Width))

    _, indices, counts = np.unique(PointCloud[:, 0:2],
                                   axis=0,
                                   return_index=True,
                                   return_counts=True)
    PointCloud_top = PointCloud[indices]
    # print(64)
    # print(np.log(64))
    # print(np.log10(10))
    normalizedCounts = np.minimum(1.0, np.log10(counts + 1) / 64)

    intensityMap[np.int_(PointCloud_top[:, 0]),
                 np.int_(PointCloud_top[:, 1])] = PointCloud_top[:, 3]
    densityMap[np.int_(PointCloud_top[:, 0]),
               np.int_(PointCloud_top[:, 1])] = normalizedCounts
    """
    plt.imshow(densityMap[:,:])
    plt.pause(2)
    plt.close()
    plt.show()
    plt.pause(2)
    plt.close()
    plt.show(block=False)
    plt.pause(2)
    plt.close()
    plt.imshow(intensityMap[:,:])
    plt.show(block=False)
    plt.pause(2)
    plt.close()
    """
    RGB_Map = np.zeros((Height, Width, 3))
    RGB_Map[:, :, 0] = densityMap  # r_map
    RGB_Map[:, :, 1] = heightMap  # g_map
    RGB_Map[:, :, 2] = intensityMap  # b_map

    save = np.zeros((512, 1024, 3))
    save = RGB_Map[0:512, 0:1024, :]
    return save
Example #45
0
    def __init__(self, 
                 n_neurons       = "micro",             # else: "brunel" or arrays
                 C_ab            = "micro",             # else: "brunel" or arrays
                 area            = net.area,            # simulation size
                 neuron_model    = net.neuron_model,    # "iaf_psc_delta" or "iaf_psc_exp"
                 connection_rule = net.connection_rule, # "fixed_total_number" or "fixed_indegree"
                 j02             = net.j02, 
                 weight_rel_sd   = net.weight_rel_sd, 
                 delay_rel_sd    = net.delay_rel_sd,  
                 g               = net.g, 
                 rate_ext        = net.rate_ext):                

        """Class of network parameters.

        Contains:
        - network parameters
        - single-neuron parameters
        - stimulus parameters
        
        Specify area, neuron_model, neuron_numbers, C_ab, 
            j02, connection_rule, weight_rel_sd, g, rate_ext_factor.
        Default values correspond to Potjans' model.

        Neuron numbers and synapse numbers (C_ab) can be specified separately.
        Both except either a string in {'micro', 'brunel'} or an array of 
        the corresponding shape.

        Note that V_r, theta, tau_m and rate_ext are already changed to values of 
        the microcircuit model, thus not the original values of the Brunel model.
        
        Brunel's model:
        j02     = 1.0
        connection_rule = fixed_indegree
        weight_rel_sd      = 0.0
        g, rate_ext varied accordingly

        Naming: C_ab, C_aext, J_ab, J_ext, rate_ext
        Don't use conn_probs, K_bg, PSPs, PSP_ext, v_ext, any more!
        """
        ###################################################
        ###     	Network parameters		###        
        ###################################################

        # area of network in mm^2; scales numbers of neurons
        # use 1 for the full-size network (77,169 neurons)
        self.area    = area
        
        self.layers         = net.layers    #np.array(["L23", "L4", "L5", "L6"])
        self.types          = net.types     #np.array(["e", "i"]) 
        self.populations    = np.array([layer + typus for layer in self.layers for typus in self.types])
        self.n_populations  = len(self.populations)
        self.n_layers       = len(self.layers)
        self.n_types        = len(self.types)
        
        # Neuron numbers
        if n_neurons == "micro":
            self.n_neurons  = np.int_(net.full_scale_n_neurons * self.area)
        elif n_neurons == "brunel":
            # Provide an array of equal number of neurons in each exc./inh. population
            gamma       = 0.25
            inh_factor  = 1. / (gamma + 1.)
            exc_factor  = 1. - inh_factor 
            n_total_micro = np.sum(net.full_scale_n_neurons * self.area)
            N_exc       = n_total_micro/self.n_populations * exc_factor
            N_inh       = n_total_micro/self.n_populations * inh_factor
            self.n_neurons  = np.tile([N_exc, N_inh], self.n_layers).astype(int)
        else:
            if type(n_neurons) == np.ndarray:
                if n_neurons.shape == (self.n_populations, ):
                    self.n_neurons   = np.int_(n_neurons)
                else:
                    raise Exception("'n_neurons' has wrong shape. "+
                                    "Expects (%i,)"%self.n_populations)
            else: 
                raise Exception("'n_neurons' expects either numpy.ndarray or string "+
                                "in {'micro', 'brunel'}")
        self.n_total    = np.sum(self.n_neurons)

        
        # Synapse numbers
        # Connection probabilities: conn_probs[post, pre] = conn_probs[target, source]
        conn_probs = net.conn_probs
        # Scale synapse numbers of the C_ab
        if net.scale_C_linearly:
            n_outer_full    = np.outer(net.full_scale_n_neurons, net.full_scale_n_neurons)
            C_full_scale    = np.log(1. - conn_probs) / np.log(1. - 1. / n_outer_full)
            C_scaled        = np.int_(C_full_scale * self.area)
        else:
            n_outer         = np.outer(self.n_neurons, self.n_neurons)
            C_scaled        = np.int_(np.log(1. - conn_probs) / np.log(1. - 1. / n_outer))

        self.connection_rule = connection_rule
        if self.connection_rule == "fixed_total_number":
            C_ab_micro = C_scaled   # total number, do not divide! 
        elif self.connection_rule == "fixed_indegree":
            C_ab_micro = (C_scaled.T / (net.full_scale_n_neurons * self.area)).T
        else:
            raise Exception("Unexpected connection type. Use 'fixed_total_number' for microcircuit " + 
                            "model or 'fixed_indegree' for Brunel's model!")

        if C_ab == "micro":
            self.C_ab = C_ab_micro # shall not be integer at this point!
        elif C_ab == "brunel":
            C_e     = np.mean(C_ab_micro) # mean for microcircuit (= 501 in full scale)
            C_i     = gamma * C_e
            self.C_ab   = np.tile([C_e, C_i], (self.n_populations, self.n_layers)).astype(int) 
        else:
            if type(C_ab) == np.ndarray:
                if C_ab.shape == (self.n_populations, self.n_populations):
                    self.C_ab   = np.int_(C_ab)
                else:
                    raise Exception("'C_ab' has wrong shape. "+
                                    "Expects (%i, %i)"%(self.n_populations, self.n_populations))
            else: 
                raise Exception("'C_ab' expects either numpy.ndarray or string "+
                                "in {'micro', 'brunel'}")


        ###################################################
        ###          Single-neuron parameters		###        
        ###################################################
        self.neuron_model   = neuron_model
        self.Vm0_mean       = net.Vm0_mean            # mean of initial membrane potential (mV)
        self.Vm0_std        = net.Vm0_std            # std of initial membrane potential (mV)
        self.model_params   = net.model_params
        if not self.neuron_model=="iaf_psc_delta":
            self.model_params["tau_syn_ex"] = net.tau_syn_ex # excitatory synaptic time constant (ms)
            self.model_params["tau_syn_in"] = net.tau_syn_in # inhibitory synaptic time constant (ms)
        self.tau_syn_ex = net.tau_syn_ex             # ms
        self.tau_syn_in = net.tau_syn_in             # ms
        self.tau_syn    = np.tile([self.tau_syn_ex, self.tau_syn_in], (self.n_populations, self.n_layers))
        # Rescaling for model calculations: these values are not used in the simulation!
        self.tau_m  = self.model_params["tau_m"]                 # ms
        self.t_ref  = self.model_params["t_ref"]                 # ms
        self.E_L    = self.model_params["E_L"]                   # mV
        self.V_r    = self.model_params["V_reset"] - self.E_L    # mV
        self.theta  = self.model_params["V_th"] - self.E_L       # mV
        self.C_m    = self.model_params["C_m"]                   # pF


        ######################################################
        # Synaptic weights. Depend on neuron_model!         ##
        ######################################################
        self.g      = g
        self.j02    = j02

        g_all       = np.tile([1., -self.g], (self.n_populations, self.n_layers))
        L23e_index  = np.where(self.populations == "L23e")[0][0]
        L4e_index   = np.where(self.populations == "L4e")[0][0]
        g_all[L23e_index, L4e_index] *= self.j02
        
        self.J              = net.PSP_e           # mv; mean PSP, used as reference PSP
        self.J_ab           = self.J * g_all
        self.weight_rel_sd  = weight_rel_sd # Standard deviation of weight relative to mean weight
        # Transformation from peak PSP to PSC
        delta_tau       = self.tau_syn - self.tau_m
        ratio_tau       = self.tau_m / self.tau_syn
        PSC_over_PSP    = self.C_m * delta_tau / (self.tau_m * self.tau_syn * \
            (ratio_tau**(self.tau_m / delta_tau) - ratio_tau**(self.tau_syn / delta_tau))) 
        # Actual weights have to be adapted: from peak PSP to PSC (and back...)
        if self.neuron_model=="iaf_psc_exp": # PSCs calculated from PSP amplitudes
            self.weights    = self.J_ab  * PSC_over_PSP     # neuron populations
        elif self.neuron_model=="iaf_psc_delta":
            self.weights    = self.J_ab * PSC_over_PSP * (self.tau_syn_ex) / self.C_m
            # This might be an overkill / doing things twice...
        elif self.neuron_model=="iaf_psc_alpha": # PSCs calculated from PSP amplitudes
            self.weights = self.J_ab * np.exp(1) / (self.tau_syn_ex) / self.C_m
        else:
            raise Exception("Neuron model should be iaf_psc_ - {delta, exp, alpha}!")


        ###################################################
        ###          Delays and dicts                   ###        
        ###################################################
        # mean dendritic delays for excitatory and inhibitory transmission (ms)
        self.delay_e = net.delay_e   # ms, excitatory synapses
        self.delay_i = net.delay_i   # ms, inhibitory synapses

        self.delays  = np.tile([self.delay_e, self.delay_i], (self.n_populations, self.n_layers)) # adapt...
        self.delay_rel_sd = delay_rel_sd 
        
        # Synapse dictionaries
        # default connection dictionary
        self.conn_dict   = {"rule": connection_rule}
        # weight distribution of connections between populations
        self.weight_dict_exc = net.weight_dict_exc
        self.weight_dict_inh = net.weight_dict_inh
        # delay distribution of connections between populations
        self.delay_dict  = net.delay_dict
        # default synapse dictionary
        self.syn_dict = net.syn_dict
        
        
        ###################################################
        ###          External stimuli                    ##        
        ###################################################
        # rate of background Poisson input at each external input synapse (spikes/s) 
        self.rate_ext   = rate_ext      # Hz 
        self.J_ext      = net.PSP_ext   # external synaptic weight
        self.delay_ext  = self.delay_e  # ms;  mean delay of external input
        self.dc_amplitude = net.dc_amplitude  # constant bg amplitude
        self.C_aext     = net.C_aext        # in-degrees for background input
        # Adapt weights
        if self.neuron_model=="iaf_psc_exp": # PSCs calculated from PSP amplitudes
            self.weight_ext = self.J_ext * PSC_over_PSP[0, 0] 
        elif self.neuron_model=="iaf_psc_delta":
            self.weight_ext = self.J_ext * PSC_over_PSP[0, 0] * self.tau_syn_ex / self.C_m
        elif self.neuron_model=="iaf_psc_alpha": # PSCs calculated from PSP amplitudes
            self.weight_ext = self.J_ext * np.exp(1) / self.tau_syn_ex / self.C_m

        # optional additional thalamic input (Poisson)
        self.n_th           = net.n_th      # size of thalamic population
        self.th_start       = net.th_start  # onset of thalamic input (ms)
        self.th_duration    = net.th_duration   # duration of thalamic input (ms)
        self.th_rate        = net.th_rate      # rate of thalamic neurons (spikes/s)
        self.J_th           = net.PSP_th      # mean EPSP amplitude (mV) for thalamic input
        # Adapt weights
        if self.neuron_model=="iaf_psc_exp": # PSCs calculated from PSP amplitudes
            self.weight_th = self.J_th * PSC_over_PSP[0, 0] 
        elif self.neuron_model=="iaf_psc_delta":
            self.weight_th = self.J_th * PSC_over_PSP[0, 0] * self.tau_syn_ex / self.C_m
        elif self.neuron_model=="iaf_psc_alpha": # PSCs calculated from PSP amplitudes
            self.weight_th = self.J_th * np.exp(1) / self.tau_syn_ex / self.C_m

        
        # connection probabilities for thalamic input
        conn_probs_th = net.conn_probs_th
        if net.scale_C_linearly:
            if not self.n_th == 0:
                C_th_full_scale = np.log(1. - conn_probs_th) / \
                    np.log(1. - 1. / (self.n_th * net.full_scale_n_neurons))
                self.C_th_scaled     = np.int_(C_th_full_scale * self.area)
        else:
            if not self.n_th == 0:
                self.C_th_scaled     = np.int_(np.log(1. - conn_probs_th) / \
                    np.log(1. - 1. / (self.n_th * self.n_neurons_micro)))
        if self.n_th == 0:
            self.C_th_scaled = None
        
        # mean delay of thalamic input (ms)
        self.delay_th    = net.delay_th
        # standard deviation relative to mean delay of thalamic input
        self.delay_th_rel_sd = net.delay_th_rel_sd


        ######################################################
        # Predefine matrices for mean field                 ##
        ######################################################
        if self.neuron_model=="iaf_psc_delta":
            self.J_mu       = self.weights
            self.J_sd       = self.weights
            self.J_mu_ext   = self.weight_ext   
            self.J_sd_ext   = self.weight_ext
        elif self.neuron_model=="iaf_psc_exp":
            self.J_mu       = self.weights    * self.tau_syn   / self.C_m
            self.J_sd       = self.weights    * np.sqrt(self.tau_syn / 2.) / self.C_m
            self.J_mu_ext   = self.weight_ext * self.tau_syn_ex / self.C_m
            self.J_sd_ext   = self.weight_ext * np.sqrt(self.tau_syn_ex / 2.) / self.C_m
        elif self.neuron_model=="iaf_psc_alpha":
            self.J_mu       = self.weights    * self.tau_syn**2          / self.C_m
            self.J_sd       = self.weights    * self.tau_syn**(3./2.)    / (self.C_m * 2.)
            self.J_mu_ext   = self.weight_ext * self.tau_syn_ex**2       / self.C_m
            self.J_sd_ext   = self.weight_ext * self.tau_syn_ex**(3./2.) / (self.C_m * 2.)
        self.mat_mu     = self.tau_m * 1e-3 * self.J_mu        * self.C_ab
        self.mu_ext     = self.tau_m * 1e-3 * self.J_mu_ext    * self.C_aext * self.rate_ext
        self.mat_var    = self.tau_m * 1e-3 * (1 + self.weight_rel_sd ** 2) * self.J_sd**2     * self.C_ab
        self.var_ext    = self.tau_m * 1e-3 * (1 + self.weight_rel_sd ** 2) * self.J_sd_ext**2 * self.C_aext * self.rate_ext
def pose_change_a_to_b(frame_a, frame_b):

    try:
        first_img = frame_a.get_amplitude_image()  # adjust image sizes
        # first_img = np.float32(np.gradient(data.normalize_full(first_img), 2, axis=0, edge_order=2))
        first_img = np.uint8(filter_image(first_img) * 255)
        second_img = frame_b.get_amplitude_image()
        # second_img = np.float32(np.gradient(data.normalize_full(second_img), 2, axis=0, edge_order=2))
        second_img = np.uint8(filter_image(second_img) * 255)

        sift_img = cv2.xfeatures2d.SIFT_create(contrastThreshold=0.0015, edgeThreshold=10, nOctaveLayers=3, sigma=1.2)
        mask = np.zeros_like(first_img)
        mask[frame_a.confidence >= 6] = 1
        kp1, desc1 = sift_img.detectAndCompute(first_img, mask=mask)  # sift test first image
        mask[frame_b.confidence >= 6] = 1
        kp2, desc2 = sift_img.detectAndCompute(second_img, mask=mask)  # sift second image

        img1_points = []
        img2_points = []
        detected_feature_loc1 = []
        detected_feature_loc2 = []

        if desc1 is None or desc2 is None or not kp1 or not kp2:  # remove frames that don't have anything
            pass
            # cv2.imshow(frame_text + " 1", first_img)
            # cv2.imshow(frame_text + " 2", second_img)
        else:
            compare_desc = cv2.BFMatcher()  # setup brute force matcher
            match = compare_desc.knnMatch(desc1, desc2, k=2)
            j = 0
            for points, points2 in match:  # {
                if points.distance > 0.55 * points2.distance:
                    continue
                # get the x and y pixel locations the features from the matcher
                # get points from the key points array that were matched
                (x1, y1) = kp1[points.queryIdx].pt
                (x2, y2) = kp2[points.trainIdx].pt

                # point pairs for each image, indices match pairs
                # array to hold all (x,y) pixel loc of features
                img1_points = np.append(img1_points, [x1, y1])
                img2_points = np.append(img2_points, [x2, y2])

                # round values and cast to integer
                img1_points = np.int_(np.round(img1_points, decimals=0, out=None))
                img2_points = np.int_(np.round(img2_points, decimals=0, out=None))

                # img1_points = img1_points[status == 1]
                # img2_points = img2_points[status == 1]

                # get the xvect, yvect, and z distance of each point of interest based on the pixel xy location
                # and round to 4 decimal places.  final full array of all matching points
                feature_loc1 = np.round(frame_a.get_position(img1_points[j * 2], img1_points[j * 2 + 1]), 4)
                feature_loc2 = np.round(frame_b.get_position(img2_points[j * 2], img2_points[j * 2 + 1]), 4)
                detected_feature_loc1 = np.append(detected_feature_loc1,
                                                  (feature_loc1[1], feature_loc1[2], feature_loc1[0]))
                detected_feature_loc2 = np.append(detected_feature_loc2,
                                                  (feature_loc2[1], feature_loc2[2], feature_loc2[0]))
                j += 1

            img1_points = np.reshape(img1_points, (-1, 1, 2))
            img2_points = np.reshape(img2_points, (-1, 1, 2))
            # print("img1 points: ", img1_points)

            H, status = cv2.findHomography(img1_points, img2_points, cv2.RANSAC,
                                           ransacReprojThreshold=0.999)

            detected_feature_loc1 = np.reshape(detected_feature_loc1, (-1, 1, 3))
            detected_feature_loc2 = np.reshape(detected_feature_loc2, (-1, 1, 3))
            # print("img1 points: ", detected_feature_loc1)
            detected_feature_loc1 = detected_feature_loc1[status == 1]
            detected_feature_loc2 = detected_feature_loc2[status == 1]

            img1_points = img1_points[status == 1]
            img2_points = img2_points[status == 1]

            # show two original frames and matched features
            # display = np.hstack((first_img, second_img))
            display = second_img
            img1_points = img1_points.reshape((-1, 2))
            img2_points = img2_points.reshape((-1, 2))
            display = calc.render_keypoints(display, img2_points, img1_points, scale=3)  # , offset=(first_img.shape[1], 0))

            cv2.imshow(frame_text, display)
            # cv2.waitKey(10)

        # after each image loop before getting next image
        # reshape vector into 3D array then take transpose
        detected_feature_loc1 = np.reshape(detected_feature_loc1, (-1, 3))  # format [[x1y1z1][x2y2z2]]
        detected_feature_loc2 = np.reshape(detected_feature_loc2, (-1, 3))  # format [[x1y1z1][x2y2z2]]

        detected_feature_loc1_tuple = tuple(detected_feature_loc1)
        detected_feature_loc2_tuple = tuple(detected_feature_loc2)

        tupled_features = [[tuple(x), tuple(y)] for x, y in zip(detected_feature_loc1, detected_feature_loc2)]

        # send to ransac calculation
        param = RansacParams(samples=4, iterations=15, confidence=0.9, threshold=ransac_thresh)
        my_model = ThreeD()
        found_inliers = find_inliers(tupled_features, my_model, param)
        # print(found_inliers)
        if len(found_inliers) < 4:
            return None
        print("\tnumber of inliers: ", len(found_inliers))
        # inlier_count = np.append(inlier_count, len(found_inliers))

    except:
        traceback.print_exc()
        return None
        # return np.mat(np.eye(3)), np.mat(np.zeros(3).T)

    return my_model.rotate, my_model.translate
def extract_image_patches(image,
                          patch_size,
                          max_number_of_patches='all',
                          stride_length=1,
                          mask_image=None,
                          random_seed=None,
                          return_as_array=False):
    """
    Extract 2-D or 3-D image patches.

    Arguments
    ---------
    image : ANTsImage
        Input image with one or more components.

    patch_size: n-D tuple (depending on dimensionality).
        Width, height, and depth (if 3-D) of patches.

    max_number_of_patches:  integer or string
        Maximum number of patches returned.  If "all" is specified, then
        all patches in sequence (defined by the stride_length are extracted.

    stride_length:  integer or n-D tuple
        Defines the sequential patch overlap for max_number_of_patches = "all".
        Can be a image-dimensional vector or a scalar.

    mask_image:  ANTsImage (optional)
        Optional image specifying the sampling region for
        the patches when max_number_of_patches does not equal "all".
        The way we constrain patch selection using a mask is by forcing
        each returned patch to have a masked voxel at its center.

    random_seed: integer (optional)
        Seed value that allows reproducible patch extraction across runs.

    return_as_array: boolean
        Specifies the return type of the function.  If
        False (default) the return type is a list where each element is
        a single patch.  Otherwise the return type is an array of size
        dim( number_of_patches, patch_size ).

    Returns
    -------
    A list (or array) of patches.

    Example
    -------
    >>> import ants
    >>> image = ants.image_read(ants.get_ants_data('r16'))
    >>> image_patches = extract_image_patches(image, patch_size=(32, 32))
    """

    if random_seed is not None:
        random.seed(random_seed)

    image_size = image.shape
    dimensionality = image.dimension

    if dimensionality != 2 and dimensionality != 3:
        raise ValueError("Unsupported dimensionality.")

    number_of_image_components = image.components

    if len(image_size) != len(patch_size):
        raise ValueError("Mismatch between the image size and the specified patch size.")

    if patch_size > image_size:
        raise ValueError("Patch size is greater than the image size.")

    image_array = image.numpy()
    if number_of_image_components > 1:
        if dimensionality == 2:
            image_array = np.transpose(image_array, [1, 2, 0])
        else:
            image_array = np.transpose(image_array, [1, 2, 3, 0])

    patch_list = []
    patch_array = np.empty([1, 1])
    mid_patch_index = tuple(np.int_(np.subtract(np.round(0.5 * (np.array(patch_size))), 1)))

    number_of_extracted_patches = max_number_of_patches

    if isinstance(max_number_of_patches, str) and max_number_of_patches.lower() == 'all':
        stride_length_tuple = stride_length
        if isinstance(stride_length, int):
            stride_length_tuple = tuple(np.multiply(np.ones_like(patch_size), stride_length))
        elif len(stride_length) != dimensionality:
            raise ValueError("stride_length is not a scalar or vector of length dimensionality.")
        elif np.any(np.less(stride_length, 1)):
            raise ValueError("stride_length elements must be positive integers.")

        number_of_extracted_patches = 1

        indices = []
        for d in range(dimensionality):
            indices.append(range(0, image_size[d] - patch_size[d] + 1, stride_length_tuple[d]))
            number_of_extracted_patches *= len(indices[d])

        if return_as_array:
            if number_of_image_components == 1:
                patch_array = np.zeros((number_of_extracted_patches, *patch_size))
            else:
                patch_array = np.zeros((number_of_extracted_patches, *patch_size, number_of_image_components))

        count = 0
        if dimensionality == 2:
            for i in indices[0]:
                for j in indices[1]:
                    start_index = (i, j)
                    end_index = tuple(np.add(start_index, patch_size))

                    if number_of_image_components == 1:
                        patch = image_array[start_index[0]:end_index[0],
                                            start_index[1]:end_index[1]]
                    else:
                        patch = image_array[start_index[0]:end_index[0],
                                            start_index[1]:end_index[1],:]

                    if return_as_array:
                        if number_of_image_components == 1:
                            patch_array[count, :, :] = patch
                        else:
                            patch_array[count, :, :, :] = patch
                    else:
                        patch_list.append(patch)

                    count += 1
        else:
            for i in indices[0]:
                for j in indices[1]:
                    for k in indices[2]:
                        start_index = (i, j, k)
                        end_index = tuple(np.add(start_index, patch_size))

                        if number_of_image_components == 1:
                            patch = image_array[start_index[0]:end_index[0],
                                                start_index[1]:end_index[1],
                                                start_index[2]:end_index[2]]
                        else:
                            patch = image_array[start_index[0]:end_index[0],
                                                start_index[1]:end_index[1],
                                                start_index[2]:end_index[2],:]

                        if return_as_array:
                            if number_of_image_components == 1:
                                patch_array[count, :, :, :] = patch
                            else:
                                patch_array[count, :, :, :, :] = patch
                        else:
                            patch_list.append(patch)

                        count += 1
    else:

        random_indices = np.zeros((max_number_of_patches, dimensionality), dtype=int)

        if mask_image != None:
            mask_array = mask_image.numpy()
            mask_array[np.where(mask_array != 0)] = 1

            # The way we constrain patch selection using a mask is by assuming that
            # each patch must have a masked voxel at its midPatchIndex.

            mask_indices = np.column_stack(np.where(mask_array != 0))

            for d in range(dimensionality):
                shifted_mask_indices = np.subtract(mask_indices[:, d], mid_patch_index[d])
                outside_indices = np.where(shifted_mask_indices < 0)
                mask_indices = np.delete(mask_indices, outside_indices, axis = 0)
                shifted_mask_indices = np.add(mask_indices[d], mid_patch_index[d])
                outside_indices = np.where(shifted_mask_indices >= image_size[d])
                mask_indices = np.delete(mask_indices, outside_indices, axis = 0)

            # After pruning the mask indices, which were originally defined in terms of the
            # mid_patch_index, we subtract the midPatchIndex so that it's now defined at the
            # corner for patch selection.

            for d in range(dimensionality):
                mask_indices[:, d] = np.subtract(mask_indices[:, d], mid_patch_index[d])

            number_of_extracted_patches = min(max_number_of_patches, mask_indices.shape[0])

            random_indices = mask_indices[
              random.sample(range(mask_indices.shape[0] + 1), number_of_extracted_patches), :]
        else:

            for d in range(dimensionality):
                random_indices[:, d] = random.sample(range(image_size[d] - patch_size[d] + 1), max_number_of_patches)

        if return_as_array:
            if number_of_image_components == 1:
                patch_array = np.zeros((number_of_extracted_patches, *patch_size))
            else:
                patch_array = np.zeros((number_of_extracted_patches, *patch_size, number_of_image_components))

        start_index = np.ones( dimensionality )
        for i in range(number_of_extracted_patches):
            start_index = random_indices[i, :]
            end_index = np.add(start_index, patch_size)

            if dimensionality == 2:
                if number_of_image_components == 1:
                    patch = image_array[start_index[0]:end_index[0],
                                        start_index[1]:end_index[1]]
                else:
                    patch = image_array[start_index[0]:end_index[0],
                                        start_index[1]:end_index[1], :]
            else:
                if number_of_image_components == 1:
                    patch = image_array[start_index[0]:end_index[0],
                                        start_index[1]:end_index[1],
                                        start_index[2]:end_index[2]]
                else:
                    patch = image_array[start_index[0]:end_index[0],
                                        start_index[1]:end_index[1],
                                        start_index[2]:end_index[2], :]

            if return_as_array:
                if dimensionality == 2:
                    if number_of_image_components == 1:
                        patch_array[i, :, :] = patch
                    else:
                        patch_array[i, :, :, :] = patch
                else:
                    if number_of_image_components == 1:
                        patch_array[i, :, :] = patch
                    else:
                        patch_array[i, :, :, :, :] = patch
            else:
                patch_list.append(patch)

    if return_as_array:
       return(patch_array)
    else:
       return(patch_list)
Example #48
0
 def _convert_state_to_bbox(self, state):
     return Rect(tf_rect=np.int_(np.round(state[:4, 0])))
def makeBVFeature(PointCloud_origin, BoundaryCond,
                  size_cell):  # Discretization:ROI长度(m)/该长度方向上的栅格数,即每个cell的边长
    Height = 700
    Width = 500

    PointCloud = removePoints(PointCloud_origin, BoundaryCond)
    PointCloud[:, 0] = BoundaryCond['maxX'] - PointCloud[:, 0]  # 坐标系统一为左上角
    PointCloud[:, 1] = BoundaryCond['maxY'] - PointCloud[:, 1]

    # PointCloud_grid = np.copy(PointCloud )
    PointCloud[:, 0] = np.int_(np.floor(PointCloud[:, 0] /
                                        size_cell))  # np.floor 返回不大于输入参数的最大整数;
    PointCloud[:, 1] = np.int_(np.floor(
        PointCloud[:, 1] /
        size_cell))  # 将点云坐标转化为栅格坐标,python数组从0开始,所以不用+1,matlab从0开始,所以需要+1

    # sort-3times
    indices = np.lexsort(
        (-PointCloud[:, 2], PointCloud[:, 1], PointCloud[:, 0])
    )  #  np.lexsort((b,a)) 先对a排序,再对b.排序按x轴(栅格)进行从小到大排列,当x值相同时,按y轴(栅格)从小到大排序,y也相同时,按z从大到小排序
    PointCloud = PointCloud[
        indices]  #  目的是将每个栅格的最大z排在最前面,下面unique时,便只会保留z最大值(排在第一位)的索引

    # Height Map& DensityMap
    heightMap = np.zeros(
        (Height, Width))  # 括号括起来 表示是一个参数,即np.zeros()的第一个参数是(Height,Width)
    densityMap = np.zeros((Height, Width))
    _, indices, counts = np.unique(PointCloud[:, 0:2],
                                   axis=0,
                                   return_index=True,
                                   return_counts=True)
    PointCloud_frac = PointCloud[indices]  # counts返回的是每个元素在原始数组出现的次数,这里是每个栅格中
    # some important problem is image coordinate is (y,x), not (x,y)
    height_z = np.int(BoundaryCond['maxZ']) - np.int(BoundaryCond['minZ'])
    heightMap[np.int_(PointCloud_frac[:, 0]),
              np.int_(PointCloud_frac[:, 1])] = (PointCloud_frac[:, 2] +
                                                 2) / height_z
    heightMap.astype(np.int32)
    normalizedCounts = np.minimum(1.0,
                                  np.log(counts + 1) /
                                  np.log(64))  # 即normalizedCounts最大为1
    densityMap[np.int_(PointCloud_frac[:, 0]),
               np.int_(PointCloud_frac[:, 1])] = normalizedCounts / 1
    #densityMap=densityMap.astype(np.uint8)
    intensityMap = np.zeros((Height, Width))
    indices = np.lexsort((-PointCloud[:, 3], PointCloud[:, 1],
                          PointCloud[:, 0]))  # 按x由小到大,y由小到大, intensity由大到小
    PointCloud_intensity = PointCloud[indices]
    _, indices = np.unique(PointCloud_intensity[:, 0:2],
                           axis=0,
                           return_index=True)  # 只保留每个栅格中intensity最大的点
    PointCloud_max_intensity = PointCloud_intensity[indices]
    intensityMap[np.int_(PointCloud_max_intensity[:, 0]),
                 np.int_(PointCloud_max_intensity[:, 1]
                         )] = PointCloud_max_intensity[:, 3]  # 将反射强度放大100倍
    #intensityMap=intensityMap.astype(np.uint8)

    RGB_Map = np.zeros((Height, Width, 3))
    RGB_Map[:, :, 0] = densityMap  # r_map

    RGB_Map[:, :, 1] = heightMap  # g_map

    RGB_Map[:, :, 2] = intensityMap  # b_map

    grid_picture = RGB_Map[0:Height, 0:Width, :]
    #grid_picture = grid_picture.astype(np.uint8)
    grid_picture = np.ceil(grid_picture[:, :, ::-1] * 255)
    grid_picture = grid_picture.astype(np.uint8)
    #cv2.imwrite('outimage/1.png', grid_picture)
    # grid_picture=np.ceil(grid_picture)
    # grid_picturenew=grid_picture.astype(np.uint8)

    return grid_picture
Example #50
0
def search_around_poly(warped_binary):
    # Choose the width of the margin around the previous polynomial to search
    margin = 40

    # Grab activated pixels
    nonzero = binary_warped.nonzero()
    nonzeroy = np.array(nonzero[0])
    nonzerox = np.array(nonzero[1])

    # Search within the polynomial based 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)

    # 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)

    # Colors in the left and right lane regions
    out_img[lefty, leftx] = [255, 0, 0]
    out_img[righty, rightx] = [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')

    return result, left_fit, right_fit, ploty
Example #51
0
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]), (10, 255, 100))
    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 predict(pred_func, input_file):
    for frame_name in os.listdir(input_file):

        intensity_file = pd.read_csv(os.path.join(intensity_path, frame_name))
        pts_file = pd.read_csv(os.path.join(pts_path, frame_name))
        file_name = str.split(frame_name, '.')[0]
        pts = pd.read_csv(os.path.join(pts_path, file_name + '.csv'),
                          header=None)
        pts = np.array(pts)
        out = np.zeros((len(pts), 1), dtype=np.int8)
        start = time.time()
        PointCloud = np.hstack((pts_file, intensity_file))
        img = makeBVFeature(PointCloud, size_ROI, size_cell)
        results = detect_one_image(img, pred_func)
        #objectfile=open('results1/%s.txt'%file_name,'w')
        pts[:, 0] = maxX - pts[:, 0]  # 坐标系统一为左上角
        pts[:, 1] = maxY - pts[:, 1]
        # PointCloud_grid = np.copy(PointCloud )
        pts[:, 0] = np.int_(np.floor(pts[:, 0] /
                                     size_cell))  # np.floor 返回不大于输入参数的最大整数;
        pts[:, 1] = np.int_(np.floor(pts[:, 1] / size_cell))
        for r in results:
            label = cfg.DATA.CLASS_NAMES[r.class_id][0:3]
            #if r.score<0.4:
            #label=0
            indx = np.where((pts[:, 1] > int(r[0][0]))
                            & (pts[:, 1] < int(r[0][2]))
                            & (pts[:, 0] < int(r[0][3]))
                            & (pts[:, 0] > int(r[0][1])))
            if label == 'cyc':

                if r.score < 0.6:
                    label = 0
                else:
                    label = 1
                # indx = np.where(
                #     (pts[:, 1] > int(r[0][0])) & (pts[:, 1] < int(r[0][2])) & (pts[:, 0] < int(r[0][3])) & (pts[:, 0] > int(r[0][1]))& (pts[:, 2] <-0.3))

            elif label == 'tri':
                label = 2
                # indx = np.where(
                #     (pts[:, 1] > int(r[0][0])) & (pts[:, 1] < int(r[0][2])) & (pts[:, 0] < int(r[0][3])) & (
                #                 pts[:, 0] > int(r[0][1])) & (pts[:, 2] < -0.3))
            elif label == 'sma':
                label = 3
                # indx = np.where(
                #     (pts[:, 1] > int(r[0][0])) & (pts[:, 1] < int(r[0][2])) & (pts[:, 0] < int(r[0][3])) & (
                #             pts[:, 0] > int(r[0][1])) & (pts[:, 2] < 0))
            elif label == 'big':
                label = 4
                # indx = np.where(
                #     (pts[:, 1] > int(r[0][0])) & (pts[:, 1] < int(r[0][2])) & (pts[:, 0] < int(r[0][3])) & (
                #             pts[:, 0] > int(r[0][1])) )
            elif label == 'ped':
                if r.score < 0.4:
                    label = 0
                else:
                    label = 5
                #label = 5
                # indx = np.where(
                #     (pts[:, 1] > int(r[0][0])) & (pts[:, 1] < int(r[0][2])) & (pts[:, 0] < int(r[0][3])) & (
                #             pts[:, 0] > int(r[0][1])) & (pts[:, 2] < -0.1))
            elif label == 'cro':
                label = 6
                # indx = np.where(
                #     (pts[:, 1] > int(r[0][0])) & (pts[:, 1] < int(r[0][2])) & (pts[:, 0] < int(r[0][3])) & (
                #             pts[:, 0] > int(r[0][1])) & (pts[:, 2] < -0.1))
            elif label == 'unk':
                if r.score < 0.3:
                    label = 0
                # else:
                #     label=7
                # indx = np.where(
                #     (pts[:, 1] > int(r[0][0])) & (pts[:, 1] < int(r[0][2])) & (pts[:, 0] < int(r[0][3])) & (
                #             pts[:, 0] > int(r[0][1])) )
                label = 7
            out[indx] = label
        end = time.time()
        np.savetxt(os.path.join(upload_path, file_name + '.csv'),
                   out,
                   delimiter=',',
                   fmt='%d')
        print(end - start)
    def run(self, img, debug=False):
        und_img = self.undistort(img)
        if debug is True:
            plt.imshow(und_img)
            plt.title('undistorted image')
            plt.show()

        res_img = self.transform_img(und_img, debug)
        if debug is True:
            plt.imshow(res_img, cmap='gray')
            plt.title('transformed image')
            plt.show()

        warped_img = self.warp_img(res_img, debug)
        if debug is True:
            plt.imshow(warped_img, cmap='gray')
            plt.title('warped image')
            plt.show()
            print(warped_img.shape)

        # detect line within the img
        left, right = self.find_lines(warped_img, debug)
        if debug is True:
            #draw line on top of warped_img
            temp_warpimg = np.copy(warped_img)
            tempcolor_warp = (np.dstack(
                (temp_warpimg, temp_warpimg, temp_warpimg)) * 255).astype(
                    np.uint8)

            pts_left = np.array(
                [np.transpose(np.vstack([left.allx, left.ally]))])
            pts_right = np.array(
                [np.flipud(np.transpose(np.vstack([right.allx, right.ally])))])
            # pts = np.hstack((pts_left, pts_right)
            pts_left = pts_left.reshape((-1, 1, 2))
            pts_right = pts_right.reshape((-1, 1, 2))
            cv2.polylines(tempcolor_warp,
                          np.int_([pts_left]),
                          False, (0, 0, 255),
                          thickness=5)
            cv2.polylines(tempcolor_warp,
                          np.int_([pts_right]),
                          False, (255, 0, 0),
                          thickness=5)
            plt.imshow(tempcolor_warp)
            plt.title('detected lines')
            plt.show()

        left, right = self.lane.line_detected_current_frame(left, right)
        lane_detected = self.draw(warped_img, left, right, debug)
        if debug is True:
            plt.imshow(lane_detected)
            plt.title('lane detected')
            plt.show()

        # Combine the result with the original image
        result = cv2.addWeighted(img, 1, lane_detected, 0.3, 0)

        # add text on image
        curve_text = "curvature:" + '{:.3f}'.format(self.lane.get_curvature())
        self.write_on_image(result, curve_text, location=(10, 50))
        road_width = "lane width:" + '{:.3f}'.format(self.lane.get_lanewidth())
        self.write_on_image(result, road_width, location=(10, 150))
        offset_text = "offset from center:" + '{:.3f}'.format(
            self.lane.get_offset(result.shape[1]))
        self.write_on_image(result, offset_text, location=(10, 100))

        return result
Example #54
0
def index_from_nearest_path_with_pt_in_bbox_(
    level_index,
    l_i,
    nb_c_per_l,
    nb_pt_per_c,
    indices_of_first_pts,
    x_value,
    y_value,
    x_min_per_c,
    y_min_per_c,
    x_max_per_c,
    y_max_per_c,
    xpt,
    ypt,
):
    """Get index from nearest path in edge bbox contain pt
    """
    # Nb contour in level
    if nb_c_per_l[level_index] == 0:
        return -1
    # First contour in level
    i_start_c = l_i[level_index]
    # First contour of the next level
    i_end_c = i_start_c + nb_c_per_l[level_index]

    # Flag to check if we iterate
    find_contour = 0
    # We select the first pt of the first contour in the level
    # to initialize dist
    i_ref = i_start_c
    i_start_pt = indices_of_first_pts[i_start_c]
    dist_ref = (x_value[i_start_pt] - xpt)**2 + (y_value[i_start_pt] - ypt)**2

    # We iterate over contour in the same level
    for i_elt_c in range(i_start_c, i_end_c):
        # if bbox of contour doesn't contain pt, we skip this contour
        if y_min_per_c[i_elt_c] > ypt:
            continue
        if y_max_per_c[i_elt_c] < ypt:
            continue
        x_min = x_min_per_c[i_elt_c]
        xpt_ = (xpt - x_min) % 360 + x_min
        if x_min > xpt_:
            continue
        if x_max_per_c[i_elt_c] < xpt_:
            continue
        # Indice of first pt of contour
        i_start_pt = indices_of_first_pts[i_elt_c]
        # Indice of first pt of the next contour
        i_end_pt = i_start_pt + nb_pt_per_c[i_elt_c]
        # We set flag to true, because we check contour
        find_contour = 1

        # We do iteration on pt to check dist, if it's inferior we store
        # index of contour
        for i_elt_pt in range(i_start_pt, i_end_pt):
            d_x = x_value[i_elt_pt] - xpt_
            if abs(d_x) > 180:
                d_x = (d_x + 180) % 360 - 180
            dist = d_x**2 + (y_value[i_elt_pt] - ypt)**2
            if dist < dist_ref:
                dist_ref = dist
                i_ref = i_elt_c
    # No iteration on contour, we return no index of contour
    if find_contour == 0:
        return int_(-1)
    # We return index of contour, for the specific level
    return int_(i_ref - i_start_c)
def makeBVFeature(PointCloud_, BoundaryCond, Discretization):  # Dis=
    Height = Discretization + 1  #
    Width = Discretization + 1

    # Discretize Feature Map
    PointCloud = np.copy(PointCloud_)
    PointCloud[:, 0] = np.int_(
        np.floor(PointCloud[:, 0] /
                 abs(BoundaryCond['maxX'] - BoundaryCond['minX']) *
                 Discretization))  # x  倍增
    PointCloud[:, 1] = np.int_(
        np.floor(PointCloud[:, 1] /
                 abs(BoundaryCond['maxY'] - BoundaryCond['minY']) *
                 Discretization))  # y, 倍增+平移

    indices = np.lexsort((PointCloud[:, 1], PointCloud[:, 2], PointCloud[:,
                                                                         0]))
    PointCloud = PointCloud[indices]

    # Height Map & Intensity Map & DensityMap
    heightMap = np.zeros((Height, Width))  # 空 矩阵
    intensityMap = np.zeros((Height, Width))
    densityMap = np.zeros((Height, Width))

    #_, indices = np.unique(PointCloud[:, 0:2], axis=0, return_index=True)  # 去重
    _, indices, counts = np.unique(PointCloud[:, 0:2],
                                   axis=0,
                                   return_index=True,
                                   return_counts=True)

    PointCloud_remain = PointCloud[indices]  # 剩余点
    # !!!!!some important problem is image coordinate is (y,x), not (x,y)调整方向调整这个
    y = np.int_(PointCloud_remain[:, 0])  # x axis is -y in LIDAR
    # y = Discretization - y
    x = np.int_(PointCloud_remain[:, 1])  # y axis is -x in LIDAR
    x = Discretization - x

    # heightMap[y, x] = PointCloud_remain[:, 2]  # 高度作为数值
    heightMap[x, y] = PointCloud_remain[:, 2]

    #PointCloud_top = PointCloud[indices]
    # intensityMap[y, x] = PointCloud_remain[:, 3]  # 反射率
    intensityMap[x, y] = PointCloud_remain[:, 3]
    normalizedCounts = np.minimum(1.0, np.log(counts + 1) / np.log(64))
    # densityMap[y, x] = normalizedCounts  # 用的是出现的次数作为对应坐标的数值
    densityMap[x, y] = normalizedCounts

    # 对三个通道的值进行归一化,后期输出的话可能要×255
    densityMap = densityMap / densityMap.max()
    heightMap = heightMap / heightMap.max()
    intensityMap = intensityMap / intensityMap.max()
    # densityMap = 255*densityMap / densityMap.max()
    # heightMap = 255*heightMap / heightMap.max()
    # intensityMap = 255*intensityMap / intensityMap.max()

    RGB_Map = np.zeros((Height, Width, 3))
    RGB_Map[:, :, 0] = densityMap  # r_map
    RGB_Map[:, :, 1] = heightMap  # g_map
    RGB_Map[:, :, 2] = intensityMap  # b_map

    save = np.zeros((Discretization, Discretization, 3))
    save = RGB_Map[0:Discretization, 0:Discretization, :]
    # misc.imsave('test_bv.png',save[::-1,::-1,:])
    # misc.imsave('test_bv.png',save)
    return save
Example #56
0
from keras.models import load_model
from keras.constraints import min_max_norm, non_neg, binary_m, two_value
from PIL import Image
import os
import image_evaluate as ievalue
import time
import method_module as mm
import math

dataset = ['stl', 'face']
dataset_cifar = ['cifar']
size = np.zeros(3)
size[0] = 32
size[1] = 64
size[2] = 128
size = np.int_(size)
size_cifar = np.zeros(1)
size_cifar[0] = 32
size_cifar = np.int_(size_cifar)
rate = np.arange(10) * 0.1 - 0.1
rate[0] = 0.001
rate[1] = 0.01
print(rate)
mf1 = np.zeros((3, 3, 10, 4))
mf2 = np.zeros((3, 3, 10, 4))
mf3 = np.zeros((3, 3, 10, 4))
mf4 = np.zeros((3, 3, 10, 4))
mf5 = np.zeros((3, 3, 10, 4))
mf6 = np.zeros((3, 3, 10, 4))
mf7 = np.zeros((3, 3, 10, 4))
Example #57
0
    def fkt_pycosmo_2(y, lna, dydlna, k, ha_lna, eta_lna, taudot_lna, lmax, H0,
                      omega_r_0, omega_m_0, omega_k_0, omega_l_0, rh,
                      omega_gam, omega_neu, omega_dm_0, omega_b_0, xc_damp,
                      a_tca):
        a = np.exp(lna)
        r_bph_a = 3. / 4. * omega_b_0 / omega_gam * a
        idx_f = (lna - ha_lna[0]) / (ha_lna[2] - ha_lna[0]) * 2.
        idx_i = np.floor(idx_f)
        idx = np.int_(idx_i)
        ratio = idx_f - idx_i
        eta = (1 - ratio) * eta_lna[idx] + ratio * eta_lna[idx + 1]
        ha = (H0 * (omega_r_0 * a**-4 + omega_m_0 * a**-3 + omega_k_0 * a**-2 +
                    omega_l_0)**0.5) / H0 / rh
        tdot = (1 - ratio) * taudot_lna[idx] + ratio * taudot_lna[idx + 1]
        psi = -y[0] - 12. / (rh * k * a)**2 * (omega_gam * y[9] +
                                               omega_neu * y[6 + 2 * lmax + 3])
        dphidlna = psi - k**2 / (3. * a**2 * ha**2) * y[0] + 0.5 / (
            ha * rh)**2 * (omega_dm_0 * a**(-3) * y[1] + omega_b_0 * a**
                           (-3) * y[3] + 4. * omega_gam * a**(-4) * y[5] +
                           4. * omega_neu * a**(-4) * y[6 + 2 * lmax + 1])
        ppi = y[9] + y[6] + y[10]
        n_y = 8 + 3 * lmax
        dydlna[:] = 0
        dydlna[0] = dphidlna
        dydlna[1] = -k / (a * ha) * y[2] - 3. * dphidlna
        dydlna[2] = -y[2] + k / (a * ha) * psi
        dydlna[3] = -k / (a * ha) * y[4] - 3. * dphidlna
        dydlna[5] = -k / (a * ha) * y[7] - dphidlna
        dydlna[6] = k / (a * ha) * (-y[8]) + tdot / (a * ha) * (y[6] -
                                                                ppi / 2.)
        if a < a_tca:
            dh_dlna = -1. / (2. * ha) * (4. * omega_r_0 * a**-4 +
                                         3. * omega_m_0 * a**-3 +
                                         2. * omega_k_0 * a**-2) / rh**2
            slip = 2. / (1. + r_bph_a) * (y[4] - 3. * y[7]) + 1. / tdot / (
                1 + 1. / r_bph_a) * ((2. * a * ha + a * dh_dlna) * y[4] + k *
                                     (2. * y[5] + psi) + k * dydlna[5])
            dydlna[4] = -y[4] / (1. + 1. / r_bph_a) + k / (a * ha) * (
                (y[5] - 2. * y[9]) /
                (1. + r_bph_a) + psi) + slip / (1. + r_bph_a)
            dydlna[7] = k / (3. * a * ha) * (
                y[5] - 2. * y[9] +
                (1. + r_bph_a) * psi) - r_bph_a / 3. * (dydlna[4] + y[4])
            dydlna[8] = k / (a * ha) / 3. * (y[6] - 2. * y[10]) + tdot / (
                a * ha) * (y[8])
        if a >= a_tca:
            dydlna[4] = -y[4] + k / (a * ha) * psi + tdot / r_bph_a / (
                a * ha) * (y[4] - 3. * y[7])
            dydlna[7] = k / (3. * a * ha) * (y[5] - 2. * y[9] + psi) + tdot / (
                a * ha) * (y[7] - y[4] / 3.)
            dydlna[8] = k / (a * ha) / 3. * (y[6] - 2. * y[10]) + tdot / (
                a * ha) * (y[8])
            dydlna[9] = k / (5. * a * ha) * (2. * y[7] - 3. * y[11]) + tdot / (
                a * ha) * (y[9] - ppi / 10.)
            dydlna[10] = k / (a * ha) / 5. * (
                2. * y[8] - 3. * y[12]) + tdot / (a * ha) * (y[10] - ppi / 5.)
            for i in range(3, lmax):
                dydlna[5 + 2 * i] = k / (a * ha) / (2. * i + 1.) * (
                    i * y[5 + 2 * (i - 1)] -
                    (i + 1.) * y[5 + 2 *
                                 (i + 1)]) + tdot / (a * ha) * y[5 + 2 * i]
                dydlna[6 + 2 * i] = k / (a * ha) / (2. * i + 1.) * (
                    i * y[6 + 2 * (i - 1)] -
                    (i + 1.) * y[6 + 2 *
                                 (i + 1)]) + tdot / (a * ha) * y[6 + 2 * i]
            dydlna[5 +
                   2 * lmax] = 1. / (a * ha) * (k * y[5 + 2 * (lmax - 1)] - (
                       (lmax + 1.) / eta - tdot) * y[5 + 2 * lmax])
            dydlna[6 +
                   2 * lmax] = 1. / (a * ha) * (k * y[6 + 2 * (lmax - 1)] - (
                       (lmax + 1.) / eta - tdot) * y[6 + 2 * lmax])

        dydlna[6 + 2 * lmax +
               1] = -k / (a * ha) * y[6 + 2 * lmax + 2] - dphidlna
        dydlna[6 + 2 * lmax +
               2] = k / (3. * a * ha) * (y[6 + 2 * lmax + 1] -
                                         y[6 + 2 * lmax + 3] + psi)
        for j in range(2, lmax):
            dydlna[6 + 2 * lmax + 1 + j] = k / (a * ha) / (
                2. * j + 1.) * (j * y[6 + 2 * lmax + 1 + j - 1] -
                                (j + 1.) * y[6 + 2 * lmax + 1 + j + 1])
        dydlna[6 + 2 * lmax + 1 + lmax] = 1. / (
            a * ha) * (k * y[6 + 2 * lmax + 1 + lmax - 1] -
                       (lmax + 1.) / eta * y[6 + 2 * lmax + 1 + lmax])
        if xc_damp > 0:
            tanhArg = (k * eta - xc_damp) / 50.
            tanhA = np.fabs(tanhArg)
            tanhB = 1.26175667589988239 + tanhA * (-0.54699348440059470 +
                                                   tanhA * 2.66559097474027817)
            damping = (1. - tanhB * tanhArg / (tanhB * tanhA + 1)) / 2.
            dydlna[5:n_y - 1] = dydlna[5:n_y - 1] * damping
        return dydlna
Example #58
0
def clean_windows(X,
                  sfreq,
                  max_bad_chans=0.2,
                  zthresholds=[-3.5, 5],
                  win_len=.5,
                  win_overlap=0.66,
                  min_clean_fraction=0.25,
                  max_dropout_fraction=0.1,
                  show=False):
    """Remove periods with abnormally high-power content from continuous data.

    This function cuts segments from the data which contain high-power
    artifacts. Specifically, only windows are retained which have less than a
    certain fraction of "bad" channels, where a channel is bad in a window if
    its power is above or below a given upper/lower threshold (in standard
    deviations from a robust estimate of the EEG power distribution in the
    channel).

    Parameters
    ----------
    X : array, shape=(n_channels, n_samples)
        Continuous data set, assumed to be appropriately high-passed (e.g. >
        1Hz or 0.5Hz - 2.0Hz transition band)
    max_bad_chans : float
        The maximum number or fraction of bad channels that a retained window
        may still contain (more than this and it is removed). Reasonable range
        is 0.05 (very clean output) to 0.3 (very lax cleaning of only coarse
        artifacts) (default=0.2).
    zthresholds : 2-tuple
        The minimum and maximum standard deviations within which the power of a
        channel must lie (relative to a robust estimate of the clean EEG power
        distribution in the channel) for it to be considered "not bad".
        (default=[-3.5, 5]).

    The following are detail parameters that usually do not have to be tuned.
    If you can't get the function to do what you want, you might consider
    adapting these to your data.

    win_len : float
        Window length that is used to check the data for artifact content. This
        is ideally as long as the expected time scale of the artifacts but not
        shorter than half a cycle of the high-pass filter that was used.
        Default: 1.
    win_overlap : float
        Window overlap fraction. The fraction of two successive windows that
        overlaps. Higher overlap ensures that fewer artifact portions are going
        to be missed, but is slower (default=0.66).
    max_dropout_fraction : float
        Maximum fraction that can have dropouts. This is the maximum fraction
        of time windows that may have arbitrarily low amplitude (e.g., due to
        the sensors being unplugged) (default=0.1).
    min_clean_fraction : float
        Minimum fraction that needs to be clean. This is the minimum fraction
        of time windows that need to contain essentially uncontaminated EEG.
        (default=0.25)

    The following are expert-level parameters that you should not tune unless
    you fully understand how the method works.

    truncate_quant :
        Truncated Gaussian quantile. Quantile range [upper,lower] of the
        truncated Gaussian distribution that shall be fit to the EEG contents.
        (default=[0.022, 0.6])
    step_sizes :
        Grid search stepping. Step size of the grid search, in quantiles;
        separately for [lower,upper] edge of the truncated Gaussian. The lower
        edge has finer stepping because the clean data density is assumed to be
        lower there, so small changes in quantile amount to large changes in
        data space (default=[0.01 0.01]).
    shape_range :
        Shape parameter range. Search range for the shape parameter of the
        generalized Gaussian distribution used to fit clean EEG (default:
        1.7:0.15:3.5).

    Returns
    -------
    clean : array, shape=(n_channels, n_samples)
        Dataset with bad time periods removed.
    sample_mask : boolean array, shape=(1, n_samples)
        Mask of retained samples (logical array).

    """
    assert 0 < max_bad_chans < 1, "max_bad_chans must be a fraction !"

    truncate_quant = [0.0220, 0.6000]
    step_sizes = [0.01, 0.01]
    shape_range = np.linspace(1.7, 3.5, 13)
    max_bad_chans = np.round(X.shape[0] * max_bad_chans)

    [nc, ns] = X.shape
    N = int(win_len * sfreq)
    offsets = np.int_(np.arange(0, ns - N, np.round(N * (1 - win_overlap))))
    logging.debug('[ASR] Determining channel-wise rejection thresholds')

    wz = np.zeros((nc, len(offsets)))
    for ichan in range(nc):
        x = X[ichan, :]**2
        Y = []
        for o in offsets:
            Y.append(np.sqrt(np.sum(x[o:o + N]) / N))

        mu, sig, alpha, beta = fit_eeg_distribution(Y, min_clean_fraction,
                                                    max_dropout_fraction,
                                                    truncate_quant, step_sizes,
                                                    shape_range)
        wz[ichan] = (Y - mu) / sig

    # sort z scores into quantiles
    wz[np.isnan(wz)] = np.inf  # Nan to inf
    swz = np.sort(wz, axis=0)

    # determine which windows to remove
    if np.max(zthresholds) > 0:
        mask1 = swz[-(np.int(max_bad_chans) + 1), :] > np.max(zthresholds)
    if np.min(zthresholds) < 0:
        mask2 = (swz[1 + np.int(max_bad_chans - 1), :] < np.min(zthresholds))

    bad_by_mad = mad(wz, c=1, axis=0) < .1
    bad_by_std = np.std(wz, axis=0) < .1
    mask3 = np.logical_or(bad_by_mad, bad_by_std)

    remove_mask = np.logical_or.reduce((mask1, mask2, mask3))
    removed_wins = np.where(remove_mask)

    sample_maskidx = []
    for i in range(len(removed_wins[0])):
        if i == 0:
            sample_maskidx = np.arange(offsets[removed_wins[0][i]],
                                       offsets[removed_wins[0][i]] + N)
        else:
            sample_maskidx = np.vstack(
                (sample_maskidx,
                 np.arange(offsets[removed_wins[0][i]],
                           offsets[removed_wins[0][i]] + N)))

    sample_mask2remove = np.unique(sample_maskidx)
    clean = np.delete(X, sample_mask2remove, 1)
    sample_mask = np.ones((1, ns), dtype=bool)

    if sample_mask2remove.size:
        sample_mask[0, sample_mask2remove] = False

    if show:
        import matplotlib.pyplot as plt
        f, ax = plt.subplots(nc, sharex=True, figsize=(8, 5))
        times = np.arange(ns) / float(sfreq)
        for i in range(nc):
            ax[i].fill_between(times,
                               0,
                               1,
                               where=sample_mask.flat,
                               transform=ax[i].get_xaxis_transform(),
                               facecolor='none',
                               hatch='...',
                               edgecolor='k',
                               label='selected window')
            ax[i].plot(times, X[i], lw=.5, label='EEG')
            ax[i].set_ylim([-50, 50])
            # ax[i].set_ylabel(raw.ch_names[i])
            ax[i].set_yticks([])
        ax[i].set_xlabel('Time (s)')
        ax[i].set_ylabel(f'ch{i}')
        ax[0].legend(fontsize='small',
                     bbox_to_anchor=(1.04, 1),
                     borderaxespad=0)
        plt.subplots_adjust(hspace=0, right=0.75)
        plt.suptitle('Clean windows')
        plt.show()

    return clean, sample_mask
def pipeline(image):

    #### UNDISTORTING THE IMAGES/FRAMES ####

    ret, mtx, dist, rvecs, tvecs = cv2.calibrateCamera(objpoints, imgpoints,
                                                       gray.shape[::-1], None,
                                                       None)

    undist = cv2.undistort(
        image, mtx, dist, None, mtx
    )  #using the parameters generated from objpoints and imagepoints to undistort images

    #### APPLYING THRESHOLDS TO IMAGE/FRAME ####

    r_thresh = (205, 255)
    s_thresh = (170, 255)
    sx_thresh = (20, 100)

    img = np.copy(undist)

    hls = cv2.cvtColor(img, cv2.COLOR_RGB2HLS).astype(
        np.float)  #using HLS color space (L-Channel, S-Channel)
    l_channel = hls[:, :, 1]
    s_channel = hls[:, :, 2]

    r_channel = img[:, :, 0]

    sobelx = cv2.Sobel(l_channel, cv2.CV_64F, 1, 0)  #applying sobelx
    abs_sobelx = np.absolute(sobelx)
    scaled_sobel = np.uint8(255 * abs_sobelx / np.max(abs_sobelx))

    sxbinary = np.zeros_like(scaled_sobel)
    sxbinary[(scaled_sobel >= sx_thresh[0])
             & (scaled_sobel <= sx_thresh[1])] = 1

    r_binary = np.zeros_like(r_channel)  #using R-Channel for color threshold
    r_binary[(r_channel >= r_thresh[0]) & (r_channel <= r_thresh[1])] = 1

    s_binary = np.zeros_like(s_channel)  #using S-Channel for color threshold
    s_binary[(s_channel >= s_thresh[0]) & (s_channel <= s_thresh[1])] = 1

    binary = np.zeros_like(sxbinary)
    binary[(s_binary == 1) | (sxbinary == 1) |
           (r_binary == 1)] = 1  #combining the thresholds to one threshold

    #### APPLYING WARP-FUNCTION ON IMAGE FOR "BIRD-EYE-VIEW" ####

    src = np.float32([[585, 460], [203, 720], [1127, 720],
                      [705, 460]])  #points on the original image
    dst = np.float32([[320, 0], [320, 720], [960, 720],
                      [960, 0]])  #destination point on the warped image

    M = cv2.getPerspectiveTransform(src,
                                    dst)  #calculate matrix for transformation

    imshape = image.shape

    binary_warped1 = cv2.warpPerspective(
        binary, M,
        (imshape[1], imshape[0]))  #image transformation to bird-eye-view
    #cv2.line(binary_warped1, (320,0),(320,720), (255,0,0))
    #plt.imshow(binary_warped1)

    #### APPLYING A REGION OF INTEREST ON IMAGE/FRAME TO DISINCLUDE BRIGHT HIGHWAY WALLS AND LIGHT WHEELS ####

    mask2 = np.zeros_like(binary_warped1)
    vertices2 = np.array([[(230, imshape[0]), (imshape[1] - 230, imshape[0]),
                           (imshape[1] - 80, 0), (200, 0)]],
                         dtype=np.int32)
    cv2.fillPoly(mask2, vertices2, 255)
    binary_warped2 = cv2.bitwise_and(
        binary_warped1, mask2)  #mask areas on the sides of the car

    mask3 = np.zeros_like(binary_warped1)
    vertices3 = np.array([[(0, 720), (500, 720), (650, 0), (0, 0)]],
                         dtype=np.int32)
    cv2.fillPoly(mask3, vertices3, 255)
    binary_warped3 = cv2.bitwise_and(
        binary_warped2, mask3
    )  #mask area inside lane (experiment to improve challenge video, not neccessary in project video)

    mask4 = np.zeros_like(binary_warped1)
    vertices4 = np.array([[(1280, 720), (900, 720), (800, 0), (1280, 0)]],
                         dtype=np.int32)
    cv2.fillPoly(mask4, vertices4, 255)
    binary_warped4 = cv2.bitwise_and(binary_warped2,
                                     mask4)  #again masking inside of the lane

    binary_warped = np.zeros_like(binary_warped1)
    binary_warped[(binary_warped3 == 1) |
                  (binary_warped4 == 1)] = 1  #combining both masks to one

    #### APPLYING HISTOGRAM TO SEARCH FOR PEAKS ####

    histogram = np.sum(binary_warped[0:720, :], axis=0)  #applying histogram

    out_img = np.dstack((binary_warped, binary_warped, binary_warped)) * 255

    midpoint = np.int(histogram.shape[0] /
                      2)  #setting all up for first starting points
    leftx_base = np.argmax(histogram[:midpoint])
    rightx_base = np.argmax(histogram[midpoint:]) + midpoint

    nwindows = 9  #number of windows

    window_height = np.int(binary_warped.shape[0] / nwindows)

    nonzero = binary_warped.nonzero()  #identify all white pixels
    nonzeroy = np.array(nonzero[0])
    nonzerox = np.array(nonzero[1])

    leftx_current = leftx_base
    rightx_current = rightx_base

    margin = 130  #window margin

    minpix = 50  #minimum pixel to be found to recenter window

    left_lane_inds = []
    right_lane_inds = []

    for window in range(nwindows):  #step through windows one by one

        win_y_low = binary_warped.shape[0] - (window + 1) * window_height
        win_y_high = binary_warped.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

        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)

        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]

        left_lane_inds.append(good_left_inds)  #append good indices
        right_lane_inds.append(good_right_inds)

        if len(good_left_inds) > minpix:
            leftx_current = np.int(
                np.mean(nonzerox[good_left_inds]
                        ))  #recenter window when more pixels found then minpix
        if len(good_right_inds) > minpix:
            rightx_current = np.int(np.mean(nonzerox[good_right_inds]))

    left_lane_inds = np.concatenate(left_lane_inds)
    right_lane_inds = np.concatenate(right_lane_inds)

    leftx = nonzerox[left_lane_inds]  #extract x and y from found points
    lefty = nonzeroy[left_lane_inds]
    rightx = nonzerox[right_lane_inds]
    righty = nonzeroy[right_lane_inds]

    left_fit = np.polyfit(
        lefty, leftx, 2)  #set a second order function to left and right points
    right_fit = np.polyfit(righty, rightx, 2)

    ploty = np.linspace(0, binary_warped.shape[0] - 1, binary_warped.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)

    #### SEARCHING FOR NEW PEAKS IN DEFINED AREA AROUND THE PREVIOUS LINE POSITION ####

    nonzero = binary_warped.nonzero(
    )  #searching for new points in a margin of 80
    nonzeroy = np.array(nonzero[0])
    nonzerox = np.array(nonzero[1])
    margin = 80
    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)))

    leftx = nonzerox[left_lane_inds]  #extract x and y from found points
    lefty = nonzeroy[left_lane_inds]
    rightx = nonzerox[right_lane_inds]
    righty = nonzeroy[right_lane_inds]

    left_fit = np.polyfit(lefty, leftx,
                          2)  #second order function to left and right points
    right_fit = np.polyfit(righty, rightx, 2)

    ploty = np.linspace(0, binary_warped.shape[0] - 1,
                        binary_warped.shape[0])  #generate values for plotting
    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 = np.dstack((binary_warped, binary_warped, binary_warped)) * 255
    window_img = np.zeros_like(out_img)

    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]

    left_line_window1 = np.array([
        np.transpose(np.vstack([left_fitx - margin, ploty]))
    ])  #generate a polygon to illustrate the search window area
    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))

    cv2.fillPoly(window_img, np.int_([left_line_pts]),
                 (0, 255, 0))  #draw the lane onto the warped blank image
    cv2.fillPoly(window_img, np.int_([right_line_pts]), (0, 255, 0))
    result = cv2.addWeighted(out_img, 1, window_img, 0.3, 0)
    #plt.imshow(result)
    #plt.plot(left_fitx, ploty, color='yellow')
    #plt.plot(right_fitx, ploty, color='yellow')
    #plt.xlim(0, 1280)
    #plt.ylim(720, 0)

    #### APPLYING FINAL LINE AND MEASURING LINE CURVATURE ####

    ploty = np.linspace(0, binary_warped.shape[0] - 1, binary_warped.shape[0])

    leftx = np.array(
        [left_fit[2] + (y**2) * left_fit[0] + left_fit[1] * y for y in ploty])
    rightx = np.array([
        right_fit[2] + (y**2) * right_fit[0] + right_fit[1] * y for y in ploty
    ])

    left_fit = np.polyfit(ploty, leftx, 2)  #fit a second order polynomial
    left_fitx = left_fit[0] * ploty**2 + left_fit[1] * ploty + left_fit[2]
    right_fit = np.polyfit(ploty, rightx, 2)
    right_fitx = right_fit[0] * ploty**2 + right_fit[1] * ploty + right_fit[2]

    #mark_size = 3
    #plt.plot(leftx, ploty, 'o', color='red', markersize=mark_size)
    #plt.plot(rightx, ploty, 'o', color='blue', markersize=mark_size)
    #plt.xlim(0, 1280)
    #plt.ylim(0, 720)
    #plt.plot(left_fitx, ploty, color='green', linewidth=3)
    #plt.plot(right_fitx, ploty, color='green', linewidth=3)
    #plt.gca().invert_yaxis()

    y_eval = np.max(ploty)
    left_curverad = (
        (1 + (2 * left_fit[0] * y_eval + left_fit[1])**2)**1.5) / np.absolute(
            2 * left_fit[0])  #calculate curvature in pixels
    right_curverad = ((1 + (2 * right_fit[0] * y_eval + right_fit[1])**2)**
                      1.5) / np.absolute(2 * right_fit[0])
    #print(left_curverad, right_curverad)

    ym_per_pix = 30 / 720  #convert pixels to meters
    xm_per_pix = 3.7 / 700

    left_fit_cr = np.polyfit(ploty * ym_per_pix, leftx * xm_per_pix, 2)
    right_fit_cr = np.polyfit(ploty * ym_per_pix, rightx * xm_per_pix, 2)

    left_curverad = (
        (1 + (2 * left_fit_cr[0] * y_eval * ym_per_pix + left_fit_cr[1])**2)**
        1.5) / np.absolute(2 * left_fit_cr[0])  #calculate curvature in meters
    right_curverad = (
        (1 + (2 * right_fit_cr[0] * y_eval * ym_per_pix + right_fit_cr[1])**2)
        **1.5) / np.absolute(2 * right_fit_cr[0])

    #print(left_curverad, 'm', right_curverad, 'm')
    curvature = (float(left_curverad) + float(right_curverad)
                 ) / 2  #calculate mean curvature in meters

    #### PUTTING ALL BACK TOGETHER AND INVERSE PERSPECTIVE ####

    warp_zero = np.zeros_like(binary_warped).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))

    Minv = np.linalg.inv(M)  #inverse matrix

    newwarp = cv2.warpPerspective(
        color_warp, Minv,
        (image.shape[1],
         image.shape[0]))  #warp the image back to original image space

    result = cv2.addWeighted(undist, 1, newwarp, 0.3, 0)  #output the result

    #### APPLYING TEXT TO IMAGE/FRAME FOR CURVATURE AND DISTANCE TO LANE CENTER ####

    font = cv2.FONT_HERSHEY_DUPLEX
    text = "Radius of Curvature: {} m".format(int(curvature))  #print curvature
    cv2.putText(result, text, (360, 100), font, 1, (255, 255, 255), 2)

    pts = np.transpose(np.nonzero((newwarp[:, :, 1])))

    center_cam = imshape[1] / 2

    try:
        left_low = np.min(
            pts[(pts[:, 1] < center_cam) & (pts[:, 0] > 680)][:, 1]
        )  #taking the farest point away from center on the left side (the lowest x value)
        right_high = np.max(
            pts[(pts[:, 1] > center_cam) & (pts[:, 0] > 680)][:, 1]
        )  #taking the farest point away from center on the right site (the highest x value)
        center = (
            left_low + right_high
        ) / 2  #taking just points above 680 (near to car), then add them to together and divide by 2

        position = (
            center_cam - center
        ) * xm_per_pix  #converting values to meters and small if function so that values are always postive
        if position > 0:
            text = "Vehicle is {:.2f} m right of center".format(position)
        else:
            text = "Vehicle is {:.2f} m left of center".format(-position)
        cv2.putText(result, text, (360, 150), font, 1, (255, 255, 255), 2)
    except ValueError:
        pass

    #plt.imshow(result, cmap = 'gray')    #output the result with text on it
    #plt.savefig('output_images/test1_out_img3.jpg')
    #cv2.imwrite('output_images/test1_binary.jpg', binary)
    return result
def fit_polynomial(binary_warped, isFirstImage):

    # Grab left_fit & right_fit
    global left_fit, right_fit

    # HYPERPARAMETERS
    # Number of sliding windows
    nwindows = 9
    # Width of the windows +/- margin
    margin = 100
    # Minimum number of pixels found to recenter window
    minpix = 50

    # Identify the x and y positions of all nonzero (i.e. activated) pixels in the image
    nonzero = binary_warped.nonzero()
    nonzeroy = np.array(nonzero[0])
    nonzerox = np.array(nonzero[1])

    # If first image, find lane pixels
    if (isFirstImage):

        left_lane_inds, right_lane_inds = find_lane_pixels_initial(
            binary_warped, nwindows, margin, minpix, nonzero, nonzeroy,
            nonzerox)
    # If not first image, search near previous polynomial
    else:
        left_lane_inds, right_lane_inds = find_lane_pixels(
            binary_warped, margin, nonzero, nonzeroy, nonzerox)

    # 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 a second order polynomial to each with np.polyfit() #
    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, binary_warped.shape[0] - 1, binary_warped.shape[0])
    # Calc both polynomials using ploty, left_fit and right_fit ###
    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]

    # Calculate vehicle center offset
    midpoint = (left_fitx[-1] + right_fitx[-1]) // 2
    # veh_pos = img.shape[1]//2
    # xm_per_pix = 3.7/680
    # dx = (veh_pos - midpoint)*xm_per_pix

    ## 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)

    # Draw detected lanes
    dr_window_img = np.zeros_like(out_img)
    dr_left_line_pts = np.array(
        [np.flipud(np.transpose(np.vstack([left_fitx, ploty])))])
    dr_right_line_pts = np.array(
        [np.transpose(np.vstack([right_fitx, ploty]))])
    dr_line_pts = np.hstack((dr_left_line_pts, dr_right_line_pts))
    cv2.fillPoly(dr_window_img, np.int_([dr_line_pts]),
                 (0, 255, 0))  # Green: Area between two lanes
    cv2.fillPoly(dr_window_img, np.int_([dr_left_line_pts]),
                 (255, 0, 0))  # Red (RGB): left lane
    cv2.fillPoly(dr_window_img, np.int_([dr_right_line_pts]),
                 (0, 0, 255))  # Blue (RGB): right lane

    # Reverse Warp
    img_lanes = perspective_transform(dr_window_img, rev=True)

    return result, ploty, midpoint, img_lanes