'''
Airmass related funcitons.

Don't you think these must be implemented to astropy...?
'''
from warnings import warn

import numpy as np
from astropy.io import fits
from astropy import units as u
from astropy.coordinates import AltAz, EarthLocation, SkyCoord
from astropy.io.fits import Card
from astropy.time import Time

from .hduutil import get_if_none, cmt2hdr
from .misc import change_to_quantity

__all__ = ["calc_airmass", "airmass_obs", "airmass_to_hdr", "airmass_from_hdr"]

def calc_airmass(zd_deg=None, cos_zd=None, scale=750.):
    ''' Calculate airmass by nonrefracting radially symmetric atmosphere model.

    Parameters
    ----------
    zd_deg : float, optional
        The zenithal distance in degrees

    cos_zd : float, optional
        The cosine of zenithal distance. If given, ``zd_deg`` is not used.

    scale : float, optional
        Earth radius divided by the atmospheric height (usually scale height)
        of the atmosphere. In IRAF documentation, it is mistakenly written that
        this ``scale`` is the "scale height".

    Notes
    -----
    Wiki:

        https://en.wikipedia.org/wiki/Air_mass_(astronomy)#Nonrefracting_radially_symmetrical_atmosphere

    Identical to the airmass calculation for a given observational run of
    IRAF's asutil.setairmass:

        http://stsdas.stsci.edu/cgi-bin/gethelp.cgi?setairmass

    '''
    if not ((zd_deg is None) ^ (cos_zd is None)):
        raise ValueError("One and only one of `zd_deg` or `cos_zd` should be given.")

    # NOTE: for this part, using numexpr is slower than using numpy. It is
    # because this airmass calculation is usually done for a single zd value
    # from a single exposure. If one needs a large amount of such calculations,
    # numexpr may boost the speed, but that will be like few ms to us order.
    # Using numexpr will introduce a large overhead for the calculation if this
    # is repeated for thousands of images:
    # numexpr version:
    # %timeit yfu.calc_airmass(10)
    # 21.3 µs +/- 2.44 µs per loop (mean +/- std. dev. of 7 runs, 10000 loops each)
    # numpy version:
    # %timeit yfu.calc_airmass(10)
    # 3.65 µs +/- 93.5 ns per loop (mean +/- std. dev. of 7 runs, 100000 loops each)

    if cos_zd is None:
        cos_zd = np.cos(np.deg2rad(zd_deg))

    am = np.sqrt((scale*cos_zd)**2 + 2*scale + 1) - scale*cos_zd

    return am

def airmass_obs(targetcoord, obscoord, ut, exptime, scale=750., full=False, in_deg=True):
    ''' Calculate airmass by nonrefracting radially symmetric atmosphere model.

    Parameters
    ----------
    targetcoord: astropy.SkyCoord
        The target's coorndinate.

    obscoord : astropy.EarthLocation
        The observer's location.

    ut : astropy.Time
        The time when the exposure is started.

    exptime : astropy.Quantity
        The exposure time.

    scale : float, optional
        Earth radius divided by the atmospheric height (usually scale height)
        of the atmosphere. In IRAF documentation, it is mistakenly written that
        this `scale` is the "scale height".

    in_deg : bool, optional
        If True, return alt/az in degrees. Otherwise, return in string with separation ":".

    Notes
    -----
    Wiki:

        https://en.wikipedia.org/wiki/Air_mass_(astronomy)#Nonrefracting_radially_symmetrical_atmosphere

    Identical to the airmass calculation for a given observational run of
    IRAF's asutil.setairmass:

        http://stsdas.stsci.edu/cgi-bin/gethelp.cgi?setairmass

    Partly contributed by Geonwoo Kang (Seoul National University) in Apr 2018.
    '''
    if not isinstance(ut, Time):
        warn("ut is not Time object. Assume format='isot', scale='utc'.")
        ut = Time(ut, format='isot', scale='utc')
    if not isinstance(exptime, u.Quantity):
        warn("exptime is not astropy Quantity. Assume it is in seconds.")
        exptime = exptime * u.s

    t_start = ut
    t_mid = ut + exptime / 2
    t_final = ut + exptime

    alldict = {"alt": [], "az": [], "zd": [], "am": []}
    for t in [t_start, t_mid, t_final]:
        C_altaz = AltAz(obstime=t, location=obscoord)
        target = targetcoord.transform_to(C_altaz)
        if in_deg:
            alt = target.alt.to_value(unit=u.deg)
            az = target.az.to_value(unit=u.deg)
        else:
            alt = target.alt.to_string(unit=u.deg, sep=':')
            az = target.az.to_string(unit=u.deg, sep=':')
        zd = target.zen.to(u.deg).value
        am = calc_airmass(zd_deg=zd, scale=scale)
        alldict["alt"].append(alt)
        alldict["az"].append(az)
        alldict["zd"].append(zd)
        alldict["am"].append(am)

    am_eff = (alldict["am"][0] + 4*alldict["am"][1] + alldict["am"][2]) / 6

    if full:
        return am_eff, alldict

    return am_eff

def airmass_to_hdr(
    header: fits.Header,
    obscoord: EarthLocation,
    targetcoord: SkyCoord = None,
    ra: str | float | u.Quantity = "RA",
    dec: str | float | u.Quantity = "DEC",
    time_start: str | Time = "DATE-OBS",
    time_scale: str = "utc",
    exptime: str | float | u.Quantity = "EXPTIME",
    scale=750.,
    in_deg=True,
    frame: str = "icrs",
):
    """
    Calculates airmass from a given header and location information

    Parameters
    ----------
    header : fits.Header
        The header of the image to calculate airmass from.

    obscoord : EarthLocation
        The location of the observatory.

    targetcoord : SkyCoord, optional
        The coordinate of the target. If not given, it will be calculated from
        the header.

    ra, dec : str|float|u.Quantity, optional
        The name of the header keyword or the value of the RA and DEC of the
        target. If float, it should be in [deg].

    time_start : str|Time, optional
        The name of the header keyword or the value of the time when the
        exposure started.

    time_scale : str, optional
        The scale of the time if it is read from header. Default is "utc".

    exptime : str|float|u.Quantity, optional
        The name of the header keyword or the value of the exposure time. If
        given as float, it should be in [sec].

    scale : float, optional
        Earth radius divided by the atmospheric height (usually scale height)
        of the atmosphere. In IRAF documentation, it is mistakenly written that
        this `scale` is the "scale height".
    in_deg : bool, optional
        If True, return alt/az in degrees. Otherwise, return in string with separation ":".

    """
    _t = Time.now()
    if obscoord is None:
        ra = change_to_quantity(header.get(ra) if isinstance(ra, str) else ra, u.deg)
        de = change_to_quantity(header.get(dec) if isinstance(dec, str) else dec, u.deg)
        obscoord = SkyCoord(ra, de, frame=frame)

    t0 = Time(
        header[time_start] if isinstance(time_start, str) else time_start,
        scale=time_scale
    )
    expt = change_to_quantity(header[exptime] if isinstance(exptime, str) else exptime, u.s)
    # if isinstance(time_start, str)

    am_eff, alldict = airmass_obs(
        targetcoord, obscoord, ut=t0, exptime=expt, scale=scale, full=True, in_deg=in_deg
    )

    header.set("AIRMASS", am_eff, "Effective airmass (Stetson 1988; see COMMENT)")
    header.set("AM_STR", alldict["am"][0], "[deg] Airmass (start of exposure)")
    header.set("ZD_STR", alldict["zd"][0], "[deg] Zenithal distance (start of exposure)")
    header.set("ALT_STR", alldict["alt"][0], "[deg] Altitude (start of exposure)")
    header.set("AZ_STR", alldict["az"][0], "[deg] Azimuth (start of exposure)")
    header.set("AM_MID", alldict["am"][1], "[deg] Airmass (midpoint of exposure)")
    header.set("ZD_MID", alldict["zd"][1], "[deg] Zenithal distance (midpoint of exposure)")
    header.set("ALT_MID", alldict["alt"][1], "[deg] Altitude (midpoint of exposure)")
    header.set("AZ_MID", alldict["az"][1], "[deg] Azimuth (midpoint of exposure)")
    header.set("AM_END", alldict["am"][2], "[deg] Airmass (end of exposure)")
    header.set("ZD_END", alldict["zd"][2], "[deg] Zenithal distance (end of exposure)")
    header.set("ALT_END", alldict["alt"][2], "[deg] Altitude (end of exposure)")
    header.set("AZ_END", alldict["az"][2], "[deg] Azimuth (end of exposure)")
    cmt2hdr(
        header, "c", t_ref=_t,
        s=("[yfu.airmass] AIRMASS, AM_XXX, ZD_XXX, ALT_XXX, AZ_XXX are calculated. "
           + "`ysfitsutilpy` uses airmass calculation algorithm identical to IRAF: See "
           + "'Some Factors Affecting the Accuracy of Stellar Photometry with CCDs', "
           + "by P. Stetson, DAO preprint, September 1988. "
           ))

# TODO: change key, unit, etc as input dict.
def airmass_from_hdr(
        header,
        ra=None,
        dec=None,
        ut=None,
        exptime=None,
        lon=None,
        lat=None,
        height=None,
        equinox=None,
        frame=None,
        scale=750.,
        ra_key="RA",
        dec_key="DEC",
        ut_key="DATE-OBS",
        lon_key="LONGITUD",
        lat_key="LATITUDE",
        height_key="HEIGHT",
        exptime_key="EXPTIME",
        equinox_key="EPOCH",
        frame_key="RADECSYS",
        ra_unit=u.hourangle,
        dec_unit=u.deg, exptime_unit=u.s,
        lon_unit=u.deg,
        lat_unit=u.deg,
        height_unit=u.m,
        ut_format='isot',
        ut_scale='utc',
        return_header=False,
        verbose=False
):
    ''' Calculate airmass using the header.
    Parameters
    ----------
    ra, dec: float or Quantity, optional
        The RA and DEC of the target. If not specified, it tries to find them
        in the header using ``ra_key`` and ``dec_key``.

    ut: str or Time, optional
        The *starting* time of the observation in UT.

    exptime: float or Time, optional
        The exposure time.

    lon, lat, height: str, float, or Quantity
        The longitude, latitude, and height of the observatory. See
        astropy.coordinates.EarthLocation.

    equinox, frame: str, optional
        The `equinox` and `frame` for SkyCoord.

    scale: float, optional
        Earth radius divided by the atmospheric height (usually scale height)
        of the atmosphere.

    XX_key: str, optional
        The header key to find XX if ``XX`` is `None`.

    XX_unit: Quantity, optional
        The unit of ``XX``

    ut_format, ut_scale: str, optional
        The `format` and `scale` for Time.

    return_header: bool, optional
        Whether to return the updated header.
    '''
    # If there is no header keyword matches the ``key``, it should give
    # KeyError. For such reason, I didn't use ``get_from_header`` here.
    def _conversion(header, val, key, unit=None):
        if val is None:
            val = header[key]  # assume it is in the unit of ``unit``.
        elif unit is not None:
            if isinstance(val, u.Quantity):
                val = val.to(unit).value
            # else: just return ``val``.
        return val

    def _cards_airmass(am_eff, alldict):
        ''' Gives airmass and alt-az related header cards.
        '''
        amstr = (
            "ysfitsutilpy's airmass calculation uses the same algorithm as IRAF: From "
            + "'Some Factors Affecting the Accuracy of Stellar Photometry with CCDs' by "
            + "P. Stetson, DAO preprint, September 1988.")

        # At some times, hdr["AIRMASS"] = am, for example, did not work for
        # some reasons which I don't know.... So I used Card. -
        # YPBach 2018-05-04
        cs = [
            Card("AIRMASS", am_eff,
                 "Effective airmass (Stetson 1988; see COMMENT)"),
            Card("ZD", alldict["zd"][0],
                 "[deg] Zenithal distance (start of the exposure)"),
            Card("ALT", alldict["alt"][0],
                 "Altitude (start of the exposure)"),
            Card("AZ", alldict["az"][0],
                 "Azimuth (start of the exposure)"),
            Card("ALT_MID", alldict["alt"][1],
                 "Altitude (midpoint of the exposure)"),
            Card("AZ_MID", alldict["az"][1],
                 "Azimuth (midpoint of the exposure)"),
            Card("ZD_MID", alldict["zd"][1],
                 "[deg] Zenithal distance (midpoint of the exposure)"),
            Card("ALT_END", alldict["alt"][2],
                 "Altitude (end of the exposure)"),
            Card("AZ_END", alldict["az"][2],
                 "Azimuth (end of the exposure)"),
            Card("ZD_END", alldict["zd"][2],
                 "[deg] Zenithal distance (end of the exposure)"),
            Card("COMMENT", amstr),
            Card("HISTORY", "ALT-AZ calculated from ysfitsutilpy."),
            Card("HISTORY", "AIRMASS calculated from ysfitsutilpy.")
        ]
        return cs

    ra = get_if_none(ra, header, ra_key, unit=ra_unit, verbose=verbose)[0]
    dec = get_if_none(dec, header, dec_key, unit=dec_unit, verbose=verbose)[0]
    exptime = get_if_none(exptime, header, exptime_key, unit=exptime_unit,
                          verbose=verbose)[0]
    lon = get_if_none(lon, header, lon_key, lon_unit)[0]
    lat = get_if_none(lat, header, lat_key, lat_unit)[0]
    height = get_if_none(height, header, height_key, height_unit)[0]
    equinox = get_if_none(equinox, header, equinox_key)[0]
    frame = get_if_none(frame, header, frame_key)[0]

    ra = _conversion(header, ra, ra_key, ra_unit)
    dec = _conversion(header, dec, dec_key, dec_unit)
    exptime = _conversion(header, exptime, exptime_key, exptime_unit)
    lon = _conversion(header, lon, lon_key, lon_unit)
    lat = _conversion(header, lat, lat_key, lat_unit)
    height = _conversion(header, height, height_key, height_unit)
    equinox = _conversion(header, equinox, equinox_key)
    frame = _conversion(header, frame, frame_key)

    if ut is None:
        ut = header[ut_key]
    elif isinstance(ut, Time):
        ut = ut.isot
        # ut_format = 'isot'
        # ut_scale = 'utc'

    targetcoord = SkyCoord(
        ra=ra, dec=dec, unit=(ra_unit, dec_unit), frame=frame, equinox=equinox
    )

    try:  # It should work here but just in case I put except...
        observcoord = EarthLocation(lon=lon*lon_unit, lat=lat*lat_unit, height=height*height_unit)
    except ValueError:
        observcoord = EarthLocation(lon=lon, lat=lat, height=height)

    am_eff, alldict = airmass_obs(
        targetcoord=targetcoord,
        obscoord=observcoord,
        ut=ut,
        exptime=exptime*exptime_unit,
        scale=scale,
        full=True
    )

    if return_header:
        nhdr = header.copy()
        cards = _cards_airmass(am_eff, alldict)
        # Remove if there is, e.g., AIRMASS a priori to the original header:
        for c in cards:
            try:
                nhdr.remove(c.keyword)
            except KeyError:
                continue
        nhdr = nhdr + cards
        return nhdr

    else:
        return am_eff, alldict


