Source code for DiatomTrack.gui.load

import ast
import io
import pickle


import numpy as np
import pandas as pd
import tkinter as tk

from tkinter import messagebox
from sys import exit


[docs]class ReadSegmentationWindow: """ Class containing window and reading of segmentation data. """ def __init__(self, file): """ Initialize window and start reading data. Parameters ---------- file : str Path to the segmentation data. Returns ------- None. """ self.file = file self.master = tk.Tk() self.master.title("Read file") self.master.protocol("WM_DELETE_WINDOW", self.close_window) self.master.lift() self.master.focus_force() self.label = tk.Label( self.master, text="Loading from file. Please wait...", width=40) self.label.pack(padx=20, pady=20) self.master.after(10, self.read_file) self.master.mainloop()
[docs] def read_file(self): """ Read the data from file. """ if self.file.split('.')[-1] == 'pkl': with open(self.file, 'rb') as file: self.data = pickle.load(file) else: self.data = {'single': {}, 'aggregate': {}} with open(self.file, 'r') as file: for i, line in enumerate(file): if i > 0: image_name, single_str, aggregate_str = line.strip( ).split(';') single_list = ast.literal_eval(single_str) aggregate_list = ast.literal_eval(aggregate_str) single_arrays = [np.array(arr) for arr in single_list] aggregate_arrays = [ np.array(arr) for arr in aggregate_list] self.data['single'][image_name] = single_arrays self.data['aggregate'][image_name] = aggregate_arrays self.master.after(10, self.master.destroy)
[docs] def close_window(self): """ Confirm abrupt exit. """ if messagebox.askyesno( "Confirm Exit", "Are you sure you want to exit?"): self.master.destroy() self.window_exception()
[docs] def window_exception(self): """ Exit immediately. """ messagebox.showerror( "Window closed.", "Window was closed unexpectedly. The program terminates now.") exit()
[docs]def read_seg(name): """ Create and close window for reading of segmentation data. Parameters ---------- name : str Path to file. Returns ------- dict The segmentation data (contours). """ read = ReadSegmentationWindow(name) return read.data
[docs]class ReadTrackWindow: """ Class containing window and reading of tracking data. """ def __init__(self, file): """ Initialize window and start reading data. Parameters ---------- file : str Path to the track data. Returns ------- None. """ self.file = file self.master = tk.Tk() self.master.title("Read track file") self.master.protocol("WM_DELETE_WINDOW", self.close_window) self.master.lift() self.master.focus_force() self.label = tk.Label( self.master, text="Loading from data from .csv. Please wait...", width=40) self.label.pack(padx=20, pady=20) self.master.after(10, self.read_file) self.master.mainloop()
[docs] def read_file(self): """ Read data from csv to dataframe. """ with open(self.file, 'rb') as file: self.data = pd.read_csv(file, index_col=['f', 'row']) self.master.after(10, self.master.destroy)
[docs] def close_window(self): """ Confirm abrupt exit. """ if messagebox.askyesno( "Confirm Exit", "Are you sure you want to exit?"): self.master.destroy() self.window_exception()
[docs] def window_exception(self): """ Exit immediately. """ messagebox.showerror( "Window closed.", "Window was closed unexpectedly. The program terminates now.") exit()
[docs]def read_track(name): """ Create and close window for reading of tracking data. Parameters ---------- name : str Path to file. Returns ------- Pandas dataframe The tracking data. """ read = ReadTrackWindow(name) return read.data
[docs]class ReadTrackVideoWindow: """ Class containing window and extraction of tracking data from track video file. """ def __init__(self, file): """ Initialize window and start extracting data. Parameters ---------- file : str Path to the video file. Returns ------- None. """ self.file = file self.master = tk.Tk() self.master.title("Read file") self.master.protocol("WM_DELETE_WINDOW", self.close_window) self.master.lift() self.master.focus_force() self.label = tk.Label( self.master, text="Loading from file. Please wait...", width=40) self.label.pack(padx=20, pady=20) self.master.after(10, self.read_file) self.master.mainloop()
[docs] def read_file(self): """ Open video and extract metadata to dataframe. """ with open(self.file, 'rb') as file: data = file.read() dataframe_file = data.split(b'\n===TRACK=DATA===\n')[-1] dataframe_str = dataframe_file.decode('utf-8') dataframe_io = io.StringIO(dataframe_str) self.data = pd.read_csv(dataframe_io, index_col=['f', 'row']) self.master.after(10, self.master.destroy)
[docs] def close_window(self): """ Confirm abrupt exit. """ if messagebox.askyesno( "Confirm Exit", "Are you sure you want to exit?"): self.master.destroy() self.window_exception()
[docs] def window_exception(self): """ Exit immediately. """ messagebox.showerror( "Window closed.", "Window was closed unexpectedly. The program terminates now.") exit()
[docs]def read_tvf(name): """ Create and close window for extraction of tracking data from tvf. Parameters ---------- name : str Path to video file. Returns ------- Pandas dataframe The tracking data. name : str Path to video file. """ read = ReadTrackVideoWindow(name) return read.data, name