diff --git a/mesures/p2d.py b/mesures/p2d.py index 81ff136..faa70ab 100644 --- a/mesures/p2d.py +++ b/mesures/p2d.py @@ -5,6 +5,7 @@ from mesures.utils import check_line_terminator_param from zipfile import ZipFile import os +import pandas as pd class P2D(P2): @@ -41,6 +42,45 @@ def zip_filename(self): version=self.version ) + def reader(self, filepath): + if isinstance(filepath, str): + df = pd.read_csv(filepath, sep=';', names=self.columns) + elif isinstance(filepath, list): + df = pd.DataFrame(data=filepath) + else: + raise Exception("Filepath must be an str or a list") + + df['tipo_medida'] = 11 + df.groupby( + ['cups', 'tipo_medida', 'timestamp', 'season', 'method'] + ).aggregate( + {'ai': 'sum', + 'ae': 'sum', + 'r1': 'sum', + 'r2': 'sum', + 'r3': 'sum', + 'r4': 'sum'} + ) + + df['timestamp'] = df['timestamp'].apply(lambda x: x.strftime(DATETIME_MASK)) + + df['method'] = 1 + df['res'] = 0 + df['res2'] = 0 + + df['ai'] = df['ai'].astype('int') + df['ae'] = df['ae'].astype('int') + df['r1'] = df['r1'].astype('int') + df['r2'] = df['r2'].astype('int') + df['r3'] = df['r3'].astype('int') + df['r4'] = df['r4'].astype('int') + + for key in self.columns: + if 'quality' in key and key not in df: + df[key] = 0 + df = df[self.columns] + return df + def writer(self): """ P2D contains all curve measure in one file diff --git a/spec/generation_files_spec.py b/spec/generation_files_spec.py index c5a536f..ce69bfd 100644 --- a/spec/generation_files_spec.py +++ b/spec/generation_files_spec.py @@ -35,6 +35,7 @@ import numpy as np import zipfile +DELETE_FILES = 'rm -rf *.bz2 *.zip *.[0123456789]' class SampleData: @staticmethod @@ -118,10 +119,10 @@ def get_sample_p2d_data(): data_p2d = [basic_p2d.copy()] - ts = "2022-01-01 01:00:00" - for x in range(50): + ts = "2022-01-01 00:15:00" + for x in range(360): datas = basic_p2d.copy() - ts = (datetime.strptime(ts, '%Y-%m-%d %H:%M:%S') + timedelta(hours=1)).strftime('%Y-%m-%d %H:%M:%S') + ts = (datetime.strptime(ts, '%Y-%m-%d %H:%M:%S') + timedelta(minutes=15)).strftime('%Y-%m-%d %H:%M:%S') ai = randint(0, 5000) ae = randint(0, 2) r1 = randint(0, 30) @@ -132,10 +133,10 @@ def get_sample_p2d_data(): data_p2d.append(datas) cups = "ES0012345678923456780F" - ts = "2022-01-01 00:00:00" - for x in range(70): + ts = "2022-01-01 00:15:00" + for x in range(360): datas = basic_p2d.copy() - ts = (datetime.strptime(ts, '%Y-%m-%d %H:%M:%S') + timedelta(hours=1)).strftime('%Y-%m-%d %H:%M:%S') + ts = (datetime.strptime(ts, '%Y-%m-%d %H:%M:%S') + timedelta(minutes=15)).strftime('%Y-%m-%d %H:%M:%S') ai = randint(0, 5000) ae = randint(0, 2) r1 = randint(0, 30) @@ -903,7 +904,7 @@ def get_sample_cups45_data(): f1 = f.writer() assert isinstance(f1, str) -with description('A P2D'): +with fdescription('A P2D'): with it('is instance of P2D Class'): data = SampleData().get_sample_p2d_data() f = P2D(data)