text:
import cartopy.crs as ccrs
import matplotlib.pyplot as plt
from .tools import map_large_scale, add_colorbar
import numpy as np
import xarray as xr

def plot_500hPa_composite(ds,ax=None, label='', colorbar=True, x_qk=0.93, y_qk=0.9, receptor_loc=None
                            ,receptor_name=None,vector_scale=1, angles='xy',hatches='xx', hatch_color='gray'):
    if ax==None:
        ax = plt.gca()
    map_large_scale(ax)
    extent = ax.get_extent()
    ds = ds.sel(longitude=slice(extent[0],extent[1]),latitude=slice(extent[3],extent[2]))
    if receptor_name and isinstance(receptor_name,str):
        Z = receptor_name+'_Z'
        u = receptor_name+'_u'
        v = receptor_name+'_v'
        hws = receptor_name+'_hws'
    else:
        Z='Z'
        u = 'u'
        v = 'v'
        hws = 'hws'
    im = ds[hws].plot.contourf(transform=ccrs.PlateCarree(),  levels= np.linspace(-7.5,7.5,16),
                                                    cmap='bwr', add_colorbar=False, ax=ax)
    CS = ds[Z].plot.contour(transform=ccrs.PlateCarree(), ax=ax,colors='black', linewidths=1,
                       add_labels=False, alpha=1,  vmin=-6, vmax=6, levels=7)
    CS.collections[3].set_linewidth(2)
    ax.text( x=0.03,y=0.94, s=label, fontsize=16, transform=ax.transAxes)
    cs=ds[Z+'_significance_map_005'].where(ds[hws+'_significance_map_005'] !=0, drop=True).plot.contourf(ax=ax,colors='none',hatches=[hatches, None],
                 add_colorbar=False)
    for i, collection in enumerate(cs.collections):
        collection.set_edgecolor(hatch_color)
        collection.set_linewidth(0.)

    Q = ax.quiver(ds.longitude[::22], ds.latitude[::22], ds[u][::22,::22],
                   ds[v][::22,::22],transform=ccrs.PlateCarree(),color='saddlebrown',
              units='xy', zorder=1002, minlength=2, pivot='middle', scale=vector_scale,angles=angles)
    # ax.clabel(CS, fmt='%d', colors='black', fontsize=12, inline=1, zorder=1030)
    qk=ax.quiverkey(Q, x_qk,y_qk, U=2, label='2 m/s', labelpos='E', coordinates='axes', color='black')
    qk.text.set_backgroundcolor('w')
    ax.set_xlabel('')
    ax.set_ylabel('')

    if colorbar:
        add_colorbar(im,im.levels, label='Composite difference 500hPa winds [m/s] \n  (strong years - weak years)')
    if receptor_loc and isinstance(receptor_loc,list):
        ax.scatter(receptor_loc[0], receptor_loc[1], color='black', marker='*')

def plot_200hPa_composite(ds,ax=None, label='', colorbar=True, receptor_loc=None,  x_qk=0.93, y_qk=0.9,
                            receptor_name=None,vector_scale=1, angles='xy',vmin=-6, vmax=6,hatches='xx', hatch_color='gray'):
    if ax==None:
        ax = plt.gca()
    map_large_scale(ax)
    extent = ax.get_extent()
    ds = ds.sel(longitude=slice(extent[0],extent[1]),latitude=slice(extent[3],extent[2]))
    if receptor_name and isinstance(receptor_name,str):
        Z = receptor_name+'_Z'
        hws = receptor_name+'_hws'
    else:
        Z='Z'
        hws = 'hws'
    im = ds[hws].plot.contourf(transform=ccrs.PlateCarree(),
                                        cmap='bwr', add_colorbar=False, ax=ax, levels=np.linspace(-9,9, 16))
    ax.text( x=0.03,y=0.94, s=label, fontsize=16, transform=ax.transAxes)
    ax.set_xlabel('')
    ax.set_ylabel('')
    CS = ds[Z].plot.contour(transform=ccrs.PlateCarree(), ax=ax,colors='black', linewidths=1, add_labels=False, alpha=1,
                           vmin=vmin, vmax=vmax, levels=13)
    CS.collections[6].set_linewidth(3)

    ax.clabel(CS, fmt='%d', colors='black', fontsize=12, inline=1, zorder=1030)
    cs=ds[Z+'_significance_map_005'].where(ds[Z+'_significance_map_005'] !=0, drop=True).plot.contourf(ax=ax,colors='none',hatches=[hatches, None],
                 add_colorbar=False)
    for i, collection in enumerate(cs.collections):
        collection.set_edgecolor(hatch_color)
        collection.set_linewidth(0.)
    if colorbar:
        add_colorbar(im,im.levels, label='Composite difference 200hPa winds [m/s] \n  (strong years - weak years)')
    if receptor_loc and isinstance(receptor_loc,list):
        ax.scatter(receptor_loc[0], receptor_loc[1], color='black', marker='*')
    return im

def plot_mslp_850hpa_composite(ds,
oro='/mnt/acam-ns2806k/ovewh/tracing_the_winds/Master_thesis_UiO_workflow/Master_thesis_UiO_workflow/downloads/ERA5_orography.nc'
                                ,ax=None, x_qk=0.93, y_qk=0.9, label='', colorbar=True, title='', receptor_loc=None,
                                receptor_name=None, vector_scale=1, angles='xy',U=2, q_label='',
                                vmin=-6, vmax=6, significance_mask=True,hatches='xx', hatch_color='gray'):
    oro = xr.open_dataset(oro)
    oro = oro.sel(longitude=slice(69,105), latitude=slice(40,27)).isel(time=0)
    if receptor_name and isinstance(receptor_name,str):
        msl = receptor_name+'_msl'
        u = receptor_name+'_u'
        v = receptor_name+'_v'
    else:
        msl='msl'
        u = 'u'
        v = 'v'
    if ax==None:
        ax = plt.gca()
    map_large_scale(ax)
    extent = ax.get_extent()
    ds = ds.sel(longitude=slice(extent[0],extent[1]),latitude=slice(extent[3],extent[2]))
    im = ds[msl].plot.contourf(transform=ccrs.PlateCarree(),levels=16, vmin=vmin, vmax=vmax,
                                                               cmap='bwr', add_colorbar=False, ax=ax)
    if significance_mask:
        cs=ds[msl+'_significance_map_005'].where(ds[msl+'_significance_map_005'] !=0, drop=True).plot.contourf(ax=ax,colors='none',hatches=[hatches, None],
                    add_colorbar=False)
        for i, collection in enumerate(cs.collections):
            collection.set_edgecolor(hatch_color)
            collection.set_linewidth(0.)
    # cs.collections[0].set_edgecolor('gainsboro')
    if q_label == '':
        q_label = f'{U} m/s'
    Q = ax.quiver(ds.longitude[::22], ds.latitude[::22], ds[u][::22,::22],
                   ds[v][::22,::22],transform=ccrs.PlateCarree(),color='saddlebrown',
              units='xy', zorder=1002, minlength=2, pivot='middle', scale=vector_scale, angles = angles)
    qk=ax.quiverkey(Q, x_qk,y_qk, U=U, label=q_label, labelpos='E', coordinates='axes', color='black')
    qk.text.set_backgroundcolor('w')
    oro.z.where(oro.z > 33000,drop=True).plot.contourf(transform=ccrs.PlateCarree(),
                                                       levels=2, colors='lightgrey'
                                                        , add_colorbar=False,ax =ax, zorder=1100, hatches='/')
    if colorbar:
        add_colorbar(im,im.levels, label='Mean Sea Level Pressure \n  (strong years - weak years)')
    if receptor_loc and isinstance(receptor_loc,list):
        ax.scatter(receptor_loc[0], receptor_loc[1], color='black', marker='*', zorder=2000)

    ax.text( x=0.03,y=0.94, s=label, fontsize=14, transform=ax.transAxes)

    ax.set_xlabel('')
    ax.set_ylabel('')
    ax.set_title(title)
    return im

def plot_which_years_composited(composite_ds,ax=None, xlabelsize=8,locs=None,**scatter_kwargs):
    if ax==None:
        ax = plt.gca()
    ax.cla()
    ax.set_xticks(range(1999,2020))
    ax.set_xlim(1998,2020)
    ax.set_xticklabels(ax.get_xticks(), rotation = 45,)
    ax.yaxis.tick_right()

    ax.set_yticks(range(1,8))
    variable = list(composite_ds.data_vars)[0].split('_')[-1]
    n = 1
    if locs==None:
        locs = composite_ds.attrs['locations']

    for loc in locs:
        ax.scatter((composite_ds[loc+'_'+variable].strong_years),
                [n for i in range(len(composite_ds[loc+'_'+variable].strong_years))],color='deeppink', **scatter_kwargs)
        ax.scatter((composite_ds[loc+'_'+variable].weak_years),
                [n for i in range(len(composite_ds[loc+'_'+variable].weak_years))], color='slateblue', **scatter_kwargs)

        n=n+1
    if locs[4] == 'BADOE':
        locs[4] = 'BAODE'

    ax.set_yticklabels(locs)
    ax.set_ylim(0,9)
    ax.tick_params(axis='x', which='major', labelsize=xlabelsize)
    ax.legend(['Strong years','Weak Years'],mode='expand', ncol=2, frameon=False)


text:
import numpy as np
import scipy.sparse as sps

from ad.forward_mode import Ad_array, initAdArrays

def concatenate(variables, axis=0):
    vals = [var.val for var in variables]
    jacs = np.array([var.jac for var in variables])

    vals_stacked = np.concatenate(vals, axis=axis)
    jacs_stacked = []
    jacs_stacked = sps.vstack(jacs)
    #    for i in range(jacs.shape[1]):
    #        jacs_stacked.append(sps.vstack(jacs[:, i]))

    return Ad_array(vals_stacked, jacs_stacked)

ext: py

reason: import numpy,import scipy


text:
import os
import sys
import time
import numpy as np

from asap3.Tools.MaterialProperties import MaterialPropertiesData
from asap3.Tools.ParameterOptimization import ParameterPerformance
from asap3.Tools.ParameterOptimization.Optimization import ParameterOptimization
from asap3.Tools.ParameterOptimization.SearchParallel import ParameterSearch
from asap3.Tools.ParameterOptimization.EMT import EMT2011Fit, EMTStdParameters
from asap3.Tools.ParameterOptimization.GetParameters import get_parameters

paramdirs = sys.argv[1:]

#paramfile = sys.argv[1]

# Name of property in code, structure, name in data file, weight.
temp_metal_prop = [('lattice_constant_a', 'fcc', 'a', 0.001),
                   ('bulk_modulus', 'fcc', 'B', 0.01),
                   ('elastic_anisotropy', 'fcc', 'A', 0.05),
                   #('elastic_constant_C11', 'fcc', 'C11', 0.03),
                   #('elastic_constant_C12', 'fcc', 'C12', 0.03),
                   ('elastic_constant_C44', 'fcc', 'C44', 0.01),
                   ('cohesive_energy', 'fcc', 'Ecoh', 0.001),
                   ('surface_energy', 'fcc111', 'E111', 0.02),
                   #('surface_energy', 'fcc100', 'E100', 0.02),
                   ('surface_ratio', 'fcc111-fcc100', 'E111_100', 0.01),
                   ('stacking_fault', 'fcc', 'Esf', 0.02)
                   ]

