<commit_before>from datetime import datetime, date, time

activeFilename = 'active.json'
averagesFilename = 'averages.json'
predActiveFilename = 'predictionsNow.json'
predFutureFilename = 'predictionsLater.json'


try:
    with open( activeFilename ) as activefile:        
        activeAreas = json.load( activefile )
        activeAreas[0] = datetime.strptime( activeAreas[0], '%M-%H-%d-%m-%Y' )
except IOError:
    activeAreas = [ datetime( 1970, 1, 1), {} ]

try:
    with open( averagesFilename ) as averagesfile:
        averageAreas = json.load(averagesfile)
except IOError:
    averageAreas = {}<commit_msg>predicts current occupancy<commit_after>from datetime import datetime, date, time, timedelta
import json, math

activeFilename = 'src/src/client/active.json'
averagesFilename = 'src/src/client/averages.json'
predActiveFilename = 'src/src/client/predictionsNow.json'
predFutureFilename = 'src/src/client/predictionsLater.json'

MINUTESBEFOREPREDICT = 30
MINUTESAFTERNOCURRENT = 150
IGNOREHISTHOURS = 2

curTime = datetime.now()

predActiveDict = {}
predFutureDict = {}

def refresh():
	curTime = activeAreas[0] + timedelta(seconds = 60 * (MINUTESBEFOREPREDICT + 50))
	if (curTime > (activeAreas[0] + timedelta(seconds = 60*MINUTESBEFOREPREDICT))):
		for area in activeAreas[1].keys():
			predActiveDict[area] = predict(area, (curTime - activeAreas[0]), curTime)
	else:
		for area in activeAreas[1].keys():
			predActiveDict[area] = activeAreas[1][area]
	with open(predActiveFilename, 'w') as PAF:
		json.dump(predActiveDict, PAF)

def predict(area, updateLag, predictTime):
	updateLagMin = updateLag.total_seconds()/60 - MINUTESBEFOREPREDICT
	MINUTEWEIGHTINGDIFF = MINUTESAFTERNOCURRENT - MINUTESBEFOREPREDICT
	dayOfWeek = parsedate(predictTime)
	histPred = ceil(histAvg(averageAreas[area][dayOfWeek], predictTime))
	if (updateLagMin > MINUTESAFTERNOCURRENT):
		return histPred
	recentDataWeight = (MINUTEWEIGHTINGDIFF - updateLagMin)/MINUTEWEIGHTINGDIFF
	return math.ceil(activeAreas[1][area] * recentDataWeight + histPred * (1 - recentDataWeight))


def histAvg(avgDict, predictTime):
	leftPtr = 0
	rightPtr = 24
	leftVal = 0
	rightVal = 0
	predTimeOfDay = int(predictTime.strftime("%H")) + float(predictTime.strftime("%M"))/60

	for time in avgDict.keys():
		formTime = strToTime(time)
		if (formTime < predTimeOfDay and formTime > leftPtr):
			leftPtr = formTime
			leftVal = avgDict[time]
		if (formTime > predTimeOfDay and formTime < rightPtr):
			rightPtr = formTime
			rightVal = avgDict[time]

	if (leftPtr == 0 or (leftPtr + IGNOREHISTHOURS) < predTimeOfDay):
		leftVal = 0
	if (rightPtr == 24 or (rightPtr - IGNOREHISTHOURS) > predTimeOfDay):
		rightVal = 0

	leftDiff = predTimeOfDay - leftPtr
	rightDiff = rightPtr - predTimeOfDay
	totalDiff = leftDiff + rightDiff

	return (rightDiff*leftVal/totalDiff + leftDiff*rightVal/totalDiff)

def parsedate( date ):
    return date.strftime( '%A' )

def strToTime(timeString):
    time = timeString.split( ':' )
    hours = int( time[0] )
    
    if ( time[1][-2] == 'p' ):
      hours += 12
    
    mins = time[1][0:2]
    return (hours + float( mins )/60)

try:
    with open( activeFilename ) as activefile:        
        activeAreas = json.load( activefile )
        activeAreas[0] = datetime.strptime( activeAreas[0], '%Y-%m-%d %H-%M' )
except IOError:
    activeAreas = [ datetime( 1970, 1, 1), {} ]

try:
    with open( averagesFilename ) as averagesfile:
        averageAreas = json.load(averagesfile)
except IOError:
    averageAreas = {}


refresh()
{'fraction_non_alphanumeric': 0.12001132182281347, 'fraction_numerical': 0.016982734220209454, 'mean_word_length': 4.420245398773006, 'pattern_counts': {'":': 0, '<': 6, '<?xml version=': 0, '>': 8, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1944732', 'n_tokens_mistral': 1298, 'n_tokens_neox': 1204, 'n_words': 268}