Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

141

142

143

144

145

146

147

148

149

150

151

152

153

154

155

156

157

158

159

160

161

162

163

164

165

166

167

168

169

170

171

172

173

174

175

176

177

178

179

180

181

182

183

184

185

186

187

188

189

190

191

192

193

194

195

196

197

198

199

200

201

202

203

204

205

206

207

208

209

210

211

212

213

214

215

216

217

218

219

220

221

222

223

224

225

226

227

228

229

230

231

232

233

234

235

236

237

238

239

240

241

242

243

244

245

246

247

248

249

250

251

252

253

254

255

256

257

258

259

260

261

262

263

264

265

266

267

268

269

270

271

272

273

274

275

276

277

278

279

280

281

282

283

284

285

286

287

288

289

290

291

292

293

294

295

296

297

298

299

300

301

302

303

304

305

306

307

308

309

310

311

312

313

314

315

316

317

318

319

320

321

322

323

324

325

326

327

328

329

330

331

332

333

334

335

336

337

338

339

340

341

342

343

344

345

346

347

348

349

350

351

352

353

354

355

356

357

358

359

360

361

362

363

364

365

366

367

368

369

370

371

372

373

374

375

376

377

378

379

380

381

382

383

384

385

386

387

388

389

390

391

392

393

394

395

396

397

398

399

400

401

402

403

404

405

406

407

408

409

410

411

412

413

414

415

416

417

418

419

420

421

422

423

424

425

426

427

428

429

430

431

432

433

434

435

436

437

438

439

440

441

442

443

444

445

446

447

448

449

450

451

452

453

454

455

456

457

458

459

460

461

462

463

464

465

466

467

468

469

470

471

472

473

474

475

476

477

478

479

480

481

482

483

484

485

486

487

488

489

490

491

492

493

494

495

496

497

498

499

500

501

502

503

504

505

506

507

508

509

510

511

512

513

514

515

516

517

518

519

520

521

522

523

524

525

526

527

528

529

530

531

532

533

534

535

536

537

538

539

540

541

542

543

544

545

546

547

548

549

550

551

552

553

554

555

556

557

558

559

560

561

562

563

564

565

566

567

568

569

570

571

572

573

574

575

576

577

578

579

580

581

582

583

584

585

586

587

588

589

590

591

592

593

594

595

596

597

598

599

600

601

602

603

604

605

606

607

608

609

610

611

612

613

614

615

616

617

618

619

620

621

622

623

624

625

626

import getpass 

import json 

import numbers 

import numpy as np 

import pandas as pd 

import tarfile 

import tempfile 

import socket 

 

from ase import Atoms 

from ase.io import Trajectory 

from ase.io import write as ase_write, read as ase_read 

from collections import OrderedDict 

from datetime import datetime 

from typing import BinaryIO, Dict, List, TextIO, Tuple, Union 

from icet import __version__ as icet_version 

from .data_analysis import analyze_data 

from .observers.base_observer import BaseObserver 

 

 

class Int64Encoder(json.JSONEncoder): 

 

def default(self, obj): 

if isinstance(obj, np.int64): 

return int(obj) 

return json.JSONEncoder.default(self, obj) 

 

 

class DataContainer: 

""" 

Data container for storing information concerned with 

Monte Carlo simulations performed with mchammer. 

 

Parameters 

---------- 

structure : ASE Atoms object 

reference atomic structure associated with the data container 

 

ensemble_parameters : dict 

parameters associated with the underlying ensemble 

 

metadata : dict 

metadata associated with the data container 

""" 

 

def __init__(self, structure: Atoms, ensemble_parameters: dict, 

metadata: dict = OrderedDict()): 

""" 

Initializes a DataContainer object. 

""" 

if not isinstance(structure, Atoms): 

raise TypeError('structure is not an ASE Atoms object') 

 

self.structure = structure.copy() 

self._ensemble_parameters = ensemble_parameters 

self._metadata = metadata 

self._add_default_metadata() 

self._last_state = {} 

 

self._observables = set() 

self._data_list = [] 

 

def append(self, mctrial: int, 

record: Dict[str, Union[int, float, list]]): 

""" 

Appends data to data container. 

 

Parameters 

---------- 

mctrial 

current Monte Carlo trial step 

record 

dictionary of tag-value pairs representing observations 

 

Raises 

------ 

TypeError 

if input parameters have the wrong type 

 

""" 