'''
Created on Jan 24, 2019
@author: GANESHRAM KANAKASABAI
'''
from labs.common import ConfigUtil
from labs.common import ConfigConst
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
'''
creating a class smtpClientConnector
'''
class SmtpClientConnector(object):
    '''
    constructor of smtpClientConnector
    '''
    def __init__(self):
        self.config = ConfigUtil.ConfigUtil('../../../config/ConnectedDevicesConfig.props');
        print('Configuration data...\n' + str(self.config));
        self.config.loadConfig();
    '''
    PublishMessage function is used to build and construct message and is sent as a notification.
    @param topic: Subject to Email
    @param data : Sensor notification
    '''
    def publishMessage(self, topic, data):
        host = self.config.getProperty(ConfigConst.ConfigConst.SMTP_CLOUD_SECTION, ConfigConst.ConfigConst.HOST_KEY);
        port = self.config.getProperty(ConfigConst.ConfigConst.SMTP_CLOUD_SECTION, ConfigConst.ConfigConst.PORT_KEY);
        fromAddr = self.config.getProperty(ConfigConst.ConfigConst.SMTP_CLOUD_SECTION, ConfigConst.ConfigConst.FROM_ADDRESS_KEY);
        toAddr = self.config.getProperty(ConfigConst.ConfigConst.SMTP_CLOUD_SECTION, ConfigConst.ConfigConst.TO_ADDRESS_KEY);
        authToken = self.config.getProperty(ConfigConst.ConfigConst.SMTP_CLOUD_SECTION, ConfigConst.ConfigConst.USER_AUTH_TOKEN_KEY);
        msg = MIMEMultipart();
        msg['From'] = fromAddr;
        msg['To'] = toAddr;
        msg['Subject'] = topic;
        msgBody = str(data);
        msg.attach(MIMEText(msgBody));
        msgText = msg.as_string();# Here the email notification is sent .

        try:
            smtpServer = smtplib.SMTP_SSL(host, port);#Connecting SMTP server at given port number.
            smtpServer.ehlo();#method calling SMTP extended hello
            smtpServer.login(fromAddr, authToken);
            smtpServer.sendmail(fromAddr, toAddr, msgText);
            smtpServer.close();
        except:
            print("Error in Sending Mail");


import os
from collections import OrderedDict
from configparser import RawConfigParser
from importlib.metadata import PackageNotFoundError, version

from django.core.checks import Error

from df_config.checks import missing_package, settings_check_results
from df_config.config.dynamic_settings import ExpandIterable
from df_config.guesses.social_providers import SOCIAL_PROVIDER_APPS
from df_config.utils import is_package_present

def allauth_provider_apps(settings_dict):
    parser = RawConfigParser()
    config = settings_dict["ALLAUTH_APPLICATIONS_CONFIG"]
    if not os.path.isfile(config):
        return []
    # noinspection PyBroadException
    try:
        parser.read([config])
    except Exception:  # nosec  # nosec
        settings_check_results.append(
            Error("Invalid config file. %s" % config, obj="configuration")
        )
        return []
    return [
        parser.get(section, "django_app")
        for section in parser.sections()
        if parser.has_option(section, "django_app")
    ]

allauth_provider_apps.required_settings = ["ALLAUTH_APPLICATIONS_CONFIG"]

class InstalledApps:
    """Provide a complete `INSTALLED_APPS` list, transparently adding common third-party packages.
    Specifically handle apps required by django-allauth (one by allowed method).

    """

    default_apps = [
        ExpandIterable("DF_INSTALLED_APPS"),
    ]
    base_django_apps = [
        "df_config",
        "django.contrib.auth",
        "django.contrib.contenttypes",
        "django.contrib.messages",
        "django.contrib.humanize",
        "django.contrib.sitemaps",
        "django.contrib.sites",
        "django.contrib.staticfiles",
        "django.contrib.admin",
    ]
    common_third_parties = OrderedDict(
        [
            ("USE_WEBSOCKETS", ["df_websockets", "channels"]),
            (
                "USE_SITE",
                [
                    "df_site",
                    "bootstrap4",
                    "channels",
                    "dal",
                    "dal_select2",
                    "fontawesome_5",
                    "smart_selects",
                ],
            ),
            ("USE_DEBUG_TOOLBAR", ["debug_toolbar.apps.DebugToolbarConfig"]),
            ("USE_PIPELINE", ["pipeline"]),
            ("USE_PAM_AUTHENTICATION", ["django_pam"]),
            ("USE_CORS_HEADER", ["corsheaders"]),
            ("USE_DAPHNE", ["daphne"]),
        ]
    )
    required_settings = [
        "ALLAUTH_PROVIDER_APPS",
        "DF_INSTALLED_APPS",
        "SESSION_ENGINE",
        "USE_ALL_AUTH",
    ] + list(common_third_parties)
    social_apps = SOCIAL_PROVIDER_APPS

    def __call__(self, settings_dict):
        apps = self.default_apps
        if settings_dict["SESSION_ENGINE"] == "django.contrib.sessions.backends.db":
            apps += ["django.contrib.sessions"]
        apps += self.process_django_allauth(settings_dict)
        apps += self.process_third_parties(settings_dict)
        apps += self.base_django_apps
        return apps

    def process_third_parties(self, settings_dict):
        result = []
        for k, v in self.common_third_parties.items():
            package_name = v[0].partition(".")[0]
            if not settings_dict[k]:
                continue
            elif not is_package_present(package_name):
                settings_check_results.append(missing_package(package_name, ""))
                continue
            result += v
        return result

    def process_django_allauth(self, settings_dict):
        if (
            not settings_dict["USE_ALL_AUTH"]
            and not settings_dict["ALLAUTH_PROVIDER_APPS"]
        ):
            return []
        try:
            version("django-allauth")
        except PackageNotFoundError:
            settings_check_results.append(
                missing_package(
                    "django-allauth", " to use OAuth2 or OpenID authentication"
                )
            )
            return []
        app = "django.contrib.sites"
        if app not in self.default_apps and app not in self.base_django_apps:
            settings_check_results.append(
                Error(
                    '"django.contrib.sites" app must be enabled.',
                    obj="configuration",
                    id="df_config.E001",
                )
            )
            return []
        result = [
            "allauth",
            "allauth.account",
            "allauth.socialaccount",
            "allauth.socialaccount.providers.openid",
        ]
        if settings_dict["ALLAUTH_PROVIDER_APPS"]:
            result += [
                k
                for k in settings_dict["ALLAUTH_PROVIDER_APPS"]
                if k in self.social_apps
            ]
        return result

    def __repr__(self):
        return "%s.%s" % (self.__module__, "installed_apps")

installed_apps = InstalledApps()

class Middlewares:
    use_cache_middleware = True
    base_django_middlewares = [
        "django.contrib.sessions.middleware.SessionMiddleware",
        "django.middleware.common.CommonMiddleware",
        "django.middleware.csrf.CsrfViewMiddleware",
        "django.contrib.auth.middleware.AuthenticationMiddleware",
        "django.middleware.locale.LocaleMiddleware",
        "django.contrib.messages.middleware.MessageMiddleware",
        "django.middleware.clickjacking.XFrameOptionsMiddleware",
        "django.middleware.security.SecurityMiddleware",
        "df_config.apps.middleware.DFConfigMiddleware",
    ]
    common_third_parties = OrderedDict(
        [
            ("USE_WHITENOISE", "whitenoise.middleware.WhiteNoiseMiddleware"),
            ("USE_WEBSOCKETS", "df_websockets.middleware.WebsocketMiddleware"),
            ("USE_CSP", "csp.middleware.CSPMiddleware"),
            ("USE_CORS_HEADER", "corsheaders.middleware.CorsMiddleware"),
        ]
    )
    required_settings = ["DF_MIDDLEWARE"] + list(common_third_parties)
    social_apps = SOCIAL_PROVIDER_APPS

    def __call__(self, settings_dict):
        mw_list = []
        mw_list += self.base_django_middlewares
        mw_list.append(ExpandIterable("DF_MIDDLEWARE"))
        mw_list += self.process_third_parties(settings_dict)
        if settings_dict["USE_DEBUG_TOOLBAR"]:
            mw_list.insert(0, "debug_toolbar.middleware.DebugToolbarMiddleware")
        if self.use_cache_middleware:
            mw_list.insert(0, "django.middleware.cache.UpdateCacheMiddleware")
            mw_list.append("django.middleware.cache.FetchFromCacheMiddleware")
        return mw_list

    def process_third_parties(self, settings_dict):
        result = []
        for k, v in self.common_third_parties.items():
            package_name = v.partition(".")[0]
            if not settings_dict[k]:
                continue
            elif not is_package_present(package_name):
                settings_check_results.append(missing_package(package_name, ""))
                continue
            result.append(v)
        return result

    def __repr__(self):
        return "%s.%s" % (self.__module__, "middlewares")

middlewares = Middlewares()


from multiprocessing import Process, freeze_support
import time

import screen
import glob

def main():
    freeze_support()
    scr = screen.Screen()
    scr_t = Process(target=scr.start, args=('test.yaml',))
    scr_t.start()

    #Test
    glob.set_variable('venta', 0)
    while True:
        time.sleep(.1)
        glob.var_sum('venta', .1)
        if glob.get_variable('venta') >= 30:
            break
    return 0

if __name__ == '__main__':
    main()


import configargparse
import torch
import torchvision
import torch.nn as nn
from torch.utils.data import Dataset, DataLoader
from sklearn.model_selection import train_test_split
import numpy as np
import json
import os
import matplotlib.pyplot as plt
import tqdm

device = torch.device(
    'cuda') if torch.cuda.is_available() else torch.device('cpu')

def config_parser():
    parser = configargparse.ArgumentParser()
    parser.add_argument('--data_path', default='data\spike\snn_spike.json')
    parser.add_argument('--hidden_layer_dim', type=int, default=512)
    parser.add_argument('--num_layers', type=int, default=8)
    parser.add_argument('--lr', type=float, default=1e-3)
    parser.add_argument('--batch_size', type=int, default=16)
    parser.add_argument('--epochs', type=int, default=100)
    parser.add_argument('--i_save', type=int, default=10)
    parser.add_argument('--output_path', default='./output/')
    return parser

def read_data(args):
    with open(args.data_path, 'r') as f:
        datas = json.load(f)
    signals, labels = [], []
    for key in datas.keys():
        data = datas[key]
        labels.append(data['label'])
        signal = np.zeros((30, 5))
        for i, channel in enumerate(list(data.keys())[:5]):
            for j in data[channel]:
                signal[int(j), i] += 1
        signals.append(signal)
    return signals, labels

