Skip to content

Instantly share code, notes, and snippets.

@weallwegot
Last active November 10, 2018 08:28
Show Gist options
  • Save weallwegot/47a9b978efdeb8ba8ab33da3e0b6a410 to your computer and use it in GitHub Desktop.
Save weallwegot/47a9b978efdeb8ba8ab33da3e0b6a410 to your computer and use it in GitHub Desktop.
A scaled down implementation of calculating double texts occurrences.
PARTICPANT_1 = "Me"
PARTICPANT_2 = "Friend"
time_diffs_s1 = []
time_diffs_s2 = []
#tes is a list of TextEquivalent objects
for i in range(len(tes)):
earlier_te = tes[i]
# because calculations require 2 Text Equivalents simulataneously
if i < len(tes)-1:
later_te = tes[i+1]
time_diff_dict = calc_time_between_text_equivalents(earlier_te,later_te)
# store all the time difference data separately depending on who sent the first message
if earlier_te.sender == PARTICPANT_1:
time_diffs_s1.append(time_diff_dict)
elif earlier_te.sender == PARTICPANT_2:
time_diffs_s2.append(time_diff_dict)
# use the time differences only if the sender & responder are different
non_double_texts_time_diffs_s1 = [td['time diff'] for td in time_diffs_s1 if not td['double text']]
# calculate media response rate with numpy
median_wait_time = np.median(non_double_texts_time_diffs_s1)
# count double texts by seeing when sender & responder are the same AND time elapsed is > 3.5*median_wait_time
num_double_texts_s1 = len([x for x in time_diffs_s1 if x['double text'] and x['time diff'] > median_wait_time*3.5])
# convert to a percentage
my_double_text_rate = 100.*float(num_double_texts_s1)/number_of_text_eqs_sent_s1
def calc_time_between_text_equivalents(tes_1,tes_2):
"""
calculate the time between texts
check if both texts are from the same person
:param tes_1: the earlier of the text objects
:tes_1 type: TextEquivalent object
:param tes_2: the later of the text objects
:tes_2 type: TextEquivalent object
:returns: dictionary with information about time between two input texts
:rtype: dictionary
"""
return_vals = {}
# it is important to subtract later from earlier for proper time
tdelta = (tes_2.timestamp-tes_1.timestamp)
# TODO: implement Pint units module
return_vals['time diff'] = tdelta.seconds + tdelta.days*24.0*60.0*60.0
return_vals['responder'] = tes_2.sender
return_vals['sender'] = tes_1.sender
# use the person who sent the message to calculate day of week
return_vals['day of week'] = tes_1.date_day_of_week
# check if sender is the same for both messages
return_vals['double text'] = tes_2.sender==tes_1.sender
# store the hour for future segmenting data by hour of day
return_vals['hour'] = tes_1.timestamp.hour
return return_vals
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment