Updated get/set terminal width.

这个提交包含在:
Craig Warren
2016-08-22 18:20:12 +01:00
父节点 06cef7474a
当前提交 086f7c4443
共有 4 个文件被更改,包括 39 次插入27 次删除

查看文件

@@ -24,7 +24,6 @@ from enum import Enum
import itertools
import os
import psutil
from shutil import get_terminal_size
import sys
from time import perf_counter
@@ -46,7 +45,7 @@ from .input_cmds_singleuse import process_singlecmds
from .materials import Material, process_materials
from .pml import build_pmls
from .receivers import store_outputs
from .utilities import logo, human_size, get_machine_cpu_os
from .utilities import logo, human_size, get_machine_cpu_os, get_terminal_width
from .writer_hdf5 import write_hdf5
from .yee_cell_build import build_electric_components, build_magnetic_components
@@ -154,7 +153,7 @@ def run_std_sim(args, numbermodelruns, inputfile, usernamespace, optparams=None)
run_model(args, modelrun, numbermodelruns, inputfile, modelusernamespace)
tsimend = perf_counter()
simcompletestr = '\n=== Simulation completed in [HH:MM:SS]: {}'.format(datetime.timedelta(seconds=int(tsimend - tsimstart)))
print('{} {}\n'.format(simcompletestr, '=' * (get_terminal_size()[0] - 1 - len(simcompletestr))))
print('{} {}\n'.format(simcompletestr, '=' * (get_terminal_width() - 1 - len(simcompletestr))))
def run_benchmark_sim(args, inputfile, usernamespace):
@@ -189,7 +188,7 @@ def run_benchmark_sim(args, inputfile, usernamespace):
np.savez(os.path.splitext(inputfile)[0], threads=threads, benchtimes=benchtimes, machineID=machineIDlong, version=__version__)
simcompletestr = '\n=== Simulation completed'
print('{} {}\n'.format(simcompletestr, '=' * (get_terminal_size()[0] - 1 - len(simcompletestr))))
print('{} {}\n'.format(simcompletestr, '=' * (get_terminal_width() - 1 - len(simcompletestr))))
def run_mpi_sim(args, numbermodelruns, inputfile, usernamespace, optparams=None):
@@ -269,7 +268,7 @@ def run_mpi_sim(args, numbermodelruns, inputfile, usernamespace, optparams=None)
tsimend = perf_counter()
simcompletestr = '\n=== Simulation completed in [HH:MM:SS]: {}'.format(datetime.timedelta(seconds=int(tsimend - tsimstart)))
print('{} {}\n'.format(simcompletestr, '=' * (get_terminal_size()[0] - 1 - len(simcompletestr))))
print('{} {}\n'.format(simcompletestr, '=' * (get_terminal_width() - 1 - len(simcompletestr))))
def run_model(args, modelrun, numbermodelruns, inputfile, usernamespace):
@@ -295,7 +294,7 @@ def run_model(args, modelrun, numbermodelruns, inputfile, usernamespace):
# Normal model reading/building process; bypassed if geometry information to be reused
if 'G' not in globals():
inputfilestr = '\n--- Model {} of {}, input file: {}'.format(modelrun, numbermodelruns, inputfile)
print(Fore.GREEN + '{} {}\n'.format(inputfilestr, '-' * (get_terminal_size()[0] - 1 - len(inputfilestr))) + Style.RESET_ALL)
print(Fore.GREEN + '{} {}\n'.format(inputfilestr, '-' * (get_terminal_width() - 1 - len(inputfilestr))) + Style.RESET_ALL)
# Add the current model run to namespace that can be accessed by user in any Python code blocks in input file
usernamespace['current_model_run'] = modelrun
@@ -348,7 +347,7 @@ def run_model(args, modelrun, numbermodelruns, inputfile, usernamespace):
# Build the model, i.e. set the material properties (ID) for every edge of every Yee cell
print()
pbar = tqdm(total=2, desc='Building FDTD grid', ncols=get_terminal_size()[0] - 1, file=sys.stdout, disable=G.tqdmdisable)
pbar = tqdm(total=2, desc='Building FDTD grid', ncols=get_terminal_width() - 1, file=sys.stdout, disable=G.tqdmdisable)
build_electric_components(G.solid, G.rigidE, G.ID, G)
pbar.update()
build_magnetic_components(G.solid, G.rigidH, G.ID, G)
@@ -382,7 +381,7 @@ def run_model(args, modelrun, numbermodelruns, inputfile, usernamespace):
# If geometry information to be reused between model runs
else:
inputfilestr = '\nInput file not re-processed'
print(Fore.GREEN + '{} {}\n'.format(inputfilestr, '-' * (get_terminal_size()[0] - 1 - len(inputfilestr))))
print(Fore.GREEN + '{} {}\n'.format(inputfilestr, '-' * (get_terminal_width() - 1 - len(inputfilestr))))
# Clear arrays for field components
G.initialise_field_arrays()
@@ -417,7 +416,7 @@ def run_model(args, modelrun, numbermodelruns, inputfile, usernamespace):
for i, geometryview in enumerate(G.geometryviews):
geometryview.set_filename(modelrun, numbermodelruns, G)
geoiters = 6 * (((geometryview.xf - geometryview.xs) / geometryview.dx) * ((geometryview.yf - geometryview.ys) / geometryview.dy) * ((geometryview.zf - geometryview.zs) / geometryview.dz))
pbar = tqdm(total=geoiters, unit='byte', unit_scale=True, desc='Writing geometry file {} of {}, {}'.format(i + 1, len(G.geometryviews), os.path.split(geometryview.filename)[1]), ncols=get_terminal_size()[0] - 1, file=sys.stdout, disable=G.tqdmdisable)
pbar = tqdm(total=geoiters, unit='byte', unit_scale=True, desc='Writing geometry file {} of {}, {}'.format(i + 1, len(G.geometryviews), os.path.split(geometryview.filename)[1]), ncols=get_terminal_width() - 1, file=sys.stdout, disable=G.tqdmdisable)
geometryview.write_vtk(modelrun, numbermodelruns, G, pbar)
pbar.close()
# geometryview.write_xdmf(modelrun, numbermodelruns, G)
@@ -445,7 +444,7 @@ def run_model(args, modelrun, numbermodelruns, inputfile, usernamespace):
# Absolute time
abstime = 0
for timestep in tqdm(range(G.iterations), desc='Running simulation, model ' + str(modelrun) + ' of ' + str(numbermodelruns), ncols=get_terminal_size()[0] - 1, file=sys.stdout, disable=G.tqdmdisable):
for timestep in tqdm(range(G.iterations), desc='Running simulation, model ' + str(modelrun) + ' of ' + str(numbermodelruns), ncols=get_terminal_width() - 1, file=sys.stdout, disable=G.tqdmdisable):
# Store field component values for every receiver and transmission line
store_outputs(timestep, G.Ex, G.Ey, G.Ez, G.Hx, G.Hy, G.Hz, G)
@@ -453,7 +452,7 @@ def run_model(args, modelrun, numbermodelruns, inputfile, usernamespace):
for i, snap in enumerate(G.snapshots):
if snap.time == timestep + 1:
snapiters = 36 * (((snap.xf - snap.xs) / snap.dx) * ((snap.yf - snap.ys) / snap.dy) * ((snap.zf - snap.zs) / snap.dz))
pbar = tqdm(total=snapiters, leave=False, unit='byte', unit_scale=True, desc=' Writing snapshot file {} of {}, {}'.format(i + 1, len(G.snapshots), os.path.split(snap.filename)[1]), ncols=get_terminal_size()[0] - 1, file=sys.stdout, disable=G.tqdmdisable)
pbar = tqdm(total=snapiters, leave=False, unit='byte', unit_scale=True, desc=' Writing snapshot file {} of {}, {}'.format(i + 1, len(G.snapshots), os.path.split(snap.filename)[1]), ncols=get_terminal_width() - 1, file=sys.stdout, disable=G.tqdmdisable)
snap.write_vtk_imagedata(G.Ex, G.Ey, G.Ez, G.Hx, G.Hy, G.Hz, G, pbar)
pbar.close()

