Skip to content

Instantly share code, notes, and snippets.

@sgongar
Created February 27, 2020 13:19
Show Gist options
  • Save sgongar/21c2333de5eaba3b235ddc7d50eb1501 to your computer and use it in GitHub Desktop.
Save sgongar/21c2333de5eaba3b235ddc7d50eb1501 to your computer and use it in GitHub Desktop.
Markov
msg = "Markov - ... Finished parallelized contribution algorithm."
self.log.info("{0} - {1}".format(self.task_id, msg))
try:
# Get total impact for all channels
# Sum all impacts belonging to each channel
for i in range(len(self.segments)):
total_contribution += self.tv_contribution[self.segments_n[i]].impact.sum()
msg = '{0} Segment {1} Contribution {2}'.format(self.task_id,
self.segments_n[i],
total_contribution)
self.log.debug(msg)
self.log.debug('Segments involved are {0}'.format(len(self.segments)))
self.log.debug('Total contribution is {0}'.format(total_contribution))
# Extract percentiles of channel impact
# Total contribution must be always higher than zero
self.log.debug('Total contribution before {0}'.format(type(total_contribution)))
total_contribution = float(total_contribution)
self.log.debug('Total contribution after {0}'.format(type(total_contribution)))
for i in range(len(self.segments)):
self.log.debug('Total contribution (?) {0}'.format(total_contribution))
if total_contribution > 0:
contribution = float(self.tv_contribution[self.segments_n[i]].impact.sum()) / total_contribution
self.log.debug('Contribution is {0} for segment {1}'.format(contribution, self.segments_n[i]))
percent_contribution[self.segments_n[i]] = contribution
else:
percent_contribution[self.segments_n[i]] = 0.0
self.log.debug('percent_contribution keys {0}'.format(percent_contribution.keys()))
self.log.debug('percent_contribution {0}'.format(percent_contribution))
except Exception as e:
# Raises a message if something goes wrong
percentil_extraction(self.log, self.task_id, e)
msg = "Error in percentil extraction"
raise PercentilExtractionException(msg)
# Execute Markov model
# TODO gets the exact error that could raises from
# execute_markov instead catching everything
msg = ' - Markov - Executing Markov model algorithm ... '
self.log.info("{0}{1}".format(self.task_id, msg))
try:
markov = execute_markov(markov_data)
msg = ' - Markov - ... Executed Markov model algorithm.'
self.log.info("{0}{1}".format(self.task_id, msg))
except Exception as e:
# Raises a message when something goes wrong
markov_execution(self.log, self.task_id, e)
# Raises an exception when something goes wrong
msg = "Error in Markov execution"
raise MarkovExecutionException(msg)
# Upload Markov data
if self.upload_to_db:
msg = ' - Markov - Uploading data ... '
self.log.debug("{0}{1}".format(self.task_id, msg))
try:
upload_to_matriz(self.log, self.task_id, self.start_date,
markov.transition_matrix_)
upload_to_csv(self.log, self.start_date, markov_data,
self.task_id)
msg = ' - Markov - Uploaded data ... '
self.log.info("{0}{1}".format(self.task_id, msg))
except Exception as e:
# Raises a message when something goes wrong
markov_uploading(self.log, self.task_id, e)
# Raises an exception when something goes wrong
msg = "Error uploading Markov data"
raise MarkovUploadingException(msg)
# prepare data result
final = markov.removal_effects_
final = final.set_index('channel_name')
final.columns = ['markov_effect', 'markov_effect_value']
final['channel_attribution'] = 0.0 # Create new column
final['tv_contribution'] = 0.0 # Create new column
final['tv_attribution'] = 0.0 # Create new column
final['tv_attribution_value'] = 0.0 # Create new column
self.log.debug('final structure is {0}'.format(final))
# for each impact channel
for channel, impact in percent_contribution.items():
self.log.debug('channel {0} - impact {0}'.format(channel, impact))
# we calculate the percents
# TODO Take a look over these values
if channel in final.index:
# impact of tv contribution
final.at[channel, 'tv_contribution'] = impact
self.log.debug('impact {0}'.format(impact))
# Markov effect for channel
tv_attr = float(impact * final.at[channel, 'markov_effect'])
self.log.debug('tv_attr {0}'.format(tv_attr))
final.at[channel, 'tv_attribution'] = tv_attr
# Markov effect value for channel
self.log.debug('strange value {0}'.format(final.at[channel, 'markov_effect_value']))
tv_attr_value = impact * final.at[channel,
'markov_effect_value']
tv_attr_value = float(tv_attr_value)
final.at[channel, 'tv_attribution_value'] = tv_attr_value
# ¿?
channel_markov_effect = final.at[channel, 'markov_effect']
channel_tv_attr = final.at[channel, 'tv_attribution']
channel_attr = channel_markov_effect - channel_tv_attr
final.at[channel, 'channel_attribution'] = channel_attr
else:
msg = 'Channel {0} have no contribution'.format(channel)
self.log.debug(msg)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment