from __future__ import division
import logging
import functools
from collections import defaultdict

import numpy as np

from interval_unionizer import IntervalUnionizer
from cav_unionize import CavUnionize


class CavUnionizer(IntervalUnionizer):


    @classmethod
    def record_cb(cls):
        return CavUnionize()

  
    @classmethod
    def propagate_record(cls, child_record, ancestor_record, copy_all=False):
        return child_record.propagate(ancestor_record)


    def extract_data(self, data_arrays, low, high):
        '''As parent
        '''
    
        unionize = self.__class__.record_cb()
        unionize.calculate(low, high, data_arrays)
        
        return unionize 


    def postprocess_unionizes(self, raw_unionizes, image_series_id, volume_scale):

        unionizes = []
        
        logging.info('getting formatted unionize output')
        for sid, un in raw_unionizes.iteritems():
            
            if sid < 0:
                hemisphere = 'left'
            else:
                hemisphere = 'right'
            sid = abs(sid)
                
            out = un.output(volume_scale)
            out['structure_id'] = sid
            out['hemisphere'] = hemisphere
            out['image_series_id'] = image_series_id

            unionizes.append(out)

        return unionizes
