Beispiel #1
0
    def _dealWithRates(self, combinedRates, femaleRates, maleRates, **kwargs):
        """ blah """

        # Save code version info with instance.  Here because I think
        # it is screwing up based on # some weird scoping thing in MF
        self.LcObjectINFO = LcObjectINFO
        #raise(self.LcObjectINFO)

        # Combined Rates
        self.rates_textComb = combinedRates
        self.nmxComb = LcUtil.parseRates(self.rates_textComb)
        infs_or_nans = N.isinf(self.nmxComb) | N.isnan(self.nmxComb)
        assert not infs_or_nans.any(), \
            AssertionError("why are there infs or nans in combined_nmx???:\n %r" \
               % N.round_(self.nmxComb, 2))
        # F Rates
        self.rates_textFem = femaleRates
        self.nmxFem = LcUtil.parseRates(self.rates_textFem)
        infs_or_nans = N.isinf(self.nmxFem) | N.isnan(self.nmxFem)
        assert not infs_or_nans.any(), \
            AssertionError("why are there infs or nans in female_nmx???:\n %r" \
               % N.round_(self.nmxFem, 2))
        # M Rates
        self.rates_textMale = maleRates
        self.nmxMale = LcUtil.parseRates(self.rates_textMale)
        infs_or_nans = N.isinf(self.nmxMale) | N.isnan(self.nmxMale)
        assert not infs_or_nans.any(), \
            AssertionError("why are there infs or nans in nmxMale???:\n %r" \
               % N.round_(self.nmxMale, 2))
Beispiel #2
0
	def _dealWithRates(self, combinedRates, femaleRates, maleRates, **kwargs):
		""" blah """

		# Save code version info with instance.  Here because I think
		# it is screwing up based on # some weird scoping thing in MF
		self.LcObjectINFO = LcObjectINFO
		#raise(self.LcObjectINFO)

		# Combined Rates
		self.rates_textComb = combinedRates
		self.nmxComb = LcUtil.parseRates(self.rates_textComb)
		infs_or_nans = N.isinf(self.nmxComb) | N.isnan(self.nmxComb)
		assert not infs_or_nans.any(), \
			   AssertionError("why are there infs or nans in combined_nmx???:\n %r" \
							  % N.round_(self.nmxComb, 2)) 
		# F Rates
		self.rates_textFem = femaleRates
		self.nmxFem = LcUtil.parseRates(self.rates_textFem)
		infs_or_nans = N.isinf(self.nmxFem) | N.isnan(self.nmxFem)
		assert not infs_or_nans.any(), \
			   AssertionError("why are there infs or nans in female_nmx???:\n %r" \
							  % N.round_(self.nmxFem, 2)) 
		# M Rates
		self.rates_textMale = maleRates
		self.nmxMale = LcUtil.parseRates(self.rates_textMale)
		infs_or_nans = N.isinf(self.nmxMale) | N.isnan(self.nmxMale)
		assert not infs_or_nans.any(), \
			   AssertionError("why are there infs or nans in nmxMale???:\n %r" \
							  % N.round_(self.nmxMale, 2)) 
Beispiel #3
0
	def _dealWithRates(self, rates, **kwargs):
		"""Parses the rates.  Sets up datastructures for missing data."""
		# Save code version info with instance.  Here because I think
		# it is screwing up based on # some weird scoping thing in MF
		self.LcObjectINFO = LcObjectINFO
		
		assert type(rates) == types.StringType, \
			   AssertionError("Rates should be as string for input.  Instead: %s." % type(rates))

		# Get rates from text, extend ...
		self.rates_text = rates
		self.nmx = LcUtil.parseRates(self.rates_text) 

		# ... handle missing data: if there are nans after parsing,
		# determine which years (rows) they are in ...
		self.nmxNans = N.isnan(self.nmx)
		self.goodRowsBool = (~self.nmxNans).any(axis=1)
		self.goodRowsNum = N.where(self.goodRowsBool)[0].tolist() # weird numpy.where() return
		self.yearIndices = N.arange(1, self.nmx.shape[0]+1, dtype=N.int0) # XXX 1 indexed for missing data formulas

		# ... extend everything ...
		self.nmxExtended = LcUtil.emptyLikeWithNans(self.nmx)
		tmp = LcExtension.extendMx(self.nmx[self.goodRowsNum,:], ageCutoff=self.ageCutoff)
		assert self.nmxExtended[self.goodRowsNum,:].shape == tmp.shape,  \
			   AssertionError("orig shape: %s.  extended shape: %s" \
							  % (tmp.shape, self.nmxExtended[self.goodRowsNum,:].shape))
		self.nmxExtended[self.goodRowsNum,:] = tmp
		assert (self.nmxExtended[self.goodRowsNum,:] > 0.0).all() and (N.isfinite(self.nmxExtended[self.goodRowsNum,:])).all(), \
			   AssertionError("%s" % self.nmxExtended)
		
		# ... asserts, and hopefully fall of the end (only update object state).
		self.nmxInfs = N.isinf(self.nmx)
		assert not self.nmxInfs.any(), \
			   AssertionError("why are there infs in nmx???:\n %r" % N.round_(self.nmx, 2)) 
		return None 					
