mirror of
https://git.roussel.pro/telecom-paris/GIN206.git
synced 2026-02-09 02:30:17 +01:00
added documentation
This commit is contained in:
185
main.py
185
main.py
@@ -4,11 +4,37 @@ from generate_data import *
|
||||
from analyze import *
|
||||
from poll import *
|
||||
|
||||
# sort two lists based on the first list
|
||||
def sort(X,Y):
|
||||
return zip(*sorted(zip(X,Y)))
|
||||
def sort(X, Y):
|
||||
"""
|
||||
Sorts two lists X and Y in ascending order based on the values in X.
|
||||
|
||||
Args:
|
||||
X (list): The first list to be sorted.
|
||||
Y (list): The second list to be sorted.
|
||||
|
||||
Returns:
|
||||
tuple: A tuple containing the sorted X and Y lists.
|
||||
|
||||
Example:
|
||||
X = [3, 1, 2]
|
||||
Y = ['c', 'a', 'b']
|
||||
sorted_X, sorted_Y = sort(X, Y)
|
||||
# sorted_X: [1, 2, 3]
|
||||
# sorted_Y: ['a', 'b', 'c']
|
||||
"""
|
||||
return zip(*sorted(zip(X, Y)))
|
||||
|
||||
def plot_temperature_data(df, recent_count=None):
|
||||
"""
|
||||
Plots the temperature data from a DataFrame.
|
||||
|
||||
Args:
|
||||
df (pandas.DataFrame): The DataFrame containing the temperature data.
|
||||
recent_count (int, optional): The number of recent data points to plot. If specified, only the last 'recent_count' rows will be plotted. Defaults to None.
|
||||
|
||||
Returns:
|
||||
None
|
||||
"""
|
||||
plt.figure(figsize=(5, 5))
|
||||
|
||||
# Check if recent_count is specified and valid
|
||||
@@ -25,6 +51,20 @@ def plot_temperature_data(df, recent_count=None):
|
||||
plt.show()
|
||||
|
||||
def test_sample_every_kth_point(df):
|
||||
"""
|
||||
Test the sample_every_kth_point function with different values of k.
|
||||
|
||||
Parameters:
|
||||
- df: The input DataFrame containing the data.
|
||||
|
||||
Returns:
|
||||
- X: The array of values used for sampling.
|
||||
- EFFICIENCY: The efficiency values for each sampling.
|
||||
- MEAN: The mean error values for each sampling.
|
||||
- MEDIAN: The median error values for each sampling.
|
||||
- STD: The standard deviation of error values for each sampling.
|
||||
"""
|
||||
|
||||
X = np.arange(1, 10, 1)
|
||||
MEAN = []
|
||||
STD = []
|
||||
@@ -46,24 +86,59 @@ def test_sample_every_kth_point(df):
|
||||
|
||||
|
||||
def example_sample_every_kth_point(k=10):
|
||||
"""
|
||||
Example function that demonstrates how to sample every kth point from a dataframe and plot the temperature data.
|
||||
|
||||
Parameters:
|
||||
k (int): The sampling interval. Default is 10.
|
||||
|
||||
Returns:
|
||||
None
|
||||
"""
|
||||
df = generate_greenhouse_data("datasets/greenhouse.csv")
|
||||
df = df.tail(150)
|
||||
df = sample_every_kth_point(df, k)
|
||||
plot_temperature_data(df)
|
||||
|
||||
def example_sample_reglin():
|
||||
"""
|
||||
This function demonstrates the usage of the sample_reglin function.
|
||||
It generates greenhouse data, selects the last 150 rows, applies the sample_reglin function,
|
||||
and plots the temperature data.
|
||||
"""
|
||||
df = generate_greenhouse_data("datasets/greenhouse.csv")
|
||||
df = df.tail(150)
|
||||
df = sample_reglin(df)
|
||||
plot_temperature_data(df)
|
||||
|
||||
def exaample_optimal_sample(dT = 0.3):
|
||||
def example_optimal_sample(dT = 0.3):
|
||||
"""
|
||||
Example function that demonstrates the usage of the optimal_sample function.
|
||||
|
||||
Parameters:
|
||||
dT (float): The threshold value for temperature difference. Default is 0.3.
|
||||
|
||||
Returns:
|
||||
None
|
||||
"""
|
||||
df = generate_greenhouse_data("datasets/greenhouse.csv")
|
||||
df = df.tail(150)
|
||||
df = optimal_sample(df, threshold_dT=dT)
|
||||
plot_temperature_data(df)
|
||||
|
||||
def example_sample_avg_rate_of_change():
|
||||
"""
|
||||
This function demonstrates how to calculate the sample average rate of change for temperature data.
|
||||
It generates greenhouse data, calculates the hourly rate of change, selects the last 150 records,
|
||||
and then calculates the sample average rate of change based on the hourly rate of change.
|
||||
Finally, it plots the temperature data.
|
||||
|
||||
Parameters:
|
||||
None
|
||||
|
||||
Returns:
|
||||
None
|
||||
"""
|
||||
df = generate_greenhouse_data("datasets/greenhouse.csv")
|
||||
hroc = hourly_rate_of_change(df)
|
||||
df = df.tail(150)
|
||||
@@ -71,6 +146,25 @@ def example_sample_avg_rate_of_change():
|
||||
plot_temperature_data(df)
|
||||
|
||||
def test_sample_reglin(df):
|
||||
"""
|
||||
Perform a test on the sample_reglin function with different values of max_dT.
|
||||
|
||||
Parameters:
|
||||
- df: DataFrame
|
||||
The input DataFrame containing temperature data.
|
||||
|
||||
Returns:
|
||||
- X: ndarray
|
||||
An array of values ranging from 0.4 to 3 with a step of 0.05.
|
||||
- EFFICIENCY: list
|
||||
A list of efficiency values calculated for each max_dT value.
|
||||
- MEAN: list
|
||||
A list of mean error values calculated for each max_dT value.
|
||||
- MEDIAN: list
|
||||
A list of median error values calculated for each max_dT value.
|
||||
- STD: list
|
||||
A list of standard deviation error values calculated for each max_dT value.
|
||||
"""
|
||||
X = np.arange(0.4, 3, 0.05)
|
||||
MEAN = []
|
||||
STD = []
|
||||
@@ -91,6 +185,20 @@ def test_sample_reglin(df):
|
||||
|
||||
|
||||
def test_optimal_sample(df):
|
||||
"""
|
||||
Test the optimal sample function with different threshold values.
|
||||
|
||||
Args:
|
||||
df (pandas.DataFrame): The input DataFrame containing temperature data.
|
||||
|
||||
Returns:
|
||||
tuple: A tuple containing the following lists:
|
||||
- X (numpy.ndarray): An array of threshold values.
|
||||
- EFFICIENCY (list): A list of efficiency values for each threshold.
|
||||
- MEAN (list): A list of mean error values for each threshold.
|
||||
- MEDIAN (list): A list of median error values for each threshold.
|
||||
- STD (list): A list of standard deviation error values for each threshold.
|
||||
"""
|
||||
X = np.arange(0.1, 3, 0.05)
|
||||
MEAN = []
|
||||
STD = []
|
||||
@@ -109,7 +217,24 @@ def test_optimal_sample(df):
|
||||
EFFICIENCY.append(compute_efficiency(df_sampeld))
|
||||
return X, EFFICIENCY, MEAN, MEDIAN, STD
|
||||
|
||||
def test_sample_avg_rate_of_change(df,hourly_rate_of_change):
|
||||
def test_sample_avg_rate_of_change(df, hourly_rate_of_change):
|
||||
"""
|
||||
Test the sample average rate of change.
|
||||
|
||||
This function takes a DataFrame `df` and the `hourly_rate_of_change` as input.
|
||||
It performs a series of calculations on the data and returns the results.
|
||||
|
||||
Parameters:
|
||||
- df (pandas.DataFrame): The input DataFrame containing the data.
|
||||
- hourly_rate_of_change (float): The hourly rate of change.
|
||||
|
||||
Returns:
|
||||
- X (numpy.ndarray): An array of values ranging from 0.01 to 3 with a step of 0.05.
|
||||
- EFFICIENCY (list): A list of efficiency values calculated for each sample.
|
||||
- MEAN (list): A list of mean values calculated for each sample.
|
||||
- MEDIAN (list): A list of median values calculated for each sample.
|
||||
- STD (list): A list of standard deviation values calculated for each sample.
|
||||
"""
|
||||
X = np.arange(0.01, 3, 0.05)
|
||||
MEAN = []
|
||||
STD = []
|
||||
@@ -128,35 +253,56 @@ def test_sample_avg_rate_of_change(df,hourly_rate_of_change):
|
||||
EFFICIENCY.append(compute_efficiency(df_sampled))
|
||||
return X, EFFICIENCY, MEAN, MEDIAN, STD
|
||||
|
||||
def comparaison_mean(df,limit=1000):
|
||||
def comparaison_mean(df, limit=1000):
|
||||
"""
|
||||
Compare different sampling methods based on their mean and efficiency.
|
||||
|
||||
Parameters:
|
||||
- df: DataFrame
|
||||
The input DataFrame containing the data.
|
||||
- limit: int, optional
|
||||
The number of rows to consider from the end of the DataFrame. Default is 1000.
|
||||
|
||||
Returns:
|
||||
None
|
||||
"""
|
||||
plt.figure(figsize=(10, 5))
|
||||
hroc = hourly_rate_of_change(df)
|
||||
df = df.tail(limit)
|
||||
X, EFFICIENCY, MEAN, MEDIAN, STD = test_sample_every_kth_point(df)
|
||||
MEAN, EFFICIENCY = sort(MEAN, EFFICIENCY)
|
||||
plt.plot( MEAN,EFFICIENCY, label="Constant Polling Interval", marker='x')
|
||||
plt.plot(MEAN, EFFICIENCY, label="Constant Polling Interval", marker='x')
|
||||
|
||||
X, EFFICIENCY, MEAN, MEDIAN, STD = test_sample_reglin(df)
|
||||
MEAN, EFFICIENCY = sort(MEAN, EFFICIENCY)
|
||||
plt.plot( MEAN,EFFICIENCY, label="Linear Regression", marker='x')
|
||||
plt.plot(MEAN, EFFICIENCY, label="Linear Regression", marker='x')
|
||||
|
||||
X, EFFICIENCY, MEAN, MEDIAN, STD = test_optimal_sample(df)
|
||||
MEAN, EFFICIENCY = sort(MEAN, EFFICIENCY)
|
||||
plt.plot( MEAN,EFFICIENCY, label="Optimal Polling rate", marker='x')
|
||||
plt.plot(MEAN, EFFICIENCY, label="Optimal Polling rate", marker='x')
|
||||
|
||||
X, EFFICIENCY, MEAN, MEDIAN, STD = test_sample_avg_rate_of_change(df,hroc)
|
||||
X, EFFICIENCY, MEAN, MEDIAN, STD = test_sample_avg_rate_of_change(df, hroc)
|
||||
MEAN, EFFICIENCY = sort(MEAN, EFFICIENCY)
|
||||
plt.plot( MEAN,EFFICIENCY, label="Hourly Rate of Change", marker='x')
|
||||
plt.plot(MEAN, EFFICIENCY, label="Hourly Rate of Change", marker='x')
|
||||
|
||||
plt.ylabel("Average seconds between polls")
|
||||
plt.xlabel("Average error")
|
||||
plt.ylim(0, 8000)
|
||||
plt.xlim(0,1.3)
|
||||
plt.xlim(0, 1.3)
|
||||
|
||||
plt.legend()
|
||||
plt.show()
|
||||
|
||||
def example_optimal_sample(dT = 0.3):
|
||||
"""
|
||||
This function demonstrates how to use the `optimal_sample` function to generate an optimal sample of greenhouse data.
|
||||
|
||||
Parameters:
|
||||
dT (float): The threshold value for temperature difference. Default is 0.3.
|
||||
|
||||
Returns:
|
||||
None
|
||||
"""
|
||||
df = generate_greenhouse_data("datasets/greenhouse.csv")
|
||||
df = df.tail(1000)
|
||||
df = optimal_sample(df, threshold_dT=dT)
|
||||
@@ -169,6 +315,15 @@ def example_optimal_sample(dT = 0.3):
|
||||
plt.show()
|
||||
|
||||
def histogram_sample_every_kth_point(k=10):
|
||||
"""
|
||||
Generate a histogram of the differences between the original data and the sampled data.
|
||||
|
||||
Parameters:
|
||||
- k (int): The sampling interval. Only every kth point will be included in the sampled data.
|
||||
|
||||
Returns:
|
||||
None
|
||||
"""
|
||||
df = generate_greenhouse_data("datasets/greenhouse.csv")
|
||||
df = df.tail(1000)
|
||||
df_sampled = sample_every_kth_point(df, k)
|
||||
@@ -192,9 +347,9 @@ def histogram_sample_every_kth_point(k=10):
|
||||
# comparaison_mean(df)
|
||||
|
||||
# Temperature rate of change over the day
|
||||
df = generate_greenhouse_data("datasets/greenhouse.csv")
|
||||
hcor = hourly_rate_of_change(df)
|
||||
print(hcor)
|
||||
# df = generate_greenhouse_data("datasets/greenhouse.csv")
|
||||
# hcor = hourly_rate_of_change(df)
|
||||
# print(hcor)
|
||||
# hcor.plot()
|
||||
# plt.xlabel("Hour of the day")
|
||||
# plt.ylabel("Average absolute rate of change (°C/hour)")
|
||||
|
||||
Reference in New Issue
Block a user