def roitodata(save_fld):

    # Local Variables: I_cum, bkg, save_fld, k, j, l, m_bloc, I, background, avg, data
    # Function calls: load, save, nanmean, CC, NaN, cd, length, importdata, roitodata, mean, dir, size
    #% ROITODATA(save_fld) loads CC from save_fld for data extraction from I.mat
    #%Example:
    #% [data,bkg] = roitodata(save_fld)
    os.chdir(save_fld)
    np.load('CC.mat')
    m_bloc = dir('*I*.mat')
    data = np.array([])
    background = np.array([])
    avg = np.array([])
    bkg = np.array([])
    for j in np.arange(1., (length(m_bloc))+1):
        I_cum = importdata((m_bloc[int(j)-1].name))
        for k in np.arange(1., (length(I_cum))+1):
            #%looping through files
            
        data = np.array(np.hstack((data)))
        background = np.array(np.hstack((background, bkg)))
        
    plt.save('data.mat', 'data')
    plt.save('bkg.mat', 'background')
    return [data, bkg]
def average_face(imgpaths,
                 dest_filename=None,
                 width=500,
                 height=500,
                 background='black',
                 blur_edges=False,
                 out_filename='result.png'):
    size = (height, width)

    images = []
    point_set = []
    for path in imgpaths:
        img, points = load_image_points(path, size)
        if img is not None:
            images.append(img)
            point_set.append(points)

    if len(images) == 0:
        raise FileNotFoundError(
            'Could not find any valid images. Supported formats are .jpg, .png, .jpeg'
        )

    if dest_filename is not None:
        dest_img, dest_points = load_image_points(dest_filename, size)
        if dest_img is None or dest_points is None:
            raise Exception('No face or detected face points in dest img: ' +
                            dest_filename)
    else:
        dest_img = np.zeros(images[0].shape, np.uint8)
        dest_points = locator.average_points(point_set)

    num_images = len(images)
    result_images = np.zeros(images[0].shape, np.float32)
    for i in range(num_images):
        result_images += warper.warp_image(images[i], point_set[i],
                                           dest_points, size, np.float32)

    result_image = np.uint8(result_images / num_images)
    face_indexes = np.nonzero(result_image)
    dest_img[face_indexes] = result_image[face_indexes]

    mask = blender.mask_from_points(size, dest_points)
    if blur_edges:
        blur_radius = 10
        mask = cv2.blur(mask, (blur_radius, blur_radius))

    if background in ('transparent', 'average'):
        dest_img = np.dstack((dest_img, mask))

        if background == 'average':
            average_background = np.uint8(locator.average_points(images))
            dest_img = blender.overlay_image(dest_img, mask,
                                             average_background)

    print('Averaged {} images'.format(num_images))
    plt = plotter.Plotter(False, num_images=1, out_filename=out_filename)
    plt.save(dest_img)
Exemple #3
0
def ncc(seg, width):

    # Local Variables: B, hash, y, i, siz, s, N, seg, width, S, cacheDir, u, x, Nu, xs, ys, uniq, o2, o1
    # Function calls: load, hashMat, unique, false, floor, sum, nan, uint8, ceil, uniqfilt, length, save, ncc, find, bwlabel, size
    #% N = ncc( seg, w )
    #%
    #% N(i,j) = number of connected components in the
    #%   w-by-w window centered on location (i,j) of seg
    #%
    #% What do I mean by "number of connected components"?
    #% Here are some example segmentation patches:
    #%
    #%   1 1 2 2 1 1
    #%   1 1 2 2 1 1      this patch has THREE segments, even
    #%   1 1 2 2 1 1      though unique() only returns [1;2].
    #%   1 1 2 2 1 1
    #%
    #%   3 3 7 7 7 7
    #%   3 3 7 7 7 7      this patch has FOUR segments, even
    #%   7 7 3 3 3 3      though unique() is simply [3;7].
    #%   7 7 3 3 3 3
    #%
    #% THIS CODE IS VERY SLOW, hence all results are cached.
    cacheDir = '/home/ashishmenon/labpc/oef/cache/clust/nccCache/'
    hash = hashMat(
        np.array(np.vstack((np.hstack((seg.flatten(1))), np.hstack((width))))))
    try:
        np.load(np.array(np.hstack((cacheDir, hash))), 'N')
    except:
        siz = matcompat.size(seg)
        o1 = np.floor(((width - 1.) / 2.))
        o2 = np.ceil(((width - 1.) / 2.))
        #% This takes ~80 seconds per image
        N = np.nan(siz)
        B = false(siz)
        B[int(o1 + 1.) - 1:0 - o2, int(o1 + 1.) - 1:0 - o2] = 1.
        Nu = uniqfilt(seg, o2)
        N[int(np.logical_and(B, Nu == 1.)) - 1] = 1.
        [ys, xs] = nonzero(np.logical_and(B, Nu > 1.))
        for i in np.arange(1., (length(ys)) + 1):
            y = ys[int(i) - 1]
            x = xs[int(i) - 1]
            S = seg[int(y - o1) - 1:y + o2, int(x - o1) - 1:x + o2]
            uniq = np.unique(S)
            N[int(y) - 1, int(x) - 1] = 0.
            for s in uniq.flatten(0).conj():
                u = np.unique(bwlabel((S == s), 4.))
                N[int(y) - 1,
                  int(x) - 1] = N[int(y) - 1, int(x) - 1] + np.sum((u != 0.))

        #% convert to uint8 to save disk space
        #% (but this converts NaNs to 0s..)
        N = np.uint8(N)
        plt.save(np.array(np.hstack((cacheDir, hash))), 'N')

    return [N]
