#!/usr/bin/env python# Copyright 2016-2024 Biomedical Imaging Group Rotterdam, Departments of# Medical Informatics and Radiology, Erasmus MC, Rotterdam, The Netherlands## Licensed under the Apache License, Version 2.0 (the "License");# you may not use this file except in compliance with the License.# You may obtain a copy of the License at## http://www.apache.org/licenses/LICENSE-2.0## Unless required by applicable law or agreed to in writing, software# distributed under the License is distributed on an "AS IS" BASIS,# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.# See the License for the specific language governing permissions and# limitations under the License.fromWORC.IOparser.config_io_classifierimportload_configfromWORC.classification.construct_classifierimportcreate_param_gridfromWORC.classification.trainclassifierimportadd_parameters_to_gridfromWORC.classification.AdvancedSamplerimportlog_uniform,discrete_uniform,boolean_uniformfromscipy.stats._distn_infrastructureimportrv_continuous_frozenprinters={log_uniform:lambdax:'$\mathcal{U}^l('+str(x.base)+'^{'+str(x.loc)+'}, '+str(x.base)+'^{'+str(x.loc+x.scale)+'})$',discrete_uniform:lambdax:'$\mathcal{U}^d('+str(x.loc)+', '+str(x.loc+x.scale)+')$',rv_continuous_frozen:lambdax:'$\mathcal{U}('+str(x.kwds['loc'])+', '+str(x.kwds['loc']+x.kwds['scale'])+')$',boolean_uniform:lambdax:'$\mathcal{B}('+str(x.threshold)+')$',list:lambdax:'{['+', '.join([str(y).replace('_','\_')foryinx])+']}'}exclude=[# exclude certain params as they are not part of hyp par optim'FeatureScaling_skip_features','FeatPreProcess','OneHotEncoding_feature_labels_tofit',]printer_types=tuple(printers.keys())
[docs]defexport_hyper_params_to_latex(config_file_path,output_file_path):config=load_config(config_file_path)param_grid=create_param_grid(config)params=add_parameters_to_grid(param_grid,config)table_out=''forparaminsorted(params.keys()):ifparaminexclude:continuedistri=params[param]ifisinstance(distri,printer_types):tex=printers[distri.__class__](distri)table_out+=param.replace("_","\\_")+f' & {tex} '+'\\\\\\hline\n'else:raiseValueError(f'Could not map {param} - {distri.__dict__}')table="""\\begin{table}[]\\begin{tabular}{l|l}"""+table_out+"""\\end{tabular}\\end{table}"""withopen(output_file_path,'w')asfh:fh.write(table)