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 					
Exemple #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)) 
Exemple #3
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))
def fitSingleKt(ax, bx, kt, nmx, lifeTableParams):
	'''
	Fit kt such that it generates empirical life expectancy based on
	nMx from input data, constrained by given ax and bx.

	Returns fitted kt.
	'''
	lifeTableParams_copy = copy.copy(lifeTableParams)
	target_e0 = LcUtil.lifeTable(nmx, **lifeTableParams_copy)
	fittedKt = LcUtil.fitX(func=LcUtil.kt2e0, target=target_e0, ax=ax, bx=bx, lifeTableParams=lifeTableParams) 
	return fittedKt
Exemple #5
0
	def __init__(self, hmdinput='XXX', start_year='XXX', notes='XXX', **kwargs):
		"""Get beginning state.  Note that some of this code makes it
		possible to instantiate an empty HMD object, to have its
		__dict__ filled from the database."""

		lcfitlogger.debug( 'HMD start')
		if start_year == '':
			raise LcException, "Must enter a start year for HMD Object"
		elif start_year == 'XXX':
			self.start_year = 0
		else:
			self.start_year = LcUtil.parseDate(start_year)
		
		if notes == '':
			raise LcException, "Must enter notes for HMD Object--country at least"
		elif notes == 'XXX':
			self.notes = notes

		self.datapath =	os.path.join(LCFIT_DATADIR, str(random.randint(0, 10**10)))
		
		self.tableString = ''
		self.imagesDict = {}


		if hmdinput == 'XXX':
			return
		else:
			lcfitlogger.debug( 'HMD Stop')
			self.hmdinput = hmdinput
			(self.femaleRates, self.maleRates, self.totalRates) = hmd2lare(hmdinput)
			return
def lots_e0s(percentileIndices=None, lots_nmx=None, lifeTableParams=None, sortflag='end-result'):
	
	'''
	Calculates and returns a matrix of life exps from a 3-d matrix of
	innovations with nmx(run, time, age), selecting only those
	corresponding to the parameterized indexes corresponding to
	percentiles.
	
	Usage in LC:  self.e0s = lots_e0s(PTILE_INDS, self.nmx, lifeTableParams)
	'''
	if len(lots_nmx.shape) != 3:
		raise Exception("Bad nmx shape: %s" % lots_nmx.shape)
	lots_nmx = lots_nmx.copy()

	# compute all the e0s, sort on ending e0s, then return the appropriate percentiles
	stepsForward = lots_nmx.shape[1]
	numRuns = lots_nmx.shape[0]
	e0s = N.zeros([numRuns, stepsForward], N.float64)
	for i in range(0, e0s.shape[0]):	# Particular run
		for j in range(0, e0s.shape[1]): # For each projection step
			e0s[i,j] = LcUtil.lifeTable(lots_nmx[i,j,:], **lifeTableParams)
			pass
		pass

	if sortflag=='column-wise':
		e0s.sort(0)
	elif sortflag in (True, 'end-result'):
		sorti = N.argsort(e0s[:,-1])	# sort on e0 of end of run/ forecast
		e0s = e0s[sorti,:]
		pass

	if percentileIndices is None:
		return(e0s)
	else:
		return(e0s[percentileIndices,:])
	def _dumpText(self):
		self.dumpString = LcUtil.dumpObject(self,
											helpParagraph=LCFIT_DUMP_HELP,
											dontDump=LCFIT_NOTWANTED_ATTRIBUTE_DUMPS,
											annoStructure=LCFIT_VAR_ANNOTATION_SINGLESEX,
											fieldsep=LCFIT_FIELDSEP,
											rowsep=LCFIT_ROWSEP,
											stanzasep=LCFIT_STANZASEP)
Exemple #8
0
 def _dumpText(self):
     self.dumpString = LcUtil.dumpObject(self,
                                         helpParagraph=LCFIT_DUMP_HELP,
                                         dontDump=LCFIT_NOTWANTED_ATTRIBUTE_DUMPS,
                                         annoStructure=LCFIT_VAR_ANNOTATION_COHERENT,
                                         fieldsep=LCFIT_FIELDSEP,
                                         rowsep=LCFIT_ROWSEP,
                                         stanzasep=LCFIT_STANZASEP)