def stackstoroi(save_fld, sd):

    # Local Variables: I_bw2, I_cum, I_bw1, I, I_overlay, locs, CC, i,
    # j, img_content, I_edge, m_bloc, I_mean, marks, save_fld, sd
    # Function calls: save, stackstoroi, mad, findpeaks, cat, bwareaopen,
    #  length, edge, bwconncomp, importdata, max, threshold, mode, cd, dir, mean
    #% STACKSTOROI(image_fld,sd) loads *I*.mat from save_fld for image
    #% segmentation. sd is how many standard deviations above the mean the 
    #% threshold is set for segmentation.  Outputs:
    #% CC: connected components found in I_bw2
    #% I_mean: averaged image of *I*.mat
    #% I_bw2: binary image containing image segementation of *I*.mat
    #% I_overlay: overlay of identified ROI over I_mean
    #%Example:
    #% [CC,I_mean,I_bw2,I_overlay] = stackstoroi(save_fld,sd);
    chdir(save_fld)
    m_bloc = listdir('*I*.mat*')
    i_cum = np.array([])
    img_content = np.array([])
    if length(m_bloc) == 11:
        for i in np.arange(1, 12.0):
            i_cum = cat(3, i_cum, importdata((m_bloc[int(i)-1].name)))
            
    else:
        I_cum = importdata((m_bloc[0].name))
        
    
    for j in np.arange(1., (length(I_cum))+1):
        I = I_cum[:,:,int(j)-1]
        img_content[int(j)-1] = np.mean(I.flatten(1))
        
    [marks, locs] = findpeaks(img_content, 'minpeakdistance', 100.)
    I_mean = np.mean(I_cum[:,:,int(locs)-1], 3.)
    #%I_bw1 = bwspecial(otsu(I_mean,3));
    I_bw1 = threshold(I_mean, (mode(I_mean.flatten(1))+np.dot(sd, mad(I_mean.flatten(1), 1.))))
    I_bw2 = bwareaopen(I_bw1, 20., 4.)
    CC = bwconncomp(I_bw2)
    I_edge = edge(I_bw2)
    I_overlay = I_mean
    I_overlay[int(I_edge)-1] = 10.*matcompat.max(I_mean.flatten(1))/9.
    plt.save('CC.mat', 'CC')
    return [CC, I_mean, I_bw2, I_overlay]
Exemple #5
0
def vis_eigen_cutoff(refvec,
                     evc_avg,
                     cutoff_list,
                     G,
                     figdir="",
                     savestr="StyleGAN2_proj",
                     RND=None):
    if RND is None: RND = np.random.randint(10000)
    samp_n = refvec.shape[0]
    codes_all = [refvec]
    for cutoff in cutoff_list:
        refvec_proj = refvec @ evc_avg[:, -cutoff:] @ evc_avg[:, -cutoff:].T
        codes_all.append(refvec_proj)
    codes_all = np.concatenate(tuple(codes_all), axis=0)
    img_all = G.visualize_batch_np(codes_all)
    mtg = make_grid(img_all, nrow=samp_n)
    ToPILImage()(mtg).show()
    ctf_str = "_".join([str(ct) for ct in cutoff_list])
    plt.save(
        join(figdir, "%s_%s_%04d.png" % (savestr, ctf_str, RND)),
        mtg.numpy(),
    )
    return mtg
