import argparse
import os.path
from datetime import datetime as dt
from datetime import timedelta
from netCDF4 import Dataset

import numpy as np
import warnings

warnings.simplefilter('ignore', UserWarning)
warnings.simplefilter('ignore', RuntimeWarning)

parser = argparse.ArgumentParser(description="Arctic climatology")
parser.add_argument("-m", "--mode", help="Mode", choices=["MULTI","SCRIPT"], required=True)
parser.add_argument("-c", "--config_file", help="Config file", required=True)
# parser.add_argument('-i', "--inputpath", help="Input directory")
parser.add_argument('-sd', "--start_date", help="Start date (yyyy-mm-dd)", required=True)
parser.add_argument('-ed', "--end_date", help="End Date (yyyy-mm-dd)", required=True)
# parser.add_argument('-o', "--output", help="Output directory")
parser.add_argument("-v", "--verbose", help="Verbose mode.", action="store_true")
args = parser.parse_args()

def do_test():
    file_nc = '/mnt/c/DATA_LUIS/OCTAC_WORK/ARC_MULTI_CLIMATOLOGY/TEMP_07_19/TempResults_800_1000_800_1000.nc'
    from netCDF4 import Dataset
    dataset = Dataset(file_nc)
    dc = np.array(dataset.variables['DATA_COMPUTATION'])
    dw = np.array(dataset.variables['DATA_WEIGHT'])

    avg_var = np.array(dataset.variables['W_AVG'])
    avg_var_t = np.zeros(dc.shape)
    avg_var_t[:, :, :] = avg_var[:, :]
    ndata = np.array(dataset.variables['NOFOBS'])
    num_wstd = np.power((dc - avg_var_t), 2)
    num_wstd = np.multiply(dw, num_wstd)
    el = num_wstd[:, 14, 173]
    for e in el:
        print('---> el cuadrado', e)
    lasuma = np.nansum(num_wstd, axis=0)
    print('la suma: ', lasuma[14, 173])
    # num_wstd = np.multiply(num_wstd, dw)
    factor = (ndata - 1) / ndata
    print('Ndata ', ndata[14, 173], 'factor: ', factor[14, 173])
    weighted_std = np.nansum(num_wstd, axis=0) / (factor * np.nansum(dw, axis=0))
    weighted_std = np.sqrt(weighted_std)

    data = dc[:, 14, 173]
    weights = dw[:, 14, 173]
    sum_data = 0
    sum_weights = 0
    for d, w in zip(data, weights):
        # print(d,w)
        if not np.isnan(d):
            sum_data = sum_data + (d * w)
            sum_weights = sum_weights + w
    avg_w = sum_data / sum_weights

    n_obs = 0
    num = 0
    denom = 0
    for d, w in zip(data, weights):
        # print(d,w)
        if not np.isnan(d):
            num = num + (w * ((d - avg_w) * (d - avg_w)))
            denom = denom + w
            n_obs = n_obs + 1
    print('la suma tal: ', num)
    factor = (n_obs - 1) / n_obs
    print('nobs: ', n_obs, 'factor: ', factor)
    denom = ((n_obs - 1) / n_obs) * denom
    w_std = np.sqrt(num / denom)
    # avg = np.nanmean(dc,axis=0)
    # print('-----------------------')
    # print(np.nanmean(tal))
    # print(avg[14,173])
    print('------------')
    # avg_var = np.array(dataset.variables['W_AVG'])
    # print(avg_var[14,173])
    # print(avg_w)
    # std_nor = np.array(dataset.variables['N_STD'])
    std_var = np.array(dataset.variables['W_STD'])
    # print(std_nor[14,173])
    # print('NDATA',ndata[14,173],n_obs)
    print(weighted_std[14, 173])
    print(w_std)
    print(std_var[14, 173])

    dataset.close()

    return True

def do_test2():
    file_nc = '/mnt/c/DATA_LUIS/OCTAC_WORK/ARC_MULTI_CLIMATOLOGY/TEMP_07_19/Temp_200_400_400_600_2022.nc'
    from netCDF4 import Dataset
    dataset = Dataset(file_nc)
    w = np.array(dataset.variables['WEIGHTS'])
    w[w == -999] = np.nan
    sw = np.nansum(w, axis=0)
    print(np.min(sw), np.max(sw))
    dataset.close()

    return True

def make_script(options,output_path):
    print(f'[INFO] Making script')
    ref = 'CLIMATOLOGY'
    if not options.has_option(ref,'variable'):
        print(f'[ERROR] Variable option in file config is required')
        return
    variable = options[ref]['variable']
    date_here, date_here_end = get_dates()

    sdate = date_here.strftime('%Y%m%d')
    edate = date_here_end.strftime('%Y%m%d')

    name_output = f'Script_{variable}_{sdate}_{edate}.sh'
    file_output = os.path.join(output_path,name_output)
    name_slurm = f'run_climatology_multi_{variable}_date.slurm'
    index = 1
    f1= open(file_output,'w')
    while date_here <= date_here_end:
        date_here_str = date_here.strftime('%Y-%m-%d')
        if index==1:
            line = f'job1=$(sbatch /store/COP2-OC-TAC/arc/code/scripts_climatology/{name_slurm} {date_here_str})'
            f1.write(line)
            f1.write('\n')
            line = f'job1id=$(echo "$job1" | awk ' + f"'" + '{print $NF}' + f"'" + ')'
            f1.write(line)
            f1.write('\n')
            f1.write('\n')
        else:
            index_prev = index-1
            line = f'job{index}=$(sbatch --dependency=afterany:$job{index_prev}id /store/COP2-OC-TAC/arc/code/scripts_climatology/{name_slurm} {date_here_str})'
            f1.write(line)
            f1.write('\n')
            line = f'job{index}id=$(echo "$job{index}" | awk ' + f"'" + '{print $NF}' + f"'" + ')'
            f1.write(line)
            f1.write('\n')
            f1.write('\n')
        index = index + 1
        date_here = date_here + timedelta(hours=24)
    f1.close()
    print(f'[INFO] File created: {file_output}')

def get_dates():
    ## getting dates
    date_here_str = args.start_date
    try:
        date_here = dt.strptime(date_here_str, '%Y-%m-%d')
    except:
        print(f'[ERROR] Start Date for climatology is not in the correct format YYYY-mm-dd')
        return

    if args.end_date:
        date_here_end_str = args.end_date
        try:
            date_here_end = dt.strptime(date_here_end_str, '%Y-%m-%d')
        except:
            print(f'[WARNING] Start Date for climatology is not in the correct format YYYY-mm-dd.')
            return
    else:
        date_here_end = date_here

    return date_here, date_here_end

def main():
    # if do_test2():
    #     return
    print('[INFO] STARTED CLIMATOLOGY')
    if not os.path.exists(args.config_file):
        print(f'[ERROR] Config file {args.config_file} does not exist. Exiting...')
        return
    try:
        import configparser
        options = configparser.ConfigParser()
        options.read(args.config_file)
    except:
        print(f'[ERROR] Config file {args.config_file} could not be read. Exiting...')

    ref = 'CLIMATOLOGY'
    if not options.has_section(ref):
        print(f'[ERROR] Config file {args.config_file} must containt a section named CLIMATOLOGY')
        return

    if not options.has_option(ref, 'input_path'):
        print(f'[ERROR] input_path option is required in config file {args.config_file}')
        return

    input_path = options[ref]['input_path']
    if not os.path.exists(input_path):
        print(f'[ERROR] Input path {input_path} does not exist')
        return
    if options.has_option(ref, 'output_path'):
        output_path = options[ref]['output_path']
    else:
        output_path = input_path
    if not os.path.exists(output_path):
        try:
            os.mkdir(output_path)
        except:
            print(f'[ERROR] Output path {output_path} does not exist and could not be created')
            return

    if args.mode == 'SCRIPT':
        make_script(options,output_path)
        return

    # defaults
    options_clim = {
        'temporal_window': 11,
        'spatial_window': 3,
        'nywindow': 200,
        'nxwindow': 200,
        'use_weights': True,
        'variable': 'CHL',
        'ny': -1,
        'nx': -1,
        'file_ref': '/mnt/c/DATA_LUIS/OCTAC_WORK/ARC_TEST/MULTI/GRID_FILES/ArcGrid_65_90_4KM_GridBase.nc',
        'year_ini': 1998,
        'year_end': 2022,
        'yref': -1,
        'xref': -1,
        'name_out': '$DATE$_chl_arc_multi_clima.nc',
        'applyPool': 0,
        'input_name': 'C$DATE$_chl-arc-4km.nc',
        'log_scale': False
    }

    # setting options
    for option in options_clim:
        if options.has_option(ref, option):
            if option == 'file_ref' or option == 'variable' or option == 'name_out' or option == 'input_name':
                options_clim[option] = options[ref][option].strip()
            elif option == 'use_weights' or option == 'log_scale':
                if options[ref][option].strip().lower() == 'true' or options[ref][option].strip() == '1':
                    options_clim[option] = True
                else:
                    options_clim[option] = False
            else:
                options_clim[option] = int(options[ref][option].strip())

    date_here, date_here_end = get_dates()

    while date_here<=date_here_end:
        if args.mode == 'MULTI':
            year_ini = options_clim['year_ini']
            year_end = options_clim['year_end']
            apply_pool = options_clim['applyPool']
            if apply_pool != 0:
                params_list = []
                from multiprocessing import Pool
                if args.verbose:
                    print('[INFO] Starting parallel processing')
            else:
                if args.verbose:
                    print('[INFO] Starting sequential processing')

            for year in range(year_ini, year_end + 1):
                date_here_y = date_here.replace(year=year)
                options_clim_here = options_clim.copy()
                options_clim_here['year_ini'] = year
                options_clim_here['year_end'] = year
                warnings.simplefilter('ignore', UserWarning)
                warnings.simplefilter('ignore', RuntimeWarning)
                if apply_pool == 0:  ##sequential
                    make_clim_multi_day_extracts(input_path, output_path, date_here_y, options_clim_here)
                else:
                    params_here = [input_path, output_path, date_here_y, options_clim_here]
                    params_list.append(params_here)
                    if len(params_list) == options_clim['applyPool']:
                        if args.verbose:
                            print(f'[INFO] Running parallel processes: {apply_pool}')
                        poolhere = Pool(apply_pool)
                        poolhere.map(make_clim_multi_day_extracts_parallel, params_list)
                        params_list = []
            if apply_pool != 0 and len(params_list) > 0:
                if args.verbose:
                    print(f'[INFO] Running parallel processes: {apply_pool} -> {len(params_list)}')
                poolhere.map(make_clim_multi_day_extracts_parallel, params_list)
            make_clime_multi_day_computation(output_path, date_here, options_clim)
            make_clim_together(output_path, date_here, options_clim)

        date_here = date_here + timedelta(hours=24)

def make_clim_multi_day_extracts_parallel(params):
    make_clim_multi_day_extracts(params[0], params[1], params[2], params[3])

