def img_to_frond_phase(folder,
                       label_folder,
                       dark=0,
                       avg=3,
                       dT=60,
                       p_range=12,
                       f_range=5,
                       save=""):
    # 上の全部まとめたった!!
    data = im.read_imgs(folder)
    data = data.astype(float) - dark
    label_img = im.read_imgs(label_folder)
    # 解析をする.
    imgs_phase = make_frond_phase_imgs(
        imgs=data,
        label_imgs=label_img,
        avg=avg,
        dT=dT,
        p_range=p_range,
        f_range=f_range,
        save=save,
    )
    print(imgs_phase[0, 254, 254])
    imgs_phase[(label_img != 0) * (np.isnan(imgs_phase))] = -2
    print(imgs_phase[0, 254, 254])
    color_phase = make_colors(imgs_phase, grey=-2, black=-1)
    print(color_phase.shape)
    if save is not False:
        # color 画像の保存
        im.save_imgs(save + "frond_phase_color", color_phase, extension="png")
    return color_phase, imgs_phase
Example #2
0
def frond_transform(
    parent_directory,
    calc_folder="mask_frond.tif",
    other_folder_list=["mask_frond_lum.tif", "frond_lum.tif"],
    motionType=1,
    align=True,
):
    """移動補正を一括化

    Args:
        parent_directory: [description]
        calc_folder: [description] (default: {"mask_frond"})
        other_folder_list: [description] (default: {["mask_frond_lum", 'frond']})
        motionType: [description] (default: {1})
    """
    calc_imgs = im.read_imgs(os.path.join(parent_directory, calc_folder))
    move_img, warps, _roops, trun_pi = imgs_transformECC(calc_imgs,
                                                         motionType=motionType,
                                                         align=align)
    im.save_imgs(parent_directory, move_img, file_name="moved_" + calc_folder)
    for i in other_folder_list:
        other_imgs = im.read_imgs(os.path.join(parent_directory, i))
        other_imgs = imgs_transformECC_warp(other_imgs, warps, trun_pi)
        im.save_imgs(parent_directory, other_imgs, file_name="moved_" + i)
    np.save(os.path.join(parent_directory, "warps.npy"), warps)
    with open(os.path.join(parent_directory, "trun_pi"), mode="w") as f:
        f.write(str(trun_pi))
Example #3
0
def img_circadian_analysis(
    folder,
    avg=3,
    f_avg=5,
    mesh=1,
    calc_range=[24, 24 * 3],
    dt=60,
    offset=0,
    save=True,
    tau_range=[16, 30],
    background=0,
    start_idx=0,
    fft_nlls=True,
):
    mask_frond = im.read_imgs(os.path.join(folder, "moved_mask_frond.tif"))
    mask_frond = img_for_bio(mask_frond)
    mask_frond[:start_idx, :, :] = 0
    im.save_imgs(folder, mask_frond, "moved_mask_frond_usable.tif")
    lum_folder = os.path.join(folder, "moved_mask_frond_lum.tif")
    mask_folder = os.path.join(folder, "moved_mask_frond_usable.tif")
    args = {
        "folder": lum_folder,
        "mask_folder": mask_folder,
        "mesh": mesh,
        "dt": dt,
        "save": save,
    }
    img_pixel_theta(p_range=7,
                    f_avg=f_avg,
                    f_range=5,
                    make_color=[22, 28],
                    pdf=True,
                    xlsx=True,
                    r2_cut=0,
                    min_tau=16,
                    max_tau=30,
                    offset=offset,
                    background=background,
                    **args)
    if fft_nlls is True:
        if not os.path.exists(
                os.path.join(
                    folder,
                    "FFT_nlls",
                    str(calc_range[0]) + "h-" + str(calc_range[1]) +
                    "h_mesh-" + str(mesh) + "_avg-" + str(avg),
                )):
            img_fft_nlls(calc_range=calc_range,
                         avg=avg,
                         tau_range=tau_range,
                         **args)
Example #4
0
        plt.savefig(os.path.join(pdf_save, str(i) + ".pdf"))
        plt.close()
    return 0


