Skip to content

Commit

Permalink
Merge pull request #521 from gammasim/pylint
Browse files Browse the repository at this point in the history
Pylint updates
  • Loading branch information
GernotMaier authored Aug 18, 2023
2 parents c0bc562 + 51f1bec commit 7db86f0
Show file tree
Hide file tree
Showing 41 changed files with 1,090 additions and 187 deletions.
539 changes: 539 additions & 0 deletions pyproject.toml

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions simtools/_dev_version/scm_version.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@
from setuptools_scm import get_version

version = get_version(root=pth.join("..", ".."), relative_to=__file__)
except Exception:
raise ImportError("setuptools_scm broken or not installed")
except Exception as exc:
raise ImportError("setuptools_scm broken or not installed") from exc
4 changes: 2 additions & 2 deletions simtools/applications/add_file_to_db.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ def main():

db = db_handler.DatabaseHandler(mongo_db_config=db_config)

files_to_insert = list()
files_to_insert = []
if args_dict.get("file_name", None) is not None:
for file_now in args_dict["file_name"]:
if Path(file_now).suffix in db.ALLOWED_FILE_EXTENSIONS:
Expand All @@ -133,7 +133,7 @@ def main():
plural = "s"
if len(files_to_insert) < 1:
raise ValueError("No files were provided to upload")
elif len(files_to_insert) == 1:
if len(files_to_insert) == 1:
plural = ""
else:
pass
Expand Down
6 changes: 5 additions & 1 deletion simtools/applications/compare_cumulative_psf.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,10 @@


def load_data(datafile):
"""
Load the data file with the measured PSF vs radius [cm].
"""
d_type = {"names": ("Radius [cm]", "Relative intensity"), "formats": ("f8", "f8")}
# test_data_file = io.get_test_data_file('PSFcurve_data_v2.txt')
data = np.loadtxt(datafile, dtype=d_type, usecols=(0, 2))
Expand Down Expand Up @@ -146,7 +150,7 @@ def main():

# New parameters
if args_dict.get("pars", None):
with open(args_dict["pars"]) as file:
with open(args_dict["pars"], encoding="utf-8") as file:
new_pars = yaml.safe_load(file)
tel_model.change_multiple_parameters(**new_pars)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@


def main():
config = configurator.Configurator(description=("Add a unit field to a parameter in the DB."))
config = configurator.Configurator(description="Add a unit field to a parameter in the DB.")
args_dict, db_config = config.initialize(db_config=True, telescope_model=True)

logger = logging.getLogger()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ def main():

db = db_handler.DatabaseHandler(mongo_db_config=db_config)

with open(args_dict["sections"], "r") as stream:
with open(args_dict["sections"], "r", encoding="utf-8") as stream:
parameter_catogeries = yaml.safe_load(stream)

non_optic_catagories = [
Expand All @@ -54,7 +54,7 @@ def main():
"Camera",
"Unnecessary",
]
non_optic_parameters = list()
non_optic_parameters = []
for category in non_optic_catagories:
for par_now in parameter_catogeries[category]:
non_optic_parameters.append(par_now)
Expand Down
6 changes: 3 additions & 3 deletions simtools/applications/derive_mirror_rnda.py
Original file line number Diff line number Diff line change
Expand Up @@ -367,9 +367,9 @@ def run(rnda):

logger.info(f"Start value for mirror_reflection_random_angle: {rnda_start}")

results_rnda = list()
results_mean = list()
results_sig = list()
results_rnda = []
results_mean = []
results_sig = []
if args_dict["no_tuning"]:
rnda_opt = rnda_start
else:
Expand Down
10 changes: 5 additions & 5 deletions simtools/applications/make_regular_arrays.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,13 +71,13 @@ def main():
# Reading site parameters from DB
db = db_handler.DatabaseHandler(mongo_db_config=db_config)

site_pars_db = dict()
layout_center_data = dict()
corsika_telescope_data = dict()
site_pars_db = {}
layout_center_data = {}
corsika_telescope_data = {}
for site in ["North", "South"]:
site_pars_db[site] = db.get_site_parameters(site=site, model_version="prod5")