查看文件

@@ -17,7 +17,6 @@
# along with gprMax. If not, see <http://www.gnu.org/licenses/>.
import os
from shutil import get_terminal_size
import sys
import h5py
@@ -31,7 +30,7 @@ from gprMax.exceptions import CmdInputError
from gprMax.fractals import FractalSurface, FractalVolume, Grass
from gprMax.geometry_primitives import build_edge_x, build_edge_y, build_edge_z, build_face_yz, build_face_xz, build_face_xy, build_triangle, build_box, build_cylinder, build_cylindrical_sector, build_sphere, build_voxels_from_array, build_voxels_from_array_mask
from gprMax.materials import Material
from gprMax.utilities import round_value
from gprMax.utilities import round_value, get_terminal_width
def process_geometrycmds(geometry, G):
@@ -48,7 +47,7 @@ def process_geometrycmds(geometry, G):
else:
tqdmdisable = G.tqdmdisable
for object in tqdm(geometry, desc='Processing geometry related cmds', unit='cmds', ncols=get_terminal_size()[0] - 1, file=sys.stdout, disable=tqdmdisable):
for object in tqdm(geometry, desc='Processing geometry related cmds', unit='cmds', ncols=get_terminal_width() - 1, file=sys.stdout, disable=tqdmdisable):
tmp = object.split()
if tmp[0] == '#geometry_objects_file:':

