text:
#!/usr/bin/env python

"""A class for handling 5C read data."""

import os
import sys

import numpy
import h5py
try:
    import pysam
except:
    pass

class FiveCData(object):

    """
    This class handles interaction count data for 5C experiments.

    The FiveCData class contains all of the interaction information for a 5C experiment, including pairs of fragment indices and their associated counts.

    .. note::
      This class is also available as hifive.FiveCData

    When initialized, this class creates an h5dict in which to store all data associated with this object.

    :param filename: The file name of the h5dict. This should end with the suffix '.hdf5'
    :type filename: str.
    :param mode: The mode to open the h5dict with. This should be 'w' for creating or overwriting an h5dict with name given in filename.
    :type mode: str.
    :param silent: Indicates whether to print information about function execution for this object.
    :type silent: bool.
    :returns: :class:`FiveCData` class object.

    :Attributes: * **file** (*str.*) A string containing the name of the file passed during object creation for saving the object to.
                 * **silent** (*bool.*) - A boolean indicating whether to suppress all of the output messages.
                 * **history** (*str.*) - A string containing all of the commands executed on this object and their outcomes.
    """

    def __init__(self, filename, mode='r', silent=False):
        """Create a :class:`FiveCData` object."""
        self.file = os.path.abspath(filename)
        self.silent = silent
        self.history = ''
        self.filetype = 'fivec_data'
        if mode != 'w':
            self.load()
        return None

    def __getitem__(self, key):
        """Dictionary-like lookup."""
        if key in self.__dict__:
            return self.__dict__[key]
        else:
            return None

    def __setitem__(self, key, value):
        """Dictionary-like value setting."""
        self.__dict__[key] = value
        return None

    def save(self):
        """
        Save analysis parameters to h5dict.

        :returns: None
        """
        self.history.replace("'None'", "None")
        datafile = h5py.File(self.file, 'w')
        for key in self.__dict__.keys():
            if key in ['file', 'chr2int', 'frags', 'silent']:
                continue
            elif isinstance(self[key], numpy.ndarray):
                datafile.create_dataset(key, data=self[key])
            elif not isinstance(self[key], dict):
                datafile.attrs[key] = self[key]
        datafile.close()
        return None

    def load(self):
        """
        Load data from h5dict specified at object creation.

        Any call of this function will overwrite current object data with values from the last :func:`save` call.

        :returns: None
        """
        datafile = h5py.File(self.file, 'r')
        for key in datafile.keys():
            self[key] = numpy.copy(datafile[key])
        for key in datafile['/'].attrs.keys():
            self[key] = datafile['/'].attrs[key]
        # ensure fend h5dict exists
        if 'fragfilename' in self.__dict__:
            fragfilename = self.fragfilename
            if fragfilename[:2] == './':
                fragfilename = fragfilename[2:]
            parent_count = fragfilename.count('../')
            fragfilename = '/'.join(self.file.split('/')[:-(1 + parent_count)] +
                                fragfilename.lstrip('/').split('/')[parent_count:])
            if not os.path.exists(fragfilename):
                if not self.silent:
                    print >> sys.stderr, ("Could not find %s. No fragments loaded.\n") % (fragfilename),
            else:
                self.frags = h5py.File(fragfilename, 'r')
                # create dictionary for converting chromosome names to indices
                self.chr2int = {}
                for i, chrom in enumerate(self.frags['chromosomes']):
                    self.chr2int[chrom] = i
        datafile.close()
        return None

    def load_data_from_counts(self, fragfilename, filelist):
        """
        Read interaction counts from a text file(s) and place in h5dict.

        :param fragfilename: This specifies the file name of the :class:`Fragment` object to associate with the dataset.
        :type fragfilename: str.
        :param filelist: A list containing all of the file names of counts text files to be included in the dataset. If only one file is needed, this may be passed as a string.
        :type filelist: list
        :returns: None

        :Attributes: * **fragfilename** (*str.*) - A string containing the relative path of the fragment file.
                     * **cis_data** (*ndarray*) - A numpy array of type int32 and shape N x 3 where N is the number of valid non-zero intra-regional fragment pairings observed in the data. The first column contains the fragment index (from the 'fragments' array in the Fragment object) of the upstream fragment, the second column contains the idnex of the downstream fragment, and the third column contains the number of reads observed for that fragment pair.
                     * **cis_indices** (*ndarray*) - A numpy array of type int64 and a length of the number of fragments + 1. Each position contains the first entry for the correspondingly-indexed fragment in the first column of 'cis_data'. For example, all of the downstream cis interactions for the fragment at index 5 in the Fragment object 'fragments' array are in cis_data[cis_indices[5]:cis_indices[6], :].
                     * **trans_data** (*ndarray*) - A numpy array of type int32 and shape N x 3 where N is the number of valid non-zero inter-regional fragment pairings observed in the data. The first column contains the fragment index (from the 'fragments' array in the Fragment object) of the upstream fragment (upstream also refers to the lower indexed chromosome in this context), the second column contains the index of the downstream fragment, and the third column contains the number of reads observed for that fragment pair.
                     * **trans_indices** (*ndarray*) - A numpy array of type int64 and a length of the number of fragments + 1. Each position contains the first entry for the correspondingly-indexed fragment in the first column of 'trans_data'. For example, all of the downstream trans interactions for the fragment at index 5 in the Fragment object 'fragments' array are in cis_data[cis_indices[5]:cis_indices[6], :].
                     * **frags** (*ndarray*) - A filestream to the hdf5 Fragment file such that all saved Fragment attributes can be accessed through this class attribute.

        When data is loaded the 'history' attribute is updated to include the history of the Fragment file that becomes associated with it.
        """
        self.history += "FiveCData.load_data_from_counts(fragfilename='%s', filelist=%s) - " % (fragfilename, str(filelist))
        # determine if fragment file exists and if so, load it
        if not os.path.exists(fragfilename):
            if not self.silent:
                print >> sys.stderr, \
                ("The fragment file %s was not found. No data was loaded.\n") % (fragfilename),
            self.history += "Error: '%s' not found\n" % fragfilename
            return None
        self.fragfilename = "%s/%s" % (os.path.relpath(os.path.dirname(os.path.abspath(fragfilename)),
                                       os.path.dirname(self.file)), os.path.basename(fragfilename))
        self.frags = h5py.File(fragfilename, 'r')
        self.history = self.frags['/'].attrs['history'] + self.history
        strands = self.frags['fragments']['strand'][...]
        chr2int = {}
        for i, j in enumerate(self.frags['chromosomes'][:]):
            chr2int[j] = i
        # create fragment name dictionary
        names = {}
        for i in range(self.frags['fragments'].shape[0]):
            names[self.frags['fragments']['name'][i]] = i
        # load data from all files, skipping if name not in the fragment file.
        if isinstance(filelist, str):
            filelist = [filelist]
        total_reads = 0
        data = {}
        for fname in filelist:
            reads = 0
            if not os.path.exists(fname):
                if not self.silent:
                    print >> sys.stderr, ("The file %s was not found...skipped.\n") % (fname.split('/')[-1]),
                self.history += "Error: '%s' not found, " % fname
                continue
            if not self.silent:
                print >> sys.stderr, ("Loading data from %s...") % (fname.split('/')[-1]),
            input = open(fname, 'r')
            for line in input:
                temp = line.strip('\n').split('\t')
                if temp[0] not in names or temp[1] not in names or temp[0] == temp[1]:
                    continue
                frag1 = names[temp[0]]
                frag2 = names[temp[1]]
                # if both in same orientation, skip
                if strands[frag1] == strands[frag2]:
                    continue
                pair = (min(frag1, frag2), max(frag1, frag2))
                if pair not in data:
                    data[pair] = 0
                data[pair] = int(temp[2])
                reads += int(temp[2])
            input.close()
            if not self.silent:
                print >> sys.stderr, ("%i validly-mapped reads loaded.\n") % (reads),
            total_reads += reads
        if len(data) == 0:
            if not self.silent:
                print >> sys.stderr, ("No valid data was loaded.\n"),
            return None
            self.history += "Error: no valid data loaded\n"
        if not self.silent:
            print >> sys.stderr, ("%i total validly-mapped read pairs loaded. %i unique pairs\n") %\
                             (total_reads,len(data)),
        # write fragment pairs to h5dict
        self._parse_fragment_pairs(data)
        self.history += "Success\n"
        return None

    def load_data_from_bam(self, fragfilename, filelist):
        """
        Read interaction counts from pairs of BAM files and place in h5dict.

        :param fragfilename: This specifies the file name of the :class:`Fragment` object to associate with the dataset.
        :type fragfilename: str.
        :param filelist: A list containing lists of paired read end files.
        :type filelist: list
        :returns: None

        :Attributes: * **fragfilename** (*str.*) - A string containing the relative path of the fragment file.
                     * **cis_data** (*ndarray*) - A numpy array of type int32 and shape N x 3 where N is the number of valid non-zero intra-regional fragment pairings observed in the data. The first column contains the fragment index (from the 'fragments' array in the Fragment object) of the upstream fragment, the second column contains the idnex of the downstream fragment, and the third column contains the number of reads observed for that fragment pair.
                     * **cis_indices** (*ndarray*) - A numpy array of type int64 and a length of the number of fragments + 1. Each position contains the first entry for the correspondingly-indexed fragment in the first column of 'cis_data'. For example, all of the downstream cis interactions for the fragment at index 5 in the Fragment object 'fragments' array are in cis_data[cis_indices[5]:cis_indices[6], :].
                     * **trans_data** (*ndarray*) - A numpy array of type int32 and shape N x 3 where N is the number of valid non-zero inter-regional fragment pairings observed in the data. The first column contains the fragment index (from the 'fragments' array in the Fragment object) of the upstream fragment (upstream also refers to the lower indexed chromosome in this context), the second column contains the idnex of the downstream fragment, and the third column contains the number of reads observed for that fragment pair.
                     * **trans_indices** (*ndarray*) - A numpy array of type int64 and a length of the number of fragments + 1. Each position contains the first entry for the correspondingly-indexed fragment in the first column of 'trans_data'. For example, all of the downstream trans interactions for the fragment at index 5 in the Fragment object 'fragments' array are in cis_data[cis_indices[5]:cis_indices[6], :].
                     * **frags** (*filestream*) - A filestream to the hdf5 Fragment file such that all saved Fragment attributes can be accessed through this class attribute.

        When data is loaded the 'history' attribute is updated to include the history of the Fragment file that becomes associated with it.
        """
        self.history += "FiveCData.load_data_from_counts(fragfilename='%s', filelist=%s) - " % (fragfilename, str(filelist))
        if 'pysam' not in sys.modules.keys():
            if not self.silent:
                print >> sys.stderr, ("The pysam module must be installed to use this function.")
            self.history += 'Error: pysam module missing\n'
            return None
        # determine if fragment file exists and if so, load it
        if not os.path.exists(fragfilename):
            if not self.silent:
                print >> sys.stderr, ("The fragment file %s was not found. No data was loaded.\n") % (fragfilename),
            self.history += "Error: '%s' not found\n" % fragfilename
            return None
        self.fragfilename = "%s/%s" % (os.path.relpath(os.path.dirname(os.path.abspath(fragfilename)),
                                       os.path.dirname(self.file)), os.path.basename(fragfilename))
        self.frags = h5py.File(fragfilename, 'r')
        strands = self.frags['fragments']['strand'][...]
        chr2int = {}
        for i, j in enumerate(self.frags['chromosomes'][:]):
            chr2int[j] = i
        # create fragment name dictionary
        names = {}
        for i in range(self.frags['fragments'].shape[0]):
            names[self.frags['fragments']['name'][i]] = i
        # load data from all files, skipping if either fragment not not in the fragment file.
        if isinstance(filelist[0], str):
            filelist = [[filelist[0], filelist[1]]]
        total_reads = 0
        data = {}
        for filepair in filelist:
            # determine which files have both mapped ends present
            present = True
            if not os.path.exists(filepair[0]):
                if not self.silent:
                    print >> sys.stderr, ("%s could not be located.") % (filepair[0]),
                self.history += "'%s' not found, " % filepair[0]
                present = False
            if not os.path.exists(filepair[1]):
                if not self.silent:
                    print >> sys.stderr, ("%s could not be located.") % (filepair[1]),
                self.history += "'%s' not found, " % filepair[1]
                present = False
            if not present:
                if not self.silent:
                    print >> sys.stderr, ("No data for one or both ends could be located. Skipping this run.\n")
            reads = 0
            unpaired = {}
            # load first half of paired ends
            if not self.silent:
                print >> sys.stderr, ("Loading data from %s...") % (filepair[0].split('/')[-1]),
            input = pysam.Samfile(filepair[0], 'rb')
            for read in input.fetch(until_eof=True):
                # Only consider reads with an alignment
                if read.is_unmapped:
                    continue
                # if mapping name not in fragment names, skip
                seq_name = input.getrname(read.tid)
                if seq_name not in names:
                    continue
                # skip multiply-aligned reads
                for tag in read.tags:
                    if tag[0] == 'XS':
                        break
                else:
                    unpaired[read.qname] = names[seq_name]
            input.close()
            if not self.silent:
                print >> sys.stderr, ("Done\n"),
            # load second half of paired ends
            if not self.silent:
                print >> sys.stderr, ("Loading data from %s...") % (filepair[1].split('/')[-1]),
            input = pysam.Samfile(filepair[1], 'rb')
            for read in input.fetch(until_eof=True):
                # Only consinder reads whose paired end was valid
                if read.qname not in unpaired:
                    continue
                # Only consider reads with an alignment
                if read.is_unmapped:
                    continue
                # if mapping name not in fragment names, skip
                seq_name = input.getrname(read.tid)
                if seq_name not in names:
                    continue
                # skip multiply-aligned reads
                for tag in read.tags:
                    if tag[0] == 'XS':
                        break
                else:
                    # if both ends map to the same orientation, skip
                    if strands[unpaired[read.qname]] != strands[names[seq_name]]:
                        pair = (min(unpaired[read.qname], names[seq_name]),
                                max(unpaired[read.qname], names[seq_name]))
                        if pair not in data:
                            data[pair] = 0
                        data[pair] += 1
                        reads += 1
                    del unpaired[read.qname]
            input.close()
            if not self.silent:
                print >> sys.stderr, ("Done\n"),
            if not self.silent:
                print >> sys.stderr, ("Read %i validly_mapped read paired.\n") % (reads),
            total_reads += reads
        if len(data) == 0:
            if not self.silent:
                print >> sys.stderr, ("No valid data was loaded.\n"),
            self.history += "Error: no valid data loaded\n"
            return None
        if not self.silent:
            print >> sys.stderr, ("%i total validly-mapped read pairs loaded. %i unique pairs\n") %\
                             (total_reads,len(data)),
        self._parse_fragment_pairs(data)
        self.history += 'Success\n'
        return None

    def _parse_fragment_pairs(self, frag_pairs):
        """Separate frag pairs into cis (within region) and trans (between region) interactions and write to h5dict with index arrays."""
        if not self.silent:
            print >> sys.stderr, ("Writing fragment pair data to file..."),
        cis = {}
        trans = {}
        for i in range(self.frags['regions'].shape[0]):
            # Find pairs from same region
            for j in range(self.frags['regions']['start_frag'][i], self.frags['regions']['stop_frag'][i] - 1):
                for k in range(j + 1, self.frags['regions']['stop_frag'][i]):
                    if (j, k) in frag_pairs:
                        cis[(j, k)] = frag_pairs[(j, k)]
            # Find pairs from different regions
            for j in range(self.frags['regions']['start_frag'][i], self.frags['regions']['stop_frag'][i]):
                for k in range(self.frags['regions']['stop_frag'][i], self.frags['fragments'].shape[0]):
                    if (j, k) in frag_pairs:
                        trans[(j, k)] = frag_pairs[(j, k)]
        # convert data into arrays
        self.cis_data = numpy.empty((len(cis), 3), dtype=numpy.int32)
        self.trans_data = numpy.empty((len(trans), 3), dtype=numpy.int32)
        keys = cis.keys()
        keys.sort()
        for i in range(len(keys)):
            self.cis_data[i, 0] = keys[i][0]
            self.cis_data[i, 1] = keys[i][1]
            self.cis_data[i, 2] = cis[keys[i]]
        keys = trans.keys()
        keys.sort()
        for i in range(len(keys)):
            self.trans_data[i, 0] = keys[i][0]
            self.trans_data[i, 1] = keys[i][1]
            self.trans_data[i, 2] = trans[keys[i]]
        # find first instance of each fragment for cis and trans data
        if self.cis_data.shape[0] > 0:
            self.cis_indices = numpy.r_[0, numpy.bincount(self.cis_data[:, 0],
                                        minlength=self.frags['fragments'].shape[0])].astype(numpy.int64)
            for i in range(1, self.cis_indices.shape[0]):
                self.cis_indices[i] += self.cis_indices[i - 1]
        if self.trans_data.shape[0] > 0:
            self.trans_indices = numpy.r_[0, numpy.bincount(self.trans_data[:, 0],
                                          minlength=self.frags['fragments'].shape[0])].astype(numpy.int64)
            for i in range(1, self.trans_indices.shape[0]):
                self.trans_indices[i] += self.trans_indices[i - 1]
        if not self.silent:
            print >> sys.stderr, ("Done\n"),
        return None