def make_clim_multi_day_extracts(input_path, output_path, date_here, options):
    temporal_window = options['temporal_window']  # 11
    spatial_window = options['spatial_window']  # 3
    nywindow = options['nywindow']
    nxwindow = options['nxwindow']
    use_weights = options['use_weights']
    variable = options['variable']
    ny = options['ny']
    nx = options['nx']
    input_name = options['input_name']
    log_scale = options['log_scale']

    date_list = get_input_file_list_day(date_here, temporal_window, input_path, date_here.year, date_here.year,
                                        input_name)
    if ny == -1 and nx == -1:
        for date_str in date_list:
            input_file = date_list[date_str]['input_file']
            if input_file is not None:
                ny, nx = get_ny_nx(input_file, variable)

    ndata = temporal_window * spatial_window * spatial_window

    data_computation = np.zeros((ndata, ny, nx))
    data_weights = np.ones((ndata, ny, nx))
    data_computation[:] = -999.0
    if use_weights:
        if args.verbose:
            print('[INFO] Retrieving weights...')
        spatial_weights = get_spatial_weight_filter_3x3(None, 1, 1)
        temporal_weights = get_temporal_weight_filter(None, temporal_window, 1, 1)
        index = 0
        for itemporal in range(len(temporal_weights)):
            for ispatial in range(len(spatial_weights)):
                data_weights[index, :, :] = spatial_weights[ispatial] * temporal_weights[itemporal]
                index = index + 1

    itime = 0
    idate = 0
    for date_str in date_list:
        # date_h = dt.strptime(date_str, '%Y-%m-%d')
        input_file = date_list[date_str]['input_file']
        if args.verbose:
            print(
                f'[INFO] Index date: {idate} Index all: {itime} Date: {date_str} Input file: {input_file}')
        if input_file is None:
            itime = itime + (spatial_window * spatial_window)
            continue

        if nywindow >= ny and nxwindow >= nx:
            indices = [itime, 0, ny, 0, nx, ny, nx]
            data_computation, weighted_avg = set_data_window_3x3(data_computation, input_file, variable, indices, None,
                                                                 None)
        else:
            for y in range(0, ny, nywindow):
                for x in range(0, nx, nxwindow):
                    limits = get_limits(y, x, nywindow, nxwindow, ny, nx)
                    nywindow_here = limits[1] - limits[0]
                    nxwindow_here = limits[3] - limits[2]
                    indices = [itime, limits[0], nywindow_here, limits[2], nxwindow_here, ny, nx]
                    data_computation, weighted_avg = set_data_window_3x3(data_computation, input_file, variable,
                                                                         indices, None, None)
        idate = idate + 1
        itime = itime + (spatial_window * spatial_window)

    data_weights[data_computation == -999] = -999

    mstr = date_here.strftime('%m')
    dstr = date_here.strftime('%d')
    output_temp = os.path.join(output_path, f'TEMP_{mstr}_{dstr}')
    if not os.path.isdir(output_temp):
        os.mkdir(output_temp)
    for y in range(0, ny, nywindow):
        for x in range(0, nx, nxwindow):
            limits = get_limits(y, x, nywindow, nxwindow, ny, nx)
            nywindow_here = limits[1] - limits[0]
            nxwindow_here = limits[3] - limits[2]
            output_file = os.path.join(output_temp,
                                       f'Temp_{limits[0]}_{limits[1]}_{limits[2]}_{limits[3]}_{date_here.year}.nc')
            dout = start_data_temporal_file(output_file, nywindow_here, nxwindow_here, ndata)
            array = data_computation[:, limits[0]:limits[1], limits[2]:limits[3]]
            if log_scale:
                array[array!=-999] = np.log10(array[array!=-999])
            dout.variables['DATA'][:, :, :] = array
            dout.variables['WEIGHTS'][:, :, :] = data_weights[:, limits[0]:limits[1], limits[2]:limits[3]]
            dout.close()

    print('[INFO] Completed')

def make_clime_multi_day_computation(output_path, date_here, options):
    temporal_window = options['temporal_window']  # 11
    spatial_window = options['spatial_window']  # 3
    nywindow = options['nywindow']
    nxwindow = options['nxwindow']
    ny = options['ny']
    nx = options['nx']
    year_ini = options['year_ini']  # 1998
    year_end = options['year_end']  # 2022
    apply_pool = options['applyPool']
    if apply_pool != 0:
        params_list = []
        from multiprocessing import Pool
        if args.verbose:
            print('[INFO] Starting parallel processing')

    mstr = date_here.strftime('%m')
    dstr = date_here.strftime('%d')
    output_temp = os.path.join(output_path, f'TEMP_{mstr}_{dstr}')

    nyears = (year_end - year_ini) + 1
    nbyyear = temporal_window * spatial_window * spatial_window
    ndata = nyears * temporal_window * spatial_window * spatial_window

    for y in range(0, ny, nywindow):
        for x in range(0, nx, nxwindow):
            limits = get_limits(y, x, nywindow, nxwindow, ny, nx)
            if apply_pool == 0:
                make_clime_multi_day_computation_impl(ndata, nbyyear, nyears, limits, year_ini, year_end, output_temp)
            else:
                params_here = [ndata, nbyyear, nyears, limits, year_ini, year_end, output_temp]
                params_list.append(params_here)
                if len(params_list) == apply_pool:
                    if args.verbose:
                        print(f'[INFO] Running parallel processes: {apply_pool}')
                    poolhere = Pool(apply_pool)
                    poolhere.map(make_clime_multi_day_computation_impl_parallel, params_list)
                    params_list = []
    if apply_pool != 0 and len(params_list) > 0:
        if args.verbose:
            print(f'[INFO] Running parallel processes: {apply_pool} -> {len(params_list)}')
        poolhere = Pool(apply_pool)
        poolhere.map(make_clime_multi_day_computation_impl_parallel, params_list)

def make_clime_multi_day_computation_impl_parallel(params):
    make_clime_multi_day_computation_impl(params[0], params[1], params[2], params[3], params[4], params[5], params[6])

def make_clime_multi_day_computation_impl(ndata, nbyyear, nyears, limits, year_ini, year_end, output_temp):
    nywindow_here = limits[1] - limits[0]
    nxwindow_here = limits[3] - limits[2]
    data_computation = np.zeros((ndata, nywindow_here, nxwindow_here))
    data_weights = np.ones((ndata, nywindow_here, nxwindow_here))
    output_file = os.path.join(output_temp, f'TempResults_{limits[0]}_{limits[1]}_{limits[2]}_{limits[3]}.nc')
    idata = 0
    for year in range(year_ini, year_end + 1):
        input_file = os.path.join(output_temp, f'Temp_{limits[0]}_{limits[1]}_{limits[2]}_{limits[3]}_{year}.nc')
        dset = Dataset(input_file)
        data_computation[idata:idata + nbyyear, :, :] = np.array(dset.variables['DATA'][:, :, :])
        data_weights[idata:idata + nbyyear, :, :] = np.array(dset.variables['WEIGHTS'][:, :, :])

        dset.close()
        idata = idata + nbyyear

    data_weights[data_computation == -999] = np.nan
    data_computation[data_computation == -999] = np.nan

    print(f'[INFO] Computing for {limits[0]} - {limits[1]} {limits[2]} - {limits[3]}')

    avg = np.nanmean(data_computation, axis=0)
    median = np.nanmedian(data_computation, axis=0)
    std = np.nanstd(data_computation, axis=0)
    min_array = np.nanmin(data_computation, axis=0)
    max_array = np.nanmax(data_computation, axis=0)
    nofobs = np.count_nonzero(~np.isnan(data_weights), axis=0)
    weigthed_prod = np.multiply(data_computation, data_weights)
    sum_weights = np.nansum(data_weights, axis=0)
    weighted_avg = np.nansum(weigthed_prod, axis=0) / sum_weights
    diff_n_w = np.abs(avg - weighted_avg)

    # weighted str
    weighted_avg_t = np.zeros(data_computation.shape)
    weighted_avg_t[:, :, :] = weighted_avg[:, :]
    num_wstd = np.power((data_computation - weighted_avg_t), 2)
    num_wstd = np.multiply(num_wstd, data_weights)
    factor = (nofobs - 1) / nofobs
    weighted_std = np.nansum(num_wstd, axis=0) / (factor * sum_weights)
    weighted_std = np.sqrt(weighted_std)

    pw = (sum_weights / (24 * nyears)) * 100

    # ntimes = data_weights.shape[0]
    dout = start_results_temporal_file(output_file, nywindow_here, nxwindow_here)
    dout.variables['W_AVG'][:, :] = weighted_avg[:, :]
    dout.variables['N_AVG'][:, :] = avg[:, :]
    dout.variables['W_STD'][:, :] = weighted_std[:, :]
    dout.variables['N_STD'][:, :] = std[:, :]
    dout.variables['MEDIAN'][:, :] = median[:, :]
    dout.variables['MIN'][:, :] = min_array[:, :]
    dout.variables['MAX'][:, :] = max_array[:, :]
    dout.variables['NOFOBS'][:, :] = nofobs[:, :]
    dout.variables['SUM_W'][:, :] = sum_weights[:, :]
    dout.variables['PORC_W'][:, :] = pw[:, :]
    dout.variables['DIFF_N_W'][:, :] = diff_n_w[:, :]

    # dout.variables['DATA_WEIGHT'][:, :, :] = data_weights[:, :, :]
    # dout.variables['DATA_COMPUTATION'][:, :, :] = data_computation[:, :, :]

    dout.close()

def make_clim_together(output_path, date_here, options):
    nywindow = options['nywindow']
    nxwindow = options['nxwindow']
    ny = options['ny']
    nx = options['nx']
    year_ini = options['year_ini']  # 1998
    year_end = options['year_end']  # 2022
    name_out = options['name_out']

    mstr = date_here.strftime('%m')
    dstr = date_here.strftime('%d')
    output_temp = os.path.join(output_path, f'TEMP_{mstr}_{dstr}')
    date_here_str = f'{year_ini}{mstr}{dstr}_{year_end}{mstr}{dstr}'
    name_out = name_out.replace('$DATE$', date_here_str)
    file_out = os.path.join(output_path, name_out)
    dout = start_results_temporal_file(file_out, ny, nx)

    for y in range(0, ny, nywindow):
        for x in range(0, nx, nxwindow):
            limits = get_limits(y, x, nywindow, nxwindow, ny, nx)
            # nywindow_here = limits[1] - limits[0]
            # nxwindow_here = limits[3] - limits[2]
            file_results = os.path.join(output_temp, f'TempResults_{limits[0]}_{limits[1]}_{limits[2]}_{limits[3]}.nc')
            din = Dataset(file_results)
            for var in din.variables:
                dout.variables[var][limits[0]:limits[1], limits[2]:limits[3]] = din.variables[var][:, :]
            din.close()

    dout.close()

def get_input_file_list_day(date_here, temporal_window, input_path, year_ini, year_end, input_name):
    if args.verbose:
        print(f'[INFO] Getting file list...')
    ndaysw = int(np.floor(temporal_window / 2))
    date_list = {}
    if year_ini == -1 and year_end == -1:
        year_ini = date_here.year
        year_end = date_here.year
    for year in range(year_ini, year_end + 1):
        date_here_min = date_here.replace(year=year) - timedelta(days=ndaysw)
        date_here_max = date_here.replace(year=year) + timedelta(days=ndaysw)
        date_here_ref = date_here_min
        while date_here_ref <= date_here_max:
            date_here_str = date_here_ref.strftime('%Y-%m-%d')
            year_str = date_here_ref.strftime('%Y')
            jjj_str = date_here_ref.strftime('%j')
            date_str = f'{year_str}{jjj_str}'
            name_in = input_name.replace('$DATE$', date_str)
            # input_file = os.path.join(input_path, year_str, jjj_str, f'C{year_str}{jjj_str}_chl-arc-4km.nc')
            input_file = os.path.join(input_path, year_str, jjj_str, name_in)
            if os.path.exists(input_file):
                date_list[date_here_str] = {'input_file': input_file, 'input_array': None}
            else:
                date_list[date_here_str] = {'input_file': None, 'input_array': None}
            if args.verbose:
                print(f'[INFO]  Date: {date_here_str} File: {input_file}')
            date_here_ref = date_here_ref + timedelta(days=1)
    if args.verbose:
        print(f'[INFO] Getting file list: Completed')
    return date_list

def start_data_temporal_file(output_file, ny, nx, ntimes):
    try:
        dout = Dataset(output_file, 'w', format='NETCDF4')
    except PermissionError:
        return None

    print(f'Starting file:{output_file}')

    dout.createDimension('y', ny)  # ny
    dout.createDimension('x', nx)  # nx
    dout.createDimension('time', ntimes)
    dout.createVariable('DATA', 'f4', ('time', 'y', 'x'), fill_value=-999, zlib=True, complevel=6)
    dout.createVariable('WEIGHTS', 'f4', ('time', 'y', 'x'), fill_value=-999, zlib=True, complevel=6)
    return dout