if not isinstance(mctrial, numbers.Integral): 

raise TypeError('mctrial has the wrong type: {}'.format(type(mctrial))) 

 

if self._data_list: 

if self._data_list[-1]['mctrial'] > mctrial: 

raise ValueError('mctrial values should be given in ascending' 

' order. This error can for example occur' 

' when trying to append to an existing data' 

' container after having reset the time step.' 

' Note that the latter happens automatically' 

' when initializing a new ensemble.') 

 

if not isinstance(record, dict): 

raise TypeError('record has the wrong type: {}'.format(type(record))) 

 

for tag in record.keys(): 

self._observables.add(tag) 

 

row_data = OrderedDict() 

row_data['mctrial'] = mctrial 

row_data.update(record) 

self._data_list.append(row_data) 

 

def _update_last_state(self, last_step: int, occupations: List[int], 

accepted_trials: int, random_state: tuple): 

"""Updates last state of the simulation: last step, occupation vector 

and number of accepted trial steps. 

 

Parameters 

---------- 

last_step 

last trial step 

occupations 

occupation vector observed during the last trial step 

accepted_trial 

number of current accepted trial steps 

random_state 

tuple representing the last state of the random generator 

""" 

self._last_state['last_step'] = last_step 

self._last_state['occupations'] = occupations 

self._last_state['accepted_trials'] = accepted_trials 

self._last_state['random_state'] = random_state 

 

def apply_observer(self, observer: BaseObserver): 

""" Adds observer data from observer to data container. 

 

The observer will only be run for the mctrials for which the 

trajectory have been saved. 

 

The interval of the observer is ignored. 

 

Parameters 

---------- 

observer 

observer to be used 

""" 

for row_data in self._data_list: 

if 'occupations' in row_data: 

structure = self.structure.copy() 

structure.numbers = row_data['occupations'] 

record = dict() 

143 ↛ 144line 143 didn't jump to line 144, because the condition on line 143 was never true if observer.return_type is dict: 

for key, value in observer.get_observable(structure).items(): 

record[key] = value 

else: 

record[observer.tag] = observer.get_observable(structure) 

row_data.update(record) 

self._observables.add(observer.tag) 

 

def get_data(self, *tags, 

start: int = None, 

stop: int = None, 

interval: int = 1, 

fill_method: str = 'skip_none', 

apply_to: List[str] = None) \ 

-> Union[np.ndarray, List[Atoms], Tuple[np.ndarray, List[Atoms]]]: 

"""Returns the accumulated data for the requested observables, 

including configurations stored in the data container. The latter 

can be achieved by including 'trajectory' as a tag. 

 

Parameters 

---------- 

tags 

tuples of the requested properties 

 

start 

minimum value of trial step to consider; by default the 

smallest value in the mctrial column will be used. 

 

stop 

maximum value of trial step to consider; by default the 

largest value in the mctrial column will be used. 

 

interval 

increment for mctrial; by default the smallest available 

interval will be used. 

 

fill_method : {'skip_none', 'fill_backward', 'fill_forward', 

'linear_interpolate', None} 

method employed for dealing with missing values; by default 

uses 'skip_none'. 

 

apply_to 

tags of columns for which fill_method will be employed; 

by default parse all columns with fill_method. 

 

Raises 

------ 

ValueError 

if tags is empty 

ValueError 

if observables are requested that are not in data container 

ValueError 

if fill method is unknown 

ValueError 

if trajectory is requested and fill method is not skip_none 

 

Examples 

-------- 

The following lines illustrate how to use the `get_data` method 

for extracting data from the trajectory:: 

 

# obtain a list of all values of the potential represented by 

# the cluster expansion along the trajectory 

p = dc.get_data('potential') 

 

# as above but this time the MC trial step and the temperature 

# are included as well 

s, p, t = dc.get_data('mctrial', 'potential', 'temperature') 

 

# obtain configurations along the trajectory along with 

# their potential 

p, confs = dc.get_data('potential', 'trajectory') 

 

""" 

fill_methods = ['skip_none', 

'fill_backward', 

'fill_forward', 

'linear_interpolate'] 

 

if len(tags) == 0: 

raise TypeError('Missing tags argument') 

 

if 'trajectory' in tags: 

if fill_method != 'skip_none': 