# Name of property in code, structure, name in data file, weight, symmetric
# symmetric=True means that the alloy (A,B) = (B,A) and only the alphabetically
# ordered one should be used.
temp_alloy_prop = [('lattice_constant_a', 'l12', 'a', 0.01, False),
                   ('bulk_modulus', 'l12', 'B', 0.01, False),
                   ('heat_of_formation', 'l12', 'Eheat', 0.01, False),
                   ('lattice_constant_a', 'l10', 'a', 0.01, True),
                   ('lattice_ratio_ca', 'l10', 'c/a', 0.01, True),
                   ('heat_of_formation', 'l10', 'Eheat', 0.01, True),
                   ]

mp = MaterialPropertiesData(['properties_metals.dat', 'properties_alloys.dat'])

paramfiles = []
for dir in paramdirs:
    for line in open(os.path.join(dir, 'fit.dat')):
        words = line.split()
        if words and words[0] == "0":
            paramfiles.append(os.path.join(dir, "fit-"+words[1]+".dat"))
            break

quantities = []
latticeconst = []

metals = []
initparam = {}
errfuncs = {}

for paramfile in paramfiles:
    param, errf = get_parameters(paramfile, 2)
    for k,v in param.iteritems():
        if k not in errfuncs or errf < errfuncs[k]:
            initparam[k] = v
            errfuncs[k] = errf
            print "Adding from %s: %s" % (paramfile, str(k))

for m1, m2 in initparam.keys():
    assert m1 == m2
    metals.append(m1)
metals.sort()
print metals

walloy = 1   # Without any effect.

for m1 in metals:
    latticeconst.append(('fcc', m1, mp.get(m1, 'a')))
    for name, struct, id, weight in temp_metal_prop:
        if id == 'E111_100':
            value = mp.get(m1, 'E111') / mp.get(m1, 'E100')
        else:
            value = mp.get(m1, id)
        quantities.append((name, struct, m1, value, weight))

    for m2 in metals:
        if m1 == m2:
            continue
        for name, struct, id, weight, symmetric in temp_alloy_prop:
            if symmetric and m1 > m2:
                continue
            value1 = mp.get((m1, m2), id, struct)
            args = (name, struct, (m1, m2), value1, weight * walloy)
            quantities.append(args)
            if name == 'lattice_constant_a' and struct == 'l12':
                a = mp.get((m1, m2), 'a', struct)
                latticeconst.append(('l12', (m1, m2), a))
            if name == 'lattice_constant_a' and struct == 'l10':
                a = mp.get((m1, m2), 'a', struct)
                c = mp.get((m1, m2), 'c', struct)
                latticeconst.append(('l10', (m1, m2), (a, c)))

#print latticeconst
#print quantities

calculator = EMT2011Fit(metals, initparam, 'delta')
ParameterPerformance(calculator, quantities, latticeconst, debug=False)

reason: import numpy


text:
import numpy as np
import cv2
import os
import glob
import ntpath
import matplotlib.pyplot as plt

class ParamsStruct():
    def __init__(self, count=0, window_name="", img=None, points=[], draw_help_lines=True):
        self.count = count
        self.window_name = window_name
        self.img = img
        self.points = points
        self.draw_help_lines = draw_help_lines

def mouseCB(event, x, y, flags, param):
    '''
    Function which is called when event inside window happens
    '''
    if event == cv2.EVENT_LBUTTONDBLCLK or event == cv2.EVENT_LBUTTONDOWN:
        cv2.circle(param.img, (x, y), 3, (0, 0, 255), 3)  # BGR
        if param.count == 0 and param.draw_help_lines == True:
            cv2.line(param.img, (x, y), (param.img.shape[1], y), (0, 0, 255), 3)
        if param.count == 2 and param.draw_help_lines == True:
            cv2.line(param.img, (0, y), (x, y), (0, 0, 255), 3)
        param.count += 1
        param.points.append([x, y])
        cv2.imshow(param.window_name, param.img)

def choose_points(img_input, draw_help_lines=True):
    """
    Choose four input points for calculation matrix
    for perspective transformation and then press 'q'
    for quit
    1. Choose upper left points
    2. Choose upper right point
    3. Choose bottom right point
    4. Choose bottom left point
    Important observations: choose bottom points
    as much as possible close to the image bottom
    """

    copy_img = img_input.copy()
    window_name = "Choose source points"
    points = []
    param = ParamsStruct(0, window_name, copy_img, points, draw_help_lines)
    cv2.namedWindow(window_name)
    cv2.imshow(window_name, copy_img)
    cv2.setMouseCallback(window_name, mouseCB, param)

    while True:
        ip = cv2.waitKey(0) & 0xFF
        if ip == ord('q'):
            break

    cv2.destroyAllWindows()
    return param.points

def align_manually_chosen_points(points, offset_top=0, offset_bottom=0):
    '''
    Correct chosen source point so they will form trapezium
    '''
    # We suppose that lines are parallel
    points_list = []
    dist = points[0][0] - points[3][0]
    # upper left point
    x = points[0][0] - offset_top
    y = points[0][1]
    points_list.append([x, y])
    # upper right point
    x = points[1][0] + offset_top
    y = points[0][1]  # on the same level as upper left point
    points_list.append([x, y])
    # bottom right point
    x = points[1][0] + dist + offset_bottom
    y = points[2][1]
    points_list.append([x, y])
    # bottom left point
    x = points[3][0] - offset_bottom
    y = points[2][1]
    points_list.append([x, y])

    src = np.float32(points_list)
    return src, points_list

def roi(img, vertices):
    '''
    Set to zero all pixels which are not in ROI
    '''
    # blank mask
    mask = np.zeros_like(img)
    # filling pixels inside the polygon defined by vertices with the fill color
    cv2.fillPoly(mask, vertices, 255)
    # returning the image with all pixels
    masked_background = cv2.bitwise_or(img, mask)
    return masked_background

def plot_mask_on_image(src_img, points_list):
    '''
    Set to zero all pixels which are not in ROI
    '''
    vertices = np.array([points_list], dtype=np.int32)
    masked_img = roi(src_img, vertices)
    return masked_img

def set_destination_points(input_img, offset_x=300, offset_y=0):
    '''
    Choose destination source points for perspective transformation
    '''
    [height, width, z] = input_img.shape
    dst = np.float32(
        [[offset_x, offset_y], [width - offset_x, offset_y], [width - offset_x, height - offset_y],
         [offset_x, height - offset_y]])
    return dst

def region_of_interest(img, vertices, inside=False):
    """
    Applies an image mask.

    Only keeps the region of the image defined by the polygon
    formed from `vertices`. The rest of the image is set to black.
    `vertices` should be a numpy array of integer points.
    """
    # defining a blank mask to start with
    mask = np.zeros_like(img)

    # defining a 3 channel or 1 channel color to fill the mask with depending on the input image
    if len(img.shape) > 2:
        channel_count = img.shape[2]  # i.e. 3 or 4 depending on your image
        ignore_mask_color = (255,) * channel_count
    else:
        ignore_mask_color = 255

    # filling pixels inside the polygon defined by "vertices" with the fill color
    cv2.fillPoly(mask, vertices, ignore_mask_color)

    # returning the image only where mask pixels are nonzero
    if inside == False:
        masked_image = cv2.bitwise_and(img, mask)
    else:
        masked_image = cv2.bitwise_and(img, ~mask)
    return masked_image

def mask_image(input_img, inside):
    masked_img = np.copy(input_img)
    mask_points = choose_points(input_img, draw_help_lines=False)
    vertices_mask = np.array([mask_points], dtype=np.int32)
    masked_img = region_of_interest(input_img, vertices_mask, inside)
    return masked_img

def calculate_perpective_matrix(input_img):
    '''
    Pipeline for perspective transformation step
    '''
    points = choose_points(input_img)
    src, points_list = align_manually_chosen_points(points)
    result = plot_mask_on_image(input_img, points_list)
    dst = set_destination_points(input_img)
    M = cv2.getPerspectiveTransform(src, dst)
    Minv = cv2.getPerspectiveTransform(dst, src)
    return M, Minv, result

def visualize(img_ref, warped):
    # Visualize
    fig, ((ax1, ax2)) = plt.subplots(ncols=2, nrows=1, figsize=(20, 10))
    ax1.set_title('Original image')
    ax1.imshow(img_ref)

    img_ref_with_line = np.copy(warped)
    lineThickness = 2
    x = 50
    cv2.line(img_ref_with_line, (x, 0), (x, img_ref.shape[0]), (0, 255, 0), lineThickness)

    ax2.set_title('Transformed image')
    ax2.imshow(img_ref_with_line)

    plt.show()

    return

def save_binary_image(img, M, Minv, name_dir, img_name):
    directory = '../test_images/' + name_dir
    if not os.path.exists(directory):
        os.makedirs(directory)
    img_name_no_ext = os.path.splitext(img_name)[0]
    directory_img = directory + '/' + img_name_no_ext + '.npy'
    directory_M = directory + '/M_' + img_name_no_ext + '.npy'
    directory_M_inv = directory + '/Minv_' + img_name_no_ext + '.npy'
    np.save(directory_img, img)
    np.save(directory_M, M)
    np.save(directory_M_inv, Minv)

def load_and_visu_stored_arrays():
    arrays_names = glob.glob('../test_images/warped/test*.npy')
    for fname in arrays_names:
        img = np.load(fname)
        plt.imshow(img)
        plt.show()

def main():
    show_results = False
    if show_results == False:
        images_names = glob.glob('../test_images/combined_binary/test*.jpg')
        destination_dir_name = "warped"
        for fname in images_names:
            img = cv2.imread(fname)
            M, Minv, img_with_marked_area = calculate_perpective_matrix(img)
            masked_img = mask_image(img, inside=False)  # mask outside regions
            masked_img_inside = mask_image(masked_img, inside=True)  # mask inner region
            img_size = (masked_img_inside.shape[1], masked_img_inside.shape[0])
            warped = cv2.warpPerspective(masked_img_inside, M, img_size, flags=cv2.INTER_LINEAR)
            save_binary_image(warped, M, Minv, destination_dir_name, ntpath.basename(fname))
            visualize(img_with_marked_area, warped)
    else:
        load_and_visu_stored_arrays()

if __name__ == '__main__':
    main()

max_stars_repo_name: anastasia-spb/CarND-Advanced-Lane-Lines

max_issues_repo_name: anastasia-spb/CarND-Advanced-Lane-Lines

max_forks_repo_name: anastasia-spb/CarND-Advanced-Lane-Lines


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

# Form implementation generated from reading ui file 'mainwindow.ui'
#
# Created by: PyQt5 UI code generator 5.14.1
#
# WARNING! All changes made in this file will be lost!

from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5 import uic

import gmi_pathdialog

import sys, os
import numpy as np

import gmi_misc

def switch_path(pth):
    old_cwd = os.getcwd()
    try:
        os.chdir(pth)
    except:
        gmi_misc.error('CAN NOT OPEN WORKING DIRECTORY '+ pth + ', ABORTING...')
    return old_cwd

def switch_path_back(pth):
    os.chdir(pth)
    return

