from pyhande.data_preparing.hande_ccmc_fciqmc import PrepHandeCcmcFciqmc
from pyhande.extracting.extractor import Extractor
from pyhande.error_analysing.blocker import Blocker
from pyhande.results_viewer.get_results import analyse_data

extra = Extractor() # Keep the defaults, merge using UUIDs. 
prep = PrepHandeCcmcFciqmc()
ana = Blocker.inst_hande_ccmc_fciqmc(start_its=[20000, 20000]) # Keep the defaults, find starting iterations automatically, using 'blocking' finder.

"""
Explanation: This demonstration shows how CCMC/FCIQMC [1,2] calculations with complex wavefunctions and replica tricks
can be analysed.
For clarity, the extractor, preparator and analyser are defined explicity, instead of simply using get_results.
End of explanation
"""


prep._observables_init

"""
Explanation: Note that this is the original default CCMC/FCIQMC HANDE columns/key mapping in preparator. 
It is a private variable really, so user access is not encouraged, but shown here for clarity.
End of explanation
"""


results = analyse_data(["data/replica_complex_fciqmc_init_pop_10.out.xz", "data/replica_complex_fciqmc_init_pop_100.out.xz", "data/replica_complex_fciqmc_init_pop_10_part2.out.xz"], extra, prep, ana)

"""
Explanation: Now we execute our executor, preparator and analyser. 
Note that while we have three output files, we only have two calculations, one was restarted.
Since we are merging using UUIDs, the order we are passing the output file names in does not matter.
With legacy merge type, this would not work.  Order matters there.
Here, the first and third output file's data will be merged.
End of explanation
"""


prep.observables

"""
Explanation: In this analysis model, all replicas [3] are analysed individually.
Complex data is dealt with as follows:
The mapping of ref_key (the reference population N_0) and sum_key (sum_j H_0j N_j) are adapted, they are now the (negative) magnitudes of the complex equivalents.
The ratio of the means of those magnitudes is then the mean projected energy (credits to Charlie Scott for first implementation, see his note in data_preparing/hande_ccmc_fciqmc.py.).
End of explanation
"""


prep.data

"""
Explanation: The data has additional columns with this data:
End of explanation
"""


results.summary_pretty

"""
Explanation: Not everything was analysed long enough to be analysed.
End of explanation
"""


results.analyser.no_opt_block

"""
Explanation: This was unsuccessful:
End of explanation
"""


results.plot_shoulder()

"""
Explanation: Lower initial population, lower shoulder [4]?
End of explanation
"""


results.add_shoulder()
results.add_metadata(['qmc:D0_population'])
results.compare_obs(['shoulder height', 'D0_population'])

"""
Explanation: There seems to be a trend.
End of explanation
"""


