from uuid import uuid4

from storm.locals import Not

from fluiddb.data.namespace import Namespace, createNamespace
from fluiddb.data.object import getDirtyObjects, DirtyObject
from fluiddb.data.permission import (
    NamespacePermission, Operation, Policy, TagPermission, createTagPermission,
    getTagPermissions)
from fluiddb.data.system import createSystemData
from fluiddb.data.tag import Tag, createTag, getTags
from fluiddb.data.value import createTagValue, getTagValues
from fluiddb.exceptions import FeatureError
from fluiddb.model.exceptions import DuplicatePathError
from fluiddb.model.permission import PermissionAPI
from fluiddb.model.tag import TagAPI
from fluiddb.model.user import UserAPI, getUser
from fluiddb.model.value import TagValueAPI
from fluiddb.testing.basic import FluidinfoTestCase
from fluiddb.testing.resources import ConfigResource, DatabaseResource

class TagAPITestMixin(object):

    def assertDefaultPermissions(self, user, tagPath):
        """
        Assert that a L{TagPermission} exists for the specified L{Tag.path}
        and that it uses the default system-wide policy.
        """
        tag, permission = getTagPermissions([tagPath]).one()
        self.assertEqual(Policy.CLOSED, permission.updatePolicy)
        self.assertEqual([user.id], permission.updateExceptions)
        self.assertEqual(Policy.CLOSED, permission.deletePolicy)
        self.assertEqual([user.id], permission.deleteExceptions)
        self.assertEqual(Policy.CLOSED, permission.controlPolicy)
        self.assertEqual([user.id], permission.controlExceptions)
        self.assertEqual(Policy.CLOSED, permission.writeValuePolicy)
        self.assertEqual([user.id], permission.writeValueExceptions)
        self.assertEqual(Policy.OPEN, permission.readValuePolicy)
        self.assertEqual([], permission.readValueExceptions)
        self.assertEqual(Policy.CLOSED, permission.deleteValuePolicy)
        self.assertEqual([user.id], permission.deleteValueExceptions)
        self.assertEqual(Policy.CLOSED, permission.controlValuePolicy)
        self.assertEqual([user.id], permission.controlValueExceptions)

    def testCreateWithoutData(self):
        """
        L{SecureTagAPI.create} raises a L{FeatureError} when no L{Tag}
        data is provided.
        """
        self.assertRaises(FeatureError, self.tags.create, [])

    def testCreate(self):
        """L{TagAPI.create} creates new L{Tag}s using the provided data."""
        values = [(u'username/tag', u'A description')]
        self.tags.create(values)
        systemTags = self.system.tags.keys()
        tag = self.store.find(Tag, Not(Tag.path.is_in(systemTags))).one()
        self.assertEqual(u'username/tag', tag.path)
        self.assertEqual(u'tag', tag.name)
        self.assertIdentical(self.user.namespace, tag.namespace)

    def testCreateCreatesPermissions(self):
        """
        L{TagAPI.create} creates permissions for the newly created L{Tag}s.
        """
        self.tags.create([(u'username/tag', u'A description')])
        self.assertDefaultPermissions(self.user, u'username/tag')

    def testCreateChildTagInheritsPermissions(self):
        """
        L{TagAPI.create} creates new L{Namespace}s based on the provided data.
        """
        UserAPI().create([(u'user', u'secret', u'name', u'user@example.com')])
        user = getUser(u'user')
        TagAPI(user).create([(u'user/tag', u'A description')])
        self.assertDefaultPermissions(user, u'user/tag')

    def testCreateChildTagInheritsParentNamespacePermissions(self):
        """
        L{TagAPI.create} creates new L{Tag}s with permissions inherited from
        the parent L{Namespace}'s permissions.
        """
        PermissionAPI(self.user).set([
            (u'username', Operation.CREATE_NAMESPACE, Policy.CLOSED,
             [u'username']),
            (u'username', Operation.UPDATE_NAMESPACE, Policy.OPEN, []),
            (u'username', Operation.DELETE_NAMESPACE, Policy.OPEN, []),
            (u'username', Operation.LIST_NAMESPACE, Policy.CLOSED,
             [u'username']),
            (u'username', Operation.CONTROL_NAMESPACE, Policy.OPEN, [])])

        self.tags.create([(u'username/tag', u'A child tag')])
        result = getTagPermissions([u'username/tag'])
        tag, permission = result.one()
        self.assertEqual((Policy.CLOSED, [self.user.id]),
                         permission.get(Operation.UPDATE_TAG))
        self.assertEqual((Policy.CLOSED, [self.user.id]),
                         permission.get(Operation.DELETE_TAG))
        self.assertEqual((Policy.OPEN, []),
                         permission.get(Operation.CONTROL_TAG))
        self.assertEqual((Policy.CLOSED, [self.user.id]),
                         permission.get(Operation.WRITE_TAG_VALUE))
        self.assertEqual((Policy.CLOSED, [self.user.id]),
                         permission.get(Operation.READ_TAG_VALUE))
        self.assertEqual((Policy.CLOSED, [self.user.id]),
                         permission.get(Operation.DELETE_TAG_VALUE))
        self.assertEqual((Policy.OPEN, []),
                         permission.get(Operation.CONTROL_TAG_VALUE))

    def testCreateChildTagWithDelegatedCreator(self):
        """
        L{TagAPI.create} always ensures that a new L{Tag} is usable by the
        L{User} that created it.  In the case of default permissions, the user
        creating the new L{Tag} is granted L{Operation.UPDATE_TAG},
        L{Operation.DELETE_TAG}, L{Operation.WRITE_TAG_VALUE} and
        L{Operation.DELETE_TAG_VALUE}.
        """
        UserAPI().create(
            [(u'friend', u'secret', u'name', u'user1@example.com')])
        PermissionAPI(self.user).set(
            [(u'username', Operation.CREATE_NAMESPACE,
              Policy.CLOSED, [u'username', u'friend'])])

        friend = getUser(u'friend')
        TagAPI(friend).create([(u'username/tag', u'A shared tag')])
        result = getTagPermissions([u'username/tag'])
        tag, permission = result.one()
        self.assertEqual((Policy.CLOSED, [self.user.id, friend.id]),
                         permission.get(Operation.UPDATE_TAG))
        self.assertEqual((Policy.CLOSED, [self.user.id, friend.id]),
                         permission.get(Operation.DELETE_TAG))
        self.assertEqual((Policy.CLOSED, [self.user.id]),
                         permission.get(Operation.CONTROL_TAG))
        self.assertEqual((Policy.CLOSED, [self.user.id, friend.id]),
                         permission.get(Operation.WRITE_TAG_VALUE))
        self.assertEqual((Policy.OPEN, []),
                         permission.get(Operation.READ_TAG_VALUE))
        self.assertEqual((Policy.CLOSED, [self.user.id, friend.id]),
                         permission.get(Operation.DELETE_TAG_VALUE))
        self.assertEqual((Policy.CLOSED, [self.user.id]),
                         permission.get(Operation.CONTROL_TAG_VALUE))

    def testCreatePublicChildTagWithDelegatedCreator(self):
        """
        L{TagAPI.create} always ensures that a new L{Tag} is usable by the
        L{User} that created it.
        """
        UserAPI().create(
            [(u'friend', u'secret', u'name', u'user1@example.com')])
        PermissionAPI(self.user).set(
            [(u'username', Operation.CREATE_NAMESPACE, Policy.OPEN, [])])

        friend = getUser(u'friend')
        TagAPI(friend).create([(u'username/tag', u'A shared tag')])
        result = getTagPermissions([u'username/tag'])
        tag, permission = result.one()
        self.assertEqual((Policy.OPEN, []),
                         permission.get(Operation.UPDATE_TAG))
        self.assertEqual((Policy.OPEN, []),
                         permission.get(Operation.DELETE_TAG))
        self.assertEqual((Policy.CLOSED, [self.user.id]),
                         permission.get(Operation.CONTROL_TAG))
        self.assertEqual((Policy.OPEN, []),
                         permission.get(Operation.WRITE_TAG_VALUE))
        self.assertEqual((Policy.OPEN, []),
                         permission.get(Operation.READ_TAG_VALUE))
        self.assertEqual((Policy.OPEN, []),
                         permission.get(Operation.DELETE_TAG_VALUE))
        self.assertEqual((Policy.CLOSED, [self.user.id]),
                         permission.get(Operation.CONTROL_TAG_VALUE))

    def testCreatePrivateChildTagWithDelegatedCreator(self):
        """
        L{TagAPI.create} always ensures that a new L{Tag} is usable by the
        L{User} that created it.  If the L{Operation.READ_TAG_VALUE}
        permission is L{Policy.CLOSED} the creator is added to the exceptions
        list.
        """
        UserAPI().create(
            [(u'friend', u'secret', u'name', u'user1@example.com')])
        PermissionAPI(self.user).set(
            [(u'username', Operation.CREATE_NAMESPACE, Policy.CLOSED,
              [u'username', u'friend']),
             (u'username', Operation.LIST_NAMESPACE, Policy.CLOSED,
              [u'username', u'friend'])])

        friend = getUser(u'friend')
        TagAPI(friend).create([(u'username/tag', u'A shared tag')])
        result = getTagPermissions([u'username/tag'])
        tag, permission = result.one()
        self.assertEqual((Policy.CLOSED, [self.user.id, friend.id]),
                         permission.get(Operation.UPDATE_TAG))
        self.assertEqual((Policy.CLOSED, [self.user.id, friend.id]),
                         permission.get(Operation.DELETE_TAG))
        self.assertEqual((Policy.CLOSED, [self.user.id]),
                         permission.get(Operation.CONTROL_TAG))
        self.assertEqual((Policy.CLOSED, [self.user.id, friend.id]),
                         permission.get(Operation.WRITE_TAG_VALUE))
        self.assertEqual((Policy.CLOSED, [self.user.id, friend.id]),
                         permission.get(Operation.READ_TAG_VALUE))
        self.assertEqual((Policy.CLOSED, [self.user.id, friend.id]),
                         permission.get(Operation.DELETE_TAG_VALUE))
        self.assertEqual((Policy.CLOSED, [self.user.id]),
                         permission.get(Operation.CONTROL_TAG_VALUE))

    def testCreateStoresTagDescriptions(self):
        """
        L{TagAPI.create} creates new C{fluiddb/tags/description} L{TagValue}s
        to store the specified L{Tag} descriptions.
        """
        values = [(u'username/tag', u'A tag description')]
        [(objectID, path)] = self.tags.create(values)
        tag = getTags(paths=[u'fluiddb/tags/description']).one()
        value = getTagValues([(objectID, tag.id)]).one()
        self.assertEqual(u'A tag description', value.value)

    def testCreateStoresTagPaths(self):
        """
        L{TagAPI.create} creates new C{fluiddb/tags/path} L{TagValue}s to store
        the specified L{Tag} paths.
        """
        values = [(u'username/tag', u'A tag description')]
        [(objectID, path)] = self.tags.create(values)
        tag = getTags(paths=[u'fluiddb/tags/path']).one()
        value = getTagValues([(objectID, tag.id)]).one()
        self.assertEqual(u'username/tag', value.value)

    def testCreateCreatesAboutTag(self):
        """
        L{TagAPI.create} creates new C{fluiddb/about} L{TagValue}s when
        creating new L{Tag}s.
        """
        values = [(u'username/tag', u'A tag description')]
        [(objectID, path)] = self.tags.create(values)
        tag = getTags(paths=[u'fluiddb/about']).one()
        value = getTagValues([(objectID, tag.id)]).one()
        self.assertEqual(u'Object for the attribute username/tag', value.value)

    def testCreateReusesPreviousObjectIDs(self):
        """
        If a L{Tag} is deleted and created again, L{TagAPI.create}
        uses the old object ID.
        """
        values = [(u'username/tag', u'A tag description')]
        [(firstObjectID, path)] = self.tags.create(values)
        self.tags.delete([u'username/tag'])
        [(secondObjectID, path)] = self.tags.create(values)
        self.assertEqual(firstObjectID, secondObjectID)

    def testCreateWithExistingNamespacePath(self):
        """
        L{TagAPI.create} can be used to create L{Tag}s with the same path as
        an existing L{Namespace}.
        """
        createNamespace(self.user, u'username/name', self.user.namespace.id)
        values = [(u'username/name', u'A description')]
        self.tags.create(values)
        systemTags = self.system.tags.keys()
        tag = self.store.find(Tag, Not(Tag.path.is_in(systemTags))).one()
        self.assertEqual(u'username/name', tag.path)
        self.assertEqual(u'name', tag.name)
        self.assertIdentical(self.user.namespace, tag.namespace)

    def testCreateWithExistingTagPath(self):
        """
        L{TagAPI.create} raises a L{DuplicatePathError} exception if an
        attempt to create a L{Tag} with the same path an existing L{Tag} is
        made.
        """
        createTag(self.user, self.user.namespace, u'name')
        self.assertRaises(
            DuplicatePathError, self.tags.create,
            [(u'username/name', u'Already used tag path.')])

    def testCreatePermissionsWithImplicitNamespace(self):
        """
        L{TagAPI.create} creates permissions for the newly created
        L{Namespace}s and L{Tag}s.
        """
        values = [(u'username/namespace/tag', u'A description')]
        self.tags.create(values)

        result = self.store.find(
            NamespacePermission,
            NamespacePermission.namespaceID == Namespace.id,
            Namespace.path == u'username/namespace')
        permission = result.one()
        self.assertEqual(Policy.CLOSED, permission.createPolicy)
        self.assertEqual([self.user.id], permission.createExceptions)
        self.assertEqual(Policy.CLOSED, permission.updatePolicy)
        self.assertEqual([self.user.id], permission.updateExceptions)
        self.assertEqual(Policy.CLOSED, permission.deletePolicy)
        self.assertEqual([self.user.id], permission.deleteExceptions)
        self.assertEqual(Policy.OPEN, permission.listPolicy)
        self.assertEqual([], permission.listExceptions)
        self.assertEqual(Policy.CLOSED, permission.controlPolicy)
        self.assertEqual([self.user.id], permission.controlExceptions)

        self.assertDefaultPermissions(self.user, u'username/namespace/tag')

    def testDelete(self):
        """L{TagAPI.delete} removes L{Tag}s."""
        self.tags.create([(u'username/child', u'A description')])
        self.tags.delete([u'username/child'])
        self.assertIdentical(None, getTags(paths=[u'username/child']).one())

    def testDeleteDoesNotDeleteOtherTags(self):
        """
        L{TagAPI.delete} removes just the L{Tag} is is asked to and not any
        other tags.
        """
        self.tags.create([(u'username/child1', u'A description')])
        self.tags.create([(u'username/child2', u'A description')])
        self.tags.delete([u'username/child1'])
        tag = getTags(paths=[u'username/child2']).one()
        self.assertEqual(u'username/child2', tag.path)

    def testDeleteDoesNotDeleteOtherTagsWhenPassedAGenerator(self):
        """
        L{TagAPI.delete} removes just the L{Tag} is is asked to and not any
        other tags when it is passed a generator (as opposed to a C{list}).
        """
        self.tags.create([(u'username/child1', u'A description')])
        self.tags.create([(u'username/child2', u'A description')])
        self.tags.delete(name for name in [u'username/child1'])
        tag = getTags(paths=[u'username/child2']).one()
        self.assertEqual(u'username/child2', tag.path)

    def testDeleteRemovesDescription(self):
        """
        L{TagAPI.delete} removes the C{fluiddb/tags/description} values stored
        for deleted L{Tag}s.
        """
        values = [(u'username/tag', u'A description')]
        [(objectID, path)] = self.tags.create(values)
        self.tags.delete([u'username/tag'])
        result = TagValueAPI(self.user).get(
            objectIDs=[objectID], paths=[u'fluiddb/tags/description'])
        self.assertEqual({}, result)

    def testDeleteRemovesPath(self):
        """
        L{TagAPI.delete} removes the C{fluiddb/tags/path} values stored
        for deleted L{Tag}s.
        """
        values = [(u'username/tag', u'A description')]
        [(objectID, path)] = self.tags.create(values)
        self.tags.delete([u'username/tag'])
        result = TagValueAPI(self.user).get(
            objectIDs=[objectID], paths=[u'fluiddb/tags/path'])
        self.assertEqual({}, result)

    def testDeleteKeepsTheAboutTag(self):
        """
        L{TagAPI.delete} keeps the C{fluiddb/about} tag value for the deleted
        tag.
        """
        values = [(u'username/tag', u'A tag description')]
        [(objectID, path)] = self.tags.create(values)
        self.tags.delete([u'username/tag'])
        tag = getTags(paths=[u'fluiddb/about']).one()
        value = getTagValues([(objectID, tag.id)]).one()
        self.assertEqual(u'Object for the attribute username/tag', value.value)

    def testDeleteRemovesPermissions(self):
        """L{TagAPI.delete} removes permissions when L{Tag}s are deleted."""
        values = [(u'username/tag', u'A description')]
        self.tags.create(values)

        self.tags.delete([u'username/tag'])
        result = self.store.find(TagPermission,
                                 TagPermission.tagID == Tag.id,
                                 Tag.path == u'username/tag')
        self.assertTrue(result.is_empty())

    def testDeleteReturnsObjectIDs(self):
        """
        L{TagAPI.delete} returns a list of C{(objectID, Tag.path)} 2-tuples
        for L{Tag}s that were removed.
        """
        result1 = self.tags.create([(u'username/child', u'A description')])
        result2 = self.tags.delete([u'username/child'])
        self.assertEqual(result1, result2)

    def testDeleteUpdatesObjectsTagged(self):
        """
        L{TagAPI.delete} updates the objects previously tagged with the given
        paths.
        """
        objectID = uuid4()
        self.tags.create([(u'username/child', u'A description')])
        TagValueAPI(self.user).set({objectID: {u'username/child': 64}})
        getDirtyObjects([objectID]).remove()
        self.tags.delete([u'username/child'])
        self.assertIn(objectID, getDirtyObjects().values(DirtyObject.objectID))

    def testGetWithoutData(self):
        """
        L{TagAPI.get} raises a L{FeatureError} if no L{Tag.path}s are
        provided.
        """
        self.assertRaises(FeatureError, self.tags.get, [])

    def testGet(self):
        """L{TagAPI.get} returns L{Tag}s that match the specified paths."""
        createTag(self.user, self.user.namespace, u'ignored')
        tag = createTag(self.user, self.user.namespace, u'tag')
        self.assertEqual({u'username/tag': {'id': tag.objectID}},
                         self.tags.get([u'username/tag']))

    def testGetWithDescriptions(self):
        """
        L{TagAPI.get} can optionally include L{Tag.description}s in the
        result.
        """
        descriptionTag = self.system.tags[u'fluiddb/tags/description']
        tag = createTag(self.user, self.user.namespace, u'tag')
        createTagValue(self.user.id, descriptionTag.id, tag.objectID, u'A tag')
        result = self.tags.get([u'username/tag'], withDescriptions=True)
        self.assertEqual(tag.objectID, result[u'username/tag']['id'])
        self.assertEqual(u'A tag', result[u'username/tag']['description'])

    def testGetWithNonexistentDescriptions(self):
        """
        L{TagAPI.get} returns an empty string as description if the
        C{fluiddb/tags/description} tag is not present on the tag object.
        """
        tag = createTag(self.user, self.user.namespace, u'tag')
        result = self.tags.get([u'username/tag'], withDescriptions=True)
        self.assertEqual(tag.objectID, result[u'username/tag']['id'])
        self.assertEqual(u'', result[u'username/tag']['description'])

    def testSet(self):
        """L{TagAPI.set} updates the description for the specified L{Tag}s."""
        descriptionTag = self.system.tags[u'fluiddb/tags/description']
        tag = createTag(self.user, self.user.namespace, u'tag')
        createTagPermission(tag)
        self.tags.set({u'username/tag': u'A fancy new description.'})
        result = getTagValues([(tag.objectID, descriptionTag.id)])
        description = result.one()
        self.assertEqual(u'A fancy new description.', description.value)

    def testSetReturnsUpdateResultWithObjectIDAndPathPairs(self):
        """
        L{TagAPI.delete} returns a list of C{(objectID, Tag.path)} 2-tuples
        for L{Tag}s that were updated.
        """
        result1 = self.tags.create([(u'username/tag', u'A description')])
        result2 = self.tags.set({u'username/tag': u'A fancy new description.'})
        self.assertEqual(result1, result2)