class classifier(nn.Module):
    def __init__(self, args):
        super(classifier, self).__init__()
        self.encoder_layer = nn.TransformerEncoderLayer(
            d_model=args.hidden_layer_dim, nhead=8, dim_feedforward=args.hidden_layer_dim, batch_first=True)
        self.transformer_encoder = nn.TransformerEncoder(
            self.encoder_layer, num_layers=args.num_layers)
        self.embedding = nn.Sequential(
            nn.LayerNorm(5),
            nn.Linear(5, args.hidden_layer_dim),
            nn.ReLU()
        )
        self.classifier = nn.Sequential(
            nn.ReLU(),
            nn.Linear(args.hidden_layer_dim, 6)
        )

    def forward(self, x):
        x = self.embedding(x)
        x = self.transformer_encoder(x)
        y = self.classifier(x[:, 0])
        return y

class dataset(Dataset):
    def __init__(self, signals, labels):
        self.signals, self.labels = signals, labels

    def __getitem__(self, index):
        signal = self.signals[index].astype('float32')
        label = self.labels[index]
        return torch.tensor(signal).to(device), torch.tensor(label).to(device)

    def __len__(self):
        return len(self.labels)

def test(net, loader):
    n = 0
    m = 0
    for index, (signal, label) in enumerate(loader):
        y = net(signal)
        if y.argmax() == label:
            m += 1
        n += 1
    return m/n

def main(args):
    signals, labels = read_data(args)
    signal_train, signal_test, label_train, label_test = train_test_split(
        signals, labels, test_size=0.2)
    print('train size:', len(label_train))
    print('test_size:', len(label_test))

    net = classifier(args).to(device)
    train_loader = DataLoader(
        dataset(signal_train, label_train), batch_size=args.batch_size, shuffle=True)
    train_sub_loader = DataLoader(
        dataset(signal_train, label_train), batch_size=1, shuffle=True)
    test_loader = DataLoader(
        dataset(signal_test, label_test), batch_size=1, shuffle=True)
    loss_fun = nn.CrossEntropyLoss()
    optimizer = torch.optim.SGD(net.parameters(), lr=args.lr,momentum=0.1,weight_decay=0.01)
    lr_shedular = torch.optim.lr_scheduler.CosineAnnealingLR(
        optimizer, args.epochs+30)

    train_loss = []
    train_acc = []
    test_acc = []
    epoch_n = []

    for epoch in tqdm.tqdm(range(args.epochs)):
        net.train()
        for index, (signal, label) in enumerate(train_loader):
            y = net(signal)
            loss = loss_fun(y, label)
            loss.backward()
            optimizer.step()
            optimizer.zero_grad()
            train_loss.append(float(loss))
        lr_shedular.step()

        if ((epoch+1) % args.i_save == 0) or epoch == 0:
            epoch_n.append(epoch+1)
            path = args.output_path+'exp'+str(epoch+1)+'/'
            try:
                os.mkdir(path)
            except:
                pass
            net.eval()
            train_acc.append(test(net, train_sub_loader))
            test_acc.append(test(net, test_loader))
            plt.plot(train_loss)
            plt.savefig(path+'loss.png')
            with open(path+'acc.json', 'w') as f:
                json.dump(
                    {'train': train_acc, 'test': test_acc, 'time': epoch_n}, f)
            # torch.save(net,path+'mode.pt')

if __name__ == '__main__':
    args = config_parser().parse_args()
    main(args)


#   HTMLTestRunner是基于unittest框架的一个扩展，可以自己在网上自行下载
import os
import smtplib
import unittest
import time
from email.header import Header
from email.mime.text import MIMEText

from lib.HTMLTestRunner import HTMLTestRunner

def send_mail(path):
    f = open(path,'rb') # 要读的内容 二进制格式
    mail_body = f.read() # 读取html报告的内容，作为邮件的正文
    f.close()

    # 要想发邮件，要把二进制的内容转成MIME格式
    # MIME mulitpurseduo 多用途 Internate 互联网 Mail 邮件 Extension 扩展
    # MIME这种格式是对邮件协议的一个扩展，使邮件不仅支持文本格式，还支持多种格式，如：图片、音频、二进制文件等

    msg = MIMEText(mail_body, 'html', 'utf-8')  # 把二进制的内容转为MIME格式

    # 上面是邮件的正文，但是对于一个邮件来讲，除了正文，还需要主题、发件人、收件人
    # msg是字典的类型，字典类似于数组，区别是：1.字典是无序的；2.

    msg['Subject'] = Header("自动化测试报告","utf-8")  # 设置邮件主题

    # 如果想用客户端软件或者自己写代码登录邮箱，很多类型的邮箱需要单独设置一个客户端授权码，这个是为了邮箱安全着想
    # 因为自己邮箱没有设置授权码，所以发件箱统一用老师的
    msg['From'] = 'bwftest126@126.com' # 设置发件箱
    # msg['To'] = '15032683126@163.com'  # 设置收件箱
    msg['To'] = '18401569214@163.com'  # 设置收件箱

    # 现在邮件内容已经准备好了，下面开始发送邮件
    # 发邮件手动步骤：
    # 1.打开登录页面，即连接邮箱服务器
    # 要想连接服务器，首先必须搞清楚网络传输协议
    # http,https,ftp,socket
    # 发邮件的协议一般有三种，要先查看一下邮箱支持哪种协议
    # 126邮箱支持这三种协议：pom3，smtp,imap
    # 我们要选一种传输协议，用来发邮件，选smtp
    # smtp simple mail transfer protocol 简单邮件传输协议
    # 首先导入smtplib的代码库
    smtp = smtplib.SMTP()   # 实例化一个SMTP类的对象
    smtp.connect("smtp.126.com")  # 连接126邮箱的服务器地址

    # 2.登录邮箱
    smtp.login("bwftest126@126.com", "abc123asd654")

    # 3.发送邮件
    # 注意：msg是MIME类型，需要转成String类型再发送
    smtp.sendmail('bwftest126@126.com', '15032683126@163.com', msg.as_string())
    smtp.sendmail('bwftest126@126.com', '18401569214@163.com', msg.as_string())
    smtp.sendmail('bwftest126@126.com', '2410064039@qq.com', msg.as_string())
    smtp.sendmail('bwftest126@126.com', 'bszhangf@isoftstone.com', msg.as_string())

    # 4.退出邮箱
    smtp.quit()
    print("email has send out!")

if __name__ == '__main__':
    # 时间戳
    # strftime: str 是String，f是format格式
    # strftime()通过这个方法可以定义时间的格式
    # Y-year年，m-month月，d-day日，H-hour小时，M-minute分，S-second秒
    now = time.strftime("%Y-%m-%d_%H-%M-%S")

    suite = unittest.defaultTestLoader.discover("./day5", pattern="*Test.py")
    # suite = unittest.defaultTestLoader.discover(".", pattern="*Test.py")
    # unittest.TextTestRunner() 文本测试用例运行器
    # 现在用html的测试用例运行器
    # html的测试用例运行器最终会生成一个html格式的测试报告
    # 我们至少要指定测试报告的路径
    base_path = os.path.dirname(__file__)
    print(base_path)
    path = base_path + "/report/report" + now + ".html" # 给测试报告文件名加上时间戳命名
    file = open(path, "wb")
    # stream:要创建的文件
    # title:生成的测试报告的网页标签名
    # description:描述，正文，该变量的值可以为空，为空时会默认设置正文
    HTMLTestRunner(stream=file, title="海盗商城测试报告",
                   description="测试环境:Window Server 2008 + Chrome").run(suite)
    file.close()
    # 我们要把html报告作为邮件正文，发送邮件
    send_mail(path)

    # 这时生成的测试报告，只显示类名和方法名，只能给专业的人士看
    # 我们应该把相关的手动测试用例的标题加到我们的测试报告里
    # 我们自动化测试用例是从手动测试用例中挑出来的，手工测试用例怎么
    # 写，我们就怎么编写代码，所以我们的代码里应该体现手工测试用例的标题
    # 新的测试报告会覆盖原来的测试报告，如果想把所有的测试报告保存起来要怎么做
    # 加一个时间戳，按照当前时间计算一个数字，把数字作为文件名的一部分就避免了文件名重复的问题
    # 现在我们的html格式的测试报告生成了，当测试用例全部执行完成应该生成一封提醒邮件，通知所有关心测试结果的人


import utility as utl
import torchnet.meter as tnt
import performance_tracker as pt
import checkpoint as ckpt
import summary
import sensitivity_analysis as sa
import collector
import logging
import config
import math
import time
import random
from collections import OrderedDict
from torch.autograd import Variable
from apex import amp
from functools import partial
import logging
import torch
import numpy as np

msglogger = logging.getLogger()
# Currently, YOLOv1 only follow the regression loss, the class meter thus remove at this stage:
def _log_training_progress(args, losses, epoch, steps_completed, steps_per_epoch, batch_time, optimizer, loggers):
    # Log some statistics
    errs = OrderedDict()

    #if not early_exit_mode(args):
    """
    Early exist model may be incorporated in the future.
    else:
        # For Early Exit case, the Top1 and Top5 stats are computed for each exit.
        for exitnum in range(args.num_exits):
            errs['Top1_exit' + str(exitnum)] = args.exiterrors[exitnum].value(1)
            errs['Top5_exit' + str(exitnum)] = args.exiterrors[exitnum].value(5)
    """
    stats_dict = OrderedDict()
    for loss_name, meter in losses.items():
        stats_dict[loss_name] = meter.mean

    stats_dict.update(errs)
    stats_dict['LR'] = optimizer.param_groups[0]['lr']
    stats_dict['Time'] = batch_time.mean
    stats = ('Performance/Training/', stats_dict)

    params = model.named_parameters() if args.log_params_histograms else None
    utl.log_training_progress(stats, params, epoch, steps_completed,
                              steps_per_epoch, args.print_freq, loggers)

def light_train_with_distiller(args, model, optimizer, compress_scheduler, device, dataloaders, dataset_sizes, loggers, epoch=1):

    """
    Training-with-compression loop for one epoch.
    IMPORTANT INFORMATION:

    For each training step in epoch:
        compression_scheduler.on_minibatch_begin(epoch)
        output = model(input)
        loss = criterion(output, target)
        compression_scheduler.before_backward_pass(epoch)
        loss.backward()
        compression_scheduler.before_parameter_optimization(epoch)
        optimizer.step()
        compression_scheduler.on_minibatch_end(epoch)
    """
    total_samples = dataset_sizes['train']
    batch_size = dataloaders["train"].batch_size

    steps_per_epoch = math.ceil(total_samples / batch_size)
    batch_time = tnt.AverageValueMeter()
    data_time = tnt.AverageValueMeter()

    OVERALL_LOSS_KEY = 'Overall Loss'
    #OBJECTIVE_LOSS_KEY = 'Objective Loss' # Only compute loss from the cost function, but we revise its name in Natural loss.
    NAT_LOSS_KEY = "Natural Loss"
    losses = OrderedDict([(OVERALL_LOSS_KEY, tnt.AverageValueMeter()),
                         (NAT_LOSS_KEY, tnt.AverageValueMeter())])

    model.train()
    end = time.time()
    for train_step, data in enumerate(dataloaders["train"], 0):
        inputs = Variable(data[0]).float().to(device)
        labels = Variable(data[1]).float().to(device)

        if compress_scheduler:
            compress_scheduler.on_minibatch_begin(epoch, train_step, steps_per_epoch, optimizer)

        nat_output = model(inputs, labels) # Tuple (loss, pred_tensor)
        losses[NAT_LOSS_KEY].add(nat_output[0].item())

        """
        ****
        Drop the early exist mode in this first version
        ****
        if not early_exit_mode(args):
        loss = criterion(output, target)
        # Measure accuracy
        classerr.add(output.detach(), target)
        acc_stats.append([classerr.value(1), classerr.value(5)])
        else:
        # Measure accuracy and record loss
        classerr.add(output[args.num_exits-1].detach(), target) # add the last exit (original exit)
        loss = earlyexit_loss(output, target, criterion, args)
        """

        if compress_scheduler:
            # Should be revised if using adversarial robustness training.
            agg_loss =  compress_scheduler.before_backward_pass(epoch, train_step, steps_per_epoch, nat_output[0],
                                                                optimizer=optimizer, return_loss_components=True)
            # Should be modified, this may be incorporated in the future.
            loss = agg_loss.overall_loss
            # if admm loss is zero, following line may raise error.
            losses[OVERALL_LOSS_KEY].add(loss.item())
            for lc in agg_loss.loss_components:
                if lc.name not in losses:
                    losses[lc.name] = tnt.AverageValueMeter()
                try:
                    losses[lc.name].add(lc.value.item())
                except:
                    # This is a constant case which may be raised by our ADMM implementation.
                    losses[lc.name].add(lc.value)
        else:
            losses[OVERALL_LOSS_KEY].add(nat_loss.item())
            loss = nat_loss

        optimizer.zero_grad()

        # ******************
        # Try apex AMP package:
        # ******************
        if args.apex:
            with amp.scale_loss(loss, optimizer) as scaled_loss:
                scaled_loss.backward()
        else:
            loss.backward()

        if compress_scheduler:
            # Applied zero gradient here.
            compress_scheduler.before_parameter_optimization(epoch, train_step, steps_per_epoch, optimizer)

        optimizer.step()
        if compress_scheduler:
            # Sometime this on "minibatch_end" function will not do anything.
            compress_scheduler.on_minibatch_end(epoch, train_step, steps_per_epoch, optimizer)

        batch_time.add(time.time() - end)
        steps_completed = (train_step + 1)

        if steps_completed % args.print_freq == 0 :
            # arguments for this function: losses, epoch, steps_completed, steps_per_epoch, batch_time, optimizer, loggers
            _log_training_progress(args, losses, epoch, steps_completed, steps_per_epoch, batch_time, optimizer, loggers)
        end = time.time()

    utl.log_weights_sparsity(model, epoch, loggers)
    return losses[NAT_LOSS_KEY], losses[OVERALL_LOSS_KEY]