class MainWindow(QtWidgets.QMainWindow):
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)

        if sys.platform == 'darwin':
            uic.loadUi("gmi_mainwindow.ui", self)
        elif sys.platform == 'linux':
            uic.loadUi("gmi_mainwindow_linux.ui", self)
        else:
            uic.loadUi("gmi_mainwindow.ui", self)

        #opened path

        self.working_directory_opened = False
        self.GMI_PATH = ''

        #main window

        self.label_CurrentFolder = self.findChild(QtWidgets.QLabel, 'label_CurrentFolder')
        self.label_CurrentFolder.setText(self.GMI_PATH)

        self.pushButton_ChangeFolder = self.findChild(QtWidgets.QPushButton, 'pushButton_ChangeFolder')
        self.pushButton_ChangeFolder.clicked.connect(self.OpenWorkingDirectory)

        self.pushButton_Exit = self.findChild(QtWidgets.QPushButton, 'pushButton_Exit')
        self.pushButton_Exit.clicked.connect(self.close)

        #config editor tab
        self.textEdit_ConfigEditor = self.findChild(QtWidgets.QTextEdit, 'textEdit_ConfigEditor')
        self.textEdit_ConfigEditor.textChanged.connect(self.EnableConfigSave)

        self.pushButton_SaveConfig = self.findChild(QtWidgets.QPushButton, 'pushButton_SaveConfig')
        self.pushButton_SaveConfig.clicked.connect(self.SaveConfig)

        #plots
        self.zoomfactor = 0.95
        self.graphicsScene_PlotScene = QtWidgets.QGraphicsScene()
        self.graphicsView_PlotView = self.findChild(QtWidgets.QGraphicsView, 'graphicsView_PlotView')

        self.pushButton_Plot = self.findChild(QtWidgets.QPushButton, 'pushButton_Plot')
        self.pushButton_Plot.clicked.connect(self.plot)

        self.pushButton_Spectrum = self.findChild(QtWidgets.QPushButton, 'pushButton_Spectrum')
        self.pushButton_Spectrum.clicked.connect(self.spec)

        self.pushButton_ZoomIn = self.findChild(QtWidgets.QPushButton, 'pushButton_ZoomIn')
        self.pushButton_ZoomIn.clicked.connect(self.plot_zoomin)

        self.pushButton_ZoomOut = self.findChild(QtWidgets.QPushButton, 'pushButton_ZoomOut')
        self.pushButton_ZoomOut.clicked.connect(self.plot_zoomout)

        self.comboBox_GridList = self.findChild(QtWidgets.QComboBox, 'comboBox_GridList')
        self.comboBox_GridList.currentIndexChanged.connect(self.EnablePlotting)

        #stages
        self.checksums = False
        self.checkBox_InspectChecksums = self.findChild(QtWidgets.QCheckBox, 'checkBox_InspectChecksums')
        if self.checksums:
            self.checkBox_InspectChecksums.setCheckState(QtCore.Qt.Checked)
        else:
            self.checkBox_InspectChecksums.setCheckState(QtCore.Qt.Unchecked)
        self.checkBox_InspectChecksums.stateChanged.connect(self.InspectChecksums)

        self.stages_updated = False
        self.tabs = self.findChild(QtWidgets.QTabWidget, 'tabWidget_MainTabs')
        self.tabs.currentChanged.connect(self.UpdateStages)

        self.pushButton_CreateTesseroidModel = self.findChild(QtWidgets.QPushButton, 'pushButton_CreateTesseroidModel')
        self.pushButton_CreateTesseroidModel.clicked.connect(self.CreateTesseroidModel)
        self.label_IndicatorCreateTesseroidModel = self.findChild(QtWidgets.QLabel, 'label_IndicatorCreateTesseroidModel')

        self.pushButton_CalculateTesseroidsFields = self.findChild(QtWidgets.QPushButton, 'pushButton_CalculateTesseroidsFields')
        self.pushButton_CalculateTesseroidsFields.clicked.connect(self.CalculateTesseroidsFields)
        self.label_IndicatorCalculateTesseroidsFields = self.findChild(QtWidgets.QLabel, 'label_IndicatorCalculateTesseroidsFields')

        self.pushButton_CreateDesignMatrix = self.findChild(QtWidgets.QPushButton, 'pushButton_CreateDesignMatrix')
        self.pushButton_CreateDesignMatrix.clicked.connect(self.CreateDesignMatrix)
        self.label_IndicatorCreateDesignMatrix = self.findChild(QtWidgets.QLabel, 'label_IndicatorCreateDesignMatrix')

        self.pushButton_ExecuteAll = self.findChild(QtWidgets.QPushButton, 'pushButton_ExecuteAll')
        self.pushButton_ExecuteAll.clicked.connect(self.ExecuteAll)

        #inversion
        self.pushButton_Invert = self.findChild(QtWidgets.QPushButton, 'pushButton_Invert')
        self.pushButton_Invert.clicked.connect(self.Invert)

        self.listWidget_ResultsList = self.findChild(QtWidgets.QListWidget, 'listWidget_ResultsList')
        self.listWidget_ResultsList.currentItemChanged.connect(self.EnableResultPlotting)

        self.graphicsScene_ResultPlotScene = QtWidgets.QGraphicsScene()
        self.graphicsView_ResultPlotView = self.findChild(QtWidgets.QGraphicsView, 'graphicsView_ResultPlotView')

        if not self.working_directory_opened:
            self.OpenWorkingDirectory()

    def EnableResultPlotting(self):
        import gmi_config
        import gmi_gmt
        import os
        old_cwd = switch_path(self.GMI_PATH)

        config = gmi_config.read_config()
        try:
            current_opt = str(self.listWidget_ResultsList.currentItem().text())
        except:
            current_opt = '_none_'
            gmi_misc.debug('bug to fix with restarting inversion win new paramters')

        self.listWidget_ResultsList.repaint()

        print (current_opt)

        if(os.path.exists(current_opt)):
            gmi_misc.info(str(current_opt) + ' is selected for plotting')
            #self.pushButton_Plotresult.setEnabled(True)
            self.graphicsView_ResultPlotView.setEnabled(True)
            #self.pushButton_Plotresult.repaint()
        else:
            #self.pushButton_Plotresult.setDisabled(True)
            self.graphicsView_ResultPlotView.setDisabled(True)
            #self.pushButton_Plotresult.repaint()

        switch_path_back(old_cwd)

        self.PlotResult()

    def PlotResult(self):
        import gmi_config
        import gmi_gmt
        old_cwd = switch_path(self.GMI_PATH)

        self.graphicsScene_ResultPlotScene.clear() #new thing

        config = gmi_config.read_config()

        output_folder = gmi_misc.init_result_folder()

        fname = str(self.listWidget_ResultsList.currentItem().text())

        indrem = 3

        if '.dat' in fname:
            current_plot = output_folder + '/' + fname

            dat = np.loadtxt(current_plot)

            import matplotlib.pyplot as plt

            fig, ax = plt.subplots()
            ax.plot(dat[:, 0], dat[:, 1])

            ax.set(xlabel='i', ylabel='val',
                   title='.dat file plot')
            ax.grid()

            fig.savefig("temp.png")
            #plt.show()

            plt.clf()
            plt.close()

            indrem = 3

        elif '.spec' in fname:
            current_plot = output_folder + '/' + fname

            dat = np.loadtxt(current_plot)

            import matplotlib.pyplot as plt

            plt.plot(dat[1:, 0], np.log10(dat[:, 1])[1:], '-', lw=0.6)

            a_yticks = np.array([1, 0.1, 0.01, 0.001, 0.0001])
            plt.yticks(np.log10(a_yticks), a_yticks.astype(str))

            a_xticks = np.append(np.array([1]),  np.arange(10, np.ndarray.max(dat[:, 0]), 10))
            a_xticks = np.append(a_xticks, np.array([int(config.get('Spherical Harmonics', 'N_MIN_CUTOFF'))]))
            plt.xticks(a_xticks, a_xticks.astype(str))

            plt.title(current_plot + ' power spectrum')
            plt.xlabel('SH degree')
            plt.ylabel('Power [SI^2]')
            plt.grid()

            plt.savefig('temp.png')

            plt.clf()
            plt.close()

            indrem = 4

        elif '.sht_shcoeff' in fname:
            import pyshtools
            import matplotlib.pyplot as plt
            urrent_plot = output_folder + '/' + fname

            sht_shcoeff = pyshtools.SHCoeffs.from_file(fname)
            sht_shcoeff.plot_spectrum2d()

            plt.savefig('temp.png')

            plt.clf()
            plt.close()

            indrem = 11

        else:
            current_plot = output_folder + '/' + fname
            surf = False

            if 'grid' in fname:

                pname = fname
                colorsch = 'polar'
                uname = 'Magnetic Field'
                units = 'nT'
                min = -12
                max = 12

                grid = gmi_misc.read_surf_grid(current_plot)
            else:

                pname = fname
                colorsch = 'haxby'
                uname = 'Susceptibility'
                units = 'SI'
                min = 0
                max = 0.1

                grid = gmi_misc.read_sus_grid(current_plot)

            indrem = 3

            if gmi_config.T_DO_TILES:
                type_p = 2
            else:
                type_p = 1

            gmi_gmt.plot_global_grid(grid, surf, pname, min, max, colorsch, uname, units, type_p)

        result_pixmap =  QtGui.QPixmap('temp.png')
        self.graphicsScene_ResultPlotScene.addPixmap(result_pixmap.scaledToHeight(self.graphicsView_ResultPlotView.geometry().height()*0.95))

        self.graphicsView_ResultPlotView.setScene(self.graphicsScene_ResultPlotScene)
        self.graphicsView_ResultPlotView.show()

        import shutil
        shutil.copyfile('temp.png', './' + output_folder + '/' + self.listWidget_ResultsList.currentItem().text()[:-indrem]+ 'png')

        switch_path_back(old_cwd)

    def InspectChecksums(self):
        if self.checkBox_InspectChecksums.isChecked():
            self.checksums = True

            self.stages_updated = False
            self.UpdateStages()
        else:
            self.checksums = False

            self.stages_updated = False
            self.UpdateStages()

    def UpdateStages(self):
        if self.tabs.currentWidget().objectName() == 'tab_stages' and self.stages_updated == False:

            pixmap = QtGui.QPixmap('icons/icons8-process-120.png')
            self.label_IndicatorCreateTesseroidModel.setPixmap(pixmap)
            self.label_IndicatorCreateTesseroidModel.repaint()
            self.label_IndicatorCalculateTesseroidsFields.setPixmap(pixmap)
            self.label_IndicatorCalculateTesseroidsFields.repaint()
            self.label_IndicatorCreateDesignMatrix.setPixmap(pixmap)
            self.label_IndicatorCreateDesignMatrix.repaint()

            import gmi_hash_test
            stages = [0, 0, 0]
            stages, dict = gmi_hash_test.main(self.GMI_PATH, tc=self.checksums)

            pixmap_checked =  QtGui.QPixmap('icons/icons8-checkmark-48.png')
            pixmap_doublechecked =  QtGui.QPixmap('icons/icons8-double-tick-48.png')
            pixmap_unchecked =  QtGui.QPixmap('icons/icons8-delete-48.png')

            self.pushButton_CreateTesseroidModel.setEnabled(True)
            self.pushButton_CalculateTesseroidsFields.setEnabled(False)
            self.pushButton_CreateDesignMatrix.setEnabled(False)

            if stages[0] > 0:
                if self.checksums:
                    self.label_IndicatorCreateTesseroidModel.setPixmap(pixmap_doublechecked)
                else:
                    self.label_IndicatorCreateTesseroidModel.setPixmap(pixmap_checked)
                self.pushButton_CalculateTesseroidsFields.setEnabled(True)
                #self.pushButton_CreateDesignMatrix.setEnabled(False)
                #self.label_stage1.setText('correct checksum')
            elif stages[0] == 0:
                self.label_IndicatorCreateTesseroidModel.setPixmap(pixmap_unchecked)
                self.pushButton_CalculateTesseroidsFields.setEnabled(False)
                #self.label_stage1.setText('no checksum')
            else:
                self.label_IndicatorCreateTesseroidModel.setPixmap(pixmap_unchecked)
                self.pushButton_CalculateTesseroidsFields.setEnabled(False)
                #self.label_stage1.setText('checksum in dictionary\ndoes not match')
            self.label_IndicatorCreateTesseroidModel.repaint()

            if stages[1] > 0:
                if self.checksums:
                    self.label_IndicatorCalculateTesseroidsFields.setPixmap(pixmap_doublechecked)
                else:
                    self.label_IndicatorCalculateTesseroidsFields.setPixmap(pixmap_checked)
                self.pushButton_CreateDesignMatrix.setEnabled(True)
                #self.label_stage2.setText('correct checksum')
            elif stages[0] == 0:
                self.label_IndicatorCalculateTesseroidsFields.setPixmap(pixmap_unchecked)
                self.pushButton_CreateDesignMatrix.setEnabled(False)
                #self.label_stage2.setText('no checksum')
            else:
                self.label_IndicatorCalculateTesseroidsFields.setPixmap(pixmap_unchecked)
                self.pushButton_CreateDesignMatrix.setEnabled(False)
                #self.label_stage2.setText('checksum in dictionary\ndoes not match')
            self.label_IndicatorCalculateTesseroidsFields.repaint()

            if stages[2] > 0:
                if self.checksums:
                    self.label_IndicatorCreateDesignMatrix.setPixmap(pixmap_doublechecked)
                else:
                    self.label_IndicatorCreateDesignMatrix.setPixmap(pixmap_checked)
                #self.label_stage3.setText('correct checksum')
            elif stages[0] == 0:
                self.label_IndicatorCreateDesignMatrix.setPixmap(pixmap_unchecked)
                #self.label_stage3.setText('no checksum')
            else:
                self.label_IndicatorCreateDesignMatrix.setPixmap(pixmap_unchecked)
                #self.label_stage3.setText('checksum in dictionary\ndoes not match')
            self.label_IndicatorCreateDesignMatrix.repaint()

            self.stages_updated = True

    def CreateTesseroidModel(self):
        pixmap = QtGui.QPixmap('icons/icons8-process-120.png')
        self.label_IndicatorCreateTesseroidModel.setPixmap(pixmap)
        self.label_IndicatorCreateTesseroidModel.repaint()

        import gmi_create_tesseroid_model
        old_cwd = switch_path(self.GMI_PATH)

        '''
        self.oldstdout = sys.stdout

        from io import StringIO
        sys.stdout = StringIO()
        # Do processing stages
        # And later
        '''

        print('stage1')
        gmi_create_tesseroid_model.main(self.GMI_PATH)
        '''
        self.console.setText( sys.stdout.getvalue() )
        sys.stdout = self.oldstdout
        '''
        switch_path_back(old_cwd)

        self.stages_updated = False
        self.UpdateStages()

        QtWidgets.QMessageBox.information(self, "Message", "Tesseroid model created!")

    def CalculateTesseroidsFields(self):
        pixmap = QtGui.QPixmap('icons/icons8-process-120.png')
        self.label_IndicatorCalculateTesseroidsFields.setPixmap(pixmap)
        self.label_IndicatorCalculateTesseroidsFields.repaint()

        import gmi_calculate_effect_of_each_tesseroid
        old_cwd = switch_path(self.GMI_PATH)

        '''
        self.oldstdout = sys.stdout
        from io import StringIO
        sys.stdout = StringIO()
        # Do processing stages
        # And later
        '''

        print('stage2')
        gmi_calculate_effect_of_each_tesseroid.main(self.GMI_PATH)
        '''
        self.console.setText( sys.stdout.getvalue() )
        sys.stdout = self.oldstdout
        '''
        switch_path_back(old_cwd)

        self.stages_updated = False
        self.UpdateStages()

        QtWidgets.QMessageBox.information(self, "Message", "Tesseroids' fields calculated!")

    def CreateDesignMatrix(self):
        pixmap = QtGui.QPixmap('icons/icons8-process-120.png')
        self.label_IndicatorCalculateTesseroidsFields.setPixmap(pixmap)
        self.label_IndicatorCalculateTesseroidsFields.repaint()

        import gmi_create_design_matrix
        old_cwd = switch_path(self.GMI_PATH)

        print('stage3')
        gmi_create_design_matrix.main(self.GMI_PATH)

        switch_path_back(old_cwd)

        self.stages_updated = False
        self.UpdateStages()

        QtWidgets.QMessageBox.information(self, "Message", "Design matrices created")

    def ExecuteAll(self):
        self.CreateTesseroidModel()
        self.CalculateTesseroidsFields()
        self.CreateDesignMatrix()

    #inversion

    def InitResultsList(self):
        self.graphicsScene_ResultPlotScene.clear()
        self.listWidget_ResultsList.clear()

        old_cwd = switch_path(self.GMI_PATH)

        import glob
        output_folder = gmi_misc.init_result_folder()

        os.chdir(output_folder)
        file_list = glob.glob("*.xyz")
        file_list = file_list + glob.glob("*.dat") + glob.glob('*.spec') + glob.glob('*.sht_shcoeff')
        os.chdir('..')

        self.listWidget_ResultsList.clear()

        self.listWidget_ResultsList.setEnabled(True)
        for filename in file_list:
            self.listWidget_ResultsList.addItem(filename)

        switch_path_back(old_cwd)

    def Invert(self):
        self.graphicsScene_ResultPlotScene.clear()
        self.listWidget_ResultsList.clear()
        self.listWidget_ResultsList.setDisabled(True)

        import gmi_invert
        old_cwd = switch_path(self.GMI_PATH)

        print('stage4')
        gmi_invert.main(self.GMI_PATH)

        self.InitResultsList()

        switch_path_back(old_cwd)

    def SetPath(self, wpath):
        self.GMI_PATH = wpath
        self.label_CurrentFolder.setText(self.GMI_PATH)
        gmi_misc.info(self.GMI_PATH + ' is set as a working directory')

    def OpenWorkingDirectory(self):
        if self.working_directory_opened:
            self.SaveConfig()

        pathselection = gmi_pathdialog.PathDialog()
        if not pathselection.exec_(): # 'reject': user pressed 'Cancel', so quit
            if self.working_directory_opened:
                return
            else:
                exit()

        self.SetPath(pathselection.cfg.get('PATH', 'GMI_PATH'))

        self.ReadWorkingDirectory()
        self.graphicsScene_PlotScene.clear()

        self.stages_updated = False
        self.UpdateStages()

        self.working_directory_opened = True

    def ReadWorkingDirectory(self):
        import configparser
        import gmi_config

        old_cwd = switch_path(self.GMI_PATH)
        config = gmi_config.read_config()

        with open(self.GMI_PATH + '/input.txt', 'r') as config_file:
            buf = config_file.read()
            self.textEdit_ConfigEditor.setText(buf)

        self.comboBox_GridList.clear()
        self.comboBox_GridList.addItem('TOP_SURFACE')
        self.comboBox_GridList.addItem('BOT_SURFACE')
        self.comboBox_GridList.addItem('OBSERVED_DATA')
        self.comboBox_GridList.addItem('SUBTRACT_DATA')
        self.comboBox_GridList.addItem('INIT_SOLUTION')
        #self.comboBox_GridList.addItem(config[])

        switch_path_back(old_cwd)

        self.working_directory_opened = True

        self.InitResultsList()

    def EnableConfigSave(self):
        self.pushButton_SaveConfig.setText('Save')
        self.pushButton_SaveConfig.setEnabled(True)
        self.pushButton_SaveConfig.repaint()

    def SaveConfig(self):
        buf = self.textEdit_ConfigEditor.toPlainText()
        with open(self.GMI_PATH + '/input.txt', 'w') as config_file:
            config_file.write(buf)

        self.pushButton_SaveConfig.setText('Save')
        self.pushButton_SaveConfig.setEnabled(False)
        self.pushButton_SaveConfig.repaint()
        gmi_misc.info('input.txt saved')

    def EnablePlotting(self):
        import gmi_config
        import gmi_gmt
        import os
        old_cwd = switch_path(self.GMI_PATH)

        config = gmi_config.read_config()
        current_opt = str(self.comboBox_GridList.currentText())

        for sect in config.sections():
            if(config.has_option(sect, current_opt)):
                if(os.path.exists(config.get(sect, current_opt))):
                    gmi_misc.info(str(sect) + '.'+ str(current_opt) +' (' + config.get(sect, current_opt) + ') is selected for plotting')
                    self.pushButton_Plot.setEnabled(True)
                    self.pushButton_Spectrum.setEnabled(True)
                    self.pushButton_ZoomIn.setEnabled(True)
                    self.pushButton_ZoomOut.setEnabled(True)
                    self.graphicsView_PlotView.setEnabled(True)
                    self.pushButton_Plot.repaint()
                    self.pushButton_Spectrum.repaint()
                    break

                else:
                    gmi_misc.warning(str(sect) + '.'+ str(current_opt) +' (' + config.get(sect, current_opt) + ') IS EMPTY/DOES NOT EXIST!')
                    self.pushButton_Plot.setDisabled(True)
                    self.pushButton_Spectrum.setDisabled(True)
                    self.pushButton_ZoomIn.setDisabled(True)
                    self.pushButton_ZoomOut.setDisabled(True)
                    self.graphicsView_PlotView.setDisabled(True)
                    self.pushButton_Plot.repaint()
                    self.pushButton_Spectrum.repaint()
                    break

        switch_path_back(old_cwd)

    def spec(self):
        import gmi_config
        import gmi_gmt
        old_cwd = switch_path(self.GMI_PATH)

        self.graphicsScene_PlotScene.clear() #new thing

        config = gmi_config.read_config()

        plot_cutoff = False

        current_plot = str(self.comboBox_GridList.currentText())
        if current_plot == 'TOP_SURFACE':
            fname = config.get('Global Tesseroid Model', 'TOP_SURFACE');
            grid = gmi_misc.read_surf_grid(fname)
            norm = 'unnorm'
            units = 'kM'
            grid = grid / 1000.0

        elif current_plot == 'BOT_SURFACE':
            fname = config.get('Global Tesseroid Model', 'BOT_SURFACE');
            grid = gmi_misc.read_surf_grid(fname)
            norm = 'unnorm'
            units = 'kM'
            grid = grid / 1000.0

        elif current_plot == 'OBSERVED_DATA':
            fname = config.get('Inversion', 'OBSERVED_DATA');
            norm = 'schmidt'
            units = 'nT'
            plot_cutoff = True
            grid = gmi_misc.read_data_grid(fname)

        elif current_plot == 'SUBTRACT_DATA':
            fname = config.get('Inversion', 'SUBTRACT_DATA');
            norm = 'schmidt'
            units = 'nT'
            plot_cutoff = True
            grid = gmi_misc.read_data_grid(fname)

        elif current_plot == 'INIT_SOLUTION':
            x0_name = config.get('Inversion', 'INIT_SOLUTION');
            norm = 'unnorm'
            units = 'SI'
            grid = gmi_misc.read_sus_grid(x0_name)

        else:
            pass

        import matplotlib.pyplot as plt
        import matplotlib.patches as mpatches
        import pyshtools

        grid_sht = pyshtools.SHGrid.from_array(grid)
        shc_sht = grid_sht.expand(normalization=norm)
        spectrum = shc_sht.spectrum()

        deg = shc_sht.degrees()
        print(deg)

        #plotting

        plt.plot(deg[1:], np.log10(spectrum)[1:], '-', lw=0.6)

        a_yticks = np.array([1, 0.1, 0.01, 0.001, 0.0001])
        plt.yticks(np.log10(a_yticks), a_yticks.astype(str))

        a_xticks = np.append(np.array([1]), np.arange(10, max(deg), 10))
        if plot_cutoff:
            a_xticks = np.append(a_xticks, np.array([int(config.get('Spherical Harmonics', 'N_MIN_CUTOFF'))]))

        plt.xticks(a_xticks, a_xticks.astype(str))

        plt.title(current_plot + ' power spectrum')
        plt.xlabel('SH degree')
        plt.ylabel('Power [' + units +'^2]')
        plt.grid()

        plt.savefig('temp.png')

        plt.clf()
        plt.close()

        plot_pixmap =  QtGui.QPixmap('temp.png')
        self.graphicsScene_PlotScene.addPixmap(plot_pixmap.scaledToHeight(self.graphicsView_PlotView.geometry().height()*0.95))
        #self.graphicsScene_PlotScene.setSceneRect(self.graphicsView_PlotView.geometry().x(), self.graphicsView_PlotView.geometry().y(), self.graphicsView_PlotView.geometry().width(), self.graphicsView_PlotView.geometry().height())

        self.graphicsView_PlotView.setScene(self.graphicsScene_PlotScene)
        self.graphicsView_PlotView.show()

        output_folder = gmi_misc.init_result_folder()

        import shutil
        shutil.copyfile('temp.png', './' + output_folder + '/' + current_plot+ '_spec.png')

        switch_path_back(old_cwd)

    def plot(self):
        import gmi_config
        import gmi_gmt
        old_cwd = switch_path(self.GMI_PATH)

        self.graphicsScene_PlotScene.clear() #new thing

        config = gmi_config.read_config()

        fname = ''
        pname = ''
        uname = ''
        units = ''
        colorsch = ''
        min = 0
        max = 0
        surf = False
        current_plot = str(self.comboBox_GridList.currentText())
        if current_plot == 'TOP_SURFACE':
            fname = config.get('Global Tesseroid Model', 'TOP_SURFACE');
            pname = 'Top Surface'
            colorsch = 'haxby'
            uname = 'Depth'
            units = 'kM'
            min = -10
            max = 15
            surf = True

            grid = gmi_misc.read_surf_grid(fname)
            grid = grid / 1000.0

        elif current_plot == 'BOT_SURFACE':
            fname = config.get('Global Tesseroid Model', 'BOT_SURFACE');

            pname = 'Top Surface'
            colorsch = 'haxby'
            uname = 'Depth'
            units = 'kM'
            min = -70
            max = -5
            surf = True

            grid = gmi_misc.read_surf_grid(fname)
            grid = grid / 1000.0

        elif current_plot == 'OBSERVED_DATA':
            fname = config.get('Inversion', 'OBSERVED_DATA');
            pname = 'Observed Field'
            colorsch = 'polar'
            uname = 'Magnetic field'
            units = 'nT'
            min = -12
            max = 12
            surf = False

            grid = gmi_misc.read_surf_grid(fname)

        elif current_plot == 'SUBTRACT_DATA':
            fname = config.get('Inversion', 'SUBTRACT_DATA');

            pname = 'Field to be Removed from Observed'
            colorsch = 'polar'
            uname = 'Magnetic field'
            units = 'nT'
            min = -12
            max = 12
            surf = False

            grid = gmi_misc.read_surf_grid(fname)
        elif current_plot == 'INIT_SOLUTION':
            x0_name = config.get('Inversion', 'INIT_SOLUTION');
            pname = 'Initial solution'
            colorsch = 'haxby'
            uname = 'Susceptibility'
            units = 'SI'
            min = 0
            max = 0.1

            grid = gmi_misc.read_sus_grid(x0_name)
            #if float(config.get('Inversion', 'MULTIPLICATOR')) != 1.0:
            #    max = max * float(config.get('Inversion', 'MULTIPLICATOR'))
             #   #units = 'SI'.format(float(config.get('Inversion', 'MULTIPLICATOR')))

        else:
            pass

        gmi_gmt.plot_global_grid(grid, surf, pname, min, max, colorsch, uname, units, 1)

        plot_pixmap =  QtGui.QPixmap('temp.png')
        self.graphicsScene_PlotScene.addPixmap(plot_pixmap.scaledToHeight(self.graphicsView_PlotView.geometry().height()*self.zoomfactor))
        #self.graphicsScene_PlotScene.setSceneRect(self.graphicsView_PlotView.geometry().x(), self.graphicsView_PlotView.geometry().y(), self.graphicsView_PlotView.geometry().width(), self.graphicsView_PlotView.geometry().height())

        self.graphicsView_PlotView.setScene(self.graphicsScene_PlotScene)
        self.graphicsView_PlotView.show()

        output_folder = gmi_misc.init_result_folder()

        import shutil
        shutil.copyfile('temp.png', './' + output_folder + '/' + current_plot+ '.png')

        switch_path_back(old_cwd)

    def plot_zoomin(self):
        old_cwd = switch_path(self.GMI_PATH)

        self.graphicsScene_PlotScene.clear() #new thing

        plot_pixmap =  QtGui.QPixmap('temp.png')
        self.zoomfactor += 0.1
        self.graphicsScene_PlotScene.addPixmap(plot_pixmap.scaledToHeight(self.graphicsView_PlotView.geometry().height()*self.zoomfactor))

        self.graphicsView_PlotView.setScene(self.graphicsScene_PlotScene)
        self.graphicsView_PlotView.show()

        switch_path_back(old_cwd)

    def plot_zoomout(self):
        old_cwd = switch_path(self.GMI_PATH)

        self.graphicsScene_PlotScene.clear() #new thing

        plot_pixmap =  QtGui.QPixmap('temp.png')
        self.zoomfactor -= 0.1
        self.graphicsScene_PlotScene.addPixmap(plot_pixmap.scaledToHeight(self.graphicsView_PlotView.geometry().height()*self.zoomfactor))

        self.graphicsView_PlotView.setScene(self.graphicsScene_PlotScene)
        self.graphicsView_PlotView.show()

        switch_path_back(old_cwd)

    def close(self):
        exit(0)