class TagAPITest(TagAPITestMixin, FluidinfoTestCase):

    resources = [('config', ConfigResource()),
                 ('store', DatabaseResource())]

    def setUp(self):
        super(TagAPITest, self).setUp()
        self.system = createSystemData()
        UserAPI().create([(u'username', u'password', u'User',
                           u'user@example.com')])
        self.user = getUser(u'username')
        self.permissions = PermissionAPI(self.user)
        self.tags = TagAPI(self.user)


#using async
import asyncio
import random

async def produce(cookiejar, n):
    for x in range(1, n + 1):
        # produce an item
        print('producing {}/{}'.format(x, n))
        # simulate i/o operation using sleep
        print("<<producer waiting>>")
        await asyncio.sleep(random.random())
        print(">>producer waking<<")
        item = str(x)
        # put the item in the queue
        print("$producing "+item)
        await cookiejar.put(item)

    # indicate the producer is done
    await cookiejar.put(None)

async def consume(cookiejar):
    while True:
        # wait for an item from the producer
        print("<<consumer waiting>>")
        item = await cookiejar.get()
        print(">>consumer waking<<")
        if item is None:
            # the producer emits None to indicate that it is done
            break

        # process the item
        print('@consuming item {}...'.format(item))
        # simulate i/o operation using sleep
        await asyncio.sleep(random.random())

loop = asyncio.get_event_loop()
cookiejar = asyncio.Queue(loop=loop)
producer = produce(cookiejar, 10)
consumer = consume(cookiejar)
loop.run_until_complete(asyncio.gather(producer, consumer))
loop.close()


import matplotlib.pyplot as plt
import matplotlib.lines as mline
import matplotlib
import os
import numpy as np
from scipy.interpolate import griddata

#Plot preamble
matplotlib.rcdefaults()
matplotlib.rc('font',**{'family':'serif','size':16})
os.environ['PATH'] = os.environ['PATH'] + ':/opt/local/bin'
os.environ['PATH']

#File operations
filename = 'simavg0070-0134'
filein = open('/Users/Anton/Desktop/Data/hd300a0/' + filename + '.dat','rb')
fileout = '/Users/Anton/Desktop/Data/Image/' + filename + '_2.png'

input_dtype = np.dtype([
    ("ix", int),
    ("iy", int),
    ("iz", int),
    ("r", float),
    ("theta", float),
    ("phi", float),
    ("rho", float),
    ("temp", float),
    ("u_t", float),
    ("u_1", float),
    ("u_2", float),
    ("u_3", float),
    ("volume", float),
    ("bsq", float),
    ("b_1", float),
    ("b_2", float),
    ("b_3", float)
    ])

datain = np.loadtxt(filein,input_dtype)
filein.close()

def gradient_x(grid,x_value,y_value):
    return np.divide((grid[y_value,x_value]-grid[y_value,x_value-1]), (grid_x[y_value,x_value]-grid_x[y_value,x_value-1]))

def gradient_y(grid,x_value,y_value):
    return np.divide((grid[y_value,x_value]-grid[y_value-1,x_value]), (grid_y[y_value,x_value]-grid_y[y_value-1,x_value]))

# Create meshgrid (in units of gravitational radii, then converted to physical)
ymin = 0
ymax = 100
xmin = 0
xmax = 100

grid_y, grid_x = np.mgrid[ymin:ymax:150j,xmin:xmax:150j]
grid_y = 1477000*grid_y
grid_x = 1477000*grid_x

#Convert coordinates from spherical to cartesian (physical units)
points = (1477000*datain['r']*np.sin(datain['theta']),1477000*datain['r']*np.cos(datain['theta']))

####### Forces ########
## Gravitational force ##
#
# G = 6.674e-8 cm3/g/s2
# M-sun = 1.9891e33 g
F_gravity = 6.674*np.power(10.00, -8)*10.*1.9891*np.power(10.00, 33)/(np.square(1477000.*datain['r']))
F_gravity_x = -F_gravity*np.sin(datain['theta'])
F_gravity_y = -F_gravity*np.cos(datain['theta'])

## Thermal force (gas pressure) ##
# P = b*rho*T/(mu * protonmass)
# 1.38e-16
# 1.67e-24
# F_gas = -gradient(P)/rho
thermal_pressure = (np.divide(1.38,1.67)*np.multiply(datain['rho'],datain['temp'])*np.power(10.,8))

## Magnetic force (magnetic pressure) ##
magnetic_pressure = -np.divide(1, datain['rho'])*np.divide(1, 2)

## Centrifugal force ##
v_phi = (np.divide(datain['u_3'], datain['u_t'])*datain['r']*np.sin(datain['theta'])*3.e10)

