def blend_images(self, data):  #data must have iterable 0th dimension
     #pdb.set_trace()
     blended = np.zeros([self.masky, self.maskx, 4])
     if np.ndim(data) == 2:
         composite = np.zeros([self.masky, self.maskx, 4])
         color = cm.hsv(.1)
         alpha = np.squeeze(data)
         alpha[np.isnan(alpha)] = .001  #zap nans
         alpha[alpha == 0] = .001
         blended = rgba_blend(composite, color, alpha)
     elif np.ndim(data) == 3:
         composite = np.zeros([self.masky, self.maskx, 4, data.shape[0]])
         composite[:] = .001
         color_scale = np.arange(.1, 1, .9 / data.shape[0])
         for ii in range(data.shape[0]
                         ):  # for each cluster build a unique color channel
             toget = color_scale[ii]
             color = cm.hsv(toget)
             alpha = data[ii, :, :]
             alpha[np.isnan(alpha)] = .001  #zap nans
             alpha[alpha == 0] = .001
             composite[:, :, :, ii] = rgba_blend(composite, color, alpha)
         for ii in range(3):  # for each color make weighted average
             color = composite[:, :, ii, :]
             alpha = composite[:, :, 3, :]
             blended[:, :, ii] = np.average(color, axis=2, weights=alpha)
         blended[:, :, 3] = np.nanmax(composite[:, :, 3, :], axis=2)
     return blended
Пример #2
0
def compareTwoAnnualWeather(df1, lab1, df2, lab2, item, title):
    """
    df1: pandas.dataframe
        the dataframe of weather data converted from WTH or WTD     
    df2: pandas.dataframe
        the dataframe of weather data converted from WTH or WTD
    item: str
        the type of measured climate; SRAD, TMAX, TMIN, RAIN is defined 
    
    """
    unit = {
        'SRAD': 'MJ/m2',
        'TMAX': 'celcius',
        'TMIN': 'celcius',
        'RAIN': 'mm'
    }

    fig = plt.figure()
    ax = fig.add_subplot(1, 1, 1)
    ax.plot(np.arange(len(df1.index)),
            df1.loc[:, item],
            color=cm.hsv(1 / 2),
            label=lab1)
    ax.plot(np.arange(len(df2.index)),
            df2.loc[:, item],
            color=cm.hsv(2 / 2),
            label=lab2)

    plt.legend(loc='best')
    #plt.xticks(np.arange(len(wthdf.index)), np.inspace(1, 365, num=5))
    plt.title(title, fontsize=18)
    plt.xlabel('Day of year', fontsize=16)
    plt.ylabel(item + '(' + unit[item] + ')', fontsize=12)

    plt.show()
Пример #3
0
def plot(x, y, labels, K, title):
    fig = plt.figure(figsize=(6, 4))
    subplot1 = fig.add_subplot(211)
    subplot1.plot(x)
    subplot1.set_xlabel("Time Steps", size=7)
    subplot1.set_ylabel("Node 1 that sends\nsignals to Node 2", size=7)
    subplot1.set_xlim(0, len(x))
    plt.xticks(fontsize=7)
    plt.yticks(fontsize=7)
    plt.title(title, size=7)

    subplot2 = fig.add_subplot(212)
    subplot2.plot(y, c="g")
    subplot2.set_xlabel("Time Steps", size=7)
    subplot2.set_xlim(0, len(x))
    subplot2.set_ylabel("Node 2 that recieves\nsignals from Node 1", size=7)
    plt.xticks(fontsize=7)
    plt.yticks(fontsize=7)

    plt.subplots_adjust(hspace=0.5)

    for i, color_i in enumerate(labels):
        subplot1.axvspan(i,
                         i + 1,
                         facecolor=cm.hsv(float(color_i) / K),
                         alpha=0.2,
                         lw=0)
        subplot2.axvspan(i,
                         i + 1,
                         facecolor=cm.hsv(float(color_i) / K),
                         alpha=0.2,
                         lw=0)
    plt.show()