def picture(df, plt, bizhong):
    x_data = list(map(str, df['时间']))
    print(x_data)
    y_maijia_data = df['买价']
    y_maioutjia_data = df['卖价']
    min_number = y_maijia_data.min()
    max_number = y_maioutjia_data.max()
    print(min_number, max_number)
    xiaoshu_number = str(10000 * (max_number - min_number)).split('.')[0]
    print(xiaoshu_number)
    mid_num = 5 - len(str(xiaoshu_number))
    if mid_num:
        step = 0.1**mid_num
    else:
        step = 1 / int(xiaoshu_number[0])

    y_kedu = [min_number - step * 2 + i * step for i in range(0, 10)]
    print(y_kedu)

    plt.clf()  # 清除刷新前的图表,防止数据量过大消耗内存
    fig, ax = plt.subplots(1, 1)
    plt.plot(x_data, y_maijia_data, 'r')
    plt.plot(x_data, y_maioutjia_data, 'g')
    plt.xticks(x_data, rotation=90)
    plt.yticks(y_kedu)
    for label in ax.get_xticklabels():
        label.set_visible(False)
    for label in ax.get_xticklabels()[::5]:
        label.set_visible(True)
    plt.xlabel('时间', fontproperties=font_set)  # 设置x轴名称
    plt.ylabel('价格', fontproperties=font_set)  # 设置y轴名称
    plt.title(df['币种'].values[0], fontproperties=font_set)  # 设置图片名称
    plt.legend(['买价', '卖价'], loc="upper right", prop=font_set)
    if len(x_data) == 60:
        plt.save('%s.jpg' % bizhong)
    plt.show()
Exemple #7
0
def findbruteforcefit(kit=None, *args, **kwargs):

    if kit != 1:
        pylab.save('bftfile', 'kit')
    else:
        pylab.load('bftfile', 'kit')
        kit['tightnesssettings'] = copy(
            settingsfromtightness(kit['tightnesssettings']['scalartightness']))


# findbruteforcefit.m:6

    random(1)
    ABCexact = array([8572.0553, 3640.1063, 2790.9666])
    # findbruteforcefit.m:9
    ABCguess = array([8572.0, 3640.0, 2790.0])
    # findbruteforcefit.m:10

    ABCguess = multiply(ABCguess, array([0.99, 1.01, 0.99]))
    # findbruteforcefit.m:11
    kit = trimkit(kit,
                  kit['tightnesssettings']['bruteforce']['numexperimentlines'])
    # findbruteforcefit.m:12
    flimits = array([min(kit['onedpeakfs']), max(kit['onedpeakfs'])])
    # findbruteforcefit.m:14
    theoryset = linesforbruteforce2(
        ABCguess, flimits,
        kit['tightnesssettings']['bruteforce']['numtheorylines'],
        max(kit['onedpeakhsunassigned']))
    # findbruteforcefit.m:15
    linestouse['lines'] = copy(theoryset)
    # findbruteforcefit.m:17
    linestouse['heighttouse'] = copy('sixKweakpulsestrength')
    # findbruteforcefit.m:18
    linestouse['fitdescriptor'] = copy('made up brute force fit')
    # findbruteforcefit.m:19
    linestouse['ABCxxxxx'] = copy(array([ABCguess, 0, 0, 0, 0, 0]))
    # findbruteforcefit.m:20
    #addC13swithlinelist

    ABClist = [ABCguess]
    # findbruteforcefit.m:23
    dAdBdC = array([0.01, 0.01, 0.01])
    # findbruteforcefit.m:24
    linestouse['ABClist'] = copy(ABClist)
    # findbruteforcefit.m:26
    linestouse['dAdBdC'] = copy(dAdBdC)
    # findbruteforcefit.m:27
    kit['findfitsettings'] = copy(kit['tightnesssettings']['bruteforce'])
    # findbruteforcefit.m:28
    allfits = findfits(linestouse, kit)
    # findbruteforcefit.m:30
    fit = pullbest(allfits, kit)
    # findbruteforcefit.m:32
    fit['patternType'] = copy('bruteforce')
    # findbruteforcefit.m:33
    kit = addfittokit(kit, fit)
    # findbruteforcefit.m:34
    displaykitwithfits(kit)
    1
    return fit