def start_results_temporal_file(output_file, ny, nx):
    try:
        dout = Dataset(output_file, 'w', format='NETCDF4')
    except PermissionError:
        return None

    print(f'Starting file:{output_file}')

    # dout.createDimension('time', ntimes)
    dout.createDimension('y', ny)  # ny
    dout.createDimension('x', nx)  # nx
    dout.createVariable('W_AVG', 'f4', ('y', 'x'), fill_value=-999, zlib=True, complevel=6)
    dout.createVariable('N_AVG', 'f4', ('y', 'x'), fill_value=-999, zlib=True, complevel=6)
    dout.createVariable('DIFF_N_W', 'f4', ('y', 'x'), fill_value=-999, zlib=True, complevel=6)
    dout.createVariable('N_STD', 'f4', ('y', 'x'), fill_value=-999, zlib=True, complevel=6)
    dout.createVariable('W_STD', 'f4', ('y', 'x'), fill_value=-999, zlib=True, complevel=6)
    dout.createVariable('SUM_W', 'f4', ('y', 'x'), fill_value=-999, zlib=True, complevel=6)
    dout.createVariable('PORC_W', 'f4', ('y', 'x'), fill_value=-999, zlib=True, complevel=6)
    dout.createVariable('MEDIAN', 'f4', ('y', 'x'), fill_value=-999, zlib=True, complevel=6)
    dout.createVariable('MIN', 'f4', ('y', 'x'), fill_value=-999, zlib=True, complevel=6)
    dout.createVariable('MAX', 'f4', ('y', 'x'), fill_value=-999, zlib=True, complevel=6)
    dout.createVariable('NOFOBS', 'f4', ('y', 'x'), fill_value=-999, zlib=True, complevel=6)

    # dout.createVariable('DATA_WEIGHT', 'f4', ('time', 'y', 'x'), fill_value=-999, zlib=True, complevel=6)
    # dout.createVariable('DATA_COMPUTATION', 'f4', ('time', 'y', 'x'), fill_value=-999, zlib=True, complevel=6)

    return dout

def start_climatology_temporal_file(output_file, file_ref):
    from arc_mapinfo import ArcMapInfo
    ami = ArcMapInfo(None, False)
    if file_ref is not None:
        ami.ifile_base = file_ref
    datasetout = ami.copy_nc_base(output_file)
    if 'W_AVG' not in datasetout.variables:
        if args.verbose:
            print('[INFO] Creating W_AVG variable...')
        var = datasetout.createVariable('W_AVG', 'f4', ('time', 'y', 'x'), fill_value=-999, zlib=True,
                                        complevel=6)
        var[:] = -999.0
    if 'N_AVG' not in datasetout.variables:
        if args.verbose:
            print('[INFO] Creating N_AVG variable...')
        var = datasetout.createVariable('N_AVG', 'f4', ('time', 'y', 'x'), fill_value=-999, zlib=True,
                                        complevel=6)
        var[:] = -999.0
    if 'DIFF_N_W' not in datasetout.variables:
        if args.verbose:
            print('[INFO] Creating DIFF_N_W variable...')
        var = datasetout.createVariable('DIFF_N_W', 'f4', ('time', 'y', 'x'), fill_value=-999, zlib=True,
                                        complevel=6)
        var[:] = -999.0

    if 'SUM_W' not in datasetout.variables:
        if args.verbose:
            print('[INFO] Creating SUM_W variable...')
        var = datasetout.createVariable('SUM_W', 'f4', ('time', 'y', 'x'), fill_value=-999, zlib=True,
                                        complevel=6)
        var[:] = -999.0

    if 'STD' not in datasetout.variables:
        if args.verbose:
            print('[INFO] Creating STD variable...')
        var = datasetout.createVariable('STD', 'f4', ('time', 'y', 'x'), fill_value=-999, zlib=True,
                                        complevel=6)
        var[:] = -999.0

    if 'MEDIAN' not in datasetout.variables:
        if args.verbose:
            print('[INFO] Creating MEDIAN variable...')
        var = datasetout.createVariable('MEDIAN', 'f4', ('time', 'y', 'x'), fill_value=-999, zlib=True,
                                        complevel=6)
        var[:] = -999.0

    if 'MIN' not in datasetout.variables:
        if args.verbose:
            print('[INFO] Creating MIN variable...')
        var = datasetout.createVariable('MIN', 'f4', ('time', 'y', 'x'), fill_value=-999, zlib=True,
                                        complevel=6)
        var[:] = -999.0

    if 'MAX' not in datasetout.variables:
        if args.verbose:
            print('[INFO] Creating MAX variable...')
        var = datasetout.createVariable('MAX', 'f4', ('time', 'y', 'x'), fill_value=-999, zlib=True,
                                        complevel=6)
        var[:] = -999.0

    if 'PORC_W' not in datasetout.variables:
        if args.verbose:
            print('[INFO] Creating PORC_W variable...')
        var = datasetout.createVariable('PORC_W', 'f4', ('time', 'y', 'x'), fill_value=-999, zlib=True,
                                        complevel=6)
        var[:] = -999.0

    if 'N_YEARS' not in datasetout.variables:
        if args.verbose:
            print('[INFO] Creating N_YEARS variable...')
        var = datasetout.createVariable('N_YEARS', 'f4', ('time', 'y', 'x'), fill_value=-999, zlib=True,
                                        complevel=6)
        var[:] = -999.0

    if 'N_OBS' not in datasetout.variables:
        if args.verbose:
            print('[INFO] Creating N_OBS variable...')
        var = datasetout.createVariable('N_OBS', 'f4', ('time', 'y', 'x'), fill_value=-999, zlib=True,
                                        complevel=6)
        var[:] = -999.0

    if 'N_DAYS' not in datasetout.variables:
        if args.verbose:
            print('[INFO] Creating N_DAYS variable...')
        var = datasetout.createVariable('N_DAYS', 'f4', ('time', 'y', 'x'), fill_value=-999, zlib=True,
                                        complevel=6)
        var[:] = -999.0

    return datasetout

# first dimension is time
def get_ny_nx(input_file, variable):
    dataset = Dataset(input_file)
    variable = dataset.variables[variable]
    ny = variable.shape[1]
    nx = variable.shape[2]
    return ny, nx

def get_limits(y, x, ystep, xstep, ny, nx):
    yini = y
    xini = x
    yfin = y + ystep
    xfin = x + xstep
    if yfin > ny:
        yfin = ny
    if xfin > nx:
        xfin = nx
    limits = [yini, yfin, xini, xfin]
    return limits

def test_compute_avg(input_file, variable):
    # array_avg, array_avg_w = test_compute_avg(
    #     '/mnt/c/DATA_LUIS/OCTAC_WORK/ARC_MULTI_CLIMATOLOGY/2000/195/C2000195_chl-arc-4km.nc', 'CHL')
    #
    # print('==============', avg[615, 1364])
    # print('==============', weighted_avg[615, 1364])
    # print('++++++++++++++', array_avg[615, 1364])
    # print('++++++++++++++', array_avg_w[615, 1364])
    #
    # check_avg = np.divide(avg, array_avg)
    # check_avg_w = np.divide(weighted_avg, array_avg_w)
    # print('CHECKING: ')
    # print(np.nanmean(check_avg))
    # print(np.nanmean(check_avg_w))

    dataset = Dataset(input_file)
    input_data = np.array(dataset.variables[variable])
    dataset.close()
    ny = 1375
    nx = 1375
    array_avg = np.zeros((1375, 1375))
    array_avg_w = np.zeros((1375, 1375))
    mini_weigths = np.array([[0.25, 0.5, 0.25], [0.5, 1, 0.5], [0.25, 0.5, 0.25]])
    for y in range(ny):
        # print(y)
        for x in range(nx):
            yini = y - 1
            yfin = y + 2
            xini = x - 1
            xfin = x + 2
            yiniw = 0
            yfinw = 3
            xiniw = 0
            xfinw = 3
            if y == 0:
                yini = 0
                yiniw = 1
            if x == 0:
                xini = 0
                xiniw = 1
            if y == (ny - 1):
                yfin = ny
                yfinw = 2
            if x == (nx - 1):
                xfin = nx
                xfinw = 2

            mini_array = input_data[0, yini:yfin, xini:xfin]
            mini_array[mini_array == -999] = np.nan

            mini_weigths_w = mini_weigths[yiniw:yfinw, xiniw:xfinw]

            mini_weigths_w[mini_array == -999] = np.nan

            mini_array_w = np.multiply(mini_array, mini_weigths_w)
            array_avg_w[y, x] = np.nansum(mini_array_w) / np.nansum(mini_weigths_w)

            array_avg[y, x] = np.nanmean(mini_array)

    return array_avg, array_avg_w

def set_data_window_3x3(data_computation, input_file, variable, indices, spatial_weights, input_data):
    time_min = indices[0]
    yref = indices[1]
    ny = indices[2]
    xref = indices[3]
    nx = indices[4]
    ny_abs = indices[5]
    nx_abs = indices[6]

    if input_data is None:
        dataset = Dataset(input_file)
        input_data = np.array(dataset.variables[variable])
        dataset.close()

    limit_output = False
    if data_computation.shape[1] == ny and data_computation.shape[2] == nx and ny_abs != ny and nx_abs != nx:
        limit_output = True

    # 0,0
    xoutput = xref
    youtput = yref
    nyhere = ny
    nxhere = nx
    xinput = xoutput - 1
    yinput = youtput - 1
    if xoutput == 0:
        xoutput = 1
        xinput = 0
        nxhere = nxhere - 1
    if youtput == 0:
        youtput = 1
        yinput = 0
        nyhere = nyhere - 1
    if limit_output:
        if youtput > 1:
            youtput = 0
        if xoutput > 1:
            xoutput = 0
    data_computation[time_min + 0, youtput:youtput + nyhere, xoutput:xoutput + nxhere] = input_data[0,
                                                                                yinput:yinput + nyhere,
                                                                                xinput:xinput + nxhere]
    # 0,1
    xoutput = xref
    youtput = yref
    nxhere = nx
    nyhere = ny
    xinput = xoutput
    yinput = youtput - 1
    if youtput == 0:
        youtput = 1
        yinput = 0
        nyhere = nyhere - 1
    if limit_output:
        xoutput = 0
        if youtput > 1:
            youtput = 0
    data_computation[time_min + 1, youtput:youtput + nyhere, xoutput:xoutput + nxhere] = input_data[0,
                                                                                yinput:yinput + nyhere,
                                                                                xinput:xinput + nxhere]
    # 0,2
    xoutput = xref
    youtput = yref
    nxhere = nx
    nyhere = ny
    xinput = xoutput + 1
    yinput = youtput - 1
    if youtput == 0:
        youtput = 1
        yinput = 0
        nyhere = nyhere - 1
    if (xoutput + nx) == nx_abs:
        nxhere = nxhere - 1
    if limit_output:
        if youtput > 1:
            youtput = 0
        xoutput = 0
    data_computation[time_min + 2, youtput:youtput + nyhere, xoutput:xoutput + nxhere] = input_data[0,
                                                                                yinput:yinput + nyhere,
                                                                                xinput:xinput + nxhere]
    # 1,0
    xoutput = xref
    youtput = yref
    nxhere = nx
    nyhere = ny
    xinput = xoutput - 1
    yinput = youtput
    if xoutput == 0:
        xoutput = 1
        xinput = 0
        nxhere = nxhere - 1
    if limit_output:
        if xoutput > 1:
            xoutput = 0
        youtput = 0
    data_computation[time_min + 3, youtput:youtput + nyhere, xoutput:xoutput + nxhere] = input_data[0,
                                                                                yinput:yinput + nyhere,
                                                                                xinput:xinput + nxhere]
    # 1,1
    xoutput = xref
    youtput = yref
    nxhere = nx
    nyhere = ny
    xinput = xoutput
    yinput = youtput
    if limit_output:
        xoutput = 0
        youtput = 0
    data_computation[time_min + 4, youtput:youtput + nyhere, xoutput:xoutput + nxhere] = input_data[0,
                                                                                yinput:yinput + nyhere,
                                                                                xinput:xinput + nxhere]
    # 1,2
    xoutput = xref
    youtput = yref
    nxhere = nx
    nyhere = ny
    xinput = xoutput + 1
    yinput = youtput
    if (xoutput + nx) == nx_abs:
        nxhere = nxhere - 1
    if limit_output:
        xoutput = 0
        youtput = 0
    data_computation[time_min + 5, youtput:youtput + nyhere, xoutput:xoutput + nxhere] = input_data[0,
                                                                                yinput:yinput + nyhere,
                                                                                xinput:xinput + nxhere]
    # 2,0
    xoutput = xref
    youtput = yref
    nxhere = nx
    nyhere = ny
    xinput = xoutput - 1
    yinput = youtput + 1
    if xoutput == 0:
        xoutput = 1
        xinput = 0
        nxhere = nxhere - 1
    if (youtput + ny) == ny_abs:
        nyhere = nyhere - 1
    if limit_output:
        if xoutput > 1:
            xoutput = 0
        youtput = 0
    data_computation[time_min + 6, youtput:youtput + nyhere, xoutput:xoutput + nxhere] = input_data[0,
                                                                                yinput:yinput + nyhere,
                                                                                xinput:xinput + nxhere]
    # 2,1
    xoutput = xref
    youtput = yref
    nxhere = nx
    nyhere = ny
    xinput = xoutput
    yinput = youtput + 1
    if (youtput + ny) == ny_abs:
        nyhere = nyhere - 1
    if limit_output:
        xoutput = 0
        youtput = 0
    data_computation[time_min + 7, youtput:youtput + nyhere, xoutput:xoutput + nxhere] = input_data[0,
                                                                                yinput:yinput + nyhere,
                                                                                xinput:xinput + nxhere]
    # 2,2
    xoutput = xref
    youtput = yref
    nxhere = nx
    nyhere = ny
    xinput = xoutput + 1
    yinput = youtput + 1
    if (xoutput + nx) == nx_abs:
        nxhere = nxhere - 1
    if (youtput + ny) == ny_abs:
        nyhere = nyhere - 1
    if limit_output:
        xoutput = 0
        youtput = 0
    data_computation[time_min + 8, youtput:youtput + nyhere, xoutput:xoutput + nxhere] = input_data[0,
                                                                                yinput:yinput + nyhere,
                                                                                xinput:xinput + nxhere]
    weighted_avg = None
    if spatial_weights is not None:
        data_avg = data_computation[time_min:time_min + 9, yref:yref + ny, xref:xref + nx]
        data_avg[data_avg == -999] = np.nan
        spatial_weights[np.isnan(data_avg)] = np.nan
        weighted_data = np.multiply(data_avg, spatial_weights)
        weighted_data_sum = np.nansum(weighted_data, axis=0)
        spatial_weights_sum = np.nansum(spatial_weights, axis=0)
        weighted_avg = weighted_data_sum / spatial_weights_sum

    return data_computation, weighted_avg