effective_radius = 1477000.*datain['r']*np.sin(datain['theta'])
F_centrifugal = np.divide(np.square(v_phi), effective_radius)

##### Interpolate forces and vector fields #####
grid_F_gravity_x         = griddata(points, F_gravity_x, (grid_x, grid_y), method='linear')
grid_F_gravity_y        = griddata(points, F_gravity_y, (grid_x, grid_y), method='linear')

grid_thermal_pressure     = griddata(points, thermal_pressure, (grid_x, grid_y), method='linear')

grid_magnetic_pressure    = griddata(points, magnetic_pressure, (grid_x, grid_y), method='linear')
grid_bsq                = griddata(points, datain['bsq'], (grid_x, grid_y), method='linear')
grid_F_centrifugal     = griddata(points, F_centrifugal, (grid_x, grid_y), method='linear')
grid_rho               = griddata(points, datain['rho'], (grid_x, grid_y), method='linear')

########### ------------------------------------------------------------------------------------ ###########
########### ------------------------------------------------------------------------------------ ###########
########### ------------------------------------------------------------------------------------ ###########

# Wind = (40,70), Jet = (15,80), Disk = (80,20)
x         = np.array([0,40,40])
X_all     = 1477000*np.array([x[0],x[0],x[0],x[0],x[0], x[1],x[1],x[1],x[1],x[1], x[2],x[2],x[2],x[2],x[2]])
y         = np.array([0,15,2])
Y_all     = 1477000*np.array([y[0],y[0],y[0],y[0],y[0],y[1],y[1],y[1],y[1],y[1],y[2],y[2],y[2],y[2],y[2]])

#### Returns the logarithm of the value while preserving the sign.
def scale(value):
    tmp_array = value
    return tmp_array/1.e11

gravity_x             = scale(grid_F_gravity_x[y,x])
gravity_y             = scale(grid_F_gravity_y[y,x])

gradient_gas_x         = scale(-gradient_x(grid_thermal_pressure,x,y)/grid_rho[y,x])
gradient_gas_y         = scale(-gradient_y(grid_thermal_pressure,x,y)/grid_rho[y,x])

gradient_magnetic_x    = scale(-gradient_x(grid_bsq,x,y)/grid_rho[y,x])
gradient_magnetic_y    = scale(-gradient_y(grid_bsq,x,y)/grid_rho[y,x])

centrifugal_x         = scale(grid_F_centrifugal[y,x])
gradient_total_x       = gravity_x+gradient_gas_x+gradient_magnetic_x+centrifugal_x
gradient_total_y       = gravity_y+gradient_gas_y+gradient_magnetic_y+0

gradients_all = np.array([[gravity_x[0], gravity_y[0]],
                    [gradient_gas_x[0], gradient_gas_y[0]],
                    [gradient_magnetic_x[0], gradient_magnetic_y[0]],
                    [centrifugal_x[0], 0],
                    [gradient_total_x[0], gradient_total_y[0]],
                    [gravity_x[1], gravity_y[1]],
                    [gradient_gas_x[1], gradient_gas_y[1]],
                    [gradient_magnetic_x[1], gradient_magnetic_y[1]],
                    [centrifugal_x[1], 0],
                    [gradient_total_x[1], gradient_total_y[1]],
                    [gravity_x[2], gravity_y[2]],
                    [gradient_gas_x[2], gradient_gas_y[2]],
                    [gradient_magnetic_x[2], gradient_magnetic_y[2]],
                    [centrifugal_x[2], 0],
                    [gradient_total_x[2], gradient_total_y[2]]])

## Plot the interpolated data

plt.figure()
plt.rc('text', usetex=True)

#ymin = 1477000*ymin
#ymax = 1477000*ymax
#xmin = 1477000*xmin
#xmax = 1477000*xmax

plt.xlim(xmin=xmin, xmax=xmax)
plt.ylim(ymin=ymin, ymax=ymax)

c = plt.contour(grid_x/1477000, grid_y/1477000, grid_rho/1477000) #, extend='both', levels=np.linspace(8,16,60))
#plt.colorbar(c)

q_all = plt.quiver(X_all/1477000, Y_all/1477000, gradients_all[:,0], gradients_all[:,1],
    color=['blue', 'green', 'yellow', 'red', 'black'], linestyle=['solid','solid','solid','solid','dashed'],
    width=.004, scale=20)

p1 = mline.Line2D([], [], color='blue', label='Gravity')
p2 = mline.Line2D([], [], color='green', label='Gas pressure')
p3 = mline.Line2D([], [], color='yellow', label='Magnetic pressure')
p4 = mline.Line2D([], [], color='red', label='Centrifugal')
p5 = mline.Line2D([], [], color='black', label='Total')

plt.legend(handles=[p1,p2,p3,p4,p5], fontsize='x-small')

plt.annotate(s='Jet (' + str(x[0]) + ',' + str(y[0]) + ')', xy=(x[0],y[0]),
    size='x-small', xytext=(50, 30), textcoords='offset points')
plt.annotate(s='Wind (' + str(x[1]) + ',' + str(y[1]) + ')', xy=(x[1],y[1]),
    size='x-small', xytext=(0, -50), textcoords='offset points')
plt.annotate(s='Disk (' + str(x[2]) + ',' + str(y[2]) + ')', xy=(x[2],y[2]),
    size='x-small', xytext=(0, 30), textcoords='offset points')

plt.title('Force distribution and density')
plt.xlabel('$r/r_g$')
plt.ylabel('$r/r_g$')
plt.savefig(fileout)
plt.show()


from bs4 import BeautifulSoup
import lxml
import pandas as pd
import re
df = pd.read_csv('react-native.csv')

print(df.head())

#content = df['body'].tolist()

###
  ### read body as a single string
###
content = df['body'].str.cat(sep=',')

print(len(content))

soup = BeautifulSoup(content, "lxml")

###
  ### remove any code snippets and HTML tags
###
for tag in soup.find_all(['code', 'a', 'pre']):
    tag.decompose()

###
  ### remove common English stop words and non-alphanumeric characters
###
text = soup.get_text(strip=True).replace("."," ").replace(","," ").replace("is "," ").replace("a "," ").replace("the "," ")

  ### remove digits
text = re.sub("^\d+\s|\s\d+\s|\s\d+$", " ", text)

#print(len(text))

  ### remove duplicate spaces
text = " ".join(text.split())
print(len(text))

#######################################################################################

  ### save output as text file, be sure encode as utf-8
###
textfile = open('react-native.txt', 'w', encoding='utf-8')
textfile.write(text)
textfile.close()


import unittest

from mock import patch

from linotp.tokens.emailtoken import EmailTokenClass
from linotp.tokens.hmactoken import HmacTokenClass
from linotp.tokens.passwordtoken import PasswordTokenClass
from linotp.tokens.smstoken import SmsTokenClass
from linotp.tokens.totptoken import TimeHmacTokenClass
from linotp.tokens.yubikeytoken import YubikeyTokenClass

# ---------------------------------------------------------------------------- -

class FakeTokenModel(object):
    def __init__(self):
        self.LinOtpOtpLen = 8
        # emailtoken needs LinOtpCount variable to function correctly
        self.LinOtpCount = 0

    def setType(self, typ):
        self.type = typ

    def getSerial(self):
        return "FakeToken1"

    def getInfo(self):
        return ""

    def storeToken(self):
        pass

    def get_encrypted_seed(self):
        return b"", b""

def mock_send_email(self_obj):
    status = True
    status_message = "e-mail sent successfully"
    return status, status_message

def mock_send_email_fail(self_obj):
    status = False
    status_message = "sending e-mail failed"
    return status, status_message

Config_dict = {
    "TOTP_CHALLENGE_PROMPT": "Hello TOTP Prompt",
    "HMAC_CHALLENGE_PROMPT": "Hello HMAC Prompt",
    "YUBIKEY_CHALLENGE_PROMPT": "Hello yubikey Prompt",
    "PW_CHALLENGE_PROMPT": "Hello Password Prompt",
    "SMS_CHALLENGE_PROMPT": "Hello SMS Prompt",
    "EMAIL_CHALLENGE_PROMPT": "Hello E-Mail Prompt",
}

def mock_getFromConfig(key, fallback):
    return Config_dict.get(key, fallback)

class TestChallengePrompt(unittest.TestCase):
    """"""

    @patch("linotp.tokens.hmactoken.getFromConfig", mock_getFromConfig)
    @patch("linotp.tokens.smstoken.getFromConfig", mock_getFromConfig)
    @patch("linotp.tokens.base.getFromConfig", mock_getFromConfig)
    @patch("linotp.tokens.smstoken.SmsTokenClass.loadLinOtpSMSValidTime")
    @patch("linotp.tokens.smstoken.SmsTokenClass.setValidUntil")
    @patch("linotp.tokens.smstoken.SmsTokenClass.submitChallenge")
    def tests_sms_token_challenge_prompt(
        self,
        mock_submitChallenge,
        mock_setValidUntil,
        mock_loadLinOtpSMSValidTime,
    ):
        mock_submitChallenge.return_value = True, "sms submitted"
        mock_setValidUntil.return_value = None
        mock_loadLinOtpSMSValidTime.return_value = 30

        prompt = Config_dict.get("SMS_CHALLENGE_PROMPT")

        sms_token = SmsTokenClass(FakeTokenModel())

        (_, message, _data, _) = sms_token.createChallenge(
            "131231231313123", options=None
        )

        assert message == prompt

        return

    @patch("linotp.tokens.base.getFromConfig", mock_getFromConfig)
    @patch("linotp.tokens.hmactoken.getFromConfig", mock_getFromConfig)
    def tests_password_token_challenge_prompt(self):
        prompt = Config_dict.get("PW_CHALLENGE_PROMPT")

        pass_token = PasswordTokenClass(FakeTokenModel())

        (_, message, _data, _) = pass_token.createChallenge(
            "131231231313123", options=None
        )

        assert message == prompt

        return

    @patch("linotp.tokens.base.getFromConfig", mock_getFromConfig)
    def tests_yubikey_token_challenge_prompt(self):
        prompt = Config_dict.get("YUBIKEY_CHALLENGE_PROMPT")

        yubi_token = YubikeyTokenClass(FakeTokenModel())

        (_, message, _data, _) = yubi_token.createChallenge(
            transactionid="131231231313123", options=None
        )

        assert message == prompt

        return

    @patch("linotp.tokens.base.getFromConfig", mock_getFromConfig)
    @patch("linotp.tokens.hmactoken.getFromConfig", mock_getFromConfig)
    def tests_hmac_token_challenge_prompt(self):
        prompt = Config_dict.get("HMAC_CHALLENGE_PROMPT")

        hmac_token = HmacTokenClass(FakeTokenModel())

        (_, message, _data, _) = hmac_token.createChallenge(
            state="131231231313123", options=None
        )

        assert message == prompt

        return

    @patch("linotp.tokens.base.getFromConfig", mock_getFromConfig)
    @patch("linotp.tokens.totptoken.getFromConfig", mock_getFromConfig)
    @patch("linotp.tokens.hmactoken.getFromConfig", mock_getFromConfig)
    def tests_totp_token_challenge_prompt(self):
        prompt = Config_dict.get("TOTP_CHALLENGE_PROMPT")

        totp_token = TimeHmacTokenClass(FakeTokenModel())

        (_, message, _data, _) = totp_token.createChallenge(
            state="131231231313123", options=None
        )

        assert message == prompt

        return

    @patch(
        "linotp.tokens.emailtoken.EmailTokenClass._sendEmail", mock_send_email
    )
    @patch("linotp.tokens.hmactoken.getFromConfig", mock_getFromConfig)
    @patch("linotp.tokens.base.getFromConfig", mock_getFromConfig)
    @patch("linotp.tokens.emailtoken.getFromConfig", mock_getFromConfig)
    def tests_email_token_challenge_prompt(self):
        prompt = Config_dict.get("EMAIL_CHALLENGE_PROMPT")

        email_token = EmailTokenClass(FakeTokenModel())

        (_, message, _data, _) = email_token.createChallenge(
            transactionid="131231231313123", options=None
        )

        assert message == prompt

        return

    @patch(
        "linotp.tokens.emailtoken.EmailTokenClass._sendEmail",
        mock_send_email_fail,
    )
    @patch("linotp.tokens.hmactoken.getFromConfig", mock_getFromConfig)
    @patch("linotp.tokens.base.getFromConfig", mock_getFromConfig)
    @patch("linotp.tokens.emailtoken.getFromConfig", mock_getFromConfig)
    def tests_failed_email_token_challenge_prompt(self):
        """To test that the prompt will be the output of
        the email provider rather than the custom message from the config
        """

        # this is the prompt that mock_send_email will produce
        prompt = "sending e-mail failed"

        email_token = EmailTokenClass(FakeTokenModel())

        (_, message, _data, _) = email_token.createChallenge(
            transactionid="131231231313123", options=None
        )

        assert message == prompt

        return