def _log_valiation_progress(args, losses, epoch, steps_completed, steps_per_epoch, loggers):
    #if not _is_earlyexit(args):
    """
    Early exist model use following code:
    else:
        stats_dict = OrderedDict()
        for exitnum in range(args.num_exits):
            la_string = 'LossAvg' + str(exitnum)
            stats_dict[la_string] = args.losses_exits[exitnum].mean
            # Because of the nature of ClassErrorMeter, if an exit is never taken during the batch,
            # then accessing the value(k) will cause a divide by zero. So we'll build the OrderedDict
            # accordingly and we will not print for an exit error when that exit is never taken.
            if args.exit_taken[exitnum]:
                t1 = 'Top1_exit' + str(exitnum)
                t5 = 'Top5_exit' + str(exitnum)
                stats_dict[t1] = args.exiterrors[exitnum].value(1)
                stats_dict[t5] = args.exiterrors[exitnum].value(5)
    """

    stats_dict = OrderedDict(('Nat_Loss', losses['natural_loss'].mean))
    stats = ('Performance/Validation/', stats_dict)
    utl.log_training_progress(stats, None, epoch, steps_completed,
                                   steps_per_epoch, args.print_freq, loggers)

def _validate(args, dataloaders, data_group, model, device, loggers, epoch=-1, noise_factor=0):
    if epoch != -1:
        msglogger.info("----Validate (epoch=%d)----", epoch)

    losses = {'natural_loss': tnt.AverageValueMeter()}
    """
    Drop early exist model so far.
    if _is_earlyexit(args):
        # for Early Exit, we have a list of errors and losses for each of the exits.
        args.exiterrors = []
        args.losses_exits = []
        for exitnum in range(args.num_exits):
            args.exiterrors.append(tnt.ClassErrorMeter(accuracy=True, topk=(1, 5)))
            args.losses_exits.append(tnt.AverageValueMeter())
        args.exit_taken = [0] * args.num_exits
    """
    batch_time = tnt.AverageValueMeter()
    total_samples = len(dataloaders[data_group].sampler)
    batch_size = dataloaders[data_group].batch_size
    total_steps = total_samples / batch_size
    msglogger.info('%d samples (%d per mini-batch)', total_samples, batch_size)

    # For robustness testing, 20% of the testing data will be steralized.
    add_noise_steps =  int(total_steps * 0.2)
    end_range = int(total_steps) - add_noise_steps
    start_step = random.randint(0, end_range)
    start_step = 60
    end_step = start_step + add_noise_steps
    # Turn into evaluation mode.
    model.eval()
    end = time.time()
    # Starting primiary testing code here.
    with torch.no_grad():
        for validation_step, data in enumerate(dataloaders[data_group]):
            inputs = Variable(data[0]).float().to(device)
            labels = Variable(data[1]).float().to(device)

            if validation_step >= start_step and validation_step <= end_step and noise_factor:
                msglogger.info('%f is the factor multiplied in epoch %d', noise_factor, epoch)
                #print(validation_step)
                shape = inputs.shape
                # If using imagenet dataset:
                mean = np.array([0.0, 0.0, 0.0])
                std = np.array([1.0, 1.0, 1.0])
                min_val = (np.array([0,0,0]) - mean) / std
                max_val = (np.array([1,1,1]) - mean) / std
                noise = noise_factor * torch.randn(shape).to(device)

                """
                # for Plotting
                count = 0
                for fac in [0, 0.5, 1, 1.5, 2, 2.5, 3]:
                    temp_noise = fac * torch.randn(shape).to(device)
                    temp_inputs = inputs + temp_noise
                    for i in range(3):
                        temp_inputs[:, i, :, :] = torch.clamp(temp_inputs[:, i, :, :], min_val[i], max_val[i])
                    #temp_inputs = temp_inputs.numpy()
                    temp_inputs = temp_inputs[9, :, :, :].cpu().detach().numpy()

                    for i in range(3):
                        temp_inputs[i, :, :] = (temp_inputs[i, :, :] * std[i]) + mean[i]
                    temp_inputs = np.transpose(temp_inputs, (1, 2, 0))
                    def plot(img):
                        #x = x - np.min(x)
                        #x /= np.max(x)
                        img *= 255
                        img = img.astype(np.uint8)
                        img = img.reshape(224, 224, 3)
                        return img
                    plt.imsave(os.path.join(msglogger.logdir, str(count)+'.png'), plot(temp_inputs))
                    count+=1
                assert 1 == 2
                """

                inputs += noise
                for i in range(3):
                    inputs[:, i, :, :] = torch.clamp(inputs[:, i, :, :], min_val[i], max_val[i])

            nat_output = model(inputs, labels) # Tuple (loss, pred_tensor)

            # Early exist mode will incorporate in the near future.
            '''
            if not _is_earlyexit(args):
                # compute loss
                loss = criterion(output, target)
                # measure accuracy and record loss
                losses['objective_loss'].add(loss.item())
                classerr.add(output.detach(), target)
                if args.display_confusion:
                    confusion.add(output.detach(), target)
            else:
                earlyexit_validate_loss(output, target, criterion, args)
            '''

            losses['natural_loss'].add(nat_output[0].item())
            steps_completed = (validation_step + 1)
            batch_time.add(time.time() - end)
            end = time.time()
            steps_completed = (validation_step + 1)
            # "\033[0;37;40m\tExample\033[0m"
            if steps_completed % (args.print_freq) == 0 :
                _log_valiation_progress(args, losses, epoch, steps_completed, total_steps, [loggers[1]])

    stats_tmp = OrderedDict()
    stats_tmp['Nat_Loss'] = losses['natural_loss'].mean
    stats = ('Performance/Validation/', stats_tmp)

    utl.log_training_progress(stats, None, epoch, steps_completed=0,
                                    total_steps=1, log_freq=1, loggers=[loggers[0]])

    msglogger.info('==>  Nat_Loss: {:.5f} \n.'.format(losses['natural_loss'].mean))
    return losses['natural_loss'].mean

def trian_validate_with_scheduling(args, net, optimizer, compress_scheduler, device, dataloaders, dataset_sizes,
                                   loggers, tracker, epoch=1, validate=True, verbose=True):
    # Whtat's collectors_context
    # At first, we need to specify the model name, and its learning progress:
    #if not os.path.isdir(args.output_dir):
    #    os.mkdir(args.output_dir)
    #os.mkdir(args.output_dir, exist_ok=True)

    name = args.name
    if args.name == '':
        name = args.arch + "_" + args.dataset
    # Must exist pruning mode.
    # Reset learning rate and momentum buffer in the optimizer for next learning stage!
    # Should know whether the learning rate decay is based on epochs or steps
    # Or more, the meaning of last epoch argument indicates current epoch.
    #****
    # This line may raise the problems that the epoch doesn't exist any policy....
    #****
    if compress_scheduler:
        if compress_scheduler.prune_mechanism:
            if epoch == (compress_scheduler.pruner_info['max_epoch']):
                # Reset optimizer and learning rate in retrain phase.
                # NOTE: We should specify the true
                for index in range(len(compress_scheduler.policies[epoch])):
                    policy_name = compress_scheduler.policies[epoch][index].__class__.__name__.split("Policy")[0]
                    if policy_name == "LR":
                        compress_scheduler.policies[epoch][index].lr_scheduler.optimizer.param_groups[0]['lr'] = args.lr_retrain
                        compress_scheduler.policies[epoch][index].lr_scheduler.base_lrs = [args.lr_retrain]
                        compress_scheduler.policies[epoch][index].lr_scheduler.optimizer.param_groups[0]['momentum'] = 0.9
                        compress_scheduler.policies[epoch][index].lr_scheduler.optimizer.param_groups[0]['initial_lr'] = args.lr_retrain
                        for group in optimizer.param_groups:
                            for p in group['params']:
                                if 'momentum_buffer' in optimizer.state[p]:
                                    optimizer.state[p].pop('momentum_buffer', None)
                        break

            if epoch == (compress_scheduler.pruner_info['min_epoch']):
                # *****
                # NOTE If not using ADMM pruner, do we need to reset lr scheduler in this loop?
                # *****
                # Reset learning rate and momentum buffer for pruning stage!
                policy_name = compress_scheduler.policies[epoch][0].__class__.__name__.split("Policy")[0]
                #if policy_name != "ADMM":
                #    compress_scheduler.policies[epoch][0].lr_scheduler.optimizer.param_groups[0]['lr'] = args.lr_prune
                #    compress_scheduler.policies[epoch][0].lr_scheduler.base_lrs = [args.lr_prune]
                #    compress_scheduler.policies[epoch][0].lr_scheduler.optimizer.param_groups[0]['momentum'] = 0.9
                #    compress_scheduler.policies[epoch][0].lr_scheduler.optimizer.param_groups[0]['initial_lr'] = args.lr_prune
                for group in optimizer.param_groups:
                    group['lr'] = args.lr_prune
                    group['initial_lr'] = args.lr_prune
                    # for group in optimizer.param_groups:
                    for p in group['params']:
                        if 'momentum_buffer' in optimizer.state[p]:
                            optimizer.state[p].pop('momentum_buffer', None)

            if epoch >= compress_scheduler.pruner_info['max_epoch']:
                name  += "_retrain"

            elif epoch < compress_scheduler.pruner_info['min_epoch']:
                name += "_pretrain"

            else:
                name += "_prune"
    else:
        # Only proceed with pre-train or re-train phase model.
        name = name + "_" + args.stage

    if compress_scheduler:
        #dataset_name = 'val' if args.split_ratio != 0 else 'test'
        dataset_name = 'test'
        #data_group, model, criterion, device, num_classes, loggers, epoch=-1, noise_factor=0)

        # These two partial function is created for the channel pruning methods (Prof. Han):
        forward_fn = partial(_validate, args=args, dataloaders=dataloaders, data_group=dataset_name, device=device, loggers=loggers,   epoch=-1, noise_factor=0)

        #light_train_with_distiller(model, criterion, optimizer, compress_scheduler, device, num_classes,
        #                       dataset_sizes, loggers, epoch=1)

        train_fn = partial(light_train_with_distiller, args=args, optimizer=optimizer, compress_scheduler=compress_scheduler,
                               device=device, dataloaders =dataloaders, dataset_sizes=dataset_sizes, loggers=loggers, epoch=0)

        compress_scheduler.on_epoch_begin(epoch, optimizer, forward_fn=forward_fn, train_fn=train_fn)

    nat_loss, overall_loss = light_train_with_distiller(args, net, optimizer, compress_scheduler,
                                                          device, dataloaders, dataset_sizes, loggers, epoch)

    if validate:
        nat_loss = _validate(args, dataloaders, 'test', net, device, loggers, epoch=epoch)

    if compress_scheduler:
        # Or we can compute IoU here?
        loss = nat_loss
        #top1 = nat_loss
        compress_scheduler.on_epoch_end(epoch, optimizer, metrics={'min':loss}) #, 'max':top1})

    is_best, checkpoint_extras = _finalize_epoch(args, net, tracker, epoch, top1=nat_loss)
    # Check whether the out direcotry is already built.
    ckpt.save_checkpoint(epoch, args.arch, net, optimizer=optimizer,
                         scheduler=compress_scheduler, extras=checkpoint_extras,
                         is_best=is_best, name=name, dir=msglogger.logdir)

    #return top1, top5, loss, tracker
    return nat_loss, tracker