Exemple #9
0
    def __init__(self,
                 hmdinput='XXX',
                 start_year='XXX',
                 notes='XXX',
                 **kwargs):
        """Get beginning state.  Note that some of this code makes it
		possible to instantiate an empty HMD object, to have its
		__dict__ filled from the database."""

        lcfitlogger.debug('HMD start')
        if start_year == '':
            raise LcException, "Must enter a start year for HMD Object"
        elif start_year == 'XXX':
            self.start_year = 0
        else:
            self.start_year = LcUtil.parseDate(start_year)

        if notes == '':
            raise LcException, "Must enter notes for HMD Object--country at least"
        elif notes == 'XXX':
            self.notes = notes

        self.datapath = os.path.join(LCFIT_DATADIR,
                                     str(random.randint(0, 10**10)))

        self.tableString = ''
        self.imagesDict = {}

        if hmdinput == 'XXX':
            return
        else:
            lcfitlogger.debug('HMD Stop')
            self.hmdinput = hmdinput
            (self.femaleRates, self.maleRates,
             self.totalRates) = hmd2lare(hmdinput)
            return
	def __str__(self):
		"""Return a string that gives the content of the LC object"""
		
		assert type(self.percentiles) == types.ListType, LcException("Percentiles: %s" % self.percentiles)

		ltFuncType = self.lifeTableParams['ltFuncType']

		# Info about the run/software/user/etc ...
		run_info = '<pre>RUN INFORMATION:\t\n'
		run_info += 'Current time:\t %s\n' % datetime.datetime.today().strftime('%Y-%m-%d %H:%M:%S')
		run_info += 'Run time:\t %s\n' % self.timestamp 
		run_info += 'Software Id:\t %s\n' % self.LcObjectINFO
		run_info += 'Object ID:\t %s\n' % self.LcID
		run_info += '\nNotes:\t %s\n' % self.notes

		
		# ...display the scalar values of interetest ...
		run_info += '\n'
		run_info += 'First year of empirical data:\t %s\n' % self.start_year
		year_end = (self.start_year + self.nmx.shape[0] - 1)
		run_info += 'End year of empirical data:\t%s\n' % year_end
		run_info += 'Cutoff age for age nmx extension:\t %s\n' % self.ageCutoff
		run_info += 'Lifetable age regime:\t 0-1, 1-4, 5-9, ..., 105-110, 110+\n'
		run_info += 'Projection confidence interval:\t%s\n' % self.projConfidenceInterval
		run_info +=	'Percentile values for display:\t %s\n' % self.percentiles
		run_info += 'Drift:\t %s\n' % self.drift
		run_info += 'Drift uncertainty:\t %s\n' % self.stdErrorCoeff
		run_info += 'Standard error of innovations:\t %s\n'  % self.stdErrorEq 
		run_info += 'Number of projection runs:\t %s\n' % self.numRuns
		run_info += 'Number of years projected forward:\t %s\n' % self.stepsForward
		run_info += 'Width of projection step:\t %s year(s)\n' % LCFIT_PROJECTION_WIDTH
		run_info += 'Width of projection step:\t %s year(s)\n' % LCFIT_PROJECTION_WIDTH

		# ... close <pre>...
		run_info += '</pre>'

		# ... include a link to the text dump of the object...
		dumpLink = LCFIT_WWW_OBJECT_DUMP + '&LC_OBJECT_ID=' + str(self.LcID)
		run_info += "<p><a href='%s'> OBJECT DUMP </button></a></p>" % (dumpLink,)
 

		# ... close run info.
		run_info += ''

		# Empirical data by year
		yearlyDataList =  [self.years, self.kt_graph, self.kt_unfit_graph, self.e0s_emp_graph,
						   self.e0s_kt_unfit_graph]
		yearlyHeadings = ['Year', 'kt, after second stage', 'kt, from SVD',
						  'e0, from second stage kt',
						  'e0, from empirical nmx' ]
		yearlyResultsTable = LcUtil.tablefy(dataList = yearlyDataList, headings=yearlyHeadings)

		# Empirical data by age
		ageDataList = [LCFIT_AGES[:self.ageCutoffIndex], self.ax, self.bx]
		ageHeadings = ['age', 'ax', 'bx']
		ageResultsTable = LcUtil.tablefy(dataList = ageDataList, headings=ageHeadings)

		# Projected data by year
		projectedYears = range(self.start_year + len(self.kt_graph) - 1,
							   self.start_year + len(self.kt_graph) + self.stepsForward)
		projDataList = [projectedYears, self.lowerE0Projected, self.medianE0Projected,
						self.upperE0Projected, self.e0s_projected[2, :]]
		projHeadings = ['Year (projected)',
						'low analytical e0' ,
						'median analytical e0' ,
						'high analytical e0'  ,
						'median proj e0' ]
		projResultsTable =  LcUtil.tablefy(dataList = projDataList, headings=projHeadings)

		# image summarizing inference
		lc_img_path = LCFIT_WWW_DISPLAY_IMAGE + '&' + LCFIT_OBJECT_ID_KEY + '=' \
					  + str(self.LcID) + '&' + LCFIT_IMAGE_NAME_KEY + '=' + LC_IMAGE_NAME
		lc_image = '<a href=%s><img src="%s" height = %i width = %i alt="PNG of LC Summary %s"></a>\n' \
				   % (lc_img_path, lc_img_path, IMGH, IMGW, self.LcID)

		# image tracing log rates 
		lnmx_img_path = LCFIT_WWW_DISPLAY_IMAGE + '&' + LCFIT_OBJECT_ID_KEY + '=' \
					  + str(self.LcID) + '&' + LCFIT_IMAGE_NAME_KEY + '=' + LNMX_IMAGE_NAME
		lnmx_image = '<a href=%s><img src="%s" height = %i width = %i alt="PNG of selected LNMXes %s"></a>\n' \
				   % (lnmx_img_path, lnmx_img_path, IMGH, IMGW, self.LcID)

		# image summarizing forecast
		fc_img_path = LCFIT_WWW_DISPLAY_IMAGE + '&' + LCFIT_OBJECT_ID_KEY + '=' \
					  + str(self.LcID) + '&' + LCFIT_IMAGE_NAME_KEY + '=' + FC_IMAGE_NAME
		fc_image = '<a href=%s><img src="%s" height = %i width = %i alt="PNG of LC Summary %s"></a>\n' \
				   % (fc_img_path, fc_img_path, IMGH, IMGW, self.LcID)

		# image w/ three mortality profiles
		mortp_img_path = LCFIT_WWW_DISPLAY_IMAGE + '&' + LCFIT_OBJECT_ID_KEY + '=' \
					  + str(self.LcID) + '&' + LCFIT_IMAGE_NAME_KEY + '=' + MORTP_IMAGE_NAME
		mortp_image = '<a href=%s><img src="%s" height = %i width = %i alt="PNG of Mort Profiles %s"></a>\n' \
				   % (mortp_img_path, mortp_img_path, IMGH, IMGW, self.LcID)

		# Build html string (rather rudimentary, sorry)
		HTMLstr = '<table  border="1">\n' + \
				  '<tr><td colspan=3> <h3>LCFIT Run</h3></td>\n' + \
				  '<tr><td colspan=3>' + run_info + '</td></tr>\n' + \
				  '<tr><td colspan=3>' + lc_image + fc_image + lnmx_image + mortp_image + '</td></tr>\n' + \
				  '<tr><td colspan=3>' + yearlyResultsTable + '</td></tr>\n' + \
				  '<tr><td colspan=3>' + ageResultsTable + '</td></tr>\n' + \
				  '<tr><td colspan=3>' + projResultsTable + '</td></tr>\n' + \
				  '<tr><td colspan=3> Please direct questions or comments to: %s </td>\n' % EMAIL + \
				 '</table>\n'
		return HTMLstr
	def _do_lc(self):
		"""
		Do the inference and the simulation, saving the results as
		state in the instance.  Note that here we are doing the LC on
		non-missing values, then reassembling the results to keep the
		missing.
		"""

		# Do LC on the good data, fill self.* with good data only (ie ignore missing data for now).
		(self.ax, self.bx, self.kt, self.kt_unfit, self.U, self.X, self.V, self.lnmxAdjusted, self.lnmx ) = lcInfer(
			nmx=self.nmxExtended[self.goodRowsNum,:],
			lifeTableParams=self.lifeTableParams,
			ageCutoff=self.ageCutoff,
			flattenBx=self.flattenBx) 
		jumpoffAx = N.log(self.nmx[-1,0:self.ageCutoffIndex].ravel()) # Use last year of death rates to calcc
		
		## Derive current year stuff from LC inference results (kt etc),
		## put those results into the good year positions, let the nans in the non-good-years float.
		self.nmxsFromKtCurrent_unfit =  N.zeros_like(self.nmxExtended)
		self.nmxsFromKtCurrent_unfit[...] = N.nan
		self.nmxsFromKtCurrent_unfit[self.goodRowsNum,:] =  project_nmx(kt=self.kt_unfit, bx=self.bx, ax=self.ax, ageCutoff=self.ageCutoff)

		self.nmxsFromKtCurrent = N.zeros_like(self.nmxExtended) # T x X
		self.nmxsFromKtCurrent[...] = N.nan
		self.nmxsFromKtCurrent[self.goodRowsNum,:] = project_nmx(kt=self.kt, bx=self.bx, ax=self.ax, ageCutoff=self.ageCutoff) 

		self.e0sFromKtCurrent = N.zeros(self.nmxExtended.shape[0])
		self.e0sFromKtCurrent[...] = N.nan
		self.e0sFromKtCurrent[self.goodRowsNum] = N.array(
			[LcUtil.lifeTable(nmxp=nmxRow, **self.lifeTableParams) for nmxRow in self.nmxsFromKtCurrent[self.goodRowsNum,:]])
		
		self.e0sFromKtCurrent_unfit = N.zeros(self.nmxExtended.shape[0])
		self.e0sFromKtCurrent_unfit[...] = N.nan
		self.e0sFromKtCurrent_unfit[self.goodRowsNum] = N.array(
			[LcUtil.lifeTable(nmxp=nmxRow, **self.lifeTableParams) for nmxRow in self.nmxsFromKtCurrent_unfit[self.goodRowsNum,:]])

		self.e0sFromEmpiricalNmx = N.zeros(self.nmxExtended.shape[0]) # T x 1
		self.e0sFromEmpiricalNmx[...] = N.nan
		self.e0sFromEmpiricalNmx[self.goodRowsNum]  = N.array(
			[LcUtil.lifeTable(nmxp=nmxRow, **self.lifeTableParams) for nmxRow in self.nmxExtended[self.goodRowsNum,:]])

		######## Forecast ##############
		## Get random walk parameters
		if self.goodRowsBool.all():
			# Data for all years.
			self.diffedKt = S.diff(self.kt)
			self.drift = S.average(self.diffedKt)
			self.stdErrorEq = S.std(self.diffedKt)
			self.stdErrorCoeff = self.stdErrorEq/S.sqrt(len(self.diffedKt))	# XXX should be len(self.kt)?
			pass
		else:
			# Missing data (li, Lee, Tulja2002).  Find drift from the
			# endpoints, find the standard errors from some fancy
			# stuff (using xTmp and yTmp to make the math readable
			# below).
			self.drift = (self.kt[-1] - self.kt[0])/float(len(self.yearIndices))
			xTmp = N.sum(N.square(N.diff(self.kt) - (self.drift * N.diff(self.yearIndices[self.goodRowsNum]))))
			yTmp = N.sum(N.square(N.diff(self.yearIndices[self.goodRowsNum]))) / (self.yearIndices[-1]-self.yearIndices[0])
			assert xTmp > 0.001, "xTmp < 0.001: %s." % xTmp
			assert yTmp < (self.yearIndices[-1] - self.yearIndices[0]), "yTmp < total years: yTmp: %s." % yTmp
			self.stdErrorEqSq = xTmp/(self.yearIndices[-1] - self.yearIndices[0] - yTmp)
			self.stdErrorCoeffSq = self.stdErrorEqSq/(self.yearIndices[-1]-self.yearIndices[0])
			self.stdErrorEq = N.sqrt(self.stdErrorEqSq)
			self.stdErrorCoeff = N.sqrt(self.stdErrorCoeffSq)
			pass
					
		## Simulation with above paramenters  XXX how do we handle starting kt?
		self.kt_simul = sim_kt(SEC=self.stdErrorCoeff, SEE=self.stdErrorEq, drift=self.drift, numRuns=self.numRuns, stepsForward=self.stepsForward, sortflag=False)
		self.nmx_projected = project_nmx(kt=self.kt_simul, bx=self.bx, ax=jumpoffAx, ageCutoff=self.ageCutoff)
		self.nmx_projected_stochastic_median_final = project_nmx(kt=N.median(N.sort(self.kt_simul,0)), bx=self.bx, ax=jumpoffAx, ageCutoff=self.ageCutoff)
		self.e0s_projected = lots_e0s(self.percentileIndices, self.nmx_projected, self.lifeTableParams)

		######### Other Forecasting ######
		## Derive analytic mean and 95% forecast interval of kt
		x = S.arange(0.0, float(self.stepsForward+1))
		self.KtStdError = ((x*(self.stdErrorEq**2.0)) +
						   (x*self.stdErrorCoeff)**2.0)**.5  # kt.stderr <- ( (x*see^2) + (x*sec)^2 )^.5
		tArray = N.array([self.drift] * (self.stepsForward+1))
		tArray[0] = 0.0
		self.meanKtProjected = N.cumsum(tArray)
		self.upperKtProjected = self.meanKtProjected + (self.zscore * self.KtStdError)
		self.lowerKtProjected = self.meanKtProjected - (self.zscore * self.KtStdError)

		## Derive the projected e0s -- note that "upper" and "lower" reversed wrt kt
		self.upperE0Projected = LcUtil.multiKt2e0(self.lowerKtProjected, ax=jumpoffAx, bx=self.bx,
												  lifeTableParams=self.lifeTableParams)
		self.lowerE0Projected = LcUtil.multiKt2e0(self.upperKtProjected, ax=jumpoffAx, bx=self.bx,
												  lifeTableParams=self.lifeTableParams)
		self.medianE0Projected =  LcUtil.multiKt2e0(self.meanKtProjected, ax=jumpoffAx, bx=self.bx,
												  lifeTableParams=self.lifeTableParams)
		self.nmx_projectedMedian = project_nmx(kt=self.meanKtProjected, bx=self.bx, ax=jumpoffAx,
											   ageCutoff=self.ageCutoff)
		self.nmx_projectedMedianFinal = self.nmx_projectedMedian[-1,:]
		return							# Don't return anything useful