if __name__ == "__main__":
    # グラフ化
    os.chdir("/hdd1/kenya/Labo/keisan/python/00data")
    # os.chdir('/home/kenya/Dropbox/Labo/python/00data')
    days = ["./170613-LD2LL-ito-MVX", "./170829-LL2LL-ito-MVX", "./170215-LL2LL-MVX"]
    for day in days:
        frond_folder = day + "/frond"
        data_file = "/small_phase_mesh1_avg3.npy"
        for i in sorted(glob.glob(frond_folder + "/*")):
            print(i)
            frond = im.read_imgs(os.path.join(i, "small_moved_mask_frond_lum", ""))
            area = np.empty(frond.shape[0])
            lum_sum = np.empty(frond.shape[0])
            for j in range(frond.shape[0]):
                area[j] = np.count_nonzero(frond[j])
                lum_sum[j] = np.sum(frond[j])
            # 解析データのフォルダ
            data = np.load(i + data_file)
            save_folder = (
                day
                + "/result/small_R_avg3/"
                + (data_file.lstrip("/")).rstrip(".npy")
                + str("_")
                + i.split("/")[-1]
            )
            if os.path.exists(day + "/result/small_R_avg3") is False:
Example #5
0
    label = np.array(change_frame.values).astype(np.int8)
    colum = np.array(change_frame.columns).astype(np.int8)
    print(label)
    for i, x in enumerate(colum):  # 置き換える値がx.それぞれの置き換える値に対して処理
        for j, y in enumerate(label[:, i]):  # 置き換えられる値がy それぞれの画像に対して処理
            if y != 0:  # 頭悪いのでおまじない.
                new_label[j, imgs[j] == y] = x
    return new_label


if __name__ == "__main__":
    # os.chdir(os.path.join('/Users', 'kenya', 'keisan', 'python', '00data'))
    os.chdir(
        os.path.join("/hdd1", "Users", "kenya", "Labo", "keisan", "python",
                     "00data"))
    # days = ('170613-LD2LL-ito-MVX')
    days = "./170829-LL2LL-ito-MVX"
    # days = ("170215-LL2LL-MVX")
    today = datetime.datetime.today().strftime("%Y-%m-%d")
    # days = ("./170215-LL2LL-MVX")
    data_folder = os.path.join(days, "edit_raw")
    img_folder = os.path.join(data_folder, "label_imgs_tmp.tif")  # 元ラベル画像
    change_file = os.path.join(data_folder, "label_190922.csv")  # 変更に用いるファイル.

    # ここから処理
    imgs = im.read_imgs(img_folder, color=False)
    change = pd.read_csv(change_file, index_col=0, header=0)
    label_img = label_change(imgs, change)
    im.save_imgs(os.path.join(data_folder), label_img,
                 "label_img" + today + ".tif")
            img_after[img == k] = j + label_number[-1] + 1
            imgs_centroids[i, j + label_number[-1]] = centroids[k - 1]
            imgs_stats_sort[i, j + label_number[-1]] = stats[k - 1]
        # 画像の格納庫に格納
        imgs_sort[i] = img_after
        label_out[i, :] = label_out[i, :] * np.in1d(label_out[i, :], img_after)
    # label_out[label_out == 0] = np.nan
    return imgs_sort, imgs_centroids, imgs_stats_sort, label_out


if __name__ == "__main__":
    os.chdir(
        os.path.join("/hdd1", "Users", "kenya", "Labo", "keisan", "python",
                     "00data"))
    # day = "./170613-LD2LL-ito-MVX"
    day = "./170829-LL2LL-ito-MVX"
    # day = "./170215-LL2LL-MVX"
    # ラベリングが終わった画像を取り込む.エッジと背景が0・それ以外は0で無い値なら良い
    # 白黒でもカラーでもOK
    frond_folder = os.path.join(day, "edit_raw", "div_ito_edit.tif")
    # frond_folder = day + '/edit_raw/label_img_ito'
    imgs = im.read_imgs(frond_folder)
    imgs_sort, imgs_centroids, imgs_stats_sort, label_out = frond_traker(
        imgs, min_size=5)
    im.save_imgs(os.path.join(day, "edit_raw"), imgs_sort,
                 "label_imgs_tmp.tif")
    label_out_save = pd.DataFrame(label_out,
                                  columns=np.arange(1, label_out.shape[1] + 1))
    label_out_save = label_out_save.replace(0, np.nan)
    label_out_save.to_csv(os.path.join(day, "edit_raw", "label_tmp.csv"))