Пример #4
0
def visualizeWeather(wthdf, title):
    """
    reference for xtick -> http://python-remrin.hatenadiary.jp/entry/2017/05/27/114816
    
    wthdf : pandas.DataFrame
        the dataframe which contains the information of weather data
        
    title_bn_nn_an : str
        the title of the graph which bn_nn_an condition you use.
    """
    fig = plt.figure()
    ax = fig.add_subplot(1, 1, 1)

    for i in range(3):
        ax.plot(np.arange(len(wthdf.index)),
                wthdf.iloc[:, i],
                color=cm.hsv(i / 5),
                label=wthdf.columns[i])

    ax.bar(np.arange(len(wthdf.index)),
           wthdf.iloc[:, 3],
           color=cm.hsv(4 / 5),
           label=wthdf.columns[3])

    print('SRAD : ' + repr(mean(wthdf.loc[:, 'SRAD'])) + ', TMAX : ' +
          repr(mean(wthdf.loc[:, 'TMAX'])) + ', TMIN : ' +
          repr(mean(wthdf.loc[:, 'TMIN'])) + ', RAIN : ' +
          repr(sum(wthdf.loc[:, 'RAIN'])))

    plt.legend(loc='best')
    #plt.xticks(np.arange(len(wthdf.index)), np.inspace(1, 365, num=5))
    plt.title(title, fontsize=18)
    plt.xlabel('Day of year', fontsize=16)
    plt.ylabel(
        'temperature(celcius), Solar radiation(MJ/m2), precipitation(mm)',
        fontsize=12)

    plt.show()
Пример #5
0
def draw_spectrum(power, psd, spectrum, shape, name, sym=2, inv=1):
    #colors=[np.array(cm.jet(i)[:3])*255 for i in range(256)]
    t0 = time.time()
    psdmax = psd.max()
    if psdmax <= 0.0:
        im = Image.new('RGB', shape)
        return np.array(im).astype(np.float32), (0, 0, 0, 0, 0)
    data = normalize_spectrum(spectrum, psdmax, inv=inv)
    colors = [np.array(cm.hsv(i)[:3]) * 255 for i in range(256)]
    colordata = data.copy()
    colordata = (1.0 - colordata)**(1 / 3.0)
    psdmax = 10 * np.log10(psdmax)
    if psdmax > -20:
        colordata = np.choose(spectrum < -30, (colordata, colordata / 10))
    else:
        colordata = np.choose(spectrum < psdmax - 10,
                              (colordata, colordata / 10))
    colordata -= colordata.min()
    colordata /= colordata.max()
    t0 = time.time() - t0
    t1 = time.time()
    #colordata*=255
    xs, ys = data_to_circle(data, psd, sym=sym)
    xs, ys = center_circle(xs, ys, shape)
    t1 = time.time() - t1
    t2 = time.time()
    nsemi = xs.shape[0] / sym
    colorads = get_radians(nsemi, 2 * pi * np.log2(nsemi))
    t2 = time.time() - t2
    t3 = time.time()
    #colors=[colors[i] for i in colordata]
    colors = get_colors(colors, colorads, colordata)
    t3 = time.time() - t3
    #print colordata.max(),colordata.min(),name
    t4 = time.time()
    im = Image.new('RGB', shape)
    #draw=ImageDraw.Draw(im)
    im = np.array(im).astype(np.float32)
    for i in range(sym):
        draw_points_interp(xs[i * nsemi:(i + 1) * nsemi],
                           ys[i * nsemi:(i + 1) * nsemi], colors, im)
    t4 = time.time() - t4
    #imsave(name,im.astype(np.uint8))
    #im.save(name)
    return np.array(im).astype(np.float32), (t0, t1, t2, t3, t4
                                             )  #.astype(np.float32)
    def plot_all(self, dat1, dat2, dat3, dat4):
        #pdb.set_trace()
        fig = plt.figure(figsize=(12, 4))
        fig.subplots_adjust(left=.05,
                            right=.95,
                            bottom=.05,
                            top=.9,
                            hspace=.05,
                            wspace=.05)
        color_scale = np.arange(.1, 1, .9 / len(self.labels_l))

        ax1 = fig.add_subplot(1, 4, 1, xticks=[], yticks=[])
        for ii in range(
                len(dat1)
        ):  # dat1 is one longer tha dat2 because it contains unused centroids
            dat1_vals = np.squeeze(np.asarray(dat1[ii].values()))
            if ii == 0:  # first entry is unused centroids
                ccc.plot_class_centroids(self, dat1_vals, cm.gray_r(.3), ax1)
            else:
                ccc.plot_class_centroids(self, dat1_vals,
                                         cm.hsv(color_scale[ii - 1]), ax1)
        ax1.set_title('clustered centroids')

        ax2 = fig.add_subplot(1, 4, 2, xticks=[], yticks=[])

        composite = ccc.blend_images(self, dat2)
        ax2.imshow(self.mask, cmap='gray_r', vmax=10, interpolation='nearest')
        ax2.imshow(composite, interpolation='nearest')
        ax2.set_title('clustered seed-pixels')

        ax3 = fig.add_subplot(1, 4, 3, xticks=[], yticks=[])
        ax3.imshow(dat3, interpolation='nearest')
        ax3.set_title('projected correlation maps')

        ax4 = fig.add_subplot(1, 4, 4, xticks=[], yticks=[])
        ax4.imshow(dat4, interpolation='nearest')
        ax4.set_title('correlation masks')
    
