from __future__ import division

import numpy as np

from unionize_record import Unionize


class CavUnionize(Unionize):

    __slots__ = ['sum_pixels', 'sum_cav_pixels']

    def __init__(self, *args, **kwargs):
        for key in self.__slots__:
            setattr(self, key, 0)
        
        
    def calculate(self, low, high, data_arrays):
        data_arrays = self.slice_arrays(low, high, data_arrays)

        self.sum_pixels = data_arrays['sum_pixels'].sum()
        self.sum_cav_pixels = np.multiply(data_arrays['sum_pixels'], data_arrays['cav_density']).sum()
        
        
    def propagate(self, ancestor):
        ancestor.sum_pixels += self.sum_pixels
        ancestor.sum_cav_pixels += self.sum_cav_pixels
        return ancestor
        
        
    def output(self, volume_scale):
        return {'structure_volume': self.sum_pixels * volume_scale, 
                'signal_volume': self.sum_cav_pixels * volume_scale, 
                'signal_density': self.sum_cav_pixels / self.sum_pixels if self.sum_pixels > 0 else 0}

