Skip to content
Snippets Groups Projects
Commit da728d61 authored by Mikhail Karnevskiy's avatar Mikhail Karnevskiy
Browse files

code refactoring

parent 18bb8eda
No related branches found
No related tags found
1 merge request!68Add title page. Modify conf.py.
from collections import OrderedDict from collections import OrderedDict
import glob from glob import glob
from importlib.machinery import SourceFileLoader from importlib.machinery import SourceFileLoader
import os from os import chdir, listdir, makedirs, path, remove, stat
from os.path import isfile, isdir, splitext from os.path import isdir, isfile, splitext
from queue import Queue from queue import Queue
from shutil import copy, move, rmtree, copytree from shutil import copy, copytree, move, rmtree
from subprocess import check_output, check_call, CalledProcessError from subprocess import CalledProcessError, check_call, check_output
import textwrap from textwrap import dedent
from time import sleep from time import sleep
from jinja2 import Template from jinja2 import Template
...@@ -14,28 +14,29 @@ import tabulate ...@@ -14,28 +14,29 @@ import tabulate
from xfel_calibrate import settings from xfel_calibrate import settings
def atoi(text): def atoi(text):
''' """
Convert string to integer is possible Convert string to integer is possible
:param text: string to be converted :param text: string to be converted
:return: integer value or input string :return: integer value or input string
''' """
return int(text) if text.isdigit() else text return int(text) if text.isdigit() else text
def natural_keys(text): def natural_keys(text):
''' """
Decompose string to list of integers and sub-strings Decompose string to list of integers and sub-strings
''' """
return [ atoi(c) for c in re.split(r'(\d+)', text) ] return [atoi(c) for c in re.split(r'(\d+)', text)]
def combine_report(run_path, calibration): def combine_report(run_path, calibration):
sphinx_path = "{}/sphinx_rep".format(os.path.abspath(run_path)) sphinx_path = "{}/sphinx_rep".format(path.abspath(run_path))
os.makedirs(sphinx_path) makedirs(sphinx_path)
direntries = os.listdir(run_path) direntries = listdir(run_path)
direntries.sort(key=natural_keys) direntries.sort(key=natural_keys)
for entry in direntries: for entry in direntries:
...@@ -90,20 +91,20 @@ def prepare_plots(run_path, threshold=1000000): ...@@ -90,20 +91,20 @@ def prepare_plots(run_path, threshold=1000000):
:param threshold: Max svg file size (in bytes) to be converted to pdf :param threshold: Max svg file size (in bytes) to be converted to pdf
""" """
print('Convert svg to pdf and png') print('Convert svg to pdf and png')
run_path = os.path.abspath(run_path) run_path = path.abspath(run_path)
rst_files = glob.glob('{}/*rst'.format(run_path)) rst_files = glob('{}/*rst'.format(run_path))
for rst_file in rst_files: for rst_file in rst_files:
rst_file_name = os.path.basename(rst_file) rst_file_name = path.basename(rst_file)
rst_file_name = os.path.splitext(rst_file_name)[0] rst_file_name = path.splitext(rst_file_name)[0]
svg_files = glob.glob( svg_files = glob(
'{}/{}_files/*svg'.format(run_path, rst_file_name)) '{}/{}_files/*svg'.format(run_path, rst_file_name))
for f_path in svg_files: for f_path in svg_files:
f_name = os.path.basename(f_path) f_name = path.basename(f_path)
f_name = os.path.splitext(f_name)[0] f_name = path.splitext(f_name)[0]
if (os.stat(f_path)).st_size < threshold: if (stat(f_path)).st_size < threshold:
check_call(["svg2pdf", "{}".format(f_path)], shell=False) check_call(["svg2pdf", "{}".format(f_path)], shell=False)
new_ext = 'pdf' new_ext = 'pdf'
else: else:
...@@ -126,7 +127,7 @@ def make_timing_summary(run_path, joblist): ...@@ -126,7 +127,7 @@ def make_timing_summary(run_path, joblist):
:param joblist: List of slurm jobs :param joblist: List of slurm jobs
""" """
print('Prepare timing summary') print('Prepare timing summary')
run_path = os.path.abspath(run_path) run_path = path.abspath(run_path)
pars_vals = [] pars_vals = []
pars = 'JobID,Elapsed,Suspended' pars = 'JobID,Elapsed,Suspended'
pars_name = pars.split(',') pars_name = pars.split(',')
...@@ -135,10 +136,10 @@ def make_timing_summary(run_path, joblist): ...@@ -135,10 +136,10 @@ def make_timing_summary(run_path, joblist):
out = check_output(['sacct', '-j', job, out = check_output(['sacct', '-j', job,
'--format={}'.format(pars)], '--format={}'.format(pars)],
shell=False) shell=False)
l = str(out).split('\\n') lines = str(out).split('\\n')
# loop over output lines, skip first two lines with header. # loop over output lines, skip first two lines with header.
for line in l[2:]: for line in lines[2:]:
s = line.split() s = line.split()
if len(s) == len(pars_name): if len(s) == len(pars_name):
pars_vals.append(s) pars_vals.append(s)
...@@ -158,7 +159,7 @@ def make_timing_summary(run_path, joblist): ...@@ -158,7 +159,7 @@ def make_timing_summary(run_path, joblist):
table = tabulate.tabulate(pars_vals, tablefmt='latex', table = tabulate.tabulate(pars_vals, tablefmt='latex',
headers=pars_name) headers=pars_name)
gfile.write(textwrap.dedent(tmpl.render(table=table.split('\n')))) gfile.write(dedent(tmpl.render(table=table.split('\n'))))
def make_report(run_path, tmp_path, out_path, project, author, version, def make_report(run_path, tmp_path, out_path, project, author, version,
...@@ -178,8 +179,8 @@ def make_report(run_path, tmp_path, out_path, project, author, version, ...@@ -178,8 +179,8 @@ def make_report(run_path, tmp_path, out_path, project, author, version,
:param version: Version of the notebook :param version: Version of the notebook
:param report_to: Name or path of the report file :param report_to: Name or path of the report file
""" """
run_path = os.path.abspath(run_path) run_path = path.abspath(run_path)
report_path, report_name = os.path.split(report_to) report_path, report_name = path.split(report_to)
if report_path != '': if report_path != '':
out_path = report_path out_path = report_path
...@@ -201,7 +202,7 @@ def make_report(run_path, tmp_path, out_path, project, author, version, ...@@ -201,7 +202,7 @@ def make_report(run_path, tmp_path, out_path, project, author, version,
"Generated simple index.rst instead") "Generated simple index.rst instead")
# quickbuild went well we need to edit the index.rst and conf.py files # quickbuild went well we need to edit the index.rst and conf.py files
module_path = "{}".format(os.path.abspath(os.path.dirname(__file__))) module_path = "{}".format(path.abspath(path.dirname(__file__)))
conf = SourceFileLoader("conf", conf = SourceFileLoader("conf",
"{}/conf.py".format(run_path)).load_module() "{}/conf.py".format(run_path)).load_module()
...@@ -226,10 +227,10 @@ def make_report(run_path, tmp_path, out_path, project, author, version, ...@@ -226,10 +227,10 @@ def make_report(run_path, tmp_path, out_path, project, author, version,
mf.write(tmpl.format(var, v)) mf.write(tmpl.format(var, v))
os.remove("{}/conf.py".format(run_path)) remove("{}/conf.py".format(run_path))
move("{}/conf.py.tmp".format(run_path), "{}/conf.py".format(run_path)) move("{}/conf.py.tmp".format(run_path), "{}/conf.py".format(run_path))
direntries = os.listdir(run_path) direntries = listdir(run_path)
files_to_handle = [] files_to_handle = []
for entry in direntries: for entry in direntries:
if isfile("{}/{}".format(run_path, entry)): if isfile("{}/{}".format(run_path, entry)):
...@@ -249,10 +250,10 @@ def make_report(run_path, tmp_path, out_path, project, author, version, ...@@ -249,10 +250,10 @@ def make_report(run_path, tmp_path, out_path, project, author, version,
''') ''')
with open("{}/index.rst".format(run_path), "w+") as mf: with open("{}/index.rst".format(run_path), "w+") as mf:
mf.write(textwrap.dedent(index_tmp.render(keys=files_to_handle))) mf.write(dedent(index_tmp.render(keys=files_to_handle)))
# finally call the make scripts # finally call the make scripts
os.chdir(run_path) chdir(run_path)
try: try:
check_call(["make", "latexpdf"]) check_call(["make", "latexpdf"])
...@@ -262,7 +263,7 @@ def make_report(run_path, tmp_path, out_path, project, author, version, ...@@ -262,7 +263,7 @@ def make_report(run_path, tmp_path, out_path, project, author, version,
"can be inspected at: {}".format(run_path)) "can be inspected at: {}".format(run_path))
return return
print("Moving report to final location: {}".format(out_path)) print("Moving report to final location: {}".format(out_path))
for f in glob.glob(r'{}/_build/latex/*.pdf'.format(run_path)): for f in glob(r'{}/_build/latex/*.pdf'.format(run_path)):
copy(f, out_path) copy(f, out_path)
print("Removing temporary files at: {}".format(tmp_path)) print("Removing temporary files at: {}".format(tmp_path))
rmtree(tmp_path) rmtree(tmp_path)
...@@ -276,13 +277,13 @@ def make_titlepage(sphinx_path, project, data_path): ...@@ -276,13 +277,13 @@ def make_titlepage(sphinx_path, project, data_path):
:param project: title of the project :param project: title of the project
:param data_path: path to input data sample used for notebook :param data_path: path to input data sample used for notebook
""" """
module_path = "{}".format(os.path.abspath(os.path.dirname(__file__))) module_path = "{}".format(path.abspath(path.dirname(__file__)))
with open('{}/titlepage.tmpl'.format(module_path)) as file_: with open('{}/titlepage.tmpl'.format(module_path)) as file_:
title_tmp = Template(file_.read()) title_tmp = Template(file_.read())
with open("{}/titlepage.tex.txt".format(sphinx_path), "w+") as mf: with open("{}/titlepage.tex.txt".format(sphinx_path), "w+") as mf:
mf.write(textwrap.dedent(title_tmp.render(project=project, mf.write(dedent(title_tmp.render(project=project,
data_path=data_path))) data_path=data_path)))
def finalize(joblist, finaljob, run_path, out_path, project, calibration, def finalize(joblist, finaljob, run_path, out_path, project, calibration,
...@@ -354,7 +355,7 @@ def map_modules_from_files(filelist, file_inset, quadrants, modules_per_quad): ...@@ -354,7 +355,7 @@ def map_modules_from_files(filelist, file_inset, quadrants, modules_per_quad):
if file_infix in file: if file_infix in file:
module_files[name].put(file) module_files[name].put(file)
total_sequences += 1 total_sequences += 1
total_file_size += os.path.getsize(file) total_file_size += path.getsize(file)
return module_files, mod_ids, total_sequences, total_file_size return module_files, mod_ids, total_sequences, total_file_size
...@@ -365,19 +366,19 @@ def gain_map_files(in_folder, runs, sequences, file_inset, quadrants, modules_pe ...@@ -365,19 +366,19 @@ def gain_map_files(in_folder, runs, sequences, file_inset, quadrants, modules_pe
gain_mapped_files = OrderedDict() gain_mapped_files = OrderedDict()
for gain, run in runs.items(): for gain, run in runs.items():
ginfolder = "{}/{}".format(in_folder, run) ginfolder = "{}/{}".format(in_folder, run)
dirlist = os.listdir(ginfolder) dirlist = listdir(ginfolder)
file_list = [] file_list = []
for entry in dirlist: for entry in dirlist:
#only h5 file #only h5 file
abs_entry = "{}/{}".format(ginfolder, entry) abs_entry = "{}/{}".format(ginfolder, entry)
if os.path.isfile(abs_entry) and os.path.splitext(abs_entry)[1] == ".h5": if path.isfile(abs_entry) and path.splitext(abs_entry)[1] == ".h5":
if sequences is None: if sequences is None:
file_list.append(abs_entry) file_list.append(abs_entry)
else: else:
for seq in sequences: for seq in sequences:
if "{:05d}.h5".format(seq) in abs_entry: if "{:05d}.h5".format(seq) in abs_entry:
file_list.append(os.path.abspath(abs_entry)) file_list.append(path.abspath(abs_entry))
mapped_files, mod_ids, seq, fs = map_modules_from_files(file_list, file_inset, mapped_files, mod_ids, seq, fs = map_modules_from_files(file_list, file_inset,
quadrants, modules_per_quad) quadrants, modules_per_quad)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment