Skip to content

Commit

Permalink
Merge pull request #212 from fitbenchmarking/167_rename_minimizers_li…
Browse files Browse the repository at this point in the history
…st_defaults_file

Issue #167: Rename minimizers_list_defaults.json to fitbenchmarking_default_options.json
  • Loading branch information
wathen committed Sep 9, 2019
2 parents 9c6d969 + 103a31b commit ee63b16
Show file tree
Hide file tree
Showing 11 changed files with 131 additions and 46 deletions.
2 changes: 1 addition & 1 deletion example_scripts/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ Here is the list of all the example scripts and the difference between them:
3. `example_runScripts_expert.py` is designed to be run once Mantid and SasView (future development) are installed. This script will benchmark all the minimizers from different softwares against various type of problem definition files.
### Example usage

For default minimizers use (which loads minimizers_list_defaults.json):
For default minimizers use (which loads fitbenchmarking_default_options.json):
`mantidpython example_runScripts.py`

Can also change the `minimizers` variable within `example_runScripts.py` to customize the minimization (see example script)
Expand Down
2 changes: 1 addition & 1 deletion example_scripts/example_runScripts.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
software_options['minimizer_options'] = custom_minimizers
else:
# Using default minimizers from
# fitbenchmarking/fitbenchmarking/minimizers_list_default.json
# fitbenchmarking/fitbenchmarking/fitbenchmarking_default_options.json
software_options['minimizer_options'] = None

# Benchmark problem directories
Expand Down
2 changes: 1 addition & 1 deletion example_scripts/example_runScripts_SasView.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@
software_options['minimizer_options'] = custom_minimizers
else:
# Using default minimizers from
# fitbenchmarking/fitbenchmarking/minimizers_list_default.json
# fitbenchmarking/fitbenchmarking/fitbenchmarking_default_options.json
software_options['minimizer_options'] = None


Expand Down
2 changes: 1 addition & 1 deletion example_scripts/example_runScripts_expert.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@
software_options['minimizer_options'] = custom_minimizers
else:
# Using default minimizers from
# fitbenchmarking/fitbenchmarking/minimizers_list_default.json
# fitbenchmarking/fitbenchmarking/fitbenchmarking_default_options.json
software_options['minimizer_options'] = None

# Benchmark problem directories
Expand Down
2 changes: 1 addition & 1 deletion example_scripts/example_runScripts_mantid.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@
software_options['minimizer_options'] = custom_minimizers
else:
# Using default minimizers from
# fitbenchmarking/fitbenchmarking/minimizers_list_default.json
# fitbenchmarking/fitbenchmarking/fitbenchmarking_default_options.json
software_options['minimizer_options'] = None

# Benchmark problem directories
Expand Down
12 changes: 12 additions & 0 deletions fitbenchmarking/fitbenchmarking_default_options.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"minimizers": {
"mantid" : ["BFGS", "Conjugate gradient (Fletcher-Reeves imp.)",
"Conjugate gradient (Polak-Ribiere imp.)",
"Damped GaussNewton",
"Levenberg-Marquardt", "Levenberg-MarquardtMD",
"Simplex","SteepestDescent",
"Trust Region"],
"scipy" : ["lm", "trf", "dogbox"],
"sasview" : ["amoeba", "lm", "newton", "de", "pt", "mp"]
}
}
10 changes: 0 additions & 10 deletions fitbenchmarking/minimizers_list_default.json

This file was deleted.

58 changes: 28 additions & 30 deletions fitbenchmarking/utils/misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,54 +22,52 @@
# <https://github.com/mantidproject/fitbenchmarking>.
# Code Documentation is available at: <http://doxygen.mantidproject.org>

from __future__ import (absolute_import, division, print_function)
from __future__ import absolute_import, division, print_function

import os
import json
import glob
import os

from utils.logging_setup import logger
from utils import user_input
from fitbenchmarking.utils import options, user_input
from fitbenchmarking.utils.logging_setup import logger


def get_minimizers(software_options):
"""
Gets an array of minimizers to fitbenchmark from the json file depending
on which software is used.
@param software_options :: dictionary containing software used in fitting the problem and list of minimizers or location of json file contain minimizers
@param software_options :: dictionary containing software used in fitting
the problem and list of minimizers or location
of json file contain minimizers
@returns :: an array of strings containing minimizer names and software used
"""
try:
if not isinstance(software_options, dict):
raise ValueError('software_options must be a dictionary')