arr = np.asarray(arr)
arr = np.concatenate((arr, np.unique(adat["PREF"]).reshape(-1,1)), axis=1)

adat3 = pd.DataFrame(arr, index=np.arange(1, arr.shape[0]+1), columns=adat.columns)

#visualize ADAT
adat2 = adat3.iloc[:, [0,1,3]]

fig = plt.figure(figsize=(10, 6))
ax = fig.add_subplot(1,1,1)
name = ["observed", "Koshihikari", "SD1"]

n = 0
for i in range(len(adat2.columns)):
    ax.bar(np.arange(len(adat2.index))+n, adat2.iloc[:, i], color=cm.hsv(i/3),
           label=name[i], width=0.8/3)
    n = n + 0.8/3
    
plt.legend(loc="best",fontsize=14)
plt.xticks(np.arange(len(adat2.index)+0.55), adat3["PREF"].values, rotation=45, fontsize=14)
plt.title("Comparison of anthesis date among observed, koshi_param and SD1_param", fontsize=18)
plt.xlabel("field name", fontsize=15)
plt.ylabel("Anthesis date", fontsize=15)
plt.ylim([min(adat2.iloc[:, 1]-10), max(adat2.iloc[:, 0]+10)])
ax.set_yticklabels(doylist2Date(ax.get_yticks() -2018000))
plt.setp(ax.get_yticklabels(), fontsize=14, rotation=45, visible=True)
plt.savefig("190313_rice_gencalc_png/190316_ADAT_validation_SD1.png", bbox_inches="tight")

plt.show()
"""

fig = plt.figure(figsize=(12, 6))
ax = fig.add_subplot(1, 1, 1)

order = sum_df.sort_values("HWAH").index.values.astype(np.int32)
#wnum = order[[1,50,80]]
wnum = [83, 97, 94]

for i in range(len(wnum)):
    prep = searchElementforX(wlis[wnum[i] - 1],
                             "^18")["RAIN"].values.astype(np.float32)
    hwah = sum_df.loc[repr(wnum[i]), 'HWAH']
    ax.plot(np.arange(0, len(prep)),
            prep,
            color=cm.hsv(i / len(wnum)),
            label="No" + repr(wnum[i]) + "(Yield=" + repr(hwah) + "kg/ha)")

plt.legend(fontsize=14, loc="best")
ax.set_ylabel('daily rainfall(mm)', fontsize=15)
plt.title("Daily rainfall of the scenario", fontsize=18)
plt.xlabel('Day of year', fontsize=15)
ax.set_xlim(180, 320)
plt.setp(ax.get_xticklabels(), fontsize=13, visible=True)
plt.setp(ax.get_yticklabels(), fontsize=13, visible=True)
plt.setp(ax2.get_yticklabels(), fontsize=13, visible=True)
#ax.set_ylim(0, 30)
#plt.savefig('190108_Reiton_RIX_png/190105_N_flow_in_1993_SSKT9605.png', bbox_inches='tight')
plt.show()
"""
190201 single year's plant N and precipitation.
Пример #9
0
plt.setp(ax.get_xticklabels(), fontsize=15, visible=True)
plt.setp(ax.get_yticklabels(), fontsize=15, visible=True)
#plt.savefig('181224_Dssat_png/190105_NH4_surface_in_DAS0_VS_yield_SSKT9601.png')

