Mercurial > repos > jay > gaiac_precision
annotate gaiac_data_averaging/gaiac_dataaveraging.py @ 3:1f9e67edde6a draft default tip
planemo upload for repository https://github.com/jaidevjoshi83/gaiac commit e9587f93346c7b55e1be00bad5844bf2db3ed03d-dirty
| author | jay | 
|---|---|
| date | Thu, 10 Jul 2025 19:42:02 +0000 | 
| parents | 287d6cc86582 | 
| children | 
| rev | line source | 
|---|---|
| 
0
 
287d6cc86582
planemo upload for repository https://github.com/jaidevjoshi83/gaiac.git commit c29a769ed165f313a6410925be24f776652a9663-dirty
 
jay 
parents:  
diff
changeset
 | 
1 import pandas as pd | 
| 
 
287d6cc86582
planemo upload for repository https://github.com/jaidevjoshi83/gaiac.git commit c29a769ed165f313a6410925be24f776652a9663-dirty
 
jay 
parents:  
diff
changeset
 | 
2 import argparse | 
| 
 
287d6cc86582
planemo upload for repository https://github.com/jaidevjoshi83/gaiac.git commit c29a769ed165f313a6410925be24f776652a9663-dirty
 
jay 
parents:  
diff
changeset
 | 
3 | 
| 
 
287d6cc86582
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 | 
| 
 
287d6cc86582
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 | 
| 
 
287d6cc86582
planemo upload for repository https://github.com/jaidevjoshi83/gaiac.git commit c29a769ed165f313a6410925be24f776652a9663-dirty
 
jay 
parents:  
diff
changeset
 | 
6 | 
| 
 
287d6cc86582
planemo upload for repository https://github.com/jaidevjoshi83/gaiac.git commit c29a769ed165f313a6410925be24f776652a9663-dirty
 
jay 
parents:  
diff
changeset
 | 
7 | 
| 
 
287d6cc86582
planemo upload for repository https://github.com/jaidevjoshi83/gaiac.git commit c29a769ed165f313a6410925be24f776652a9663-dirty
 
jay 
parents:  
diff
changeset
 | 
8 def main(): | 
| 
 
287d6cc86582
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.") | 
| 
 
287d6cc86582
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)") | 
| 
 
287d6cc86582
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") | 
| 
 
287d6cc86582
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'") | 
| 
 
287d6cc86582
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)") | 
| 
 
287d6cc86582
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") | 
| 
 
287d6cc86582
planemo upload for repository https://github.com/jaidevjoshi83/gaiac.git commit c29a769ed165f313a6410925be24f776652a9663-dirty
 
jay 
parents:  
diff
changeset
 | 
15 | 
| 
 
287d6cc86582
planemo upload for repository https://github.com/jaidevjoshi83/gaiac.git commit c29a769ed165f313a6410925be24f776652a9663-dirty
 
jay 
parents:  
diff
changeset
 | 
16 args = parser.parse_args() | 
| 
 
287d6cc86582
planemo upload for repository https://github.com/jaidevjoshi83/gaiac.git commit c29a769ed165f313a6410925be24f776652a9663-dirty
 
jay 
parents:  
diff
changeset
 | 
17 | 
| 
 
287d6cc86582
planemo upload for repository https://github.com/jaidevjoshi83/gaiac.git commit c29a769ed165f313a6410925be24f776652a9663-dirty
 
jay 
parents:  
diff
changeset
 | 
18 # Load data | 
| 
 
287d6cc86582
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) | 
| 
 
287d6cc86582
planemo upload for repository https://github.com/jaidevjoshi83/gaiac.git commit c29a769ed165f313a6410925be24f776652a9663-dirty
 
jay 
parents:  
diff
changeset
 | 
20 | 
| 
 
287d6cc86582
planemo upload for repository https://github.com/jaidevjoshi83/gaiac.git commit c29a769ed165f313a6410925be24f776652a9663-dirty
 
jay 
parents:  
diff
changeset
 | 
21 # Extract the correct datetime column name | 
| 
 
287d6cc86582
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 | 
| 
 
287d6cc86582
planemo upload for repository https://github.com/jaidevjoshi83/gaiac.git commit c29a769ed165f313a6410925be24f776652a9663-dirty
 
jay 
parents:  
diff
changeset
 | 
23 datetime_col = data.columns[col_index] | 
| 
 
287d6cc86582
planemo upload for repository https://github.com/jaidevjoshi83/gaiac.git commit c29a769ed165f313a6410925be24f776652a9663-dirty
 
jay 
parents:  
diff
changeset
 | 
24 | 
| 
 
287d6cc86582
planemo upload for repository https://github.com/jaidevjoshi83/gaiac.git commit c29a769ed165f313a6410925be24f776652a9663-dirty
 
jay 
parents:  
diff
changeset
 | 
25 # Set datetime index | 
| 
 
287d6cc86582
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') | 
| 
 
287d6cc86582
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) | 
| 
 
287d6cc86582
planemo upload for repository https://github.com/jaidevjoshi83/gaiac.git commit c29a769ed165f313a6410925be24f776652a9663-dirty
 
jay 
parents:  
diff
changeset
 | 
28 | 
| 
 
287d6cc86582
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 | 
| 
 
287d6cc86582
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) | 
| 
 
287d6cc86582
planemo upload for repository https://github.com/jaidevjoshi83/gaiac.git commit c29a769ed165f313a6410925be24f776652a9663-dirty
 
jay 
parents:  
diff
changeset
 | 
31 | 
| 
 
287d6cc86582
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 | 
| 
 
287d6cc86582
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') | 
| 
 
287d6cc86582
planemo upload for repository https://github.com/jaidevjoshi83/gaiac.git commit c29a769ed165f313a6410925be24f776652a9663-dirty
 
jay 
parents:  
diff
changeset
 | 
34 | 
| 
 
287d6cc86582
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}") | 
| 
 
287d6cc86582
planemo upload for repository https://github.com/jaidevjoshi83/gaiac.git commit c29a769ed165f313a6410925be24f776652a9663-dirty
 
jay 
parents:  
diff
changeset
 | 
36 | 
| 
 
287d6cc86582
planemo upload for repository https://github.com/jaidevjoshi83/gaiac.git commit c29a769ed165f313a6410925be24f776652a9663-dirty
 
jay 
parents:  
diff
changeset
 | 
37 if __name__ == "__main__": | 
| 
 
287d6cc86582
planemo upload for repository https://github.com/jaidevjoshi83/gaiac.git commit c29a769ed165f313a6410925be24f776652a9663-dirty
 
jay 
parents:  
diff
changeset
 | 
38 main() | 