import os
import torch
import numpy as np
from torch.nn import functional as F
from .model import CriticNet, Actor_1_Net, Actor_2_Net, Actor_3_Net
from .Image_model import CNN, Ray, Feature
from .replay_memory import Transition, Memory

class MultiDDPG_Agent:
    def __init__(self, **config):
        self.config = config
        self.n_states = self.config["n_states"]
        self.n_actions = self.config["n_actions"]
        self.batch_size = self.config["batch_size"]
        self.actor_lr = self.config["actor_lr"]
        self.critic_lr = self.config["critic_lr"]
        self.gamma = self.config["gamma"]
        self.sigma = self.config["sigma"]
        self.tau = self.config["tau"]
        self.memory = Memory(self.config["mem_size"], self.config["seed"])
        self.device = "cuda" if torch.cuda.is_available() else "cpu"

        self.critic = CriticNet(n_states=self.n_states, n_actions=self.n_actions,
                                n_hidden_filters=self.config['n_hidden_filters']).to(self.device)

        self.actor_1 = Actor_1_Net(n_actions=self.n_actions, action_bound=self.config["action_bound"]).to(self.device)
        self.actor_2 = Actor_2_Net(n_actions=self.n_actions, action_bound=self.config["action_bound"]).to(self.device)
        self.actor_3 = Actor_3_Net(n_actions=self.n_actions, action_bound=self.config["action_bound"]).to(self.device)

        self.actors_list = [self.actor_1, self.actor_2, self.actor_3]

        self.target_critic = CriticNet(n_states=self.n_states, n_actions=self.n_actions,
                                       n_hidden_filters=self.config['n_hidden_filters']).to(self.device)
        self.target_actor_1 = Actor_1_Net(n_actions=self.n_actions,
                                          action_bound=self.config["action_bound"]).to(self.device)
        self.target_actor_2 = Actor_2_Net(n_actions=self.n_actions,
                                          action_bound=self.config["action_bound"]).to(self.device)
        self.target_actor_3 = Actor_3_Net(n_actions=self.n_actions,
                                          action_bound=self.config["action_bound"]).to(self.device)

        self.target_critic.load_state_dict(self.critic.state_dict())
        self.target_actor_1.load_state_dict(self.actor_1.state_dict())
        self.target_actor_2.load_state_dict(self.actor_2.state_dict())
        self.target_actor_3.load_state_dict(self.actor_3.state_dict())

        self.critic_optimizer = torch.optim.Adam(self.critic.parameters(), lr=self.critic_lr)
        self.actor_1_optimizer = torch.optim.Adam(self.actor_1.parameters(), lr=self.actor_lr)
        self.actor_2_optimizer = torch.optim.Adam(self.actor_2.parameters(), lr=self.actor_lr)
        self.actor_3_optimizer = torch.optim.Adam(self.actor_3.parameters(), lr=self.actor_lr)

        if self.config["unity_camera"]:
            self.cnn = CNN().to(self.device)
            self.ray = Ray().to(self.device)
            self.feature = Feature().to(self.device)

    def take_action(self, state, actor_index):
        state = torch.tensor([state], dtype=torch.float).to(self.device)
        selected_actor = self.actors_list[actor_index]
        action = selected_actor(state)[0]
        action = action.detach().cpu().numpy()
        action = action + self.sigma * np.random.randn(self.n_actions)
        return action

    def soft_update(self, net, target_net):
        for param_target, parma in zip(target_net.parameters(), net.parameters()):
            param_target.data.copy_(param_target.data * (1 - self.tau) + parma.data * self.tau)

    def update(self):
        if len(self.memory) >= self.batch_size:
            batch = self.memory.sample(self.batch_size)
            states, actions, rewards, next_states, dones = self.unpack(batch)

            next_q1_values = self.target_critic(next_states, self.target_actor_1(next_states))
            next_q2_values = self.target_critic(next_states, self.target_actor_2(next_states))
            next_q3_values = self.target_critic(next_states, self.target_actor_3(next_states))

            q_targets_1 = rewards + self.gamma * next_q1_values * (1 - dones.int())
            q_targets_2 = rewards + self.gamma * next_q2_values * (1 - dones.int())
            q_targets_3 = rewards + self.gamma * next_q3_values * (1 - dones.int())

            critic_loss = torch.mean(F.mse_loss(self.critic(states, actions), q_targets_1)) + \
                          torch.mean(F.mse_loss(self.critic(states, actions), q_targets_2)) + \
                          torch.mean(F.mse_loss(self.critic(states, actions), q_targets_3))

            self.critic_optimizer.zero_grad()
            critic_loss.backward()
            self.critic_optimizer.step()

            actor_loss_1 = -torch.mean(self.critic(states, self.actor_1(states)))
            self.actor_1_optimizer.zero_grad()
            actor_loss_1.backward()
            self.actor_1_optimizer.step()
            actor_loss_2 = -torch.mean(self.critic(states, self.actor_2(states)))
            self.actor_2_optimizer.zero_grad()
            actor_loss_2.backward()
            self.actor_2_optimizer.step()
            actor_loss_3 = -torch.mean(self.critic(states, self.actor_3(states)))
            self.actor_3_optimizer.zero_grad()
            actor_loss_3.backward()
            self.actor_3_optimizer.step()

            self.soft_update(self.actor_1, self.target_actor_1)
            self.soft_update(self.actor_2, self.target_actor_2)
            self.soft_update(self.actor_3, self.target_actor_3)
            self.soft_update(self.critic, self.target_critic)

            return actor_loss_1, actor_loss_2, actor_loss_3
        else:
            return None, None, None

    def unpack(self, batch):
        batch = Transition(*zip(*batch))

        states = torch.cat(batch.state).view(self.batch_size, self.n_states).to(self.device)
        actions = torch.cat(batch.action).view(-1, self.config["n_actions"]).to(self.device)
        rewards = torch.cat(batch.reward).view(self.batch_size, 1).to(self.device)
        next_states = torch.cat(batch.next_state).view(self.batch_size, self.n_states).to(self.device)
        dones = torch.cat(batch.done).view(self.batch_size, 1).to(self.device)

        return states, actions, rewards, next_states, dones

    def store(self, state, action, reward, next_state, done):
        state = torch.tensor([state], dtype=torch.float).to(self.device)
        action = torch.tensor([action], dtype=torch.float).to(self.device)
        reward = torch.tensor([reward], dtype=torch.float).to(self.device)
        next_state = torch.tensor([next_state], dtype=torch.float).to(self.device)
        done = torch.BoolTensor([done]).to(self.device)

        self.memory.add(state, action, reward, next_state, done)

    def check_state(self, state_0, state_1):
        image = torch.unsqueeze(torch.tensor(state_0, dtype=torch.float), 0).to(self.device)
        ray = torch.unsqueeze(torch.tensor(state_1, dtype=torch.float), 0).to(self.device)

        image = self.cnn(image)
        ray = self.ray(ray)
        feature = self.feature(image, ray).detach().cpu().numpy().squeeze()

        return feature

    def save_checkpoints(self, env_name, suffix="", ckpt_path=None):
        if not os.path.exists('checkpoints/UGV/01/3actors'):
            os.makedirs('checkpoints/UGV/01/3actors')
        if ckpt_path is None:
            ckpt_path = "checkpoints/UGV/01/3actors/Multi-DDPG_checkpoint_{}_{}".format(env_name, suffix)
            print(ckpt_path)
        print('Saving models to {}'.format(ckpt_path))
        if self.config["unity_camera"]:
            torch.save({'critic': self.critic.state_dict(),
                        'actor_1': self.actor_1.state_dict(),
                        'actor_2': self.actor_2.state_dict(),
                        'actor_3': self.actor_3.state_dict(),
                        'target_critic': self.target_critic.state_dict(),
                        'target_actor_1': self.target_actor_1.state_dict(),
                        'target_actor_2': self.target_actor_2.state_dict(),
                        'target_actor_3': self.target_actor_3.state_dict(),
                        'CNN': self.cnn.state_dict(),
                        'Ray': self.ray.state_dict(),
                        'Feature': self.feature.state_dict()}, ckpt_path)
        else:
            torch.save({'critic': self.critic.state_dict(),
                        'actor_1': self.actor_1.state_dict(),
                        'actor_2': self.actor_2.state_dict(),
                        'actor_3': self.actor_3.state_dict(),
                        'target_critic': self.target_critic.state_dict(),
                        'target_actor_1': self.target_actor_1.state_dict(),
                        'target_actor_2': self.target_actor_2.state_dict(),
                        'target_actor_3': self.target_actor_3.state_dict()}, ckpt_path)


"""add age column into user table

Revision ID: eaf754b4cce2
Revises: 4c1ed9483d5e
Create Date: 2020-08-30 18:43:16.390626

"""
from alembic import op
import sqlalchemy as sa

# revision identifiers, used by Alembic.
revision = 'eaf754b4cce2'
down_revision = '4c1ed9483d5e'
branch_labels = None
depends_on = None

def upgrade():
    connection = op.get_bind()
    connection.execute('ALTER TABLE user ADD COLUMN age INT')

def downgrade():
    connection = op.get_bind()
    connection.execute('ALTER TABLE user DROP COLUMN age')


# -*- coding: utf-8 -*-
# @Author: Nandadeep Davuluru
# @Date:   2019-03-10 11:38:34
# @Last Modified by:   nandadeep
# @Last Modified time: 2019-03-12 20:42:22
import os, time
from kmp import KMP
from naive import NaiveStringMatch
from bm import BoyerMoore
from random import randint
from random import shuffle
from matplotlib import pyplot as plt
from timeit import default_timer as timer
import itertools

PATTERN_SIZE = 4 # hyper-parameter?
DIRECTORY = '/Users/nandadeepd/Desktop/string-matching/'

class Plotter(object):

    def plot_text_time(self, experiment, s_times, lengths, color):
        print("-----------------")
        print(s_times, lengths)
        print("-----------------")
        plt.scatter(s_times, lengths, c=["r", "g", "c", "b", "k"])
        plt.xlabel("Time"); plt.ylabel("Text Length")
        plt.savefig(DIRECTORY + "/metrics/" + str(experiment) + ".png")

    def sample_plot(self):
        times = [1, 5, 3, 2, 6, 7, 8, 9, 10]
        lengt = [50, 100, 40, 60 , 80 , 90, 300, 60, 500]
        self.plot_text_time(experiment=1, times=times, lengths=lengt)

def pattern_generator(text, total_len):
	len_edge = total_len - PATTERN_SIZE # avoiding out of bounds!
	start_num = randint(0, len_edge)
	end_num = start_num + PATTERN_SIZE # size of the pattern.
	return start_num, end_num

def main():

	files = [f for f in os.listdir(DIRECTORY + "/datasets/")] # linear time with respect to the files.
	file_sizes, times, p_times = list(), list(), list() # for plots
	# files = shuffle(files)
	shuffle(files)
	for file in files: # linear pass over all files

		# if "url" in file:
		# 	# experiment = "all"
		# 	print("reading file: {}".format(file))
		# 	content = ""
		# 	with open(DIRECTORY + "/datasets/" + file, 'rb') as f: # avoiding unicode parse errors
		# 		content = f.read()
		# 	total_len = len(content)
		# 	print("file length: {}".format(total_len))
		# 	file_sizes.append(total_len)
		# 	pattern = 'jpg'
		# 	print("patter to search for:", pattern)
		# 	start_time = timer()
		# 	naive_matcher = NaiveStringMatch(content, pattern)
		# 	matches = naive_matcher.search()
		# 	print(len(matches))
		# 	end_time = timer()
		# 	times.append((end_time - start_time) )

		if "dna" in file:
			# experiment = "dna_"
			print("reading file: {}".format(file))

			content = ""
			with open(DIRECTORY + "/datasets/" + file, 'rb') as f: # avoiding unicode parse errors
				content = f.read()
			total_len = len(content)
			file_sizes.append(total_len)
			print("file length: {}".format(total_len))
			indices = pattern_generator(content, total_len)
			pattern = content[indices[0]:indices[1]]
			print("patter to search for: ", pattern)
			##################### NAIVE TIMER BEGINS ######################
			start_time = time.time()
			naive_matcher = NaiveStringMatch(content, pattern)
			matches = naive_matcher.search()
			print(len(matches))
			end_time = time.time()
			##################### NAIVE TIMER ENDS #######################
			###################### KMP TIMER BEGINS ######################
			# start_time = time.time()
			# kmp_matcher = KMP(pattern, content)
			# matches = kmp_matcher.KMPSearch()
			# end_time = time.time()
			###################### KMP TIMER ENDS #######################
			times.append((end_time - start_time) )
			print((end_time - start_time) * 10)
		print("--------------------------------------------------------------")
	p = Plotter()
	colors = itertools.cycle(5 * ["r", "g", "c", "b", "k"])
	p.plot_text_time(experiment="experiment", s_times=times, lengths=file_sizes, color = next(colors))
		# break