raise ValueError('Only skip_none fill method is avaliable' 

' when trajectory is requested') 

 

new_tags = tuple(['occupations' if tag == 'trajectory' else tag for tag in tags]) 

return self._get_trajectory(*new_tags, start=start, stop=stop, interval=interval) 

 

for tag in tags: 

if tag == 'mctrial': 

continue 

if tag not in self.observables: 

raise ValueError('No observable named {} in data container'.format(tag)) 

 

mctrials = [row_dict['mctrial'] for row_dict in self._data_list] 

data = pd.DataFrame.from_records(self._data_list, index=mctrials, columns=tags) 

if start is None and stop is None: 

data = data.loc[::interval, tags].copy() 

else: 

# slice and pass a copy to avoid slowing down dropna method below 

if start is None: 

data = data.loc[:stop:interval, tags].copy() 

elif stop is None: 

data = data.loc[start::interval, tags].copy() 

else: 

data = data.loc[start:stop:interval, tags].copy() 

 

252 ↛ 285line 252 didn't jump to line 285, because the condition on line 252 was never false if fill_method is not None: 

if fill_method not in fill_methods: 

raise ValueError('Unknown fill method: {}' 

.format(fill_method)) 

 

if apply_to is None: 

apply_to = tags 

 

# retrieve only valid observations 

if fill_method == 'skip_none': 

data.dropna(inplace=True, subset=apply_to) 

 

else: 

# if requested, drop NaN values in columns 

subset = [tag for tag in tags if tag not in apply_to] 

data.dropna(inplace=True, subset=subset) 

 

# fill NaN with the next valid observation 

if fill_method == 'fill_backward': 

data.fillna(method='bfill', inplace=True) 

 

# fill NaN with the last valid observation 

elif fill_method == 'fill_forward': 

data.fillna(method='ffill', inplace=True) 

 

# fill NaN with the linear interpolation of the last and 

# next valid observations 

279 ↛ 283line 279 didn't jump to line 283, because the condition on line 279 was never false elif fill_method == 'linear_interpolate': 

data.interpolate(limit_area='inside', inplace=True) 

 

# drop any left-over nan values 

data.dropna(inplace=True) 

 

data_list = [] 

for tag in tags: 

# convert NaN to None 

data_list.append(np.array([None if np.isnan(x).any() else x for x in data[tag]])) 

if len(tags) > 1: 

# return a tuple if more than one tag is given 

return tuple(data_list) 

else: 

# return a list if only one tag is given 

return data_list[0] 

 

@property 

def data(self) -> pd.DataFrame: 

""" pandas data frame (see :class:`pandas.DataFrame`) """ 

if self._data_list: 

df = pd.DataFrame.from_records(self._data_list, index='mctrial', 

exclude=['occupations']) 

df.dropna(axis='index', how='all', inplace=True) 

df.reset_index(inplace=True) 

return df 

else: 

return pd.DataFrame() 

 

@property 

def ensemble_parameters(self) -> dict: 

""" parameters associated with Monte Carlo simulation """ 

return self._ensemble_parameters.copy() 

 

@property 

def observables(self) -> List[str]: 

""" observable names """ 

return list(self._observables) 

 

@property 

def metadata(self) -> dict: 

""" metadata associated with data container """ 

return self._metadata 

 

@property 

def last_state(self) -> Dict[str, Union[int, List[int]]]: 

""" last state to be used to restart Monte Carlo simulation """ 

return self._last_state 

 

def get_number_of_entries(self, tag: str = None) -> int: 

""" 

Returns the total number of entries with the given observable tag. 

 

Parameters 

---------- 

tag 

name of observable; by default the total number of rows in the 

data frame will be returned. 

 

Raises 

------ 

ValueError 

if observable is requested that is not in data container 

""" 

data = pd.DataFrame.from_records(self._data_list) 

if tag is None: 

return len(data) 

else: 

if tag not in data: 

raise ValueError('No observable named {} in data container'.format(tag)) 

return data[tag].count() 

 

def analyze_data(self, tag: str, start: int = None, 

stop: int = None, max_lag: int = None) -> dict: 