def main():
    app = QtWidgets.QApplication(sys.argv)

    pathselection = gmi_pathdialog.PathDialog()
    if not pathselection.exec_(): # 'reject': user pressed 'Cancel', so quit
        sys.exit(-1)

    window = MainWindow()
    window.setFixedSize(QtCore.QSize(window.width(), window.height()) )

    window.ReadWorkingDirectory()
    window.show()

    sys.exit(app.exec_())

if __name__ == '__main__':
    main()

max_stars_repo_name: eldarbaykiev/magtess-inversion-python

max_issues_repo_name: eldarbaykiev/magtess-inversion-python

max_forks_repo_name: eldarbaykiev/magtess-inversion-python


text:
"""A class that handles writing out matrices of a system to a C++ or Java file.
"""

import numpy as np
import os

class SystemWriter:
    def __init__(
        self,
        system,
        class_name,
        header_path_prefix,
        header_extension,
        period_variant=False,
    ):
        """Exports matrices to pair of C++ source files.

        Keyword arguments:
        system -- System object
        class_name -- subsystem class name in camel case
        header_path_prefix -- path prefix in which header exists
        header_extension -- file extension of header file
        period_variant -- True to use PeriodVariantLoop, False to use
                          StateSpaceLoop
        """
        self.system = system
        self.class_name = class_name
        self.header_path_prefix = header_path_prefix
        self.header_extension = header_extension
        template_cpp = (
            "<"
            + str(system.sysd.A.shape[0])
            + ", "
            + str(system.sysd.B.shape[1])
            + ", "
            + str(system.sysd.C.shape[0])
            + ">"
        )

        template_java = (
            "<N"
            + str(system.sysd.A.shape[0])
            + ", N"
            + str(system.sysd.B.shape[1])
            + ", N"
            + str(system.sysd.C.shape[0])
            + ">"
        )

        self.period_variant = period_variant
        if period_variant:
            self.class_type = "PeriodVariant"
            self.plant_coeffs_header = "PeriodVariantPlantCoeffs"
            self.obsv_coeffs_header = "PeriodVariantObserverCoeffs"
            self.loop_header = "PeriodVariantLoop"
        else:
            self.class_type = "StateSpace"
            self.plant_coeffs_header = "StateSpacePlantCoeffs"
            self.obsv_coeffs_header = "StateSpaceObserverCoeffs"
            self.loop_header = "StateSpaceLoop"

        self.ctrl_coeffs_header = "StateSpaceControllerCoeffs"
        self.ctrl_coeffs_type_cpp = "frc::" + self.ctrl_coeffs_header + template_cpp
        self.plant_coeffs_type_cpp = "frc::" + self.plant_coeffs_header + template_cpp
        self.obsv_coeffs_type_cpp = "frc::" + self.obsv_coeffs_header + template_cpp
        self.loop_type_cpp = "frc::" + self.loop_header + template_cpp

        self.ctrl_coeffs_type_java = self.ctrl_coeffs_header + template_java
        self.plant_coeffs_type_java = self.plant_coeffs_header + template_java
        self.obsv_coeffs_type_java = self.obsv_coeffs_header + template_java
        self.loop_type_java = self.loop_header + template_java

    def write_cpp_header(self):
        """Writes C++ header file."""
        prefix = "#include <frc/controller/"
        headers = []
        headers.append(prefix + self.plant_coeffs_header + ".h>")
        headers.append(prefix + self.ctrl_coeffs_header + ".h>")
        headers.append(prefix + self.obsv_coeffs_header + ".h>")
        headers.append(prefix + self.loop_header + ".h>")

        with open(
            self.class_name + "Coeffs." + self.header_extension, "w"
        ) as header_file:
            print("#pragma once" + os.linesep, file=header_file)
            for header in sorted(headers):
                print(header, file=header_file)
            header_file.write(os.linesep)
            self.__write_cpp_func_name(
                header_file, self.plant_coeffs_type_cpp, "PlantCoeffs", in_header=True
            )
            self.__write_cpp_func_name(
                header_file,
                self.ctrl_coeffs_type_cpp,
                "ControllerCoeffs",
                in_header=True,
            )
            self.__write_cpp_func_name(
                header_file, self.obsv_coeffs_type_cpp, "ObserverCoeffs", in_header=True
            )
            self.__write_cpp_func_name(
                header_file, self.loop_type_cpp, "Loop", in_header=True
            )

    def write_cpp_source(self):
        """Writes C++ source file."""
        if len(self.header_path_prefix) > 0 and self.header_path_prefix[-1] != os.sep:
            self.header_path_prefix += os.sep

        with open(self.class_name + "Coeffs.cpp", "w") as source_file:
            print(
                '#include "'
                + self.header_path_prefix
                + self.class_name
                + "Coeffs."
                + self.header_extension
                + '"'
                + os.linesep,
                file=source_file,
            )
            print("#include <Eigen/Core>" + os.linesep, file=source_file)

            # Write MakePlantCoeffs()
            self.__write_cpp_func_name(
                source_file, self.plant_coeffs_type_cpp, "PlantCoeffs", in_header=False
            )
            if self.period_variant:
                self.__write_cpp_matrix(source_file, self.system.sysc.A, "Acontinuous")
                self.__write_cpp_matrix(source_file, self.system.sysc.B, "Bcontinuous")
                self.__write_cpp_matrix(source_file, self.system.sysd.C, "C")
                self.__write_cpp_matrix(source_file, self.system.sysd.D, "D")
                print(
                    "  return "
                    + self.plant_coeffs_type_cpp
                    + "(Acontinuous, Bcontinuous, C, D);",
                    file=source_file,
                )
            else:
                self.__write_cpp_matrix(source_file, self.system.sysd.A, "A")
                self.__write_cpp_matrix(source_file, self.system.sysd.B, "B")
                self.__write_cpp_matrix(source_file, self.system.sysd.C, "C")
                self.__write_cpp_matrix(source_file, self.system.sysd.D, "D")
                print(
                    "  return " + self.plant_coeffs_type_cpp + "(A, B, C, D);",
                    file=source_file,
                )
            print("}" + os.linesep, file=source_file)

            # Write MakeControllerCoeffs()
            self.__write_cpp_func_name(
                source_file,
                self.ctrl_coeffs_type_cpp,
                "ControllerCoeffs",
                in_header=False,
            )
            self.__write_cpp_matrix(source_file, self.system.K, "K")
            self.__write_cpp_matrix(source_file, self.system.Kff, "Kff")
            self.__write_cpp_matrix(source_file, self.system.u_min, "Umin")
            self.__write_cpp_matrix(source_file, self.system.u_max, "Umax")
            print(
                "  return " + self.ctrl_coeffs_type_cpp + "(K, Kff, Umin, Umax);",
                file=source_file,
            )
            print("}" + os.linesep, file=source_file)

            # Write MakeObserverCoeffs()
            self.__write_cpp_func_name(
                source_file,
                self.obsv_coeffs_type_cpp,
                "ObserverCoeffs",
                in_header=False,
            )
            if self.period_variant:
                self.__write_cpp_matrix(source_file, self.system.Q, "Qcontinuous")
                self.__write_cpp_matrix(source_file, self.system.R, "Rcontinuous")
                self.__write_cpp_matrix(
                    source_file, self.system.P_steady, "PsteadyState"
                )

                first_line_prefix = "  return " + self.obsv_coeffs_type_cpp + "("
                space_prefix = " " * len(first_line_prefix)
                print(first_line_prefix + "Qcontinuous, Rcontinuous,", file=source_file)
                print(space_prefix + "PsteadyState);", file=source_file)
            else:
                self.__write_cpp_matrix(source_file, self.system.kalman_gain, "K")
                print(
                    "  return " + self.obsv_coeffs_type_cpp + "(K);", file=source_file
                )
            print("}" + os.linesep, file=source_file)

            # Write MakeLoop()
            self.__write_cpp_func_name(
                source_file, self.loop_type_cpp, "Loop", in_header=False
            )
            first_line_prefix = "  return " + self.loop_type_cpp + "("
            space_prefix = " " * len(first_line_prefix)
            print(
                first_line_prefix + "Make" + self.class_name + "PlantCoeffs(),",
                file=source_file,
            )
            print(
                space_prefix + "Make" + self.class_name + "ControllerCoeffs(),",
                file=source_file,
            )
            print(
                space_prefix + "Make" + self.class_name + "ObserverCoeffs());",
                file=source_file,
            )
            print("}", file=source_file)

    def write_java_source(self):
        wpilibj_prefix = "import edu.wpi.first.wpilibj."
        wpiutil_prefix = "import edu.wpi.first.wpiutil."
        imports = []

        # State space types
        imports.append(wpilibj_prefix + "controller." + self.plant_coeffs_header + ";")
        imports.append(wpilibj_prefix + "controller." + self.ctrl_coeffs_header + ";")
        imports.append(wpilibj_prefix + "controller." + self.obsv_coeffs_header + ";")
        imports.append(wpilibj_prefix + "controller." + self.loop_header + ";")

        # Number types and matrices
        imports.append(wpiutil_prefix + "math.numbers.*;")
        imports.append(wpiutil_prefix + "math.*;")

        with open(self.class_name + "Coeffs.java", "w") as source_file:
            for imp in sorted(imports):
                print(imp, file=source_file)

            source_file.write(os.linesep)

            print("public class " + self.class_name + "Coeffs {", file=source_file)

            states = "Nat.N" + str(self.system.sysd.A.shape[0]) + "()"
            inputs = "Nat.N" + str(self.system.sysd.B.shape[1]) + "()"
            outputs = "Nat.N" + str(self.system.sysd.C.shape[0]) + "()"
            # MakePlantCoeffs()
            self.__write_java_func_name(
                source_file, self.plant_coeffs_type_java, "PlantCoeffs"
            )
            if self.period_variant:
                self.__write_java_matrix(source_file, self.system.sysc.A, "Acontinuous")
                self.__write_java_matrix(source_file, self.system.sysc.B, "Bcontinuous")
                self.__write_java_matrix(source_file, self.system.sysd.C, "C")
                self.__write_java_matrix(source_file, self.system.sysd.D, "D")

                print(
                    "    return "
                    + self.plant_coeffs_type_java
                    + "("
                    + states
                    + ", "
                    + inputs
                    + ", "
                    + outputs
                    + ", Acontinuous, Bcontinuous, C, D);",
                    file=source_file,
                )
            else:
                self.__write_java_matrix(source_file, self.system.sysd.A, "A")
                self.__write_java_matrix(source_file, self.system.sysd.B, "B")
                self.__write_java_matrix(source_file, self.system.sysd.C, "C")
                self.__write_java_matrix(source_file, self.system.sysd.D, "D")

                print(
                    "    return new "
                    + self.plant_coeffs_type_java
                    + "("
                    + states
                    + ", "
                    + inputs
                    + ", "
                    + outputs
                    + ", A, B, C, D);",
                    file=source_file,
                )
            print("  }" + os.linesep, file=source_file)

            # MakeControllerCoeffs()
            self.__write_java_func_name(
                source_file, self.ctrl_coeffs_type_java, "ControllerCoeffs"
            )
            self.__write_java_matrix(source_file, self.system.K, "K")
            self.__write_java_matrix(source_file, self.system.Kff, "Kff")
            self.__write_java_matrix(source_file, self.system.u_min, "Umin")
            self.__write_java_matrix(source_file, self.system.u_max, "Umax")
            print(
                "    return new "
                + self.ctrl_coeffs_type_java
                + "(K, Kff, Umin, Umax);",
                file=source_file,
            )
            print("  }" + os.linesep, file=source_file)

            # MakeObserverCoeffs()
            self.__write_java_func_name(
                source_file, self.obsv_coeffs_type_java, "ObserverCoeffs"
            )
            if self.period_variant:
                self.__write_java_matrix(source_file, self.system.Q, "Qcontinuous")
                self.__write_java_matrix(source_file, self.system.R, "Rcontinuous")
                self.__write_java_matrix(
                    source_file, self.system.P_steady, "PsteadyState"
                )

                first_line_prefix = "    return new " + self.obsv_coeffs_type_java + "("
                space_prefix = " " * len(first_line_prefix)
                print(first_line_prefix + "Qcontinuous, Rcontinuous,", file=source_file)
                print(space_prefix + "PsteadyState);", file=source_file)
            else:
                self.__write_java_matrix(source_file, self.system.kalman_gain, "K")
                print(
                    "    return new " + self.obsv_coeffs_type_java + "(K);",
                    file=source_file,
                )
            print("  }" + os.linesep, file=source_file)

            # Write MakeLoop()
            self.__write_java_func_name(source_file, self.loop_type_java, "Loop")
            first_line_prefix = "    return new " + self.loop_type_java + "("
            space_prefix = " " * len(first_line_prefix)
            print(
                first_line_prefix + "make" + self.class_name + "PlantCoeffs(),",
                file=source_file,
            )
            print(
                space_prefix + "make" + self.class_name + "ControllerCoeffs(),",
                file=source_file,
            )
            print(
                space_prefix + "make" + self.class_name + "ObserverCoeffs());",
                file=source_file,
            )

            print("  }", file=source_file)
            print("}", file=source_file)

    def __write_cpp_func_name(self, cpp_file, return_type, object_suffix, in_header):
        """Writes either declaration or definition of C++ factory function.

        Keyword arguments:
        cpp_file -- file object to which to write name
        return_type -- string containing the return type
        object_suffix -- asdf
        in_header -- if True, print prototype instead of declaration
        """
        if in_header:
            func_suffix = ";"
        else:
            func_suffix = " {"
        func_name = "Make" + self.class_name + object_suffix + "()" + func_suffix
        if len(return_type + " " + func_name) > 80:
            print(return_type, file=cpp_file)
            print(func_name, file=cpp_file)
        else:
            print(return_type + " " + func_name, file=cpp_file)

    def __write_cpp_matrix(self, cpp_file, matrix, matrix_name):
        print(
            "  Eigen::Matrix<double, "
            + str(matrix.shape[0])
            + ", "
            + str(matrix.shape[1])
            + "> "
            + matrix_name
            + ";",
            file=cpp_file,
        )
        for row in range(matrix.shape[0]):
            for col in range(matrix.shape[1]):
                print(
                    "  "
                    + matrix_name
                    + "("
                    + str(row)
                    + ", "
                    + str(col)
                    + ") = "
                    + str(matrix[row, col])
                    + ";",
                    file=cpp_file,
                )

    def __write_java_func_name(self, java_file, return_type, object_suffix):
        func_name = "make" + self.class_name + object_suffix + "() {"
        if len("  public static " + return_type + " " + func_name) > 80:
            print("  public static " + return_type, file=java_file)
            print("    " + func_name, file=java_file)
        else:
            print("  public static " + return_type + " " + func_name, file=java_file)

    def __write_java_matrix(self, java_file, matrix, matrix_name):
        row_major_data = np.ravel(matrix)
        # Matrix<R, C> <name> = MatrixUtils.mat(R, C).fill(
        decl = (
            "    Matrix<N"
            + str(matrix.shape[0])
            + ", N"
            + str(matrix.shape[1])
            + "> "
            + matrix_name
            + " = MatrixUtils.mat(Nat.N"
            + str(matrix.shape[0])
            + "(), Nat.N"
            + str(matrix.shape[1])
            + "()).fill("
        )

        for (i, value) in enumerate(row_major_data):
            decl += str(value)
            if i != matrix.shape[0] * matrix.shape[1] - 1:
                decl += ", "

        decl += ");"

        print(decl, file=java_file)