Beispiel #4
0
    def _dealWithRates(self, populations, mortRates, labels='', **kwargs):
        """ Store the population and rate data.  Create the combined rates matrix. """

        # Save code version info with instance.  Here because I think
        # it is screwing up based on # some weird scoping thing in MF
        self.LcObjectINFO = LcObjectINFO
        
        # Age cutoff
        self.ageCutoffIndex = LCFIT_AGE_INDICES[self.ageCutoff] + 1

        # Population data
        self.populationText = re.sub('\n', '\n', populations) 
        self.populationText = re.sub('\r', '', self.populationText).strip()
        self.populationText = re.sub('\n+$', '', self.populationText)
        self.populationText = re.sub('^\n+', '', self.populationText)
        if LCFIT_EMPTY_ALL_RE.search(self.populationText):
            self.useWeightedMx = False 
            self.populationTextList = []
            self.populationList = []
        else:
            self.useWeightedMx = True
            self.populationTextList = re.split('\n\n+', self.populationText) 
            self.populationList = [LcUtil.parseRates(pops) for pops in self.populationTextList]

        # Mortality data
        self.mortRatesText = re.sub('\n', '\n', mortRates) 
        self.mortRatesText = re.sub('\r', '', mortRates)
        self.mortRatesText = re.sub('\n+$', '', self.mortRatesText)
        self.mortRatesText = re.sub('^\n+', '', self.mortRatesText)
        if LCFIT_EMPTY_ALL_RE.search(self.mortRatesText):
            raise LcException("empty rates data")       
        self.mortRatesTextList = re.split('\n\n+', self.mortRatesText) # 
        self.mortRatesList = [LcUtil.parseRates(rates) for rates in self.mortRatesTextList]

        # Go over each data matrix, check input:  no weird numbers, same size.
        shapeList = []
        shapeShape = self.mortRatesList[0].shape
        for i, data in enumerate(self.populationList + self.mortRatesList):
            if data.shape != shapeShape:
                raise LcException("Inconsistent rate matrix shapes. First matrix shape: %r, current matrix [%r] shape: %r\nData: %r" % \
                                  (shapeShape, i, data.shape, data[0,:].tolist()))
            shapeList.append(data.shape)
            pass
        if self.useWeightedMx:
            if len(self.populationList) != len(self.mortRatesList):
               raise LcException("Must have pop and mx of same length.  Pop = %i, Mort = %i." % \
                                 (len(self.populationList), len(self.mortRatesList)))

        # CG and takes logs of each of the rates
        self.mortRatesListLog = []
        for i, mxMatrix in enumerate(self.mortRatesList):
            self.mortRatesList[i] = LcExtension.extendMx(mxData=mxMatrix, ageCutoff=LCFIT_DEFAULT_AGE_CUTOFF)
            self.mortRatesListLog.append(N.log(self.mortRatesList[i]))

        # Labels
        if labels == '':
            self.labels = map(str,range(1, len(self.mortRatesListLog)+1))
        else:
            self.labels = re.split('\s+', labels.strip())
            if len(self.labels) < len(self.mortRatesListLog):
                labelExtraNumbers = range(len(self.labels)+1, len(self.mortRatesListLog)+1)
                labelExtra = map(str, labelExtraNumbers)
                self.labels = self.labels + labelExtra
            elif len(self.labels) > len(self.mortRatesListLog):
                self.labels = self.labels[0:(len(self.mortRatesListLog))]

        # Years
        self.yearIndices = N.arange(1, self.mortRatesList[0].shape[0]+1, dtype=N.int0) 
        years_end = self.start_year + self.mortRatesList[-1].shape[0]
        self.years = N.array(range(self.start_year, years_end)) 
        assert len(self.years) >= 1, AssertionError("years: %s" % self.years)
        self.years_fcst = N.array(range(years_end-1, years_end + self.stepsForward)) 

        # Create an average mx matrix.  If populations empty, no
        # weights; if there is data for populations (1) check for
        # reasonableness in size and number and (2) use population as
        # weights and do the averaging.
        if  self.useWeightedMx:
            self.totalWeightedMx = N.zeros_like(self.mortRatesList[0])
            self.totalPop = N.zeros_like(self.populationList[0]) 
            for (mx, pop) in zip(self.mortRatesList, self.populationList):
                self.totalWeightedMx = self.totalWeightedMx + (mx*pop)
                self.totalPop = self.totalPop + pop
                pass
            self.averagedMx = self.totalWeightedMx/self.totalPop
        else:
            temp_mx = N.zeros_like(self.mortRatesList[0])
            for mx in self.mortRatesList:
                temp_mx += mx
            self.averagedMx = temp_mx / len(self.mortRatesList)
        self.averagedMx = LcExtension.extendMx(mxData=self.averagedMx, ageCutoff=LCFIT_DEFAULT_AGE_CUTOFF)
        self.averagedMxLog = N.log(self.averagedMx)

        # Check data for ok-ness in mortality (not population, since
        # that might be non-CG'ed and have nans).
        for i, data in enumerate(self.mortRatesList + self.mortRatesListLog + [self.averagedMxLog, self.averagedMx]):
            assert N.isfinite(data).all(), \
                   AssertionError("Bad data in mx[%r]:\n %r" % (N.round_(data, 2), i))
        # Check for weird (ie > 1.0) numbers
        for i, data in enumerate(self.mortRatesList + [self.averagedMx]):
            if not (data<1.2).all():
                raise LcException("Weird mx: \n%s\n%s" % (data[data<1.2], i))