# Author: Carsten Sachse 29-Aug-2011
# Copyright: EMBL (2010 - 2018), Forschungszentrum Juelich (2019 - 2021)
# License: see license.txt for details
"""
Program to interactively explore refinement cycle statistics
"""
from spring.csinfrastr.csdatabase import SpringDataBase, refine_base, RefinementCycleTable
from spring.csinfrastr.csfeatures import Features
from spring.csinfrastr.cslogger import Logger
from spring.csinfrastr.csproductivity import DiagnosticPlot
from spring.csinfrastr.csreadinput import OptHandler
from spring.segment2d.segmentplot import SegmentPlot
from spring.segment3d.seggridexplore import SegGridExploreSupport
from sqlalchemy.sql.expression import asc
import numpy as np
import os
import shutil
[docs]class SegRefine3dCycleExplorePar(object):
"""
Class to initiate default dictionary with input parameters including help and range values and status dictionary
"""
def __init__(self):
# package/program identity
self.package = 'emspring'
self.progname = 'segrefine3dcyclexplore'
self.proginfo = __doc__
self.code_files = [self.progname]
self.seggridexplore_features = Features()
self.feature_set = self.seggridexplore_features.setup(self)
self.define_parameters_and_their_properties()
self.define_program_states()
[docs] def define_parameters_and_their_properties(self):
self.feature_set = self.seggridexplore_features.set_inp_refinement_path(self.feature_set)
self.feature_set = self.seggridexplore_features.set_interactive_vs_batch_mode(self.feature_set)
self.feature_set = self.seggridexplore_features.set_diagnostic_prefix(self.feature_set, 'intermediate',
'Batch mode')
self.feature_set = self.set_criterion_to_be_plotted(self.feature_set)
[docs] def define_program_states(self):
self.feature_set.program_states['extract_desired_criteria']='Extract desired refinement criteria'
self.program_states['launch_segrefine3dcyclexplore_gui']='Explore different refinement criteria interactively'
[docs] def set_criterion_to_be_plotted(self, feature_set):
inp9 = 'Criterion'
feature_set.parameters[inp9]=str('mean_helical_ccc')
criteria = SegRefine3dCycleExploreSupport().get_segrefin3dcyclexpore_criteria()
feature_set.properties[inp9] = feature_set.choice_properties(2, criteria, 'QComboBox')
feature_set.hints[inp9]=SpringDataBase().get_hints_from_refinement_cycle(criteria)
feature_set.relatives[inp9]=('Batch mode')
feature_set.level[inp9]='intermediate'
return feature_set
[docs]class SegRefine3dCycleExploreSupport(object):
[docs] def get_segrefin3dcyclexpore_criteria(self):
criteria = SegGridExploreSupport().get_common_criterias() + \
SegRefine3dCycleExploreSupport().get_additional_criteria()
return criteria
[docs] def get_additional_criteria(self):
return ['excluded_out_of_plane_tilt_count', 'excluded_inplane_count', 'total_excluded_count']
[docs]class SegRefine3dCycleExplorePreparation(object):
def __init__(self, parset = None):
self.log = Logger()
if parset is not None:
self.feature_set = parset
p = self.feature_set.parameters
self.refinement_db = p['refinement.db file']
self.infile = self.refinement_db
self.batch_mode = p['Batch mode']
self.outfile_prefix = p['Diagnostic plot prefix']
self.criterion = p['Criterion']
[docs] def get_refinement_info_from_database(self, refinement_db):
ref_session = SpringDataBase().setup_sqlite_db(refine_base, refinement_db)
ref_cycles = ref_session.query(RefinementCycleTable).order_by(asc(RefinementCycleTable.id)).all()
ref_data = []
ylabels = []
filtered_quant = []
criteria = SegRefine3dCycleExploreSupport().get_segrefin3dcyclexpore_criteria()
for each_attr in criteria:
ref_data, ylabels, filtered_quant = SegGridExploreSupport().get_all_attr_from_search_result(ref_cycles,
each_attr, filtered_quant, ref_data, ylabels, RefinementCycleTable)
ref_cycle_ids = [each_ref_cycle.id for each_ref_cycle in ref_cycles]
xlabel = 'Iteration cycle'
ref_data = [(ylabels[each_id], np.array([ref_cycle_ids, each_ref_data])) for (each_id, each_ref_data) in\
enumerate(ref_data)]
labels = [[xlabel, each_y_label] for each_y_label in ylabels]
hints = SpringDataBase().get_hints_from_refinement_cycle(filtered_quant)
return ref_data, labels, criteria, hints
[docs]class SegRefine3dCycleExplore(SegRefine3dCycleExplorePreparation):
[docs] def plot_refinement_criteria_per_cycle(self):
work_refinement_db = os.path.basename(self.refinement_db)
shutil.copy(self.refinement_db, work_refinement_db)
xy_quantities, labels, criteria, hints = self.get_refinement_info_from_database(work_refinement_db)
criterion_id = [crit_id for (crit_id, each_criterion) in enumerate(criteria) if each_criterion == self.criterion]
self.log.plog(10)
figures = self.plot_data_on_figure(xy_quantities, labels, criterion_id)
if not self.batch_mode:
SegmentPlot().launch_interactive_plot_gui(self.feature_set, figures, xy_quantities, labels, hints)
self.log.endlog(self.feature_set)
[docs]def main():
# Option handling
parset = SegRefine3dCycleExplorePar()
mergeparset = OptHandler(parset)
######## Program
sseg = SegRefine3dCycleExplore(mergeparset)
sseg.plot_refinement_criteria_per_cycle()
if __name__ == '__main__':
main()