查看文件

@@ -21,7 +21,6 @@ import datetime
from importlib import import_module
import os
import pickle
from shutil import get_terminal_size
from time import perf_counter
from colorama import init, Fore, Style
@@ -31,6 +30,7 @@ import numpy as np
from gprMax.constants import floattype
from gprMax.exceptions import CmdInputError
from gprMax.gprMax import run_std_sim, run_mpi_sim
from gprMax.utilities import get_terminal_width
def run_opt_sim(args, numbermodelruns, inputfile, usernamespace):
@@ -77,7 +77,7 @@ def run_opt_sim(args, numbermodelruns, inputfile, usernamespace):
OA, N, cols, k, s, t = construct_OA(optparams)
taguchistr = '\n--- Taguchi optimisation'
print('{} {}\n'.format(taguchistr, '-' * (get_terminal_size()[0] - 1 - len(taguchistr))))
print('{} {}\n'.format(taguchistr, '-' * (get_terminal_width() - 1 - len(taguchistr))))
print('Orthogonal array: {:g} experiments per iteration, {:g} parameters ({:g} will be used), {:g} levels, and strength {:g}'.format(N, cols, k, s, t))
tmp = [(k, v) for k, v in optparams.items()]
print('Parameters to optimise with ranges: {}'.format(str(tmp).strip('[]')))
@@ -120,7 +120,7 @@ def run_opt_sim(args, numbermodelruns, inputfile, usernamespace):
os.remove(outputfile)
taguchistr = '\n--- Taguchi optimisation, iteration {}: {} initial experiments with fitness values {}.'.format(iteration + 1, numbermodelruns, fitnessvalues)
print('{} {}\n'.format(taguchistr, '-' * (get_terminal_size()[0] - 1 - len(taguchistr))))
print('{} {}\n'.format(taguchistr, '-' * (get_terminal_width() - 1 - len(taguchistr))))
# Calculate optimal levels from fitness values by building a response table; update dictionary of parameters with optimal values
optparams, levelsopt = calculate_optimal_levels(optparams, levels, levelsopt, fitnessvalues, OA, N, k)
@@ -142,13 +142,13 @@ def run_opt_sim(args, numbermodelruns, inputfile, usernamespace):
os.rename(outputfile, os.path.splitext(outputfile)[0] + '_final' + str(iteration + 1) + '.out')
taguchistr = '\n--- Taguchi optimisation, iteration {} completed. History of optimal parameter values {} and of fitness values {}'.format(iteration + 1, dict(optparamshist), fitnessvalueshist)
print('{} {}\n'.format(taguchistr, '-' * (get_terminal_size()[0] - 1 - len(taguchistr))))
print('{} {}\n'.format(taguchistr, '-' * (get_terminal_width() - 1 - len(taguchistr))))
iteration += 1
# Stop optimisation if stopping criterion has been reached
if fitnessvalueshist[iteration - 1] > fitness['stop']:
taguchistr = '\n--- Taguchi optimisation stopped as fitness criteria reached: {:g} > {:g}'.format(fitnessvalueshist[iteration - 1], fitness['stop'])
print('{} {}\n'.format(taguchistr, '-' * (get_terminal_size()[0] - 1 - len(taguchistr))))
print('{} {}\n'.format(taguchistr, '-' * (get_terminal_width() - 1 - len(taguchistr))))
break
# Stop optimisation if successive fitness values are within a percentage threshold
@@ -157,7 +157,7 @@ def run_opt_sim(args, numbermodelruns, inputfile, usernamespace):
fitnessvaluesthres = 0.1
if fitnessvaluesclose < fitnessvaluesthres:
taguchistr = '\n--- Taguchi optimisation stopped as successive fitness values within {}%'.format(fitnessvaluesthres)
print('{} {}\n'.format(taguchistr, '-' * (get_terminal_size()[0] - 1 - len(taguchistr))))
print('{} {}\n'.format(taguchistr, '-' * (get_terminal_width() - 1 - len(taguchistr))))
break
tsimend = perf_counter()
@@ -170,7 +170,7 @@ def run_opt_sim(args, numbermodelruns, inputfile, usernamespace):
pickle.dump(optparamsinit, f)
taguchistr = '\n=== Taguchi optimisation completed in [HH:MM:SS]: {} after {} iteration(s)'.format(datetime.timedelta(seconds=int(tsimend - tsimstart)), iteration)
print('{} {}\n'.format(taguchistr, '=' * (get_terminal_size()[0] - 1 - len(taguchistr))))
print('{} {}\n'.format(taguchistr, '=' * (get_terminal_width() - 1 - len(taguchistr))))
print('History of optimal parameter values {} and of fitness values {}\n'.format(dict(optparamshist), fitnessvalueshist))

