From f0053e38785f1c4d35f1fa0739e18c1cc07b0953 Mon Sep 17 00:00:00 2001
From: raul-silva <40128918+raul-silva@users.noreply.github.com>
Date: Mon, 28 Apr 2025 16:03:59 +0200
Subject: [PATCH] v250428: Removing Cache where it is not needed

For single event scripts it is not necessary.
Also adding a close call for sqlite connections (for safety).
Minor changes of homogeneity.
---
 .../Rebuild_All_Events_LMT.ipynb              | 14 +---
 .../Rebuild_Huddling_events.ipynb             | 64 +++++++----------
 .../Rebuild_InterestZone_events.ipynb         | 35 ++++-----
 EMT/EMT_notebooks/Rebuild_SAP_events.ipynb    | 63 +++++++---------
 .../Rebuild_WallJump_events.ipynb             | 64 +++++++----------
 .../Rebuild_WaterZone_events.ipynb            | 28 +++-----
 .../Rebuild_All_Events_LMT_script.py          |  6 +-
 .../Rebuild_Huddling_events_script.py         | 72 ++++++++-----------
 EMT/EMT_scripts/Rebuild_SAP_events_script.py  | 67 ++++++++---------
 .../Rebuild_WallJump_events_script.py         | 68 ++++++++----------
 EMT/lmtanalysis/Animal.py                     | 42 +++++------
 EMT/lmtanalysis/BuildEventHuddling.py         |  9 ++-
 .../BuildEventHuddling_parallel.py            |  8 ++-
 EMT/lmtanalysis/BuildEventSAP.py              | 10 +--
 EMT/lmtanalysis/BuildEventSAP_parallel.py     |  3 +-
 EMT/lmtanalysis/BuildEventWallJump.py         |  2 +-
 .../BuildEventWallJump_parallel.py            | 11 ++-
 EMT/lmtanalysis/BuildEventWaterPoint.py       |  8 +--
 EMT/lmtanalysis/Event.py                      |  8 +--
 EMT/lmtanalysis/Util.py                       |  2 +-
 EMT/setup.py                                  |  2 +-
 21 files changed, 244 insertions(+), 342 deletions(-)

diff --git a/EMT/EMT_notebooks/Rebuild_All_Events_LMT.ipynb b/EMT/EMT_notebooks/Rebuild_All_Events_LMT.ipynb
index f04ab8f..d596977 100644
--- a/EMT/EMT_notebooks/Rebuild_All_Events_LMT.ipynb
+++ b/EMT/EMT_notebooks/Rebuild_All_Events_LMT.ipynb
@@ -35,13 +35,12 @@
     "    BuildEventFloorSniffing, BuildEventSocialApproach, BuildEventSocialEscape, BuildEventApproachContact, \\\n",
     "    BuildEventOralOralContact, BuildEventApproachRear, BuildEventGroup2, BuildEventGroup3, BuildEventGroup4, \\\n",
     "    BuildEventOralGenitalContact, BuildEventStop, BuildEventMove, BuildEventGroup3MakeBreak, BuildEventGroup4MakeBreak, \\\n",
-    "    BuildEventSideBySide, BuildEventSideBySideOpposite, BuildDataBaseIndex, BuildEventWallJump, BuildEventSAP, \\\n",
+    "    BuildEventSideBySide, BuildEventSideBySideOpposite, BuildEventWallJump, BuildEventSAP, \\\n",
     "    BuildEventOralSideSequence, CheckWrongAnimal, BuildEventDetection, BuildEventCenterPeripheryLocation, BuildEventNest4, \\\n",
     "    BuildEventNest3, BuildEventGetAway, BuildEventOtherContact, BuildEventPassiveAnogenitalSniff, BuildEventRearCenterPeriphery\n",
     "from lmtanalysis.TaskLogger import TaskLogger\n",
     "from lmtanalysis.FileUtil import getFilesToProcess\n",
     "from lmtanalysis.EventTimeLineCache import flushEventTimeLineCache, disableEventTimeLineCache\n",
-    "from lmtanalysis.EventTimeLineCache import EventTimeLineCached\n",
     "from lmtanalysis.AnimalType import AnimalType\n"
    ]
   },
@@ -192,17 +191,6 @@
     "    except:\n",
     "        print( \"METADATA field already exists\" , file )\n",
     "\n",
-    "    #build index\n",
-    "\n",
-    "    BuildDataBaseIndex.buildDataBaseIndex( connection, force=False )\n",
-    "\n",
-    "    '''\n",
-    "    # build sensor data\n",
-    "    animalPool = AnimalPool( )\n",
-    "    animalPool.loadAnimals( connection )\n",
-    "    animalPool.buildSensorData(file)\n",
-    "    '''\n",
-    "\n",
     "    currentT = minT\n",
     "\n",
     "    try:\n",
diff --git a/EMT/EMT_notebooks/Rebuild_Huddling_events.ipynb b/EMT/EMT_notebooks/Rebuild_Huddling_events.ipynb
index 9bc5997..7f160dc 100644
--- a/EMT/EMT_notebooks/Rebuild_Huddling_events.ipynb
+++ b/EMT/EMT_notebooks/Rebuild_Huddling_events.ipynb
@@ -47,9 +47,7 @@
     "minT = 0\n",
     "maxT = 1*oneDay\n",
     "''' time window to compute the events (in frame). '''\n",
