[docs]defto_alchemlyb(energy_trajectories,temperature=None,energy_unit="kcal/mol"):""" Convert the passed list of energy trajectories into a single alchemlyb-compatible DataFrame, ready for free energy analysis via alchemlyb. Parameters ---------- energy_trajectories : list of sire.maths.EnergyTrajectory objects, list of filenames of s3 files, globbed expression for list of filenames etc. A list of EnergyTrajectory objects, each containing the energy trajectory for a single simulation at a single lambda value. temperature : temperature, optional The temperature of the simulation. If not provided, the temperature will be taken from the values in each EnergyTrajectory energy_unit: str Whichever of the alchemlyb energy units you want the output DataFrame to use. This is in alchemlyb format, e.g. `kcal/mol`, `kJ/mol`, or `kT` Returns ------- pandas.DataFrame A single DataFrame containing the energy trajectories from all simulations, ready for free energy analysis via alchemlyb. """iftype(energy_trajectories)isstr:# this could be a globbed file pathimportglobenergy_trajectories=glob.glob(energy_trajectories)eliftype(energy_trajectories)isnotlist:energy_trajectories=[energy_trajectories]# convert all of the energy trajectories to pandas DataFramesdataframes={}forenergy_trajectoryinenergy_trajectories:iftype(energy_trajectory)isstr:# load the energy trajectory from the s3 filefrom..streamimportloadenergy_trajectory=load(energy_trajectory)df=energy_trajectory.to_alchemlyb(temperature=temperature,energy_unit=energy_unit)# get the lambda value of the first rowtry:lambda_value=df.index[0][1]exceptIndexError:# this is a corrupt energy trajectory...from..utilsimportConsoleConsole.warning("Corrupt energy trajectory detected. Skipping...")continueiflambda_valueindataframes:dataframes[lambda_value].append(df)else:dataframes[lambda_value]=[df]# get the list of lambda values and sort...lambda_values=list(dataframes.keys())lambda_values.sort()# now create a list of dataframes in lambda orderdfs=[]forlambda_valueinlambda_values:dfs.extend(dataframes[lambda_value])iflen(dfs)==0:returnNoneeliflen(dfs)==1:returndfs[0]else:attrs=dfs[0].attrs# now concatenate the dataframesimportpandasaspdcombined=pd.concat(dfs)combined.attrs=attrsreturncombined