Use better streak algorithm.
This commit is contained in:
		
							
								
								
									
										37
									
								
								pi_temp.py
									
									
									
									
									
								
							
							
						
						
									
										37
									
								
								pi_temp.py
									
									
									
									
									
								
							@@ -120,7 +120,7 @@ def history():
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
    fig = Figure(data=data, layout=layout)
 | 
					    fig = Figure(data=data, layout=layout)
 | 
				
			||||||
    graphJSON = json.dumps(fig, cls=plotly.utils.PlotlyJSONEncoder)
 | 
					    graphJSON = json.dumps(fig, cls=plotly.utils.PlotlyJSONEncoder)
 | 
				
			||||||
    
 | 
					
 | 
				
			||||||
    ## Duration of significant heat increases
 | 
					    ## Duration of significant heat increases
 | 
				
			||||||
    # Timestep from raw database values for temperature
 | 
					    # Timestep from raw database values for temperature
 | 
				
			||||||
    timestep_minutes = int(round((datetime.datetime.strptime(temperatures[1][0], "%Y-%m-%d %H:%M:%S")-datetime.datetime.strptime(temperatures[0][0], "%Y-%m-%d %H:%M:%S")).seconds/60))
 | 
					    timestep_minutes = int(round((datetime.datetime.strptime(temperatures[1][0], "%Y-%m-%d %H:%M:%S")-datetime.datetime.strptime(temperatures[0][0], "%Y-%m-%d %H:%M:%S")).seconds/60))
 | 
				
			||||||
@@ -128,7 +128,8 @@ def history():
 | 
				
			|||||||
        timestep_minutes = 1
 | 
					        timestep_minutes = 1
 | 
				
			||||||
    # Calculate minutes for each streak
 | 
					    # Calculate minutes for each streak
 | 
				
			||||||
    streak_minutes = streak_lengths(time_series_temperature_values)*timestep_minutes
 | 
					    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
 | 
					    # Send output to page
 | 
				
			||||||
    return render_template("history.html",  timezone = timezone,
 | 
					    return render_template("history.html",  timezone = timezone,
 | 
				
			||||||
                                            graphJSON = graphJSON,
 | 
					                                            graphJSON = graphJSON,
 | 
				
			||||||
@@ -136,21 +137,23 @@ def history():
 | 
				
			|||||||
                                            range_hours = range_hours,
 | 
					                                            range_hours = range_hours,
 | 
				
			||||||
                                            debug = streak_minutes,
 | 
					                                            debug = streak_minutes,
 | 
				
			||||||
                                            )
 | 
					                                            )
 | 
				
			||||||
# Calculate streak lengths (https://stackoverflow.com/a/20614650/2152245)                                            
 | 
					
 | 
				
			||||||
def streak_lengths(temps):
 | 
					# Calculate streak lengths. Based on https://stackoverflow.com/a/33403822/2152245.
 | 
				
			||||||
    if len(temps) < 2:
 | 
					def streak_lengths(x): 
 | 
				
			||||||
        return len(temps)
 | 
					    # find the boundaries where numbers are not consecutive
 | 
				
			||||||
    else:
 | 
					    boundaries = [i for i in range(1, len(x)) if x[i] < x[i-1]]
 | 
				
			||||||
        start, streaks = -1, []
 | 
					    # add the start and end boundaries
 | 
				
			||||||
        for idx, (x, y) in enumerate(zip(temps, temps[1:])):
 | 
					    boundaries = [0] + boundaries + [len(x)]
 | 
				
			||||||
            if x > y:
 | 
					    # take the boundaries as pairwise slices
 | 
				
			||||||
                streaks.append(idx - start)
 | 
					    slices = [boundaries[i:i + 2] for i in range(len(boundaries) - 1)]
 | 
				
			||||||
                start = idx 
 | 
					    # extract all sequences with length greater than one
 | 
				
			||||||
        else:
 | 
					    temporary = [x[start:end] for start, end in slices if end - start > 1]
 | 
				
			||||||
            streaks.append(idx - start + 1) 
 | 
					    # Remove tuples with all equal values
 | 
				
			||||||
        long_streaks = [x-1 for x in streaks if x > 4]
 | 
					    screened = [x for x in temporary if not all(y==x[0] for y in x)]
 | 
				
			||||||
        return long_streaks
 | 
					    # Calculate length of each list
 | 
				
			||||||
        
 | 
					    out = [len(x) for x in screened]
 | 
				
			||||||
 | 
					    return out
 | 
				
			||||||
 | 
					
 | 
				
			||||||
def get_records():
 | 
					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
 | 
					    from_date_str   = request.args.get('from',time.strftime("%Y-%m-%d 00:00")) #Get the from date value from the URL
 | 
				
			||||||
    to_date_str     = request.args.get('to',time.strftime("%Y-%m-%d %H:%M"))   #Get the to date value from the URL
 | 
					    to_date_str     = request.args.get('to',time.strftime("%Y-%m-%d %H:%M"))   #Get the to date value from the URL
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -82,7 +82,7 @@
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
    <div class='row' id='plotly-plot'></div>
 | 
					    <div class='row' id='plotly-plot'></div>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    {{ debug }}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  </body>
 | 
					  </body>
 | 
				
			||||||
      
 | 
					      
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user