""" 

Returns detailed analysis of a scalar observerable. 

 

Parameters 

---------- 

tag 

tag of field over which to average 

start 

minimum value of trial step to consider; by default the 

smallest value in the mctrial column will be used. 

stop 

maximum value of trial step to consider; by default the 

largest value in the mctrial column will be used. 

max_lag 

maximum lag between two points in data series, by default the 

largest length of the data series will be used. 

Used for computing autocorrelation 

Raises 

------ 

ValueError 

if observable is requested that is not in data container 

ValueError 

if observable is not scalar 

ValueError 

if observations is not evenly spaced 

 

Returns 

------- 

dict 

calculated properties of the data including mean, 

standard_deviation, correlation_length and error_estimate 

(95% confidence) 

""" 

386 ↛ 387line 386 didn't jump to line 387, because the condition on line 386 was never true if tag in ['trajectory', 'occupations']: 

raise ValueError('{} is not scalar'.format(tag)) 

steps, data = self.get_data('mctrial', tag, start=start, stop=stop) 

 

# check that steps are evenly spaced 

diff = np.diff(steps) 

step_length = diff[0] 

393 ↛ 394line 393 didn't jump to line 394, because the condition on line 393 was never true if not np.allclose(step_length, diff): 

raise ValueError('data records must be evenly spaced.') 

 

summary = analyze_data(data, max_lag=max_lag) 

summary['correlation_length'] *= step_length # in mc-trials 

return summary 

 

def get_average(self, tag: str, start: int = None, stop: int = None) -> float: 

""" 

Returns average of a scalar observable. 

 

Parameters 

---------- 

tag 

tag of field over which to average 

start 

minimum value of trial step to consider; by default the 

smallest value in the mctrial column will be used. 

stop 

maximum value of trial step to consider; by default the 

largest value in the mctrial column will be used. 

 

Raises 

------ 

ValueError 

if observable is requested that is not in data container 

ValueError 

if observable is not scalar 

""" 

if tag in ['trajectory', 'occupations']: 

raise ValueError('{} is not scalar'.format(tag)) 

data = self.get_data(tag, start=start, stop=stop) 

return np.mean(data) 

 

def get_trajectory(self, start: int = None, stop: int = None, interval: int = 1) -> List[Atoms]: 

""" Returns trajectory as a list of ASE Atoms objects. 

 

Parameters 

---------- 

start 

minimum value of trial step to consider; by default the 

smallest value in the mctrial column will be used. 

stop 

maximum value of trial step to consider; by default the 

largest value in the mctrial column will be used. 

interval 

increment for mctrial; by default the smallest available 

interval will be used. 

""" 

return self.get_data('trajectory', start=start, stop=stop, interval=interval) 

 

def _get_trajectory(self, *tags, start: int = None, stop: int = None, interval: int = 1) \ 

-> Union[List[Atoms], Tuple[List[Atoms], np.ndarray]]: 

""" 

Returns a trajectory in the form of a list of ASE Atoms 

along with the corresponding values of the mctrial and/or scalar 

properties upon request. 

Configurations with non properties will be 

skipped in the trajectory if the property is requested. 

 

Parameters 

---------- 

start 

minimum value of trial step to consider; by default the 

smallest value in the mctrial column will be used. 

stop 

maximum value of trial step to consider; by default the 

largest value in the mctrial column will be used. 

interval 

increment for mctrial; by default the smallest available 

interval will be used. 

""" 

 

if 'occupations' in tags: 

new_tags = tags 

else: 

new_tags = ('occupations', ) + tags 

 

data = self.get_data(*new_tags, start=start, stop=stop, interval=interval) 

 

if len(new_tags) > 1: 

data_list = list(data) 

else: 

data_list = [data] 

 

tag_list = list(new_tags) 

structure_list = [] 

for tag, data_row in zip(tag_list, data_list): 

if tag == 'occupations': 

ind = tag_list.index('occupations') 

for occupation_vector in data_row: 

structure = self.structure.copy() 

structure.numbers = occupation_vector 

structure_list.append(structure) 

data_list[ind] = structure_list 

 

if len(data_list) > 1: 

return tuple(data_list) 

else: 

return data_list[0] 

 

def write_trajectory(self, outfile: Union[str, BinaryIO, TextIO]) -> None: 

"""Writes the configurations along the trajectory to file in ASE 

trajectory format. The file also includes the respectives 

values of the potential for each configuration. If the file 

exists the trajectory will be appended. The ASE `convert` 

command can be used to convert the trajectory file to other 

formats. The ASE `gui` can be used to visualize the 

trajectory. 

 

Parameters 

---------- 

outfile 

output file name or file object 

""" 