def _log_best_scores(args, performance_tracker, logger, how_many=-1):
    """
    Utility to log the best scores (minimum loss value).
    This function is currently written for pruning use-cases, but can be generalized.
    """
    assert isinstance(performance_tracker, (pt.SparsityAccuracyTracker))
    if how_many < 1:
        how_many = performance_tracker.max_len

    how_many = min(how_many, performance_tracker.max_len)
    best_scores = performance_tracker.best_scores(how_many)
    for score in best_scores:
        logger.info('==> Best [Nat_Loss: %.3f Sparsity:%.2f  NNZ-Params: %d on epoch: %d]',
                    -1*score.top1, score.sparsity, -score.params_nnz_cnt, score.epoch)

def _finalize_epoch(args, net, tracker, epoch, **kwargs):
    # ****
    # We mulitply -1 to the loss value so that the performance tracker can work well without any modifications.
    # ****
    loss_multiply = -1 * kwargs['top1']
    tracker.step(net, epoch, top1=loss_multiply, adv_train=False)
    _log_best_scores(args, tracker, msglogger)
    best_score = tracker.best_scores()[0]
    is_best = epoch == best_score.epoch
    checkpoint_extras = {'current_top1': kwargs['top1'],
                         'best_top1': -1 * best_score.top1,
                         'best_epoch': best_score.epoch}
    return is_best, checkpoint_extras


from datetime import datetime, timezone
from random import randint, shuffle

import config

daily_water_consumption_avg = 330

daily_water_consumption_in_percentage = (1.5, 0.5, 0.5, 0.5, 1.0, 2.0, 2.5, 4.0,
                                         6.0, 7.0, 6.0, 7.0, 6.0, 5.5, 4.0, 2.0, 6.0, 7.5, 8.0, 7.5, 5.5, 5.5, 2.5, 1.5)

def get_hourly_random_water_usage(current_hour_max):
    nb_of_step_per_hour = int(3600/config.delay)

    steps_per_hour = [0 for i in range(nb_of_step_per_hour)]

    total_hourly_random_water_usage = 0
    for step in range(nb_of_step_per_hour):
        random_value = randint(1, 3)
        res = random_value if (total_hourly_random_water_usage + random_value <=
                               current_hour_max) else 0
        total_hourly_random_water_usage += res
        steps_per_hour[step] = res

    shuffle(steps_per_hour)
    return steps_per_hour

def get_daily_random_water_usage():
    daily_water_usage = [-1 for i in range(24)]
    total_daily_random_water_usage = 0

    for hour in range(24):
        condition_for_more_water_usage = randint(0, 3) == 0
        delta = randint(1, 8) if condition_for_more_water_usage else 0
        hourly_percentage = daily_water_consumption_in_percentage[hour]
        current_hour_max = int(daily_water_consumption_avg *
                               hourly_percentage / 100 + delta)
        hourly_random_water_usage = get_hourly_random_water_usage(
            current_hour_max)
        daily_water_usage[hour] = hourly_random_water_usage
        total_daily_random_water_usage += current_hour_max

    return daily_water_usage

# def simulate_water_usage(time):
#     if ((time.hour > 21 and time.hour <= 23) or (time.hour >= 0 and time.hour < 7)):
#         return randint(0, 120) == 0

#     return randint(0, 25) == 0

def get_last_db_record(client, data_source: str):
    # return client.query('SELECT LAST(*) FROM {data_source}'.format(data_source=data_source.upper()))
    # return client.query('SELECT * FROM {data_source} GROUP BY * ORDER BY DESC LIMIT 1'.format(data_source=data_source.upper()))
    return client.query('SELECT * FROM {data_source} ORDER BY DESC LIMIT 1'.format(data_source=data_source.upper()))

def utc_to_local(utc_dt):
    return utc_dt.replace(tzinfo=timezone.utc).astimezone(tz=None)

daily_random_water_usage = None

def generate_random_entry(client, data_source: str, time: datetime):
    global daily_random_water_usage

    if(daily_random_water_usage is None or (time.hour == 0 and time.minute == 0)):
        print("Generating new daily water usage...")
        daily_random_water_usage = get_daily_random_water_usage()

    latest_record_query_res = get_last_db_record(client, data_source.upper())

    latest_record_list = list(latest_record_query_res.get_points())

    if(len(latest_record_list) == 0):
        return (0, 0)

    latest_record = latest_record_list[0]
    # latest_record = next(latest_record_query_res.get_points())

    latest_record_time_str = latest_record["time"]
    latest_record_time_date = utc_to_local(datetime.strptime(
        latest_record_time_str, "%Y-%m-%dT%H:%M:%S.%fZ")).day

    daily_consumption = latest_record["daily_consumption"] if latest_record_time_date == time.day else 0
    counter_index = latest_record["counter_index"]

    # new_value = randint(1, 10) if simulate_water_usage(time) else 0
    step = int((time.minute * 60 + time.second) / config.delay)
    new_value = daily_random_water_usage[time.hour][step]

    daily_consumption += new_value
    counter_index += new_value

    return (daily_consumption, counter_index)

if __name__ == "__main__":
    # now = datetime.now()
    # print(now.astimezone())
    fake_last_record_time_str = "2021-08-16T22:56:30.001017Z"
    fake_last_record_time_date = datetime.strptime(
        fake_last_record_time_str, "%Y-%m-%dT%H:%M:%S.%fZ")

    print(utc_to_local(fake_last_record_time_date))


import hashlib
import json
import logging
from decimal import Decimal
from functools import wraps
from typing import Callable, Optional

from django.conf import settings
from django.core.cache import caches
from helpers.json import CustomJSONEncoder
from rest_framework.request import Request
from rest_framework.response import Response
from sentry_sdk import capture_exception

logger = logging.getLogger("api_cache")

class CacheDecorator:
    """Decorator that can retrieve and save view's response."""

    def __init__(
        self, prefix=str, alias=str, timeout=int,
    ):
        self.prefix = prefix
        self.alias = alias
        self.timeout = timeout

    def __call__(self, func) -> Callable[..., Response]:
        """Handle request call."""

        @wraps(func)
        def cache_wrapper(_self, request: Request, *args, **kwargs) -> Response:
            response = self.cache_get(request, *args, **kwargs)
            if response:
                return response

            response = func(_self, request, *args, **kwargs)
            self.cache_save(response, request, *args, **kwargs)
            return response

        return cache_wrapper

    @property
    def cache(self):
        """Cache property."""
        return caches[self.alias]

    @classmethod
    def cache_build_hash(cls, request, *args, **kwargs) -> str:
        """Build hash from view's args and kwargs parameters."""
        parts = []

        for arg in args:
            parts.append(str(arg))

        kwarg_items = [(str(key), str(value)) for key, value in kwargs.items()]  # noqa: WPS221
        kwarg_items = kwarg_items + [
            (str(key), str(value)) for key, value in request.query_params.items()
        ]  # noqa: WPS221
        kwarg_items.sort()
        for key, value in kwarg_items:
            parts.append("{key}:{value}".format(key=key, value=value))

        return hashlib.sha256("-".join(parts).encode("utf-8")).hexdigest()

    def cache_build_key(self, request: Request, *args, **kwargs) -> str:
        """Build cache key, under which response will be retrieved and saved."""
        assert self.prefix, "{name}.cache_prefix cannot be empty".format(name=self.__class__.__name__)  # noqa: S101
        return "{prefix}-{lang}-{hash}".format(
            prefix=self.prefix, lang=request.LANGUAGE_CODE, hash=self.cache_build_hash(request, *args, **kwargs)
        )

    def cache_get(self, request: Request, *args, **kwargs) -> Optional[Response]:
        """Retrieve response from cache."""
        if not self.prefix:
            return None

        key = self.cache_build_key(request, *args, **kwargs)
        data = self.cache.get(key, b"")
        if data:
            try:
                data = Response(json.loads(data, parse_float=Decimal))
                logger.debug("Cache hit: %s" % request.path)
                return data
            except (TypeError, EOFError) as error:
                self.cache.delete(key)
                capture_exception(error)

    def cache_save(self, response: Response, request: Request, *args, **kwargs):
        """Save response in cache."""
        if not self.prefix:
            return

        if not isinstance(response, Response):
            return

        if response.status_code != 200:
            return

        key = self.cache_build_key(request, *args, **kwargs)
        try:
            self.cache.set(key, json.dumps(response.data, cls=CustomJSONEncoder), timeout=self.timeout)
            logger.debug("Cache saved: %s [timeout=%s]" % (request.path, self.timeout))
        except Exception as error:
            capture_exception(error)

    def cache_clear(self):
        """Clear cache for a given city."""
        for lang in dict(settings.LANGUAGES).keys():
            key = f"{self.prefix}-{lang}-*"
            self.cache.delete_pattern(key)

def cache(prefix, alias=settings.API_CACHE_ALIAS, timeout=settings.API_CACHE_TIMEOUT,) -> CacheDecorator:
    """Returns instance CacheDecorator."""
    return CacheDecorator(prefix, alias, timeout)


import sys
import os

import argparse
import time
import torch
import models.losses as ls
#import models.train as tr
from tqdm import tqdm
from typing import Callable, List, Optional, Iterator, Tuple, NamedTuple, Union
import random
from ithemal_utils import *
import training
import pandas as pd
#import utilities as ut

def main():
    # type: () -> None
    parser = argparse.ArgumentParser()

    # data arguments
    parser.add_argument('--data', required=True, help='The data file to load from')
    parser.add_argument('--embed-mode', help='The embedding mode to use (default: none)', default='none')
    parser.add_argument('--embed-size', help='The size of embedding to use (default: 256)', default=256, type=int)
    parser.add_argument('--hidden-size', help='The size of hidden layer to use (default: 256)', default=256, type=int)

    parser.add_argument('--use-rnn', action='store_true', default=False)
    rnn_type_group = parser.add_mutually_exclusive_group()
    rnn_type_group.add_argument('--rnn-normal', action='store_const', const=md.RnnType.RNN, dest='rnn_type')
    rnn_type_group.add_argument('--rnn-lstm', action='store_const', const=md.RnnType.LSTM, dest='rnn_type')
    rnn_type_group.add_argument('--rnn-gru', action='store_const', const=md.RnnType.GRU, dest='rnn_type')
    parser.set_defaults(rnn_type=md.RnnType.LSTM)

    rnn_hierarchy_type_group = parser.add_mutually_exclusive_group()
    rnn_hierarchy_type_group.add_argument('--rnn-token', action='store_const', const=md.RnnHierarchyType.NONE, dest='rnn_hierarchy_type')
    rnn_hierarchy_type_group.add_argument('--rnn-dense', action='store_const', const=md.RnnHierarchyType.DENSE, dest='rnn_hierarchy_type')
    rnn_hierarchy_type_group.add_argument('--rnn-multiscale', action='store_const', const=md.RnnHierarchyType.MULTISCALE, dest='rnn_hierarchy_type')
    rnn_hierarchy_type_group.add_argument('--rnn-linear-model', action='store_const', const=md.RnnHierarchyType.LINEAR_MODEL, dest='rnn_hierarchy_type')
    rnn_hierarchy_type_group.add_argument('--rnn-mop', action='store_const', const=md.RnnHierarchyType.MOP_MODEL, dest='rnn_hierarchy_type')
    parser.set_defaults(rnn_hierarchy_type=md.RnnHierarchyType.MULTISCALE)

    parser.add_argument('--rnn-skip-connections', action='store_true', default=False)
    parser.add_argument('--rnn-learn-init', action='store_true', default=False)
    parser.add_argument('--rnn-connect-tokens', action='store_true', default=False)

    sp = parser.add_subparsers(dest='subparser')

    train = sp.add_parser('train', help='Train an ithemal model')
    train.add_argument('--experiment-name', required=True, help='Name of the experiment to run')
    train.add_argument('--experiment-time', required=True, help='Time the experiment was started at')
    train.add_argument('--epochs', type=int, default=30, help='Number of epochs to run for')
    train.add_argument('--batch-size', type=int, default=32, help='The batch size to use in train')
    train.add_argument('--initial-lr', type=float, default=0.1, help='Initial learning rate')
    train.add_argument('--lr-decay-rate', default=1.2, help='LR division rate', type=float)

    cont = sp.add_parser('cont', help='Train an ithemal model')
    cont.add_argument('--experiment-name', required=True, help='Name of the experiment to run')
    cont.add_argument('--experiment-time', required=True, help='Time the experiment was started at')
    cont.add_argument('--epochs', type=int, default=30, help='Number of epochs to run for')
    cont.add_argument('--batch-size', type=int, default=32, help='The batch size to use in train')
    cont.add_argument('--initial-lr', type=float, default=0.1, help='Initial learning rate')
    cont.add_argument('--lr-decay-rate', default=1.2, help='LR division rate', type=float)
    cont.add_argument('--model',  help='Model architecture to use(dump file)', required=True)
    cont.add_argument('--model-data', help='Model data to use(mdl file)', required=True)

    args = parser.parse_args()

    base_params = BaseParameters(
        data=args.data,
        embed_mode=args.embed_mode,
        embed_size=args.embed_size,
        hidden_size=args.hidden_size,
        use_rnn=args.use_rnn,
        rnn_type=args.rnn_type,
        rnn_hierarchy_type=args.rnn_hierarchy_type,
        rnn_connect_tokens=args.rnn_connect_tokens,
        rnn_skip_connections=args.rnn_skip_connections,
        rnn_learn_init=args.rnn_learn_init,
    )

    if args.subparser == 'train':
        train_params = TrainParameters(
            experiment_name=args.experiment_name,
            experiment_time=args.experiment_time,
            epochs=args.epochs,
            batch_size=args.batch_size,
            initial_lr=args.initial_lr,
            lr_decay_rate=args.lr_decay_rate,
        )
        trainer = training.load_trainer(base_params,train_params)
        trainer.train()

    elif args.subparser == 'cont':
        train_params = TrainParameters(
            experiment_name=args.experiment_name,
            experiment_time=args.experiment_time,
            epochs=args.epochs,
            batch_size=args.batch_size,
            initial_lr=args.initial_lr,
            lr_decay_rate=args.lr_decay_rate,
        )
        trainer = training.load_continue(base_params,train_params, args.model, args.model_data)
        trainer.train()

    else:
        raise ValueError('Unknown mode "{}"'.format(args.subparser))