Exemple #12
0
	def _do_lc(self):
		"""
		Do the inference and the simulation, saving the results as
		state in the instance.
		"""
		########## LC step #######
		## Basic LC step, combined.  Store all the results in the instance.
		(self.axComb, self.bxComb, self.ktComb, self.kt_unfitComb,
		 self.UComb, self.XComb, self.VComb, self.lnmxAdjustedComb, self.lnmxComb) = lcInfer(
			self.nmxComb, ageCutoff=self.ageCutoff, lifeTableParams=self.lifeTableParams, flattenBx=self.flattenBx) 

		## Basic LC step, combined.  Store all the results in the instance.
		(self.axFem, self.bxFem, self.ktFem, self.kt_unfitFem,
		 self.UFem, self.XFem, self.VFem, self.lnmxAdjustedFem, self.lnmxFem) = lcInfer(
			self.nmxFem, ageCutoff=self.ageCutoff, lifeTableParams=self.lifeTableParams, flattenBx=self.flattenBx) 
		
		## Basic LC step, combined.  Store all the results in the instance.
		(self.axMale, self.bxMale, self.ktMale, self.kt_unfitMale,
		 self.UMale, self.XMale, self.VMale, self.lnmxAdjustedMale, self.lnmxMale) = lcInfer(
			self.nmxMale, ageCutoff=self.ageCutoff, lifeTableParams=self.lifeTableParams, flattenBx=self.flattenBx) 
			
		jumpoffAxFem = N.log(self.nmxFem[-1,0:self.ageCutoffIndex].ravel())
		jumpoffAxMale = N.log(self.nmxMale[-1,0:self.ageCutoffIndex].ravel())

		## Derive current year stuff from LC inference results (kt etc).
		## Note use of list comprehensions.
		self.e0sFromEmpiricalNmxComb = N.array([LcUtil.lifeTable(nmxp=nmxRow, **self.lifeTableParams)
												for nmxRow in self.nmxComb])
		self.e0sFromEmpiricalNmxMale = N.array([LcUtil.lifeTable(nmxp=nmxRow, **self.lifeTableParams)
												for nmxRow in self.nmxMale])
		self.e0sFromEmpiricalNmxFem = N.array([LcUtil.lifeTable(nmxp=nmxRow, **self.lifeTableParams)
												for nmxRow in self.nmxFem])
		self.nmxsFromKtCurrentComb = project_nmx(kt=self.ktComb, bx=self.bxComb, ax=self.axComb,
											 ageCutoff=self.ageCutoff)
		self.nmxsFromKtCurrentMale = project_nmx(kt=self.ktComb, bx=self.bxComb, ax=self.axMale,
											 ageCutoff=self.ageCutoff)
		self.nmxsFromKtCurrentFem = project_nmx(kt=self.ktComb, bx=self.bxComb, ax=self.axFem,
											 ageCutoff=self.ageCutoff)
		self.nmxsFromKtCurrent_unfitComb = project_nmx(kt=self.kt_unfitComb, bx=self.bxComb, ax=self.axComb,
												   ageCutoff=self.ageCutoff) 
		self.nmxsFromKtCurrent_unfitMale = project_nmx(kt=self.kt_unfitComb, bx=self.bxComb, ax=self.axMale,
												   ageCutoff=self.ageCutoff) 
		self.nmxsFromKtCurrent_unfitFem = project_nmx(kt=self.kt_unfitComb, bx=self.bxComb, ax=self.axFem,
												   ageCutoff=self.ageCutoff) 
		self.e0sFromKtCurrentComb = N.array([LcUtil.lifeTable(nmxp=nmxRow, **self.lifeTableParams)
										 for nmxRow in self.nmxsFromKtCurrentComb])
		self.e0sFromKtCurrentMale = N.array([LcUtil.lifeTable(nmxp=nmxRow, **self.lifeTableParams)
										 for nmxRow in self.nmxsFromKtCurrentMale])
		self.e0sFromKtCurrentFem = N.array([LcUtil.lifeTable(nmxp=nmxRow, **self.lifeTableParams)
										 for nmxRow in self.nmxsFromKtCurrentFem])
		self.e0sFromKtCurrent_unfitComb = N.array([LcUtil.lifeTable(nmxp=nmxRow, **self.lifeTableParams)
										 for nmxRow in self.nmxsFromKtCurrent_unfitComb])
		self.e0sFromKtCurrent_unfitMale = N.array([LcUtil.lifeTable(nmxp=nmxRow, **self.lifeTableParams)
										 for nmxRow in self.nmxsFromKtCurrent_unfitMale])
		self.e0sFromKtCurrent_unfitFem = N.array([LcUtil.lifeTable(nmxp=nmxRow, **self.lifeTableParams)
										 for nmxRow in self.nmxsFromKtCurrent_unfitFem])
		assert len(self.e0sFromKtCurrentComb) >= 1, \
			   AssertionError("self.e0sFromKtCurrentComb: %s" % self.e0sFromKtCurrentComb)
		assert type(self.e0sFromKtCurrentComb) == N.ndarray, \
			   AssertionError("Weird type: %s" % type(self.e0sFromKtCurrentComb))

		######## Simulation ##############
		## Get random walk parameters
		self.diffedKt = S.diff(self.ktComb)
		self.drift = S.average(self.diffedKt)
		self.stdErrorEq = S.std(self.diffedKt)
		self.stdErrorCoeff = self.stdErrorEq/S.sqrt(len(self.diffedKt))
		
		## Multi run simulation with above paramenters
		self.kt_simul = sim_kt(SEC=self.stdErrorCoeff, SEE=self.stdErrorEq, drift=self.drift,
							   numRuns=self.numRuns, stepsForward=self.stepsForward, sortflag=False)
		self.nmx_projectedFem = project_nmx(kt=self.kt_simul, bx=self.bxComb, ax=jumpoffAxFem,
										 ageCutoff=self.ageCutoff)
		self.nmx_projectedMale = project_nmx(kt=self.kt_simul, bx=self.bxComb, ax=jumpoffAxMale,
										 ageCutoff=self.ageCutoff)
		self.nmx_projected_stochastic_median_final_F = project_nmx(kt=N.median(N.sort(self.kt_simul,0)),
																   bx=self.bxComb, ax=jumpoffAxFem, ageCutoff=self.ageCutoff)
		self.nmx_projected_stochastic_median_final_M = project_nmx(kt=N.median(N.sort(self.kt_simul,0)),
																   bx=self.bxComb, ax=jumpoffAxMale, ageCutoff=self.ageCutoff)

		self.e0s_projectedFem = lots_e0s(self.percentileIndices, self.nmx_projectedFem, self.lifeTableParams)
		self.e0s_projectedMale = lots_e0s(self.percentileIndices, self.nmx_projectedMale, self.lifeTableParams)
		
		## Derive analytic mean and x% forecast interval of kt
		x = S.arange(0.0, float(self.stepsForward+1))
		self.KtStdError = ((x*(self.stdErrorEq**2.0)) +
						   (x*self.stdErrorCoeff)**2.0)**.5  # kt.stderr <- ( (x*see^2) + (x*sec)^2 )^.5
		tArray = N.array([self.drift] * (self.stepsForward+1))
		tArray[0] = 0.0
		self.meanKtProjected = N.cumsum(tArray)
		self.upperKtProjected = self.meanKtProjected + (self.zscore * self.KtStdError)
		self.lowerKtProjected = self.meanKtProjected - (self.zscore * self.KtStdError)

		self.nmx_projectedMedian_F = project_nmx(kt=self.meanKtProjected, bx=self.bxComb, ax=jumpoffAxFem,
												 ageCutoff=self.ageCutoff)
		self.nmx_projectedMedian_M = project_nmx(kt=self.meanKtProjected, bx=self.bxComb, ax=jumpoffAxMale,
												 ageCutoff=self.ageCutoff)
		self.nmx_projected_median_final_F = self.nmx_projectedMedian_F[-1,:]
		self.nmx_projected_median_final_M = self.nmx_projectedMedian_M[-1,:]

		
		## Derive the projected e0s, F
		self.upperE0ProjectedFem = LcUtil.multiKt2e0(self.upperKtProjected, ax=jumpoffAxFem, bx=self.bxComb,
												  lifeTableParams=self.lifeTableParams)
		self.lowerE0ProjectedFem = LcUtil.multiKt2e0(self.lowerKtProjected, ax=jumpoffAxFem, bx=self.bxComb,
												  lifeTableParams=self.lifeTableParams)
		self.meanE0ProjectedFem =  LcUtil.multiKt2e0(self.meanKtProjected, ax=jumpoffAxFem, bx=self.bxComb,
												  lifeTableParams=self.lifeTableParams)
		## Derive the projected e0s, M
		self.upperE0ProjectedMale = LcUtil.multiKt2e0(self.upperKtProjected, ax=jumpoffAxMale, bx=self.bxComb,
												  lifeTableParams=self.lifeTableParams)
		self.lowerE0ProjectedMale = LcUtil.multiKt2e0(self.lowerKtProjected, ax=jumpoffAxMale, bx=self.bxComb,
												  lifeTableParams=self.lifeTableParams)
		self.meanE0ProjectedMale =  LcUtil.multiKt2e0(self.meanKtProjected, ax=jumpoffAxMale, bx=self.bxComb,
												  lifeTableParams=self.lifeTableParams)
		
		return							# Don't return anything useful