max_stars_repo_name: bxlab/hifive

max_issues_repo_name: bxlab/hifive

max_forks_repo_name: bxlab/hifive


text:
import os, sys, inspect
import pickle
import numpy as np
import pdb

sys.path.insert(0, '../..')
import util as ut
import varinf
import generate_msbm as gn

def main():
    file_list = sorted(os.listdir('data'))
    # Need to read in original data + trained moments and elbos

    ari_Y_list = []
    ari_spectral_list = []
    mari_Z_list = []
    chd_2_list = []
    pi_dist_list = []
    CH_dist_list = []
    for data_file in file_list:
        print("Reading data + moments from model: {}".format(data_file))
        # load data
        data = ut.load_data('data/' + data_file)
        model_file = data_file.replace('two_prot','msbm')
        model_url = os.path.join('models', model_file)
        results_mom, elbo_seq = ut.load_results(model_url)

        # Adjusted rand index
        ari_Y = ut.adj_rand(results_mom['MU'], data['Y'])
        ari_Y_list.append(ari_Y)
        # Adjusted rand index for Z
        mari_Z = np.mean(ut.adj_rand_Z(results_mom, data))
        mari_Z_list.append(mari_Z)
        # Rand Index of spectral method
        model_file = data_file.replace('two_prot','spectral')
        model_url = os.path.join('models', model_file)
        results_mom, elbo_seq = ut.load_results(model_url)
        ari_spectral= ut.adj_rand(results_mom['MU'], data['Y'])
        ari_spectral_list.append(ari_spectral)

        #####
        # X- Axis
        #####
        #Detectability of second prototype
        chd_2_list.append(data['SNR'])
        ###
        #pi_gamma_dist after unshuffling
        #prototype 0
        gamma_0 = data['GAMMA'][0]
        pi_0 = data['PI'][0]
        #unshuffle
        PQ_0 = np.matmul(np.diag(gamma_0), pi_0)
        com_degs_0 = np.sum(PQ_0 * np.log(data['N']), axis=0)
        orders_0 = np.argsort(com_degs_0)
        gamma_0 = gamma_0[orders_0]
        pi_0 = pi_0[orders_0, :]
        pi_0 = pi_0[:, orders_0]
        #prototype 1
        gamma_1 = data['GAMMA'][1]
        pi_1 = data['PI'][1]
        #unshuffle
        PQ_1 = np.matmul(np.diag(gamma_1), pi_1)
        com_degs_1 = np.sum(PQ_1 * np.log(data['N']), axis=0)
        orders_1 = np.argsort(com_degs_1)
        gamma_1 = gamma_1[orders_1]
        pi_1 = pi_1[orders_1, :]
        pi_1 = pi_1[:, orders_1]

        pi_dist = np.sum( (gamma_0 - gamma_1)**2) + np.sum( (pi_0 - pi_1)**2)
        pi_dist_list.append(pi_dist)

        # Max Min CH-Div criterion
        ch_dist = gn.get_CH_dist(gamma_0, pi_0, gamma_1, pi_1, n = data['N'])
        CH_dist_list.append(ch_dist)

    pdb.set_trace()
    stats_url = os.path.join('stats', 'stats_' + 'mucha.pickle')
    print('Saving file to {:s} ... '.format(stats_url))
    stats_dict = {
        'ari_Y_list':ari_Y_list,
        'ari_spectral_list':ari_spectral_list,
        'mari_Z_list':mari_Z_list,
        'chd_2_list':chd_2_list,
        'pi_dist_list':pi_dist_list,
        'CH_dist_list':CH_dist_list,
        }
    pickle.dump(stats_dict, open(stats_url, 'wb'))
    sys.exit()

if __name__ == '__main__':
    main()

ext: py

max_stars_repo_name: cassianobecker/msbm

max_issues_repo_name: cassianobecker/msbm

max_forks_repo_name: cassianobecker/msbm

reason: import numpy


text:
import numpy as np

class FunctionsBase:
    def __init__(self):
        self.value = None
        self.deriv = None

class Sigmoid(FunctionsBase):
    def __init__(self):
        super().__init__()

    def calculate(self, value):
        self.value = 1/(1+np.exp(-value))
        return self.value

    def derivative(self, x):
        self.deriv = self.calculate(x) * (1-self.calculate(x))
        return self.deriv

class Identity(FunctionsBase):
    def __init__(self):
        super().__init__()

    def calculate(self, value):
        self.value = value
        return self.value

    def derivative(self, x):
        self.deriv = 1 + 0*x
        return self.deriv

class RELU(FunctionsBase):
    def __init__(self):
        super().__init__()

    def calculate(self, value):
        D = (value > 0).astype(int)
        self.value = value*D
        return self.value

    def derivative(self, x):
        self.deriv = (x > 0).astype(int)
        return self.deriv

max_stars_repo_name: BasileiosKal/NeuralNet

max_issues_repo_name: BasileiosKal/NeuralNet