if __name__ == '__main__':
    main()


import webbrowser

from PySide6 import QtCore, QtWidgets

from mapclientplugins.sparccurationhelperstep.ui_sparccurationhelperwidget import Ui_SparcCurationHelperWidget
from sparc.curation.tools.utilities import convert_to_bytes
from sparc.curation.tools.ondisk import OnDiskFiles

class SparcCurationHelperWidget(QtWidgets.QWidget):

    def __init__(self, parent=None):
        super(SparcCurationHelperWidget, self).__init__(parent)
        self._ui = Ui_SparcCurationHelperWidget()
        self._ui.setupUi(self)
        self._ui.tabPlotAnnotation.setVisible(False)

        self._callback = None

        self._make_connections()

    def _make_connections(self):
        self._ui.pushButtonDocumentation.clicked.connect(self._documentationButtonClicked)
        self._ui.pushButtonDone.clicked.connect(self._done_button_clicked)

    def set_dataset_location(self, location):
        max_size = convert_to_bytes('3MiB')
        OnDiskFiles().setup_dataset(location, max_size)
        self._ui.tabContextAnnotation.update_info(location)
        self._ui.tabScaffoldAnnotation.update_annotations(location)

    def register_done_execution(self, callback):
        self._callback = callback

    def _documentationButtonClicked(self):
        webbrowser.open("https://abi-mapping-tools.readthedocs.io/en/latest/mapclientplugins.sparccurationhelperstep/docs/index.html")

    def _done_button_clicked(self):
        QtWidgets.QApplication.setOverrideCursor(QtCore.Qt.WaitCursor)
        self._ui.tabContextAnnotation.write_context_annotation()
        self._callback()
        QtWidgets.QApplication.restoreOverrideCursor()


from dataclasses import field
from rest_framework import serializers
from .models import Chat, Message
from django.contrib.auth.models import User
from django.db import transaction

class ChatViewSerializer(serializers.ModelSerializer):
    users = serializers.PrimaryKeyRelatedField(many=True,
                     queryset=User.objects.all())

    class Meta:
        model = Chat
        fields = ['id', 'name', 'users', 'chat_picture', 'type', 'messages']

class ChatViewPatchSerializer(serializers.ModelSerializer):
    users = serializers.PrimaryKeyRelatedField(many=True,
                     queryset=User.objects.all())

    class Meta:
        model = Chat
        fields = ['id', 'name', 'users', 'chat_picture', 'type', 'messages']

    @transaction.atomic
    def update(self, instance, validated_data):
        if 'users' in validated_data:
            users = validated_data.pop('users')[0]
            user = User.objects.get(id=users.id)
            instance.users.add(user)
            instance.save()

        return super().update(instance, validated_data)

# create serializer for sending message
class MessageSendSerializer(serializers.ModelSerializer):
    class Meta:
        model = Message
        fields = ['id', 'from_user', 'text', 'time', 'photo', 'file']

    @transaction.atomic
    def create(self, validated_data):
        # if 'chat_id' in validated_data:
        # print("I am here")
        request = self.context['request']
        from_user = validated_data['from_user']
        text = validated_data['text']
        message = Message.objects.create(from_user=from_user, text=text)
        chat = Chat.objects.get(id=request.data.get('chat_id'))
        chat.messages.add(message)

        # chat.messages.add(message)
        return message

class MessageViewSerializer(serializers.ModelSerializer):
    class Meta:
        model = Message
        fields = ['id', 'from_user', 'text', 'time', 'photo', 'file']

    # def create(self, validated_data):
    #     if 'chat_id' in validated_data:
    #         requested_chat = validated_data.get('chat_id')
    #         chat = Chat.objects.get(id=requested_chat)
    #         print(chat)
    # def create(self, validated_data):
    #     # if 'chat_id' in validated_data:
    #     print(validated_data['chat_id'])
    #     from_user = validated_data['from_user']
    #     text = validated_data['text']
    #     message = Message.objects.create(from_user=from_user, text=text)
    #     # chat = Chat.objects.get(id=validated_data['chat_id'])

    #     # chat.messages.add(message)
    #     return message


import numpy as np
import matplotlib.pyplot as plt
import glob
import os
import csv
from scipy.signal import savgol_filter
from numpy import mean

txtfiles=[]

def getFiles():
    for file in glob.glob("*.txt"):
        txtfiles.append(file)

def openFile():

    for i in range (txtfiles.__len__()):
        print("Press "+"{}".format(i+1)+" to open "+txtfiles[i])

    choice=int(input())
    f=open(txtfiles[choice-1])
    return f

def peakdetect(y_axis, x_axis = None, window = 50, delta = 0):
    """
    Wtitten based on a MATLAB script at http://billauer.co.il/peakdet.html

    Algorithm to detect local maximas and minmias in a signal.
    It detects peaks by searching for values which are surrounded by lower
    or larger values for maximas and minimas respectively

    keyword arguments:
    y_axis: List containg the signal over which to find peaks
    x_axis (optional): A x-axis whose values correspond to the y-axis and is used
        to obtain the x value at the maximas or minimas. (Default: 0 , return the
        index of the y-axis)

    window (optional): distance to look ahead from a peak candidate to
        determine if it is the actual peak (default: 50)

    delta (optional): specifies a minimum difference between a peak and
        the following points, before a peak may be considered a peak.
        Useful to allow for finer control over processing

    returns two lists [maxtab, mintab] containing the maximas and minimas
        respectively. Each cell of the lists contains a tupple of:
        (position, peak_value)
    """
    maxtab = []
    mintab = []
    dump = []   #Used to pop the first hit which always if false

    length = len(y_axis)

    # if x-axis isnt provided
    if x_axis is None:
        x_axis = range(length)

    #maxima and minima candidates are temporarily stored in mx and mn
    mn, mx = np.Inf, -np.Inf

    #Only detect peak if there is 'window' amount of points after it
    for index, (x, y) in enumerate(zip(x_axis[:-window], y_axis[:-window])):
        if y > mx:
            mx = y
            mxpos = x
        if y < mn:
            mn = y
            mnpos = x

        ####look for max####
        if y < mx-delta and mx != np.Inf:
            #Maxima peak candidate found
            #look ahead in signal to ensure that this is a peak and not jitter
            if y_axis[index:index+window].max() < mx:
                maxtab.append((mxpos, mx))
                dump.append(True)
                #set algorithm to only find minima now
                mx = np.Inf
                mn = np.Inf

        ####look for min####
        if y > mn+delta and mn != -np.Inf:
            #Minima peak candidate found
            #look ahead in signal to ensure that this is a peak and not jitter
            if y_axis[index:index+window].min() > mn:
                mintab.append((mnpos, mn))
                dump.append(False)
                #set algorithm to only find maxima now
                mn = -np.Inf
                mx = -np.Inf

    #Remove the false hit on the first value of the y_axis
    try:
        if dump[0]:
            maxtab.pop(0)
            #print "pop max"
        else:
            mintab.pop(0)
            #print "pop min"
        del dump
    except IndexError:
        #no peaks were found, should the function return empty lists?
        pass

    return maxtab, mintab

#fuction to filter signal
def filter(data_in,N,poly):

    filtered_sig= savgol_filter(data_in,N,poly)

    return filtered_sig

def outlierDeletion(data,N):
    mylist = data
    size = mylist.__len__()

    for i in range (size):
        if(mylist[i]>2*mean(mylist[i:i+N])):
            mylist[i]=mean(mylist[i:i+N])
        elif(mylist[i]<2*mean(mylist[i:i+20])):
            mylist[i]=mean(mylist[i:i+N])
        elif(mylist[i]<mean(mylist)):
            mylist[i]=mean(mylist[i:i+int(N/2)])
        elif(mylist[i]>mean(mylist)):
            mylist[i]=mean(mylist[i:i+int(N/2)])

if __name__=="__main__":

    voltage_current_pairs=[]
    voltage=[]
    current=[]

    #open file
    getFiles()

    for j in range(txtfiles.__len__()):
        file=open(txtfiles[j])

        #read file
        for line in file:
            csv_reader = csv.reader(file, delimiter=',')
            line_count = 0
            for row in csv_reader:
                voltage_current_pairs.append(row)

        #split current and voltage into seperate array
        i=0
        while i<voltage_current_pairs.__len__():
            voltage.append(voltage_current_pairs[i][0]) #store voltage only
            current.append(voltage_current_pairs[i][1]) #store current only
            i+=1

        #convert into numpy array
        y = np.asarray(current, dtype=np.float)
        x= np.asarray(voltage, dtype=np.float)

        #using algorithm to delete outliers
        window=3
        outlierDeletion(y,window)

        poly=2
        window=11
        y=filter(y,window,poly)

        _max, _min = peakdetect(y,x,10)
        xm = [p[0] for p in _max]
        ym = [p[1] for p in _max]
        xn = [p[0] for p in _min]
        yn = [p[1] for p in _min]

        # color=['r','g','b','k','m','r','r']
        # print(color[j])
        name=["blank calibration","150uM","50uM","250uM","blank","200uM","100uM","150uM"]

        plt.plot(x,y,label=name[j])
        plt.plot(xm,ym,'b+')
        plt.ticklabel_format(style='sci',scilimits=(0,0),axis='y')
        plt.xlabel('Voltage/V')
        plt.ylabel('Current/A')
        plt.title("P-aminophenol Calibration graph by square wave voltammetry")
        print(_max)

        voltage_current_pairs.clear()
        voltage.clear()
        current.clear()

    plt.legend(loc='best')
    plt.show()


from sqlalchemy_declarative import Person, Base, Address
from sqlalchemy import create_engine
engine = create_engine('sqlite:///sqlalchemy_example.db')
Base.metadata.bind = engine
from sqlalchemy.orm import sessionmaker
DBSession = sessionmaker()
DBSession.bind = engine
session = DBSession()
# Make a query to find all Persons in the database
cursor = session.query(Person).all()
for c in cursor:
    print(c.name)
print("---------")

# Return the first Person from all Persons in the database
person = session.query(Person).first()
print(person.name)

# Find all Address whose person field is pointing to the person object
session.query(Address).filter(Address.person == person).all()

# Retrieve one Address whose person field is point to the person object
session.query(Address).filter(Address.person == person).one()
address = session.query(Address).filter(Address.person == person).one()
print(address.post_code)


#!/usr/bin/env python

import os
import sys

from troposphere import (
     Parameter, Template,
     Output, GetAtt,
     Tags, Join,
     Ref, Sub,
     FindInMap,
)

from troposphere import (
     Equals, Not,
     Condition
)

from troposphere.ssm import (
     Parameter as SSMParam
)

from troposphere.servicediscovery import (
     Instance as MapInstance,
     PrivateDnsNamespace as VpcSpace,
)

from troposphere.route53 import (
     AliasTarget,
     HostedZoneConfiguration,
     HostedZoneVPCs,
     HostedZone
)

from troposphere.ec2 import (
     VPC as VPCType, VPCGatewayAttachment,
     VPCEndpoint, VPCEndpointConnectionNotification,
     VPCEndpointService, VPCEndpointServicePermissions,
     Subnet, SubnetRouteTableAssociation, SubnetNetworkAclAssociation,
     Route, RouteTable,
     InternetGateway, EgressOnlyInternetGateway, NatGateway,
     SecurityGroup, SecurityGroupRule, PortRange, NetworkAcl, NetworkAclEntry,
     NetworkInterfaceProperty, EIP, FlowLog,
     DHCPOptions, VPCDHCPOptionsAssociation
)

from ozone.outputs import object_outputs, comments_outputs
from string import ascii_lowercase as alpha
from troposphere.s3 import (
     BucketPolicy,
     Bucket
)