plt.show()

#plot rainfall and soil N content
fig = plt.figure(figsize=(8, 6))
ax = fig.add_subplot(1, 1, 1)
ax2 = ax.twinx()

for i in range(1, 5):
    weath = searchElementforX(wlis[i - 1], '^18')
    ax.plot(sn_df2.loc[:, 'DOY'][sn_df2.index == i].values.astype(np.int32),
            sn_df2.loc[:, 'NI1D'][sn_df2.index == i].values.astype(np.float64),
            color=cm.hsv((i - 1) / 4),
            label="soil" + repr(i) + '(y=' + repr(hwah[i - 1]) + ')')
    ax2.plot(np.arange(1,
                       len(weath.index) + 1, 1),
             weath.loc[:, 'RAIN'].values.astype(np.float64),
             color=cm.hsv((i - 1) / 4))

ax.legend(loc='best', ncol=2)
#plt.xticks(np.arange(len(wthdf.index)), np.inspace(1, 365, num=5))
plt.title("Total soil N content(TPDOY=213)", fontsize=18)
ax.set_xlabel('Day of year', fontsize=15)
ax.set_ylabel('Total soil N in profile(kg/ha)', fontsize=15)
ax2.set_ylabel('Daily rainfall(mm)', fontsize=15)
ax.set_ylim(0, 105)
ax.set_xlim(200, 250)
plt.setp(ax.get_xticklabels(), fontsize=13, visible=True)
lis5 = []
for i in range(1986, 2017):
    df = searchElementforX(nitro_df5, repr(i))
    nucm = sum(df["NUCM"])
    lis5.append(nucm)


#visualize the result

fig = plt.figure(figsize = (12, 6))
ax = fig.add_subplot(1,1,1)

lis = [lis1, lis4, lis5]

for i in range(len(lis)):
    ax.plot(np.arange(len(lis[i])), lis[i], color=cm.hsv(i/len(lis)),
            label="pattern"+repr(i+1))

plt.legend(loc='best', fontsize=15)
plt.title("amount of N uptake in each simulation", fontsize=18)
plt.xticks(np.arange(0, len(lis[i]), 5), np.arange(1986, 1986+len(lis[i]), 5))
plt.xlabel('Year', fontsize=16)
plt.ylabel("N uptake (kg/ha)", fontsize=16)
plt.setp(ax.get_xticklabels(), fontsize=15, visible=True)
plt.setp(ax.get_yticklabels(), fontsize=15, visible=True)
plt.savefig('181224_Dssat_png/181227_Amount_of_N_uptake.png')

plt.show()


def genDaysInEachYear(year):
comparing calh vs real vs sim

visualize the 8 point of data by bar plot
"""
#HWAH
hwah2 = hwah.iloc[:, [0, 1, 3]]

fig = plt.figure(figsize=(10, 6))
ax = fig.add_subplot(1, 1, 1)
name = ["Koshihikari", "observed", "SD2"]

n = 0
for i in range(len(hwah2.columns)):
    ax.bar(np.arange(len(hwah2.index)) + n,
           hwah2.iloc[:, i],
           color=cm.hsv(i / 3),
           label=name[i],
           width=0.8 / 3)
    n = n + 0.8 / 3

plt.legend(loc="best", fontsize=14)
plt.xticks(np.arange(len(hwah2.index) + 0.55), [
    "Kasai_q2_low", "Kasai_q2_high", "Kasai_d4_low", "Kasai_d4_high",
    "Makabe_low", "Makabe_high", "Akita_low", "Akita_high"
],
           rotation=30,
           fontsize=14)
plt.title("Comparison of yield among observed, koshi_param and SD2_param",
          fontsize=18)
plt.xlabel("field name", fontsize=15)
plt.ylabel("Yield (kg/ha)", fontsize=15)
Пример #12
0
 
 'NI1D', 'NI2D', 'NI3D', 'NI4D', 'NI5D', 'NI6D', 'NI7D', 'NI8D',
 'NH1D', 'NH2D', 'NH3D', 'NH4D', 'NH5D', 'NH6D', 'NH7D', 'NH8D',
 ]
"""