text:
from tqdm import tqdm
import numpy as np
from torchvision import transforms,models
import torch
import torch.optim as optim
import torch.nn as nn
from collections import OrderedDict
import os
from train import train_function, save_checkpoint
from test import test_function
from pneumonia import Pneumonia
import pandas as pd
from train import calculateMetrics

class_to_idx = {'Normal': 0, 'Lung Opacity': 1}
cat_to_name = {class_to_idx[i]: i for i in list(class_to_idx.keys())}
'''
model = models.densenet121(pretrained=True)
#model = models.resnet50(pretrained=True)
#model = models.googlenet(pretrained=True)
model.classifier = nn.Sequential(OrderedDict([
    ('fcl1', nn.Linear(1024,256)),
    ('dp1', nn.Dropout(0.3)),
    ('r1', nn.ReLU()),
    ('fcl2', nn.Linear(256,32)),
    ('dp2', nn.Dropout(0.3)),
    ('r2', nn.ReLU()),
    ('fcl3', nn.Linear(32,1)),
    #('out', nn.Softmax(dim=1)),
]))
'''
device = torch.device('cuda:0')
batch_size = 16
train_on_gpu = True
checkpoint = torch.load('/home/dcl/Desktop/TrustableAIProject/dense_fold0/checkpoint_4.pth.tar', map_location={'cuda:2': 'cuda:0'})
model = checkpoint['model']