from ozone.resources.s3.bucket import S3Bucket

def generate_vpc_template(layers, az_count, cidr_block):
     TPL = Template()
     TPL.set_description('VPC - Version 2019-06-05')
     TPL.set_metadata({
               'Author': 'https://github.com/johnpreston'
     })
     VPC = VPCType(
          'VPC',
          CidrBlock=cidr_block,
          EnableDnsHostnames=True,
          EnableDnsSupport=True,
          Tags=Tags(
               Name=Ref('AWS::StackName'),
               EnvironmentName=Ref('AWS::StackName')
          )
     )
     IGW = InternetGateway("InternetGateway")
     IGW_ATTACH = VPCGatewayAttachment(
          "VPCGatewayAttachement",
          InternetGatewayId=Ref(IGW),
          VpcId=Ref(VPC)
     )
     DHCP_OPTIONS = DHCPOptions(
          'VpcDhcpOptions',
          DomainName=Sub(f'${{AWS::StackName}}.local'),
          DomainNameServers=['AmazonProvidedDNS'],
          Tags=Tags(
               Name=Sub(f'DHCP-${{{VPC.title}}}')
          )
     )
     DHCP_ATTACH = VPCDHCPOptionsAssociation(
          'VpcDhcpOptionsAssociate',
          DhcpOptionsId=Ref(DHCP_OPTIONS),
          VpcId=Ref(VPC)
     )
     DNS_HOSTED_ZONE = HostedZone(
          'VpcHostedZone',
          VPCs=[
               HostedZoneVPCs(
                    VPCId=Ref(VPC),
                    VPCRegion=Ref('AWS::Region')
               )
          ],
          Name=Sub(f'${{AWS::StackName}}.local'),
          HostedZoneTags=Tags(Name=Sub(f'ZoneFor-${{{VPC.title}}}'))
     )
     TPL.add_resource(VPC)
     TPL.add_resource(IGW)
     TPL.add_resource(IGW_ATTACH)
     TPL.add_resource(DHCP_OPTIONS)
     TPL.add_resource(DHCP_ATTACH)
     TPL.add_resource(DNS_HOSTED_ZONE)
     STORAGE_RTB = TPL.add_resource(RouteTable(
          'StorageRtb',
          VpcId=Ref(VPC),
          Tags=Tags(Name='StorageRtb')
     ))
     STORAGE_SUBNETS = []
     for count, subnet_cidr in zip(az_count, layers['stor']):
          subnet = Subnet(
               f'StorageSubnet{alpha[count].upper()}',
               CidrBlock=subnet_cidr,
               VpcId=Ref(VPC),
               AvailabilityZone=Sub(f'${{AWS::Region}}{alpha[count]}'),
               Tags=Tags(
                    Name=Sub(f'${{AWS::StackName}}-Storage-{alpha[count]}'),
                    Usage="Storage"
               )
          )
          subnet_assoc = TPL.add_resource(SubnetRouteTableAssociation(
               f'StorageSubnetAssoc{alpha[count].upper()}',
               SubnetId=Ref(subnet),
               RouteTableId=Ref(STORAGE_RTB)
          ))
          STORAGE_SUBNETS.append(subnet)
          TPL.add_resource(subnet)
     PUBLIC_RTB = TPL.add_resource(RouteTable(
          'PublicRtb',
          VpcId=Ref(VPC),
          Tags=Tags(Name='PublicRtb')
     ))
     PUBLIC_ROUTE = TPL.add_resource(Route(
          'PublicDefaultRoute',
          GatewayId=Ref(IGW),
          RouteTableId=Ref(PUBLIC_RTB),
          DestinationCidrBlock='0.0.0.0/0'
     ))
     PUBLIC_SUBNETS = []
     NAT_GATEWAYS = []
     for count, subnet_cidr in zip(az_count, layers['pub']):
          subnet = Subnet(
               f'PublicSubnet{alpha[count].upper()}',
               CidrBlock=subnet_cidr,
               VpcId=Ref(VPC),
               AvailabilityZone=Sub(f'${{AWS::Region}}{alpha[count]}'),
               MapPublicIpOnLaunch=True,
               Tags=Tags(Name=Sub(f'${{AWS::StackName}}-Public-{alpha[count]}'))
          )
          eip = TPL.add_resource(EIP(
               f"NatGatewayEip{alpha[count].upper()}",
               Domain='vpc'
          ))
          nat = NatGateway(
               f"NatGatewayAz{alpha[count].upper()}",
               AllocationId=GetAtt(eip, 'AllocationId'),
               SubnetId=Ref(subnet)
          )
          subnet_assoc = TPL.add_resource(SubnetRouteTableAssociation(
               f'PublicSubnetsRtbAssoc{alpha[count].upper()}',
               RouteTableId=Ref(PUBLIC_RTB),
               SubnetId=Ref(subnet)
          ))
          NAT_GATEWAYS.append(nat)
          PUBLIC_SUBNETS.append(subnet)
          TPL.add_resource(nat)
          TPL.add_resource(subnet)
     APP_SUBNETS = []
     APP_RTBS = []
     for count, subnet_cidr, nat in zip(az_count, layers['app'], NAT_GATEWAYS):
          SUFFIX = alpha[count].upper()
          subnet = Subnet(
               f'AppSubnet{SUFFIX}',
               CidrBlock=subnet_cidr,
               VpcId=Ref(VPC),
               AvailabilityZone=Sub(f'${{AWS::Region}}{alpha[count]}'),
               Tags=Tags(Name=Sub(f'${{AWS::StackName}}-App-{alpha[count]}'))
          )
          APP_SUBNETS.append(subnet)
          rtb = RouteTable(
               f'AppRtb{alpha[count].upper()}',
               VpcId=Ref(VPC),
               Tags=Tags(Name=f'AppRtb{alpha[count].upper()}')
          )
          APP_RTBS.append(rtb)
          route = Route(
               f'AppRoute{alpha[count].upper()}',
               NatGatewayId=Ref(nat),
               RouteTableId=Ref(rtb),
               DestinationCidrBlock='0.0.0.0/0'
          )
          subnet_assoc = SubnetRouteTableAssociation(
               f'SubnetRtbAssoc{alpha[count].upper()}',
               RouteTableId=Ref(rtb),
               SubnetId=Ref(subnet)
          )
          TPL.add_resource(subnet)
          TPL.add_resource(rtb)
          TPL.add_resource(route)
          TPL.add_resource(subnet_assoc)

     APP_S3_ENDPOINT = VPCEndpoint(
          'AppS3Endpoint',
          VpcId=Ref(VPC),
          RouteTableIds=[Ref(rtb) for rtb in APP_RTBS],
          ServiceName=Sub('com.amazonaws.${AWS::Region}.s3'),
          VpcEndpointType='Gateway',
     )
     PUBLIC_S3_ENDPOINT = VPCEndpoint(
          'PublicS3Endpoint',
          VpcId=Ref(VPC),
          RouteTableIds=[Ref(PUBLIC_RTB)],
          ServiceName=Sub('com.amazonaws.${AWS::Region}.s3'),
          VpcEndpointType='Gateway',
     )
     STORAGE_S3_ENDPOINT = VPCEndpoint(
          'StorageS3Endpoint',
          VpcId=Ref(VPC),
          RouteTableIds=[Ref(STORAGE_RTB)],
          ServiceName=Sub('com.amazonaws.${AWS::Region}.s3'),
          VpcEndpointType='Gateway')
     RESOURCES = []
     for count in az_count:
          resource = TPL.add_resource(EIP(
               f'Eip{count}',
               Domain='vpc'
          ))
          RESOURCES.append(resource)
     TPL.add_resource(APP_S3_ENDPOINT)
     TPL.add_resource(PUBLIC_S3_ENDPOINT)
     TPL.add_resource(STORAGE_S3_ENDPOINT)
     SG_RULES = []
     for subnet in layers['app']:
          RULE = SecurityGroupRule(
               IpProtocol="tcp",
               FromPort="443",
               ToPort="443",
               CidrIp=subnet,
          )
          SG_RULES.append(RULE)

     ENDPOINT_SG = TPL.add_resource(SecurityGroup(
          'VpcEndpointSecurityGroup',
          VpcId=Ref(VPC),
          GroupDescription='SG for all Interface VPC Endpoints',
          SecurityGroupIngress=SG_RULES,
          Tags=Tags(Name="sg-endpoints"),
     ))

     APP_SNS_ENDPOINT = VPCEndpoint(
          'AppSNSEndpoint',
          VpcId=Ref(VPC),
          SubnetIds=[Ref(subnet) for subnet in APP_SUBNETS],
          SecurityGroupIds=[
               GetAtt(ENDPOINT_SG, 'GroupId')
          ],
          ServiceName=Sub('com.amazonaws.${AWS::Region}.sns'),
          VpcEndpointType='Interface',
          PrivateDnsEnabled=True
     )
     TPL.add_resource(APP_SNS_ENDPOINT)

     APP_SQS_ENDPOINT = VPCEndpoint(
          'AppSQSEndpoint',
          VpcId=Ref(VPC),
          SubnetIds=[Ref(subnet) for subnet in APP_SUBNETS],
          SecurityGroupIds=[
               GetAtt(ENDPOINT_SG, 'GroupId')
          ],
          ServiceName=Sub('com.amazonaws.${AWS::Region}.sqs'),
          VpcEndpointType='Interface',
          PrivateDnsEnabled=True
     )
     TPL.add_resource(APP_SQS_ENDPOINT)

     APP_ECR_API_ENDPOINT = VPCEndpoint(
          'AppECRAPIEndpoint',
          VpcId=Ref(VPC),
          SubnetIds=[Ref(subnet) for subnet in APP_SUBNETS],
          SecurityGroupIds=[
               GetAtt(ENDPOINT_SG, 'GroupId')
          ],
          ServiceName=Sub('com.amazonaws.${AWS::Region}.ecr.api'),
          VpcEndpointType='Interface',
          PrivateDnsEnabled=True
     )
     TPL.add_resource(APP_ECR_API_ENDPOINT)

     APP_ECR_DKR_ENDPOINT = VPCEndpoint(
          'AppECRDKREndpoint',
          VpcId=Ref(VPC),
          SubnetIds=[Ref(subnet) for subnet in APP_SUBNETS],
          SecurityGroupIds=[
               GetAtt(ENDPOINT_SG, 'GroupId')
          ],
          ServiceName=Sub('com.amazonaws.${AWS::Region}.ecr.dkr'),
          VpcEndpointType='Interface',
          PrivateDnsEnabled=True
     )
     TPL.add_resource(APP_ECR_DKR_ENDPOINT)

     APP_SECRETS_MANAGER_ENDPOINT = VPCEndpoint(
          'AppSecretsManagerEndpoint',
          VpcId=Ref(VPC),
          SubnetIds=[Ref(subnet) for subnet in APP_SUBNETS],
          SecurityGroupIds=[
               GetAtt(ENDPOINT_SG, 'GroupId')
          ],
          ServiceName=Sub('com.amazonaws.${AWS::Region}.secretsmanager'),
          VpcEndpointType='Interface',
          PrivateDnsEnabled=True
     )
     TPL.add_resource(APP_SECRETS_MANAGER_ENDPOINT)

     APP_SSM_ENDPOINT = VPCEndpoint(
          'AppSSMEndpoint',
          VpcId=Ref(VPC),
          SubnetIds=[Ref(subnet) for subnet in APP_SUBNETS],
          SecurityGroupIds=[
               GetAtt(ENDPOINT_SG, 'GroupId')
          ],
          ServiceName=Sub('com.amazonaws.${AWS::Region}.ssm'),
          VpcEndpointType='Interface',
          PrivateDnsEnabled=True
     )
     TPL.add_resource(APP_SSM_ENDPOINT)

     APP_SSM_MESSAGES_ENDPOINT = VPCEndpoint(
          'AppSSMMessagesEndpoint',
          VpcId=Ref(VPC),
          SubnetIds=[Ref(subnet) for subnet in APP_SUBNETS],
          SecurityGroupIds=[
               GetAtt(ENDPOINT_SG, 'GroupId')
          ],
          ServiceName=Sub('com.amazonaws.${AWS::Region}.ssmmessages'),
          VpcEndpointType='Interface',
          PrivateDnsEnabled=True
     )
     TPL.add_resource(APP_SSM_MESSAGES_ENDPOINT)

     ################################################################################
     #
     # OUTPUTS
     #
     TPL.add_output(object_outputs(VPC, name_is_id=True))
     TPL.add_output(object_outputs(APP_SQS_ENDPOINT, name_is_id=True))
     TPL.add_output(object_outputs(APP_SNS_ENDPOINT, name_is_id=True))
     TPL.add_output(comments_outputs(
          [
               {'EIP': Join(',', [GetAtt(resource, "AllocationId") for resource in RESOURCES])},
               {'PublicSubnets': Join(',', [Ref(subnet) for subnet in PUBLIC_SUBNETS])},
               {'StorageSubnets': Join(',', [Ref(subnet) for subnet in STORAGE_SUBNETS])},
               {'ApplicationSubnets': Join(',', [Ref(subnet) for subnet in APP_SUBNETS])},
               {'StackName': Ref('AWS::StackName')},
               {'VpcZoneId': Ref(DNS_HOSTED_ZONE)}
          ]
     ))
     return TPL


