Mercurial > repos > jay > gaiac_box_plot
annotate gaiac_data_averaging/gaiac_dataaveraging.py @ 2:f4bde1be9ed0 draft
planemo upload for repository https://github.com/jaidevjoshi83/gaiac commit c31ef5726cd1d2659da9aeb956c22fb834b177ff
| author | jay | 
|---|---|
| date | Fri, 23 May 2025 17:53:36 +0000 | 
| parents | 0763bb545f98 | 
| children | 
| rev | line source | 
|---|---|
| 0 
0763bb545f98
planemo upload for repository https://github.com/jaidevjoshi83/gaiac.git commit c29a769ed165f313a6410925be24f776652a9663-dirty
 jay parents: diff
changeset | 1 import pandas as pd | 
| 
0763bb545f98
planemo upload for repository https://github.com/jaidevjoshi83/gaiac.git commit c29a769ed165f313a6410925be24f776652a9663-dirty
 jay parents: diff
changeset | 2 import argparse | 
| 
0763bb545f98
planemo upload for repository https://github.com/jaidevjoshi83/gaiac.git commit c29a769ed165f313a6410925be24f776652a9663-dirty
 jay parents: diff
changeset | 3 | 
| 
0763bb545f98
planemo upload for repository https://github.com/jaidevjoshi83/gaiac.git commit c29a769ed165f313a6410925be24f776652a9663-dirty
 jay parents: diff
changeset | 4 #This tool would average your time series data based on the time intervals based on the time and date column | 
| 
0763bb545f98
planemo upload for repository https://github.com/jaidevjoshi83/gaiac.git commit c29a769ed165f313a6410925be24f776652a9663-dirty
 jay parents: diff
changeset | 5 #python avg_timeseries.py -I data.tsv -C 1 -T 5 -O averaged_output.tsv | 
| 
0763bb545f98
planemo upload for repository https://github.com/jaidevjoshi83/gaiac.git commit c29a769ed165f313a6410925be24f776652a9663-dirty
 jay parents: diff
changeset | 6 | 
| 
0763bb545f98
planemo upload for repository https://github.com/jaidevjoshi83/gaiac.git commit c29a769ed165f313a6410925be24f776652a9663-dirty
 jay parents: diff
changeset | 7 | 
| 
0763bb545f98
planemo upload for repository https://github.com/jaidevjoshi83/gaiac.git commit c29a769ed165f313a6410925be24f776652a9663-dirty
 jay parents: diff
changeset | 8 def main(): | 
| 
0763bb545f98
planemo upload for repository https://github.com/jaidevjoshi83/gaiac.git commit c29a769ed165f313a6410925be24f776652a9663-dirty
 jay parents: diff
changeset | 9 parser = argparse.ArgumentParser(description="Average time series data over specified intervals.") | 
| 
0763bb545f98
planemo upload for repository https://github.com/jaidevjoshi83/gaiac.git commit c29a769ed165f313a6410925be24f776652a9663-dirty
 jay parents: diff
changeset | 10 parser.add_argument("-I", "--infile", required=True, help="Input data file (TSV format)") | 
| 
0763bb545f98
planemo upload for repository https://github.com/jaidevjoshi83/gaiac.git commit c29a769ed165f313a6410925be24f776652a9663-dirty
 jay parents: diff
changeset | 11 parser.add_argument("-C", "--dt_column", required=True, help="Column number (1-based) for the DateTime column") | 
| 
0763bb545f98
planemo upload for repository https://github.com/jaidevjoshi83/gaiac.git commit c29a769ed165f313a6410925be24f776652a9663-dirty
 jay parents: diff
changeset | 12 parser.add_argument("-T", "--time_interval", required=True, help="Time interval in minutes, e.g., '5' or '30'") | 
| 
0763bb545f98
planemo upload for repository https://github.com/jaidevjoshi83/gaiac.git commit c29a769ed165f313a6410925be24f776652a9663-dirty
 jay parents: diff
changeset | 13 parser.add_argument("-O", "--out_file", default='OutFile.tsv', help="Output file name (TSV format)") | 
| 
0763bb545f98
planemo upload for repository https://github.com/jaidevjoshi83/gaiac.git commit c29a769ed165f313a6410925be24f776652a9663-dirty
 jay parents: diff
changeset | 14 parser.add_argument("-S", "--sep", default='\t', help="deliminator") | 
| 
0763bb545f98
planemo upload for repository https://github.com/jaidevjoshi83/gaiac.git commit c29a769ed165f313a6410925be24f776652a9663-dirty
 jay parents: diff
changeset | 15 | 
| 
0763bb545f98
planemo upload for repository https://github.com/jaidevjoshi83/gaiac.git commit c29a769ed165f313a6410925be24f776652a9663-dirty
 jay parents: diff
changeset | 16 args = parser.parse_args() | 
| 
0763bb545f98
planemo upload for repository https://github.com/jaidevjoshi83/gaiac.git commit c29a769ed165f313a6410925be24f776652a9663-dirty
 jay parents: diff
changeset | 17 | 
| 
0763bb545f98
planemo upload for repository https://github.com/jaidevjoshi83/gaiac.git commit c29a769ed165f313a6410925be24f776652a9663-dirty
 jay parents: diff