if __name__ == '__main__':
	main()


import typing

import boto3

iot_client = boto3.client('iot')

def describe_thing(thing_name: str) -> dict:
    thing = iot_client.describe_thing(thingName=thing_name)
    thing.pop('ResponseMetadata', None)
    return thing

def list_things_in_thing_group(thing_group_name: str, callback: typing.Callable[[str], None]) -> None:
    response = None
    while not response or 'nextToken' in response:

        kwargs = {}
        if response and 'nextToken' in response:
            kwargs['nextToken'] = response['nextToken']

        response = iot_client.list_things_in_thing_group(
            thingGroupName=thing_group_name,
            recursive=True,
            maxResults=100,
            **kwargs
        )

        for thing_name in response['things']:
            callback(thing_name)

def search_index(query: str, index_name: str = 'AWS_Things') -> typing.List[str]:
    things = []

    response_list = None

    while not response_list or 'nextToken' in response_list:

        kwargs = {}
        if response_list and 'nextToken' in response_list:
            kwargs['nextToken'] = response_list['nextToken']

        response_list = iot_client.search_index(
            indexName=index_name,
            queryString=query,
            **kwargs)

        things.extend(response_list['things'])

    return things


# coding: utf-8
"""
The functions below use OpenCV functions to detect edges in a given frame.

Sources:
- cv2.equalizeHist: improve the contrast of the frame
    https://docs.opencv.org/3.2.0/d5/daf/tutorial_py_histogram_equalization.html

- cv2.Canny: edge detection.
    https://docs.opencv.org/3.3.1/da/d22/tutorial_py_canny.html

- cv2.GaussianBlur: removing gaussian noise from the frame.
    https://docs.opencv.org/3.1.0/d4/d13/tutorial_py_filtering.html

- cv2.getPerspectiveTransform & cv.warpPerspective: convert frame to a wrapped/flattened bird's eye view
    https://docs.opencv.org/3.4.0/da/d6e/tutorial_py_geometric_transformations.html

"""

# libraries and dependencies
# ---------------------------------------------------------------------------- #
import cv2
import numpy as np
import glob
# ---------------------------------------------------------------------------- #

def get_yellow_lanes(frame):
    """
    Apply HSV mask to a frame to highlight yellow lanes in the frame.

    OpenCV Docs:
        https://docs.opencv.org/3.0.0/df/d9d/tutorial_py_colorspaces.html
    """
    min_value = np.array([0, 70, 70]) # light red
    max_value = np.array([50, 255, 255]) # light blue

    # convert BGR to HSV
    hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)

    # define range of yellow color in hsv
    lower_threshold = np.all(hsv > min_value, axis=2)
    upper_threshold = np.all(hsv < max_value, axis=2)

    # define a threshold to get only yellow colors.
    mask = np.logical_and(lower_threshold, upper_threshold)

    return mask

def get_white_lanes(frame):
    """
    Apply histogram equalization to a frame to improve the contrast of the frame
    then threshold it, which will help to highlight the white lines in the frame

    OpenCv Docs:
        https://docs.opencv.org/3.2.0/d5/daf/tutorial_py_histogram_equalization.html
    """
    # convert BGR to GrayScale
    grayscale = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)

    equalization_histogram = cv2.equalizeHist(grayscale)

    _, mask = cv2.threshold(equalization_histogram, thresh=250, maxval=255, type=cv2.THRESH_BINARY)

    return mask

def canny_edge_detection(frame, sigma=0.33, kernel_size=3):
    """
    Apply Canny edge detection to a frame, then threshold the result
    to highlight only the edges points in the frame

    OpenCV Docs:
        Canny - https://docs.opencv.org/3.3.1/da/d22/tutorial_py_canny.html
        GaussianBlur - https://docs.opencv.org/3.1.0/d4/d13/tutorial_py_filtering.html
    """
    # convert BGR to GrayScale
    grayscale = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)

    # Gaussian blurring is highly effective in removing gaussian noise from the frame.
    # kernel_size: should be positive and odd
    blurred = cv2.GaussianBlur(grayscale, (kernel_size, kernel_size), 0)

    # compute the median of the pixles intensity
    median = np.median(blurred)

    # calculate thresholds using the computed median
    lower_threshold = int(max(0, (1.0 - sigma) * median))
    upper_threshold = int(min(255, (1.0 + sigma) * median))

    #apply Canny edge detection
    return cv2.Canny(blurred, lower_threshold, upper_threshold)

def convert_to_bitmap(frame, kernel_size=3):
    """
    Convert a input frame to a bitmap (2D-array of zeros and ones)
    where only the pixels corresponding to lanes in the frame
    would be highlighted by assigning them with (ones)
    and the rest of the pixels would be ignored by assigning them to (zeros).
    """
    # create an empty bitmap and initialize all its values to zeros
    lanes_bitmap = np.zeros(shape=frame.shape[:2], dtype=np.uint8)

    # highlight yellow lanes by threshold in HSV color space
    lanes_bitmap = np.logical_or(lanes_bitmap, get_yellow_lanes(frame))

    # highlight white lanes by thresholding the equalized frame
    lanes_bitmap = np.logical_or(lanes_bitmap, get_white_lanes(frame))

    # apply canny_edge_detection algorithm to enhance lanes detection
    lanes_bitmap = np.logical_or(lanes_bitmap, canny_edge_detection(frame, kernel_size))

    # apply a morphological transformation to paint/fill the small gaps in the detected lines
    # basically to get a (solid line) instead of an (intermittent or dashed line)
    # for more information please check out this page:
    # https://docs.opencv.org/3.0-beta/doc/py_tutorials/py_imgproc/py_morphological_ops/py_morphological_ops.html
    bitmap = cv2.morphologyEx(
        lanes_bitmap.astype(np.uint8),
        cv2.MORPH_CLOSE,
        np.ones((kernel_size, kernel_size), np.uint8))

    return bitmap

def convert_to_birdseye_view(frame):
    """
    Convert frame to a wrapped/flattened bird's eye view.
    """
    height, width = frame.shape[:2]

    # define 4 points in the original space
    src = np.float32([[width, height],
                      [0, height],
                      [width/3, height * (2/3)],
                      [width/2, height * (2/3)]])

    # define 4 points in the warped space
    dst = np.float32([[width, height],
                      [0, height],
                      [0, 0],
                      [width, 0]])

    # compute transformation matrices
    forward_transformation_matrix = cv2.getPerspectiveTransform(src, dst)
    backward_transformation_matrix = cv2.getPerspectiveTransform(dst, src)

    # warp frame to get bird's eye view
    birdseye_view = cv2.warpPerspective(frame,
        forward_transformation_matrix,
        (width, height),
        flags=cv2.INTER_LINEAR)

    return birdseye_view, forward_transformation_matrix, backward_transformation_matrix


import json

import pytest
from model_bakery import baker
from rest_framework import status

from usaspending_api.accounts.models import TreasuryAppropriationAccount
from usaspending_api.common.tests.autocomplete import check_autocomplete

@pytest.fixture
def tas_data(db):
    baker.make(TreasuryAppropriationAccount, tas_rendering_label="zzz", main_account_code="abc")
    baker.make(TreasuryAppropriationAccount, tas_rendering_label="###", main_account_code="789")
    baker.make(TreasuryAppropriationAccount, main_account_code="XYZ")

@pytest.mark.parametrize(
    "fields,value,expected",
    [
        (
            ["main_account_code", "tas_rendering_label"],
            "z",
            {"main_account_code": ["XYZ"], "tas_rendering_label": ["zzz"]},
        ),
        (["main_account_code"], "ab", {"main_account_code": ["abc"]}),
        (["main_account_code"], "12", {"main_account_code": ["123"]}),
        (["main_account_code"], "789", {"main_account_code": ["789"]}),
        (["tas_rendering_label"], "###", {"tas_rendering_label": "###"}),
        (["tas_rendering_label"], "ghi", {"tas_rendering_label": []}),
    ],
)
@pytest.mark.skip(reason="Deprecated endpoints; to remove later")
@pytest.mark.django_db
def test_awards_autocomplete(client, tas_data, fields, value, expected):
    """test partial-text search on awards."""

    check_autocomplete("tas", client, fields, value, expected)

@pytest.mark.skip(reason="Deprecated endpoints; to remove later")
@pytest.mark.django_db
def test_bad_awards_autocomplete_request(client):
    """Verify error on bad autocomplete request for awards."""

    resp = client.post("/api/v1/tas/autocomplete/", content_type="application/json", data=json.dumps({}))
    assert resp.status_code == status.HTTP_400_BAD_REQUEST


import numpy as np
from flask import Flask, request, jsonify, render_template
import joblib  # Use joblib for model loading

app = Flask(__name__)
model = joblib.load('model.pkl')  # Load the trained model

@app.route('/')
def home():
    return render_template('index.html')

@app.route('/predict', methods=['POST'])
def predict():
    try:
        # Get input features from the HTML form
        pregnancies = int(request.form['pregnancies'])
        glucose = int(request.form['glucose'])
        blood_pressure = int(request.form['blood_pressure'])
        skin_thickness = int(request.form['skin_thickness'])
        insulin = int(request.form['insulin'])
        bmi = float(request.form['bmi'])
        diabetes_pedigree_function = float(request.form['diabetes_pedigree_function'])
        age = int(request.form['age'])

        # Prepare input data for prediction
        input_features = [pregnancies, glucose, blood_pressure, skin_thickness, insulin, bmi, diabetes_pedigree_function, age]
        final_features = [np.array(input_features)]

        # Make a prediction using the loaded model
        prediction = model.predict(final_features)

        # Interpret the prediction (0 or 1) as "Diabetes" or "No Diabetes"
        if prediction[0] == 0:
            output = 'No Diabetes'
        else:
            output = 'Diabetes'

        return render_template('index.html', prediction_text=f'Prediction: {output}')
    except Exception as e:
        return render_template('index.html', prediction_text='An error occurred during prediction.')

@app.route('/predict_api', methods=['POST'])
def predict_api():
    try:
        # Get input data as JSON
        data = request.get_json(force=True)

        # Convert JSON data to a list of feature values
        input_features = [int(data['pregnancies']), int(data['glucose']), int(data['blood_pressure']),
                          int(data['skin_thickness']), int(data['insulin']), float(data['bmi']),
                          float(data['diabetes_pedigree_function']), int(data['age'])]

        # Prepare input data for prediction
        final_features = [np.array(input_features)]

        # Make a prediction using the loaded model
        prediction = model.predict(final_features)

        # Interpret the prediction (0 or 1) as "Diabetes" or "No Diabetes"
        if prediction[0] == 0:
            output = 'No Diabetes'
        else:
            output = 'Diabetes'

        return jsonify({'prediction': output})
    except Exception as e:
        return jsonify({'error': 'An error occurred during prediction.'})

if __name__ == "__main__":
    app.run(debug=True)


import torch
import seaborn as sns
import pandas as pd
from  sklearn.metrics import confusion_matrix
import torchaudio
from dataset import audioDataset
from train import SAMPLE_RATE, NUM_SAMPLES
from sklearn.metrics import (ConfusionMatrixDisplay, classification_report,
                             confusion_matrix)
import matplotlib.pyplot as plt
import torchvision

NUM_SAMPLES = 32000
SAMPLE_RATE = 16000

DATASET = "gender"
CNN = "resnet50"

if DATASET == "gender":
    AUDIO_DIR = "H:\\voxc2\\voxceleb2\\test\\aac" #replace with own path
if DATASET == "emotion":
    AUDIO_DIR = "EMOTION_TEST" # replace with own path

if (DATASET == "gender"):
        class_mapping = [
        "female",
        "male"
    ]
elif (DATASET == "emotion"):
    class_mapping = [
        "anger",
        "disgust",
        "fear",
        "happiness",
        "neutral",
        "sadness",
    ]