layout_center_data[site] = dict()
layout_center_data[site] = {}
layout_center_data[site]["center_lat"] = (
float(site_pars_db[site]["ref_lat"]["Value"]) * u.deg
)
Expand All @@ -88,7 +88,7 @@ def main():
float(site_pars_db[site]["altitude"]["Value"]) * u.m
)
layout_center_data[site]["EPSG"] = site_pars_db[site]["EPSG"]["Value"]
corsika_telescope_data[site] = dict()
corsika_telescope_data[site] = {}
corsika_telescope_data[site]["corsika_obs_level"] = layout_center_data[site]["center_alt"]
corsika_telescope_data[site]["corsika_sphere_center"] = corsika_pars[
"corsika_sphere_center"
Expand Down
6 changes: 3 additions & 3 deletions simtools/applications/plot_simtel_histograms.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,11 +60,11 @@ def main():
logger.setLevel(gen.get_log_level_from_user(args_dict["log_level"]))

n_lists = len(args_dict["file_lists"])
simtel_histograms = list()
simtel_histograms = []
for this_list_of_files in args_dict["file_lists"]:
# Collecting hist files
histogram_files = list()
with open(this_list_of_files) as file:
histogram_files = []
with open(this_list_of_files, encoding="utf-8") as file:
for line in file:
# Removing '\n' from filename, in case it is left there.
histogram_files.append(line.replace("\n", ""))
Expand Down
2 changes: 1 addition & 1 deletion simtools/applications/print_array_elements.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ def _parse(label=None, description=None):

def main():
label = Path(__file__).stem
args_dict, _ = _parse(label, description=("Print a list of array element positions"))
args_dict, _ = _parse(label, description="Print a list of array element positions")

_logger = logging.getLogger()
_logger.setLevel(gen.get_log_level_from_user(args_dict["log_level"]))
Expand Down
24 changes: 12 additions & 12 deletions simtools/applications/production.py
Original file line number Diff line number Diff line change
Expand Up @@ -167,25 +167,25 @@ def _proccess_simulation_config_file(config_file, primary_config, logger):
"""

try:
with open(config_file) as file:
with open(config_file, encoding="utf-8") as file:
config_data = yaml.load(file)
except FileNotFoundError:
logger.error(f"Error loading simulation configuration file from {config_file}")
raise

label = config_data.pop("label", dict())
default_data = config_data.pop("default", dict())
config_showers = dict()
config_arrays = dict()
label = config_data.pop("label", {})
default_data = config_data.pop("default", {})
config_showers = {}
config_arrays = {}

for primary, primary_data in config_data.items():
if primary_config is not None and primary != primary_config:
continue

this_default = copy(default_data)

config_showers[primary] = copy(this_default.pop("showers", dict()))
config_arrays[primary] = copy(this_default.pop("array", dict()))
config_showers[primary] = copy(this_default.pop("showers", {}))
config_arrays[primary] = copy(this_default.pop("array", {}))

# Grabbing common entries for showers and array
for key, value in primary_data.items():
Expand All @@ -195,12 +195,12 @@ def _proccess_simulation_config_file(config_file, primary_config, logger):
config_arrays[primary][key] = value

# Grabbing showers entries
for key, value in primary_data.get("showers", dict()).items():
for key, value in primary_data.get("showers", {}).items():
config_showers[primary][key] = value
config_showers[primary]["primary"] = primary

# Grabbing array entries
for key, value in primary_data.get("array", dict()).items():
for key, value in primary_data.get("array", {}).items():
config_arrays[primary][key] = value
config_arrays[primary]["primary"] = primary

Expand All @@ -213,7 +213,7 @@ def _proccess_simulation_config_file(config_file, primary_config, logger):


def main():
args_dict, db_config = _parse(description=("Air shower and array simulations"))
args_dict, db_config = _parse(description="Air shower and array simulations")

logger = logging.getLogger()
logger.setLevel(gen.get_log_level_from_user(args_dict["log_level"]))
Expand All @@ -224,7 +224,7 @@ def main():
if args_dict["label"] is None:
args_dict["label"] = label

shower_simulators = dict()
shower_simulators = {}
for primary, config_data in shower_configs.items():
shower_simulators[primary] = Simulator(
label=label,
Expand All @@ -242,7 +242,7 @@ def main():
_task_function()

if args_dict["array_only"]:
array_simulators = dict()
array_simulators = {}
for primary, config_data in array_configs.items():
array_simulators[primary] = Simulator(
label=label,
Expand Down
2 changes: 1 addition & 1 deletion simtools/applications/sim_showers_for_trigger_rates.py
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ def main():
log_file_list = output_dir.joinpath(f"log_files_{args_dict['primary']}.list")

def print_list_into_file(list_of_files, file_name):
with open(file_name, "w") as f:
with open(file_name, "w", encoding="utf-8") as f:
for line in list_of_files:
f.write(line + "\n")

Expand Down
4 changes: 2 additions & 2 deletions simtools/applications/simulate_prod.py
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ def _proccess_simulation_config_file(config_file, primary_config, logger):
"""

try:
with open(config_file) as file:
with open(config_file, encoding="utf-8") as file:
config_data = yaml.load(file)
except FileNotFoundError:
logger.error(f"Error loading simulation configuration file from {config_file}")
Expand Down Expand Up @@ -274,7 +274,7 @@ def _proccess_simulation_config_file(config_file, primary_config, logger):


def main():
args_dict, db_config = _parse(description=("Run simulations for productions"))
args_dict, db_config = _parse(description="Run simulations for productions")

logger = logging.getLogger()
logger.setLevel(gen.get_log_level_from_user(args_dict["log_level"]))
Expand Down
16 changes: 12 additions & 4 deletions simtools/applications/tune_psf.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,9 +102,17 @@
from simtools.visualization import visualize


def load_data(datafile):
def load_data(data_file):
"""
Load data from file txt file.
Parameters
----------
data_file: str
Name of the data file with the measured cumulative PSF.
"""
d_type = {"names": ("Radius [cm]", "Cumulative PSF"), "formats": ("f8", "f8")}
data = np.loadtxt(datafile, dtype=d_type, usecols=(0, 2))
data = np.loadtxt(data_file, dtype=d_type, usecols=(0, 2))
data["Radius [cm]"] *= 0.1
data["Cumulative PSF"] /= np.max(np.abs(data["Cumulative PSF"]))
return data
Expand Down Expand Up @@ -169,7 +177,7 @@ def main():
# }
# tel_model.change_multiple_parameters(**pars_to_change)

all_parameters = list()
all_parameters = []

def add_parameters(
mirror_reflection, mirror_align, mirror_reflection_fraction=0.15, mirror_reflection_2=0.035
Expand All @@ -178,7 +186,7 @@ def add_parameters(
Transform the parameters to the proper format and add a new set of
parameters to the all_parameters list.
"""
pars = dict()
pars = {}
mrra = f"{mirror_reflection:.4f},{mirror_reflection_fraction:.2f},{mirror_reflection_2:.4f}"
pars["mirror_reflection_random_angle"] = mrra
mar = f"{mirror_align:.4f},28.,0.,0."
Expand Down
2 changes: 1 addition & 1 deletion simtools/camera_efficiency.py
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ def analyze(self, export=True, force=False):

# Search for at least 5 consecutive numbers to see that we are in the table
re_table = re.compile("{0}{0}{0}{0}{0}".format(r"[-+]?[0-9]*\.?[0-9]+\s+"))
with open(self._file_simtel, "r") as file:
with open(self._file_simtel, "r", encoding="utf-8") as file:
for line in file:
if re_table.match(line):
words = line.split()
Expand Down
2 changes: 1 addition & 1 deletion simtools/configuration/configurator.py
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,7 @@ def _fill_from_config_file(self, config_file):

try:
self._logger.debug(f"Reading configuration from {config_file}")
with open(config_file, "r") as stream:
with open(config_file, "r", encoding="utf-8") as stream:
_config_dict = yaml.safe_load(stream)
if "CTASIMPIPE" in _config_dict:
try:
Expand Down
8 changes: 4 additions & 4 deletions simtools/corsika/corsika_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ def load_corsika_parameters_file(corsika_parameters_file):

logger = logging.getLogger(__name__)
logger.debug(f"Loading CORSIKA parameters from file {corsika_parameters_file}")
with open(corsika_parameters_file, "r") as f:
with open(corsika_parameters_file, "r", encoding="utf-8") as f:
corsika_parameters = yaml.load(f)
return corsika_parameters

Expand Down Expand Up @@ -180,7 +180,7 @@ def set_user_parameters(self, corsika_config_data):
"""

self._logger.debug("Setting user parameters from corsika_config_data")
self._user_parameters = dict()
self._user_parameters = {}

user_pars = self._corsika_parameters["USER_PARAMETERS"]

Expand Down Expand Up @@ -259,7 +259,7 @@ def _validate_and_convert_argument(self, par_name, par_info, value_args_in):
par_unit = gen.copy_as_list(par_info["unit"])

# Checking units and converting them, if needed.
value_args_with_units = list()
value_args_with_units = []
for arg, unit in zip(value_args, par_unit):
if unit is None:
value_args_with_units.append(arg)
Expand Down Expand Up @@ -374,7 +374,7 @@ def _get_text_multiple_lines(pars):
text += _get_text_single_line(new_pars)
return text

with open(self._config_file_path, "w") as file:
with open(self._config_file_path, "w", encoding="utf-8") as file:
file.write("\n* [ RUN PARAMETERS ]\n")
# Removing AZM entry first
_user_pars_temp = copy.copy(self._user_parameters)
Expand Down
Loading

0 comments on commit 7db86f0

Please sign in to comment.