data_transforms = {
            'train': transforms.Compose([
                        transforms.Resize((224, 224)),
                        transforms.CenterCrop(224),
                        transforms.RandomHorizontalFlip(), # randomly flip and rotate
                        transforms.RandomRotation(10),
                        transforms.ToTensor(),
                        transforms.Normalize((0.485, 0.456, 0.406), (0.229, 0.224, 0.225)),
                    ]),

            'test': transforms.Compose([
                        transforms.Resize((224,224)),
                        transforms.ToTensor(),
                        transforms.Normalize((0.485, 0.456, 0.406), (0.229, 0.224, 0.225)),
                        ]),

            'valid': transforms.Compose([
                        transforms.Resize((224,224)),
                        transforms.ToTensor(),
                        transforms.Normalize((0.485, 0.456, 0.406), (0.229, 0.224, 0.225)),
                    ])
            }

valid_data = Pneumonia('X_test_fold_0.txt', class_to_idx=class_to_idx, transforms=data_transforms['train'])
valid_loader = torch.utils.data.DataLoader(valid_data, batch_size=batch_size, num_workers=0, shuffle=True)

model.eval()
number_correct, number_data = 0, 0
all_preds = []
all_gts = []
for data, target in tqdm(valid_loader):
    if train_on_gpu:
        data, target = data.to(device), target.to(device)
    output = torch.squeeze(model(data))
    ############# calculate the accurecy
    #_, pred = torch.max(output, 1)
    pred = output
    correct_tensor = pred.eq(target.data.view_as(pred))
    all_preds = all_preds + pred.detach().cpu().numpy().tolist()
    all_gts = all_gts + target.data.tolist()
    correct = np.squeeze(correct_tensor.numpy()) if not train_on_gpu \
                            else np.squeeze(correct_tensor.cpu().numpy())
    number_correct += sum(correct)
    number_data += correct.shape[0]
