def interpolate_series(self, series: pd.Series) -> pd.Series: logger.debug(f"Interpolating series; series len = {len(series)}") series = pd.to_numeric(series, downcast="float") interpolated_series = series.interpolate(method="linear", limit_area="outside", limit_direction="both") return interpolated_series
def read_temp_graph_from_binary_stream( self, binary_stream: BinaryIO) -> pd.DataFrame: logger.debug("Loading temp graph") temp_graph_df = pd.read_csv(binary_stream, encoding=self._encoding, sep=self._separator) return temp_graph_df
def __init__(self, filepath: str, writer: AbstractSyncHeatingObjWriter) -> None: self._filepath = filepath self._writer = writer logger.debug(f"Creating instance:" f"filepath: {filepath}" f"writer: {writer}")
def predict(self, weather_df: pd.DataFrame, system_state_history_df: pd.DataFrame, control_action_df: pd.DataFrame) -> pd.DataFrame: logger.debug("Requested prediction") boiler_temp, control_action_timestamp = self._unpack_control_action( control_action_df) heating_system_reaction = [] heating_obj_ids = self._timedelta_df[ column_names.HEATING_OBJ_ID].to_list() timedelta_list = self._timedelta_df[ column_names.AVG_TIMEDELTA].to_list() for obj_id, timedelta in zip(heating_obj_ids, timedelta_list): coolant_temp = self._calc_coolant_temp_for_object( obj_id, boiler_temp) heating_system_reaction.append({ column_names.TIMESTAMP: control_action_timestamp + timedelta, column_names.HEATING_OBJ_ID: obj_id, column_names.HEATING_OBJ_TYPE: self._objects_type, column_names.CIRCUIT_TYPE: self._circuit_type, column_names.FORWARD_PIPE_COOLANT_TEMP: coolant_temp, column_names.BACKWARD_PIPE_COOLANT_TEMP: None }) return pd.DataFrame(heating_system_reaction)
def load_temp_graph(self) -> pd.DataFrame: filepath = os.path.abspath(self._filepath) logger.debug(f"Loading temp graph from {filepath}") with open(filepath, mode="rb") as input_file: temp_graph_df = self._reader.read_temp_graph_from_binary_stream( input_file) return temp_graph_df
def find_lag(self, x: np.ndarray, y: np.ndarray) -> int: logger.debug("Lag calculation is requested") rounded_x_column = "rounded_x" y_column = "y" min_std = float("inf") lag = None for test_lag in range(self._min_lag, self._max_lag): x_with_lag = x[:-test_lag] y_with_lag = y[test_lag:] rounded_x = x_with_lag // self._x_round_step * self._x_round_step correlation_df = pd.DataFrame({ rounded_x_column: rounded_x, y_column: y_with_lag }) mean_series = correlation_df.groupby( rounded_x_column)[y_column].mean() y_mean_group_value = (correlation_df[rounded_x_column].replace( mean_series)).to_numpy() y_delta = y_with_lag - y_mean_group_value # noinspection PyUnresolvedReferences std_var = np.std(y_delta) if std_var < min_std: min_std = std_var lag = test_lag return lag
def __init__( self, heating_system_model: AbstractHeatingSystemModel, temp_requirements_constraint: SingleTypeHeatingObjOnWeatherConstraint, controlled_circuit_type: str = circuit_types.HEATING, min_boiler_temp: float = 30, max_boiler_temp: float = 85, min_regulation_step: float = 0.3, ) -> None: self._heating_system_model = heating_system_model self._temp_requirements_constraint = temp_requirements_constraint self._min_boiler_temp = min_boiler_temp self._max_boiler_temp = max_boiler_temp self._min_regulation_step = min_regulation_step self._controlled_circuit_type = controlled_circuit_type logger.debug( f"Created instance: " f"min boiler temp: {self._max_boiler_temp}" f"max boiler temp: {self._max_boiler_temp}" f"min regulation step: {self._min_regulation_step}" f"controlled circuit type: {self._controlled_circuit_type}" f"heating system model: {self._heating_system_model}" f"temp requirements constraint: {self._temp_requirements_constraint}" )
def __init__( self, temp_graph: pd.DataFrame, weather_temp_round_algorithm: AbstractFloatRoundAlgorithm) -> None: self._temp_graph = temp_graph self._weather_temp_round_algo = weather_temp_round_algorithm logger.debug("Creating instance")
def predict_on_weather(self, weather_df: pd.DataFrame) -> pd.DataFrame: logger.debug( f"Predicting on weather is requested; weather_df len = {len(weather_df)}" ) weather_df = self._round_weather_temp(weather_df) temp_requirements_df = self._calc_for_weather_df(weather_df) return temp_requirements_df
def __init__(self, encoding: str = "utf-8", separator: str = ";") -> None: self._encoding = encoding self._separator = separator logger.debug(f"Creating instance:" f"encoding: {encoding}" f"separator: {separator}")
def write_weather_to_binary_stream(self, binary_stream: BinaryIO, weather_df: pd.DataFrame) -> None: logger.debug("Storing weather") weather_df.to_csv(binary_stream, encoding=self._encoding, sep=self._separator, index=False)
def write_temp_graph_to_binary_stream(self, binary_stream: BinaryIO, temp_graph_df: pd.DataFrame) -> None: logger.debug("Storing temp graph") temp_graph_df.to_csv(binary_stream, encoding=self._encoding, sep=self._separator, index=False)
def _load_from_file(self): filepath = os.path.abspath(self._filepath) logger.debug(f"Loading weather from {filepath}") with open(filepath, mode="rb") as input_file: weather_df = self._reader.read_weather_from_binary_stream( input_file) return weather_df
def write_timedelta_to_binary_stream(self, binary_stream: BinaryIO, timedelta_df: pd.DataFrame) -> None: logger.debug("Storing timedelta") timedelta_df = self._convert_timedelta_to_seconds(timedelta_df) with io.TextIOWrapper(binary_stream, encoding=self._encoding) as text_stream: timedelta_df.to_csv(text_stream, index=False, sep=self._separator)
def write_heating_obj_to_binary_stream( self, binary_stream: BinaryIO, heating_obj_df: pd.DataFrame) -> None: logger.debug(f"Storing heating object; len = {len(heating_obj_df)}") heating_obj_df.to_csv(binary_stream, index=False, encoding=self._encoding, sep=self._separator)
def __init__(self, filepath: str, reader: AbstractSyncTempGraphReader) -> None: self._filepath = filepath self._reader = reader logger.debug(f"Creating instance:" f"filepath: {filepath}" f"reader: {reader}")
def round_series(self, series: pd.Series) -> pd.Series: logger.debug(f"Rounding series; len: {len(series)}") multiplier = 10 ** self._decimals rounded_series = series.copy() rounded_series = rounded_series.abs() * multiplier + 0.5 rounded_series = np.floor(rounded_series) / multiplier rounded_series = np.copysign(rounded_series, series) return rounded_series
def read_timedelta_from_binary_stream(self, binary_stream: BinaryIO ) -> pd.DataFrame: logger.debug("Loading timedelta") with io.TextIOWrapper(binary_stream, encoding=self._encoding) as text_stream: timedelta_df = pd.read_csv(text_stream, sep=self._separator) timedelta_df = self._convert_timedelta_from_seconds(timedelta_df) return timedelta_df
def load_weather(self, start_datetime: Optional[pd.Timestamp] = None, end_datetime: Optional[pd.Timestamp] = None ) -> pd.DataFrame: logger.debug("Loading weather") weather_df = self._load_from_storage() weather_df = self._filter_by_timestamp(end_datetime, start_datetime, weather_df) return weather_df
def read_weather_from_binary_stream(self, binary_stream: BinaryIO) -> pd.DataFrame: logger.debug("Loading weather") weather_df = pd.read_csv( binary_stream, encoding=self._encoding, sep=self._separator, parse_dates=[column_names.TIMESTAMP] ) return weather_df
def get_weather_start_end_timestamps( self, control_action_timestamp: pd.Timestamp ) -> Tuple[pd.Timestamp, pd.Timestamp]: logger.debug( f"Get weather start end timestamps for control action timestamp {control_action_timestamp}" ) min_timedelta = self._timedelta_df[column_names.AVG_TIMEDELTA].min() max_timedelta = self._timedelta_df[column_names.AVG_TIMEDELTA].max() return control_action_timestamp + min_timedelta, control_action_timestamp + max_timedelta
def filter_df_by_min_max_timestamp( self, df: pd.DataFrame, min_timestamp: Union[pd.Timestamp, None], max_timestamp: Union[pd.Timestamp, None]) -> pd.DataFrame: logger.debug(f"Filter range: [{min_timestamp}, {max_timestamp})") if min_timestamp is not None: df = df[df[self._timestamp_column_name] >= min_timestamp] if max_timestamp is not None: df = df[df[self._timestamp_column_name] < max_timestamp] df = df.copy() return df
def __init__( self, filter_algorithm: AbstractTimestampFilterAlgorithm = LeftClosedTimestampFilterAlgorithm( ) ) -> None: self._storage = dataset_prototypes.HEATING_OBJ.copy() self._filter_algorithm = filter_algorithm logger.debug(f"Creating instance:" f"filter algorithm: {filter_algorithm}")
def load_heating_obj( self, start_datetime: Optional[pd.Timestamp] = None, end_datetime: Optional[pd.Timestamp] = None) -> pd.DataFrame: logger.debug(f"Loading for {start_datetime},{end_datetime}") heating_object_df = self._load_from_file() heating_object_df = self._filter_by_timestamp(end_datetime, heating_object_df, start_datetime) return heating_object_df
def read_heating_obj_from_binary_stream(self, binary_stream: BinaryIO ) -> pd.DataFrame: logger.debug("Loading heating object") heating_obj_df = pd.read_csv( binary_stream, parse_dates=[column_names.TIMESTAMP], sep=self._separator, encoding=self._encoding ) return heating_obj_df
def __init__( self, timestamp_round_algo: AbstractTimestampRoundAlgorithm, interpolation_step: pd.Timedelta, ) -> None: self._interpolation_step = interpolation_step self._timestamp_round_algo = timestamp_round_algo self._timestamp_column_name = column_names.TIMESTAMP logger.debug(f"Creating instance:" f"timestamp round algo: {timestamp_round_algo}" f"interpolation step: {interpolation_step}")
def __init__(self, x_round_step: float = 0.1, min_lag: int = 1, max_lag: int = 20) -> None: self._x_round_step = x_round_step self._min_lag = min_lag self._max_lag = max_lag logger.debug(f"Creating instance:" f"x_round_step: {self._x_round_step}" f"min_lag: {self._min_lag}" f"max_lag: {self._max_lag}")
def __init__(self, min_value: Optional[float] = 0, max_value: Optional[float] = 100 ) -> None: self._min_value = min_value self._max_value = max_value logger.debug( f"Creating instance" f"min value is {min_value}" f"max value is {max_value}" )
def find_lag(self, x: np.ndarray, y: np.ndarray) -> int: logger.debug("Lag calculation is requested") x -= x.mean() x /= x.std() y -= y.mean() y /= y.std() corr = np.correlate(y, x) lag = corr.argmax() + 1 - len(y) return lag
def __init__(self, temp_correlation_df: pd.DataFrame, timedelta_df: pd.DataFrame, objects_type: str = heating_object_types.APARTMENT_HOUSE, circuit_type: str = circuit_types.HEATING) -> None: self._temp_correlation_df = temp_correlation_df self._timedelta_df = timedelta_df self._objects_type = objects_type self._circuit_type = circuit_type logger.debug(f"Creating instance:" f"objects type: {self._objects_type}" f"circuit type: {self._circuit_type}")