max_forks_repo_name: BasileiosKal/NeuralNet


text:
import cv2, os, sys, math, pickle
import numpy as np
CATEGORIES = ['white','yellow','orange','red','green','blue']

import tensorflow as tf
model = tf.keras.models.load_model("cube_ai_model_9996")

class Cameras_Helper:
    def __init__(self):
        self.cameras = []
        self.Get_valid_Cameras()
        if len(self.cameras) < 2:
            raise Exception("There are not enough cameras to run. Only " + str(len(self.cameras)) + " cameras detected.")

    def Get_valid_Cameras(self):
        for i in range(5):
            cap = self.Test_Device(i)
            if (cap):
                print("Found camera. It is on device video " + str(i) + ".")
                self.cameras.append(cap)

            if len(self.cameras) == 2:
                break

    def Test_Device(self, source):
        cap = cv2.VideoCapture(source)
        if cap is None or not cap.isOpened():
            return None

        return cap

    def Get_Frame(self, camera_number):
        success, frame = self.cameras[camera_number].read()

        return frame

    def Get_Frames(self):
        frames = []
        for camera_number in range(0, len(self.cameras)):
            frames.append(self.Get_Frame(camera_number))

        return frames

    def Generate_Frame(self, camera_number):
        while True:
            success, frame = self.cameras[camera_number].read()

            if not success:
                break
            else:
                ret, buffer = cv2.imencode('.jpg', frame)
                frame = buffer.tobytes()
                yield (b'--frame\r\n'
                    b'Content-Type: image/jpeg\r\n\r\n' + frame + b'\r\n')  # concat frame one by one and show result

    def Switch_Cameras(self):
        self.cameras = self.cameras[::-1]

    def Get_Sticker_Images(self):
        sticker_images = []
        frames = self.Get_Frames()

        point_saves = pickle.load(open( "Saves/Points_Saves.p", "rb"))

        for i, point in enumerate(point_saves):
            frame = frames[self.Get_Camera_Number(i)]
            sticker_images.append(
                self.Get_Sticker_Image(point, frame)
            )

        return sticker_images

    def Get_Sticker_Image(self, point, frame):
        copy_frame = frame.copy()
        return copy_frame[
            point[1] - 25: point[1] + 25,
            point[0] - 25: point[0] + 25
        ]

    def Get_Predicted_Cube_Colors(self):
        sticker_images = self.Get_Sticker_Images()
        colors = []
        for image in sticker_images:
            colors.append(self.Predict_Cube_Color(image))

        return colors

    def Predict_Cube_Color(self, image):
        formatted_image = self.Format_Image_For_Color_Prediction(image)

        # Predict
        predictions = model.predict([formatted_image])[0].tolist()
        prediction = CATEGORIES[predictions.index(max(predictions))]

        return prediction

    def Format_Image_For_Color_Prediction(self, image):
        return np.array((image / 255.0)).reshape(-1, 50, 50, 3)

    def Get_Camera_Number(self, sticker_number):
        side_order = 'urfdlb'
        camera_sides = ['ubr', 'ldf']
        return 1 if side_order[math.floor(sticker_number / 9)] in camera_sides[0] else 0


text:
# -*- coding: utf-8 -*-
#
import sys

from tkinter import *
from tkinter import ttk

import CoolProp
from CoolProp.CoolProp import PropsSI

from cpgui_all import *
import gettext

import numpy as np

from PIL import ImageTk, Image
from scipy.optimize import bisect