def predict(model, input, target, class_mapping):
    model.eval()
    with torch.no_grad():
        predictions = model(input)
        predicted_index = predictions[0].argmax(0)
        predicted = class_mapping[predicted_index]
        expected = class_mapping[target]
    return predicted, expected

if __name__ == "__main__":

    cuda_ = "cuda:0"
    print(cuda_)

    if torch.cuda.is_available():
        print("cuda available")
        device = "cpu"
    else:
        device = "cpu"
    print(f"Using {device}")

    if DATASET == "gender":
        if CNN == "densenet121":
            cnn = torchvision.models.densenet121(num_classes = 2)
        elif CNN == "alexnet":
            cnn = torchvision.models.alexnet(num_classes = 2)
        elif CNN == "mobilenetv3":
            cnn = torchvision.models.mobilenet_v3_small(num_classes = 2)
        elif CNN == "resnet50":
            cnn = torchvision.models.resnet50(num_classes = 2)
    elif DATASET == "emotion":
        if CNN == "densenet121":
            cnn = torchvision.models.densenet121(num_classes = 6)
        elif CNN == "alexnet":
            cnn = torchvision.models.alexnet(num_classes = 6)
        elif CNN == "mobilenetv3":
            cnn = torchvision.models.mobilenet_v3_small(num_classes = 6)
        elif CNN == "resnet50":
            cnn = torchvision.models.resnet50(num_classes = 6)

    state_dict = torch.load(f"models/{CNN}_{DATASET}_final.pth")

    cnn.load_state_dict(state_dict)
    cnn = cnn.to(device)

    # creating mel spectrogram
    mel_spectrogram = torchaudio.transforms.MelSpectrogram(
        sample_rate=SAMPLE_RATE,
        n_fft=1024,
        hop_length=512,
        n_mels=128,
    )

    # creating the custom dataset
    emotionData = audioDataset(
                            AUDIO_DIR,
                            None,
                            mel_spectrogram,
                            NUM_SAMPLES,
                            SAMPLE_RATE,
                            device,
                            DATASET)

    count = 0
    correct = 0
    skipped = 0
    y_true = []
    y_pred = []

    while count < len(emotionData):
        input = emotionData[count][0]
        target = emotionData[count][1]
        if target < len(class_mapping):
            input.unsqueeze_(0)
            predicted, expected = predict(cnn, input, target, class_mapping)
            if (predicted == expected):
                correct = correct + 1
            y_pred.append(class_mapping.index(predicted))
            y_true.append(class_mapping.index(expected))
        else:
            skipped = skipped +1
        count = count + 1

    # confusion matrix
    print("acc = ", correct / len(emotionData))
    print(skipped)
    print(classification_report(y_true=y_true, y_pred=y_pred, target_names=class_mapping, digits=4))
    cm = confusion_matrix(y_true=y_true, y_pred=y_pred)
    print(cm)
    print("result predicted ", predicted, " expected ", expected)

    # plots
    ax = plt.subplot()
    sns.set(font_scale=1.5)
    hm = sns.heatmap(cm, annot=True, ax=ax, fmt="g", cmap='Blues')
    ax.set_xlabel('Predicted class', size=17);
    ax.set_ylabel('Actual class', size=17);
    ax.set_title('');
    if DATASET == "emotion":
        ax.xaxis.set_ticklabels(['Anger', 'Disgust', "Fear", "Happiness", "neutral", "sadness"], size=12);
        ax.yaxis.set_ticklabels(['Anger', 'Disgust', "Fear", "Happiness", "neutral", "sadness"], size=12);
    if DATASET == "gender":
        ax.xaxis.set_ticklabels(['Female', 'Male'], size=12);
        ax.yaxis.set_ticklabels(['Female', 'Male'], size=12);
    plt.show()


from django.shortcuts import render
from apps.produtos.models import (
    Produtos,
    MovimentacaoProdutos,
    MediaProduto
)
from django.http import JsonResponse
from apps.datatable.datatable import DataTableFilter, DataTableRaw
from django.contrib.auth.decorators import login_required

@login_required(login_url='/login/')
def index(request):
    return render(request, 'produtos/index.html')

@login_required(login_url='/login/')
def modal_add_produto(request):
    return render(request, 'produtos/modalAddProduto.html')

@login_required(login_url='/login/')
def modal_compra_produtos(request):
    return render(request, 'produtos/modalCompraProdutos.html')

@login_required(login_url='/login/')
def modal_vender_produtos(request):
    return render(request, 'produtos/modalVenderProdutos.html')

@login_required(login_url='/login/')
def modal_detalhes_produto(request):
    return render(request, 'produtos/modalDetalhesProduto.html')

@login_required(login_url='/login/')
def modal_produtos_comprados(request):
    lista = lista_comprados()

    return render(request, 'produtos/modalProdutosComprados.html',{
        'lista': lista
    })

@login_required(login_url='/login/')
def modal_produtos_vendidos(request):
    lista = lista_vendidos()

    return render(request, 'produtos/modalProdutosVendidos.html',{
        'lista': lista
    })

@login_required(login_url='/login/')
def salvar_add_produto(request):
    descricao = request.POST.get("descricao")

    if not descricao:
        data = {
            "title": "Erro!",
            "status": "error",
            "msg": "Descrição é um campo obrigatório"
        }

        return JsonResponse(data, safe=False)

    exists = Produtos.objects.filter(
        descricao=descricao
    ).exists()

    if not exists:
        fs = Produtos()
        fs.descricao = descricao
        fs.ativo = 'S'
        fs.save()

        data = {
            "title": "Sucesso!",
            "status": "success",
            "msg": "Produto adicionado com sucesso!"
        }
    else:
        data = {
            "title": "Erro",
            "status": "error",
            "msg": "Produto já está cadastrado!"
        }

    return JsonResponse(data, safe=False)

@login_required(login_url='/login/')
def select_desc_produto(request):
    descricao = request.POST.get('descricao', '')

    items = Produtos.objects.filter(
        descricao__contains=descricao
    )
    data = list()

    for item in items:
        data.append({
            'id': item.id_produto,
            'text': item.descricao
        })

    return JsonResponse(data, safe=False)

@login_required(login_url='/login/')
def salvar_movimentacao_estoque(request):
    id_produto = request.POST.get('id_produto')
    quantidade = request.POST.get('quantidade').replace(",", ".")
    valor = request.POST.get('valor').replace(",", ".")
    tipo = request.POST.get('tipo')

    if not id_produto or not quantidade or not valor or not tipo:
        data = {
            "title": "Erro!",
            "status": "error",
            "msg": "Todos os campos são obrigatórios"
        }

        return JsonResponse(data, safe=False)

    if float(quantidade) <= 0 or float(valor) <= 0:
        data = {
            "title": "Erro!",
            "status": "error",
            "msg": "Quantidade e Valor não podem ser 0"
        }

        return JsonResponse(data, safe=False)

    items = MovimentacaoProdutos.objects.filter(
        id_produto=id_produto
    )

    estoque = calcular_estoque(id_produto, tipo, quantidade)

    print(estoque)

    if estoque >= 0:
        fs = MovimentacaoProdutos()
        fs.id_produto_id = id_produto
        fs.quantidade = quantidade
        fs.valor = valor
        fs.media_compra = 0
        fs.tipo = tipo
        fs.save()

        fs = MediaProduto()
        fs.id_produto_id = id_produto
        fs.tipo = tipo
        fs.valor = calcular_media(id_produto, tipo)
        fs.save()

        data = {
            "title": "Sucesso",
            "status": "success",
            "msg": "Produto movimentado com sucesso!"
        }

    else:

        data = {
            "title": "Erro",
            "status": "error",
            "msg": "O estoque do produto não pode ser menor que 0"
        }

    return JsonResponse(data, safe=False)

def calcular_estoque(id_produto, tipo, quantidade):
    items = MovimentacaoProdutos.objects.filter(
        id_produto=id_produto
    )

    total = 0
    for item in items:
        if item.tipo == 'E':
            total = float(total) + float(item.quantidade)
        elif item.tipo == 'S':
            total = float(total) - float(item.quantidade)

    if tipo == 'E':
        return total + float(quantidade)
    elif tipo == 'S':
        return total - float(quantidade)

def calcular_media(id_produto, tipo):
    items = MovimentacaoProdutos.objects.filter(
        id_produto=id_produto,
        tipo=tipo
    )

    media = 0
    total = 0
    total_valor = 0
    total_quantidade = 0
    for item in items:
        total_quantidade = float(total_quantidade) + float(item.quantidade)
        total = total + (float(item.valor) * float(item.quantidade))

    media = total / total_quantidade

    return float(media)

@login_required(login_url='/login/')
def verificar_estoque(request):
    id_produto = request.POST.get("id_produto")

    items = MovimentacaoProdutos.objects.filter(
        id_produto=id_produto
    )

    total = 0
    for item in items:
        if item.tipo == 'E':
            total = float(total) + float(item.quantidade)
        elif item.tipo == 'S':
            total = float(total) - float(item.quantidade)

    data = {
        "saldo": total
    }

    return JsonResponse(data, safe=False)

@login_required(login_url='/login/')
def verificar_media(request):
    id_produto = request.POST.get("id_produto")
    tipo = request.POST.get("tipo")

    media = 0
    item = MediaProduto.objects.filter(
        id_produto=id_produto,
        tipo=tipo
    ).latest('id_media_produto')
    if item:
        media = item.valor

    data = {
        "media": media
    }

    return JsonResponse(data, safe=False)

def lista_comprados():
    items = MovimentacaoProdutos.objects.filter(
        tipo="E"
    )

    data = list()

    for item in items:

        media = MediaProduto.objects.filter(
            id_produto=item.id_produto_id,
            tipo="E"
        ).latest('id_media_produto')

        data.append({
            "descricao": item.id_produto.descricao,
            "quantidade": item.quantidade,
            "valor": item.valor,
            "media": media.valor
        })

    return data

def lista_vendidos():
    items = MovimentacaoProdutos.objects.filter(
        tipo="S"
    )

    data = list()

    for item in items:
        media = MediaProduto.objects.filter(
            id_produto=item.id_produto_id,
            tipo="S"
        ).latest('id_media_produto')

        data.append({
            "descricao": item.id_produto.descricao,
            "quantidade": item.quantidade,
            "valor": item.valor,
            "media": media.valor
        })

    return data

@login_required(login_url='/login/')
def datatable_produtos(request):

        columns = list()
        columns = [
            {
                'cl': 'id_produto',
                'type': 'str'
            },
            {
                'cl': 'descricao',
                'type': 'str'
            },
        ]

        where = {}

        order = [
            'id_produto',
        ]

        datatable = DataTableFilter(request, Produtos, columns, where, order)

        return JsonResponse(datatable.data, safe=False, json_dumps_params={'indent': 2})

@login_required(login_url='/login/')
def datatable_movimentacao_produto(request):

        id_produto = request.POST.get("id_produto", None)

        columns = list()
        columns = [
            {
                'cl': 'tipo',
                'type': 'str'
            },
            {
                'cl': 'quantidade',
                'type': 'str'
            },
            {
                'cl': 'dt_movimentacao',
                'type': 'date'
            },
        ]

        where = {
            'id_produto': id_produto
        }

        order = [
            'dt_movimentacao',
        ]

        datatable = DataTableFilter(request, MovimentacaoProdutos, columns, where, order)

        return JsonResponse(datatable.data, safe=False, json_dumps_params={'indent': 2})

@login_required(login_url='/login/')
def datatable_estoque_produtos(request):

    columns = list()
    columns = [
        'descricao',
        'entrada',
        'saida',
        'saldo',
        'id_produto_id'
    ]

    query = '''
       SELECT * FROM v_estoque_produtos
    '''

    where = None

    value_where = []

    group_by = None

    datatable = DataTableRaw(request, MovimentacaoProdutos, columns, query, where, value_where, group_by)

    return JsonResponse(datatable.data, safe=False, json_dumps_params={'indent': 2})


import matplotlib.pyplot as plt
import matplotlib.style
import csv
import numpy as np
import pandas as pd
import glob
import math
import re

matplotlib.style.use('ggplot')