Exemple #8
0
            verbose=100)

y_val_pred = np.argmax(network.predict(X_val.T), axis=0)
cm = confusion_matrix(np.argmax(y_val.T, axis=0),
                      y_val_pred,
                      labels=list(range(10)))

print(cm)

plt.imshow(cm)
plt.xlabel("predicted")
plt.ylabel("true")
plt.xticks(list(range(10)))
plt.yticks(list(range(10)))
plt.ylim([-0.5, 9.5])
plt.save('mnist-cm.png')
plt.show()

# print(network.predict(X_test.T))
plt.plot(network.cost, label='loss')
plt.plot(network.val_cost, label='val_loss')
plt.plot(network.acc, label='acc')
plt.plot(network.val_acc, label='val_acc')
plt.legend()
plt.show()

pred = np.argmax(network.predict(X_test.T), axis=0)
cm = confusion_matrix(np.argmax(y_test.T, axis=0),
                      pred,
                      labels=list(range(10)))
                    a = float(i * m) / float(M)
                    b = float(j * n) / float(N)
                    expo = 1j * 2.0 * 3.1416 * (a + b)
                    tran_act += mtz[j][i] * np.exp(expo)
            mtrans[m, n] = (tran_act.real)
    return (mtrans)


#Sube imagen que ingreso el usuario
byne = sube_imagen(nombre)

#Aplicacion de la transformada a la imagen
transim = transformada(byne)

#Aplicacion de la gaussiana a la matriz de la imagen
ga = gaussiana(byne, sig)

#Aplicacion de la transformada a la gaussiana
transgau = transformada(ga)

#Convolucion entre las transformadas (gaussiana e imagen)
con = transgau * transim

#Aplicacion de la transformada inversa al resultado de lo anterior entra como parametro la matriz de la convoluncion anteriormente realizada

rta = invtra(con)

plt.figure()
plt.imshow(rta, cmap="gray")
plt.save("suave.png")
Exemple #10
0
def pltwxt520(x, y):
    plt.grid()
    plt.plot(x, y)
    plt.save("wxt520.png", 'PNG')  #??? save the file to disk
Exemple #11
0
        tws = get_all_tweets(user["ceo"])
        user_tweets[user["ceo"]] = tws

        tws = get_all_tweets(user["company"])
        user_tweets[user["company"]] = tws

        tweets.append(user_tweets)

    return tweets


tweets_structure = get_tweets(users.users)
for dictionary in tweets_structure:
    for key in dictionary:
        print(f"{key}, {len(dictionary[key])}")
plt.save("women-ceo-tweets.npy", tweets_structure)


'''
json_obj = tweet._json
print(json.dumps(json_obj))
dtime = json_obj['created_at']
print(json_obj["retweeted"])
if json_obj["retweeted"]:
    print("This is retweeted!")

# new_datetime = datetime.strftime(datetime.strptime(dtime,'%a %b %d %H:%M:%S +0000 %Y'), '%Y-%m-%d %H:%M:%S')
new_datetime = datetime.strptime(dtime, '%a %b %d %H:%M:%S +0000 %Y')
print(new_datetime, new_datetime.year, new_datetime.month, new_datetime.day)
user_tweets[user["ceo"]].append(json_obj)
'''
Exemple #12
0
# -*- coding: utf-8 -*-

from scipy.signal.ltisys import lti, lsim
from matplotlib.pylab import save, randn
from Numeric import sqrt, array, arange

n = 128
Q = 1.
R = 1.
w = 0.3 * sqrt(Q) * randn(n)
v = 0.2 * sqrt(R) * randn(n)
ureq = array([[-1.743] * n])

t = arange(0, 0.9999, 1. / 128)
#Generator().generateSin(n, 3, 33) #-0.37727

u = ureq + w

#A, B, C, D = [[-6.,-25.], [1.,0.]], [[1.],[0.]], [[0., 1.]], [[0.]]
#sys=lti(A, B, C, D)
#y = lsim(sys, u, t)
yv = u + v