class cpg_cycle3(myDialog):

    def __init__(self, GridFrame,Caller,Debug=False):
        #
        self.initcomplete=False
        #
        self.Caller=Caller
        #
        # by module translations
        self.language=cpgui_language
        localedir=find_data_file('locale')
        self.lang = gettext.translation('cpg_cycle2', localedir=localedir, languages=[self.language])
        self.lang.install()
        #
        self.Debug=Debug
        #
        #########################################################################################################################
        # add all input variables to the following list
        self.VarVal         ={
                              't0':268.15,
                              'dt0h':10,
                              'Q0':35000,
                              'T_gc_out':273.15+40,
                              'hp':10000000,
                              'mp': 5500000,
                              'sl_dth':10,
                              'comp_lambda':0.87,
                              'comp_eta':0.769
                              }
        # create data structure like below to generate input form
        self.InputPanels    ={'Evaporator':{'Title':_('Evaporator'),
                                            'row':1,                                                # row of Evaporator Frame in InputFrame
                                            'col':1,                                                # coloumn of Evaporator Frame in InputFrame
                                            'order':['t0','dt0h','Q0'],                       # Input fields order of appearance
                                            't0':[_('Temperature'),self.VarVal['t0'],GUI_UNIT('T'),'T'],           # Input fields, Label, Variable to change, Unit Label / this List will be extended by a tkinter StringVar for callback on change
                                            'dt0h':[_('Superheat'),self.VarVal['dt0h'],GUI_UNIT('dT'),'dT'],
                                            'Q0':[_('Capacity'),self.VarVal['Q0'],GUI_UNIT('P'),'P'],
                                            },
                              'Gascooler':{'Title':_('Gascooler'),
                                            'row':1,
                                            'col':2,
                                            'order':['T_gc_out','hp','empty'],
                                            'T_gc_out':[_('T. out'),self.VarVal['T_gc_out'],GUI_UNIT('T'),'T'],
                                            'hp':[_('High pressure'),self.VarVal['hp'],GUI_UNIT('p'),'p'],
                                            'empty':[' '],
                                            },
                              'Receiver':{'Title':_('Receiver'),
                                            'row':1,
                                            'col':3,
                                            'order':['mp','empty','empty'],
                                            'mp':[_('Middle pressure'),self.VarVal['mp'],GUI_UNIT('p'),'p'],
                                            'empty':[' '],
                                            },
                              'Pipes':{'Title':_('Suction line SL'),
                                            'row':2,
                                            'col':2,
                                            'order':['sl_dth'],
                                            'sl_dth':[_('SL superheat'),self.VarVal['sl_dth'],GUI_UNIT('dT'),'dT'],

                                      },
                              'Compressor':{'Title':_('Compressor'),
                                            'row':2,
                                            'col':1,
                                            'order':['comp_eta','comp_lambda'],
                                            'comp_eta':[_('Isentropic efficiency'),self.VarVal['comp_eta'],GUI_UNIT('eta'),'eta'],
                                            'comp_lambda':[_('Volumetric efficiency'),self.VarVal['comp_lambda'],GUI_UNIT('eta'),'eta'],
                                           },
                              }
        #################################################################################################################################
        #
        self.dialogframe=GridFrame
        #
        self.Caller=Caller
        self.ref=Caller.get_ref()
        #
        self.frameborder=5
        #
        self.statetext=' '
        #
        self.InfoDict={'1'       :('1',             _('Compressor intake =M ')),
                       '2s'      :('2s',            _('Isentropic compression')),
                       '2'       :('2',             _('Real compression')),
                       '3'       :('3',             _('Gascooler out')),
                       'A'       :('A',             _('High pressure controller')),
                       '4'       :('4',             _('Inlet middle pressure receiver')),
                       'C'       :('C',             _('Middle pressure receiver')),
                       '5'       :('5',             _('Liquid out middle pressure receiver')),
                       '6'       :('6',             _('Flashgas middle pressure receiver')),
                       'B'       :('B',             _('middle pressure controller')),
                       '7'       :('7',             _('Evaporator entry')),
                       '8'       :('8',             _('Exit evaporator')),
                       '9'       :('9',             _('Exit middle pressure controller')),
                       '10'      :('10',            _('End of suction line')),
                       'M'       :('M',             _('Mixing point 9 + 10 ')),

                       'order'   :('1','2s','2','3','A','4','C','5','6','B','7','8','9','10','M'),
                       }
        # Frames for input and output
        self.InputFrame= LabelFrame(self.dialogframe,relief=GROOVE,bd=self.frameborder,text=_('Cycle inputs'),font=("Arial", 10, "bold"))
        self.InputFrame.grid(row=1,column=1,padx=8,pady=5,sticky=W)
        self.OutputFrame= LabelFrame(self.dialogframe,relief=GROOVE,bd=self.frameborder,text=_('Cycle outputs'),font=("Arial", 10,"bold"))
        self.OutputFrame.grid(row=3,column=1,padx=8,pady=5,sticky=EW,rowspan=10,columnspan=3)
        #
        # create Inputs form
        #
        for key in self.InputPanels :
            self.GridInputPanel(self.InputFrame,key,Debug=False)
        #
        self.Button_1 = Button(self.InputFrame,text=_(' Calculate '),font=("Arial", 12, "bold") )
        self.Button_1.grid(row=2,rowspan=1,column=3,pady=5,sticky=W,padx=8)
        self.Button_1.bind("<ButtonRelease-1>", self.calculate)
        #
        # Output
        #
        self.out_nb = ttk.Notebook(self.OutputFrame)
        self.out_nb.pack(fill = 'both', expand = 1, padx = 5, pady = 5)
        #
        self.tab1_frame = ttk.Frame(self.OutputFrame)
        self.tab2_frame = ttk.Frame(self.OutputFrame)
        self.tab3_frame = ttk.Frame(self.OutputFrame)
        # tab 1 cycle display and selection
        self.out_nb.add(self.tab1_frame,text=_('Cycle information'))
        #
        self.InfoFrame= LabelFrame(self.tab1_frame,relief=GROOVE,bd=self.frameborder,text=_('Statepoints'),font=("Arial", 10, "bold"))
        self.InfoFrame.grid(row=1,column=1,padx=8,pady=5,sticky=W)

        inforow=1
        for point in self.InfoDict['order']:
            pl=Label(self.InfoFrame,text='{:<10}'.format(self.InfoDict[point][0]),font=("Arial", 10) )
            pl.grid(row=inforow,rowspan=1,column=1,pady=2,sticky=W,padx=2)
            tl=Label(self.InfoFrame,text='{:<35}'.format(self.InfoDict[point][1]),font=("Arial", 10) )
            tl.grid(row=inforow,rowspan=1,column=2,pady=2,sticky=W,padx=2)
            inforow+=1

        self.cycle1png=find_data_file('co2-abb-5.png')
        imgfile = Image.open(self.cycle1png)
        render = ImageTk.PhotoImage(imgfile)
        img = Label(self.tab1_frame, image=render)
        img.image = render
        img.grid(row=1,column=2,pady=5,sticky=NS,padx=8)
        #
        self.out_nb.add(self.tab2_frame,text=_('Output table'))
        #
        self.statetext=' '
        lbframe1 = Frame( self.tab2_frame )
        self.Text_1_frame = lbframe1
        scrollbar1 = Scrollbar(lbframe1, orient=VERTICAL)
        self.Text_1 = Text(lbframe1, width="110", height="25", yscrollcommand=scrollbar1.set)
        scrollbar1.config(command=self.Text_1.yview)
        scrollbar1.pack(side=RIGHT, fill=Y)
        self.Text_1.pack(side=LEFT, fill=BOTH, expand=1)
        self.Text_1_frame.grid(row=1,column=1,columnspan=1,padx=2,sticky=W+E,pady=4)
        self.Text_1.delete(1.0, END)
        #
        self.out_nb.bind_all("<<NotebookTabChanged>>", self.tabChangedEvent)
        self.initcomplete=True

    def GridInputPanel(self,GridFrame,PanelKey,Debug=False,tfont=("Arial", 10, "bold"),font=("Arial", 10)):
        #
        LineList=self.InputPanels[PanelKey]['order']
        GIPanel=LabelFrame(GridFrame,relief=GROOVE,bd=5,text=self.InputPanels[PanelKey]['Title'])
        GIPanel.grid(row=self.InputPanels[PanelKey]['row'],column=self.InputPanels[PanelKey]['col'],padx=8,pady=5,sticky=W)
        #
        i=1
        for k in self.InputPanels[PanelKey]['order'] :
            if self.InputPanels[PanelKey][k][0] != ' ' :
                self.InputPanels[PanelKey][k].append(StringVar())
                outval=SI_TO(self.InputPanels[PanelKey][k][-2], self.InputPanels[PanelKey][k][1])
                #print('cycle 1 : GridInputPanel : SI_TO : ',outval)
                #self.InputPanels[PanelKey][k][-1].set('%f'%self.InputPanels[PanelKey][k][1])
                self.InputPanels[PanelKey][k][-1].set(outval)
                self.InputPanels[PanelKey][k][-1].trace("w", lambda name, index, mode, var=self.InputPanels[PanelKey][k][-1],quantity=self.InputPanels[PanelKey][k][-2], key=k: self.GridInputPanelUpdate(var, key, quantity))
                Label(GIPanel, text=self.InputPanels[PanelKey][k][0],font=font).grid(column=1, row=i,padx=8,pady=5,sticky=W)
                Entry(GIPanel, width=15, textvariable=self.InputPanels[PanelKey][k][-1],font=font).grid(column=2, row=i,padx=8,pady=5,sticky=W)
                Label(GIPanel, text=self.InputPanels[PanelKey][k][2],font=font).grid(column=3, row=i,padx=8,pady=5,sticky=W)
            else :
                Label(GIPanel, text=' ',font=font).grid(column=1, row=i,padx=8,pady=5,sticky=W)
            #
            i+=1
    #
    def GridInputPanelUpdate(self, sv,key,quantity):
        #print(sv, key, sv.get(),quantity)
        #self.VarVal[key]=sv.get()
        try :
            self.VarVal[key]=TO_SI(quantity,float(sv.get().replace(',','.')))
        except ValueError :
            pass
        #print( key,self.VarVal[key])

    def tabChangedEvent(self,event):
        self.ref='CO2'

    def calculate(self,event):
        #
        self.ref='CO2'
        #
        t0          =   self.VarVal['t0']           # Evaporation temperature in K
        dt0h        =   self.VarVal['dt0h']         # Superheat in K
        Q0          =   self.VarVal['Q0']           # refrigeration capacity in W
        T_gc_out    =   self.VarVal['T_gc_out']       # Gascooler outlet temperature in K
        hp          =   self.VarVal['hp']           # High pressure
        mp          =   self.VarVal['mp']           # Middle pressure
        sl_dth      =   self.VarVal['sl_dth']       # superheat suction line in K
        comp_eta    =   self.VarVal['comp_eta']     # Compressor isentropic efficiency
        comp_lambda =   self.VarVal['comp_lambda']  # Compressor volumetric efficiency
        #
        self.statetext=_('Cycle with middle pressure receiver, statepoint Table for %s\n\n')%self.ref
        self.statetext+=(" Point  |   t    |   p     |      v     |    h    |    s    |    x    | Description \n")
        self.datarow=    " %6s | %6.2f | %6.2f  | %10.5f | %7.2f | %7.4f | %7s | %s \n"
        self.statetext+="        |   {}   |  {}    |    {}   |   {} | {} |  {}  |    \n".format(GUI_UNIT('T'),GUI_UNIT('p'),GUI_UNIT('v'),GUI_UNIT('H'),GUI_UNIT('S'),GUI_UNIT('Q'),)
        self.statetext+= "-------------------------------------------------------------------------------------------------\n"
        self.row={}
        #
        ### Gascooler Exit #########################################
        #
        name='3'
        p3=hp
        t3=T_gc_out
        h3=PropsSI('H','P',p3,'T',t3,self.ref)
        v3=1/PropsSI('D','T',t3,'P',p3,self.ref)
        s3=PropsSI('S','T',t3,'P',p3,self.ref)
        x3='       '
        self.row[name]=self.datarow%(self.InfoDict[name][0],SI_TO('T',t3),SI_TO('p',p3),v3,SI_TO('H',h3),SI_TO('S',s3),x3,self.InfoDict[name][1])
        #
        ### Middle pressure receiver inlet #########################################
        #
        name='4'
        p4=mp
        t4=PropsSI('T','P',p4,'Q',0,self.ref)
        h4=h3
        v4=1/PropsSI('D','P',p4,'H',h4,self.ref)
        s4=PropsSI('S','P',p4,'H',h4,self.ref)
        x4=PropsSI('Q','P',p4,'H',h4,self.ref)
        self.row[name]=self.datarow%(self.InfoDict[name][0],SI_TO('T',t4),SI_TO('p',p4),v4,SI_TO('H',h4),SI_TO('S',s4),'%5.4f'%x4,self.InfoDict[name][1])
        #
        ### Middle pressure receiver Liquid out #########################################
        #
        name='5'
        p5=mp
        t5=PropsSI('T','P',p5,'Q',0,self.ref)
        h5=PropsSI('H','P',p5,'Q',0,self.ref)
        v5=1/PropsSI('D','P',p5,'Q',0,self.ref)
        s5=PropsSI('S','P',p5,'Q',0,self.ref)
        x5='       '
        self.row[name]=self.datarow%(self.InfoDict[name][0],SI_TO('T',t5),SI_TO('p',p5),v5,SI_TO('H',h5),SI_TO('S',s5),x5,self.InfoDict[name][1])
        #
        ### Middle pressure receiver Gas out #########################################
        #
        name='6'
        p6=mp
        t6=PropsSI('T','P',p6,'Q',1,self.ref)
        v6=1/PropsSI('D','P',p6,'Q',1,self.ref)
        h6=PropsSI('H','P',p6,'Q',1,self.ref)
        s6=PropsSI('S','P',p6,'Q',1,self.ref)
        x6='       '
        self.row[name]=self.datarow%(self.InfoDict[name][0],SI_TO('T',t6),SI_TO('p',p6),v6,SI_TO('H',h6),SI_TO('S',s6),x6,self.InfoDict[name][1])
        #
        ### Evaporator entry #########################################
        #
        name='7'
        p7=PropsSI('P','T',t0,'Q',0,self.ref)
        t7=t0
        h7=h5
        v7=1/PropsSI('D','P',p7,'H',h7,self.ref)
        s7=PropsSI('S','P',p7,'H',h7,self.ref)
        x7=PropsSI('Q','P',p7,'H',h7,self.ref)
        self.row[name]=self.datarow%(self.InfoDict[name][0],SI_TO('T',t7),SI_TO('p',p7),v7,SI_TO('H',h7),SI_TO('S',s7),'%5.4f'%x7,self.InfoDict[name][1])
        #
        ### Evaporator out #########################################
        #
        name='8'
        p8=p7
        t8=t7+dt0h
        h8=PropsSI('H','P',p8,'T',t8,self.ref)
        v8=1/PropsSI('D','P',p8,'H',h8,self.ref)
        s8=PropsSI('S','P',p8,'H',h8,self.ref)
        x8='       '
        self.row[name]=self.datarow%(self.InfoDict[name][0],SI_TO('T',t8),SI_TO('p',p8),v8,SI_TO('H',h8),SI_TO('S',s8),x8,self.InfoDict[name][1])
        #
        ### Exit middle pressure controller #########################################
        #
        name='9'
        p9=p8
        h9=h6
        t9=t7
        v9=1/PropsSI('D','P',p9,'H',h9,self.ref)
        s9=PropsSI('S','P',p9,'H',h9,self.ref)
        x9=PropsSI('Q','P',p9,'H',h9,self.ref)
        self.row[name]=self.datarow%(self.InfoDict[name][0],SI_TO('T',t9),SI_TO('p',p9),v9,SI_TO('H',h9),SI_TO('S',s9),'%5.4f'%x9,self.InfoDict[name][1])
        #
        ### Suction line out #########################################
        #
        name='10'
        p10=p8
        t10=t8+sl_dth
        h10=PropsSI('H','P',p10,'T',t10,self.ref)
        v10=1/PropsSI('D','P',p10,'H',h10,self.ref)
        s10=PropsSI('S','P',p10,'H',h10,self.ref)
        x10='       '
        self.row[name]=self.datarow%(self.InfoDict[name][0],SI_TO('T',t10),SI_TO('p',p10),v10,SI_TO('H',h10),SI_TO('S',s10),x10,self.InfoDict[name][1])
        #
        ### compressor inlet #########################################
        #
        name='1'
        massflow_evaporator = Q0 /(h8-h7)
        massflow_compressor = massflow_evaporator/(1-x4)
        massflow_flashgas   = massflow_compressor - massflow_evaporator
        #
        p1=p10
        h1=(massflow_evaporator * h10 + massflow_flashgas * h9 ) / massflow_compressor
        t1=PropsSI('T','P',p1,'H',h1,self.ref)
        v1=1/PropsSI('D','H',h1,'P',p1,self.ref)
        h1=PropsSI('H','H',h1,'P',p1,self.ref)
        s1=PropsSI('S','H',h1,'P',p1,self.ref)
        x1=PropsSI('Q','P',p1,'H',h1,self.ref)
        if x1 == -1 :
            x8  ='       '
            xx1 ='sh. gas'
        if x1 >=0 and x1 <=1 :
            xx1 = '%5.4f'%x1
        self.row[name]=self.datarow%(self.InfoDict[name][0],SI_TO('T',t1),SI_TO('p',p1),v1,SI_TO('H',h1),SI_TO('S',s1),xx1,self.InfoDict[name][1])
        #
        ### isentropic compression #########################################
        #
        name='2s'
        p2s=hp
        s2s=s1
        t2s=PropsSI('T','P',p2s,'S',s2s,self.ref)
        v2s=1/PropsSI('D','T',t2s,'P',p2s,self.ref)
        h2s=PropsSI('H','T',t2s,'P',p2s,self.ref)
        x2s='       '
        self.row[name]=self.datarow%(self.InfoDict[name][0],SI_TO('T',t2s),SI_TO('p',p2s),v2s,SI_TO('H',h2s),SI_TO('S',s2s),x2s,self.InfoDict[name][1])
        #
        ### real compression #########################################
        #
        name='2'
        p2=hp
        #
        isentropic_work = h2s-h1
        real_work       = isentropic_work / comp_eta
        #
        h2= h1 + real_work
        #
        t2=PropsSI('T','P',p2,'H',h2,self.ref)
        v2=1/PropsSI('D','T',t2,'P',p2,self.ref)
        s2=PropsSI('S','T',t2,'P',p2,self.ref)
        x2='       '
        self.row[name]=self.datarow%(self.InfoDict[name][0],SI_TO('T',t2),SI_TO('p',p2),v2,SI_TO('H',h2),SI_TO('S',s2),x2,self.InfoDict[name][1])
        #
        for point in self.InfoDict['order']:
            try :
                self.statetext+=self.row[point]
            except KeyError :
                pass
        #
        # from here units need to be fixed
        #
        self.statetext+=_('\nPower calculations                      | Key performance values\n')
        self.statetext+=_('Evaporator             %8.2f    kW   | Pressure ratio             %8.2f \n')%(SI_TO('P',Q0),(p2/p1))
        self.statetext+=_('Gascooler              %8.2f    kW   | Pressure difference        %8.2f %s\n')%(((h2-h3)*massflow_compressor/1000),SI_TO('p',p2-p1),GUI_UNIT('p'))
        self.statetext+=_('Compressor             %8.2f    kW   \n\n')%((real_work*massflow_compressor/1000))

        self.statetext+=_('Mass flow Evaporator       %5.5f kg/s | Mass flow flashgas             %8.6f kg/s \n')%(massflow_evaporator,massflow_flashgas)
        self.statetext+=_('Mass flow Compressor       %5.5f kg/s | Volume flow Compressor       %8.4f   m³/h \n')%(massflow_compressor,(massflow_compressor*3600)/(comp_lambda/v1))
        #self.statetext+=_('Compressor                 %8.2f kW  | Volumetric capacity        %8.2f kJ/m³ \n')%(((h2-h1)*self.mdot),(SI_TO('P',self.Q0)/(v1*self.mdot*1000)))
        #self.statetext+=_('                                        | COP                        %8.2f \n')%(SI_TO('P',self.Q0)/((h2-h1)*self.mdot))
        #
        self.Text_1.delete(1.0, END)
        self.Text_1.insert(END, self.statetext)
        self.out_nb.select(1)

        self.Text_1.delete(1.0, END)
        self.Text_1.insert(END, self.statetext)
        self.out_nb.select(1)

    def Update(self):
        #
        pass