def get_temporal_weight_filter(options, temporal_window, ny, nx):
    temporal_weights = np.ones((temporal_window, ny, nx))
    increm = int(temporal_window / 2)
    wsum = 0
    for idx, ir in zip(range(temporal_window), range(increm * (-1), increm + 1)):
        weight = ((increm + 1) - np.abs(ir)) / (increm + 1)
        temporal_weights[idx, :, :] = weight
        wsum = wsum + weight

    if ny == 1 and nx == 1:
        temporal_weights = temporal_weights.flatten()

    return temporal_weights

def get_spatial_weight_filter_3x3(options, ny, nx):
    spatial_weights = np.ones((9, ny, nx))
    if options is None:
        spatial_weights[0, :, :] = 0.25
        spatial_weights[1, :, :] = 0.5
        spatial_weights[2, :, :] = 0.25
        spatial_weights[3, :, :] = 0.5
        spatial_weights[4, :, :] = 1.0
        spatial_weights[5, :, :] = 0.5
        spatial_weights[6, :, :] = 0.25
        spatial_weights[7, :, :] = 0.5
        spatial_weights[8, :, :] = 0.25
    if ny == 1 and nx == 1:
        spatial_weights = spatial_weights.flatten()
    return spatial_weights

def mainV2():
    # ##version 2 DEPRECATED
    # date_list = get_input_file_list_day(date_here, temporal_window, input_path, year_ini, year_end)
    # for y in range(0, ny, nywindow):
    #     for x in range(0, nx, nxwindow):
    #         limits = get_limits(y, x, nywindow, nxwindow, ny, nx)
    #         nywindow_here = limits[1] - limits[0]
    #         nxwindow_here = limits[3] - limits[2]
    #         options_clim_here = options_clim.copy()
    #         options_clim_here['nywindow'] = nywindow_here
    #         options_clim_here['nxwindow'] = nxwindow_here
    #         options_clim_here['yref'] = limits[0]
    #         options_clim_here['xref'] = limits[2]
    #
    #         make_clim_multi_day_v2(input_path, output_path, date_here, options_clim_here, date_list, ny, nx)
    #
    # date_here_str = options[ref]['date']
    # date_here = dt.strptime(date_here_str, '%Y-%m-%d')
    return
def make_clim_multi_day_v2(input_path, output_path, date_here, options, date_list, ny, nx):
    if options is None:
        temporal_window = 11
        spatial_window = 3
        xref = 0
        yref = 0
        nywindow = 100
        nxwindow = 100
        use_weights = True
        variable = 'CHL'
        ny = -1
        nx = -1
        file_ref = '/mnt/c/DATA_LUIS/OCTAC_WORK/ARC_TEST/MULTI/GRID_FILES/ArcGrid_65_90_4KM_GridBase.nc'
        year_ini = 1998
        year_end = 2022
    else:
        temporal_window = options['temporal_window']  # 11
        spatial_window = options['spatial_window']  # 3
        nywindow = options['nywindow']
        nxwindow = options['nxwindow']
        use_weights = options['use_weights']
        variable = options['variable']
        ny = options['ny']
        nx = options['nx']
        file_ref = options['file_ref']
        year_ini = options['year_ini']  # 1998
        year_end = options['year_end']  # 2022
        yref = options['yref']
        xref = options['xref']
    input_name = options['input_name']
    if args.verbose:
        print(f'[INFO]**********************************************************')
        print(f'[INFO] YRef: {yref} XRef: {xref}  NYWindow: {nywindow} NXWindow: {nxwindow}')
        print(f'[INFO] Temporal window: {temporal_window} Spatial window: {spatial_window}')
    if date_list is None:
        date_list = get_input_file_list_day(date_here, temporal_window, input_path, year_ini, year_end, input_name)
    if ny == -1 and nx == -1:
        for date_str in date_list:
            input_file = date_list[date_str]['input_file']
            if input_file is not None:
                ny, nx = get_ny_nx(input_file, variable)
    nyears = (year_end - year_ini) + 1
    ndata = nyears * temporal_window * spatial_window * spatial_window

    data_computation = np.zeros((ndata, nywindow, nxwindow))
    data_weights = np.ones((ndata, nywindow, nxwindow))
    if use_weights:
        if args.verbose:
            print('[INFO] Retrieving weights...')
        spatial_weights = get_spatial_weight_filter_3x3(None, 1, 1)
        temporal_weights = get_temporal_weight_filter(None, temporal_window, 1, 1)
        index = 0
        for iyear in range(nyears):
            for itemporal in range(len(temporal_weights)):
                for ispatial in range(len(spatial_weights)):
                    data_weights[index, :, :] = spatial_weights[ispatial] * temporal_weights[itemporal]
                    index = index + 1

    if args.verbose:
        print('[INFO] Getting data...')
    data_computation[:] = -999.0
    itime = 0
    idate = 0
    year_ref = -1
    for date_str in date_list:
        date_h = dt.strptime(date_str, '%Y-%m-%d')
        year = date_h.year
        iyear = year - year_ini
        input_file = date_list[date_str]['input_file']
        input_data = None
        # input_data = date_list[date_str]['input_array']
        # if input_data is None:
        #     dataset = Dataset(input_file)
        #     input_data = np.array(dataset.variables[variable])
        #     dataset.close()
        if args.verbose and year != year_ref:
            year_ref = year
            print(
                f'[INFO] Date: {date_str} Index all: {itime}  Index date: {idate}  Index year: {iyear} Input file: {input_file}')
        if input_file is None:
            itime = itime + (spatial_window * spatial_window)
            idate = idate + 1
            continue

        # print(itime,yref,nywindow,xref,nxwindow,ny,nx)
        indices = [itime, yref, nywindow, xref, nxwindow, ny, nx]
        data_computation, weighted_avg = set_data_window_3x3(data_computation, input_file, variable, indices, None,
                                                             input_data)
        itime = itime + (spatial_window * spatial_window)
        idate = idate + 1

    # data_computation[data_computation == -999.0] = np.nan
    # data_weights[data_computation == -999.0] = np.nan
    #
    #
    # # r, c = np.unravel_index(np.nanargmax(std), std.shape)
    # # print(std[r, c])
    # # print(data_computation[:, r, c])
    #
    # # avg = np.nanmean(data_computation[:, :, :], axis=0)
    # std = np.std(data_computation[:, :, :], axis=0)
    # median = np.median(data_computation[:, :, :], axis=0)
    #
    # prod = data_computation * data_weights
    # weighted_avg = np.sum(prod) / np.sum(data_weights)
    # nofobs = np.count_nonzero(~np.isnan(data_computation), axis=0)
    #
    # # min_array = np.nanmin(data_computation[:, :, :], axis=0)
    # # max_array = np.nanmax(data_computation[:, :, :], axis=0)

    if args.verbose:
        print('[INFO] COMPLETED')

if __name__ == '__main__':
    main()


from .forms import FormContact
from django.template.loader import render_to_string
from django.core.mail import send_mail
from django.conf import settings
from django.contrib import messages
from django.shortcuts import redirect
from django.shortcuts import render

def contact(request):
    if request.method == 'POST':
        form = FormContact(request.POST)
        if form.is_valid():
            form.save()

            template_data = {
                'first_name': form.cleaned_data['first_name'],
                # дополнить другие данные при желании
            }
            msg = render_to_string('email_message/email_answer.html', template_data)
            subject = 'Ответ на заявку'
            sender = settings.EMAIL_HOST_USER
            recipient = form.cleaned_data['email']
            send_mail(subject, msg, sender, [recipient], html_message=msg)

            messages.success(request, "Спасибо за вашу заявку. Ожидайте.")
            form = FormContact()
    else:
        form = FormContact()
    context= {'form': form}
    return context


"""Write a Python program to check whether an element exists within a tuple"""

x=('10','20','30','test','40','50','60','70','80','90')
n=input('Enter the element you want to search in the tuple-')
for i in range(len(x)):
    if n in x:
        print('Element found in tuple')
        break
    if n not in x:
        print('Element not found in tuple')
        break


# ls = [x if (condition) else None for x in ls]

# expression1 if condition else expression2

items = [2, 12, 45, 23, 41, 59, 52, 67, 90, 112, 33]

new_items = [x*0.85 if x < 25 else x for x in items]

print(new_items)

lst = []
for x in items:
    if x < 25:
        x *= 0.85
    else:
        x = x
    lst.append(x)
print(lst)