-    "windowT = 1*oneDay\n",
-    "''' speed up process '''\n",
-    "USE_CACHE_LOAD_DETECTION_CACHE = True"
+    "windowT = 1*oneDay\n"
    ]
   },
   {
@@ -79,7 +77,7 @@
     "from lmtanalysis.Animal import *\n",
     "from lmtanalysis.Event import *\n",
     "from lmtanalysis.Measure import *\n",
-    "from lmtanalysis import BuildEventHuddling, BuildEventHuddling_oldv, BuildEventHuddling_parallel, BuildDataBaseIndex\n",
+    "from lmtanalysis import BuildEventHuddling, BuildEventHuddling_oldv, BuildEventHuddling_parallel\n",
     "from lmtanalysis.FileUtil import getFilesToProcess\n",
     "from lmtanalysis.EventTimeLineCache import flushEventTimeLineCache, disableEventTimeLineCache\n",
     "from lmtanalysis.AnimalType import AnimalType\n",
@@ -99,39 +97,31 @@
     "\n",
     "\n",
     "def processTimeWindow(file, connection, currentMinT , currentMaxT, mode=\"new\"):\n",
-    "\n",
     "    animalPool = None\n",
     "    flushEventTimeLineCache()\n",
     "\n",
-    "    if (USE_CACHE_LOAD_DETECTION_CACHE):\n",
-    "        print(\"Caching load of animal detection...\")\n",
-    "        animalPool = AnimalPool()\n",
-    "        animalPool.loadAnimals(connection)\n",
-    "        animalPool.loadDetection(start = currentMinT, end = currentMaxT)\n",
-    "        print(\"Caching load of animal detection done.\")\n",
-    "\n",
-    "        chrono = Chronometer(str(BuildEventHuddling))\n",
-    "        if \"old\" in mode.lower():\n",
-    "            BuildEventHuddling_oldv.reBuildEvent(connection, file,\n",
-    "                                        tmin=currentMinT,\n",
-    "                                        tmax=currentMaxT,\n",
-    "                                        pool = animalPool,\n",
-    "                                        animalType = AnimalType.MOUSE)\n",
-    "        elif \"new\" in mode.lower():\n",
-    "            BuildEventHuddling.reBuildEvent(connection, file,\n",
-    "                                        tmin=currentMinT,\n",
-    "                                        tmax=currentMaxT,\n",
-    "                                        pool = animalPool,\n",
-    "                                        animalType = AnimalType.MOUSE)\n",
-    "        elif \"parallel\" in mode.lower():\n",
-    "            BuildEventHuddling_parallel.reBuildEvent(connection, file,\n",
-    "                                        tmin=currentMinT,\n",
-    "                                        tmax=currentMaxT,\n",
-    "                                        pool = animalPool,\n",
-    "                                        animalType = AnimalType.MOUSE)\n",
-    "        else:\n",
-    "            raise NotImplementedError(\"This mode does not exist. Use 'old', 'new' or 'parallel'\")\n",
-    "        chrono.printTimeInS()\n",
+    "    chrono = Chronometer(str(BuildEventHuddling))\n",
+    "    if \"old\" in mode.lower():\n",
+    "        BuildEventHuddling_oldv.reBuildEvent(connection, file,\n",
+    "                                    tmin=currentMinT,\n",
+    "                                    tmax=currentMaxT,\n",
+    "                                    pool = animalPool,\n",
+    "                                    animalType = AnimalType.MOUSE)\n",
+    "    elif \"new\" in mode.lower():\n",
+    "        BuildEventHuddling.reBuildEvent(connection, file,\n",
+    "                                    tmin=currentMinT,\n",
+    "                                    tmax=currentMaxT,\n",
+    "                                    pool = animalPool,\n",
+    "                                    animalType = AnimalType.MOUSE)\n",
+    "    elif \"parallel\" in mode.lower():\n",
+    "        BuildEventHuddling_parallel.reBuildEvent(connection, file,\n",
+    "                                    tmin=currentMinT,\n",
+    "                                    tmax=currentMaxT,\n",
+    "                                    pool = animalPool,\n",
+    "                                    animalType = AnimalType.MOUSE)\n",
+    "    else:\n",
+    "        raise NotImplementedError(\"This mode does not exist. Use 'old', 'new' or 'parallel'\")\n",
+    "    chrono.printTimeInS()\n",
     "\n",
     "def process(file, mode=\"new\"):\n",
     "\n",
@@ -158,9 +148,6 @@
     "    except:\n",
     "        print(\"METADATA field already exists\" , file)\n",
     "\n",
-    "    # Build Index\n",
-    "    BuildDataBaseIndex.buildDataBaseIndex(connection, force=False)\n",
-    "\n",
     "    currentT = minT\n",
     "\n",
     "    try:\n",
@@ -193,6 +180,9 @@
     "\n",
     "        raise FileProcessException()\n",
     "\n",
+    "    connection.close()\n",
+    "\n",
+    "\n",
     "print(\"Code launched.\")\n",
     "\n",
     "mem = virtual_memory()\n",
diff --git a/EMT/EMT_notebooks/Rebuild_InterestZone_events.ipynb b/EMT/EMT_notebooks/Rebuild_InterestZone_events.ipynb
index d96443f..019f896 100644
--- a/EMT/EMT_notebooks/Rebuild_InterestZone_events.ipynb
+++ b/EMT/EMT_notebooks/Rebuild_InterestZone_events.ipynb
@@ -39,9 +39,7 @@
     "minT = 0\n",
     "maxT = 3*oneDay\n",
     "''' time window to compute the events (in frame). '''\n",
-    "windowT = 1*oneDay\n",
-    "''' speed up process '''\n",
-    "USE_CACHE_LOAD_DETECTION_CACHE = True"
+    "windowT = 1*oneDay\n"
    ]
   },
   {
@@ -74,11 +72,10 @@
     "from lmtanalysis.Animal import *\n",
     "from lmtanalysis.TaskLogger import TaskLogger\n",
     "from lmtanalysis.Event import *\n",
-    "from lmtanalysis.Measure import *\n",
-    "from lmtanalysis.Util import getAllEvents, getNumberOfFrames\n",
-    "from lmtanalysis import BuildEventInterestZoneLocation, BuildDataBaseIndex\n",
+    "from lmtanalysis.Util import getNumberOfFrames\n",
+    "from lmtanalysis import BuildEventInterestZoneLocation\n",
     "from lmtanalysis.FileUtil import getFilesToProcess\n",
-    "from lmtanalysis.EventTimeLineCache import EventTimeLineCached, flushEventTimeLineCache, disableEventTimeLineCache\n",
+    "from lmtanalysis.EventTimeLineCache import flushEventTimeLineCache, disableEventTimeLineCache\n",
     "from lmtanalysis.AnimalType import AnimalType\n",
     "from lmtanalysis.Parameters import getAnimalTypeParameters\n",
     "\n",
@@ -128,18 +125,11 @@
     "    animalPool = None\n",
     "    flushEventTimeLineCache()\n",
     "\n",
-    "    if ( USE_CACHE_LOAD_DETECTION_CACHE ):\n",
-    "        print(\"Caching load of animal detection...\")\n",
-    "        animalPool = AnimalPool()\n",
-    "        animalPool.loadAnimals(connection)\n",
-    "        animalPool.loadDetection(start = currentMinT, end = currentMaxT)\n",
-    "        print(\"Caching load of animal detection done.\")\n",
-    "\n",
-    "        chrono = Chronometer(str(BuildEventInterestZoneLocation))\n",
-    "        BuildEventInterestZoneLocation.reBuildEvent( connection, file, tmin=currentMinT, tmax=currentMaxT,\n",
-    "                                                    pool = animalPool, animalType = AnimalType.MOUSE,\n",
-    "                                                    zone_name=zone_name, zone=zone_coords  )\n",
-    "        chrono.printTimeInS()\n",
+    "    chrono = Chronometer(str(BuildEventInterestZoneLocation))\n",
+    "    BuildEventInterestZoneLocation.reBuildEvent(connection, file, tmin=currentMinT, tmax=currentMaxT,\n",
+    "                                                pool = animalPool, animalType = AnimalType.MOUSE,\n",
+    "                                                zone_name=zone_name, zone=zone_coords )\n",
+    "    chrono.printTimeInS()\n",
     "\n",
     "def process(file, zone_name, zone_coords):\n",
     "    print(file)\n",
@@ -168,9 +158,6 @@
     "    except:\n",
     "        print( \"METADATA field already exists\" , file )\n",
     "\n",
-    "    # Build Index\n",
-    "    BuildDataBaseIndex.buildDataBaseIndex( connection, force=False )\n",
-    "\n",
     "    currentT = minT\n",
     "\n",
     "    try:\n",
@@ -200,6 +187,8 @@
     "\n",
     "        raise FileProcessException()\n",
     "\n",
+    "    connection.close()\n",
+    "\n",
     "def extractFrames(video_file, count):\n",
     "    cap = cv2.VideoCapture(video_file.absolute().__str__())\n",
     "    frame = None\n",
@@ -299,7 +288,7 @@
  ],
  "metadata": {
   "kernelspec": {
-   "display_name": "Python 3 (ipykernel)",
+   "display_name": "LMT",
    "language": "python",
    "name": "python3"
   },
diff --git a/EMT/EMT_notebooks/Rebuild_SAP_events.ipynb b/EMT/EMT_notebooks/Rebuild_SAP_events.ipynb
index edf086a..b63c7b6 100644
--- a/EMT/EMT_notebooks/Rebuild_SAP_events.ipynb
+++ b/EMT/EMT_notebooks/Rebuild_SAP_events.ipynb
@@ -39,9 +39,7 @@
     "minT = 0\n",
     "maxT = 1*oneDay\n",
     "''' time window to compute the events (in frame). '''\n",
-    "windowT = 1*oneDay\n",
-    "''' speed up process '''\n",
-    "USE_CACHE_LOAD_DETECTION_CACHE = True"
+    "windowT = 1*oneDay\n"
    ]
   },
   {
@@ -70,7 +68,7 @@
     "from lmtanalysis.TaskLogger import TaskLogger\n",
     "from lmtanalysis.Animal import *\n",
     "from lmtanalysis.Event import *\n",
-    "from lmtanalysis import BuildEventSAP, BuildEventSAP_oldv, BuildEventSAP_parallel, BuildDataBaseIndex\n",
+    "from lmtanalysis import BuildEventSAP, BuildEventSAP_oldv, BuildEventSAP_parallel\n",
     "from lmtanalysis.FileUtil import getFilesToProcess\n",
     "from lmtanalysis.EventTimeLineCache import flushEventTimeLineCache, disableEventTimeLineCache\n",
     "from lmtanalysis.AnimalType import AnimalType\n",
@@ -94,35 +92,28 @@
     "    animalPool = None\n",
     "    flushEventTimeLineCache()\n",
     "\n",
-    "    if (USE_CACHE_LOAD_DETECTION_CACHE):\n",
-    "        print(\"Caching load of animal detection...\")\n",
-    "        animalPool = AnimalPool()\n",
-    "        animalPool.loadAnimals(connection)\n",
-    "        animalPool.loadDetection(start = currentMinT, end = currentMaxT)\n",
-    "        print(\"Caching load of animal detection done.\")\n",
-    "\n",
-    "        chrono = Chronometer(str(BuildEventSAP))\n",
-    "        if \"old\" in mode.lower():\n",
-    "            BuildEventSAP_oldv.reBuildEvent(connection, file,\n",
-    "                                        tmin=currentMinT,\n",
-    "                                        tmax=currentMaxT,\n",
-    "                                        pool = animalPool,\n",
-    "                                        animalType = AnimalType.MOUSE)\n",
-    "        elif \"new\" in mode.lower():\n",
-    "            BuildEventSAP.reBuildEvent(connection, file,\n",
-    "                                        tmin=currentMinT,\n",
-    "                                        tmax=currentMaxT,\n",
-    "                                        pool = animalPool,\n",
-    "                                        animalType = AnimalType.MOUSE)\n",
-    "        elif \"parallel\" in mode.lower():\n",
-    "            BuildEventSAP_parallel.reBuildEvent(connection, file,\n",
-    "                                        tmin=currentMinT,\n",
-    "                                        tmax=currentMaxT,\n",
-    "                                        pool = animalPool,\n",
-    "                                        animalType = AnimalType.MOUSE)\n",
-    "        else:\n",
-    "            raise NotImplementedError(\"This mode does not exist. Use 'old', 'new' or 'parallel'\")\n",
-    "        chrono.printTimeInS()\n",
+    "    chrono = Chronometer(str(BuildEventSAP))\n",
+    "    if \"old\" in mode.lower():\n",
+    "        BuildEventSAP_oldv.reBuildEvent(connection, file,\n",
+    "                                    tmin=currentMinT,\n",
+    "                                    tmax=currentMaxT,\n",
+    "                                    pool = animalPool,\n",
+    "                                    animalType = AnimalType.MOUSE)\n",
+    "    elif \"new\" in mode.lower():\n",
+    "        BuildEventSAP.reBuildEvent(connection, file,\n",
+    "                                    tmin=currentMinT,\n",
+    "                                    tmax=currentMaxT,\n",
+    "                                    pool = animalPool,\n",
+    "                                    animalType = AnimalType.MOUSE)\n",
+    "    elif \"parallel\" in mode.lower():\n",
+    "        BuildEventSAP_parallel.reBuildEvent(connection, file,\n",
+    "                                    tmin=currentMinT,\n",
+    "                                    tmax=currentMaxT,\n",
+    "                                    pool = animalPool,\n",
+    "                                    animalType = AnimalType.MOUSE)\n",
+    "    else:\n",
+    "        raise NotImplementedError(\"This mode does not exist. Use 'old', 'new' or 'parallel'\")\n",
+    "    chrono.printTimeInS()\n",
     "\n",
     "def process(file, mode=\"new\"):\n",
     "\n",
@@ -149,9 +140,6 @@
     "    except:\n",
     "        print(\"METADATA field already exists\" , file)\n",
     "\n",
-    "    # Build Index\n",
-    "    BuildDataBaseIndex.buildDataBaseIndex(connection, force=False)\n",
-    "\n",
     "    currentT = minT\n",
     "\n",
     "    try:\n",
@@ -183,6 +171,9 @@
     "\n",
     "        raise FileProcessException()\n",
     "\n",
+    "    connection.close()\n",
+    "\n",
+    "\n",
     "print(\"Code launched.\")\n",
     "\n",
     "mem = virtual_memory()\n",
diff --git a/EMT/EMT_notebooks/Rebuild_WallJump_events.ipynb b/EMT/EMT_notebooks/Rebuild_WallJump_events.ipynb
index f7482ea..7bd3db4 100644
--- a/EMT/EMT_notebooks/Rebuild_WallJump_events.ipynb
+++ b/EMT/EMT_notebooks/Rebuild_WallJump_events.ipynb
@@ -39,9 +39,7 @@
     "minT = 0\n",
     "maxT = 1*oneDay\n",
     "''' time window to compute the events (in frame). '''\n",
-    "windowT = 1*oneDay\n",
-    "''' speed up process '''\n",
-    "USE_CACHE_LOAD_DETECTION_CACHE = True"
+    "windowT = 1*oneDay\n"
    ]
   },
   {
@@ -70,8 +68,7 @@
     "from lmtanalysis.TaskLogger import TaskLogger\n",
     "from lmtanalysis.Animal import *\n",
     "from lmtanalysis.Event import *\n",
-    "from lmtanalysis.Measure import *\n",
-    "from lmtanalysis import BuildEventWallJump, BuildEventWallJump_oldv, BuildEventWallJump_parallel, BuildDataBaseIndex\n",
+    "from lmtanalysis import BuildEventWallJump, BuildEventWallJump_oldv, BuildEventWallJump_parallel\n",
     "from lmtanalysis.FileUtil import getFilesToProcess\n",
     "from lmtanalysis.EventTimeLineCache import flushEventTimeLineCache, disableEventTimeLineCache\n",
     "from lmtanalysis.AnimalType import AnimalType\n",
@@ -95,35 +92,28 @@
     "    animalPool = None\n",
     "    flushEventTimeLineCache()\n",
     "\n",
-    "    if (USE_CACHE_LOAD_DETECTION_CACHE):\n",
-    "        print(\"Caching load of animal detection...\")\n",
-    "        animalPool = AnimalPool()\n",
-    "        animalPool.loadAnimals(connection)\n",
-    "        animalPool.loadDetection(start = currentMinT, end = currentMaxT)\n",
-    "        print(\"Caching load of animal detection done.\")\n",
-    "\n",
-    "        chrono = Chronometer(str(BuildEventWallJump))\n",
-    "        if \"old\" in mode.lower():\n",
-    "            BuildEventWallJump_oldv.reBuildEvent(connection, file,\n",
-    "                                        tmin=currentMinT,\n",
-    "                                        tmax=currentMaxT,\n",
-    "                                        pool = animalPool,\n",
-    "                                        animalType = AnimalType.MOUSE)\n",
-    "        elif \"new\" in mode.lower():\n",
-    "            BuildEventWallJump.reBuildEvent(connection, file,\n",
-    "                                        tmin=currentMinT,\n",
-    "                                        tmax=currentMaxT,\n",
-    "                                        pool = animalPool,\n",
-    "                                        animalType = AnimalType.MOUSE)\n",
-    "        elif \"parallel\" in mode.lower():\n",
-    "            BuildEventWallJump_parallel.reBuildEvent(connection, file,\n",
-    "                                        tmin=currentMinT,\n",
-    "                                        tmax=currentMaxT,\n",
-    "                                        pool = animalPool,\n",
-    "                                        animalType = AnimalType.MOUSE)\n",
-    "        else:\n",
-    "            raise NotImplementedError(\"This mode does not exist. Use 'old', 'new' or 'parallel'\")\n",
-    "        chrono.printTimeInS()\n",
+    "    chrono = Chronometer(str(BuildEventWallJump))\n",
+    "    if \"old\" in mode.lower():\n",
+    "        BuildEventWallJump_oldv.reBuildEvent(connection, file,\n",
+    "                                    tmin=currentMinT,\n",
+    "                                    tmax=currentMaxT,\n",
+    "                                    pool = animalPool,\n",
+    "                                    animalType = AnimalType.MOUSE)\n",
+    "    elif \"new\" in mode.lower():\n",
+    "        BuildEventWallJump.reBuildEvent(connection, file,\n",
+    "                                    tmin=currentMinT,\n",
+    "                                    tmax=currentMaxT,\n",
+    "                                    pool = animalPool,\n",
+    "                                    animalType = AnimalType.MOUSE)\n",
+    "    elif \"parallel\" in mode.lower():\n",
+    "        BuildEventWallJump_parallel.reBuildEvent(connection, file,\n",
+    "                                    tmin=currentMinT,\n",
+    "                                    tmax=currentMaxT,\n",
+    "                                    pool = animalPool,\n",
+    "                                    animalType = AnimalType.MOUSE)\n",
+    "    else:\n",
+    "        raise NotImplementedError(\"This mode does not exist. Use 'old', 'new' or 'parallel'\")\n",
+    "    chrono.printTimeInS()\n",
     "\n",
     "def process(file, mode=\"new\"):\n",
     "\n",
@@ -150,9 +140,6 @@
     "    except:\n",
     "        print(\"METADATA field already exists\" , file)\n",
     "\n",
-    "    # Build Index\n",
-    "    BuildDataBaseIndex.buildDataBaseIndex(connection, force=False)\n",
-    "\n",
     "    currentT = minT\n",
     "\n",
     "    try:\n",
@@ -184,6 +171,9 @@
     "\n",
     "        raise FileProcessException()\n",
     "\n",
+    "    connection.close()\n",
+    "\n",
+    "\n",
     "print(\"Code launched.\")\n",
     "\n",
     "mem = virtual_memory()\n",
diff --git a/EMT/EMT_notebooks/Rebuild_WaterZone_events.ipynb b/EMT/EMT_notebooks/Rebuild_WaterZone_events.ipynb
index fa6c264..1c94e5b 100644
--- a/EMT/EMT_notebooks/Rebuild_WaterZone_events.ipynb
+++ b/EMT/EMT_notebooks/Rebuild_WaterZone_events.ipynb
@@ -43,9 +43,7 @@
     "minT = 0\n",
     "maxT = 3*oneDay\n",
     "''' time window to compute the events (in frame). '''\n",
-    "windowT = 1*oneDay\n",
-    "''' speed up process '''\n",
-    "USE_CACHE_LOAD_DETECTION_CACHE = True"
+    "windowT = 1*oneDay\n"
    ]
   },
   {
@@ -80,10 +78,10 @@
     "from lmtanalysis.TaskLogger import TaskLogger\n",
     "from lmtanalysis.Event import *\n",
     "from lmtanalysis.Measure import *\n",
-    "from lmtanalysis.Util import getAllEvents, getNumberOfFrames\n",
-    "from lmtanalysis import BuildEventWaterPoint, BuildDataBaseIndex\n",
+    "from lmtanalysis.Util import getNumberOfFrames\n",
+    "from lmtanalysis import BuildEventWaterPoint\n",
     "from lmtanalysis.FileUtil import getFilesToProcess\n",
-    "from lmtanalysis.EventTimeLineCache import EventTimeLineCached, flushEventTimeLineCache, disableEventTimeLineCache\n",
+    "from lmtanalysis.EventTimeLineCache import flushEventTimeLineCache, disableEventTimeLineCache\n",
     "from lmtanalysis.AnimalType import AnimalType\n",
     "from lmtanalysis.Parameters import getAnimalTypeParameters\n",
     "\n",
@@ -133,16 +131,9 @@
     "    animalPool = None\n",
     "    flushEventTimeLineCache()\n",
     "\n",
-    "    if ( USE_CACHE_LOAD_DETECTION_CACHE ):\n",
-    "        print(\"Caching load of animal detection...\")\n",
-    "        animalPool = AnimalPool()\n",
-    "        animalPool.loadAnimals(connection)\n",
-    "        animalPool.loadDetection(start = currentMinT, end = currentMaxT)\n",
-    "        print(\"Caching load of animal detection done.\")\n",
-    "\n",
-    "        chrono = Chronometer(str(BuildEventWaterPoint))\n",
-    "        BuildEventWaterPoint.reBuildEvent( connection, file, tmin=currentMinT, tmax=currentMaxT, pool = animalPool, animalType = AnimalType.MOUSE, x_wp=wp[0], y_wp=wp[1] )\n",
-    "        chrono.printTimeInS()\n",
+    "    chrono = Chronometer(str(BuildEventWaterPoint))\n",
+    "    BuildEventWaterPoint.reBuildEvent(connection, file, tmin=currentMinT, tmax=currentMaxT, pool = animalPool, animalType = AnimalType.MOUSE, x_wp=wp[0], y_wp=wp[1])\n",
+    "    chrono.printTimeInS()\n",
     "\n",
     "def process( file, wp ):\n",
     "    print(file)\n",
@@ -168,9 +159,6 @@
     "    except:\n",
     "        print( \"METADATA field already exists\" , file )\n",
     "\n",
-    "    # Build Index\n",
-    "    BuildDataBaseIndex.buildDataBaseIndex( connection, force=False )\n",
-    "\n",
     "    currentT = minT\n",
     "\n",
     "    try:\n",
@@ -202,6 +190,8 @@
     "\n",
     "        raise FileProcessException()\n",
     "\n",
+    "    connection.close()\n",
+    "\n",
     "def extractFrames(video_file, count):\n",
     "    cap = cv2.VideoCapture(video_file.absolute().__str__())\n",
     "    frame = None\n",
diff --git a/EMT/EMT_scripts/Rebuild_All_Events_LMT_script.py b/EMT/EMT_scripts/Rebuild_All_Events_LMT_script.py
index 089786c..d8ca77e 100644
--- a/EMT/EMT_scripts/Rebuild_All_Events_LMT_script.py
+++ b/EMT/EMT_scripts/Rebuild_All_Events_LMT_script.py
@@ -16,7 +16,7 @@ from lmtanalysis import BuildEventTrain3, BuildEventTrain4, BuildEventTrain2, Bu
     BuildEventFloorSniffing, BuildEventSocialApproach, BuildEventSocialEscape, BuildEventApproachContact, \
     BuildEventOralOralContact, BuildEventApproachRear, BuildEventGroup2, BuildEventGroup3, BuildEventGroup4, \
     BuildEventOralGenitalContact, BuildEventStop, BuildEventMove, BuildEventGroup3MakeBreak, BuildEventGroup4MakeBreak, \
-    BuildEventSideBySide, BuildEventSideBySideOpposite, BuildDataBaseIndex, \
+    BuildEventSideBySide, BuildEventSideBySideOpposite, \
     BuildEventOralSideSequence, CheckWrongAnimal, BuildEventDetection, BuildEventCenterPeripheryLocation, BuildEventNest4, \
     BuildEventNest3, BuildEventGetAway, BuildEventOtherContact, BuildEventPassiveAnogenitalSniff, BuildEventRearCenterPeriphery
 from lmtanalysis.TaskLogger import TaskLogger
@@ -136,8 +136,6 @@ def process(file, windowT=oneDay, minT=0, maxT=None, use_cache_load_detection_ca
     except:
         print("METADATA field already exists" , file)
 
-    BuildDataBaseIndex.buildDataBaseIndex(connection, force=False)
-
     currentT = minT
 
     try:
@@ -172,7 +170,7 @@ def process(file, windowT=oneDay, minT=0, maxT=None, use_cache_load_detection_ca
 
 
     flushEventTimeLineCache()
-
+    connection.close()
     chronoFullBatch.printTimeInS()
     print("*** ALL JOBS DONE ***")
 
diff --git a/EMT/EMT_scripts/Rebuild_Huddling_events_script.py b/EMT/EMT_scripts/Rebuild_Huddling_events_script.py
index da59cec..c4a6cd4 100644
--- a/EMT/EMT_scripts/Rebuild_Huddling_events_script.py
+++ b/EMT/EMT_scripts/Rebuild_Huddling_events_script.py
@@ -6,24 +6,19 @@
 ###################################################################################################
 import argparse
 import sqlite3
-from pathlib import Path
 from psutil import virtual_memory
-
 import traceback
 
 from lmtanalysis.Animal import *
 from lmtanalysis.Event import *
 from lmtanalysis.Measure import *
 from lmtanalysis.Util import getNumberOfFrames
-from lmtanalysis import BuildEventHuddling, BuildEventHuddling_oldv, BuildEventHuddling_parallel, BuildDataBaseIndex
+from lmtanalysis import BuildEventHuddling, BuildEventHuddling_oldv, BuildEventHuddling_parallel
 from lmtanalysis.TaskLogger import TaskLogger
-from lmtanalysis.EventTimeLineCache import flushEventTimeLineCache,\
-                                           disableEventTimeLineCache
+from lmtanalysis.EventTimeLineCache import flushEventTimeLineCache, disableEventTimeLineCache
 from lmtanalysis.AnimalType import AnimalType
 
 
-USE_CACHE_LOAD_DETECTION_CACHE = True
-
 class FileProcessException(Exception):
     pass
 
@@ -41,35 +36,29 @@ def processTimeWindow(file, connection, currentMinT , currentMaxT, mode="new"):
     animalPool = None
     flushEventTimeLineCache()
 
-    if (USE_CACHE_LOAD_DETECTION_CACHE):
-        print("Caching load of animal detection...")
-        animalPool = AnimalPool()
-        animalPool.loadAnimals(connection)
-        animalPool.loadDetection(start = currentMinT, end = currentMaxT)
-        print("Caching load of animal detection done.")
-
-        chrono = Chronometer(str(BuildEventHuddling))
-        if "old" in mode.lower():
-            BuildEventHuddling_oldv.reBuildEvent(connection, file,
-                                        tmin=currentMinT,
-                                        tmax=currentMaxT,
-                                        pool = animalPool,
-                                        animalType = AnimalType.MOUSE)
-        elif "new" in mode.lower():
-            BuildEventHuddling.reBuildEvent(connection, file,
-                                        tmin=currentMinT,
-                                        tmax=currentMaxT,
-                                        pool = animalPool,
-                                        animalType = AnimalType.MOUSE)
-        elif "parallel" in mode.lower():
-            BuildEventHuddling_parallel.reBuildEvent(connection, file,
-                                        tmin=currentMinT,
-                                        tmax=currentMaxT,
-                                        pool = animalPool,
-                                        animalType = AnimalType.MOUSE)
-        else:
-            raise NotImplementedError("This mode does not exist. Use 'old', 'new' or 'parallel'")
-        chrono.printTimeInS()
+
+    chrono = Chronometer(str(BuildEventHuddling))
+    if "old" in mode.lower():
+        BuildEventHuddling_oldv.reBuildEvent(connection, file,
+                                    tmin=currentMinT,
+                                    tmax=currentMaxT,
+                                    pool = animalPool,
+                                    animalType = AnimalType.MOUSE)
+    elif "new" in mode.lower():
+        BuildEventHuddling.reBuildEvent(connection, file,
+                                    tmin=currentMinT,
+                                    tmax=currentMaxT,
+                                    pool = animalPool,
+                                    animalType = AnimalType.MOUSE)
+    elif "parallel" in mode.lower():
+        BuildEventHuddling_parallel.reBuildEvent(connection, file,
+                                    tmin=currentMinT,
+                                    tmax=currentMaxT,
+                                    pool = animalPool,
+                                    animalType = AnimalType.MOUSE)
+    else:
+        raise NotImplementedError("This mode does not exist. Use 'old', 'new' or 'parallel'")
+    chrono.printTimeInS()
 
 def process(file, mode="new", windowT=oneDay, minT=0, maxT=None):
 
@@ -89,9 +78,6 @@ def process(file, mode="new", windowT=oneDay, minT=0, maxT=None):
     except:
         print("METADATA field already exists" , file)
 
-    # Build Index
-    BuildDataBaseIndex.buildDataBaseIndex(connection, force=False)
-
     currentT = minT
 
     try:
@@ -124,6 +110,10 @@ def process(file, mode="new", windowT=oneDay, minT=0, maxT=None):
 
         raise FileProcessException()
 
+    flushEventTimeLineCache()
+    connection.close()
+    print("*** ALL JOBS DONE ***")
+
 
 if __name__ == "__main__":
 
@@ -160,6 +150,4 @@ if __name__ == "__main__":
     except FileProcessException:
         print ("STOP PROCESSING FILE ", file , file=sys.stderr)
 
-    flushEventTimeLineCache()
-    chronoFullBatch.printTimeInS()
-    print("*** ALL JOBS DONE ***")
\ No newline at end of file
+    chronoFullBatch.printTimeInS()
\ No newline at end of file
diff --git a/EMT/EMT_scripts/Rebuild_SAP_events_script.py b/EMT/EMT_scripts/Rebuild_SAP_events_script.py
index acedfca..3fb5b73 100644
--- a/EMT/EMT_scripts/Rebuild_SAP_events_script.py
+++ b/EMT/EMT_scripts/Rebuild_SAP_events_script.py
@@ -6,19 +6,16 @@
 ###################################################################################################
 import argparse
 import sqlite3
-from pathlib import Path
 from psutil import virtual_memory
-
 import traceback
 
 from lmtanalysis.Animal import *
 from lmtanalysis.Event import *
 from lmtanalysis.Measure import *
 from lmtanalysis.Util import getNumberOfFrames
-from lmtanalysis import BuildEventSAP, BuildEventSAP_oldv, BuildEventSAP_parallel, BuildDataBaseIndex
+from lmtanalysis import BuildEventSAP, BuildEventSAP_oldv, BuildEventSAP_parallel
 from lmtanalysis.TaskLogger import TaskLogger
-from lmtanalysis.EventTimeLineCache import flushEventTimeLineCache,\
-                                           disableEventTimeLineCache
+from lmtanalysis.EventTimeLineCache import flushEventTimeLineCache, disableEventTimeLineCache
 from lmtanalysis.AnimalType import AnimalType
 
 
@@ -41,35 +38,28 @@ def processTimeWindow(file, connection, currentMinT , currentMaxT, mode="new"):
     animalPool = None
     flushEventTimeLineCache()
 
-    if (USE_CACHE_LOAD_DETECTION_CACHE):
-        print("Caching load of animal detection...")
-        animalPool = AnimalPool()
-        animalPool.loadAnimals(connection)
-        animalPool.loadDetection(start = currentMinT, end = currentMaxT)
-        print("Caching load of animal detection done.")
-
-        chrono = Chronometer(str(BuildEventSAP))
-        if "old" in mode.lower():
-            BuildEventSAP_oldv.reBuildEvent(connection, file,
-                                        tmin=currentMinT,
-                                        tmax=currentMaxT,
-                                        pool = animalPool,
-                                        animalType = AnimalType.MOUSE)
-        elif "new" in mode.lower():
-            BuildEventSAP.reBuildEvent(connection, file,
-                                        tmin=currentMinT,
-                                        tmax=currentMaxT,
-                                        pool = animalPool,
-                                        animalType = AnimalType.MOUSE)
-        elif "parallel" in mode.lower():
-            BuildEventSAP_parallel.reBuildEvent(connection, file,
-                                        tmin=currentMinT,
-                                        tmax=currentMaxT,
-                                        pool = animalPool,
-                                        animalType = AnimalType.MOUSE)
-        else:
-            raise NotImplementedError("This mode does not exist. Use 'old', 'new' or 'parallel'")
-        chrono.printTimeInS()
+    chrono = Chronometer(str(BuildEventSAP))
+    if "old" in mode.lower():
+        BuildEventSAP_oldv.reBuildEvent(connection, file,
+                                    tmin=currentMinT,
+                                    tmax=currentMaxT,
+                                    pool = animalPool,
+                                    animalType = AnimalType.MOUSE)
+    elif "new" in mode.lower():
+        BuildEventSAP.reBuildEvent(connection, file,
+                                    tmin=currentMinT,
+                                    tmax=currentMaxT,
+                                    pool = animalPool,
+                                    animalType = AnimalType.MOUSE)
+    elif "parallel" in mode.lower():
+        BuildEventSAP_parallel.reBuildEvent(connection, file,
+                                    tmin=currentMinT,
+                                    tmax=currentMaxT,
+                                    pool = animalPool,
+                                    animalType = AnimalType.MOUSE)
+    else:
+        raise NotImplementedError("This mode does not exist. Use 'old', 'new' or 'parallel'")
+    chrono.printTimeInS()
 
 def process(file, mode="new", windowT=oneDay, minT=0, maxT=None):
 
@@ -89,9 +79,6 @@ def process(file, mode="new", windowT=oneDay, minT=0, maxT=None):
     except:
         print("METADATA field already exists" , file)
 
-    # Build Index
-    BuildDataBaseIndex.buildDataBaseIndex(connection, force=False)
-
     currentT = minT
 
     try:
@@ -124,6 +111,10 @@ def process(file, mode="new", windowT=oneDay, minT=0, maxT=None):
 
         raise FileProcessException()
 
+    flushEventTimeLineCache()
+    connection.close()
+    print("*** ALL JOBS DONE ***")
+
 
 if __name__ == "__main__":
 
@@ -160,6 +151,4 @@ if __name__ == "__main__":
     except FileProcessException:
         print ("STOP PROCESSING FILE ", file , file=sys.stderr)
 
-    flushEventTimeLineCache()
     chronoFullBatch.printTimeInS()
-    print("*** ALL JOBS DONE ***")
\ No newline at end of file
diff --git a/EMT/EMT_scripts/Rebuild_WallJump_events_script.py b/EMT/EMT_scripts/Rebuild_WallJump_events_script.py
index e3b13be..b3ab57d 100644
--- a/EMT/EMT_scripts/Rebuild_WallJump_events_script.py
+++ b/EMT/EMT_scripts/Rebuild_WallJump_events_script.py
@@ -6,19 +6,16 @@
 ###################################################################################################
 import argparse
 import sqlite3
-from pathlib import Path
 from psutil import virtual_memory
-
 import traceback
 
 from lmtanalysis.Animal import *
 from lmtanalysis.Event import *
 from lmtanalysis.Measure import *
 from lmtanalysis.Util import getNumberOfFrames
-from lmtanalysis import BuildEventWallJump, BuildEventWallJump_oldv, BuildEventWallJump_parallel, BuildDataBaseIndex
+from lmtanalysis import BuildEventWallJump, BuildEventWallJump_oldv, BuildEventWallJump_parallel
 from lmtanalysis.TaskLogger import TaskLogger
-from lmtanalysis.EventTimeLineCache import flushEventTimeLineCache,\
-                                           disableEventTimeLineCache
+from lmtanalysis.EventTimeLineCache import flushEventTimeLineCache, disableEventTimeLineCache
 from lmtanalysis.AnimalType import AnimalType
 
 
@@ -41,35 +38,28 @@ def processTimeWindow(file, connection, currentMinT , currentMaxT, mode="new"):
     animalPool = None
     flushEventTimeLineCache()
 
-    if (USE_CACHE_LOAD_DETECTION_CACHE):
-        print("Caching load of animal detection...")
-        animalPool = AnimalPool()
-        animalPool.loadAnimals(connection)
-        animalPool.loadDetection(start = currentMinT, end = currentMaxT)
-        print("Caching load of animal detection done.")
-
-        chrono = Chronometer(str(BuildEventWallJump))
-        if "old" in mode.lower():
-            BuildEventWallJump_oldv.reBuildEvent(connection, file,
-                                        tmin=currentMinT,
-                                        tmax=currentMaxT,
-                                        pool = animalPool,
-                                        animalType = AnimalType.MOUSE)
-        elif "new" in mode.lower():
-            BuildEventWallJump.reBuildEvent(connection, file,
-                                        tmin=currentMinT,
-                                        tmax=currentMaxT,
-                                        pool = animalPool,
-                                        animalType = AnimalType.MOUSE)
-        elif "parallel" in mode.lower():
-            BuildEventWallJump_parallel.reBuildEvent(connection, file,
-                                        tmin=currentMinT,
-                                        tmax=currentMaxT,
-                                        pool = animalPool,
-                                        animalType = AnimalType.MOUSE)
-        else:
-            raise NotImplementedError("This mode does not exist. Use 'old', 'new' or 'parallel'")
-        chrono.printTimeInS()
+    chrono = Chronometer(str(BuildEventWallJump))
+    if "old" in mode.lower():
+        BuildEventWallJump_oldv.reBuildEvent(connection, file,
+                                    tmin=currentMinT,
+                                    tmax=currentMaxT,
+                                    pool = animalPool,
+                                    animalType = AnimalType.MOUSE)
+    elif "new" in mode.lower():
+        BuildEventWallJump.reBuildEvent(connection, file,
+                                    tmin=currentMinT,
+                                    tmax=currentMaxT,
+                                    pool = animalPool,
+                                    animalType = AnimalType.MOUSE)
+    elif "parallel" in mode.lower():
+        BuildEventWallJump_parallel.reBuildEvent(connection, file,
+                                    tmin=currentMinT,
+                                    tmax=currentMaxT,
+                                    pool = animalPool,
+                                    animalType = AnimalType.MOUSE)
+    else:
+        raise NotImplementedError("This mode does not exist. Use 'old', 'new' or 'parallel'")
+    chrono.printTimeInS()
 
 def process(file, mode="new", windowT=oneDay, minT=0, maxT=None):
 
@@ -89,9 +79,6 @@ def process(file, mode="new", windowT=oneDay, minT=0, maxT=None):
     except:
         print("METADATA field already exists" , file)
 
-    # Build Index
-    BuildDataBaseIndex.buildDataBaseIndex(connection, force=False)
-
     currentT = minT
 
     try:
@@ -124,6 +111,9 @@ def process(file, mode="new", windowT=oneDay, minT=0, maxT=None):
 
         raise FileProcessException()
 
+    flushEventTimeLineCache()
+    connection.close()
+    print("*** ALL JOBS DONE ***")
 
 if __name__ == "__main__":
 
@@ -160,6 +150,4 @@ if __name__ == "__main__":
     except FileProcessException:
         print ("STOP PROCESSING FILE ", file , file=sys.stderr)
 
-    flushEventTimeLineCache()
-    chronoFullBatch.printTimeInS()
-    print("*** ALL JOBS DONE ***")
\ No newline at end of file
+    chronoFullBatch.printTimeInS()
\ No newline at end of file
diff --git a/EMT/lmtanalysis/Animal.py b/EMT/lmtanalysis/Animal.py
index 888d145..6143da8 100644
--- a/EMT/lmtanalysis/Animal.py
+++ b/EMT/lmtanalysis/Animal.py
@@ -3,37 +3,28 @@ Created on 7 sept. 2017
 @author: Fab
 '''
 
-from lmtanalysis.Detection import *
 
 #matplotlib fix for mac (uncomment if needed)
 #matplotlib.use('TkAgg' )
 
-
-import matplotlib.pyplot as plt
-from lmtanalysis.Chronometer import *
 import matplotlib as mpl
+import matplotlib.pyplot as plt
+import matplotlib.patches as mpatches
+import matplotlib.ticker as ticker
 import numpy as np
 import pandas as pd
 from collections import defaultdict
-from mpl_toolkits.mplot3d import *
-import matplotlib.ticker
 import math
-import time
-from lmtanalysis.Measure import *
+import sqlite3
+from time import sleep
 
-from statistics import *
-#from scipy.spatial import distance
-#from scipy.ndimage.measurements import standard_deviation
-from statistics import mean
+from lmtanalysis.Measure import *
+from lmtanalysis.Detection import *
+from lmtanalysis.Chronometer import *
 from lmtanalysis.Event import EventTimeLine
 from lmtanalysis.Point import Point
 from lmtanalysis.Mask import Mask
-from lmtanalysis.Util import *
-import matplotlib.patches as mpatches
-from lxml import etree
-import matplotlib.ticker as ticker
-#from lmtanalysis.Util import convert_to_d_h_m_s, getDatetimeFromFrame, mute_prints
-from pickle import NONE
+from lmtanalysis.Util import getAllEvents, getDatetimeFromFrame, mute_prints, convert_to_d_h_m_s
 from lmtanalysis.AnimalType import AnimalType
 from lmtanalysis.ParametersMouse import ParametersMouse
 from lmtanalysis.ParametersRat import ParametersRat
@@ -41,9 +32,6 @@ from lmtanalysis.ParametersRat import ParametersRat
 
 idAnimalColor = [ None, "red","green","blue","orange"]
 
-from enum import Enum
-
-
 
 def getAnimalColor( animalId ):
     return idAnimalColor[ animalId % 5 ]
@@ -124,7 +112,6 @@ class Animal():
 
         if self.verbose:
             print( query )
-            print( self.conn )
         cursor.execute( query )
 
         rows = cursor.fetchall()
@@ -1047,7 +1034,16 @@ class Animal():
         query = "SELECT DATA FROM DETECTION WHERE ANIMALID={} AND FRAMENUMBER={}".format( self.baseId , t )
 
         cursor = self.conn.cursor()
-        cursor.execute( query )
+
+        retry = True
+        while retry:
+            try:
+                cursor.execute( query )
+            except sqlite3.OperationalError:
+                sleep(0.1)
+                continue
+            else:
+                retry = False
 
         rows = cursor.fetchall()
         cursor.close()
diff --git a/EMT/lmtanalysis/BuildEventHuddling.py b/EMT/lmtanalysis/BuildEventHuddling.py
index 1016094..43cf48b 100644
--- a/EMT/lmtanalysis/BuildEventHuddling.py
+++ b/EMT/lmtanalysis/BuildEventHuddling.py
@@ -26,11 +26,10 @@ def flush( connection ):
 def reBuildEvent( connection, file, tmin=None, tmax=None, pool = None , animalType = None, showGraph = False):
 
     ''' use the pool provided or create it'''
-    if ( pool == None ):
-        pool = AnimalPool( )
-        pool.loadAnimals( connection )
-        #pool.loadDetection( start = tmin, end = tmax )
-
+    if (pool == None):
+        pool = AnimalPool()
+        pool.loadAnimals(connection)
+        pool.loadDetection(start = tmin, end = tmax)
 
     for idAnimalA in pool.animalDictionary:
 
diff --git a/EMT/lmtanalysis/BuildEventHuddling_parallel.py b/EMT/lmtanalysis/BuildEventHuddling_parallel.py
index 3697f51..ea6d673 100644
--- a/EMT/lmtanalysis/BuildEventHuddling_parallel.py
+++ b/EMT/lmtanalysis/BuildEventHuddling_parallel.py
@@ -28,6 +28,7 @@ def computeHuddling(sqlite_file, idAnimalA, tmin=None, tmax=None):
     connection = sqlite3.connect(sqlite_file)
     pool = AnimalPool()
     pool.loadAnimals(connection)
+    pool.loadDetection(start = tmin, end = tmax, idAnimal=idAnimalA)
     animal = pool.animalDictionary[idAnimalA]
 
     huddlingTimeLine = EventTimeLine(connection, "Huddling", idAnimalA, minFrame=tmin, maxFrame=tmax, loadEvent=False)
@@ -53,14 +54,15 @@ def computeHuddling(sqlite_file, idAnimalA, tmin=None, tmax=None):
 
     huddlingTimeLine.reBuildWithDictionary(result)
     huddlingTimeLine.endRebuildEventTimeLine(connection)
+    connection.close()
     return result
 
 def reBuildEvent(connection, file, tmin=None, tmax=None, pool = None , animalType = None, showGraph = False):
 
     # ''' use the pool provided or create it'''
-    if ( pool == None ):
-        pool = AnimalPool( )
-        pool.loadAnimals( connection )
+    if (pool == None):
+        pool = AnimalPool()
+        pool.loadAnimals(connection)
 
     processes = []
     for idAnimalA in pool.animalDictionary:
diff --git a/EMT/lmtanalysis/BuildEventSAP.py b/EMT/lmtanalysis/BuildEventSAP.py
index b349867..d895a77 100644
--- a/EMT/lmtanalysis/BuildEventSAP.py
+++ b/EMT/lmtanalysis/BuildEventSAP.py
@@ -89,13 +89,13 @@ def computeMetrics(image, r = 9, debug = False):
 
     return [I1, eccentricity, length]
 
-def reBuildEvent( connection, file, tmin=None, tmax=None, pool = None , animalType = None, showGraph = False ):
+def reBuildEvent(connection, file, tmin=None, tmax=None, pool = None , animalType = None, showGraph = False):
 
     ''' use the pool provided or create it'''
-    if ( pool == None ):
-        pool = AnimalPool( )
-        pool.loadAnimals( connection )
-        pool.loadDetection( start = tmin, end = tmax, lightLoad = True )
+    if (pool == None):
+        pool = AnimalPool()
+        pool.loadAnimals(connection)
+        pool.loadDetection( start = tmin, end = tmax)
 
     '''
     Animal A is stopped (built-in event):
diff --git a/EMT/lmtanalysis/BuildEventSAP_parallel.py b/EMT/lmtanalysis/BuildEventSAP_parallel.py
index d48cd55..b572715 100644
--- a/EMT/lmtanalysis/BuildEventSAP_parallel.py
+++ b/EMT/lmtanalysis/BuildEventSAP_parallel.py
@@ -99,7 +99,7 @@ def computeSAP(sqlite_file, idAnimalA, tmin=None, tmax=None):
     connection = sqlite3.connect(sqlite_file)
     pool = AnimalPool()
     pool.loadAnimals(connection)
-    pool.loadDetection(start = tmin, end = tmax, lightLoad = True, idAnimal=idAnimalA)
+    pool.loadDetection(start = tmin, end = tmax, idAnimal=idAnimalA)
     animal = pool.animalDictionary[idAnimalA]
 
     SAPTimeLine = EventTimeLine(connection, "SAP", animal.baseId, minFrame=tmin, maxFrame=tmax, loadEvent=False)
@@ -140,6 +140,7 @@ def computeSAP(sqlite_file, idAnimalA, tmin=None, tmax=None):
 
     SAPTimeLine.reBuildWithDictionary(result)
     SAPTimeLine.endRebuildEventTimeLine(connection)
+    connection.close()
     return result
 
 def reBuildEvent(connection, file, tmin=None, tmax=None, pool = None , animalType = None, showGraph = False):
diff --git a/EMT/lmtanalysis/BuildEventWallJump.py b/EMT/lmtanalysis/BuildEventWallJump.py
index 4199c14..459a8ff 100644
--- a/EMT/lmtanalysis/BuildEventWallJump.py
+++ b/EMT/lmtanalysis/BuildEventWallJump.py
@@ -80,7 +80,7 @@ def reBuildEvent( connection, file, tmin=None, tmax=None, pool = None, animalTyp
     if (pool == None):
         pool = AnimalPool()
         pool.loadAnimals(connection)
-        # pool.loadDetection(start = tmin, end = tmax, lightLoad = True)
+        pool.loadDetection(start = tmin, end = tmax)
 
     tmin = 0 if tmin is None else tmin
     tmax = getNumberOfFrames(file) if tmax is None else tmax
diff --git a/EMT/lmtanalysis/BuildEventWallJump_parallel.py b/EMT/lmtanalysis/BuildEventWallJump_parallel.py
index 9466c26..0b63463 100644
--- a/EMT/lmtanalysis/BuildEventWallJump_parallel.py
+++ b/EMT/lmtanalysis/BuildEventWallJump_parallel.py
@@ -3,6 +3,7 @@ Created on 6 sept. 2017
 
 @author: Fab
 '''
+import sqlite3
 import numpy as np
 import skimage.morphology as skm
 from multiprocessing import Process
@@ -83,7 +84,7 @@ def computeWallJump(sqlite_file, idAnimalA, tmin=None, tmax=None):
     connection = sqlite3.connect(sqlite_file)
     pool = AnimalPool()
     pool.loadAnimals(connection)
-    pool.loadDetection(start = tmin, end = tmax, lightLoad = True, idAnimal=idAnimalA)
+    pool.loadDetection(start = tmin, end = tmax, idAnimal=idAnimalA)
     animal = pool.animalDictionary[idAnimalA]
     JumpWallTimeLine = EventTimeLine(connection, "WallJump", animal.baseId, None, None, None, loadEvent=False)
     result={}
@@ -122,12 +123,16 @@ def computeWallJump(sqlite_file, idAnimalA, tmin=None, tmax=None):
 
     JumpWallTimeLine.reBuildWithDictionary(result)
     JumpWallTimeLine.endRebuildEventTimeLine(connection)
+    connection.close()
+    return result
 
 
 def reBuildEvent( connection, file, tmin=None, tmax=None, pool = None, animalType = None, showGraph = False ):
 
-    pool = AnimalPool()
-    pool.loadAnimals(connection)
+    ''' use the pool provided or create it'''
+    if (pool == None):
+        pool = AnimalPool()
+        pool.loadAnimals(connection)
 
     eventName = "WallJump"
     print (eventName)
diff --git a/EMT/lmtanalysis/BuildEventWaterPoint.py b/EMT/lmtanalysis/BuildEventWaterPoint.py
index 3f83f01..b9bcefa 100644
--- a/EMT/lmtanalysis/BuildEventWaterPoint.py
+++ b/EMT/lmtanalysis/BuildEventWaterPoint.py
@@ -30,10 +30,10 @@ def reBuildEvent(connection, file, tmin=None, tmax=None, pool = None, animalType
     parameters = getAnimalTypeParameters( animalType )
 
     ''' use the pool provided or create it'''
-    if ( pool == None ):
-        pool = AnimalPool( )
-        pool.loadAnimals( connection )
-        pool.loadDetection( start = tmin, end = tmax, lightLoad=True )
+    if (pool == None):
+        pool = AnimalPool()
+        pool.loadAnimals(connection)
+        pool.loadDetection(start = tmin, end = tmax)
     '''
     Event Water Zone
     - the animal is in the zone around the water source
diff --git a/EMT/lmtanalysis/Event.py b/EMT/lmtanalysis/Event.py
index c2cd08d..3b22e23 100644
--- a/EMT/lmtanalysis/Event.py
+++ b/EMT/lmtanalysis/Event.py
@@ -3,22 +3,20 @@ Created on 6 sept. 2017
 
 @author: Fab
 '''
-import sqlite3
 import unittest
-from time import *
-from lmtanalysis.Chronometer import Chronometer
 
-import matplotlib
 #matplotlib fix for mac
 #matplotlib.use('TkAgg')
 
 import matplotlib.pyplot as plt
 import numpy as np
-from lmtanalysis.Measure import *
 import sys
 import json
 from numpy import NaN
 
+from lmtanalysis.Chronometer import Chronometer
+from lmtanalysis.Measure import *
+
 class Event:
     '''
     an event represent the interval of frame where the event is
diff --git a/EMT/lmtanalysis/Util.py b/EMT/lmtanalysis/Util.py
index 570ddc6..feeec5e 100644
--- a/EMT/lmtanalysis/Util.py
+++ b/EMT/lmtanalysis/Util.py
@@ -10,7 +10,7 @@ import time
 import sqlite3
 import datetime
 import contextlib
-from random import randrange, random
+from random import random
 import string
 
 
diff --git a/EMT/setup.py b/EMT/setup.py
index afed539..f8af4fc 100644
--- a/EMT/setup.py
+++ b/EMT/setup.py
@@ -3,7 +3,7 @@ from setuptools import setup
 
 setup(
     name='lmtanalysis',
-    version='1.2.0',
+    version='1.2.1',
     description='A new LMT package under development',
     author='Raul Silva',
     author_email='raul-alfredo.de-sousa-silva@lis-lab.fr',
-- 
GitLab