structure_list, energies = self._get_trajectory('occupations', 'potential') 

traj = Trajectory(outfile, mode='a') 

for structure, energy in zip(structure_list, energies): 

traj.write(atoms=structure, energy=energy) 

traj.close() 

 

@staticmethod 

def read(infile: Union[str, BinaryIO, TextIO], old_format: bool = False): 

""" 

Reads DataContainer object from file. 

 

Parameters 

---------- 

infile 

file from which to read 

old_format 

If true use old json format to read runtime data; default to false 

 

Raises 

------ 

FileNotFoundError 

if file is not found (str) 

ValueError 

if file is of incorrect type (not a tarball) 

""" 

if isinstance(infile, str): 

filename = infile 

else: 

filename = infile.name 

 

if not tarfile.is_tarfile(filename): 

raise TypeError('{} is not a tar file'.format(filename)) 

 

reference_structure_file = tempfile.NamedTemporaryFile() 

reference_data_file = tempfile.NamedTemporaryFile() 

runtime_data_file = tempfile.NamedTemporaryFile() 

 

with tarfile.open(mode='r', name=filename) as tar_file: 

# file with structures 

reference_structure_file.write(tar_file.extractfile('atoms').read()) 

 

reference_structure_file.seek(0) 

structure = ase_read(reference_structure_file.name, format='json') 

 

# file with reference data 

reference_data_file.write(tar_file.extractfile('reference_data').read()) 

reference_data_file.seek(0) 

with open(reference_data_file.name, encoding='utf-8') as fd: 

reference_data = json.load(fd) 

 

# init DataContainer 

dc = DataContainer(structure=structure, 

ensemble_parameters=reference_data['parameters']) 

 

# overwrite metadata 

dc._metadata = reference_data['metadata'] 

 

for tag, value in reference_data['last_state'].items(): 

if tag == 'random_state': 

value = tuple(tuple(x) if isinstance(x, list) else x for x in value) 

dc._last_state[tag] = value 

 

# add runtime data from file 

runtime_data_file.write(tar_file.extractfile('runtime_data').read()) 

runtime_data_file.seek(0) 

573 ↛ 574line 573 didn't jump to line 574, because the condition on line 573 was never true if old_format: 

runtime_data = pd.read_json(runtime_data_file) 

data = runtime_data.sort_index(ascending=True) 

dc._data_list = data.T.apply(lambda x: x.dropna().to_dict()).tolist() 

else: 

dc._data_list = np.load(runtime_data_file, allow_pickle=True)['arr_0'].tolist() 

 

dc._observables = set([key for data in dc._data_list for key in data]) 

dc._observables = dc._observables - {'mctrial'} 

 

return dc 

 

def write(self, outfile: Union[str, BinaryIO, TextIO]): 

""" 

Writes DataContainer object to file. 

 

Parameters 

---------- 

outfile 

file to which to write 

""" 

self._metadata['date_last_backup'] = datetime.now().strftime('%Y-%m-%dT%H:%M:%S') 

 

# Save reference atomic structure 

reference_structure_file = tempfile.NamedTemporaryFile() 

ase_write(reference_structure_file.name, self.structure, format='json') 

 

# Save reference data 

reference_data = {'parameters': self._ensemble_parameters, 

'metadata': self._metadata, 

'last_state': self._last_state} 

 

reference_data_file = tempfile.NamedTemporaryFile() 

with open(reference_data_file.name, 'w') as handle: 

json.dump(reference_data, handle, cls=Int64Encoder) 

 

# Save runtime data 

runtime_data_file = tempfile.NamedTemporaryFile() 

np.savez_compressed(runtime_data_file, self._data_list) 

 

with tarfile.open(outfile, mode='w') as handle: 

handle.add(reference_structure_file.name, arcname='atoms') 

handle.add(reference_data_file.name, arcname='reference_data') 

handle.add(runtime_data_file.name, arcname='runtime_data') 

runtime_data_file.close() 

 

def _add_default_metadata(self): 

"""Adds default metadata to metadata dict.""" 

 

self._metadata['date_created'] = \ 

datetime.now().strftime('%Y-%m-%dT%H:%M:%S') 

self._metadata['username'] = getpass.getuser() 

self._metadata['hostname'] = socket.gethostname() 

self._metadata['icet_version'] = icet_version