class _Testdialog:

    def __init__(self, master):
        frame = Frame(master)
        frame.pack()
        self.Caller = master
        self.x, self.y, self.w, self.h = -1,-1,-1,-1
        #
        self.ref='R134a'
        #
        App=cpg_cycle3(frame,self,Debug=True)

    def get_ref(self):
        return 'R134a'
    def get_language(self):
        return 'de'

def main():
    root = Tk()

    app = _Testdialog(root)
    root.mainloop()

if __name__ == '__main__':
    main()

max_stars_repo_name: emjames/GUI

max_issues_repo_name: emjames/GUI

max_forks_repo_name: emjames/GUI

reason: import numpy,from scipy


text:
import matplotlib.pyplot as plt
from PIL import Image
import pandas as pd
from sklearn.metrics import mean_absolute_error, mean_squared_error, r2_score
from sklearn.metrics import f1_score, precision_score, confusion_matrix, recall_score, accuracy_score, mean_squared_error
import numpy as np

def plot_grid(image_list):
    fig, axes = plt.subplots(ncols=2, nrows=2)

    for ax, img in zip(axes.flatten(), image_list):
        ax.imshow(Image.open(img))
        ax.axis('off')

    fig.set_size_inches(8, 6)
    plt.subplots_adjust(wspace=0, hspace=0)
    plt.show()

def basic_cleaning_pre(data):
    #Some columns used in excel for pre-processing
    data.drop(['Text Conversion', 'Text Conversion.1', 'Text Conversion.2',
       'Text Conversion.3', 'Text Conversion.4', 'Text Conversion.5',
       'Text Conversion.6', 'Founded Date_Formatted', 'Text Conversion.7',
       'Text Conversion.8', 'Text Conversion.9', 'Text Conversion.10',
       'Text Conversion.11', 'Text Conversion.12', 'Text Conversion.13'], axis=1, inplace=True)
    # Drop rows marked red in Excel. Total=17
    data.drop([851, 1688, 1779, 1782, 1784,
            1788, 1789, 1793, 1795, 1814,
            1816, 1819, 1820, 1851, 1853,
            1875, 1888], inplace=True)

    # To remove cases where there are 3 separators. Usually for Edinburgh.
    for i in data.index:
        loc = data['Headquarters Location'][i].split(',')
        if len(loc) > 3:
            new_loc = loc[0] + ',' + loc[1] + ',' + loc[3]
            data['Headquarters Location'][i] = new_loc

    location_info = data['Headquarters Location'].str.split(', ', expand=True)
    location_info.columns = ['City', 'State', 'Country']

    data.insert(3, 'City', location_info['City'].values)
    data.insert(4, 'State', location_info['State'].values)
    data.insert(5, 'Country', location_info['Country'].values)

    data.drop('Headquarters Location', axis=1, inplace=True)

    return data

def mean_absolute_percentage_error(y_true, y_pred):
    y_true, y_pred = np.array(y_true), np.array(y_pred)
    return np.mean(np.abs((y_true - y_pred) / y_true)) * 100

def calc_metrics(y_true, y_pred):
    """Calculates the error metrics to evaluate the model. Calculates the R2
    score, Pseudo-R2 Score, Root Mean Squared Error and Mean Absolute Error.
    """
    r2 = r2_score(y_true, y_pred)
    rmse = np.sqrt(mean_squared_error(y_true, y_pred))
    mae = mean_absolute_error(y_true, y_pred)
    mape = mean_absolute_percentage_error(y_true, y_pred)

    print("R2", r2, '\n', "RMSE", rmse, '\n', "MAE", mae, '\n', "MAPE", mape)

def plot_feat_imp(dataframe, model, cols_to_drop):
    feat_imp = sorted(zip(map(lambda x: round(x, 4), model.feature_importances_),
                      dataframe.drop(cols_to_drop, axis=1).columns))
    feat_imp_sort = [(elem2, elem1) for elem1, elem2 in feat_imp]
    plt.figure(figsize=[10,6])
    plt.style.use('seaborn')
    plt.title('Feature Importance - Sklearn')
    plt.barh(*zip(*feat_imp_sort))
    plt.xlabel('Importance')
    plt.show()

def grad_boost_reg(X_train, y_train, X_test, y_test, model):
    model.fit(X_train, y_train)
    pred = model.predict(X_test)
    calc_metrics(y_test, pred)

def grad_boost_classification(X_train, y_train, X_test, y_test, model):
    model.fit(X_train, y_train)
    pred = model.predict(X_test)
    calc_metrics_class(pred, y_test)

def calc_metrics_class(pred, y_test):
   precision = precision_score(pred, y_test)
   recall = recall_score(pred,y_test)
   f1 = f1_score(pred,y_test)
   accuracy = accuracy_score(pred,y_test)
   print("precision", precision, '\n', "recall", recall, '\n', "f1", f1, '\n', "accuracy", accuracy)


text:
# coding=utf-8
# Copyright 2019 The Tensor2Tensor Authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""Trax backend: all the primitive functions needed."""

from __future__ import absolute_import
from __future__ import division
from __future__ import print_function

import contextlib
import gin

import jax
from jax import lax
from jax import random as jax_random
import jax.numpy as jnp
import jax.scipy.special as jax_special
import numpy as onp
import tensorflow_datasets as tfds

def jax_conv(inp, fltr, window_strides, padding, dimension_numbers,
             filter_dilation=None):
  """A wrapper around `lax.conv_general_dilated`.

  It requires `dimension_numbers` and disallows `inp_dilation`.

  Args:
    inp: an (N+2)-D array. The input of the convolution.
    fltr: an (N+2)-D array. The filter (i.e. kernel) of the convolution.
    window_strides: the strides for moving the convolution window.
    padding: a string, either "VALID" or "SAME". The padding algorithm.
    dimension_numbers: a tuple of three strings encoding the data format of
      input, filter and output. "I" means input; "O" means output; "C" means
      channel; other characters such as "W", "H" and "D" means spatial
      dimensions.
    filter_dilation: the dilation rates for the filter. Dilating the filter
      means adding "holes" to the filter.

  Returns:
    An (N+2)-D array. The convolution result.
  """
  return lax.conv_general_dilated(inp, fltr, window_strides, padding,
                                  lhs_dilation=None,
                                  rhs_dilation=filter_dilation,
                                  dimension_numbers=dimension_numbers)

def _pooling_general(inputs, reducer, init_val, rescaler=None,
                     pool_size=(2, 2), strides=None, padding="VALID"):
  """Helper: general pooling computation used in pooling layers later."""
  spatial_strides = strides or (1,) * len(pool_size)
  rescale = rescaler(pool_size, spatial_strides, padding) if rescaler else None
  dims = (1,) + pool_size + (1,)  # NHWC
  strides = (1,) + spatial_strides + (1,)
  out = lax.reduce_window(inputs, init_val, reducer, dims, strides, padding)
  return rescale(out, inputs) if rescale else out

def jax_max_pool(x, pool_size, strides, padding):
  return _pooling_general(x, lax.max, -jnp.inf, pool_size=pool_size,
                          strides=strides, padding=padding)

def jax_sum_pool(x, pool_size, strides, padding):
  return _pooling_general(x, lax.add, 0., pool_size=pool_size,
                          strides=strides, padding=padding)

def _normalize_by_window_size(dims, spatial_strides, padding):  # pylint: disable=invalid-name
  def rescale(outputs, inputs):
    one = jnp.ones(inputs.shape[1:-1], dtype=inputs.dtype)
    window_sizes = lax.reduce_window(
        one, 0., lax.add, dims, spatial_strides, padding)
    return outputs / window_sizes[..., jnp.newaxis]
  return rescale

def jax_avg_pool(x, pool_size, strides, padding):
  return _pooling_general(x, lax.add, 0., _normalize_by_window_size,
                          pool_size, strides=strides, padding=padding)

_JAX_BACKEND = {
    "name": "jax",
    "np": jnp,
    "logsumexp": jax_special.logsumexp,
    "conv": jax_conv,
    "avg_pool": jax_avg_pool,
    "max_pool": jax_max_pool,
    "sum_pool": jax_sum_pool,
    "jit": jax.jit,
    "grad": jax.grad,
    "pmap": jax.pmap,
    "random_uniform": jax_random.uniform,
    "random_normal": jax_random.normal,
    "random_bernoulli": jax_random.bernoulli,
    "random_get_prng": jax.jit(jax_random.PRNGKey),
    "random_split": jax_random.split,
    "dataset_as_numpy": tfds.as_numpy,
}

_NUMPY_BACKEND = {
    "name": "numpy",
    "np": onp,
    "jit": (lambda f: f),
    "random_get_prng": lambda seed: None,
    "random_split": lambda prng, num=2: (None,) * num,
}

def get_name():
  return backend()["name"]

def logsumexp(*args, **kwargs):
  return backend()["logsumexp"](*args, **kwargs)

def conv(*args, **kwargs):
  return backend()["conv"](*args, **kwargs)

def avg_pool(*args, **kwargs):
  return backend()["avg_pool"](*args, **kwargs)

def max_pool(*args, **kwargs):
  return backend()["max_pool"](*args, **kwargs)