changeset | 18 # Load data | 
| 
0763bb545f98
planemo upload for repository https://github.com/jaidevjoshi83/gaiac.git commit c29a769ed165f313a6410925be24f776652a9663-dirty
 jay parents: diff
changeset | 19 data = pd.read_csv(args.infile, sep=args.sep) | 
| 
0763bb545f98
planemo upload for repository https://github.com/jaidevjoshi83/gaiac.git commit c29a769ed165f313a6410925be24f776652a9663-dirty
 jay parents: diff
changeset | 20 | 
| 
0763bb545f98
planemo upload for repository https://github.com/jaidevjoshi83/gaiac.git commit c29a769ed165f313a6410925be24f776652a9663-dirty
 jay parents: diff
changeset | 21 # Extract the correct datetime column name | 
| 
0763bb545f98
planemo upload for repository https://github.com/jaidevjoshi83/gaiac.git commit c29a769ed165f313a6410925be24f776652a9663-dirty
 jay parents: diff
changeset | 22 col_index = int(args.dt_column) - 1 # Convert 1-based index to 0-based | 
| 
0763bb545f98
planemo upload for repository https://github.com/jaidevjoshi83/gaiac.git commit c29a769ed165f313a6410925be24f776652a9663-dirty
 jay parents: diff
changeset | 23 datetime_col = data.columns[col_index] | 
| 
0763bb545f98
planemo upload for repository https://github.com/jaidevjoshi83/gaiac.git commit c29a769ed165f313a6410925be24f776652a9663-dirty
 jay parents: diff
changeset | 24 | 
| 
0763bb545f98
planemo upload for repository https://github.com/jaidevjoshi83/gaiac.git commit c29a769ed165f313a6410925be24f776652a9663-dirty
 jay parents: diff
changeset | 25 # Set datetime index | 
| 
0763bb545f98
planemo upload for repository https://github.com/jaidevjoshi83/gaiac.git commit c29a769ed165f313a6410925be24f776652a9663-dirty
 jay parents: diff
changeset | 26 data[datetime_col] = pd.to_datetime(data[datetime_col], errors='coerce') | 
| 
0763bb545f98
planemo upload for repository https://github.com/jaidevjoshi83/gaiac.git commit c29a769ed165f313a6410925be24f776652a9663-dirty
 jay parents: diff
changeset | 27 data.set_index(datetime_col, inplace=True) | 
| 
0763bb545f98
planemo upload for repository https://github.com/jaidevjoshi83/gaiac.git commit c29a769ed165f313a6410925be24f776652a9663-dirty
 jay parents: diff
changeset | 28 | 
| 
0763bb545f98
planemo upload for repository https://github.com/jaidevjoshi83/gaiac.git commit c29a769ed165f313a6410925be24f776652a9663-dirty
 jay parents: diff
changeset | 29 # Group by time intervals and compute mean for numeric columns | 
| 
0763bb545f98
planemo upload for repository https://github.com/jaidevjoshi83/gaiac.git commit c29a769ed165f313a6410925be24f776652a9663-dirty
 jay parents: diff
changeset | 30 df_avg = data.resample(f'{args.time_interval}Min').mean(numeric_only=True) | 
| 
0763bb545f98
planemo upload for repository https://github.com/jaidevjoshi83/gaiac.git commit c29a769ed165f313a6410925be24f776652a9663-dirty
 jay parents: diff
changeset | 31 | 
| 
0763bb545f98
planemo upload for repository https://github.com/jaidevjoshi83/gaiac.git commit c29a769ed165f313a6410925be24f776652a9663-dirty
 jay parents: diff
changeset | 32 # Round to 3 decimals and save to output file | 
| 
0763bb545f98
planemo upload for repository https://github.com/jaidevjoshi83/gaiac.git commit c29a769ed165f313a6410925be24f776652a9663-dirty
 jay parents: diff
changeset | 33 df_avg.round(3).to_csv(args.out_file, sep='\t') | 
| 
0763bb545f98
planemo upload for repository https://github.com/jaidevjoshi83/gaiac.git commit c29a769ed165f313a6410925be24f776652a9663-dirty
 jay parents: diff
changeset | 34 | 
| 
0763bb545f98
planemo upload for repository https://github.com/jaidevjoshi83/gaiac.git commit c29a769ed165f313a6410925be24f776652a9663-dirty
 jay parents: diff
changeset | 35 print(f"Averaged data saved to {args.out_file}") | 
| 
0763bb545f98
planemo upload for repository https://github.com/jaidevjoshi83/gaiac.git commit c29a769ed165f313a6410925be24f776652a9663-dirty
 jay parents: diff
changeset | 36 | 
| 
0763bb545f98
planemo upload for repository https://github.com/jaidevjoshi83/gaiac.git commit c29a769ed165f313a6410925be24f776652a9663-dirty
 jay parents: diff
changeset | 37 if __name__ == "__main__": | 
| 
0763bb545f98
planemo upload for repository https://github.com/jaidevjoshi83/gaiac.git commit c29a769ed165f313a6410925be24f776652a9663-dirty
 jay parents: diff
changeset | 38 main() | 