#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
@CreateTime    : 2019/6/27
@Author  : Easou Chen
@file:  share_view
@for :  这个是公用的view, 简单一些的post接口，可以直接使用本view
"""
from django.utils.timezone import now
from rest_framework.response import Response
from rest_framework.views import APIView
from server_info.models import ServerInfo

class ShareView(APIView):
    def post(self, request):
        rd = {'error_code': '200', 'error_message': '请求成功'}
        data = request.data
        my_model = self.get_model()
        if "id" not in data.keys():
            my_model.objects.get_or_create(**data)
        else:
            data = my_model.objects.filter(**data)
            if data.exists():
                rd['error_code'] = '4000'
                rd['error_message'] = '数据已存在，无需更新'
            else:
                data['update_time'] = now()
                my_model.objects.filter(id=data.pop('id')).update(**data)
        return Response(rd)

    def get_model(self, model=None):
        return model


import zope.component
from zope.app.component import hooks
from zope.app.zopeappgenerations import getRootFolder
from zope.app.generations.utility import findObjectsProviding
import zc.relation
from zope.dublincore.interfaces import IWriteZopeDublinCore

from quotationtool.site.interfaces import IQuotationtoolSite
from quotationtool.commentary.aboutreference import CommentAboutReferenceContainer, dump, load
from quotationtool.commentary.interfaces import ICommentAboutReferenceContainer, ICommentAboutReference

def evolve(context):
    """ Add a commentary folder and its relation catalog."""
    root = getRootFolder(context)

    site = None
    for s in findObjectsProviding(root, IQuotationtoolSite):
        site = s
        break
    if site is None: raise Exception('No quotationtool site')
    hooks.setSite(site)

    sm = site.getSiteManager()

    container = site['aboutreferences'] = CommentAboutReferenceContainer()
    sm.registerUtility(container, ICommentAboutReferenceContainer)

    IWriteZopeDublinCore(container).title = u"Comments about References"

    IWriteZopeDublinCore(container).description = u"""Comments about references in the referatory are stored here."""

    cat = sm['default']['commentaboutreference_relation_catalog'] = zc.relation.catalog.Catalog(
        dump, load)
    cat.addValueIndex(
        ICommentAboutReference['reference'],
        dump, load)

    sm.registerUtility(cat, zc.relation.interfaces.ICatalog,
                       name = 'commentsaboutreferences')
1


import argparse
import cv2
from datetime import datetime
import os
from PixelImageDifference import ImagePixelDifference
from HistogramImageDifference import ImageHistogramDifference
from SafetyDetection import fireDetection,smokeDetection

def get_Video_Capture_and_Write(outputname):
    outputname = getFileName() if outputname == "output" else outputname
    fourcc = cv2.VideoWriter_fourcc(*'XVID')
    video_writer = cv2.VideoWriter("output.avi", fourcc, 20.0, (640, 480))
    cap = cv2.VideoCapture(0)
    return cap,video_writer

def getFileName():
    now = datetime.now()
    dt_value = now.strftime("%d/%m/%Y %H:%M%S")
    return dt_value

def process_video(algorithm,outputname,threshold,timerlevel,reference_image,video_link):
    if(algorithm == "historgram"):
        IDif = ImageHistogramDifference(reference_image,threshold=threshold,timerlevel=timerlevel)
    else:
        IDif = ImagePixelDifference(reference_image,threshold=threshold,timerlevel=timerlevel)
    fourcc = cv2.VideoWriter_fourcc(*'XVID')
    video_writer = cv2.VideoWriter("{}.avi".format(outputname), fourcc, 20.0, (640, 480))
    cap = cv2.VideoCapture(video_link)
    frame_count = 0
    the_actual_frame_count = 0

    try:
        while cap.isOpened():

            ret, frame = cap.read()
            cv2.imshow('frame', frame)
            print(f"The actual frame count {the_actual_frame_count} the current frame count {frame_count}")
            if IDif.difference(frame=frame):
                video_writer.write(frame)
                frame_count +=1
            the_actual_frame_count+=1
            if cv2.waitKey(10) & 0xFF == ord('q'):
                break
        cap.release()
        video_writer.release()
        cv2.destroyAllWindows()
    except Exception as e:
        print()

def run(vsource = 0,outputname = "output",threshold = 1000,timerlevel = 60,reference_image = "",video_link="",algorithm="pixel"):
    threshold = float(threshold[0])
    timerlevel = int(timerlevel[0])
    if(video_link == "" ):
        if(algorithm == "historgram"):
            IDif = ImageHistogramDifference(reference_image,threshold=threshold,timerlevel=timerlevel)
        else:
            IDif = ImagePixelDifference(reference_image,threshold=threshold,timerlevel=timerlevel)
        fourcc = cv2.VideoWriter_fourcc(*'XVID')
        video_writer = cv2.VideoWriter("{}.avi".format(outputname), fourcc, 20.0, (640, 480))
        cap = cv2.VideoCapture(vsource)
        frame_count = 0
        the_actual_frame_count = 0
        try:
            while cap.isOpened():
                ret, frame = cap.read()
                cv2.imshow('frame', frame)
                print(f"The actual frame count {the_actual_frame_count} the current frame count {frame_count}")
                if IDif.difference(frame=frame):
                    video_writer.write(frame)
                    frame_count +=1
                the_actual_frame_count+=1
                if cv2.waitKey(10) & 0xFF == ord('q'):
                    break
            cap.release()
            video_writer.release()
            cv2.destroyAllWindows()
        except Exception as e:
            print(e)
    else:
        process_video(algorithm,outputname,threshold,timerlevel,reference_image,video_link)

def parse_opt():
    parser = argparse.ArgumentParser()
    parser.add_argument("--vsource",nargs= '+',type= int,default=0,help= "Enter the Video camara value")
    parser.add_argument("--outputname",default= "output",help= "Enter the name of the file name ")
    parser.add_argument("--threshold",nargs= '+',type= float,default=1000.0,help= "Enter the threshold value")
    parser.add_argument("--timerlevel",nargs= '+',type=int,default=60,help= "Enter the timer level to compare")
    parser.add_argument("--reference_image",default="",help="Enter the address of the reference image")
    parser.add_argument("--video_link",default="",help="Enter Video Addrress")
    parser.add_argument("--algorithm",default="pixel",help="Enter suitable algorithm")
    opt = parser.parse_args()
    return opt

def main(opt):
    run(**(vars(opt)))

if __name__ == "__main__":
    opt = parse_opt()
    main(opt)


"""
This script uses PyAutoGui to control the mouse and keyboard and place manual orders in the ERP system