Exemple #13
0
    def _do_lc(self):
        """
		Do the inference and the simulation, saving the results as
		state in the instance.
		"""
        ########## LC step #######
        ## Basic LC step, combined.  Store all the results in the instance.
        (self.axComb, self.bxComb, self.ktComb, self.kt_unfitComb, self.UComb,
         self.XComb, self.VComb, self.lnmxAdjustedComb,
         self.lnmxComb) = lcInfer(self.nmxComb,
                                  ageCutoff=self.ageCutoff,
                                  lifeTableParams=self.lifeTableParams,
                                  flattenBx=self.flattenBx)

        ## Basic LC step, combined.  Store all the results in the instance.
        (self.axFem, self.bxFem, self.ktFem, self.kt_unfitFem, self.UFem,
         self.XFem, self.VFem, self.lnmxAdjustedFem,
         self.lnmxFem) = lcInfer(self.nmxFem,
                                 ageCutoff=self.ageCutoff,
                                 lifeTableParams=self.lifeTableParams,
                                 flattenBx=self.flattenBx)

        ## Basic LC step, combined.  Store all the results in the instance.
        (self.axMale, self.bxMale, self.ktMale, self.kt_unfitMale, self.UMale,
         self.XMale, self.VMale, self.lnmxAdjustedMale,
         self.lnmxMale) = lcInfer(self.nmxMale,
                                  ageCutoff=self.ageCutoff,
                                  lifeTableParams=self.lifeTableParams,
                                  flattenBx=self.flattenBx)

        jumpoffAxFem = N.log(self.nmxFem[-1, 0:self.ageCutoffIndex].ravel())
        jumpoffAxMale = N.log(self.nmxMale[-1, 0:self.ageCutoffIndex].ravel())

        ## Derive current year stuff from LC inference results (kt etc).
        ## Note use of list comprehensions.
        self.e0sFromEmpiricalNmxComb = N.array([
            LcUtil.lifeTable(nmxp=nmxRow, **self.lifeTableParams)
            for nmxRow in self.nmxComb
        ])
        self.e0sFromEmpiricalNmxMale = N.array([
            LcUtil.lifeTable(nmxp=nmxRow, **self.lifeTableParams)
            for nmxRow in self.nmxMale
        ])
        self.e0sFromEmpiricalNmxFem = N.array([
            LcUtil.lifeTable(nmxp=nmxRow, **self.lifeTableParams)
            for nmxRow in self.nmxFem
        ])
        self.nmxsFromKtCurrentComb = project_nmx(kt=self.ktComb,
                                                 bx=self.bxComb,
                                                 ax=self.axComb,
                                                 ageCutoff=self.ageCutoff)
        self.nmxsFromKtCurrentMale = project_nmx(kt=self.ktComb,
                                                 bx=self.bxComb,
                                                 ax=self.axMale,
                                                 ageCutoff=self.ageCutoff)
        self.nmxsFromKtCurrentFem = project_nmx(kt=self.ktComb,
                                                bx=self.bxComb,
                                                ax=self.axFem,
                                                ageCutoff=self.ageCutoff)
        self.nmxsFromKtCurrent_unfitComb = project_nmx(
            kt=self.kt_unfitComb,
            bx=self.bxComb,
            ax=self.axComb,
            ageCutoff=self.ageCutoff)
        self.nmxsFromKtCurrent_unfitMale = project_nmx(
            kt=self.kt_unfitComb,
            bx=self.bxComb,
            ax=self.axMale,
            ageCutoff=self.ageCutoff)
        self.nmxsFromKtCurrent_unfitFem = project_nmx(kt=self.kt_unfitComb,
                                                      bx=self.bxComb,
                                                      ax=self.axFem,
                                                      ageCutoff=self.ageCutoff)
        self.e0sFromKtCurrentComb = N.array([
            LcUtil.lifeTable(nmxp=nmxRow, **self.lifeTableParams)
            for nmxRow in self.nmxsFromKtCurrentComb
        ])
        self.e0sFromKtCurrentMale = N.array([
            LcUtil.lifeTable(nmxp=nmxRow, **self.lifeTableParams)
            for nmxRow in self.nmxsFromKtCurrentMale
        ])
        self.e0sFromKtCurrentFem = N.array([
            LcUtil.lifeTable(nmxp=nmxRow, **self.lifeTableParams)
            for nmxRow in self.nmxsFromKtCurrentFem
        ])
        self.e0sFromKtCurrent_unfitComb = N.array([
            LcUtil.lifeTable(nmxp=nmxRow, **self.lifeTableParams)
            for nmxRow in self.nmxsFromKtCurrent_unfitComb
        ])
        self.e0sFromKtCurrent_unfitMale = N.array([
            LcUtil.lifeTable(nmxp=nmxRow, **self.lifeTableParams)
            for nmxRow in self.nmxsFromKtCurrent_unfitMale
        ])
        self.e0sFromKtCurrent_unfitFem = N.array([
            LcUtil.lifeTable(nmxp=nmxRow, **self.lifeTableParams)
            for nmxRow in self.nmxsFromKtCurrent_unfitFem
        ])
        assert len(self.e0sFromKtCurrentComb) >= 1, \
            AssertionError("self.e0sFromKtCurrentComb: %s" % self.e0sFromKtCurrentComb)
        assert type(self.e0sFromKtCurrentComb) == N.ndarray, \
            AssertionError("Weird type: %s" % type(self.e0sFromKtCurrentComb))

        ######## Simulation ##############
        ## Get random walk parameters
        self.diffedKt = S.diff(self.ktComb)
        self.drift = S.average(self.diffedKt)
        self.stdErrorEq = S.std(self.diffedKt)
        self.stdErrorCoeff = self.stdErrorEq / S.sqrt(len(self.diffedKt))

        ## Multi run simulation with above paramenters
        self.kt_simul = sim_kt(SEC=self.stdErrorCoeff,
                               SEE=self.stdErrorEq,
                               drift=self.drift,
                               numRuns=self.numRuns,
                               stepsForward=self.stepsForward,
                               sortflag=False)
        self.nmx_projectedFem = project_nmx(kt=self.kt_simul,
                                            bx=self.bxComb,
                                            ax=jumpoffAxFem,
                                            ageCutoff=self.ageCutoff)
        self.nmx_projectedMale = project_nmx(kt=self.kt_simul,
                                             bx=self.bxComb,
                                             ax=jumpoffAxMale,
                                             ageCutoff=self.ageCutoff)
        self.nmx_projected_stochastic_median_final_F = project_nmx(
            kt=N.median(N.sort(self.kt_simul, 0)),
            bx=self.bxComb,
            ax=jumpoffAxFem,
            ageCutoff=self.ageCutoff)
        self.nmx_projected_stochastic_median_final_M = project_nmx(
            kt=N.median(N.sort(self.kt_simul, 0)),
            bx=self.bxComb,
            ax=jumpoffAxMale,
            ageCutoff=self.ageCutoff)

        self.e0s_projectedFem = lots_e0s(self.percentileIndices,
                                         self.nmx_projectedFem,
                                         self.lifeTableParams)
        self.e0s_projectedMale = lots_e0s(self.percentileIndices,
                                          self.nmx_projectedMale,
                                          self.lifeTableParams)

        ## Derive analytic mean and x% forecast interval of kt
        x = S.arange(0.0, float(self.stepsForward + 1))
        self.KtStdError = ((x * (self.stdErrorEq**2.0)) +
                           (x * self.stdErrorCoeff)**2.0
                           )**.5  # kt.stderr <- ( (x*see^2) + (x*sec)^2 )^.5
        tArray = N.array([self.drift] * (self.stepsForward + 1))
        tArray[0] = 0.0
        self.meanKtProjected = N.cumsum(tArray)
        self.upperKtProjected = self.meanKtProjected + (self.zscore *
                                                        self.KtStdError)
        self.lowerKtProjected = self.meanKtProjected - (self.zscore *
                                                        self.KtStdError)

        self.nmx_projectedMedian_F = project_nmx(kt=self.meanKtProjected,
                                                 bx=self.bxComb,
                                                 ax=jumpoffAxFem,
                                                 ageCutoff=self.ageCutoff)
        self.nmx_projectedMedian_M = project_nmx(kt=self.meanKtProjected,
                                                 bx=self.bxComb,
                                                 ax=jumpoffAxMale,
                                                 ageCutoff=self.ageCutoff)
        self.nmx_projected_median_final_F = self.nmx_projectedMedian_F[-1, :]
        self.nmx_projected_median_final_M = self.nmx_projectedMedian_M[-1, :]

        ## Derive the projected e0s, F
        self.upperE0ProjectedFem = LcUtil.multiKt2e0(
            self.upperKtProjected,
            ax=jumpoffAxFem,
            bx=self.bxComb,
            lifeTableParams=self.lifeTableParams)
        self.lowerE0ProjectedFem = LcUtil.multiKt2e0(
            self.lowerKtProjected,
            ax=jumpoffAxFem,
            bx=self.bxComb,
            lifeTableParams=self.lifeTableParams)
        self.meanE0ProjectedFem = LcUtil.multiKt2e0(
            self.meanKtProjected,
            ax=jumpoffAxFem,
            bx=self.bxComb,
            lifeTableParams=self.lifeTableParams)
        ## Derive the projected e0s, M
        self.upperE0ProjectedMale = LcUtil.multiKt2e0(
            self.upperKtProjected,
            ax=jumpoffAxMale,
            bx=self.bxComb,
            lifeTableParams=self.lifeTableParams)
        self.lowerE0ProjectedMale = LcUtil.multiKt2e0(
            self.lowerKtProjected,
            ax=jumpoffAxMale,
            bx=self.bxComb,
            lifeTableParams=self.lifeTableParams)
        self.meanE0ProjectedMale = LcUtil.multiKt2e0(
            self.meanKtProjected,
            ax=jumpoffAxMale,
            bx=self.bxComb,
            lifeTableParams=self.lifeTableParams)

        return  # Don't return anything useful
