Aquascat Data Loader is updated with the Loader of Guillaume Froment (with Dexmes data). Save as and Open actions are added and connected to create_table_for_save_as.py and read_table_for_open.py. Data are saved in sqlite database. Four tables are created : File (acoustic_data (integer) and acoustic file), Measure (acoustic_data, frequencies, kt, nb_profiles, nb_cells and other measurement information), Settings (temperature, tmin, tmax, depth_min, depth_max) and BS_data (backscatter data with time, depth (list of 2D arrays) and BS_raw_data (list of 3D arrays)). 3D arrays are saved with binary (np.tobytes()) in one cell of table BS_data (BLOB type). They are then loaded with np.frombuffer() function.

This commit is contained in:
brahim committed 2024-04-22 10:56:10 +02:00
1 parent 75eeac0310
commit e08d769f79
7 files changed
+1986 -219

No files matched your search

File diff suppressed because it is too large. Load diff
+5 -1
View File
@@ -103,7 +103,11 @@ class AcousticDataLoader:
# order="F")
r = np.zeros((self._r.shape[1] * self._time.shape[1], self._freq.shape[0]))
for i, _ in enumerate(self._freq):
r[:, i] = np.repeat(self._r[i, :], self._time.shape[1])
for j in range(self._time.shape[1]):
r[j*self._r.shape[1]:(j+1)*self._r.shape[1], i] = self._r[i, :]
# r[:, i] = np.repeat(self._r[i, :], self._time.shape[1])
print(r.shape)
return r
+328
View File
@@ -0,0 +1,328 @@
import numpy as np
from PyQt5.QtWidgets import QFileDialog, QApplication
import sqlite3
import settings as stg
from os import chdir
import time
class CreateTableForSaveAs():
def __init__(self):
self.create_AcousticFile = """CREATE TABLE File(
ID INTEGER PRIMARY KEY AUTOINCREMENT,
acoustic_data INTEGER,
acoustic_file STRING)"""
self.create_Measure = """ CREATE TABLE Measure(
ID INTEGER PRIMARY KEY AUTOINCREMENT,
acoustic_data INTEGER,
frequency FLOAT,
kt FLOAT,
NbProfiles INTEGER,
NbProfilesPerSeconds FLOAT,
NbCells INTEGER,
CellSize FLOAT,
PulseLength FLOAT,
NbPingsPerSeconds FLOAT,
NbPingsAveragedPerProfile FLOAT,
GainRx FLOAT,
GainTx FLOAT)
"""
# for i in stg.acoustic_data:
# exec("self.create_BSRawData_" + str(i) + " = '''CREATE TABLE BSRawData_" + str(i) +
# "(ID INTEGER PRIMARY KEY AUTOINCREMENT, " +
# "acoustic_data INTEGER, " +
# " ".join(["time_" + str(int(f)) + " FLOAT," for f in stg.freq[i][:-1]]) +
# ["time_" + str(int(stg.freq[i][-1]))][0] + " FLOAT" + ")''' ")
# for i in stg.acoustic_data:
# exec("self.create_BSRawData_" + str(i) + " = '''CREATE TABLE BSRawData_" + str(i) +
# "(ID INTEGER PRIMARY KEY AUTOINCREMENT, " +
# "acoustic_data INTEGER, " +
# " ".join(["time_" + str(int(f)) + " FLOAT," for f in stg.freq[i]]) +
# " ".join(["depth_" + str(int(f)) + " FLOAT," for f in stg.freq[i]]) +
# " ".join(["BS_raw_data_" + str(int(f)) + " FLOAT," for f in stg.freq[i][:-1]]) +
# ["BS_raw_data_" + str(int(stg.freq[i][-1]))][0] + " FLOAT" + ")''' ")
self.create_BSRawData = '''CREATE TABLE BSRawData(
ID INTEGER PRIMARY KEY AUTOINCREMENT,
acoustic_data INTEGER,
time BLOB,
depth BLOB,
BS_raw_data BLOB)'''
self.create_Settings = '''CREATE TABLE Settings(
ID INTEGER PRIMARY KEY AUTOINCREMENT,
acoustic_data INTEGER,
temperature FLOAT,
time_min_ind INTEGER,
time_min_val FLOAT,
time_max_ind INTEGER,
time_max_val FLOAT,
depth_min_ind INTEGER,
depth_min_val FLOAT,
depth_max_ind INTEGER,
depth_max_val FLOAT)'''
self.open_file_dialog()
start = time.time()
self.create_table()
print(f"end : {time.time() - start} sec")
def open_file_dialog(self):
options = QFileDialog.Options()
name = QFileDialog.getSaveFileName(
caption="Save As", directory="", filter="AcouSed Files (*.acd)", options=options)
if name:
stg.dirname_save_as = "/".join(name[0].split("/")[:-1]) + "/"
stg.filename_save_as = name[0].split("/")[-1]
chdir(stg.dirname_save_as)
# file = open(stg.filename_save_as, 'w')
# file.write(self.create_Measure)
# file.close()
def create_table(self):
# Create a new database and open a database connection to allow sqlite3 to work with it.
cnx = sqlite3.connect(stg.filename_save_as + '.acd')
# Create database cursor to execute SQL statements and fetch results from SQL queries.
cur = cnx.cursor()
# --------------------------------------------------------------------------------------------------------------
# --- Table File ---
start_table_File = time.time()
cur.execute("DROP TABLE if exists File")
cur.execute(self.create_AcousticFile)
for i in stg.acoustic_data:
query0 = (f"INSERT into File(acoustic_data, acoustic_file)"
f"VALUES({stg.acoustic_data[i]}, {str(stg.filename_BS_raw_data[i]).split('.')[0]})")
cur.execute(query0)
cnx.commit()
print(f"table File : {time.time() - start_table_File} sec")
# --------------------------------------------------------------------------------------------------------------
# --- Table Measure ---
start_table_Measure = time.time()
# Drop Table if exists
cur.execute("DROP TABLE if exists Measure")
# Execute the CREATE TABLE statement
cur.execute(self.create_Measure)
# Fill the table Measure
for i in stg.acoustic_data:
for f, k, r, t in zip(stg.freq[i], stg.kt[i], stg.gain_rx[i], stg.gain_tx[i]):
query = (f"INSERT into Measure(acoustic_data, frequency, kt, NbProfiles, NbProfilesPerSeconds, "
f"NbCells, CellSize, PulseLength, NbPingsPerSeconds, "
f"NbPingsAveragedPerProfile, GainRx, GainTx) "
f"VALUES({i}, {f}, {k}, {stg.nb_profiles[i]}, {stg.nb_profiles_per_sec[i]}, "
f"{stg.nb_cells[i]}, {stg.cell_size[i]}, {stg.pulse_length[i]}, {stg.nb_pings_per_sec[i]}, "
f"{stg.nb_pings_averaged_per_profile[i]}, {r}, {t})")
cur.execute(query)
# Commit the transaction after executing INSERT.
cnx.commit()
print(f"table Measure : {time.time() - start_table_Measure} sec")
# --------------------------------------------------------------------------------------------------------------
# --- Table BSRawData_i ---
start_table_BSRawData = time.time()
cur.execute('DROP TABLE if exists BSRawData')
# # Drop Table BSRawData_i if exists
# for i in stg.acoustic_data:
# exec("cur.execute('DROP TABLE if exists BSRawData_" + str(i) + "')")
#
# # Execute the CREATE TABLE BSRawData_i statement
# for i in stg.acoustic_data:
# exec("cur.execute(self.create_BSRawData_" + str(i) + ")")
# Fill the table BSRawData_i
# # --- Fill table BSRawData_i with acoustic data integer ---
# for i in stg.acoustic_data:
# for j in range(stg.time_reshape[i].shape[0]):
# exec(f"query1 = ('''INSERT into BSRawData_" + str(i) + "(acoustic_data)" +
# f" VALUES({i}) ''') ")
#
# exec("cur.execute(query1)")
# ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
# for i in stg.acoustic_data:
# , depth, BS_raw_data
cur.execute(self.create_BSRawData)
# homer = np.array([1, 2, 3, 4, 5])
# bart = np.array([6, 7, 8, 9, 10])
# lisa = np.array([11, 12, 13, 14, 15])
# marge = homer.tobytes()
# print("to string : ", homer.tostring())
# print("to bytes : ", homer.tobytes())
# query2 = f''' INSERT into BSRawData(acoustic_data, time) VALUES({0}, {marge}) '''
# cur.execute('''CREATE TABLE IF NOT EXISTS BSRawData(time BLOB)''')
for i in stg.acoustic_data:
# cur.execute(''' INSERT into BSRawData(acoustic_data, time, depth, BS_raw_data) VALUES(?, ?, ?, ?)''',
# (stg.acoustic_data[i], homer.tobytes(),
# bart.tobytes(), lisa.tobytes()))
cur.execute(''' INSERT into BSRawData(acoustic_data, time, depth, BS_raw_data, BS_cross_section)
VALUES(?, ?, ?, ?)''',
(stg.acoustic_data[i], stg.time[i].tobytes(),
stg.depth[i].tobytes(), stg.BS_raw_data[i].tobytes()))
# cur.execute(''' INSERT into BSRawData(time) VALUES(?)''', (marge,))
# query2 = f''' INSERT into BSRawData(acoustic_data, time) VALUES({0}, {stg.time[0][0, :].tobytes()}) ''' # , {stg.depth[i].tobytes()}, {stg.BS_raw_data[i].tobytes()}
# for j in range(stg.time_reshape[i].shape[0]):
#
# # print("query2 = ('''INSERT into BSRawData_" + str(i) + "(acoustic_data, " +
# # ", ".join(["time_" + str(int(f)) for f in stg.freq[i]]) + ", " +
# # ", ".join(["depth_" + str(int(f)) for f in stg.freq[i]]) + ")" +
# # f" VALUES" + str(eval("0, " +
# # ", ".join(["stg.time_reshape[" + str(i) + "][" + str(
# # j) + ", " + str(k) + "]" for k, _ in
# # enumerate(stg.freq[i])]) + ", " +
# # ", ".join(["stg.depth_reshape[" + str(i) + "][" + str(j) + ", " + str(k) + "]" for k, _ in enumerate(stg.freq[i])]))) + " ''' )")
#
# exec("query2 = ('''INSERT into BSRawData_" + str(i) + "(acoustic_data, " +
# ", ".join(["time_" + str(int(f)) for f in stg.freq[i]]) + ", " +
# ", ".join(["depth_" + str(int(f)) for f in stg.freq[i]]) + ", " +
# ", ".join(["BS_raw_data_" + str(int(f)) for f in stg.freq[i]]) + ")"
# f" VALUES" + str(eval(str(i) + ", " +
# ", ".join(["stg.time_reshape[" + str(i) + "][" + str(
# j) + ", " + str(k) + "]" for k, _ in
# enumerate(stg.freq[i])]) + ", " +
# ", ".join(["stg.depth_reshape[" + str(i) + "][" + str(
# j) + ", " + str(k) + "]" for k, _ in
# enumerate(stg.freq[i])]) + ", " +
# ", ".join(["stg.BS_raw_data_reshape[" + str(i) + "][" + str(
# j) + ", " + str(k) + "]" for k, _ in
# enumerate(stg.freq[i])]))) + " ''' )")
# cur.execute(query2)
# Commit the transaction after executing INSERT.
cnx.commit()
print(f"table BSRawData : {time.time() - start_table_BSRawData} sec")
# ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
# # --- Fill table BSRawData_i with data stg.time_reshape ---
# for i in stg.acoustic_data:
# for j in range(stg.time_reshape[i].shape[0]):
#
# # print("query2 = ('''INSERT into BSRawData_" + str(i) + "(acoustic_data, " +
# # ", ".join(["time_" + str(int(f)) for f in stg.freq[i]]) + ")" +
# # f" VALUES" + str(eval("0, " + ", ".join(["stg.time_reshape[" + str(i) + "][" + str(j) + ", " + str(k) + "]" for k, _ in enumerate(stg.freq[i])]))) + " ''' )")
#
# exec("query2 = ('''INSERT into BSRawData_" + str(i) + "(acoustic_data, " +
# ", ".join(["time_" + str(int(f)) for f in stg.freq[i]]) + ")" +
# f" VALUES" + str(eval("0, " + ", ".join(["stg.time_reshape[" + str(i) + "][" + str(j) + ", " + str(k) + "]" for k, _ in enumerate(stg.freq[i])]))) + " ''' )")
#
# exec("cur.execute(query2)")
#
# # Commit the transaction after executing INSERT.
# cnx.commit()
#
# # --- Fill table BSRawData_i with data stg.depth_reshape ---
# for i in stg.acoustic_data:
# for f in stg.freq[i]:
# exec("query3 = ('''ALTER TABLE BSRawData_" + str(i) +
# " ADD " + ["depth_" + str(int(f)) + " FLOAT"][0] + " ''')")
#
# exec("cur.execute(query3)")
#
# # Commit the transaction after executing INSERT.
# cnx.commit()
#
# for i in stg.acoustic_data:
# for k, f in enumerate(stg.freq[i]):
# for j in range(stg.depth_reshape[i].shape[0]):
# exec("query4 = '''UPDATE BSRawData_" + str(i) +
# " SET " + ["depth_" + str(int(f))][0] + " = " +
# str(eval("stg.depth_reshape[" + str(i) + "][" + str(j) + ", " + str(k) + "]")) +
# " WHERE ID = " + str(j + 1) + " ''' ")
#
# exec("cur.execute(query4)")
# # --- Fill table BSRawData_i with data stg.BS_raw_data_reshape ---
# for i in stg.acoustic_data:
# for f in stg.freq[i]:
#
# exec("query5 = ('''ALTER TABLE BSRawData_" + str(i) +
# " ADD " + ["BS_raw_data_" + str(int(f)) + " FLOAT"][0] + " ''')")
#
# exec("cur.execute(query5)")
#
# # Commit the transaction after executing INSERT.
# cnx.commit()
#
# for i in stg.acoustic_data:
# for k, f in enumerate(stg.freq[i]):
# for j in range(stg.BS_raw_data_reshape[i].shape[0]):
#
# exec("query6 = '''UPDATE BSRawData_" + str(i) +
# " SET " + ["BS_raw_data_" + str(int(f))][0] + " = " +
# str(eval("stg.BS_raw_data_reshape[" + str(i) + "][" + str(j) + ", " + str(k) + "]")) +
# " WHERE ID = " + str(j+1) + " ''' ")
#
# exec("cur.execute(query6)")
#
# cnx.commit()
# --------------------------------------------------------------------------------------------------------------
# --- Table Settings ---
start_table_Settings = time.time()
cur.execute("DROP TABLE if exists Settings")
cur.execute(self.create_Settings)
print(stg.acoustic_data, stg.temperature, stg.rmin, stg.rmax, stg.tmin, stg.tmax)
for i in stg.acoustic_data:
query7 = (f'''INSERT into Settings(acoustic_data, temperature,
time_min_ind, time_min_val, time_max_ind, time_max_val,
depth_min_ind, depth_min_val, depth_max_ind, depth_max_val)
VALUES({stg.acoustic_data[i]},
{stg.temperature[i]},
{stg.tmin[i][0]},
{stg.tmin[i][1]},
{stg.tmax[i][0]},
{stg.tmax[i][1]},
{stg.rmin[i][0]},
{stg.rmin[i][1]},
{stg.rmax[i][0]},
{stg.rmax[i][1]})''')
cur.execute(query7)
cnx.commit()
print(f"table Settings : {time.time() - start_table_Settings} sec")
# Close database cursor
cur.close()
# Close database connection
cnx.close()
+157
View File
@@ -0,0 +1,157 @@
import numpy as np
from PyQt5.QtWidgets import QFileDialog, QApplication
import sqlite3
from os import path, chdir
import settings as stg
class ReadTableForOpen:
def __init__(self):
self.open_file_dialog()
chdir(stg.dirname_open)
self.sql_file_to_open = open(stg.filename_open)
self.read_table()
# self.reshape_variables()
def open_file_dialog(self):
name = QFileDialog.getOpenFileName(caption="Open Acoused file", directory="", filter="Acoused file (*.acd)")
if name:
stg.dirname_open = path.dirname(name[0])
stg.filename_open = path.basename(name[0])
def read_table(self):
# connexion to File db
cnx = sqlite3.connect(stg.filename_open)
# Create database cursor to execute SQL statements and fetch results from SQL queries.
cur = cnx.cursor()
query = '''SELECT acoustic_data, acoustic_file FROM File'''
data = cur.execute(query).fetchall()
stg.acoustic_data, stg.filename_BS_raw_data = [x[0] for x in data], [y[1] for y in data]
for i in stg.acoustic_data:
query1 = f'''SELECT frequency, kt, NbProfiles, NbProfilesPerSeconds, "
f"NbCells, CellSize, PulseLength, NbPingsPerSeconds, "
f"NbPingsAveragedPerProfile, GainRx, GainTx
FROM Measure WHERE (acoustic_data = {i})'''
data1 = cur.execute(query1).fetchall()
# print(data1)
# print([x[0] for x in data1])
stg.freq.append([x[0] for x in data1])
stg.kt.append([x[1] for x in data1])
stg.nb_profiles.append([x[2] for x in data1])
stg.nb_profiles_per_sec.append([x[3] for x in data1])
stg.nb_cells.append([x[4] for x in data1])
stg.cell_size.append([x[5] for x in data1])
stg.pulse_length.append([x[6] for x in data1])
stg.nb_pings_per_sec.append([x[7] for x in data1])
stg.nb_pings_averaged_per_profile.append([x[8] for x in data1])
stg.gain_rx.append([x[9] for x in data1])
stg.gain_tx.append([x[10] for x in data1])
# for f_ind, f_val in enumerate(stg.freq[i]):
#
# exec("query2 = '''SELECT time_" + str(int(f_val)) + ", depth_" + str(int(f_val)) + ", BS_raw_data_" + str(int(f_val)) +
# " FROM BSRawData_" + str(int(i)) + " WHERE (acoustic_data = " + str(i) + ") ''' ")
# exec("data2 = cur.execute(query2).fetchall()")
# # print(eval("data2"))
#
# if f_ind == 0:
# time_reshape_temp = np.array([[x[0] for x in eval("data2")]])
# # print("1 time_reshape_temp.shape ", time_reshape_temp.shape)
# depth_reshape_temp = np.array([[x[1] for x in eval("data2")]])
# BS_raw_data_reshape_temp = np.array([[x[2] for x in eval("data2")]])
# else:
# time_reshape_temp = np.insert(time_reshape_temp, f_ind, [x[0] for x in eval("data2")], axis=0)
# # print("2 time_reshape_temp.shape ", time_reshape_temp.shape)
# depth_reshape_temp = np.insert(depth_reshape_temp, f_ind, [x[1] for x in eval("data2")], axis=0)
# BS_raw_data_reshape_temp = np.insert(BS_raw_data_reshape_temp, f_ind, [x[1] for x in eval("data2")], axis=0)
#
# stg.time_reshape.append(time_reshape_temp.transpose())
# # print(len(stg.time_reshape))
# # print(stg.time_reshape[i].shape)
# stg.depth_reshape.append(depth_reshape_temp.transpose())
# stg.BS_raw_data_reshape.append(BS_raw_data_reshape_temp.transpose())
query2 = f'''SELECT acoustic_data, time, depth, BS_raw_data FROM BSRawData WHERE (acoustic_data = {i})'''
data2 = cur.execute(query2).fetchall()
print("len(data2) ", len(data2[0]))
# print("data2 : ", data2)
# data2_retreived = np.frombuffer(data2[0][1], dtype=np.float64)
# print("", data2_retreived)
stg.time.append(np.frombuffer(data2[0][1], dtype=np.float64).reshape((len(stg.freq[i]), -1)))
print(stg.time[0].shape)
print(stg.time)
stg.depth.append(np.frombuffer(data2[0][2], dtype=np.float64).reshape(len(stg.freq[i]), -1))
print(stg.depth[0].shape)
print(stg.depth)
stg.BS_raw_data.append(np.frombuffer(data2[0][3], dtype=np.float64))
# time_reshape_temp = np.insert(time_reshape_temp, time_reshape_temp.shape[0], [x[0] for x in eval("data2")], axis=0)
# stg.depth_reshape.append([x[2] for x in data2])
# stg.BS_raw_data_reshape.append([x[3] for x in data2])
# query = '''SELECT acoustic_file FROM File'''
# cur.execute(query)
# stg.acoustic_data = cur.fetchall()
# Close database cursor
cur.close()
# Close database connection
cnx.close()
def reshape_variables(self):
for i in stg.acoustic_data:
for f, _ in enumerate(stg.freq[i]):
if f == 0:
depth_temp = np.array([
stg.depth_reshape[i][np.where(stg.depth_reshape[i][:, f] == stg.depth_reshape[i][0, f])[0][0]:
np.where(stg.depth_reshape[i][:, f] == stg.depth_reshape[i][0, f])[0][1], f]
])
time_temp = np.array([
stg.time_reshape[i][
np.where(stg.depth_reshape[i][:, f] == stg.depth_reshape[i][0, f])[0], f]
])
else:
# print(np.where(stg.depth_reshape[i][:, f] == stg.depth_reshape[i][0, f]))
depth_temp = np.insert(depth_temp,
depth_temp.shape[0],
stg.depth_reshape[i][np.where(stg.depth_reshape[i][:, f] == stg.depth_reshape[i][0, f])[0][0]:
np.where(stg.depth_reshape[i][:, f] == stg.depth_reshape[i][0, f])[0][1], f],
axis=0)
time_temp = np.insert(time_temp,
time_temp.shape[0],
stg.time_reshape[i][
np.where(stg.depth_reshape[i][:, f] == stg.depth_reshape[i][0, f])[0], f],
axis=0)
stg.depth.append(depth_temp)
stg.time.append(time_temp)
stg.BS_raw_data.append(np.reshape(stg.BS_raw_data_reshape[i],
(len(stg.freq[i]), stg.depth[i].shape[1], stg.time[i].shape[1])))