send_results.py 1.61 KB
Newer Older
Agata Malgorzata Chadaj's avatar
wip    
Agata Malgorzata Chadaj committed
1
"""This script is a workaround as we didn't manage to install papermill as script in the Jenkins pipeline"""
Agata Malgorzata Chadaj's avatar
wip    
Agata Malgorzata Chadaj committed
2
from pathlib import Path
Agata Malgorzata Chadaj's avatar
wip    
Agata Malgorzata Chadaj committed
3
import pandas as pd
Agata Malgorzata Chadaj's avatar
wip    
Agata Malgorzata Chadaj committed
4
5
import argparse

Agata Malgorzata Chadaj's avatar
wip    
Agata Malgorzata Chadaj committed
6
import nbformat
Agata Malgorzata Chadaj's avatar
wip    
Agata Malgorzata Chadaj committed
7
from lhcsmapi import Time
Agata Malgorzata Chadaj's avatar
wip    
Agata Malgorzata Chadaj committed
8
from lhcsmnb import outputs, parameters
Agata Malgorzata Chadaj's avatar
wip    
Agata Malgorzata Chadaj committed
9
10
11
12
13
14
15
16
17
18
19
20
21

def _get_arg_parser():
    """Specifies script arguments. Returns parser.
    """
    parser = argparse.ArgumentParser(description='Runs notebook with papermill.')
    parser.add_argument('notebook')
    parser.add_argument('circuit_name')
    parser.add_argument('discharge_level')
    parser.add_argument('start_time')
    parser.add_argument('end_time')
    return parser


Agata Malgorzata Chadaj's avatar
wip    
Agata Malgorzata Chadaj committed
22
23
24
25
26
def get_result():
    result_path = Path("./output/result.ipynb")
    result_nb = nbformat.read(result_path, as_version=4)
    nb = outputs.NotebookOutput(result_nb)
    try:
Agata Malgorzata Chadaj's avatar
wip    
Agata Malgorzata Chadaj committed
27
        summary = nb.get_cells_by_tag('result')[0].get_cell_output(outputs.CellOutputType.STDOUT)[0]
Agata Malgorzata Chadaj's avatar
wip    
Agata Malgorzata Chadaj committed
28
        result_flag = nb.get_cells_by_tag('result')[1].get_cell_output(outputs.CellOutputType.STDOUT)[0]
Agata Malgorzata Chadaj's avatar
wip    
Agata Malgorzata Chadaj committed
29
    except IndexError:
Agata Malgorzata Chadaj's avatar
wip    
Agata Malgorzata Chadaj committed
30
        return 'FAILED', ''
Agata Malgorzata Chadaj's avatar
wip    
Agata Malgorzata Chadaj committed
31
    return result_flag, summary
Agata Malgorzata Chadaj's avatar
wip    
Agata Malgorzata Chadaj committed
32
33
34


def save_json_to_file(args, flag, comment):
Agata Malgorzata Chadaj's avatar
wip    
Agata Malgorzata Chadaj committed
35
    file_path = '/eos/home-m/mpesoft/' + ':'.join(args.notebook, args.circuit_name, args.discharge_level, Time.to_unix_timestamp(args.start_time), Time.to_unix_timestamp(args.end_time), flag) + '.csv'
Agata Malgorzata Chadaj's avatar
wip    
Agata Malgorzata Chadaj committed
36
    df = pd.DataFrame(eval(comment))
Agata Malgorzata Chadaj's avatar
wip    
Agata Malgorzata Chadaj committed
37
38
    with open(file_path, 'a') as f:
        df.to_csv(f, mode='a', header=f.tell() == 0)
Agata Malgorzata Chadaj's avatar
wip    
Agata Malgorzata Chadaj committed
39

Agata Malgorzata Chadaj's avatar
wip    
Agata Malgorzata Chadaj committed
40

Agata Malgorzata Chadaj's avatar
wip    
Agata Malgorzata Chadaj committed
41
42
if __name__ == '__main__':
    args = _get_arg_parser().parse_args()
Agata Malgorzata Chadaj's avatar
wip    
Agata Malgorzata Chadaj committed
43
44
    print('args: ')
    print(args)
Agata Malgorzata Chadaj's avatar
wip    
Agata Malgorzata Chadaj committed
45
46
    flag, text = get_result()
    save_json_to_file(args, flag, text)
Agata Malgorzata Chadaj's avatar
wip    
Agata Malgorzata Chadaj committed
47