# Path: Utilities/jtools.py
# class OutOfOrderException(Exception):
# class AnalysisError(Exception):
# class CycledList(list):
#     def __getitem__(self, key):
# def slurm_id_from_output(sbatch_output):
# def check_slurm_for_job(squeue_output):
# def get_slurm_exit_state(sjobexitmod_output):
# def write_finished_file(f):
# def midpoint(x1, x2):
# def distance(x1, x2):
# def ANobj_times_to_time_window(t):
# def break_path(p, line_length):
# def text_location(xrange, yrange):
# def valid_int_from_str(s):
# def valid_float_from_str(s):
# def complex_mag(z):
# def complex_mag_list(zz):
# def type_verify(var, typ):
# def time_window_to_filelike_str(w):
# def scan_config(f):
# def in_tkStringVar_array(s, tksvarr):
# def valid_probe_array(s, f = DEFAULT_CONFIG_FILE):
# def return_methods():
# def valid_method(s):
# def probe_positions(probe_array):
# def valid_window(s):
# def remove_key(d, k):
# def time_window_parser(s):
# def t_in_window(t, win):
# def window_subset(w1, w2):
# def valid_num_or_range(s):
# def valid_shot_str(s):
# def shot_str_parser(s):
# def reverse_shot_str_parser(shot_list):
# def squareish_grid(n, swapxy=False):
# def find_closest(arr, x):


import os
import copy
import itertools
import pickle
import numpy as np
import pyfusion as pf
import pyfusion.clustering as clust
import pyfusion.clustering.extract_features_scans as ext
        import matplotlib.pyplot as plt
        import matplotlib.pyplot as plt  # These imports are here since this file will be imported into an
        import matplotlib as mpl         # environment where matplotib cannot be imported
        import matplotlib.pyplot as plt
        import matplotlib as mpl
from multiprocessing import Pool
from PyFusionGUI import *
from Utilities import jtools as jt
        from time import strftime
        from time import strftime
        axesf1 = axes1.flatten() if n_shots > 1 else np.array([axes1], dtype=object)
        axesf2 = axes2.flatten() if n_shots > 1 else np.array([axes2], dtype=object)
        _big_axes(figure1.add_subplot(111, frameon=False))
        _big_axes(figure2.add_subplot(111, frameon=False))
        for current_axes1, current_axes2, shot, result in \
                zip(axesf1, axesf2, self.DM.shot_info["shots"], self.results):
            assignments = self.z.cluster_assignments
            details = self.z.cluster_details["EM_VMM_kappas"]
            shot_details = self.z.feature_obj.misc_data_dict["shot"]
            time_base = result[3]
            signal = result[2][0, :]
            dt = np.mean(np.diff(time_base))
            current_axes1.specgram(signal, NFFT=1024, Fs=1./dt, noverlap=128, xextent=[time_base[0], time_base[-1]])
            current_axes2.specgram(signal, NFFT=1024, Fs=1./dt, noverlap=128, xextent=[time_base[0], time_base[-1]])
            for assignment in np.unique(assignments):
                mask = (assignments == assignment)*(shot_details == shot)
                if np.sum(mask) > 1 and np.mean(details[assignment, :]) > 5:  # FixMe: What does this do?
                    current_axes1.plot(self.z.feature_obj.misc_data_dict["time"][mask],
                                       self.z.feature_obj.misc_data_dict["freq"][mask],
                                       "o", markersize=markersize,
                                       color=PLOT_COLORS[assignment])
        shots = self.DM.shot_info["shots"]
        time_windows = self.DM.shot_info["time_windows"]
        freq_window = [self.DM.fft_settings["lower_freq"], self.DM.fft_settings["upper_freq"]]
        for i in range(n_shots):
            shot = str(shots[i])
            axesf1[i].set_xlim(time_windows[i])
            axesf2[i].set_xlim(time_windows[i])
            axesf1[i].set_ylim(freq_window)
            axesf2[i].set_ylim(freq_window)