Exemple #14
0
    def __str__(self):
        """Return a string that gives the content of the LC object"""

        run_info = ''
        # Info about the run/software/user/etc ...
        # ... include a link to the text dump of the object...
        dumpLink = LCFIT_WWW_OBJECT_DUMP + '&LC_OBJECT_ID=' + str(self.LcID)
        run_info += "<p><a href='%s'> Object Dump </button></a></p>" % (dumpLink,)

        # Info about the run/software/user/etc ...
        run_info += '<pre>RUN INFORMATION:\t\n'
        run_info += 'Current time:\t %s\n' % datetime.datetime.today().strftime('%Y-%m-%d %H:%M:%S')
        run_info += 'Run time:\t %s\n' % self.timestamp 
        run_info += 'Software Id:\t %s\n' % self.LcObjectINFO
        run_info += 'Object ID:\t %s\n' % self.LcID
        run_info += '\nNotes:\t %s\n' % self.notes

        
        # ...display the scalar values of interetest ...
        run_info += '\n'
        run_info += 'First year of empirical data:\t %s\n' % self.start_year
        run_info += 'End year of empirical data:\t%s\n' % self.years[-1]
        run_info += 'End of forecast:\t%s\n' % self.years_fcst[-1]
        run_info += 'Cutoff age for age nmx extension:\t %s\n' % self.ageCutoff
        run_info += 'Lifetable age regime:\t 0-1, 1-4, 5-9, ..., 105-110, 110+\n'
        run_info += 'Percentile values for display:\t %s\n' % self.percentiles
        run_info += 'Number of projection runs:\t %s\n' % self.numRuns
        run_info += 'Number of years projected forward:\t %s\n' % self.stepsForward
        run_info += 'Width of projection step:\t %s year(s)\n' % LCFIT_PROJECTION_WIDTH
        run_info += 'Width of projection step:\t %s year(s)\n' % LCFIT_PROJECTION_WIDTH
        # ... close <pre>...
        run_info += '</pre>'

        # Explanation coefficients
        run_info += "<p>EPRs:</p>\n" + "<p>\n" + \
                    LcUtil.tablefy(dataList=N.array([self.R_S, self.R_C, self.R_AC,  self.R_RW, self.R_AR1, self.R_Foo]),
                                   sideLabels=self.labels,
                                   headings=['R_S', 'R_C', 'R_AC', 'R_RW', 'R_AR1', 'R_Foo']) + "</p>\n"
        run_info += "<p>EPCR: %s</p>\n" % self.proportion_first_eigenvalue_from_combined

        if True:
            # Ax for each input
            tmp_list =[L['ax'] for L in self.individualLc] + [self.combinedLc['ax']]
            tmp_str = LcUtil.tablefy(dataList=N.array(tmp_list),
                                        sideLabels=LCFIT_AGES,
                                        headings=self.labels + ['comb'],
                                        itemName='Age') 
            run_info += "<p>ax's:</p>\n" + "<p>\n" + tmp_str + "</p>\n"
            del tmp_list, tmp_str

            # bx for each input
            tmp_list =[L['bx'] for L in self.individualLc]  + [self.combinedLc['bx']]
            tmp_str = LcUtil.tablefy(dataList=N.array(tmp_list),
                                        sideLabels=LCFIT_AGES,
                                        headings=self.labels + ['comb'],
                                        itemName='Age') 
            run_info += "<p>bx's:</p>\n" + "<p>\n" + tmp_str + "</p>\n"
            del tmp_list, tmp_str

            # kt for each input
            tmp_list = [L['ktUnfit'] for L in self.individualResidualLc] \
                       + [self.combinedLc['ktFit'], self.combinedLc['e0emp']]
            tmp_array = N.array(tmp_list)
            tmp_str = LcUtil.tablefy(dataList=tmp_array,
                                     sideLabels=self.years,
                                     headings=self.labels + ['combination kt', 'e0-empirical'] ,
                                     itemName='Year') 
            run_info += "<p>Yearly Kt's:</p>\n" + "<p>\n" + tmp_str + "</p>\n"
            del  tmp_array, tmp_list, tmp_str

        if True:
            # AR(1) model for all residuals
            #self.individualResidualLc[i]['ktunfit_ar1']['c0, sda0, c1, sda1, Rsq, stderr_est']
            AR1_stuff = N.zeros((len(self.individualResidualLc),6))
            tmp_list = []
            tmp_headings = ['c0', 'sda0', 'c1', 'sda1', 'Rsq', 'stderr_est']
            for i, pop in enumerate(self.individualResidualLc):
                tmp_list.append([pop['ktunfit_ar1'][k] for k in tmp_headings])
            tmp_array = N.array(tmp_list, N.float_)
            run_info += "<p>AR(1) models for residuals:</p>\n" + "<p>\n" + \
                        LcUtil.tablefy(dataList=tmp_array.transpose(),
                                       sideLabels=self.labels+[],
                                       headings=tmp_headings,
                                       itemName='AR1 Model') + \
                                       "</p>\n"
            del(tmp_array, pop)

        if False:               # XXX This doesn't work!
            # forecast e0 for each input (think I switch indexing
            # conventions between inference and simulation ... not
            # good... [1]['kt_unfit'] vs ['kt_unfit'][1]
            tmp_list = [L for L in self.Simulation['e0_indiv']]
            tmp_array = N.array(tmp_list)
            tmp_str = LcUtil.tablefy(dataList=tmp_array,
                                     sideLabels=self.years,
                                     headings=self.labels,
                                     itemName='Year') 
            run_info += "<p>Yearly Forecast e0's:</p>\n" + "<p>\n" + tmp_str + "</p>\n"
            del  tmp_array, tmp_list, tmp_str


        # image summarizing forecast
        fc_img_path = LCFIT_WWW_DISPLAY_IMAGE + '&' + LCFIT_OBJECT_ID_KEY + '=' \
                      + str(self.LcID) + '&' + LCFIT_IMAGE_NAME_KEY + '=' + FC_IMAGE_NAME
        fc_image = '<a href=%s><img src="%s" height = %i width = %i alt="PNG of forecast kts %s"></a>\n' \
                   % (fc_img_path, fc_img_path, IMGH, IMGW, self.LcID)

        # image showing e0s
        e0_img_path = LCFIT_WWW_DISPLAY_IMAGE + '&' + LCFIT_OBJECT_ID_KEY + '=' \
                      + str(self.LcID) + '&' + LCFIT_IMAGE_NAME_KEY + '=' + E0S_IMAGE_NAME
        e0_image = '<a href=%s><img src="%s" height = %i width = %i alt="PNG of empirical e0s %s"></a>\n' \
                   % (e0_img_path, e0_img_path, IMGH, IMGW, self.LcID)
        
        # Build html string
        #'<tr><td colspan=3>' + lc_image + fc_image + lnmx_image + '</td></tr>\n' + \
        HTMLstr = '<table  border="1">\n' + \
                  '<tr><td colspan=3> <h3>LCFIT Run</h3></td>\n' + \
                  '<tr><td colspan=3>' + run_info + '</td></tr>\n' + \
                  '<tr><td>' + e0_image + fc_image + '</td></tr>\n' + \
                  '<tr><td colspan=3> Please direct questions or comments to: %s </td>\n' % EMAIL + \
                 '</table>\n'
        return HTMLstr
Exemple #15
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))