def sum_pool(*args, **kwargs):
  return backend()["sum_pool"](*args, **kwargs)

def jit(*args, **kwargs):
  return backend()["jit"](*args, **kwargs)

def grad(*args, **kwargs):
  return backend()["grad"](*args, **kwargs)

def pmap(*args, **kwargs):
  return backend()["pmap"](*args, **kwargs)

def dataset_as_numpy(*args, **kwargs):
  return backend()["dataset_as_numpy"](*args, **kwargs)

# For numpy and random modules, we need to call "backend()" lazily, only when
# the function is called -- so that it can be set by gin configs.
# (Otherwise, backend() is called on import before gin-config is parsed.)
# To do that, we make objects to encapsulated these modules.

class RandomBackend(object):
  """Backend providing random functions."""

  def get_prng(self, seed):
    return backend()["random_get_prng"](seed)

  def split(self, prng, num=2):
    return backend()["random_split"](prng, num)

  def uniform(self, *args, **kwargs):
    return backend()["random_uniform"](*args, **kwargs)

  def normal(self, *args, **kwargs):
    return backend()["random_normal"](*args, **kwargs)

  def bernoulli(self, *args, **kwargs):
    return backend()["random_bernoulli"](*args, **kwargs)

random = RandomBackend()

# A class that just forwards attribute accesses to backend's numpy object.
class NumpyBackend(object):

  def __getattr__(self, attr):
    return getattr(backend()["np"], attr)

numpy = NumpyBackend()

override_backend_name = None

@gin.configurable()
def backend(name="jax"):
  name = name if not override_backend_name else override_backend_name
  if name == "numpy":
    return _NUMPY_BACKEND
  return _JAX_BACKEND

@contextlib.contextmanager
def use_backend(name):
  global override_backend_name
  prev_name = override_backend_name
  override_backend_name = name
  yield
  override_backend_name = prev_name

max_stars_repo_name: yyht/bert

max_issues_repo_name: yyht/bert

max_forks_repo_name: yyht/bert

reason: import numpy,import jax,from jax


text:
from typing import Iterable, Tuple

import numpy as np
from .simulations_data_config import *
from matplotlib import pyplot as plt
import seaborn as sns
from lifelines import KaplanMeierFitter
from ..config import *
import os
import string
import warnings
warnings.filterwarnings('ignore')

def add_panel_text(ax, text, xplace=-0.15, fsz=17):
    ax.text(xplace, 1.1, text, transform=ax.transAxes, fontsize=fsz,
            fontweight='bold', va='top', ha='right')

def plot_first_model_coefs(models, times, train_df, n_cov=5, filename=None):
    fig, axes = plt.subplots(1, 2, figsize=(14, 6))
    ax = axes[0]
    ax.tick_params(axis='both', which='major', labelsize=15)
    ax.tick_params(axis='both', which='minor', labelsize=15)
    add_panel_text(ax=ax, text='a')
    ax.set_title(r'$\alpha_{jt}$', fontsize=26)
    ax.scatter(times, models[1].params[:len(times)].values, label='J=1 (Pred)', color='tab:blue')
    ax.plot(times, -1 -0.3*np.log(times), label='J=1 (True)', ls='--', color='tab:blue')
    ax.scatter(times, models[2].params[:len(times)].values, label='J=2 (Pred)', color='tab:green')
    ax.plot(times, -1.75 -0.15*np.log(times), label='J=2 (True)', ls='--', color='tab:green')
    ax.set_xlabel(r'Time', fontsize=18)
    ax.set_ylabel(r'$\alpha_{t}$', fontsize=18)
    ax.legend(loc='upper center', fontsize=14)
    ax.set_ylim([-3, 0.5])
    ax2 = ax.twinx()
    ax2.hist(train_df[train_df['J'] != 0]['X'], color='r', alpha=0.3, bins=times)
    ax2.set_ylabel('N events', fontsize=16, color='red')
    ax2.tick_params(axis='y', colors='red')

    ax = axes[1]
    ax.tick_params(axis='both', which='major', labelsize=15)
    ax.tick_params(axis='both', which='minor', labelsize=15)
    add_panel_text(ax=ax, text='b')
    ax.set_title(r'$\beta_{j}$', fontsize=26)
    ax.set_xlabel(r'Covariate', fontsize=18)
    ax.set_ylabel(r'$\beta}$', fontsize=18)
    ax.bar(np.arange(1, n_cov+1), models[1].params[-n_cov:], label='J=1 (Pred)', width=0.3, alpha=0.4, color='tab:blue')
    ax.scatter(-0.2+np.arange(1, n_cov+1), -np.log([0.8, 3, 3, 2.5, 2]), color='tab:blue', label='J=1 (True)',
               marker="4", s=130)
    ax.bar(np.arange(1, n_cov+1), models[2].params[-n_cov:], color='tab:green', label='J=2 (Pred)', align='edge',
           width=0.3, alpha=0.4)
    ax.scatter(0.35+np.arange(1, n_cov+1), -np.log([1, 3, 4, 3, 2]), color='tab:green', label='J=2 (True)', marker="3",
               s=130)
    ax.legend(loc='upper center', fontsize=14)
    ax.set_ylim([-1.5, 1])
    fig.tight_layout()
    if filename is not None:
        fig.savefig(os.path.join(OUTPUT_DIR, filename), dpi=300)

def plot_second_model_coefs(alpha_df, beta_models, times, n_cov=5, filename=None):
    fig, axes = plt.subplots(1, 2, figsize=(14, 6))
    ax = axes[0]
    ax.tick_params(axis='both', which='major', labelsize=15)
    ax.tick_params(axis='both', which='minor', labelsize=15)
    add_panel_text(ax=ax, text='a')
    ax.set_title(r'$\alpha_{jt}$', fontsize=26)
    tmp_ajt = alpha_df[alpha_df['J'] == 1]
    ax.scatter(tmp_ajt['X'].values, tmp_ajt['alpha_jt'].values, label='J=1 (Pred)', color='tab:blue')
    ax.plot(times, -1 -0.3*np.log(times), label='J=1 (True)', ls='--', color='tab:blue' )
    tmp_ajt = alpha_df[alpha_df['J'] == 2]
    ax.scatter(tmp_ajt['X'].values, tmp_ajt['alpha_jt'].values, label='J=2 (Pred)', color='tab:green')
    ax.plot(times, -1.75 -0.15*np.log(times), label='J=2 (True)', ls='--', color='tab:green')
    ax.set_xlabel(r'Time', fontsize=18)
    ax.set_ylabel(r'$\alpha_{t}$', fontsize=18)
    ax.legend(loc='upper center', fontsize=14)
    ax.set_ylim([-3, 0.5])
    ax2 = ax.twinx()
    ax2.bar(alpha_df.groupby('X')['n_jt'].sum().index, alpha_df.groupby('X')['n_jt'].sum().values, color='r', alpha=0.3)
    ax2.set_ylabel('N events', fontsize=16, color='red')
    ax2.tick_params(axis='y', colors='red')

    ax = axes[1]
    ax.tick_params(axis='both', which='major', labelsize=15)
    ax.tick_params(axis='both', which='minor', labelsize=15)
    add_panel_text(ax=ax, text='b')
    ax.set_title(r'$\beta_{j}$', fontsize=26)
    ax.bar(np.arange(1, n_cov+1), beta_models[1].params_.values, label='J=1 (Pred)', width=0.3, alpha=0.4,
           color='tab:blue')
    ax.scatter(-0.2+np.arange(1, n_cov+1), -np.log([0.8, 3, 3, 2.5, 2]), color='tab:blue', label='J=1 (True)',
               marker="4", s=130)
    ax.bar(np.arange(1, n_cov+1), beta_models[2].params_.values, color='tab:green', label='J=2 (Pred)', align='edge',
           width=0.3, alpha=0.4)
    ax.scatter(0.35+np.arange(1, n_cov+1), -np.log([1, 3, 4, 3, 2]), color='tab:green', label='J=2 (True)', marker="3",
               s=130)
    ax.legend(loc='upper center', fontsize=14)
    ax.set_xlabel('Covariate', fontsize=18)
    ax.set_ylabel(r'$\beta}$', fontsize=18)
    ax.set_ylim([-1.5, 1])
    fig.tight_layout()
    ax.legend(loc='upper center', fontsize=14)
    fig.tight_layout()
    if filename is not None:
        fig.savefig(os.path.join(OUTPUT_DIR, filename), dpi=300)

def plot_models_coefficients(alpha_dict: dict, beta_dict: dict, times: Iterable,
                             njt_counts: Iterable,
                             n_cov: int = 5,
                             first_model_name: str = 'Lee',
                             second_model_name: str = 'Ours',
                             filename: str = None) -> None:
    """
    This method takes the repetitive runs results and plotting the comparison between the methods coefs

    Args:
        alpha_dict (dict): a dict that contains for each event type (key) a dataframe of all the $\alpha_t$ (value)
        beta_dict (dict): a dict that contains for each event type(key) a dataframe of all the $\beta_t$ (value)
        times (Iterable): array like that contains all the unique times that were used
        njt_counts (Iterable): an array-like which contains how many events per each time t (not including censorship)
        n_cov (int): number of covariates (used to plot beta plot)
        first_model_name (Optional[str]): the name of the first model
        second_model_name (Optional[str]): the name of the second model

    Returns:
        None
    """
    fig, axes = plt.subplots(1, 2, figsize=(14, 6))
    ax = axes[0]
    ax.tick_params(axis='both', which='major', labelsize=15)
    ax.tick_params(axis='both', which='minor', labelsize=15)
    add_panel_text(ax=ax, text='a')
    ax.set_title(r'$\alpha_{jt}$', fontsize=26)
    tmp_ajt = alpha_dict[1]
    ax.scatter(times, tmp_ajt[f'{first_model_name}_mean'].values,
               label=f'J=1 ({first_model_name} Pred)', color='tab:blue', marker='o', alpha=0.4, s=40)
    ax.scatter(times, tmp_ajt[f'{second_model_name}_mean'].values,
               label=f'J=1 ({second_model_name} Pred)', color='navy', marker='*', alpha=0.7, s=40)
    ax.plot(times, tmp_ajt['real_mean'].values, label='J=1 (True)', ls='--', color='tab:blue')
    tmp_ajt = alpha_dict[2]
    ax.scatter(times, tmp_ajt[f'{first_model_name}_mean'].values,
               label=f'J=2 ({first_model_name} Pred)', color='tab:green', alpha=0.4, s=30)
    ax.scatter(times, tmp_ajt[f'{second_model_name}_mean'].values,
               label=f'J=2 ({second_model_name} Pred)', color='darkgreen', marker='*', alpha=0.7, s=30)
    ax.plot(times, tmp_ajt['real_mean'].values, label='J=2 (True)', ls='--', color='tab:green')
    ax.set_xlabel(r'Time', fontsize=18)
    ax.set_ylabel(r'$\alpha_{t}$', fontsize=18)
    ax.legend(loc='upper center', fontsize=14)
    ax.set_ylim([-3, 0.5])
    ax2 = ax.twinx()
    ax2.bar(times, njt_counts, color='r', alpha=0.3)
    ax2.set_ylabel('N events', fontsize=16, color='red')
    ax2.tick_params(axis='y', colors='red')

    ax = axes[1]
    ax.tick_params(axis='both', which='major', labelsize=15)
    ax.tick_params(axis='both', which='minor', labelsize=15)
    ax.set_title(r'$\beta_{j}$', fontsize=26)
    add_panel_text(ax=ax, text='b')
    beta_j = beta_dict[1]
    ax.bar(np.arange(1, n_cov + 1), beta_j['real_mean'].values, label='J=1 (True)', width=0.3, alpha=0.4,
           color='tab:blue')
    ax.scatter(-0.2 + np.arange(1, n_cov + 1), beta_j[f'{first_model_name}_mean'].values,
               color='tab:blue', label=f'J=1 ({first_model_name} Pred)', marker="4", s=130)
    ax.scatter(-0.2 + np.arange(1, n_cov + 1), beta_j[f'{second_model_name}_mean'].values,
               color='navy', label=f'J=1 ({second_model_name} Pred)', marker=">", s=130, alpha=0.4)

    beta_j = beta_dict[2]
    ax.bar(np.arange(1, n_cov + 1), beta_j['real_mean'].values, color='tab:green', label='J=2 (True)', align='edge',
           width=0.3, alpha=0.4)
    ax.scatter(0.35 + np.arange(1, n_cov + 1), beta_j[f'{first_model_name}_mean'].values,
               color='tab:green', label=f'J=2 ({first_model_name} Pred)', marker="3",
               s=130)
    ax.scatter(0.35 + np.arange(1, n_cov + 1), beta_j[f'{second_model_name}_mean'].values,
               color='darkgreen', label=f'J=2 ({second_model_name} Pred)', marker="<",
               s=130, alpha=0.4)
    ax.set_xlabel('Covariate', fontsize=18)
    ax.set_ylabel(r'$\beta}$', fontsize=18)
    ax.legend(loc='upper center', fontsize=14)
    ax.set_ylim([-1.5, 1])
    fig.tight_layout()
    if filename is not None:
        fig.savefig(os.path.join(OUTPUT_DIR, filename), dpi=300)

