#! /usr/bin/env python

"""
Copyright 2015-2018 Jacob M. Graving <jgraving@gmail.com>

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
"""

import numpy as np
import imgstore


class StoreFrameReader:

    def __init__(self, path):
        self.store = imgstore.new_for_filename(path)
        self.metadata = self.store.get_frame_metadata()
        self.metaindex = np.array(self.metadata['frame_number'])
        self.shape = self.store.image_shape

    def __len__(self):
        return self.metaindex.shape[0]

    def get_data(self, indexes):
        indexes = self.metaindex[indexes]

        frames = []
        frame_numbers = []
        frame_timestamps = []
        for idx in indexes:
            if idx is self.store.frame_number + 1:
                frame, (frame_number, frame_timestamp) = self.store.get_next_framenumber(exact_only=False)
            else:
                frame, (frame_number, frame_timestamp) = self.store.get_image(idx, exact_only=False)
            frames.append(frame)
            frame_numbers.append(frame_number)
            frame_timestamps.append(frame_timestamp)
        frames = np.stack(frames)
        if frames.ndim == 3:
            frames = frames[..., np.newaxis]
        frame_numbers = np.array(frame_numbers)
        frame_timestamps = np.array(frame_timestamps)

        return frames, frame_numbers, frame_timestamps

    def _check_index(self, key):
        if isinstance(key, slice):
            start = key.start
            stop = key.stop
            if start is None:
                start = 0
            if stop is None or stop >= len(self):
                stop = len(self)
            if stop <= len(self):
                idx = range(start, stop)
            else:
                raise IndexError
        elif isinstance(key, (int, np.integer)):
            if key < len(self):
                idx = [key]
            else:
                raise IndexError
        elif isinstance(key, np.ndarray):
            if np.max(key) < len(self):
                idx = key.tolist()
            else:
                raise IndexError
        elif isinstance(key, list):
            if max(key) < len(self):
                idx = key
            else:
                raise IndexError
        else:
            raise IndexError
        return idx

    def __getitem__(self, key):
        indexes = self._check_index(key)
        return self.get_data(indexes)


class StoreReader:

    def __init__(self, path, batch_size):
        self.store = StoreFrameReader(path)
        self.shape = self.store.shape
        self.batch_size = batch_size
        self.n_frames = len(self.store)
        self.idx = 0

    def __len__(self):
        return int(np.ceil(self.n_frames / float(self.batch_size)))

    def get_batch(self, index=None):
        if index:
            if isinstance(index, (int, np.integer)):
                if index < 0:
                    index += len(self)
                if index < len(self):
                    idx0 = index * self.batch_size
                    idx1 = idx0 + self.batch_size
                    self.idx = index
                else:
                    raise IndexError('index out of range')
            else:
                raise NotImplementedError
        else:
            if self.idx >= len(self) - 1:
                self.idx = 0
            if self.idx < len(self) - 1:
                idx0 = self.idx * self.batch_size
                idx1 = idx0 + self.batch_size
                self.idx += 1
            else:
                raise StopIteration

        return self.store[idx0:idx1]

    def __getitem__(self, index):
        return self.get_batch(index)
@@ -1092,24 +1092,21 @@
    frame
-_numbers
+s_idx
  = %5B%5D%0A  
@@ -1103,38 +1103,32 @@
 dx = %5B%5D%0A        
-frame_
 timestamps = %5B%5D%0A
@@ -1232,38 +1232,29 @@
 ame, (frame_
-number, frame_
+idx, 
 timestamp) =
@@ -1356,22 +1356,13 @@
 ame_
-number, frame_
+idx, 
 time
@@ -1464,24 +1464,21 @@
    frame
-_numbers
+s_idx
 .append(
@@ -1483,22 +1483,19 @@
 d(frame_
-number
+idx
 )%0A      
@@ -1492,38 +1492,32 @@
 dx)%0A            
-frame_
 timestamps.appen
@@ -1518,22 +1518,16 @@
 .append(
-frame_
 timestam
@@ -1650,24 +1650,21 @@
    frame
-_numbers
+s_idx
  = np.ar
@@ -1672,24 +1672,21 @@
 ay(frame
-_numbers
+s_idx
 )%0A      
@@ -1687,22 +1687,16 @@
         
-frame_
 timestam
@@ -1709,22 +1709,16 @@
 p.array(
-frame_
 timestam
@@ -1754,24 +1754,15 @@
 rame
-_numbers, frame_
+s_idx, 
 time

Update StoreReader