#compare the NO3 contents in soil surface layer
fig = plt.figure(figsize=(12, 6))
ax = fig.add_subplot(1, 1, 1)

lis = [nitro_df1, nitro_df2, nitro_df3, nitro_df4, nitro_df5]

for i in range(len(lis)):
    ax.plot(np.arange(0, len(lis[i].index)),
            lis[i]["NI1D"],
            color=cm.hsv(i / len(lis)),
            label="SSKT860" + repr(i + 1))

plt.legend(loc="best")
plt.xticks(np.linspace(0, len(lis[i].index), 31), np.arange(1986, 2017, 1))
plt.title("Soil surface NO3 content in each situation", fontsize=18)
plt.xlabel('Year', fontsize=15)
plt.ylabel('Soil surface NO3(ppm)', fontsize=15)
ax.set_xlim(0, 30)
#ax.set_ylim(0, 100)
#plt.savefig('181224_Dssat_png/181224_comparison_of_yield_5pattern.png', bbox_inches='tight')
plt.show()


def genDaysInEachYear(year):
    if year % 4 == 0:
Пример #13
0
    def plot_all(self, dat1, dat2, dat3, dat4):

        font = {
            'family': 'sans-serif',
            'fontname': 'Arial',
            'color': 'black',
            'weight': 'normal',
            'size': 10,
        }

        text_ycords = self.masky * .05
        text_xcords = self.maskx * .04
        fig = plt.figure(figsize=(16, 4))
        fig.subplots_adjust(left=.05,
                            right=.95,
                            bottom=.05,
                            top=.9,
                            hspace=.1,
                            wspace=.05)
        color_scale = np.arange(.1, 1, .9 / len(self.labels))

        ax1 = fig.add_subplot(1, 4, 1, xticks=[], yticks=[])
        ccc.plot_class_centroids(self, dat1, cm.gist_heat_r, ax1, hexbin=True)
        ax1.set_title('all centroids', fontdict=font, fontsize=14)
        ax1.text(text_xcords,
                 text_ycords * 19,
                 'threshold: ' + str(self.corr_thresh),
                 fontdict=font)
        ax1.text(text_xcords,
                 text_ycords * 18,
                 'density: ' + str(self.eps),
                 fontdict=font)
        ax1.text(text_xcords,
                 text_ycords * 17,
                 'min. size: ' + str(self.min_samples),
                 fontdict=font)

        ax2 = fig.add_subplot(1, 4, 2, xticks=[], yticks=[])
        for ii in range(
                len(dat2)
        ):  # dat2 is one longer tha dat1 because it contains unused centroids
            dat2_vals = np.squeeze(np.asarray(dat2[ii].values()))
            if ii == 0:  # first entry is unused centroids
                ccc.plot_class_centroids(self, dat2_vals, cm.gray_r(.3), ax2)
            else:
                ccc.plot_class_centroids(self, dat2_vals,
                                         cm.hsv(color_scale[ii - 1]), ax2)
        ax2.set_title('clustered centroids', fontdict=font, fontsize=14)
        ax2.text(text_xcords,
                 text_ycords,
                 'threshold: ' + str(self.corr_thresh),
                 fontdict=font)
        ax2.text(text_xcords,
                 text_ycords * 2,
                 'density: ' + str(self.eps),
                 fontdict=font)
        ax2.text(text_xcords,
                 text_ycords * 3,
                 'min.size: ' + str(self.min_samples),
                 fontdict=font)

        ax3 = fig.add_subplot(1, 4, 3, xticks=[], yticks=[])
        ax3.imshow(dat3, interpolation='nearest')
        ax3.set_title('projected correlation maps', fontdict=font, fontsize=14)
        ax3.text(text_xcords,
                 text_ycords,
                 'threshold: ' + str(self.corr_thresh),
                 fontdict=font)
        ax3.text(text_xcords,
                 text_ycords * 2,
                 'density: ' + str(self.eps),
                 fontdict=font)
        ax3.text(text_xcords,
                 text_ycords * 3,
                 'min.size: ' + str(self.min_samples),
                 fontdict=font)

        ax4 = fig.add_subplot(1, 4, 4, xticks=[], yticks=[])
        ax4.imshow(dat4, interpolation='nearest')
        ax4.set_title('seed-pixel masks', fontdict=font, fontsize=14)
        ax4.text(text_xcords,
                 text_ycords,
                 'threshold: ' + str(self.corr_thresh),
                 fontdict=font)
        ax4.text(text_xcords,
                 text_ycords * 2,
                 'density: ' + str(self.eps),
                 fontdict=font)
        ax4.text(text_xcords,
                 text_ycords * 3,
                 'min.size: ' + str(self.min_samples),
                 fontdict=font)

        #timestamp_str = time.strftime("%Y%m%d-%H%M")
        if self.save_out:
            savename = self.basepath + '_ccc.png'
            fig.savefig(savename, dpi=300, format='png')