calculateMetrics(all_gts, all_preds, -1)


text:
import numpy as np
import heapq

class PriorityQueue(list):
    def pop(self):
        return heapq.heappop(self)
    def push(self, value):
        return heapq.heappush(self, value)

def neighbors(i, j):
    return ((i-1, j), (i+1, j), (i, j-1), (i, j+1))

def numpy_dijkstra(costs):
    m, n = costs.shape

    start = (0, 0)
    end = (m - 1, n - 1)

    q = PriorityQueue()
    q.push((0, start))

    g = np.full_like(costs, np.inf)
    g[start] = 0

    while q:
        cost, node = q.pop()
        if node == end:
            return int(g[end])

        for adj in neighbors(*node):
            if not (0 <= adj[0] < m and 0 <= adj[1] < n):
                continue
            adj_cost = cost + costs[adj]
            if adj_cost < g[adj]:
                g[adj] = adj_cost
                q.push((adj_cost, adj))

def expand_block(block, M, N):
    m, n = block.shape

    shift = np.add.outer(np.arange(M), np.arange(N))
    shift = np.repeat(np.repeat(shift, m, axis=0), n, axis=1)

    return ((np.tile(block, (M, N)) + shift - 1) % 9) + 1

def solve(data):
    costs_a = np.array([list(row) for row in data], dtype=float)
    ans_a = numpy_dijkstra(costs_a)

    costs_b = expand_block(costs_a, 5, 5)
    ans_b = numpy_dijkstra(costs_b)

    return ans_a, ans_b


