Coverage for dynasor / trajectory / abstract_trajectory_reader.py: 81%

28 statements  

« prev     ^ index     » next       coverage.py v7.13.4, created at 2026-03-16 12:31 +0000

1from abc import ABC, abstractmethod 

2from dynasor.logging_tools import logger 

3 

4 

5class AbstractTrajectoryReader(ABC): 

6 """Provides a way to iterate through a molecular dynamics trajectory 

7 file. 

8 

9 Each frame/time-step is returned as a trajectory_frame. 

10 """ 

11 

12 # unit conversion tables 

13 lengthunits_to_Angstrom_table = { 

14 'Angstrom': 1.0, 

15 'nm': 10.0, 

16 'pm': 1e3, 

17 'fm': 1e6, 

18 } 

19 

20 timeunits_to_fs_table = { 

21 'fs': 1.0, 

22 'ps': 1000, 

23 'ns': 1000000, 

24 } 

25 

26 @abstractmethod 

27 def __iter__(self): 

28 """ Iterates through the trajectory file, frame by frame. """ 

29 pass 

30 

31 @abstractmethod 

32 def __next__(self): 

33 """ Gets next trajectory frame. """ 

34 pass 

35 

36 @abstractmethod 

37 def close(self): 

38 """ Closes down, release resources etc. """ 

39 pass 

40 

41 def set_unit_scaling_factors(self, length_unit: str, time_unit: str): 

42 # setup units 

43 if length_unit is None: 

44 logger.info('Assuming the trajectory has the default length unit (Ångström), since no ' 

45 'unit was specified.') 

46 self.x_factor = self.lengthunits_to_Angstrom_table['Angstrom'] 

47 elif length_unit not in self.lengthunits_to_Angstrom_table: 47 ↛ 48line 47 didn't jump to line 48 because the condition on line 47 was never true

48 raise ValueError(f'Specified length unit {length_unit} is not an available option.') 

49 else: 

50 self.x_factor = self.lengthunits_to_Angstrom_table[length_unit] 

51 if time_unit is None: 

52 logger.info('Assuming the trajectory has the default time unit (fs), since no ' 

53 'unit was specified.') 

54 self.t_factor = self.timeunits_to_fs_table['fs'] 

55 elif time_unit not in self.timeunits_to_fs_table: 55 ↛ 56line 55 didn't jump to line 56 because the condition on line 55 was never true

56 raise ValueError(f'Specified time unit {time_unit} is not an available option.') 

57 else: 

58 self.t_factor = self.timeunits_to_fs_table[time_unit] 

59 self.v_factor = self.x_factor / self.t_factor