from Estimator.geo_estimator import GeoEstimator
import numpy as np
from numpy.linalg import inv
import matplotlib.pyplot as plt

class Circle2D(GeoEstimator):
    def __init__(self, x_data, y_data, n):
        super().__init__(x_data, y_data, n)
        self.num_points_per_model = 3

    def find_model(self, sample):
        pt1 = sample[0]
        pt2 = sample[1]
        pt3 = sample[2]
        A = np.array([[pt2[0] - pt1[0], pt2[1] - pt1[1]], [pt3[0] - pt2[0], pt3[1] - pt2[1]]])
        B = np.array([[pt2[0] ** 2 - pt1[0] ** 2 + pt2[1] ** 2 - pt1[1] ** 2],
                      [pt3[0] ** 2 - pt2[0] ** 2 + pt3[1] ** 2 - pt2[1] ** 2]])
        inv_A = inv(A)
        c_x, c_y = np.dot(inv_A, B) / 2
        c_x, c_y = c_x[0], c_y[0]
        r = np.sqrt((c_x - pt1[0]) ** 2 + (c_y - pt1[1]) ** 2)
        return c_x, c_y, r

    def eval_model(self, model):
        d = 0
        c_x, c_y, r = model
        for i in range(len(self.x_data)):
            dis = np.sqrt((self.x_data[i] - c_x) ** 2 + (self.y_data[i] - c_y) ** 2)
            if dis >= r:
                d += dis - r
            else:
                d += r - dis
        return d

    def get_plt_geo(self):
        return plt.Circle((self.best_model[0], self.best_model[1]), radius=self.best_model[2], color='r', fc='y', fill=False)


import os
import nmap

print("hello world")
a = [1, 3, 5, 8]

#print(a)

'''
for i in a:
    print(i)

a = 2
b = 2

if a == b :
    print('equal')
'''
#files = os.system('ls')
#for file in files:
#print(files)

def sum(first, second, **kwargs):
    return first + second

#print(dir(os))

#print(sum(2, 5))

'''
class Animal:
    def __init__(self, name):
        self.name = name

    def get_name(self):
        return self.name

pet = Animal(name="seru")

print(pet.get_name())

for i in range(50, 100):
    print(i)
'''

double = lambda a: a * 2

print(double(5))

#setInterval(() => { } , 10)

aa= 'nme'
a = aa if aa is 'name' else 'asd'
#print(a)

'''
with open('file.txt', 'w') as f:
    print(f.write("asddsaasdasd"))

try:
    print(12/0)
except Exception as e:
    print(e)
'''

files = os.system('ps')
print(files)

nm = nmap.PortScannerAsync()
print(nm)


#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Sat Dec  1 16:26:35 2018

@author: user1
"""

import tensorflow as tf
from PIL import Image
import numpy as np
INPUT_SHAPE = (299,299,3)

trainf = tf.python_io.tf_record_iterator("../input_tf/Train-1.tfrecords")
ite = next(trainf)

def augment(image):
    image = tf.image.random_flip_left_right(image)
    image = tf.image.random_flip_up_down(image)
    nk = tf.random_uniform([], minval=0, maxval=4, dtype=tf.int32)
    image = tf.image.rot90(image, k = nk)
    return image

def parse_fn(example):
  "Parse TFExample records and perform simple data augmentation."
  example_fmt = {
    "height": tf.io.FixedLenFeature((), tf.int64, -1),
    "width": tf.io.FixedLenFeature((), tf.int64, -1),
    "channel": tf.io.FixedLenFeature((), tf.int64, -1),
    "image": tf.io.FixedLenFeature((), tf.string, ""),
    "label": tf.io.FixedLenFeature((), tf.string, ""),
  }
  parsed = tf.parse_single_example(example, example_fmt)
  image = tf.decode_raw(parsed["image"], tf.uint8)
  image = tf.reshape(image,[parsed["height"],parsed["width"],4])
  image = tf.div(tf.cast(image,tf.float32), 255.0)

  image = tf.stack([tf.reshape(image[:,:,0],[parsed["height"],parsed["width"]])
                  ,tf.reshape(image[:,:,1],[parsed["height"],parsed["width"]])
                  ,tf.reshape(image[:,:,2],[parsed["height"],parsed["width"]])
                  +tf.reshape(image[:,:,3],[parsed["height"],parsed["width"]])],axis=2)

  image = tf.slice(image,[0,0,0],[parsed["height"],parsed["width"],3])
  image = tf.image.resize_images(image, (INPUT_SHAPE[0], INPUT_SHAPE[1]))
  image = augment(image)

  labels = tf.decode_raw(parsed["label"], tf.uint8)
  labels = tf.cast(labels, tf.float32)
  return image, labels

image, labels = parse_fn(ite)

with tf.Session() as sess:
    image_tf = sess.run(tf.contrib.image.rotate(image,tf.random.uniform([1])))
    labels_tf = sess.run(labels)
    r = sess.run(tf.random.uniform([1]))
    #test = sess.run(parse_fn(ite))

#test.shape

#RGB = Image.fromarray((test*255).astype(np.uint8))
#RGB.show()

#(image_tf*255).astype(int).shape

RGB = Image.fromarray((image_tf*255).astype(np.uint8))
RGB.show()

file0.tfrecord: [0, 1]
file1.tfrecord: [2, 3]
file2.tfrecord: [4]

with tf.python_io.TFRecordWriter('./file2.tfrecord') as writer:
    for r in range(4,5):
        example = tf.train.Example(features=tf.train.Features(feature={
                "n": tf.train.Feature(int64_list=tf.train.Int64List(value=[r]))}))
        writer.write(example.SerializeToString())
    writer.close()

files = ["file{}.tfrecord".format(i) for i in range(3)]
files = tf.data.Dataset.from_tensor_slices(files)
dataset = files.interleave(lambda x: tf.data.TFRecordDataset(x),
                           cycle_length=3, block_length=1)

dataset = dataset.repeat(None)

iterator = dataset.make_one_shot_iterator()
x = iterator.get_next()

with tf.Session() as sess:
    for _ in range(12):
        print(sess.run(x))

x = tf.constant([0.9, 2.55, 2.3, 1.5, -4.5])
rnd =tf.round(x)

with tf.Session() as sess:
    print(sess.run(x[0]))


import qrcode
from tempfile import NamedTemporaryFile
from reportlab.lib.pagesizes import letter
from reportlab.lib.units import inch
from reportlab.pdfgen import canvas

# Function to generate a QR code image
def generate_qr_code_image(data, scale):
    qr = qrcode.QRCode(
        version=1,
        error_correction=qrcode.constants.ERROR_CORRECT_H,
        box_size=scale,
        border=4,
    )
    qr.add_data(data)
    qr.make(fit=True)
    qr_img = qr.make_image(fill_color="black", back_color="white")
    # qr_img = qr_img.resize((scale, scale))
    return qr_img

# Function to generate a PDF with a grid of QR codes
def generate_qr_code_pdf(data, output_path, qr_per_row=3,qr_per_col=3, margin=0.5, qr_scale=100):
    qr_img = generate_qr_code_image(data, qr_scale)
    qr_width = qr_img.width
    qr_height = qr_img.height

    c = canvas.Canvas(output_path, pagesize=letter)

    page_width, page_height = letter
    available_width = page_width # - 2 * inch
    available_height = page_height # - 2 * inch

    qr_margin = margin * inch
    qr_size = min((available_width - (qr_per_row - 1) * qr_margin) / qr_per_row , (available_height - (qr_per_col - 1) * qr_margin) / qr_per_col)

    x = qr_margin / 2 # + inch
    y = page_height - qr_size - qr_margin / 2 # - inch

    for i in range(qr_per_col):
        for j in range(qr_per_row):
            with NamedTemporaryFile(delete=False) as tmp_file:
                qr_img.save(tmp_file.name, format='PNG')
                c.drawImage(tmp_file.name, x, y, qr_size, qr_size)
            x += qr_size + qr_margin

        x = qr_margin / 2 # + inch
        y -= qr_size + qr_margin

    c.save()


"""Calculate statistics on simulated example.
This program uses the functions in :file:`get_T.py` to calculate
the t-statistics of 2 arrays. The results are saved in bin.
the files used will be downloaded if they are not in the working directory
of the IPython engines.
"""

from __future__ import print_function

from IPython.parallel import Client
import numpy as np
from timeit import default_timer as clock
from permustat import perm_t

# simulated data
A = np.arange(120).reshape((2,3,20))
B = np.arange(180).reshape((2,3,30))

nSim = 150

# Connect to the IPython cluster
c = Client()
c[:].run('./permustat/perm_t.py')

# the number of engines
n = len(c)
nSimn = nSim/n
id0 = c.ids[0]
v = c[:]
v.block = True
#fetch the pi-files
#print("downloading %i files of pi"%n)
#v.map(fetch_pi_file, files[:n])
v.map(perm_t.get_Ts, A, B, nSimn)
print("done")

# Run 10m digits on 1 engine
t1 = clock()
Ts1 = c[id0].apply_sync(perm_t.get_Ts, A, B, nSim)
t2 = clock()
digits_per_second1 = 10.0e6/(t2-t1)
print("Digits per second (1 core, 10m digits):   ", digits_per_second1)

# Run n*10m digits on all engines
t1 = clock()
Ts_each = v.map(perm_t.get_Ts, A, B, nSimn)
Ts_all = reduce_freqs(Ts_each)
t2 = clock()
digits_per_second8 = n*10.0e6/(t2-t1)
print("Digits per second (%i engines, %i0m digits): "%(n,n), digits_per_second8)

print("Speedup: ", digits_per_second8/digits_per_second1)

# plot_two_digit_freqs(freqs150m)
# plt.title("2 digit sequences in %i0m digits of pi"%n)
# plt.show()


# -*- coding: utf-8 -*-
"""
Created on Fri Feb 10 14:34:25 2023

@author: MICRO
"""

import cv2
from matplotlib import pyplot as plt
import numpy as np

x=np.arange(256)
y=np.sin(2*np.pi*x/50)

y+=max(y)

y_img=np.array([[y[j]*127 for j in range(256)] for i in range(256)],dtype=np.uint8)

plt.imshow(y_img)

img=cv2.imread('images/onion.png',0)

dft=cv2.dft(np.float32(img),flags=cv2.DFT_COMPLEX_OUTPUT)

dft=np.fft.fftshift(dft) #shift to center

magnitude_spectrum = 20*np.log((cv2.magnitude(dft[:,:,0],dft[:,:,1]))+1) #to rise negative value

fig=plt.figure(figsize=(12,12))
ax1=fig.add_subplot(2,2,1)
ax1.imshow(img)
ax1.title.set_text('Input image')

ax2=fig.add_subplot(2,2,2)
ax2.imshow(magnitude_spectrum)
ax2.title.set_text('FFT image')


import prompter
from pdfminer.high_level import extract_text
from connection import ask

def run():

    prompt = """
        The following file is a class study plan. Please provide some recommendations considering the following information:
    """

    #map subject to subject enum

    text = extract_text("api/example.pdf")

    prompt += text

    print(ask(prompt))

run()


from config import *
from utilities import *
import random
import tweepy

#driver.get(twitter_url)
products = list( db.products.find( {'published_at': False, 'discount_percentage' : {'$gt': 20} } ).limit(25) )
# personal information
consumer_key =""
consumer_secret =""
access_token =""
access_token_secret =""

# authentication
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)

api = tweepy.API(auth)

for product in products:
    tweets = [f"الحق خصم {product['discount_percentage']}% على {product['product_title']} ",
         f"جوميا عاملالك خصم {product['discount_percentage']}% على {product['product_title']} ",
         f"متفكرش كتير خصم {product['discount_percentage']}% على {product['product_title']} ",
         f"لو حابب تشتري {product['product_title']} الحق بسرعة عشان جوميا موفرة عليك {product['discount_percentage']}% وهتشتريه ب{product['current_price']} بدل من {product['old_price']}  جنيه"
             ]
    tweet = random.choice(tweets)
    tweet = " ".join([ word for word in tweet.replace('\n', ' ').strip().split(' ')  if word.strip() != '' ] )
    tweet += '\n'
    tweet += f"{product['affiliate_url']}"
    try:
        print(tweet)
        if tweet != '\n':
            api.update_status(tweet)
        else:
            pass
    except tweepy.TweepError as e:

        print(e.reason)
    sleep(10)