# Path: Utilities/jtools.py
# class OutOfOrderException(Exception):
# class AnalysisError(Exception):
# class CycledList(list):
#     def __getitem__(self, key):
# def slurm_id_from_output(sbatch_output):
# def check_slurm_for_job(squeue_output):
# def get_slurm_exit_state(sjobexitmod_output):
# def write_finished_file(f):
# def midpoint(x1, x2):
# def distance(x1, x2):
# def ANobj_times_to_time_window(t):
# def break_path(p, line_length):
# def text_location(xrange, yrange):
# def valid_int_from_str(s):
# def valid_float_from_str(s):
# def complex_mag(z):
# def complex_mag_list(zz):
# def type_verify(var, typ):
# def time_window_to_filelike_str(w):
# def scan_config(f):
# def in_tkStringVar_array(s, tksvarr):
# def valid_probe_array(s, f = DEFAULT_CONFIG_FILE):
# def return_methods():
# def valid_method(s):
# def probe_positions(probe_array):
# def valid_window(s):
# def remove_key(d, k):
# def time_window_parser(s):
# def t_in_window(t, win):
# def window_subset(w1, w2):
# def valid_num_or_range(s):
# def valid_shot_str(s):
# def shot_str_parser(s):
# def reverse_shot_str_parser(shot_list):
# def squareish_grid(n, swapxy=False):
# def find_closest(arr, x):


# Path: Analysis/analysis.py
import os
import copy
import itertools
import pickle
import numpy as np
import pyfusion as pf
import pyfusion.clustering as clust
import pyfusion.clustering.extract_features_scans as ext
        import matplotlib.pyplot as plt
        import matplotlib.pyplot as plt  # These imports are here since this file will be imported into an
        import matplotlib as mpl         # environment where matplotib cannot be imported
        import matplotlib.pyplot as plt
        import matplotlib as mpl
from multiprocessing import Pool
from PyFusionGUI import *
from Utilities import jtools as jt
        from time import strftime
        from time import strftime

        axesf1 = axes1.flatten() if n_shots > 1 else np.array([axes1], dtype=object)
        axesf2 = axes2.flatten() if n_shots > 1 else np.array([axes2], dtype=object)
        _big_axes(figure1.add_subplot(111, frameon=False))
        _big_axes(figure2.add_subplot(111, frameon=False))
        for current_axes1, current_axes2, shot, result in \
                zip(axesf1, axesf2, self.DM.shot_info["shots"], self.results):
            assignments = self.z.cluster_assignments
            details = self.z.cluster_details["EM_VMM_kappas"]
            shot_details = self.z.feature_obj.misc_data_dict["shot"]
            time_base = result[3]
            signal = result[2][0, :]
            dt = np.mean(np.diff(time_base))
            current_axes1.specgram(signal, NFFT=1024, Fs=1./dt, noverlap=128, xextent=[time_base[0], time_base[-1]])
            current_axes2.specgram(signal, NFFT=1024, Fs=1./dt, noverlap=128, xextent=[time_base[0], time_base[-1]])
            for assignment in np.unique(assignments):
                mask = (assignments == assignment)*(shot_details == shot)
                if np.sum(mask) > 1 and np.mean(details[assignment, :]) > 5:  # FixMe: What does this do?
                    current_axes1.plot(self.z.feature_obj.misc_data_dict["time"][mask],
                                       self.z.feature_obj.misc_data_dict["freq"][mask],
                                       "o", markersize=markersize,
                                       color=PLOT_COLORS[assignment])
        shots = self.DM.shot_info["shots"]
        time_windows = self.DM.shot_info["time_windows"]
        freq_window = [self.DM.fft_settings["lower_freq"], self.DM.fft_settings["upper_freq"]]
        for i in range(n_shots):
            shot = str(shots[i])
            axesf1[i].set_xlim(time_windows[i])
            axesf2[i].set_xlim(time_windows[i])
            axesf1[i].set_ylim(freq_window)
            axesf2[i].set_ylim(freq_window)

            tx, ty = jt.text_location(time_windows[i], freq_window)