try:
software = software_options['software']
minimizer_options = software_options['minimizer_options']
if not isinstance(software, list):
software = [software]
minimizers = []
for x in software:
if not minimizer_options:
current_path = os.path.dirname(os.path.realpath(__file__))
fitbm_path = os.path.abspath(os.path.join(current_path, os.pardir))
minimizer_file = os.path.join(fitbm_path,
"minimizers_list_default.json")
minimizers_list = json.load(open(minimizer_file))
elif isinstance(minimizer_options, str):
minimizers_list = json.load(open(minimizer_options))
elif isinstance(minimizer_options, dict):
minimizers_list = minimizer_options
else:
raise ValueError('minimizer_options required to be None, string '
'or dictionary, type(minimizer_options) '
'= {}'.format(type(minimizer_options)))
minimizers.append(minimizers_list[x])
except:
except KeyError:
raise ValueError('software_options required to be a dictionary with '
'keys software and minimizer_options')
if len(software) > 1:
return minimizers, software

if minimizer_options is None:
minimizers_list = options.get_option(option='minimizers')
elif isinstance(minimizer_options, str):
minimizers_list = options.get_option(options_file=minimizer_options, option='minimizers')
elif isinstance(minimizer_options, dict):
minimizers_list = minimizer_options
else:
raise ValueError('minimizer_options required to be None, string '
'or dictionary, type(minimizer_options) '
'= {}'.format(type(minimizer_options)))

if not isinstance(software, list):
return minimizers_list.get(software, []), software
else:
return minimizers[0], software[0]
minimizers = []
for x in software:
minimizers.append(minimizers_list.get(x, []))
return minimizers, software


def setup_fitting_problems(data_dir, group_name):
Expand Down
26 changes: 26 additions & 0 deletions fitbenchmarking/utils/options.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
'''
This file will handle all interaction with the options configuration file.
'''

import json


def get_option(options_file='fitbenchmarking/fitbenchmarking_default_options.json', option=None):
'''
Get a value for the given option from a config file.
The options file should contain json formatted data
The default path is fitbenchmarking/fitbenchmarking_default_options.json
@param options_file :: The file name for the options configuration.
@param option :: String for specifying the option to return.
@returns :: json parsed item from the options file
'''
options = json.load(open(options_file, 'r'))
if option is None:
return options
else:
try:
return options[option]
except KeyError:
raise ValueError('Option not found in file: {}'.format(options_file))
2 changes: 1 addition & 1 deletion fitbenchmarking/utils/tests/test_misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ def get_minimizers_file(self):
utils_path = os.path.abspath(os.path.join(current_path, os.pardir))
fitbm_path = os.path.abspath(os.path.join(utils_path, os.pardir))
minimizer_json = os.path.join(fitbm_path,
"minimizers_list_default.json")
"fitbenchmarking_default_options.json")
return str(minimizer_json)

def test_getMinimizers_load_correct_minimizers_mantid_default(self):
Expand Down
59 changes: 59 additions & 0 deletions fitbenchmarking/utils/tests/test_options.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
'''
Test the options.py file
'''

import datetime
import json
import os
import unittest

import fitbenchmarking.utils.options as options

try:
FileNotFoundError
except NameError:
FileNotFoundError = IOError


class OptionsTests(unittest.TestCase):
def setUp(self):
'''
Create an options file and store input
'''

opts = {'option1': True,
'option2': [0, 1, 2, 3, 4, 5],
'option3': {'foo': 1, 'bar': 2}}

opts_file = 'test_options_tests_{}.txt'.format(datetime.datetime.now())
with open(opts_file, 'w') as f:
f.write(json.dumps(opts))

self.options = opts
self.options_file = opts_file

def tearDown(self):
os.remove(self.options_file)

def testGetOption(self):
# Test whole options dict
opts = options.get_option(options_file=self.options_file)
self.assertEqual(opts, self.options)

# Test individual options
for k in self.options:
val = options.get_option(options_file=self.options_file, option=k)
self.assertEqual(val, self.options[k])

# Test missing options
with self.assertRaises(ValueError):
options.get_option(options_file=self.options_file, option='not_real')

# Test missing file
with self.assertRaises(FileNotFoundError):
fake_file = 'fake_{}'.format(self.options_file)
options.get_option(options_file=fake_file)


if __name__ == '__main__':
unittest.main()

0 comments on commit ee63b16

Please sign in to comment.