def plot_LOS_simulation_figure1(data_df):
    text_sz = 16

    fig, axes = plt.subplots(2, 2, figsize=(14, 8))

    ax = axes[0, 0]
    add_panel_text(ax=ax, text='a')
    data_df.groupby([ADMISSION_YEAR_COL, DEATH_MISSING_COL]).size().unstack().plot(kind='bar', stacked=True, ax=ax)
    ax.set_ylabel('Number of patients', fontsize=font_sz)
    ax.set_xlabel('Year of admission', fontsize=font_sz)
    ax.legend(labels=['Yes', 'No'], title="In hospital death", fontsize='small', fancybox=True)
    ax.set_ylim([0, 1000])

    ax = axes[0, 1]
    add_panel_text(ax=ax, text='b')
    tmp = data_df[[AGE_COL, GENDER_COL]]
    tmp[AGE_COL] = pd.cut(tmp[AGE_COL], bins=AGE_BINS, labels=AGE_LABELS)
    tmp.groupby([AGE_COL, GENDER_COL]).size().unstack().plot(kind='bar', ax=ax)
    ax.set_xlabel('Age [years]', fontsize=font_sz)
    ax.set_ylabel('Number of patients', fontsize=font_sz)
    ax.set_xticklabels(AGE_LABELS, rotation=90)

    ax = axes[1, 0]
    add_panel_text(ax=ax, text='c')
    ser = data_df.sort_values(by=[PATIENT_NO_COL, ADMISSION_SERIAL_COL]).drop_duplicates(
        subset=[PATIENT_NO_COL], keep='last')[ADMISSION_SERIAL_COL]
    sns.distplot(ser, kde=False, ax=ax)
    ax.set_yscale('log')
    ax.set_ylabel('Number of patients', fontsize=font_sz)
    ax.set_xlabel('Number of admissions', fontsize=font_sz)
    ax.axvline(1.5, ls='--', color='r')
    ax.axvline(4.5, ls='--', color='r')
    ax.axvline(8.5, ls='--', color='r')
    ax.text(x=0.5, y=1100, s='0', fontsize=text_sz, color='r')
    ax.text(x=2.5, y=1100, s='1', fontsize=text_sz, color='r')
    ax.text(x=6.5, y=1100, s='2', fontsize=text_sz, color='r')
    ax.text(x=10, y=1100, s='3', fontsize=text_sz, color='r')
    ax.text(x=9, y=200, s='Returning patient\ngroup', fontsize=text_sz, color='r')

    ax.grid('both', 'both')

    ax = axes[1, 1]
    add_panel_text(ax=ax, text='d')
    kmf = KaplanMeierFitter(label='In-hospital death censoring')
    T = data_df[DISCHARGE_RELATIVE_COL].values
    E = data_df[DEATH_RELATIVE_COL].isnull().astype(int).values
    kmf.fit(durations=T, event_observed=E)
    kmf.plot_survival_function(ax=ax)

    kmf = KaplanMeierFitter(label='No censoring')
    T = data_df[DISCHARGE_RELATIVE_COL].values
    E = np.ones_like(T)
    kmf.fit(durations=T, event_observed=E)
    kmf.plot_survival_function(ax=ax)

    ax.set_ylabel('Population', fontsize=font_sz)
    ax.set_xlabel('LOS', fontsize=font_sz)
    ax.set_xlim([0, 30])
    ax.grid()

    fig.tight_layout()

def plot_LOS_simulation_figure2(data_df):
    tmp = data_df.copy()
    tmp['binned_age'] = pd.cut(tmp[AGE_COL], bins=AGE_BINS, labels=AGE_LABELS)
    tmp['death_at_hosp_ind'] = tmp[DEATH_RELATIVE_COL].notnull().astype(int)

    max_time = 30
    total_ad = len(data_df)
    ihd = (tmp.groupby(DEATH_RELATIVE_COL).size() / total_ad).reindex(range(0, max_time + 1)).fillna(0)
    ihd = ihd.loc[:max_time].cumsum()
    released = ((tmp.groupby(DISCHARGE_RELATIVE_COL).size() -
                 tmp.groupby(DEATH_RELATIVE_COL).size()) / total_ad).reindex(range(0, max_time + 1)).fillna(0)
    released = released.loc[:max_time].cumsum()
    o = np.ones_like(released)

    fig, axes = plt.subplots(2, 2, figsize=(12, 7))

    text_sz = 12
    every_nth = 5

    ax = axes[0, 0]
    add_panel_text(ax=ax, text='a')
    ax.fill_between(x=released.index, y1=(o - released.values - ihd.values), color='b', alpha=0.4)
    ax.fill_between(x=released.index, y1=o, y2=(o - released.values), color='g', alpha=0.4)
    ax.fill_between(x=released.index, y1=(o - released.values), y2=(o - released.values - ihd.values), color='r',
                    alpha=0.4)
    ax.text(x=1, y=0.2, s='Hospitalized', fontsize=text_sz)
    ax.text(x=20, y=0.8, s='Released', fontsize=text_sz)
    ax.text(x=20, y=0.3, s='Dead', fontsize=text_sz)
    ax.set_ylim([0, 1])
    ax.set_xlim([0, max_time])
    ax.set_xlabel('LOS', fontsize=font_sz)
    ax.set_ylabel('Patient status ratio', fontsize=font_sz)

    ax = axes[0, 1]
    add_panel_text(ax=ax, text='b')
    tmp[tmp['death_at_hosp_ind'] == 1].groupby(['binned_age', GENDER_COL]).size().unstack().plot(kind='bar', ax=ax)
    ax.set_xlabel('Age [years]', fontsize=font_sz)
    ax.set_ylabel('Number of patients', fontsize=font_sz)
    ax.set_xticklabels(AGE_LABELS, rotation=90)
    ax.set_title('In-hospital Death', fontsize=title_sz)

    ax = axes[1, 0]
    add_panel_text(ax=ax, text='c')
    ser = tmp[tmp['death_at_hosp_ind'] != 1].groupby(DISCHARGE_RELATIVE_COL).size()
    ser = ser.reindex(range(0, max_time + 1)).fillna(0)
    ser.plot(kind='bar', ax=ax)
    ax.set_xlim([0, max_time])
    ax.set_ylabel('Released', fontsize=font_sz)
    ax.set_xlabel('LOS', fontsize=font_sz)
    ax.grid(axis='y')

    for idl, label in enumerate(ax.xaxis.get_ticklabels()):
        if (idl % every_nth) > 0:
            label.set_visible(False)

    ax = axes[1, 1]
    add_panel_text(ax=ax, text='d')
    ser = tmp[tmp['death_at_hosp_ind'] == 1].groupby(DEATH_RELATIVE_COL).size()
    ser = ser.reindex(range(0, max_time + 1)).fillna(0)
    ser.plot(kind='bar', ax=ax)
    ax.set_xlim([0, max_time])
    ax.set_ylabel('Died', fontsize=font_sz)
    ax.set_xlabel('LOS', fontsize=font_sz)
    ax.grid(axis='y')

    for idl, label in enumerate(ax.xaxis.get_ticklabels()):
        if (idl % every_nth) > 0:
            label.set_visible(False)

    # ax = axes[2, 0]
    # add_panel_text(ax=ax, text='e')
    # ser = tmp[tmp['death_at_hosp_ind'] != 1].groupby(DISCHARGE_RELATIVE_COL).size()
    # ser = ser.reindex(range(0, max_time + 1)).fillna(0)
    # ser.plot(kind='bar', logy=True, ax=ax)
    # ax.set_xlim([0, max_time])
    # ax.set_ylabel('Released', fontsize=font_sz)
    # ax.set_xlabel('Days from Hospitalization', fontsize=font_sz)
    # ax.grid(axis='y')
    # ax.set_ylim([0, 1000])
    #
    # for idl, label in enumerate(ax.xaxis.get_ticklabels()):
    #     if (idl % every_nth) > 0:
    #         label.set_visible(False)
    #
    # ax = axes[2, 1]
    # add_panel_text(ax=ax, text='f')
    # ser = tmp[tmp['death_at_hosp_ind'] == 1].groupby(DEATH_RELATIVE_COL).size()
    # ser = ser.reindex(range(0, max_time + 1)).fillna(0)
    # ser.plot(kind='bar', logy=True, ax=ax)
    # ax.set_xlim([0, max_time])
    # ax.set_ylabel('Died', fontsize=font_sz)
    # ax.set_xlabel('Days from Hospitalization', fontsize=font_sz)
    # ax.grid(axis='y')
    # ax.set_ylim([0, 1000])
    #
    # for idl, label in enumerate(ax.xaxis.get_ticklabels()):
    #     if (idl % every_nth) > 0:
    #         label.set_visible(False)

    fig.tight_layout()