##save('Q.txt', Q)
##save('R.txt', R)
save('w.txt', w)
save('v.txt', v)
save('yv.txt', yv)
save('u.txt', u)
save('ureq.txt', ureq)
Exemple #13
0
# if RND is None:
RND = np.random.randint(10000)
classid = np.random.randint(0, 1000, samp_n)
refvec = np.vstack((0.7 * np.random.randn(128, samp_n), EmbedMat[:,
                                                                 classid])).T
codes_all = [refvec]
for cutoff in cutoff_list:
    refvec_proj = refvec @ evc_BG[:, -cutoff:] @ evc_BG[:, -cutoff:].T
    codes_all.append(refvec_proj)
codes_all = np.concatenate(tuple(codes_all), axis=0)
img_all = BG.visualize_batch_np(codes_all)
mtg = make_grid(img_all, nrow=samp_n)
ToPILImage()(mtg).show()
ctf_str = "_".join([str(ct) for ct in cutoff_list])
plt.save(
    join(figdir, "BigGAN_proj_%s_%04d.png" % (ctf_str, RND)),
    mtg.numpy(),
)

#%% StyleGAN2
Hessdir = join(rootdir, 'StyleGAN2')
modellist = [
    "ffhq-512-avg-tpurun1", "stylegan2-cat-config-f",
    "2020-01-11-skylion-stylegan2-animeportraits"
]
modelsnms = ["Face512", "Cat256", "Anime"]
#%% for modelnm, modelsnm in zip(modellist, modelsnms):
modelnm, modelsnm = modellist[1], modelsnms[1]
SGAN = loadStyleGAN2(modelnm + ".pt", size=256)
SG = StyleGAN2_wrapper(SGAN)
figdir = join(compressdir, "StyleGAN2", modelsnm)
os.makedirs(figdir, exist_ok=True)
    for user in users:
        if user is plt.np.nan:
            print("THIS IS THE NAN WE WANT TO AVOID")
            continue

        tws = get_all_tweets(user)
        tweets[user] = tws

    return tweets


tweets_structure = get_tweets(users.users)
for key in tweets_structure:
    print(f"{key}, {len(tweets_structure[key])}")
# plt.save("company-tweets.npy", tweets_structure)
plt.save("competitor-tweets.npy", tweets_structure)

'''
json_obj = tweet._json
print(json.dumps(json_obj))
dtime = json_obj['created_at']
print(json_obj["retweeted"])
if json_obj["retweeted"]:
    print("This is retweeted!")

# new_datetime = datetime.strftime(datetime.strptime(dtime,'%a %b %d %H:%M:%S +0000 %Y'), '%Y-%m-%d %H:%M:%S')
new_datetime = datetime.strptime(dtime, '%a %b %d %H:%M:%S +0000 %Y')
print(new_datetime, new_datetime.year, new_datetime.month, new_datetime.day)
user_tweets[user["ceo"]].append(json_obj)
'''
chi0 = np.arange(chi0min, (chi0max)+(dchi0), dchi0)
#% make full fixed chi array (chi0)
#% make all-phi qchi arrays
Qchi0_allphi = np.zeros(lchi0int, lq0int)
#% empty array for combined phi image
Qchi0log_allphi = np.zeros(lchi0int, lq0int)
#% empty array for combined log phi image
#%% Stitch together all the files!
#% for phi
for p in np.arange(1., (numphi)+1):
    #% make qchi arrays
    
#% save q and chi data files
chi0name = np.array(np.hstack((dumpname, '\\', imgname, 'chi0.mat')))
q0name = np.array(np.hstack((dumpname, '\\', imgname, 'Q0.mat')))
plt.save(q0name, 'Q0')
plt.save(chi0name, 'chi0')
#%% Normalize and save qchi with all phis
Qchi0_allphi = matdiv(Qchi0_allphi, numphi)
#% normalize the combined-phi image
for n in np.arange(1., (lchi0int)+1):
    #% make a log plot
    
#% Plot qchi
H = imagesc(Q0, chi0, Qchi0log_allphi)
plt.xlabel('Q (A^-1)')
plt.ylabel('chi (deg)')
plt.colorbar
colormap(plt.jet(256.))
#%xlim([2 8]) % these values specific to your data; CHECK!!!
#%ylim([-80 30]) % these values specific to your data; CHECK!!!