plt.setp(ax.get_yticklabels(), fontsize=15, visible=True)
#plt.savefig('181224_Dssat_png/190105_NH4_surface_in_DAS0_VS_yield_SSKT9601.png')

plt.show()


#plot rainfall and soil N content
fig = plt.figure(figsize = (8, 6))
ax = fig.add_subplot(1,1,1)
ax2 = ax.twinx()

for i in range(1, 5):
    weath = searchElementforX(wlis[i-1], '^18')
    ax.plot(sn_df.loc[:, 'DOY'][sn_df.index == i].values.astype(np.int32), 
            sn_df.loc[:, 'NI1D'][sn_df.index == i].values.astype(np.float64),
            color=cm.hsv((i-1)/4), label="soil"+repr(i)+'(y='+repr(hwah[i-1])+')')
    ax2.plot(np.arange(1, len(weath.index)+1, 1), 
            weath.loc[:, 'RAIN'].values.astype(np.float64),
            color=cm.hsv((i-1)/4))
    
ax.legend(loc='best', ncol=2)
#plt.xticks(np.arange(len(wthdf.index)), np.inspace(1, 365, num=5))
plt.title("Total soil N content(TPDOY=213)", fontsize=18)
ax.set_xlabel('Day of year', fontsize=15)
ax.set_ylabel('Total soil N in profile(kg/ha)', fontsize=15)
ax2.set_ylabel('Daily rainfall(mm)', fontsize=15)
ax.set_ylim(5, 105)
ax.set_xlim(200, 250)
plt.setp(ax.get_xticklabels(), fontsize=13, visible=True)
plt.setp(ax.get_yticklabels(), fontsize=13, visible=True)
plt.setp(ax2.get_yticklabels(), fontsize=13, visible=True)
y_value = pd.concat(
    [sum1["HWAH"], sum2["HWAH"], sum3["HWAH"], sum4["HWAH"], sum5["HWAH"]],
    axis=1).values.astype(np.int32)
y_df = pd.DataFrame(y_value,
                    index=sum1.index,
                    columns=["sum1", "sum2", "sum3", "sum4", "sum5"])

fig = plt.figure(figsize=(12, 6))
ax = fig.add_subplot(1, 1, 1)

for i in range(len(y_df.columns)):
    ax.plot(np.arange(1,
                      len(y_df.index) + 1),
            y_df.iloc[:, i],
            color=cm.hsv(i / len(y_df.columns)))

sum_1, = plt.plot((1, 1), (1, 1),
                  color=cm.hsv(0 / len(y_df.columns)),
                  linewidth=10)
sum_2, = plt.plot((1, 1), (1, 1),
                  color=cm.hsv(1 / len(y_df.columns)),
                  linewidth=10)
sum_3, = plt.plot((1, 1), (1, 1),
                  color=cm.hsv(2 / len(y_df.columns)),
                  linewidth=10)
sum_4, = plt.plot((1, 1), (1, 1),
                  color=cm.hsv(3 / len(y_df.columns)),
                  linewidth=10)
sum_5, = plt.plot((1, 1), (1, 1),
                  color=cm.hsv(4 / len(y_df.columns)),