Use better streak algorithm.
This commit is contained in:
		
							
								
								
									
										31
									
								
								pi_temp.py
									
									
									
									
									
								
							
							
						
						
									
										31
									
								
								pi_temp.py
									
									
									
									
									
								
							@@ -128,6 +128,7 @@ def history():
 | 
			
		||||
        timestep_minutes = 1
 | 
			
		||||
    # Calculate minutes for each streak
 | 
			
		||||
    streak_minutes = streak_lengths(time_series_temperature_values)*timestep_minutes
 | 
			
		||||
    streak_minutes = [x-1 for x in streak_minutes] #Turn this into a duration
 | 
			
		||||
 | 
			
		||||
    # Send output to page
 | 
			
		||||
    return render_template("history.html",  timezone = timezone,
 | 
			
		||||
@@ -136,20 +137,22 @@ def history():
 | 
			
		||||
                                            range_hours = range_hours,
 | 
			
		||||
                                            debug = streak_minutes,
 | 
			
		||||
                                            )
 | 
			
		||||
# Calculate streak lengths (https://stackoverflow.com/a/20614650/2152245)                                            
 | 
			
		||||
def streak_lengths(temps):
 | 
			
		||||
    if len(temps) < 2:
 | 
			
		||||
        return len(temps)
 | 
			
		||||
    else:
 | 
			
		||||
        start, streaks = -1, []
 | 
			
		||||
        for idx, (x, y) in enumerate(zip(temps, temps[1:])):
 | 
			
		||||
            if x > y:
 | 
			
		||||
                streaks.append(idx - start)
 | 
			
		||||
                start = idx 
 | 
			
		||||
        else:
 | 
			
		||||
            streaks.append(idx - start + 1) 
 | 
			
		||||
        long_streaks = [x-1 for x in streaks if x > 4]
 | 
			
		||||
        return long_streaks
 | 
			
		||||
 | 
			
		||||
# Calculate streak lengths. Based on https://stackoverflow.com/a/33403822/2152245.
 | 
			
		||||
def streak_lengths(x): 
 | 
			
		||||
    # find the boundaries where numbers are not consecutive
 | 
			
		||||
    boundaries = [i for i in range(1, len(x)) if x[i] < x[i-1]]
 | 
			
		||||
    # add the start and end boundaries
 | 
			
		||||
    boundaries = [0] + boundaries + [len(x)]
 | 
			
		||||
    # take the boundaries as pairwise slices
 | 
			
		||||
    slices = [boundaries[i:i + 2] for i in range(len(boundaries) - 1)]
 | 
			
		||||
    # extract all sequences with length greater than one
 | 
			
		||||
    temporary = [x[start:end] for start, end in slices if end - start > 1]
 | 
			
		||||
    # Remove tuples with all equal values
 | 
			
		||||
    screened = [x for x in temporary if not all(y==x[0] for y in x)]
 | 
			
		||||
    # Calculate length of each list
 | 
			
		||||
    out = [len(x) for x in screened]
 | 
			
		||||
    return out
 | 
			
		||||
 | 
			
		||||
def get_records():
 | 
			
		||||
    from_date_str   = request.args.get('from',time.strftime("%Y-%m-%d 00:00")) #Get the from date value from the URL
 | 
			
		||||
 
 | 
			
		||||
@@ -82,7 +82,7 @@
 | 
			
		||||
 | 
			
		||||
    <div class='row' id='plotly-plot'></div>
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
    {{ debug }}
 | 
			
		||||
 | 
			
		||||
  </body>
 | 
			
		||||
      
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user