MetadataAnalyzer.py 5.93 KB
Newer Older
1 2 3 4 5 6 7
import numpy as np
from WorkingPyDemo import *
from Remove_Noise import *
import pandas as pd
import statsmodels.api as sm
from matplotlib import pyplot as plt
from sklearn.metrics import mean_squared_error
Bryce Hepner's avatar
Bryce Hepner committed
8
'''Not externally usable code because it was just used for internal testing of a few minor things'''
9

10 11 12 13 14 15
# scenes = file_extractor("images")
# images = image_extractor(scenes)
# print(images)

scenes = remote_file_extractor("/media/elphel/NVME/lwir16-proc/te0607/scenes/")
images = remote_image_extractor(scenes)
16
# images = find_only_in_channel(images, "5")
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
sftp_client = setup_remote_sftpclient()
image_array = np.array(Image.open(sftp_client.open(images[250])))
# image_array = np.array(Image.open(images[250])).astype(np.uint16)
# print(image_array[0])
# print(images[0:5])
# print(image_array[0,0].dtype)
thebitstring = bytes_to_bitstring(image_array[0])
frame = thebitstring[84*8:88*8]
# print(int(frame,2))
otherframe = thebitstring[88*8:92*8]
# print(int(otherframe,2))
# print(image_array[0].astype(np.uint16))
# print(image_array[0,42])
# print(bin(image_array[0,0])[2:].zfill(16))
# for i in range(len(images)):
#     image_array = np.array(Image.open(images[i]))
#     print(image_array[0,91] - image_array[0,90])
# print(np.array(Image.open(images[100]))[0])
def uint16_array_to_bitstring(array):
    bitstring = ""
    for i in range(len(array)):
        bitstring += bin(array[i])[2:].zfill(16)
    return bitstring
def repopulate_array_with_bitstring(bitstring):
    array = []
    for i in range(0,len(bitstring),8):
        array.append(int(bitstring[i:i+8],2))
    return np.array(array)
def intarray_to_uint32(int_array):
    bitstring = ""
    # print(int_array)
    for i in range(len(int_array)):
        bitstring += bin(int_array[i])[2:].zfill(8)
    # print(bitstring)
    return int(bitstring,2)
# print(repopulate_array_with_bitstring(uint16_array_to_bitstring(image_array[0]))[0:6])
frame_spacings = []
signaltonoise = []
55
information_array = np.zeros((len(images),5))
56 57
def celcius_to_kelvin(celcius):
    return celcius + 273.15
58 59
last_image = np.array(Image.open(sftp_client.open(images[0]))).astype(np.uint16)
for i in range(1,3000):
60 61 62 63 64
    image_array = np.array(Image.open(sftp_client.open(images[i]))).astype(np.uint16)
    current_frame_count = intarray_to_uint32(repopulate_array_with_bitstring(uint16_array_to_bitstring(image_array[0]))[84:88])
    frame_count_at_FFC = intarray_to_uint32(repopulate_array_with_bitstring(uint16_array_to_bitstring(image_array[0]))[88:92])
    # signaltonoise.append(1/np.std(image_array[1:]))
    information_array[i,0] = current_frame_count - frame_count_at_FFC
65 66
    information_array[i,-1] = 4096/np.std(image_array[1:])
    information_array[i,-2] = mean_squared_error(gaussian_filter(image_array[1:],sigma=.4),image_array[1:])
67
    print(intarray_to_uint32(repopulate_array_with_bitstring(uint16_array_to_bitstring(image_array[0]))[110:111]))
68 69
    # print(repopulate_array_with_bitstring(uint16_array_to_bitstring(image_array[0]))[76:77])
    # print("start")
70 71 72 73 74 75 76 77 78 79
    # information_array[i,1] = intarray_to_uint32(repopulate_array_with_bitstring(uint16_array_to_bitstring(image_array[0]))[94:96]) - \
    # celcius_to_kelvin(intarray_to_uint32(repopulate_array_with_bitstring(uint16_array_to_bitstring(image_array[0]))[162:166]))
    information_array[i,1] = mean_squared_error(gaussian_filter(last_image[1:],sigma=.4),last_image[1:])
    # information_array[i,2] = 20*np.log10(np.std(image_array[1:]))
    if images[i][-7] == "1":
        information_array[i,2] = images[i][-7:-5]
    else:
        information_array[i,2] = images[i][-6:-5]
    # print(information_array[i,2])
    # information_array[i,3] = np.std(image_array[1:])
80 81 82 83 84 85 86
    # information_array[i,2] = intarray_to_uint32(repopulate_array_with_bitstring(uint16_array_to_bitstring(image_array[0]))[96:98])
    # information_array[i,2] = celcius_to_kelvin(intarray_to_uint32(repopulate_array_with_bitstring(uint16_array_to_bitstring(image_array[0]))[162:166]))
    # information_array[i,4] = information_array[i,1] - information_array[i,2]
    # information_array[i,5] = information_array[i,1] - information_array[i,3]
    # information_array[i,4] = intarray_to_uint32(repopulate_array_with_bitstring(uint16_array_to_bitstring(image_array[0]))[170:174])
    # information_array[i,5] = intarray_to_uint32(repopulate_array_with_bitstring(uint16_array_to_bitstring(image_array[0]))[174:178])
    # frame_spacings.append(current_frame_count - frame_count_at_FFC)
87
    last_image = np.array(Image.open(sftp_client.open(images[i]))).astype(np.uint16)
88 89 90 91 92 93 94
# print(np.argmin(signaltonoise))
# print(np.argmax(signaltonoise))
# print(np.min(abs(np.array(frame_spacings))))
# print("middle")


mask = information_array[:,0] > 0
95 96

information_df = pd.DataFrame(information_array[mask],columns=["Frame_Spacing","Last_MSE","ImageName","Signal_to_Noise","MSE"])
97
# information_df.to_csv("smaller_information_weird_array.csv")
98
information_df = pd.read_csv("smaller_information_weird_array.csv",index_col=0)
99
information_array = information_df.values
100
print(information_array[0,2])
101
plt.scatter(information_array[:,0],information_array[:,-1],s=1)
102 103 104 105 106 107 108 109 110 111 112 113
plt.legend()
plt.xlabel("Frame_Spacing")
plt.ylabel("Signal_to_Noise")
plt.show()
colors = ["red","blue","green","orange","purple","brown","pink","black","grey","cyan","magenta","yellow","white","lime","teal","olive","maroon","navy","silver","gold","indigo","violet"]
secondmask = information_array[:,1] < 30000
print(np.unique(information_array[:,2]))
for g in np.unique(information_array[:,2]):
    i = np.where(information_array[:,2] == g)
    print(g)
    plt.scatter(information_array[:,1][secondmask][i],information_array[:,4][secondmask][i],s=1,label=g,color = colors[int(g)])

114 115 116 117
plt.legend()
plt.xlabel("Frame_Spacing")
plt.ylabel("Signal_to_Noise")
plt.show()
Bryce Hepner's avatar
Bryce Hepner committed
118 119
# print(np.max(information_array[:,0]))
# print(information_array[-5:-1,0])
120 121 122 123 124 125 126 127 128

X = sm.add_constant(information_df.drop(["Signal_to_Noise"],axis=1))

y = information_df["Signal_to_Noise"]


end_result = sm.OLS(y,X).fit()
print(end_result.summary())
sftp_client.close()