Mercurial > repos > bimib > cobraxy
comparison COBRAxy/custom_data_generator.py @ 377:f4b83b3a3486 draft
Uploaded
| author | francesco_lapi |
|---|---|
| date | Fri, 05 Sep 2025 06:16:00 +0000 |
| parents | 130043620f8d |
| children | 0a3ca20848f3 |
comparison
equal
deleted
inserted
replaced
| 376:a4db8d63dd98 | 377:f4b83b3a3486 |
|---|---|
| 190 writer.writeheader() | 190 writer.writeheader() |
| 191 | 191 |
| 192 for key, value in data.items(): | 192 for key, value in data.items(): |
| 193 writer.writerow({ fieldNames[0] : key, fieldNames[1] : value }) | 193 writer.writerow({ fieldNames[0] : key, fieldNames[1] : value }) |
| 194 | 194 |
| 195 def save_as_tabular_df(df: pd.DataFrame, path: str) -> None: | |
| 196 try: | |
| 197 os.makedirs(os.path.dirname(path) or ".", exist_ok=True) | |
| 198 df.to_csv(path, sep="\t", index=False) | |
| 199 except Exception as e: | |
| 200 raise utils.DataErr(path, f"failed writing tabular output: {e}") | |
| 201 | |
| 202 def save_as_xlsx_df(df: pd.DataFrame, path: str) -> None: | |
| 203 try: | |
| 204 if not path.lower().endswith(".xlsx"): | |
| 205 path += ".xlsx" | |
| 206 os.makedirs(os.path.dirname(path) or ".", exist_ok=True) | |
| 207 df.to_excel(path, index=False) | |
| 208 except Exception as e: | |
| 209 raise utils.DataErr(path, f"failed writing xlsx output: {e}") | |
| 210 | |
| 195 ###############################- ENTRY POINT -################################ | 211 ###############################- ENTRY POINT -################################ |
| 196 def main(args:List[str] = None) -> None: | 212 def main(args:List[str] = None) -> None: |
| 197 """ | 213 """ |
| 198 Initializes everything and sets the program in motion based on the fronted input arguments. | 214 Initializes everything and sets the program in motion based on the fronted input arguments. |
| 199 | 215 |
| 257 #merged.to_csv(out_file, sep = '\t', index = False) | 273 #merged.to_csv(out_file, sep = '\t', index = False) |
| 258 | 274 |
| 259 | 275 |
| 260 #### | 276 #### |
| 261 | 277 |
| 278 # write only the requested output | |
| 262 if ARGS.output_format == "xlsx": | 279 if ARGS.output_format == "xlsx": |
| 263 if not ARGS.out_xlsx: | 280 if not ARGS.out_xlsx: |
| 264 raise utils.ArgsErr("out_xlsx", "output path (--out_xlsx) is required when output_format == xlsx", ARGS.out_xlsx) | 281 raise utils.ArgsErr("out_xlsx", "output path (--out_xlsx) is required when output_format == xlsx", ARGS.out_xlsx) |
| 265 if not ARGS.out_xlsx.lower().endswith(".xlsx"): | 282 save_as_xlsx_df(merged, ARGS.out_xlsx) |
| 266 ARGS.out_xlsx += ".xlsx" | 283 expected = ARGS.out_xlsx |
| 267 merged.to_excel(ARGS.out_xlsx, index=False, sheet_name='Sheet_name_1') | |
| 268 else: | 284 else: |
| 269 if not ARGS.out_tabular: | 285 if not ARGS.out_tabular: |
| 270 raise utils.ArgsErr("out_tabular", "output path (--out_tabular) is required when output_format == tabular", ARGS.out_tabular) | 286 raise utils.ArgsErr("out_tabular", "output path (--out_tabular) is required when output_format == tabular", ARGS.out_tabular) |
| 271 merged.to_csv(ARGS.out_tabular, sep="\t", index=False) | 287 save_as_tabular_df(merged, ARGS.out_tabular) |
| 288 expected = ARGS.out_tabular | |
| 289 | |
| 290 # verify output exists and non-empty | |
| 291 if not expected or not os.path.exists(expected) or os.path.getsize(expected) == 0: | |
| 292 raise utils.DataErr(expected, "Output non creato o vuoto") | |
| 272 | 293 |
| 273 print("CustomDataGenerator: completed successfully") | 294 print("CustomDataGenerator: completed successfully") |
| 274 | 295 |
| 275 if __name__ == '__main__': | 296 if __name__ == '__main__': |
| 276 main() | 297 main() |