text:
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
@author: myrthings
"""

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import datetime as dt

from copy import deepcopy

from aux import *

class GrowthAccounting(object):
    """
    GrowthAccounting is an object implementation of the Growth Accounting Framework. The framework
    was developed by Tribe Capital on a Quantitative Approach towards Product Market Fit.

    This implementation uses PMF units data to fit the model and returns the Growth Accounting
    attributes. It also plots the data in two different ways to help quick lookups to the data.

    We call PMF unit the unit you are using to measure Product Market Fit. Most common PMF unit
    are user interactions or revenue. Read 'Further thoughts and future directions' on references
    article for more information.

    Parameters
    ----------
    period : {'Q','q','M','m','28D','28d','7D','7d','D','d'}, default='M'
        Used to specify the period in which to divde the data. It supports both cased or uncased
        parameters.
         - 'Q' means quarter of a year /3 months.
         - 'M' means monthly.
         - '28D' means 4 weeks starting on Monday and it's used as an alterantive to months to fix
            the number of days for the period.
         - '7D' means weekly starting on Monday.
         - 'D' refers to daily.

    simple : bool, default=True
        Used to specify the kind of data that will fit the object. True means each row will be
        a PMF unit (ex. user interactions) and we only need the 'user_id' and the 'date' of the
        interaction to build the framework. False means each row represent a quantity of the PMF
        unit (ex. revenue) and we need the columns on simple but also a 'quantity' column to
        build the framework.

    Attributes
    ----------
    period : the period input (see on Parameters section)

    simple : the simple input (see on Parameters section)

    period_rep_list : nparray of datetime objects
        Each period ocurrs between the date in this list and the date in this list + period (attribute).
        It uses the first day as the representative of the whole period.

    period_list: nparray of strings objects
        Gives a string tag to each period. It's more knowledgable than period_rep_list but
        worse to use directly on charts because of its string nature.

    total : nparray of floats of length len(period_list)
        Represents total PMF unit per period.

    new : nparray of floats of length len(period_list)
        Represents new PMF unit per period.

    resurrected : nparray of floats of length len(period_list)
        Represents resurrected PMF unit per period.

    expansion : nparray of floats of length len(period_list)
        Represents expansion PMF unit per period.

    contraction : nparray of floats of length len(period_list)
        Represents contraction PMF unit per period.

    retained : nparray of floats of length len(period_list)
        Represents retained PMF unit per period.

    churned : nparray of floats of length len(period_list)
        Represents churned PMF unit per period.

    new_rate : nparray of floats of length len(period_list)
        Represents the change in new PMF unit per period.

    resurrected_rate : nparray of floats of length len(period_list)
        Represents the change in resurrected PMF unit per period.

    expansion_rate : nparray of floats of length len(period_list)
        Represents the change in expansion PMF unit per period.

    contraction_rate : nparray of floats of length len(period_list)
        Represents the change in contraction PMF unit per period.

    retained_rate : nparray of floats of length len(period_list)
        Represents the change in retained PMF unit per period.

    churned_rate : nparray of floats of length len(period_list)
        Represents the change in churned PMF unit per period.

    growth_rate : nparray of floats of length len(period_list)
        Represents Growth Rate of PMF unit per period.

    gross_retention : nparray of floats of length len(period_list)
        Represents Gross Retention of PMF unit per period.

    quick_ratio : nparray of floats of length len(period_list)
        Represents Quick Ratio of PMF unit per period.

    net_churn : nparray of floats of length len(period_list)
        Represents Net Churn of PMF unit per period.

    df : Pandas DataFrame with len(period_list) rows and 16+ columns.
        Is a dataframe containing at least all parameters above from 'total' to 'net_churn'
        that describes the PMF unit behavior in this framework. It's said at least because if
        'compound_growth' is executed it also add that to the dataframe.

    Detailed Use Example
    --------------------
    (future link to a jupyter notebook implementation)

    References
    ----------
    Article with the framework:
        https://tribecap.co/a-quantitative-approach-to-product-market-fit/

    """

    def __init__(self,period='M',simple=True):
        _period=period.lower()

        if _period not in ['m','q','d','28d','7d']:
            raise ValueError("Period paremeter should be in: {'m', 'q', 'd', '28d', '7d'}")
        else:
            self.period=_period

        self.simple=simple

    def fit(self,data,column_date,column_id,column_input=None):
        """
        Fit the model with the given data.

        Parameters
        ----------
        data : Pandas DataFrame with at least 2 columns (or 3 in the case of not simple)
            Each row in this DataFrame should represent and action of PMF unit. We can use both
            actions as they are (simple) or with a quantity column (not simple)

        column_date : string
            Name of the column on 'data' with inputs in datetime format.

        column_id : string
            Name of the column on 'data' containing the ids of the consumers / users.

        column_input : string, default=None
            Name of the column on 'data' containing the quantinty of the PMF unit.
            Default is None because 'simple' parameter is True by default.

        Returns
        -------
        self
            Fitted object.

        """

        if not self.simple and not column_input:
            raise ValueError("simple = False requires argument 'column_input'")

        else:
            print('Preparing the data...')
            df=deepcopy(data)
            df['unique_id']=np.arange(len(df))

            if self.simple:
                column_input='column_input'
                df[column_input]=1

            df['period_rep']=df[column_date].apply(lambda x: custom_representative(self.period,x))

            df.set_index(column_id,inplace=True)
            df['cohort_rep']=df.groupby(level=0)['period_rep'].min()
            df=df.reset_index()

            df_users=df.groupby([column_id,'cohort_rep','period_rep'])[['unique_id']].count().rename(columns={'unique_id':'freq'})
            df_users=df_users.unstack().fillna(0).stack().reset_index()
            df_users=df_users[df_users['period_rep']>=df_users['cohort_rep']]
            df_users['freq']=df_users['freq'].astype(int)
            df_users['uniques']=df_users['freq'].apply(lambda x: np.where(x>0,1,0))

            def user_change(df):
                df['change']=df['uniques'].diff()
                return df

            df_users=df_users.groupby([column_id,'cohort_rep']).apply(user_change)
            df_users['change']=df_users['change'].fillna(2)
            df_users['supercolumn']=df_users['change']+df_users['uniques']

            df_revenue=df_users[[column_id,'cohort_rep','period_rep','supercolumn']].set_index([column_id,'period_rep'])
            df_revenue['revenue']=df.groupby([column_id,'period_rep'])[column_input].sum()
            df_revenue['revenue']=df_revenue['revenue'].fillna(0)
            df_revenue=df_revenue.reset_index()

            def revenue_change(df):
                df['revchange']=df['revenue'].diff()
                return df

            df_revenue=df_revenue.groupby([column_id,'cohort_rep']).apply(revenue_change)
            df_revenue['revstatus']=df_revenue['revchange'].apply(lambda x: np.where(x>0,1,np.where(x<0,-1,0)))
            df_revenue['retained']=df_revenue['revenue']-df_revenue['revchange'].apply(lambda x: np.where(x<0,0,x))

            print('Computing Growth Accounting...')
            dfgrowth=df.groupby(['cohort_rep'])[[column_id]].nunique().rename(columns={column_id:'new_ids'})
            dfgrowth['total_ids']=df.groupby(['period_rep'])[[column_id]].nunique()
            dfgrowth['total_orders']=df.groupby(['period_rep'])[[column_id]].count()

            dfgrowth['new']=df_revenue[df_revenue['supercolumn']==3].groupby('period_rep')['revenue'].sum()
            dfgrowth['resurrected']=df_revenue[df_revenue['supercolumn']==2].groupby('period_rep')['revenue'].sum()
            dfgrowth['expansion']=df_revenue[(df_revenue['supercolumn']==1)&(df_revenue['revstatus']==1)].groupby('period_rep')['revchange'].sum()
            dfgrowth['contraction']=df_revenue.loc[(df_revenue['supercolumn']==1)&(df_revenue['revstatus']==-1)].groupby('period_rep')['revchange'].sum()
            dfgrowth['retained']=df_revenue.loc[(df_revenue['supercolumn']==1)].groupby('period_rep')['retained'].sum()
            dfgrowth['churned']=df_revenue.loc[(df_revenue['supercolumn']==-1)].groupby('period_rep')['revchange'].sum()

            dfgrowth=dfgrowth.fillna(0)

            dfgrowth['total']=df_revenue.groupby('period_rep')['revenue'].sum()
            dfgrowth['total_rev2']=dfgrowth['new']+dfgrowth['retained']+dfgrowth['expansion']+dfgrowth['resurrected']
            dfgrowth['total_rev-1']=dfgrowth['retained']-dfgrowth['churned']-dfgrowth['contraction']

            dfgrowth['new_rate']=dfgrowth['new']/dfgrowth['new'].shift(1)
            dfgrowth['resurrected_rate']=dfgrowth['resurrected']/dfgrowth['resurrected'].shift(1)
            dfgrowth['expansion_rate']=dfgrowth['expansion']/dfgrowth['expansion'].shift(1)
            dfgrowth['contraction_rate']=dfgrowth['contraction']/dfgrowth['contraction'].shift(1)
            dfgrowth['retained_rate']=dfgrowth['retained']/dfgrowth['retained'].shift(1)
            dfgrowth['churned_rate']=dfgrowth['churned']/dfgrowth['churned'].shift(1)

            dfgrowth['growth_rate']=dfgrowth['new_rate']+dfgrowth['resurrected_rate']+dfgrowth['expansion_rate']-dfgrowth['contraction_rate']-dfgrowth['churned_rate']
            dfgrowth['gross_retention']=dfgrowth['retained']/dfgrowth['total'].shift(1)
            dfgrowth['quick_ratio']=(dfgrowth['new']+dfgrowth['resurrected']+dfgrowth['expansion'])/(-dfgrowth['churned']-dfgrowth['contraction'])
            dfgrowth['net_churn']=(-dfgrowth['churned']-dfgrowth['contraction']-dfgrowth['resurrected']-dfgrowth['expansion'])/(dfgrowth['total'].shift(1))

            dfgrowth=dfgrowth.replace(np.inf, np.nan).replace(-np.inf, np.nan)
            # dfgrowth=dfgrowth.fillna(0) #this falsifies the data

            self.period_rep_list=np.array(dfgrowth.index)
            self.period_list=np.array(pd.Series(dfgrowth.index).apply(lambda x: custom_period(self.period,x)))

            self.total=np.array(dfgrowth['total'])
            self.new=np.array(dfgrowth['new'])
            self.resurrected=np.array(dfgrowth['resurrected'])
            self.expansion=np.array(dfgrowth['expansion'])
            self.contraction=np.array(dfgrowth['contraction'])
            self.retained=np.array(dfgrowth['retained'])
            self.churned=np.array(dfgrowth['churned'])

            self.new_rate=np.array(dfgrowth['new_rate'])
            self.resurrected_rate=np.array(dfgrowth['resurrected_rate'])
            self.expansion_rate=np.array(dfgrowth['expansion_rate'])
            self.contraction_rate=np.array(dfgrowth['contraction_rate'])
            self.retained_rate=np.array(dfgrowth['retained_rate'])
            self.churned_rate=np.array(dfgrowth['churned_rate'])

            self.growth_rate=np.array(dfgrowth['growth_rate'])
            self.gross_retention=np.array(dfgrowth['gross_retention'])
            self.quick_ratio=np.array(dfgrowth['quick_ratio'])
            self.net_churn=np.array(dfgrowth['net_churn'])

            self.df=dfgrowth[['total','new','resurrected','expansion','contraction','retained','churned',
                             'new_rate','resurrected_rate','expansion_rate','retained_rate','churned_rate',
                             'growth_rate','gross_retention','quick_ratio','net_churn']]

            print('Done!')

    def compound_growth(self,num_periods):
        """
        Calculate the Compound Growth on the period size, given a number of periods.

        Parameters
        ----------
        num_periods : int
            Number of periods to calculate the compound growth.

        Returns
        -------
        self
            Adds a column with the value to the 'df' attribute.

        np array of floats with the values

        """
        label='C{}GR{}'.format(self.period,num_periods).upper()
        self.df[label]=(self.df['total']/self.df['total'].shift(num_periods)).apply(lambda x: x**(1/num_periods)-1)
        return np.array(self.df[label])

    def plot_compound_growth(self,num_periods):
        """
        Calculate and plots the Compound Growth on the period size, given a number of periods.

        Parameters
        ----------
        num_periods : int
            Number of periods to calculate the compound growth.

        Returns
        -------
        self
            Adds a column with the value to the 'df' attribute.

        figure
            It directly plots the values on a matplolib figure

        """
        label='C{}GR{}'.format(self.period,num_periods).upper()
        self.df[label]=(self.df['total']/self.df['total'].shift(num_periods)).apply(lambda x: x**(1/num_periods)-1)
        comp = np.array(self.df[label])

        fig,ax1=plt.subplots(1,1,figsize=(15,5))
        ax2=ax1.twinx() #sacar eje secundario

        ax1.bar(self.period_list,self.total,color='blue',width=1,alpha=0.5,label='Revenue')
        ax2.plot(self.period_list,comp,color='black',label=label)

        fig.legend()
        plt.show()

    def create_plot(self,ax1):
        """
        Creates the plot for the Growth Accounting Framework. It can be used to personalize
        later the plot with custom title, legend, axlabels or whatever.

        Parameters
        ----------
        ax1 : matplotlib figure ax
            Used to plot the charts and to get a duplicated axis as ax2

        Returns
        -------
        ax1 : matplotlib figure ax
            With the elements ploted

        ax2 : matplotlib twinx of ax1.
            With the elements ploted

        """
        ax1.bar(self.period_list,self.new,color='blue',width=1,alpha=0.2,label='New')
        ax1.bar(self.period_list,self.resurrected,bottom=self.new,color='blue',width=1,alpha=0.5,label='Resurrected')
        ax1.bar(self.period_list,self.expansion,bottom=self.new+self.resurrected,color='blue',width=1,alpha=0.7,label='Expansion')

        ax1.bar(self.period_list,self.churned,color='red',width=1,alpha=0.2,label='Churned')
        ax1.bar(self.period_list,self.contraction,bottom=self.churned,color='red',width=1,alpha=0.5,label='Contraction')

        ax2=ax1.twinx()
        ax2.plot(self.period_list,self.gross_retention,color='blue',label='Gross Retention')
        ax2.plot(self.period_list,self.net_churn,color='black',label='Net Churn')
        ax2.plot(self.period_list,self.quick_ratio,color='green',label='Quick Ratio')

        #arreglamos los ticks
        ticks1=ax1.get_yticks()
        ax1.set_ylim(ymin=ticks1.min(),ymax=ticks1.max())

        ticks1=ax1.get_yticks()
        ticks2=ax2.get_yticks()
        ax2.set_ylim(ymax=ticks2.max(),ymin=(ticks2.max()*ticks1.min())/ticks1.max())

        ticks=ax1.get_xticks()
        ax1.set_xlim(xmin=min(ticks)-0.5,xmax=max(ticks)+0.5)

        ax1.hlines(0,min(ticks)-1,max(ticks)+1,linewidth=0.5,alpha=0.5)

        return ax1,ax2

    def plot(self):
        """
        Plot the Growth Accounting Framework.

        Parameters
        ----------
        None

        Returns
        -------
        figure
            It directly plots the values on a matplolib figure

        """
        fig,ax1=plt.subplots(1,1,figsize=(10,8))

        #creates the plot
        ax1,ax2=self.create_plot(ax1)
        fig.legend()

        plt.show()

max_stars_repo_name: myrthings/giraffe-startups

max_issues_repo_name: myrthings/giraffe-startups

max_forks_repo_name: myrthings/giraffe-startups


text:
import cv2
import spreadSheet as sp
import numpy as np
import urllib.request
from PIL import Image

def draw_boundary(img, classifier, scaleFactor, minNeighbours, color,text, clf):
    gray_img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    features = classifier.detectMultiScale(gray_img, scaleFactor, minNeighbours)
    coords = []

    for(x, y, w, h) in features:
        cv2.rectangle(img, (x,y), (x+w, y+h), color, 2)
        id, _= clf.predict(gray_img[y:y+h, x:x+w])
        id, confi= clf.predict(gray_img[y:y+h, x:x+w])

        print("userid = ", id, " confidence = " ,confi, end ="\n\n")
        if(confi < 60):
            if (id == 1):
              name = "Aastha"
            elif (id == 2):
              name = "Abhay"
            elif (id == 3):
              name = "Aima"
            elif (id == 4):
              name = "Alamdar Abbas"
            elif (id == 5):
              name = "Aman"
            elif (id == 6):
              name = "Arjun"
            elif (id == 7):
              name = "Ashar"
            elif (id == 8):
              name = "Ashutosh Dwivedi"
            elif (id == 9):
              name = "Ashutosh Kumar"
            elif (id == 10):
              name = "Dipanshu"
            elif (id == 11):
              name = "Himunshu"
            elif (id == 12):
              name = "Ifra"
            elif (id == 13):
              name = "Isha"
            elif (id == 14):
              name = "Ishraq"
            elif (id == 15):
              name = "Israil"
            elif (id == 16):
              name = "Kavya"
            elif (id == 17):
              name = "Kunal"
            elif (id == 18):
              name = "Mariam Jamal"
            elif (id == 19):
              name = "Mariam Khan"
            elif (id == 20):
              name = "Asad"
            elif (id == 21):
              name = "Najir"
            elif (id == 22):
              name = "Atif"
            elif (id == 23):
              name = "Muzzakir"
            elif (id == 24):
              name = "Sahil"
            elif (id == 25):
              name = "Sharique"
            elif (id == 26):
              name = "Shoaib"
            elif (id == 27):
              name = "Touheed"
            elif (id == 28):
              name = "Zahid"
            elif (id == 29):
              name = "Zargham"
            elif (id == 30):
              name = "Moin"
            elif (id == 31):
              name = "Naman"
            elif (id == 32):
              name = "Nausheen"
            elif (id == 33):
              name = "Naval"
            elif (id == 34):
              name = "Nazreen"
            elif (id == 35):
              name = "Nikhil"
            elif (id == 36):
              name = "Pushpendra"
            elif (id == 37):
              name = "Rishi"
            elif (id == 38):
              name = "Shabhat"
            elif (id == 39):
              name = "Saba"
            elif (id == 40):
              name = "Sandeep"
            elif (id == 41):
              name = "Shaba"
            elif (id == 42):
              name = "Fahad"
            elif (id == 43):
              name = "Shaqib"
            elif (id == 44):
              name = "Shivam"
            elif (id == 45):
              name = "Shubam"
            elif (id == 46):
              name = "Shubangi"
            elif (id == 47):
              name = "Sumit"
            elif (id == 48):
              name = "Hamid"
            elif (id == 49):
              name = "Tairah"
            elif (id == 50):
              name = "Tarun"
            elif (id == 51):
              name = "Vaishali"
            elif (id == 52):
              name = "Vinay"
            elif (id == 53):
              name = "Wasif"
            elif (id == 54):
              name = "Wasit"
            elif (id == 55):
              name = "Yogesh"
            elif (id == 56):
              name = "Ives"
            elif (id == 57):
              name = "Zain"
            font = cv2.FONT_HERSHEY_SIMPLEX
            cv2.putText(img, name, (x,y-4), font, 0.8, color, 1, cv2.LINE_AA)
            sp.markAttendence(id)
        else:
           font = cv2.FONT_HERSHEY_SIMPLEX
           cv2.putText(img, "Face not recognized....!", (x,y-4), font, 0.8, color, 1, cv2.LINE_AA)

        coords = [x, y, w, h]
    return coords
def recognize(img, clf, faceCascade):
    color = {"blue":(255,0,0),"red": (0,0,255)}

    coords = draw_boundary(img, faceCascade, 1.1, 10, color['blue'], "face", clf)
    return img

def detect(img, faceCascade, img_id):
    color = {"blue":(255, 0, 0), "red":(255, 255, 0)}
    coords = draw_boundary(img, faceCascade, 1.1, 10, color['red'], "face", clf)
    if len(coords) == 4:
        roi_img = img[coords[1] : coords[1]+coords[3],coords[0] : coords[0]+coords[2]]
        user_id = 3 # change the user for each different users
        generate_dataset(roi_img, user_id, img_id)

    return img

faceCascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
clf = cv2.face.LBPHFaceRecognizer_create()
clf.read("classifier.yml")

img_id  = 0
url = input("enter URL")
while True:
    imgResp = urllib.request.urlopen(url)
    imgNp = np.array(bytearray(imgResp.read()), np.uint8)
    img = cv2.imdecode(imgNp, -1)
    cv2.imshow("face Detection", img)
    img = recognize(img, clf, faceCascade)
    cv2.imshow("face Detection", img)
    img_id = img_id + 1
    if cv2.waitKey(1) & 0xff == ord('q'):
        break
cv2.destroyAllWindows()