def plot_LOS_simulation_figure3(data_df):
    fig, ax = plt.subplots(1, 1, figsize=(6, 4))

    missingness_cols = [WEIGHT_COL]
    missingness_titles = [WEIGHT_COL]
    cols = missingness_cols
    tmp = data_df[cols + [ADMISSION_YEAR_COL]]
    tmp['missing'] = tmp.apply(lambda row: row.isnull().any(), axis=1)
    tmp.groupby([ADMISSION_YEAR_COL, 'missing']).size().unstack().plot(kind='bar', stacked=True, ax=ax)
    ax.set_ylabel('Number of patients', fontsize=font_sz)
    ax.set_xlabel('Year of admission', fontsize=font_sz)
    ax.set_title(missingness_titles[0], fontsize=title_sz)
    ax.set_ylim([0, 1100])
    fig.tight_layout()

# todo: move from here
def compare_beta_models_for_example(first_models: dict, second_models: dict,
                                    n_cov: int = 5, real_coef_dict: dict = None) -> dict:
    """

    Args:
        first_models:
        second_models:
        n_cov:
        real_coef_dict (dict):

    Returns:

    """
    from pydts.utils import compare_models_coef_per_event
    models_dict = {
        "alpha": {},
        "beta": {}
    }
    assert real_coef_dict is not None, "The user should supply the coefficients of the experiment"
    for event in first_models.keys():
        for model_type in models_dict.keys():
            if model_type == "alpha":
                first_slicing = slice(-n_cov)    # for alpha, similar to [:-n_cov]
                first_model = first_models[event].params[first_slicing].copy()
                first_model.index = first_model.index.str.replace(r"\D+", "", regex=True)
                first_model = first_model.add_prefix("a")
                second_model = second_models[event][1][["X", "alpha_jt"]].copy()
                second_model = second_model.set_index("X")["alpha_jt"].add_prefix("a")
                real_coef = real_coef_dict[model_type][event](np.arange(1, first_model.index.shape[0] + 1))
            else:
                first_slicing = slice(-n_cov, None)  # for beta, similar to [-n_cov:]
                first_model = first_models[event].params[first_slicing].copy()
                second_model = second_models[event][0].params_ # beta
                real_coef = real_coef_dict[model_type][event]
            models_dict[model_type][event] = compare_models_coef_per_event(first_model=first_model,
                                                                           second_model=second_model,
                                                                           real_values=real_coef,
                                                                           event=event,
                                                                           first_model_label="Lee",
                                                                           second_model_label="Ours"
                                                                           )
    return models_dict

def plot_reps_coef_std(rep_dict: dict, return_summary: bool = True, filename: str = None):
    """

    Args:
        rep_dict:
        return_summary:
        filename:
    Returns:

    """
    # todo: use mean as well
    # todo: split to different functions
    alphabet_list = list(string.ascii_lowercase)
    first_key = next(iter(rep_dict))    # deal with cases where there isn't 0 in samples
    coef_types = list(rep_dict[first_key].keys())  # alpha, beta
    event_types = rep_dict[first_key][coef_types[0]].keys()
    mapping = {t: i for i, t in enumerate(coef_types)}
    fig, axes = plt.subplots(len(coef_types), len(event_types), figsize=(12, 10))
    res_dict = {coef: {event_type: None for event_type in event_types} for coef in coef_types}
    for idct, coef_type in enumerate(coef_types):
        for idet, event_type in enumerate(event_types):
            ax = axes[mapping[coef_type]][event_type - 1]
            add_panel_text(ax=ax, text=alphabet_list[idct*len(event_types)+idet])
            ax.tick_params(axis='both', which='major', labelsize=15)
            ax.tick_params(axis='both', which='minor', labelsize=15)
            df = pd.concat([dfs[coef_type][event_type] for dfs in rep_dict.values()])
            temp_df = df.groupby(df.index).agg(["mean", "std"])
            prefix = "a" if coef_type == "alpha" else "Z"
            temp_df = temp_df.loc[[f"{prefix}{idx}_{event_type}" for idx in range(1, temp_df.shape[0]+1)]]
            temp_df.columns = temp_df.columns.get_level_values(0) + "_" + temp_df.columns.get_level_values(1)
            res_dict[coef_type][event_type] = temp_df.copy()
            temp_df.plot(x="Lee_std", y="Ours_std", kind="scatter", ax=ax)
            ax.set_xlabel("Lee std", fontsize=18)
            ax.set_ylabel("Ours std", fontsize=18)
            ax.plot([0, 1], [0, 1], "--", transform=ax.transAxes, alpha=0.3, color="tab:green");
            ax.grid()
            # todo remove paper limits
            if idct == 0:
                ax.set_xlim([0, 0.6])
                ax.set_ylim([0, 0.6])
            elif ((idct == 1) and (idet == 0)):
                ax.set_xlim([0.02, 0.04])
                ax.set_ylim([0.02, 0.04])
            elif ((idct == 1) and (idet == 1)):
                ax.set_xlim([0.04, 0.06])
                ax.set_ylim([0.04, 0.06])
            latter = "\\alpha" if coef_type == "alpha" else "\\beta"
            ax.set_title(f"${latter}{event_type}$", fontsize=18)
    fig.tight_layout()
    fig.show()
    if filename is not None:
        fig.savefig(os.path.join(OUTPUT_DIR, filename), dpi=300)

    if return_summary:
        return res_dict

def plot_times(times_dict: dict,
               filename: str = None,
               ax = None, color='tab:blue') -> None:
    """

    Args:
        times_dict:
        filename:
    Returns:

    """

    if ax is None:

        ax = pd.DataFrame.from_dict(times_dict).boxplot(figsize=(8, 6), boxprops={"lw": 1.5, "color": "tab:blue"},
                                                        medianprops={"lw": 2, "color": "tab:green"},
                                                        flierprops={'markeredgecolor': "tab:blue"})
    else:
        pd.DataFrame.from_dict(times_dict).boxplot(boxprops={"lw": 1.5, "color": color},
                                                   flierprops={'markeredgecolor': color},
                                                   medianprops={"lw": 2, "color": color}, ax=ax)

    # ax.set_ylim(0, 15)
    ax.tick_params(axis='both', which='major', labelsize=15)
    ax.tick_params(axis='both', which='minor', labelsize=15)
    ax.set_ylabel("Fitting Time [seconds]", fontdict={"size": 18}) # "weight": 'bold',
    ax.set_xlabel("Model type", fontdict={"size": 18}) # "weight": 'bold',

    #ax.tick_params(labelsize=14, grid_lw=0.5, grid_alpha=0.6)
    plt.tight_layout()
    plt.show()
    if filename is not None:
        ax.figure.savefig(os.path.join(OUTPUT_DIR, filename), dpi=300)
    return ax

def plot_cif_plots(pred_df: pd.DataFrame, event: str, return_ax: bool = False, ax: plt.Axes = None,
                   pad: float = 0.15, scale: int = 5) -> None:
    """
    this method plot cif given pred df with cif and event

    Args:
        pred_df:
        event:
        return_ax (bool): Whether to return the ax object
        ax (plt.Axes): Axes
        pad (float): the pad to the y-axis
        scale (int): the scaling of the y-axis pad

    Returns:

    """
    cif_cols = pred_df.columns[pred_df.columns.str.startswith("cif")]

    event_cif_cols = cif_cols[cif_cols.str.contains(f"j{event}")]

    event_x = event_cif_cols.str.extract(r"(t\d+)")[0].str.extract((r"(\d+)")).apply(pd.to_numeric).values.flatten()
    if ax is None:
        ax = pred_df[event_cif_cols].T.plot(figsize=(10, 10))
    else:
        pred_df[event_cif_cols].T.plot(ax=ax)
    ax.tick_params(axis='both', which='major', labelsize=15)
    ax.tick_params(axis='both', which='minor', labelsize=15)
    ax.set_xticks(event_x)
    ax.set_xticklabels(event_x)
    y_min, y_max = get_y_perc_limits(pred_df, event_cif_cols, pad=pad, scale=scale)
    ax.set_ylim(bottom=y_min, top=y_max)
    ax.set_xlabel("t", fontdict={'size': 18})  # , "weight": "bold"
    ax.set_ylabel(f"CIF of $J_{event}$", fontdict={'size': 18})  # , "weight": "bold"
    ax.grid()
    if return_ax:
        return ax
    else:
        plt.tight_layout()
        plt.show()

def scale_perc_limits(num: float, scale: int, up: bool = False):
    """

    Args:
        num:
        scale:
        up:

    Returns:

    """
    func = np.ceil if up else np.floor
    return (func((num * 100) / scale) * scale) / 100

def get_y_perc_limits(df: pd.DataFrame, cols: Iterable, pad: float = 0.15, scale: int = 5) -> Tuple[float, float]:
    """

    Args:
        df (pd.DataFrame):
        cols (Iterable):
        pad (float):
        scale (int):

    Returns:

    """
    y_min = max(df[cols].min().min() - pad, 0)
    y_min = scale_perc_limits(y_min, scale=scale, up=False)

    y_max = min(df[cols].max().max() + pad, 1)
    y_max = scale_perc_limits(y_max, scale=scale, up=True)
    return y_min, y_max


text:
"""
   Copyright 2017 Islam Elnabarawy

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
"""
import argparse

import numpy as np

from examples.base_example import BaseExample

__author__ = 'Islam Elnabarawy'
__description__ = 'Run a scripted example using the SC2CollectMineralShards-v2 environment.'

_PLAYER_FRIENDLY = 1
_PLAYER_NEUTRAL = 3  # beacon/minerals

_ENV_NAME = "SC2CollectMineralShards-v2"

class CollectMineralShardsGroups(BaseExample):
    def __init__(self, visualize=False, step_mul=None) -> None:
        super().__init__(_ENV_NAME, visualize, step_mul)
        self.next_group = 0

    def get_action(self, env, obs):
        neutral_y, neutral_x = (obs[0] == _PLAYER_NEUTRAL).nonzero()
        marine_y, marine_x = ((obs[0] == _PLAYER_FRIENDLY) - obs[1]).nonzero()
        if not neutral_y.any():
            raise Exception('No minerals found!')
        if not marine_y.any():
            marine_y, marine_x, _ = obs[1].nonzero()
        if not marine_y.any():
            raise Exception('No marines found!')
        marine = [np.ceil(marine_x.mean()).astype(int), np.ceil(marine_y.mean()).astype(int)]
        shards = np.array(list(zip(neutral_x, neutral_y)))
        closest_ix = np.argmin(np.linalg.norm(np.array(marine) - shards, axis=1))
        target = shards[closest_ix].tolist()
        group = [self.next_group + 1]
        self.next_group = (self.next_group + 1) % 2
        return group + target

def main():
    parser = argparse.ArgumentParser(description=__description__)
    parser.add_argument('--visualize', type=bool, default=False,
                        help='show the pysc2 visualizer')
    parser.add_argument('--num-episodes', type=int, default=10,
                        help='number of episodes to run')
    parser.add_argument('--step-mul', type=int, default=None,
                        help='number of game steps to take per turn')
    args = parser.parse_args()

    example = CollectMineralShardsGroups(args.visualize, args.step_mul)
    rewards = example.run(args.num_episodes)
    print('Total reward: {}'.format(rewards.sum()))
    print('Average reward: {} +/- {}'.format(rewards.mean(), rewards.std()))
    print('Minimum reward: {}'.format(rewards.min()))
    print('Maximum reward: {}'.format(rewards.max()))

if __name__ == "__main__":
    main()