Example #7
0
def img_fft_nlls(
    folder,
    mask_folder=False,
    avg=3,
    mesh=1,
    dt=60,
    save=True,
    calc_range=[24, 24 * 3],
    tau_range=[16, 30],
):
    """ピクセルごとに二次関数フィッティングにより解析する.

    位相・周期・Peak時刻,推定の精度 を出力する.

    Args:
        folder: 画像群が入ったフォルダを指定する.
        mask_folder: 背景が0になっている画像群を指定.
        mesh: 解析前にメッシュ化する範囲 (default: {1} しない)
        dt: Minite (default: {60})
        save: 保存先のフォルダ名.Trueなら自動で名付け. (default: {False})

    Returns:
        保存のみの関数.返り値は持たさない(0).
    """
    if mesh == 1:
        imgs = im.read_imgs(folder)
        if mask_folder is not False:
            mask = im.read_imgs(mask_folder)
            imgs[mask == 0] = 0
    else:
        imgs = im.mesh_imgs(folder, mesh)
        if mask_folder is not False:
            mask = im.mesh_imgs(mask_folder, mesh)
            imgs = imgs * (mask != 0).astype(np.float64)
    ##################################
    # 生物発光の画像群から使うデータをcsvからと同じにする
    ##################################
    # データの存在する場所のインデックスをとってくる)
    use_xy = np.where(
        np.all(
            imgs[calc_range[0] * 60 // dt:calc_range[1] * 60 // dt + 1] != 0.0,
            axis=0))  # データの存在する場所のインデックスをとってくる.
    data = imgs[:, use_xy[0], use_xy[1]]
    if data.shape[1] == 0:
        print("解析できるデータがありません")
        return False
    print(str(data.shape[1]) + "個のデータを解析します")
    ########################################################
    # 解析
    ########################################################
    data_det, data_det_ampnorm = FFT_nlls.data_norm(data, dt=dt)
    cosfit_args = {
        "s": calc_range[0],
        "e": calc_range[1],
        "dt": dt,
        "tau_range": tau_range,
    }
    fft_nlls_det = FFT_nlls.cos_fit(data_det, **cosfit_args)
    fft_nlls_ampnorm = FFT_nlls.cos_fit(data_det_ampnorm, **cosfit_args)
    ###################################
    # 周期を画像に戻す.
    ###################################
    tau_dat_img = np.full_like(imgs[0], np.nan, dtype=np.float64)
    tau_ampnorm_img = np.full_like(imgs[0], np.nan, dtype=np.float64)
    rae_dat_img = np.full_like(imgs[0], np.nan, dtype=np.float64)
    rae_ampnorm_img = np.full_like(imgs[0], np.nan, dtype=np.float64)
    # 代入
    tau_dat_img[use_xy] = fft_nlls_det["tau"]
    rae_dat_img[use_xy] = fft_nlls_det["rae"]
    tau_ampnorm_img[use_xy] = fft_nlls_ampnorm["tau"]
    rae_ampnorm_img[use_xy] = fft_nlls_ampnorm["rae"]
    # カラー画像に
    tau_dat_img_colour = ((tau_dat_img - tau_range[0]) /
                          (tau_range[1] - tau_range[0]) * 0.7)
    tau_ampnorm_img_colour = ((tau_ampnorm_img - tau_range[0]) /
                              (tau_range[1] - tau_range[0]) * 0.7)

    color_legend = np.arange(30) * 0.7 / 30
    color_legend = im.make_color(
        np.vstack([color_legend, color_legend, color_legend]))
    tau_dat_img_colour = im.make_color(tau_dat_img_colour, grey=-1)
    tau_ampnorm_img_colour = im.make_color(tau_ampnorm_img_colour, grey=-1)
    ####################################
    # 保存
    ####################
    if save is True:
        save = os.path.join(
            os.path.dirname(folder),
            "FFT_nlls",
            str(calc_range[0]) + "h-" + str(calc_range[1]) + "h_mesh-" +
            str(mesh) + "_avg-" + str(avg),
        )
    elif save is not False:
        save = (save + "fft_nlls-" + str(calc_range[0]) + "-" +
                str(calc_range[1]) + "_mesh-" + str(mesh) + "_avg-" + str(avg))
    if save is not False:
        if not os.path.exists(save):
            os.makedirs(save)
        # np.save(save + 'npy', tau_dat_img)
        np.save(os.path.join(save, "fft_tau.npy"), tau_dat_img)
        np.save(os.path.join(save, "fft_rae.npy"), rae_dat_img)
        np.save(os.path.join(save, "fft_ampnorm_tau.npy"), tau_ampnorm_img)
        np.save(os.path.join(save, "fft_ampnorm_rae.npy"), rae_ampnorm_img)
        Image.fromarray(tau_dat_img_colour).save(save + ".tif")
        Image.fromarray(tau_ampnorm_img_colour).save(save + "_ampnorm.tif")
    return tau_dat_img, tau_ampnorm_img, tau_dat_img_colour, tau_ampnorm_img_colour
Example #8
0
def img_pixel_theta(
    folder,
    mask_folder=False,
    avg=3,
    mesh=1,
    dt=60,
    offset=0,
    p_range=7,
    f_avg=1,
    f_range=5,
    background=0,
    save=False,
    make_color=[22, 28],
    pdf=False,
    xlsx=False,
    distance_center=True,
    r2_cut=0.5,
    min_tau=16,
    max_tau=32,
):
    """ピクセルごとに二次関数フィッティングにより解析する.

    位相・周期・Peak時刻,推定の精度 を出力する.

    Args:
        folder: 画像群が入ったフォルダを指定する.
        mask_folder: 背景が0になっている画像群を指定.
        mesh: 解析前にメッシュ化する範囲 (default: {1} しない)
        dt: Minite (default: {60})
        offset: hour (default: {0})
        p_range: 前後それぞれp_rangeよりも値が高い点をピークとみなす (default: {12})
        f_avg: fiting前の移動平均 (default: {1})
        f_range: 前後それぞれf_rangeのデータを用いて推定をする (default: {5})
        save: 保存先のフォルダ名.Trueなら自動で名付け. (default: {False})
        make_color: 周期を色付けする範囲 (default: {[22, 28]})
        pdf: PDFを保存するか否か (default: {False})
        xlsx: エクセルを保存するか否か (default: {False})
        distance_center: 作図の際どこからの距離を取るか (default: {Ture} center)

    Returns:
        保存のみの関数.返り値は持たさない(0).
    """
    if save is True:
        save = os.path.split(folder)[0]
        save = os.path.join(
            save,
            "_".join([
                "tau_mesh-" + str(mesh),
                "avg-" + str(avg),
                "prange-" + str(p_range),
                "frange-" + str(f_range),
            ]),
        )
    ##################################
    # 画像の読み込
    ##################################
    if mesh == 1:
        imgs = im.read_imgs(folder)
        if mask_folder is not False:
            mask = im.read_imgs(mask_folder)
            imgs[mask == 0] = 0
        else:
            mask = False
    else:
        imgs = im.mesh_imgs(folder, mesh)
        if mask_folder is not False:
            mask = im.mesh_imgs(mask_folder, mesh)
            imgs[mask == 0] = 0
        else:
            mask = False
    imgs = imgs.astype(np.float64)
    imgs[imgs != 0] = imgs[imgs != 0] - background
    ##################################
    # 生物発光の画像群から位相を求める
    ##################################
    peak_a = make_theta_imgs(
        imgs,
        mask_img=mask,
        avg=avg,
        dt=dt,
        p_range=p_range,
        f_avg=f_avg,
        f_range=f_range,
        offset=offset,
        r2_cut=r2_cut,
        min_tau=min_tau,
        max_tau=max_tau,
    )
    # データは見にくいので,カラーのデータを返す.
    cv, sd, rms, avg_lum = peak_a[7], peak_a[8], peak_a[9], peak_a[10]
    color_theta = im.make_colors(peak_a[0], grey=-1, black=np.nan)
    ###################################
    # 保存用に周期を整形
    ###################################
    tau_frond = peak_a[1] == -1
    imgs_tau = (peak_a[1] - make_color[0]) / (make_color[1] -
                                              make_color[0]) * 0.7
    nan = ~np.isnan(imgs_tau)
    imgs_tau[nan][imgs_tau[nan] > 0.7] = 0.7
    imgs_tau[nan][imgs_tau[nan] < 0] = 0
    imgs_tau[tau_frond] = -1
    color_legend = np.arange(30) * 0.7 / 30
    color_legend = im.make_color(
        np.vstack([color_legend, color_legend, color_legend]))
    color_tau = im.make_colors(imgs_tau, grey=-1)

    ####################################
    # 保存用にampを整形
    ####################
    def make_amp_color(data):
        color = data / np.nanmax(data) * 0.7
        color[data <= 0] = -1
        color = im.make_colors(color, grey=-1)
        return color

    color_cv = make_amp_color(cv)
    color_sd = make_amp_color(sd)
    color_rms = make_amp_color(rms)
    color_avg = make_amp_color(avg_lum)

    ####################################
    # Peakの画像の作成
    ####################################
    fold = int(60 / dt) * 24
    p_img = make_peak_img(peak_a[0],
                          mask_imgs=mask,
                          idx_t=[0,
                                 int(peak_a[0].shape[0] / fold) * fold + 1])
    p_img = peak_img_list(p_img, per_ber=10, m=0, fold=fold)
    if save is not False:
        # color 画像の保存
        def save_fnc(img, name=""):
            """名前つけが厄介なやつの保存"""
            name = (name + str(np.nanmax(img)) + "-" +
                    str(np.nanmin(img[img > 0])) + ".tif")
            im.save_imgs(save, img, name)

        im.save_imgs(save, color_theta, "theta.tif")
        save_fnc(color_tau, "tau_")
        save_fnc(color_cv, "cv_")
        save_fnc(color_sd, "sd_")
        save_fnc(color_rms, "rms_")
        save_fnc(color_avg, "move_avg_")

        Image.fromarray(color_legend).save(os.path.join(
            save, "color_legend.png"),
                                           compress_level=0)
        Image.fromarray(p_img).save(os.path.join(save, "peak_img.png"),
                                    compress_level=0)
        # phaseを0−1で表したものをcsvファイルに
        np.save(os.path.join(save, "theta.npy"), peak_a[0])
        np.save(os.path.join(save, "tau.npy"), imgs_tau)
        np.save(os.path.join(save, "cv.npy"), cv)
        np.save(os.path.join(save, "sd.npy"), sd)
        np.save(os.path.join(save, "rms.npy"), rms)
        np.save(os.path.join(save, "move_avg.npy"), avg_lum)

        if pdf is not False:
            if pdf is True:
                pdf = os.path.join(save, "pdf")
            else:
                pdf = os.path.join(save, pdf)
            img_analysis_pdf(
                save_folder=pdf,
                tau=peak_a[1],
                cv=cv,
                sd=sd,
                distance_center=distance_center,
                dt=dt,
            )
        if xlsx is not False:
            writer = pd.ExcelWriter(os.path.join(save, "peak_list.xlsx"))
            peak_a[2].T.to_excel(writer,
                                 sheet_name="r2",
                                 index=False,
                                 header=True)  # 保存
            peak_a[5].T.to_excel(writer,
                                 sheet_name="peak_time",
                                 index=False,
                                 header=True)
            writer.save()
    return 0