class PlotStatistics:
    def __init__(self):
        pass

    def plotReward(self, pathToCSV, numMovAvg):
        data = pd.read_csv(pathToCSV)
        data = data.rolling(window=numMovAvg).mean()

        plt.plot(data)

        plt.title('Game Reward over Time')
        plt.xlabel('Number of Games')
        plt.ylabel('Game Reward')
        plt.show()

    def plotMultiple(self, pathToFolder, numMovAvg):
        reward_files = glob.glob(pathToFolder + "/Reward*.csv")

        if len(reward_files) == 0:
            print("No reward.csv files found")

        for n in range(math.ceil(len(reward_files)/4)): # Plot 4 rewards in one figure
            row = 0
            col = 0
            temp_files = reward_files[n*4:n*4+4]
            fig, axs = plt.subplots(2, 2)
            plt.figure(n+1)

            for i, path in enumerate(temp_files):
                if i%2 == 0 and i > 0:
                    row = row + 1

                data = pd.read_csv(path)
                data = data.rolling(window=numMovAvg).mean()
                axs[row, col].plot(data)
                # Regex for getting title
                title = re.findall("(?<=Reward_)(.*?)(?=.csv)", path)
                axs[row, col].set_title(title)

                col = col + 1
                if col > 1:
                    col = 0

            fig.tight_layout()
        plt.show()

    def plot_chromosome_3D(self, path_to_csv):
        # chromosomes_generations = pd.read_csv(path_to_csv)
        # print(np.array(chromosomes_generations)[:,0])

        fig = plt.figure()
        ax = fig.add_subplot(projection='3d')

        gen_chromosomes = []

        with open(path_to_csv, mode='r') as csv_file:
            for i, row in enumerate(csv_file):
                tmp_chromosomes = []
                if i > 3:
                    print("Error more than 3 generations check csv file")

                for j in range(len(row)): # all chromosome from one generations
                    if row[j] == '[':
                        r = np.fromstring(row[1+j:-1], sep=' ')
                        tmp_chromosomes.append(r)

                gen_chromosomes.append(tmp_chromosomes)
        gen_chromosomes = np.array(gen_chromosomes)
        print(len(gen_chromosomes[2][:,0]))
        ax.scatter(gen_chromosomes[0][:,0], gen_chromosomes[0][:,1], gen_chromosomes[0][:,2], marker='o', color=['red'], label='Generation 1') # generation 1 (x,y,z)
        ax.scatter(gen_chromosomes[1][:,0], gen_chromosomes[1][:,1], gen_chromosomes[1][:,2], marker='^', color=['blue'], label='Generation 50') # generation 2 (x,y,z)
        ax.scatter(gen_chromosomes[2][:,0], gen_chromosomes[2][:,1], gen_chromosomes[2][:,2], marker='*', color=['green'], label='Generation 100') # generation 3 (x,y,z)
        ax.legend()
        #for gen_chrom in gen_chromosomes:1, 1.5, 2.5, 3, 3.5, 6.5, 5, 6, 7, 8, 7.5
           # ax.scatter(gen_chrom[:,0], gen_chrom[:,1], gen_chrom[:,2])

        ax.set_xlabel(r'$\epsilon$')
        ax.set_ylabel(r'$\gamma$')
        ax.set_zlabel(r'$\alpha$')

        plt.show()

    def plot_chromosome_2D(self, path_to_csv, labels):
        gen_chromosomes = []

        with open(path_to_csv, mode='r') as csv_file:
            for i, row in enumerate(csv_file):
                tmp_chromosomes = []
                for j in range(len(row)): # all chromosome from one generations
                    if row[j] == '[':
                        r = np.fromstring(row[1+j:-1], sep=' ')
                        tmp_chromosomes.append(r)

                gen_chromosomes.append(tmp_chromosomes)

        gen_chromosomes = np.array(gen_chromosomes)

        plot_idx = 0
        prev = 0

        for chrom_i in range(0,(len(gen_chromosomes[0][0])),2):
            fig = plt.figure(plot_idx)
            ax = fig.add_subplot()
            # ax[plot_idx].axis('equal')
            ax.scatter(gen_chromosomes[0][:,chrom_i], gen_chromosomes[0][:,chrom_i+1], marker='o', color=['red']) # generation 1
            ax.scatter(gen_chromosomes[1][:,chrom_i], gen_chromosomes[1][:,chrom_i+1], marker='^', color=['blue']) # generation 2
            ax.scatter(gen_chromosomes[2][:,chrom_i], gen_chromosomes[2][:,chrom_i+1], marker='*', color=['green']) # generation 3
            ax.set_xlabel(labels[chrom_i])
            ax.set_ylabel(labels[chrom_i+1])

            if prev == chrom_i:
                plot_idx += 1

        plt.show()

    def plot_chromosome_2D_qlearning(self, path_to_csv, labels):
        gen_chromosomes = []

        with open(path_to_csv, mode='r') as csv_file:
            for i, row in enumerate(csv_file):
                tmp_chromosomes = []
                for j in range(len(row)): # all chromosome from one generations
                    if row[j] == '[':
                        r = np.fromstring(row[1+j:-1], sep=' ')
                        tmp_chromosomes.append(r)

                gen_chromosomes.append(tmp_chromosomes)

        gen_chromosomes = np.array(gen_chromosomes)

        plot_idx = 0
        prev = 0

        for chrom_i in range((len(gen_chromosomes[0][0]))):
            for chrom_j in range((len(gen_chromosomes[0][0]))):
                fig = plt.figure(plot_idx)
                ax = fig.add_subplot()
                if chrom_i >= chrom_j:
                    continue
                # ax[plot_idx].axis('equal')
                ax.scatter(gen_chromosomes[0][:,chrom_i], gen_chromosomes[0][:,chrom_j], marker='o', color=['red'], label='Generation 1') # generation 1
                ax.scatter(gen_chromosomes[1][:,chrom_i], gen_chromosomes[1][:,chrom_j], marker='^', color=['blue'], label='Generation 50') # generation 2
                ax.scatter(gen_chromosomes[2][:,chrom_i], gen_chromosomes[2][:,chrom_j], marker='*', color=['green'], label='Generation 100') # generation 3
               # ax.set_xlabel(r'$\theta$%s' %(chrom_i+1))
               # ax.set_ylabel(r'$\theta$%s' %(chrom_j+1))
                ax.set_xlabel(labels[chrom_i])
                ax.set_ylabel(labels[chrom_j])

                ax.legend()
                if prev == chrom_i:
                    plot_idx += 1

        plt.show()


import logging

from src.common.contracts import keeper_contract
from src.common.execution import check_gas_price
from src.common.ipfs import fetch_harvest_params
from src.common.tasks import BaseTask
from src.config.settings import settings
from src.harvest.execution import submit_harvest_transaction

logger = logging.getLogger(__name__)

class HarvestTask(BaseTask):
    async def process_block(self) -> None:
        """Check vault state and send harvest transaction if needed."""
        if not await keeper_contract.can_harvest(settings.vault):
            return

        # check current gas prices
        if not await check_gas_price():
            return

        last_rewards = await keeper_contract.get_last_rewards_update()
        if not last_rewards:
            return
        harvest_params = await fetch_harvest_params(
            vault_address=settings.vault,
            ipfs_hash=last_rewards.ipfs_hash,
            rewards_root=last_rewards.rewards_root,
        )
        if not harvest_params:
            return

        logger.info('Starting vault harvest')
        tx_hash = await submit_harvest_transaction(harvest_params)
        if not tx_hash:
            return
        logger.info('Successfully harvested vault')


import os
import pathlib

from wand.image import Image as Wim

from spinorama import logger

def write_multiformat(chart, filename, force):
    """Write a png file and then convert and save to jpg and webp"""
    if not pathlib.Path(filename).is_file() or force:
        chart.write_image(filename)
    if os.path.getsize(filename) == 0:
        logger.warning("Saving %s failed!", filename)
        return
    logger.info("Saving %s", filename)

    with Wim(filename=filename) as pict:
        filename = filename.replace("_large", "")
        webp = "{}.webp".format(filename[:-4])
        if not pathlib.Path(webp).is_file() or force:
            pict.convert("webp").save(filename=webp)
        pict.compression_quality = 75
        jpg = "{}.jpg".format(filename[:-4])
        if not pathlib.Path(jpg).is_file() or force:
            pict.convert("jpg").save(filename=jpg)


'''
Tools to calculate thermal simulations for LIFU experiments

ABOUT:
     author        - Samuel Pichardo
     date          - June 23, 2021
     last update   - Nov 27, 2021

'''

import numpy as np
import matplotlib.pyplot as plt
from  scipy.io import loadmat,savemat
from BabelViscoFDTD.tools.RayleighAndBHTE import BHTE,BHTEMultiplePressureFields
from BabelViscoFDTD.H5pySimple import SaveToH5py,ReadFromH5py
from scipy.io import loadmat,savemat
from platform import platform
from os.path import isfile

def CalculateTemperatureEffects(InputPData,DutyCycle,Isppa,sel_p='p_amp',
                                bPlot=True,
                                PRF=1500,
                                bUseTargetLocations=False,
                                bForceRecalc=False,
                                bCalculateLosses=False):#this will help to calculate the final voltage to apply during experiments
    if 'Windows' in platform() or 'Linux' in platform():
        Backend='CUDA'
    else:
        Backend ='OpenCL'

    if type(InputPData) is str:
        # assert('TxMoved_DataForSim.h5' in InputPData)
        outfname=InputPData.split('.h5')[0]+'-ThermalField-DC-%i-Isppa-%2.1fW' % (DutyCycle*1000,Isppa)

    else:
        assert('TxMoved' in InputPData[0])
        outfname=InputPData[0].split('.h5')[0]+'-ThermalField-DC-%i-Isppa-%2.1fW-PRF-%iHz' % (DutyCycle*1000,Isppa,PRF)

    print(outfname)
    if bForceRecalc==False:
        if isfile(outfname+'.h5'):
            print('skipping', outfname)
            return outfname
    if type(InputPData) is str:
        dt=0.01
        Input=ReadFromH5py(InputPData)
    else:
        dt=0.01

        #this has to be always the center steering point!
        Input=ReadFromH5py(InputPData[0])

        AllInputs=np.zeros((len(InputPData),Input[sel_p].shape[0],Input[sel_p].shape[1],
                            Input[sel_p].shape[2]),Input[sel_p].dtype)
        AllInputs[0,:,:,:]=np.ascontiguousarray(np.flip(Input[sel_p],axis=2)).copy()
        for n in range(len(InputPData)):
            AllInputs[n,:,:,:]=np.ascontiguousarray(np.flip(ReadFromH5py(InputPData[n])[sel_p],axis=2))
        NCyclesOn=int(DutyCycle/dt)
        NCyclesOff=int(1.0/dt)-NCyclesOn
        assert(NCyclesOn>0)
        assert(NCyclesOff>0 )

        nStepsOnOffList=np.zeros((len(InputPData),2),np.int32)
        #all points have the same duration
        nStepsOnOffList[:,0]=NCyclesOn
        nStepsOnOffList[:,1]=NCyclesOff

    #savemat(InputPData.split('.h5')[0]+'.mat',Input)
    MaterialList={}
    MaterialList['Density']=Input['Material'][:,0]
    MaterialList['SoS']=Input['Material'][:,1]
    MaterialList['Attenuation']=Input['Material'][:,3]
    #Water, Skin, Cortical, Trabecular, Brain

    #https://itis.swiss/virtual-population/tissue-properties/database/heat-capacity/
    MaterialList['SpecificHeat']=[4178,3391,1313,2274,3630] #(J/kg/°C)
    #https://itis.swiss/virtual-population/tissue-properties/database/thermal-conductivity/
    MaterialList['Conductivity']=[0.6,0.37,0.32,0.31,0.51] # (W/m/°C)
    #https://itis.swiss/virtual-population/tissue-properties/database/heat-transfer-rate/
    MaterialList['Perfusion']=np.array([0,106,10,30,559])

    MaterialList['Absorption']=np.array([0,0.85,0.16,0.15,0.85])

    MaterialList['InitTemperature']=[37,37,37,37,37]
    SaveDict={}
    SaveDict['MaterialList']=MaterialList

    nFactorMonitoring=int(50e-3/dt) # we just track every 50 ms
    if type(InputPData) is str:
        DurationUS=40
    else:
        DurationUS=120
    TotalDurationSteps=int((DurationUS+.001)/dt)
    nStepsOn=int(DurationUS/dt)

    xf=Input['x_vec']
    yf=Input['y_vec']
    zf=Input['z_vec']

    pAmp=np.ascontiguousarray(np.flip(Input[sel_p],axis=2))
    print('pAmp.shape',pAmp.shape)

    MaterialMap=np.ascontiguousarray(np.flip(Input['MaterialMap'],axis=2))

    pAmpBrain=pAmp.copy()
    pAmpBrain[MaterialMap!=4]=0.0