查看文件

@@ -28,6 +28,20 @@ from colorama import init, Fore, Style
init()
def get_terminal_width():
"""Get/set width of terminal being used.
Returns:
terminalwidth (int): Terminal width
"""
terminalwidth = get_terminal_size()[0]
if terminalwidth == 0:
terminalwidth = 100
return(terminalwidth)
def logo(version):
"""Print gprMax logo, version, and licencing/copyright information.
@@ -50,14 +64,14 @@ def logo(version):
|___/|_|
v""" + version
print('{} {}\n'.format(description, '=' * (get_terminal_size()[0] - len(description) - 1)))
print('{} {}\n'.format(description, '=' * (get_terminal_width() - len(description) - 1)))
print(Fore.CYAN + '{}\n'.format(logo))
print(Style.RESET_ALL + textwrap.fill(copyright, width=get_terminal_size()[0] - 1, initial_indent=' '))
print(textwrap.fill(authors, width=get_terminal_size()[0] - 1, initial_indent=' '))
print(Style.RESET_ALL + textwrap.fill(copyright, width=get_terminal_width() - 1, initial_indent=' '))
print(textwrap.fill(authors, width=get_terminal_width() - 1, initial_indent=' '))
print()
print(textwrap.fill(licenseinfo1, width=get_terminal_size()[0] - 1, initial_indent=' ', subsequent_indent=' '))
print(textwrap.fill(licenseinfo2, width=get_terminal_size()[0] - 1, initial_indent=' ', subsequent_indent=' '))
print(textwrap.fill(licenseinfo3, width=get_terminal_size()[0] - 1, initial_indent=' ', subsequent_indent=' '))
print(textwrap.fill(licenseinfo1, width=get_terminal_width() - 1, initial_indent=' ', subsequent_indent=' '))
print(textwrap.fill(licenseinfo2, width=get_terminal_width() - 1, initial_indent=' ', subsequent_indent=' '))
print(textwrap.fill(licenseinfo3, width=get_terminal_width() - 1, initial_indent=' ', subsequent_indent=' '))
def round_value(value, decimalplaces=0):