Author: Mauricio Montilla
"""

from datetime import datetime
import time

try:
    import pandas as pd
    import numpy as np
    import pyautogui as bot
    import keyboard

except:
    # In case that an external package is missing, the script will install them
    from pip._internal import main as pip
    pip(['install', 'pandas'])
    pip(['install', 'numpy'])
    pip(['install', 'pyautogui'])
    pip(['install', 'keyboard'])

    import pandas as pd
    import numpy as np
    import pyautogui as bot
    import keyboard

# Remove warnings
pd.options.mode.chained_assignment = None
time.sleep(1)

# Locates the center of the image that it gets
def bot_control(image, conf, grayscale=True):
    cords = None
    counter = 0

    while cords == None:
        cords = bot.locateCenterOnScreen(
            image, grayscale=grayscale, confidence=conf)

        # Wait five seconds, if we done find the image break
        counter += 1
        if counter == 5: break

    if cords != None:
        bot.moveTo(cords, duration=2), time.sleep(1)
    else:
        print("Mario cannot find " + image)

    return cords

# Updates the promised weeks accordingly
def create_order_line(pn, quantity, week, day, wait):
    bot.press("ctrl"), time.sleep(wait)
    bot.typewrite(pn[-18:]), time.sleep(wait)
    bot.press("tab"), time.sleep(wait)
    bot.typewrite(quantity[-8:]), time.sleep(wait)
    bot.press("tab"), time.sleep(wait)
    bot.typewrite(week[-4:]), time.sleep(wait)
    bot.press("tab"), time.sleep(wait)
    bot.typewrite(day[-1:]), time.sleep(wait)
    bot.press("enter", presses=2), time.sleep(wait)
    bot.press("ctrl"), time.sleep(wait)

# Files locations
main_path = '\\\\SW03000.se.hvwan.net\\Dept\\P\\PSA\\Inventory Management\\Python\\'
image_1 = main_path + "images\\G01_SE\\8_inside_G01.png"
image_2 = main_path + "images\\G01_SE\\9_inside_A01.png"

# Current week to filter the dataframes
now = datetime.now()
current_week = int(str(now.year)[-2:] + ('0' + str(now.isocalendar()[1]))[-2:])

# Get data coming from the VBA array
df = pd.read_csv(main_path + "csvFile.csv", encoding="ISO-8859-1")
df = df.replace(np.nan, 0)

# Make sure that G01 is open as expected
if bot_control(image_1, 0.7) != None:
    bot.click(), time.sleep(1)
    bot.press("enter"), time.sleep(1)
    bot.press("ctrl"), time.sleep(1)

# if it is A01
else:
    print("Please open Rex in the G01 screen before executing the script.")
    print("Press 'End' and try again. This script ends here.")

    # Allow the use to read the problem
    while keyboard.is_pressed('end') != True:
        time.sleep(2)
    exit()

for line in range(len(df['Pn'])):

    # Prepare the pn
    pn = int(df['Pn'].iloc[line])
    # Prepare the quantity
    quantity = int(df['Qty.'].iloc[line])

    # Prepare the week requested, the week requested needs to be a valid one
    week_req = int(df['Week Req.'].iloc[line])

    if (current_week + 1) >= week_req:
        week_req = 0
        df['Day Req.'].iloc[line] = 0

    # Prepare the day requested, the day requested needs to be a valid one
    day_req = int(df['Day Req.'].iloc[line])

    if day_req > 5:
        day_req = 5

    # Fit the variables to Rex size
    pn = '000000000' + str(pn)
    quantity = '00000000' + str(quantity)
    week_req = '000000000' + str(week_req)
    day_req = '000000000' + str(day_req)

    # Include only lines which are vlid
    if int(pn) > 0 and int(quantity) > 0:
        create_order_line(pn, quantity, week_req, day_req, 1)

    # End the loop, if Bowser is not in the right place
    counter = 0

    while bot.locateCenterOnScreen(image_1, confidence=0.7) == None:
        # Wait 5 seconds, if we done find the image break
        counter += 1
        if counter ==5: exit()

        time.sleep(1)

    # Follow up advance of the script
    print(line, len(df['Pn']), int(pn), int(quantity))

# Finish the loop
bot.press("tab", presses=6), time.sleep(1)
bot.typewrite("1")


import re

a = re.sub(r'hello', 'i love the', 'hello world')
print(a)

s = '321asdf12 asdff21 wewreqr 1 asdfrew asfdfasd32r    32 321 fads  3142   ` fdas12        r       3r434 12 #$#$ 5  %%  34 1 F SA'
s = s.split()
print(s)
s = re.sub(r'(\d+)', '', s)
s = re.sub(r'(^a-zA-Z)', '', s)

print(s)


#%%

from utils.dataset_utils import CCS_Dataset
from utils.probing_utils import ModelActs
from utils.model_utils import vicuna_7b
from datasets import load_dataset, Dataset

#%%

import importlib

import utils.dataset_utils
importlib.reload(utils.dataset_utils)
from utils.dataset_utils import CCS_Dataset

import utils.probing_utils
importlib.reload(utils.probing_utils)
from utils.probing_utils import ModelActs

import utils.model_utils
importlib.reload(utils.model_utils)
from utils.model_utils import vicuna_7b

# For external libraries, like `datasets`, you may or may not need to reload
# it depends on whether you've made local changes
import datasets
importlib.reload(datasets)
from datasets import load_dataset, Dataset

#%%
label_dict = {
    "imdb": ["negative", "positive"], # This is for normal IMDB
    "amazon_polarity": ["negative", "positive"],
    "ag_news": ["politics", "sports", "business", "technology"],
    "dbpedia_14": ["company", "educational institution", "artist", "athlete", "office holder", "mean of transportation", "building", "natural place", "village", "animal",  "plant",  "album",  "film",  "written work"],
    "copa": ["choice 1", "choice 2"],
    "rte": ["yes", "no"],   # whether entail
    "boolq": ["false", "true"],
    "qnli": ["yes", "no"],  # represent whether entail
    "piqa": ["solution 1", "solution 2"],
    "story-cloze": ["choice 1", "choice 2"],
}

def format_prompt(label, text, text1, text2, dataset_name = "imdb"):
    """
    Given an imdb example ("text") and corresponding label (0 for negative, or 1 for positive),
    returns a zero-shot prompt for that example (which includes that label as the answer).

    (This is just one example of a simple, manually created prompt.)
    """
    if dataset_name == "imdb":
        return "The following movie review expresses a " + label_dict[dataset_name][label] + " sentiment:\n" + text
    if dataset_name == "amazon_polarity":
        return "The following Amazon review expresses a " + label_dict[dataset_name][label] + " sentiment:\n" + text
        # text = title and content
    if dataset_name == "ag_news":
        return "The topic of the following news article is about " + label_dict[dataset_name][label] + ":\n" + text
    if dataset_name == "dbpedia_14":
        return "The following article relates to " + label_dict[dataset_name][label] + "s:\n" + text
        # text = title and content
    if dataset_name == "copa":
        return f'Story: {text} \nIn this story, out of "{text1}" and "{text2}", the sentence is most likely to follow is {["the former", "the latter"][label]}.'
        # text = premise. text1 and text2 are choice1 choice2
    if dataset_name == "rte":
        return f"Passage: {text}\nQuestion: Does this imply that {text1}?\nAnswer here: {['Yes', 'No'][label]}."
        # text = premise
        # text1 = hypothesis
    if dataset_name == "boolq":
        return f"{text}\nQuestion: {text1}? {['Yes', 'No'][label]}"
        # text = passage
        # text1 = question
    if dataset_name == "qnli":
        return f"Question: {text}\nAnswer: {text1}\nDoes the information in the provided answer help completely the question? {['Yes', 'No'][label]}"
        # text = question
        # text1 = answer
    if dataset_name == "piqa":
        return f"Which choice makes the most sense? \nQuestion: {text}\nChoice 1: {text1}\nChoice 2: {text2}\nAnswer here: {['Choice 1', 'Choice 2'][label]}"
        # text = question
        # text1 = sol1
        # text2 = sol2
    if dataset_name == "story-cloze":
        return f"Which choice makes the most sense? \nStory: {text}\nContinuation 1: {text1}\nContinuation 2: {text2} \nAnswer here: {['Continuation 1', 'Continuation 2'][label]}"
        # text = context
        # text1 = sentence_quiz1
        # text2 = sentence_quiz2

#%%

model = vicuna_7b(device = "cuda:4")
dataset = load_dataset("imdb")["train"]

#%%
ccs_data = CCS_Dataset(label_dict, format_prompt, dataset, model.tokenizer)
probing_utils = ModelActs(model, ccs_data)

# %%

probing_utils.get_acts_pairs(N=100)
#%%
probing_utils.CCS_train(5, 10) # batch_size = 3

#%%

# find that pytorch module is bugged

import torch
import torch.nn as nn
torch.set_grad_enabled(True)

# Create a simple model
model = nn.Sequential(nn.Linear(10, 1), nn.Sigmoid()).to('cuda' if torch.cuda.is_available() else 'cpu')

# Create a simple input tensor
input_tensor = torch.randn(5, 10).to('cuda' if torch.cuda.is_available() else 'cpu')
input_tensor.requires_grad = True

# Pass the tensor through the model
output_tensor = model(input_tensor)

# Check if output tensor has requires_grad=True
print(output_tensor.requires_grad)

# %%

## REAL TODO
# CCS_inference -> test function. --> doing rn
# Make a function that does train & testing (inference).
# Make it faster by making it generate all activations beforehand.

# THEN, when everything works, rewrite & rethink function type signatures


# https://leetcode.com/problems/partition-list/

# Основная идея - сохранять два отдельных списка, а потом сцепить их

# Definition for singly-linked list.
class ListNode:
     def __init__(self, val=0, next=None):
         self.val = val
         self.next = next
class Solution:
    def partition(self, head: Optional[ListNode], x: int) -> Optional[ListNode]:

        # Всегда проверяй пустой случай
        if head is None:
            return head

        less_head, less_tail, more_head, more_tail = None, None, None, None
        current = head

        while True:
            if current.val < x:
                # надо прицепить к головному списку
                # а есть ли он?
                if less_head is None:
                    # потом надо будет вернуть указатель на голову
                    less_head = ListNode(current.val, None)
                    # хвост
                    less_tail = less_head
                # голова левого списка уже создана раньше
                else:
                    # значит "прицепим" хвост
                    less_tail.next = ListNode(current.val, None)
                    less_tail = less_tail.next
            else:
                if more_head is None:
                    more_head = ListNode(current.val, None)
                    more_tail = more_head
                else:
                    more_tail.next = ListNode(current.val, None)
                    more_tail = more_tail.next
            if current.next is None:
                break
            current = current.next

        if less_tail is None:
            return more_head

        less_tail.next = more_head
        return less_head

# testcases: [1], 2
# testcases: [1], 0
# testcases: [1, 2, 3], 5
# testcases: [1, 2, 3], 0
# testcases: [4, 5, 6, 1], 2


from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy import Column, Integer, String, Float

Base = declarative_base()

class EngineAndFuel(Base):
    __tablename__ = "engineandfuel"
    id_engine = Column('id_engine', Integer, primary_key=True)
    designation = Column('designation', Float, nullable=False)
    fuel = Column('fuel', String, nullable=False)

    def __init__(self, id_engine, designation, fuel):
        super().__init__()
        self.id_engine = id_engine
        self.designation = designation
        self.fuel = fuel


import os
import sys
import time
import string
import argparse
import subprocess

# -----------------------------------
# custom package
# -----------------------------------
import Drseqpipe

### tool function
from Drseqpipe.Utility      import          (sp,
                                   pdf_name,
                                   raise_error,
                                   wlog,
                                   readAnnotation,
                                   textformat,
                                   CMD
                                   )
### read and generate config file
from Drseqpipe.parse_config import (gen_conf,
                                   read_conf,
                                   make_conf
                                   )

# -------------------
# main step
# -------------------
from Drseqpipe.step0_integrate_data   import step0_integrate_data
from Drseqpipe.step1_generate_matrix         import step1_generate_matrix
from Drseqpipe.step3_QC    import step3_QC
from Drseqpipe.step4_analysis import step4_analysis
from Drseqpipe.step5_summary import step5_summary

# ------------------------------------
# Misc functions
# ------------------------------------

#### read options

class ChiLinParser(argparse.ArgumentParser):
    '''
    Ex version for argparse(parameter parser) , add raise error function .
    '''
    def error(self, message):
        sys.stderr.write('error: %s\n' % message)
        self.print_help()
        sys.exit()

def parse_args():
    '''
    Read parameter
    '''
    description = "Dr.seq -- a quality control and analysis pipeline for droplet sequencing"
    parser = ChiLinParser(description = description)
    sub_parsers = parser.add_subparsers(help = "sub-command help", dest = "sub_command")

    ### generate config file
    template_parser = sub_parsers.add_parser("gen",  help = "generate a template of config file",
                                             description = "Drseq config file generation. Usage: Drseq.py gen  -n config_name.conf")
    template_parser.add_argument("-n","--name", dest="config_name",required = True,help="name of your config file : config_name.conf")

    ### run config file
    pipe_parser = sub_parsers.add_parser("run", help = "run pipeline with a config file input",
                                         description = "Run Drseq pipeline with a config file input")
    pipe_parser.add_argument("-c","--config", required = True,
                             help = "specify the config file, -c config_name.conf" )
    pipe_parser.add_argument("-f","--force_overwrite",dest='fover',  default=False, action='store_true',
                             help = "The pipeline will over write output result if the output folder is already exist " )
    pipe_parser.add_argument("--clean",dest='Clean' , default=False, action='store_true',
                             help = "remove intermediate result generated during Dr.seq,default is NO" )
    ### simple mode
    simple_parser = sub_parsers.add_parser("simple", help = "run Drseq using simple mode",
                                         description = "(Run Drseq pipeline using simple mode/command line mode) Usage: Drseq.py -a barcode.fastq -b reads.fastq -n outname --maptool STAR -g mm10_refgenes.txt --mapindex /home/user/STAR_index")
    simple_parser.add_argument("-b","--barcode", dest = 'barcode',required = True,
                             help = "[required] barcode fastq file before any filtering step, only accept .fastq format" )
    simple_parser.add_argument("-r","--reads",dest='reads',required = True,
                             help = "[required] reads fastq file, accept raw fastq input or aligned sam format, format(fastq,sam) fixed by extension(.fastq or .sam)" )
    simple_parser.add_argument("-n","--name", dest="name",required = True,
                             help="[required] name of you config file and output dir, name only , no extension. The output files will be named like name.pdf, name.txt ... ")
    simple_parser.add_argument("--cellbarcodelength",dest='CBL' ,default='12',
                             help = "specify the length of your cell barcode , default is 12, 12(cellbarcode) + 8(umi) = 20 (barcodefastq)" )
    simple_parser.add_argument("--umilength",dest='UMIL',  default='8',
                             help = "specify the length of your UMI , default is 8, 12(cellbarcode) + 8(umi) = 20 (barcodefastq)" )
    simple_parser.add_argument("-g","--gene_annotation",dest='GA', required = False,
                             help = "[required absolute path if you didn't specific it in template config file] gene annotation file, the annotation file can be download from UCSC, full annotation text format(see documents for detail), or users can download gene annotation file in hg38 and mm10 version from our homepage" )
    simple_parser.add_argument("--maptool",dest='maptool' , choices = ("STAR", "bowtie2"),default="STAR",
                             help = "choose mapping software for alignment, default is STAR, you can also choose bowtie2 as another option. mapping tool should corresponded to mapindex, STAR vs. STAR index, bowtie2 vs. bowtie2 index. Dr.seq will check your total memory if you choose STAR, becuse STAR consumes huge memory for mapping though fast. If you don't have 40G for total memory and choose STAR, Dr.seq will exit to protect your server/computer. you can also turn off the checkmem option to run STAR direclty" )
    simple_parser.add_argument("--checkmem",dest='checkmem' , choices = ("0", "1"), default="1",
                             help = "(default is 1 (on), only take effect when maptool = STAR) Dr.seq will check your total memory (if turned on, set 1, default) to make sure its greater 40G if you choose STAR as mapping tool. We don't suggest to run STAR on Mac. You can turn off (set 0) this function if you prefer STAR regardless of your memory (which may cause crash down of your computer) " )
    simple_parser.add_argument("--mapindex",dest='mapindex',required = False,
                             help = "[required if you didn't specific it in template config file] mapping index folder, there should be a mm10.star folder under mapindex folder if you use STAR to map to mm10 genome, mm10.bowtie2 folder if use bowtie2. for bowtie2, the index file should named like mm10.1.bt2(see documents for detail)" )
    simple_parser.add_argument("--thread",dest='P' ,default='8',
                             help = "number of alignment threads to launch, ignored for sam input" )
    simple_parser.add_argument("-f","--force_overwrite",dest='fover',  default=False, action='store_true',
                             help = "specify the config file to create output folder , this cmd will rm existing result if set True ~!! " )
    simple_parser.add_argument("--clean",dest='Clean' , default=False, action='store_true',
                             help = "remove intermediate result generated during Dr.seq,default is No" )
    simple_parser.add_argument("--select_cell_measure",dest='select_cell_measure' , choices = ("1", "2"), default="1",
                             help = "Method to select STAMPs from cell_barcodes, choose from 1 or 2 (default is 1), 1: Cell_barcodes with more than 1000 genes covered are selected. 2: Top 1000 cell_harcodes with highest umi count will be selected" )
    simple_parser.add_argument("--remove_low_dup_cell",dest='remove_low_dup_cell' , choices = ("0", "1"), default="0",
                             help = "discard cell barcodes with low duplicate rate (< 0.1), set 1 to turn on this function, set 0 (default) to turn off. May not effective for samples with low sequencing depth" )

    args = parser.parse_args()
    ## generate config file template
    if args.sub_command == "gen":
        gen_conf(args.config_name)
        sys.exit(0)

    ## run Dr.seq pipeline with config file input
    if args.sub_command == "run":
        if os.path.isfile(args.config):
            return args
        else:
            print('ERROR : -c input is not a config file\n')
            print(pipe_parser.print_help())
            sys.exit()

    ## run Dr.seq pipeline with a simple mode, input parameter in command line
    if args.sub_command == "simple":
        if args.name.endswith('.conf'):
            args.name = args.name[:-5]
        make_conf(args.barcode,args.reads,args.name,args.fover,args.CBL,args.UMIL,args.GA,args.P,args.mapindex,args.checkmem,args.maptool,args.select_cell_measure,args.remove_low_dup_cell)
        args.config = args.name + '.conf'
        return args

# ------------------------------------
# Main function
# ------------------------------------

def main():

    args = parse_args()
    conf_dict = read_conf(args.config)
    ### read raw path of output dir, the startdir will be used when the input file is not in absolute path
    conf_dict['General']['startdir'] = os.getcwd()+'/'

    ### check output name and dir from input parameter
    if conf_dict['General']['outname'] == "":
        print('your outname cannot be left blank,exit')
        sys.exit(1)
    if "." in conf_dict['General']['outname']:
        oldname = conf_dict['General']['outname']
        newname = oldname.replace(".","-")
        conf_dict['General']['outname'] = newname
        print('replace outname from %s to %s for latex summary'%(oldname,newname))
    if conf_dict['General']['outputdirectory'] == "":
        conf_dict['General']['outputdirectory'] = conf_dict['General']['outname']
        print('output directory is blank, use outname as directory name and set output directory in current folder')
    if "~" in conf_dict['General']['outname']:
        print('ERROR: ~ cannot appeared in outname, current outname is %s'%(conf_dict['General']['outname']))
        sys.exit(1)
    if "~" in conf_dict['General']['outputdirectory']:
        print('ERROR: require absolute path for outputdirectory')
        sys.exit(1)
    if not conf_dict['General']['outputdirectory'].endswith('/'):
        conf_dict['General']['outputdirectory'] += '/'
    if not conf_dict['General']['outputdirectory'].startswith('/'):
        conf_dict['General']['outputdirectory'] = conf_dict['General']['startdir'] + conf_dict['General']['outputdirectory']

    ### creat output dir
    if os.path.isfile(conf_dict['General']['outputdirectory'].rstrip("/")):
        print('ERROR: name of your output dir is exist as a file, cannot create a dir,Dr.seq exit')
        sys.exit(1)
    elif os.path.isdir(conf_dict['General']['outputdirectory']):
        if not args.fover:
            print('ERROR: name of your output dir is exist as a dir, Dr.seq exit because overwrite function is turned off, you can add -f parameter to turn on overwite function')
            sys.exit(1)
        else:
            print('name of your output dir is exist as a dir, overwrite is turned on, write output result in existing dir')
    else:
        os.system("mkdir %s"%(conf_dict['General']['outputdirectory']))

    ### move to output dir
    os.chdir(conf_dict['General']['outputdirectory'])
    ## cp config file to output folder
    cmd = 'cp %s .'%(conf_dict['General']['startdir']+args.config)
    CMD(cmd)
    ### specify the main progress log file
    logfile = conf_dict['General']['outputdirectory']+'progress_log.txt'
    ### remove existing log file.
    if os.path.isfile(logfile):
        CMD('rm %s'%logfile)

    ### Rscript location
    #CONFIG_TEMPLATE = os.path.join(Drseq_pipe.__path__[0], "Config/Drseq_template.conf")
    conf_dict['rscript'] = os.path.join(Drseqpipe.__path__[0], "Rscript/")#'/mnt/Storage3/CR/Dropseq/drseq/Rscript/'
    conf_dict['clean'] = args.Clean

    ### main step for Dr.seq , see individual script for detail note.
    # preparing step, integrate parameter, prepare for following step
    t = time.time()
    step0_integrate_data(conf_dict,logfile)
    # main data processing step, including mapping, generate expression matrix and QC matrix which is used in next step
    step1_generate_matrix(conf_dict,logfile)
    step1time = time.time() -t
    wlog("running time for expression matrix generation: %s"%(step1time),logfile)
    # QC step, including bulk RNAseq QC(option), individual cell QC
    t = time.time()
    step3_QC(conf_dict,logfile)
    step3time = time.time()-t
    wlog("running time for QC: %s"%(step3time),logfile)
    # analysis step, including  select cell, filter high variance gene, pca + t-SNE dimentional reduction, k-means + Gap stat clustering
    t = time.time()
    step4_analysis(conf_dict,logfile)
    step4time = time.time() -t
    wlog("running time for clustering: %s"%(step4time),logfile)
    # summary step, integrate all QC figure and expression matrix, generate qC report with latex
    step5_summary(conf_dict,logfile)

if __name__== '__main__':
    try:
        main()

    except KeyboardInterrupt:
        sys.stderr.write("User interrupt Dr.seq\n")
        sys.exit(1)


from nltk.stem import WordNetLemmatizer
from sklearn.feature_extraction.text import CountVectorizer
from sklearn.naive_bayes import MultinomialNB
from sklearn.metrics import accuracy_score
import re
import pandas as pd

lemmatizer = WordNetLemmatizer()
def loadData(fname):
    reviews=[]
    labels=[]
    text = []
    text1 =[]
    f=open(fname,encoding = 'utf8')
    i=0
    for line in f:
        i+=1
        try:
            review,rating=line.strip().split('\t')
            review = review.lower()
            review = re.sub('[^a-z0-9]',' ',review)
            reviews.append(review)
            labels.append(int(rating))
        except:
            print(i)

    f.close()
    for word in reviews:
        text = lemmatizer.lemmatize(word)
        text1.append(text)
    return text1,labels

rev_train,labels_train=loadData('train.txt')
rev_test=loadData('testrun.txt')

counter = CountVectorizer()
counter.fit(rev_train)

#count the number of times each term appears in a document and transform each doc into a count vector
counts_train = counter.transform(rev_train)#transform the training data
counts_test = counter.transform(rev_test)#transform the testing data

#train classifier
clf = MultinomialNB()

#train all classifier on the same datasets
clf.fit(counts_train,labels_train)

#use hard voting to predict (majority voting)
pred=clf.predict(counts_test)
output = pd.DataFrame(pred)
output.to_csv('testrunoutput.csv', index=False)


#Program to test my knowledge of boolean logic

import random

logic_dict = {
#NOT_True = {
'not False': True,
'not True': False,

#}
#OR_True = {
'True or False': True,
'True or True': True,
'False or True': True,
'False or False': False,
#}

#AND_True = {
'True and False': False,
'True and True': True,
'False and True': False,
'False and False': False,
#}

#NOT_OR_True = {
'not(True or False)': False,
'not(True or True)': False,
'not(False or True)': False,
'not(False or False)': True,
#}

#NOT_AND_True = {
'not(True and False)': True,
'not(True and True)': False,
'not(False and True)':	True,
'not(False and False)': True,
#}

#not_equal_to_True = {
'1 != 0': True,
'1 != 1': False,
'0 != 1': True,
'0 != 0': False,
#}

#equals_True = {
'1 == 0': False,
'1 == 1': True,
'0 == 1': False,
'0 == 0': True
}

def logic_test():
    print("Let's play a game of logic.\nTell me if this is true or not. ")
    turns = 7
    points = 0
    while turns > 0:
        question = random.choice(list(logic_dict))
        prompt = (input("Is this 'True' or 'False'? "), question)
        print(prompt)
        if prompt == question:
            print("That is correct!")
            points += 1
            turns -= 1
            print(turns, "turns left\n", points, "points")
        else:
            print("Sorry, that is incorrect. ")
            turns -= 1
            print(turns, "turns left\n", points, "points")

logic_test()

'''
assign value (True or False) to each logic question

put questions into numbered list

call random list object. Have user input answer

return value of list object
'''

def NOT_True():
    not_false = True
    not_true = False


from modulo import *

def mostrar_menu_de_opciones():  # muestra el menu de opciones
    print('-' * 50)
    print('Bienvenido al Gestor de Libros de la biblioteca. \n')
    print('-' * 50)
    print(
        'A continuación, se le mostrarán las opciones posibles y usted podrá seleccionar una de ellas, escribiendo \n'
        'el número al que corresponde la opción deseada. \n\n'
        '1 -> Generar libros automáticamente. \n'
        '2 -> Agregar libros al sistema manualmente. \n'
        '3 -> Mostrar los datos de los libros en el sistema en orden alfabético. \n'
        '4 -> Mostrar la cantidad de libros por género, e indicar el género con más libros. \n'
        '5 -> Buscar el libro de mayor precio para un idioma. \n'
        '6 -> Buscar un libro por código ISBN. \n'
        '7 -> Mostrar los libros del género más popular, ordenados de mayor a menor precio. \n'
        '8 -> Consultar precio de un grupo de libros según su ISBN. \n'
        '9 -> Salir del programa\n')

def principal():
    # inicialización de variables necesarias
    genero_popular = 10
    op = 0
    libros = []

    # menu de opciones
    while op != 9:
        mostrar_menu_de_opciones()
        op = int(input('---- > Ingrese una opción: '))

        if op == 1:  # generar libros automáticamente
            n = int(input('Cuantos libros desea generar?: '))
            print('Generando libros...')
            generar_arreglo_aleatorio(n, libros)

            input('Presione enter para continuar...')
        if op == 2:  # cargar libros manualmente
            n = int(input('Cuantos libros desea agregar?: '))
            for i in range(n):
                libros.append(ingresar_datos_libro())

            input('Presione enter para continuar...')
        if op == 3:  # mostrar libros actualmente dentro del sistema

            if len(libros) == 0:  # validar que haya libros en el sistema
                print('No hay libros cargados en el sistema!!')
                input('Presione enter para continuar...')

            else:
                mostrar_libros(libros)

                input('Presione enter para continuar...')

        if op == 4:  # mostrar libros por género y determinar el que más libros tiene

            if len(libros) == 0:  # validar que haya libros en el sistema
                print('No hay libros cargados en el sistema!!')
                input('Presione enter para continuar...')
            else:
                genero_popular = contador_de_generos(libros)

                input('Presione enter para continuar...')

        if op == 5:  # mostrar el libro de mayor precio para un idioma en específico
            if len(libros) == 0:  # validar que haya libros en el sistema
                print('No hay libros cargados en el sistema!!')
                input('Presione enter para continuar...')

            else:
                libro_precio_may, idi_selec = busqueda_de_mayor(libros)
                idiomas = ["Español", "Inglés", "Francés", "Italiano", "Otros"]
                idi_selec = idiomas[idi_selec - 1]
                print(f'El libro del idioma {idi_selec}, con mayor precio es "{libro_precio_may.titulo}"',
                      f' | ISBN {libro_precio_may.codigo}, con un precio de ${libro_precio_may.precio}')

                input('Presione enter para continuar...')

        if op == 6:  # buscar un libro por isbn
            if len(libros) == 0:  # validar que haya libros en el sistema
                print('No hay libros cargados en el sistema!!')
                input('Presione enter para continuar...')

            else:
                buscado = input('Ingrese el código ISBN a buscar: ')
                resultado = buscar_por_isbn(libros, buscado)
                if resultado is False:
                    print('No se encontró el libro solicitado')
                else:
                    print(f'El libro buscado es: "{resultado.titulo}" y su precio es '
                          f'${round(resultado.precio * 1.10, 2)}')

                input('Presione enter para continuar...')

        if op == 7:  # mostrar los libros del género más popular ordenados por precio de mayor a menor
            if len(libros) == 0:  # validar que haya libros en el sistema
                print('No hay libros cargados en el sistema!!')
                input('Presione enter para continuar...')

            else:
                consulta_de_un_genero(libros, genero_popular)

                input('Presione enter para continuar...')

        if op == 8:  # buscar múltiples libros según su ISBN

            if len(libros) == 0:  # validar que haya libros en el sistema
                print('No hay libros cargados en el sistema!!')
                input('Presione enter para continuar...')

            else:
                buscar_grupo_de_libros(libros)

                input('Presione enter para continuar...')

        if op <= 0 or op >= 10:  # validar que sea una opción correcta
            print('Por favor, ingrese un valor válido.')

            input('Presione enter para continuar...')

    print('Gracias por usar!')  # chau!

if __name__ == '__main__':
    principal()

3 - Trabajos Prácticos/Trabajo Práctico 3/Main.py


"""
Shared utilities used by several test case generators.
"""

from io import BytesIO

from vc2_data_tables import (
    Profiles,
    ParseCodes,
    PARSE_INFO_HEADER_BYTES,
)

from vc2_conformance.pseudocode.state import State

from vc2_conformance.bitstream import (
    Serialiser,
    BitstreamWriter,
    ParseInfo,
    parse_info,
    vc2_default_values,
)

def make_dummy_end_of_sequence(previous_parse_offset=PARSE_INFO_HEADER_BYTES):
    """
    Make (and serialise) an end-of-sequence data unit to be placed within a
    padding data unit.
    """
    f = BytesIO()
    state = State()
    context = ParseInfo(
        parse_code=ParseCodes.end_of_sequence,
        next_parse_offset=0,
        previous_parse_offset=previous_parse_offset,
    )
    with Serialiser(BitstreamWriter(f), context, vc2_default_values) as ser:
        parse_info(ser, state)

    return f.getvalue()

def iter_transform_parameters_in_sequence(codec_features, sequence):
    """
    Iterate over all of the transform parameters in a sequence.

    Generates a series of
    :py:class:`~vc2_conformance.bitstream.TransformParameters` dicts, one for
    each picture present in the provided
    :py:class:`~vc2_conformance.bitstream.Sequence`.

    NB: This function assumes the stream is conformant.
    """
    for data_unit in sequence["data_units"]:
        # Get the TransformParameters for the current picture/fragment data
        # unit
        if "picture_parse" in data_unit:
            tp = data_unit["picture_parse"]["wavelet_transform"]["transform_parameters"]
            yield tp
        elif "fragment_parse" in data_unit:
            if "transform_parameters" in data_unit["fragment_parse"]:
                yield data_unit["fragment_parse"]["transform_parameters"]

def iter_slices_in_sequence(codec_features, sequence):
    """
    Iterate over all of the slices in a sequence.

    Generates a series of (:py:class:`~vc2_conformance.pseudocode.state.State`, sx, sy,
    :py:class:`~vc2_conformance.bitstream.LDSlice` or
    :py:class:`~vc2_conformance.bitstream.HQSlice`) tuples, one for each slice
    present in the provided :py:class:`~vc2_conformance.bitstream.Sequence`.
    The state dictionary will be populated as required by the two
    ``fill_*_slice_padding`` functions.

    NB: This function assumes the stream is conformant (i.e. has the correct
    number of slices, all fragments are present and in order etc).
    """
    sx = sy = 0

    state = None

    for data_unit in sequence["data_units"]:
        # Get the TransformData/FragmentData (td_or_fd) and SliceParameters
        # (sp) for the current picture/fragment data unit
        td_or_fd = None
        sp = None
        if "picture_parse" in data_unit:
            wt = data_unit["picture_parse"]["wavelet_transform"]
            td_or_fd = wt["transform_data"]
            sp = wt["transform_parameters"]["slice_parameters"]
        elif "fragment_parse" in data_unit:
            if "fragment_data" in data_unit["fragment_parse"]:
                td_or_fd = data_unit["fragment_parse"]["fragment_data"]
            if "transform_parameters" in data_unit["fragment_parse"]:
                sp = data_unit["fragment_parse"]["transform_parameters"][
                    "slice_parameters"
                ]

        # Got transform parameters
        if sp is not None:
            if codec_features["profile"] == Profiles.high_quality:
                state = State(
                    slice_prefix_bytes=sp["slice_prefix_bytes"],
                    slice_size_scaler=sp["slice_size_scaler"],
                    slices_x=sp["slices_x"],
                    slices_y=sp["slices_y"],
                )
            elif codec_features["profile"] == Profiles.low_delay:
                state = State(
                    slice_bytes_numerator=sp["slice_bytes_numerator"],
                    slice_bytes_denominator=sp["slice_bytes_denominator"],
                    slices_x=sp["slices_x"],
                    slices_y=sp["slices_y"],
                )

        # Got some slices
        if td_or_fd is not None:
            if codec_features["profile"] == Profiles.high_quality:
                slices = td_or_fd["hq_slices"]
            elif codec_features["profile"] == Profiles.low_delay:
                slices = td_or_fd["ld_slices"]

            for slice in slices:
                yield (state, sx, sy, slice)

                sx += 1
                if sx >= state["slices_x"]:
                    sx = 0

                    sy += 1
                    if sy >= state["slices_y"]:
                        sy = 0


'''
ai = i길이 수열에서 가장 긴 바이토닉 수열 길이 최적해
양방향(ltor, rtol)으로 증가 부분 수열을 각 자리마다 구하고 그 합 중 큰 값을 반환
if 양방향 값 모두 0이 아닌경우 -1(해당 순서 숫자 중복)

a[i][j] -> i = 순서 // j[0] = ltor 부분증가길이, j[1] = rtol 부분증길이 최적해

ltor 부분 증가 길이 최적 해 점화식
ai = max(ai, aj + 1) j는 0부터 i-1까지 반복 & ai는 1로 초기화

'''

def solve(n: int, seq: list):
    dp = [[1 for i in range(2)] for _ in range(n+1)]

    # n = 3
    for i in range(1, n+1):  # 1,2,3
        for j in range(1, i):  # 1 , 1, 2
            if seq[i] > seq[j]:
                dp[i][0] = max(dp[i][0], dp[j][0]+1)
        for k in range(1, i):  # 3 , 3, 2
            if seq[(n+1)-i] > seq[(n+1)-k]:
                dp[(n+1)-i][1] = max(dp[(n+1)-i][1], dp[(n+1)-k][1]+1)

    max_val = 0
    for i in range(1, n+1):
        max_val = max(max_val, sum(dp[i]))

    return max_val-1

N = int(input())
seq = [0] + list(map(int, input().split()))

ret = solve(N, seq)

print(ret)

가장긴바이토닉부분수열_11054.py


from selenium import webdriver
from selenium.webdriver import ActionChains
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

from selenium.webdriver.common.keys import Keys

import time
from datetime import datetime
import pywhatkit
from selenium.webdriver.chrome.service import Service

#
def get_inside_the_account_liat(driver):
    source = driver.find_element("link text", "כניסה / הרשמה")
    source.click()
    source = driver.find_element("id","LoginPhone")
    time.sleep(2)
    source.send_keys("0585532002")
    source = driver.find_element("id", "LoginPassword")
    # time.sleep(3)
    source.send_keys("lifr12345")
    source = driver.find_element("id", "loginButton")
    source.click()

def get_inside_the_account_ami(driver):
    source = driver.find_element("link text", "כניסה / הרשמה")
    source.click()
    source = driver.find_element("id","LoginPhone")
    time.sleep(2)
    source.send_keys("0585532002")
    source = driver.find_element("id", "LoginPassword")
    # time.sleep(3)
    source.send_keys("lifr12345")
    source = driver.find_element("id", "loginButton")
    source.click()

def open_lesson_by_time(driver):
    while True:
        ###
        currentDateAndTime = datetime.now()
        exact_time = currentDateAndTime.strftime("%H:%M")
        if exact_time == "20:34":
            register_to_spinning_lesson(driver)
            pywhatkit.sendwhatmsg("+972585532002", "קבעתי לך שיעור ספינינג", 20, 36)
            break
        if exact_time == "22:36":
            register_to_monday_buddy_pump_lesson(driver)
            pywhatkit.sendwhatmsg("+972585532002","קבעתי לך שיעור באדי פאמפ",22,37)
            break

def open_browser():
    s = Service("C:\Program Files (x86)\chromedriver.exe")
    driver = webdriver.Chrome(service=s)
    # driver.maximize_window()
    driver.get("https://www.holmesplace.co.il/place-studio/?club=210")
    get_inside_the_account_liat(driver)
    open_lesson_by_time(driver)

def register_to_spinning_lesson(driver):
    # choose_club(driver)
    time.sleep(5)
    source = driver.find_element(By.XPATH,"//*[@id='Friday']/div[2]/div/a")
    driver.execute_script("arguments[0].click();", source)
    time.sleep(5)
    source = driver.find_element(By.XPATH, "//*[@id='registerWithSeatBT']/a")
    driver.execute_script("arguments[0].click();", source)
    time.sleep(5)
    source = driver.find_element(By.XPATH, "//*[@id='lesson-reg-form']/div/div/div[1]/button/span")
    driver.execute_script("arguments[0].click();", source)
    time.sleep(5)
    source = driver.find_element("link text", "(התנתק)")
    source.click()

def register_to_monday_buddy_pump_lesson(driver):
    ####### body pump at 20:00 pm
    time.sleep(5)
    source = driver.find_element(By.XPATH, "//*[@id='pills-tab-studioContent']/div[2]/div[1]/div[1]/div[10]/div/a")
    driver.execute_script("arguments[0].click();", source)
    time.sleep(5)
    source = driver.find_element(By.XPATH, "//*[@id='registerButtonMobile']/a")
    driver.execute_script("arguments[0].click();", source)
    time.sleep(5)
    source = driver.find_element(By.XPATH, "//*[@id='lesson-reg-form']/div/div/div[1]/button/span")
    driver.execute_script("arguments[0].click();", source)
    time.sleep(5)
    source = driver.find_element("link text", "(התנתק)")
    source.click()
    time.sleep(4)
    driver.quit()

def main():
    open_browser()

main()


import itertools
from collections import defaultdict
def in_one_not_in_two(a,b):
    return ''.join([item for item in b if item not in a])

def containsAll(item, tr):
    for i in item:
      if i not in tr:

         return False
    return True
def all_items_are_in_transaction(item, tr):
    for i in item:
        if i not in tr:return False
    print(i,tr,"True")
    return True
def has_infrequent_itemset(canditate, frequent_itemsets):
    frequent_itemsets = [fi[0] for fi in frequent_itemsets]
    canditate_subsets = itertools.combinations(canditate, len(canditate)-1)
    for subset in canditate_subsets:
        if ''.join(subset) not in frequent_itemsets:return True
    return False
def is_frequent(item_sets, min_sup, canditate):
    count = 0
    for transaction in item_sets:
        if set(canditate) <= set(transaction):count += 1
    if count >= min_sup:return True, count
    return False, count
def frequent_list_from_iteration(transactions, minsup, candidats):
    return [[c, is_frequent(transactions, minsup, c)[1]]for c in candidats if is_frequent(transactions, minsup, c)[0]]
def hist(tr):
    transactions = defaultdict(int)
    for transaction in tr:
        for char in transaction:transactions[char] += 1
    return [[key,val] for key,val in transactions.items()]
def apriori_gen(frequent_itemsets):
    length = len(frequent_itemsets[0][0])
    if length == 1:
        for i in range(len(frequent_itemsets)-1):
            for j in range(i+1, len(frequent_itemsets)):
                canditate = sorted(
                    [frequent_itemsets[i][0], frequent_itemsets[j][0]])
                if frequent_itemsets[i][2] != ''.join(canditate) and frequent_itemsets[j][2] != ''.join(canditate):
                    yield ''.join(canditate)
    else:
        for i in range(len(frequent_itemsets)-1):
            for j in range(i+1, len(frequent_itemsets)):
                if frequent_itemsets[i][0][:length-1] == frequent_itemsets[j][0][:length-1]:
                    canditate = set(frequent_itemsets[i][0]).union(
                        set(frequent_itemsets[j][0]))
                    canditate = sorted(canditate)
                    if not has_infrequent_itemset(''.join(canditate), frequent_itemsets):
                        if frequent_itemsets[i][2] != ''.join(canditate) and frequent_itemsets[j][2] != ''.join(canditate):
                            yield ''.join(canditate)
def fermeture(frequent_itemsets, transactions):
    fermetures = []
    print(frequent_itemsets, transactions)
    for item in frequent_itemsets:
        l = [item[0]]
        tr = [t for t in transactions if containsAll(item[0], t)]
        tr = hist(tr)
        #print(tr)
        for item2 in tr:
            if item2[1] == item[1] and item2[0] not in item[0]:
                l.append(item2[0])
        l = [''.join(l), item[1]]
        fermetures.append([item[0], l[1], ''.join(sorted(l[0]))])
    return fermetures
def output_association_rules(all_frequent):
    for items in all_frequent:
        for item in items:
            if item[0]!=item[2]:
                print(f'{item[0]}  -> {in_one_not_in_two(item[0],item[2])}')
transactions = ['ABCDE', 'AB', 'CE', 'ABDE', 'ACD','F']
minsup = 2
L =  hist(transactions)
L = fermeture(frequent_list_from_iteration(transactions, minsup, [x[0] for x in L]), transactions)
all_frequent =[]
while L != []:
    all_frequent.append(L)
    L = fermeture(frequent_list_from_iteration(transactions, minsup, list(apriori_gen(L))), transactions)
output_association_rules(all_frequent)


from django.contrib import admin
from jet.filters import DateRangeFilter
from .models import ManagerReminders

class ManagerRemindersProfile(admin.ModelAdmin):
    list_display = ('date', 'due_date', 'requestor', 'requestee', 'comment_from_admin',
                    'memo_from_requestor', 'memo_from_requestee', 'status')
    list_filter = ['date', 'status', 'requestor', 'requestee',
                   ('date', DateRangeFilter)]
    list_per_page = 30
    search_fields = ('manager_under', 'status')
    readonly_fields = ['date']
    fieldsets = (
        ('Reminder Informations', {
            'fields': (
                'date',
                'due_date',
                'requestor',
                'status',
                'comment_from_admin',
                'requestee',
                'memo_from_requestor',
                'memo_from_requestee',
            )
        }),
    )

admin.site.register(ManagerReminders, ManagerRemindersProfile)