#     cx=np.argmin(np.abs(xf))
#     cy=np.argmin(np.abs(yf))
#     cz=np.argmin(np.abs(zf-0.135))
#     zl=cz

    #PressureTarget=pAmp[cx,cy,zl]
    PressureTarget=pAmpBrain.max()
    if bUseTargetLocations:
        cx=Input['TargetLocation'][0]
        cy=Input['TargetLocation'][1]
        cz=Input['TargetLocation'][2]
    else:
        cx,cy,cz=np.where(pAmpBrain==PressureTarget)
        cx=cx[0]
        cy=cy[0]
        cz=cz[0]
    zl=cz
    print('Location Max Pessure',cx,cy,cz,'\n',
          xf[cx],yf[cy],zf[cz],
          xf.shape,yf.shape,zf.shape,pAmp.shape,'\n',
          PressureTarget/1e6)

    PlanAtMaximum=pAmpBrain[:,:,cz]
    AcousticEnergy=(PlanAtMaximum**2/2/MaterialList['Density'][4]/ MaterialList['SoS'][4]*((xf[1]-xf[0])**2)).sum()

    print('Acoustic Energy at maximum plane',AcousticEnergy)

    if bCalculateLosses:
        assert(type(InputPData) is str) # we only do this for single focus
        WaterInputPData=InputPData.replace('DataForSim.h5','WaterOnly_DataForSim.h5')
        InputWater=ReadFromH5py(WaterInputPData)

        RefocuInputPData=InputPData.replace('TxMoved_DataForSim.h5','DataForSim.h5')
        InputRefocus=ReadFromH5py(RefocuInputPData)
        #the refocus case will have a slighlty different matrix dimensions
        MateriaMapRefocus=np.ascontiguousarray(np.flip(InputRefocus['MaterialMap'],axis=2))
        xfr=InputRefocus['x_vec']
        yfr=InputRefocus['y_vec']
        zfr=InputRefocus['z_vec']

        pAmpWater=np.ascontiguousarray(np.flip(InputWater['p_amp'],axis=2))
        pAmpWater[MaterialMap!=4]=0.0
        if bUseTargetLocations:
            cxw=InputRefocus['TargetLocation'][0]
            cyw=InputRefocus['TargetLocation'][1]
            czw=InputRefocus['TargetLocation'][2]
        else:
            cxw,cyw,czw=np.where(pAmpWater==pAmpWater.max())
            cxw=cxw[0]
            cyw=cyw[0]
            czw=czw[0]
        print('Location Max Pessure Water',cxw,cyw,czw,'\n',
              xf[cxw],yf[cyw],zf[czw],pAmpWater.max()/1e6)

        pAmpRefocus=np.ascontiguousarray(np.flip(InputRefocus['p_amp_refocus'],axis=2))
        pAmpRefocus[MateriaMapRefocus!=4]=0.0
        cxr,cyr,czr=np.where(pAmpRefocus==pAmpRefocus.max())
        cxr=cxr[0]
        cyr=cyr[0]
        czr=czr[0]
        print('Location Max Pessure Refocus',cxr,cyr,czr,'\n',
              xfr[cxr],yfr[cyr],zfr[czr],pAmpRefocus.max()/1e6)

        PlanAtMaximumWater=pAmpWater[:,:,cz]
        AcousticEnergyWater=(PlanAtMaximumWater**2/2/MaterialList['Density'][0]/ MaterialList['SoS'][0]*((xf[1]-xf[0])**2)).sum()
        print('Acoustic Energy at maximum plane water',AcousticEnergyWater)

        PlanAtMaximumWaterMaxLoc=pAmpWater[:,:,czw]
        AcousticEnergyWaterMaxLoc=(PlanAtMaximumWaterMaxLoc**2/2/MaterialList['Density'][0]/ MaterialList['SoS'][0]*((xf[1]-xf[0])**2)).sum()
        print('Acoustic Energy at maximum plane water max loc',AcousticEnergyWaterMaxLoc) #must be very close to AcousticEnergyWater

        PlanAtMaximumRefocus=pAmpRefocus[:,:,czr]
        AcousticEnergyRefocus=(PlanAtMaximumRefocus**2/2/MaterialList['Density'][4]/ MaterialList['SoS'][4]*((xf[1]-xf[0])**2)).sum()
        print('Acoustic Energy at maximum plane refocus',AcousticEnergyRefocus)
        if sel_p=='p_amp':
            RatioLosses=AcousticEnergyRefocus/AcousticEnergyWaterMaxLoc
        else:
            RatioLosses=AcousticEnergy/AcousticEnergyWaterMaxLoc
        print('Total losses ratio and in dB with',sel_p,RatioLosses,np.log10(RatioLosses)*10)

    IntensityTarget=PressureTarget**2/(2.0*SaveDict['MaterialList']['SoS'][4]*SaveDict['MaterialList']['Density'][4])
    IntensityTarget=IntensityTarget/1e4
    IntensityRatio=Isppa/IntensityTarget
    PressureAdjust=np.sqrt(Isppa*1e4*2.0*SaveDict['MaterialList']['SoS'][4]*SaveDict['MaterialList']['Density'][4])
    #PressureRatio=np.sqrt(IntensityRatio)
    PressureRatio=PressureAdjust/PressureTarget
    print('IntensityRatio,PressureRatio',IntensityRatio,PressureRatio)

    if type(InputPData) is str:
        ResTemp,ResDose,MonitorSlice,Qarr=BHTE(pAmp*PressureRatio,
                                                      MaterialMap,
                                                      MaterialList,
                                                      (Input['x_vec'][1]-Input['x_vec'][0]),
                                                      TotalDurationSteps,
                                                      nStepsOn,
                                                      cy,
                                                      nFactorMonitoring=nFactorMonitoring,
                                                      dt=dt,
                                                      DutyCycle=DutyCycle,
                                                      Backend=Backend)
    else:
        ResTemp,ResDose,MonitorSlice,Qarr=BHTEMultiplePressureFields(AllInputs*PressureRatio,
                                                      MaterialMap,
                                                      MaterialList,
                                                      (Input['x_vec'][1]-Input['x_vec'][0]),
                                                      TotalDurationSteps,
                                                      nStepsOnOffList,
                                                      cy,
                                                      nFactorMonitoring=nFactorMonitoring,
                                                      dt=dt,
                                                      Backend=Backend)
        ##we combine all acoustic fields to show the coverage
        pAmp=np.max(AllInputs,axis=0)

    #SaveDict['ResTemp']=ResTemp
    SaveDict['MonitorSlice']=MonitorSlice[:,:,int(nStepsOn/nFactorMonitoring)-1]

    SaveDict['p_map']=pAmp[:,cy,:].copy()*PressureRatio
    SaveDict['MaterialMap']=MaterialMap[:,cy,:]

    SelBrain=MaterialMap[:,cy,:]==4

    SelSkin = MaterialMap[:,cy,:]==1

    SelSkull = (MaterialMap[:,cy,:]>1) &\
                (MaterialMap[:,cy,:]<4)

    TI=(MonitorSlice[:,:,int(nStepsOn/nFactorMonitoring)-1][SelBrain]).max()

    TIS=(MonitorSlice[:,:,int(nStepsOn/nFactorMonitoring)-1][SelSkin]).max()

    TIC=(MonitorSlice[:,:,int(nStepsOn/nFactorMonitoring)-1][SelSkull]).max()

    print('Max. Temp. Brain, Max Temp. Skin, Max Temp. Skull',TI,TIS,TIC);

    MaxBrainPressure = SaveDict['p_map'][SaveDict['MaterialMap']==4].max()
    MI=MaxBrainPressure/1e6/np.sqrt(0.7)
    MaxIsppa=MaxBrainPressure**2/(2.0*SaveDict['MaterialList']['SoS'][4]*SaveDict['MaterialList']['Density'][4])
    MaxIsppa=MaxIsppa/1e4
    MaxIspta=DutyCycle*MaxIsppa

    Ispta =DutyCycle*Isppa

    SaveDict['MaxBrainPressure']=MaxBrainPressure
    SaveDict['TempProfileTarget']=MonitorSlice[cy,zl,:]
    SaveDict['TimeProfileTarget']=np.arange(SaveDict['TempProfileTarget'].size)*dt*nFactorMonitoring;
    SaveDict['MI']=MI
    SaveDict['x_vec']=xf*1e3
    SaveDict['y_vec']=yf*1e3
    SaveDict['z_vec']=zf*1e3
    SaveDict['TI']=TI-37.0
    SaveDict['TIC']=TIC-37.0
    SaveDict['TIS']=TIS-37.0
    SaveDict['MaxIsppa']=MaxIsppa
    SaveDict['MaxIspta']=MaxIspta
    SaveDict['Isppa']=Isppa
    SaveDict['Ispta']=Ispta
    SaveDict['TempEndFUS']=ResTemp
    SaveDict['DoseEndFUS']=ResDose
    SaveDict['TargetLocation']=Input['TargetLocation'][[0,2]]

    if bCalculateLosses:
        SaveDict['RatioLosses']=RatioLosses

    SaveToH5py(SaveDict,outfname+'.h5')
    savemat(outfname+'.mat',SaveDict)

    if bPlot:

        plt.figure()
        plt.plot(zf,SaveDict['p_map'][cx,:])
        plt.figure(figsize=(12,12))
        plt.subplot(2,2,1)
        plt.imshow(MaterialMap[:,cy,:].T,
                   extent=[xf.min(),xf.max(),zf.max(),zf.min()],cmap=plt.cm.jet)
        plt.colorbar()

        plt.subplot(2,2,2)
        plt.imshow(SaveDict['p_map'].T,extent=[xf.min(),xf.max(),zf.max(),zf.min()],
                   cmap=plt.cm.jet)
        plt.colorbar()

        plt.subplot(2,2,3)
        plt.imshow(MonitorSlice[:,:,int(nStepsOn/nFactorMonitoring)-1].T,
                   extent=[xf.min(),xf.max(),zf.max(),zf.min()],cmap=plt.cm.jet)
        plt.colorbar()

        plt.subplot(2,2,4)
        plt.imshow((MonitorSlice[:,:,int(nStepsOn/nFactorMonitoring)-1] *
                   (MaterialMap[:,cy,:]==1).astype(float)).T,
                   extent=[xf.min(),xf.max(),zf.max(),zf.min()],cmap=plt.cm.jet)
        plt.colorbar()
        plt.figure()
        plt.plot(SaveDict['TimeProfileTarget'],SaveDict['TempProfileTarget'])

    return outfname


from sklearn import ensemble, preprocessing
from linear_regression_try1 import get_error, get_data, get_some_columns

def main():
    print('working ...')
    # #############################################################################
    # Load data

    train_df, test_df = get_data()

    y_train = train_df['trip_duration']
    y_test = test_df['trip_duration']
    X_train, X_test = get_some_columns(train_df, test_df, [
        'pickup_longitude', 'dropoff_longitude', 'pickup_latitude',
        'dropoff_latitude',
        # just others
        'pickup_hour', 'pickup_minute',
        # 'pickup_month', 'pickup_day',
        # 'pickup_week', 'pickup_day_of_week',
        # weather
        # 'pickup_humidity', 'pickup_pressure',
        # 'pickup_wind_speed', 'pickup_temperature',
    ])

    scaler = preprocessing.StandardScaler().fit(X_train)
    X_train_sc = scaler.transform(X_train)
    X_test_sc = scaler.transform(X_test)

    # #############################################################################
    # Fit regression model
    params = {'n_estimators': 500, 'max_depth': 4, 'min_samples_split': 5,
              'learning_rate': 0.2, 'loss': 'ls'}
    clf = ensemble.GradientBoostingRegressor(**params)
    clf.fit(X_train_sc, y_train)

    error = get_error(clf.predict(X_test_sc), y_test)
    print("The error: %.4f" % error)

if __name__ == '__main__':
    main()


from sqlalchemy import create_engine, event
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import sessionmaker
from sqlite3 import Connection as SQLite3Connection

__author__ = 'Alex Haslehurst'

class MediaContext:
    Base = declarative_base()

    def __init__(self, connection_string, echo=False):
        self._Engine = create_engine(connection_string, echo=echo)
        event.listen(self._Engine, 'connect', MediaContext._set_sqlite_pragma)
        self.SessionMaker = sessionmaker(bind=self._Engine)

    def drop_and_create(self):
        meta = self.Base.metadata
        meta.drop_all(self._Engine)
        meta.create_all(self._Engine)

    # Check we are using sqlite3 on connect and if so, enable foreign keys.
    @staticmethod
    def _set_sqlite_pragma(dbapi_connection, connection_record):
        if isinstance(dbapi_connection, SQLite3Connection):
            cursor = dbapi_connection.cursor()
            cursor.execute("PRAGMA foreign_keys=ON;")
            cursor.close()