after_merge
stringlengths 28
79.6k
| before_merge
stringlengths 20
79.6k
| url
stringlengths 38
71
| full_traceback
stringlengths 43
922k
| traceback_type
stringclasses 555
values |
|---|---|---|---|---|
def spatial_shape(self):
"""Return spatial shape of first image in subject.
Consistency of spatial shapes across images in the subject is checked
first.
"""
self.check_consistent_spatial_shape()
return self.get_first_image().spatial_shape
|
def spatial_shape(self):
"""Return spatial shape of first image in subject.
Consistency of shapes across images in the subject is checked first.
"""
return self.shape[1:]
|
https://github.com/fepegar/torchio/issues/265
|
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-2-6b7dc2edb3cc> in <module>
----> 1 icbm.spatial_shape
~/git/torchio/torchio/data/subject.py in spatial_shape(self)
95 Consistency of shapes across images in the subject is checked first.
96 """
---> 97 return self.shape[1:]
98
99 @property
~/git/torchio/torchio/data/subject.py in shape(self)
85 Consistency of shapes across images in the subject is checked first.
86 """
---> 87 self.check_consistent_shape()
88 image = self.get_images(intensity_only=False)[0]
89 return image.shape
~/git/torchio/torchio/data/subject.py in check_consistent_shape(self)
135 f'\n{pprint.pformat(shapes_dict)}'
136 )
--> 137 raise ValueError(message)
138
139 def check_consistent_orientation(self) -> None:
ValueError: Images in subject have inconsistent shapes:
{'brain': (1, 193, 229, 193),
'eyes': (1, 193, 229, 193),
'pd': (1, 193, 229, 193),
't1': (1, 193, 229, 193),
't2': (1, 193, 229, 193),
'tissues': (3, 193, 229, 193)}
|
ValueError
|
def spacing(self):
"""Return spacing of first image in subject.
Consistency of spacings across images in the subject is checked first.
"""
self.check_consistent_attribute("spacing")
return self.get_first_image().spacing
|
def spacing(self):
"""Return spacing of first image in subject.
Consistency of shapes across images in the subject is checked first.
"""
self.check_consistent_shape()
image = self.get_images(intensity_only=False)[0]
return image.spacing
|
https://github.com/fepegar/torchio/issues/265
|
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-2-6b7dc2edb3cc> in <module>
----> 1 icbm.spatial_shape
~/git/torchio/torchio/data/subject.py in spatial_shape(self)
95 Consistency of shapes across images in the subject is checked first.
96 """
---> 97 return self.shape[1:]
98
99 @property
~/git/torchio/torchio/data/subject.py in shape(self)
85 Consistency of shapes across images in the subject is checked first.
86 """
---> 87 self.check_consistent_shape()
88 image = self.get_images(intensity_only=False)[0]
89 return image.shape
~/git/torchio/torchio/data/subject.py in check_consistent_shape(self)
135 f'\n{pprint.pformat(shapes_dict)}'
136 )
--> 137 raise ValueError(message)
138
139 def check_consistent_orientation(self) -> None:
ValueError: Images in subject have inconsistent shapes:
{'brain': (1, 193, 229, 193),
'eyes': (1, 193, 229, 193),
'pd': (1, 193, 229, 193),
't1': (1, 193, 229, 193),
't2': (1, 193, 229, 193),
'tissues': (3, 193, 229, 193)}
|
ValueError
|
def check_consistent_shape(self) -> None:
self.check_consistent_attribute("shape")
|
def check_consistent_shape(self) -> None:
shapes_dict = {}
iterable = self.get_images_dict(intensity_only=False).items()
for image_name, image in iterable:
shapes_dict[image_name] = image.shape
num_unique_shapes = len(set(shapes_dict.values()))
if num_unique_shapes > 1:
message = (
"Images in subject have inconsistent shapes:"
f"\n{pprint.pformat(shapes_dict)}"
)
raise ValueError(message)
|
https://github.com/fepegar/torchio/issues/265
|
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-2-6b7dc2edb3cc> in <module>
----> 1 icbm.spatial_shape
~/git/torchio/torchio/data/subject.py in spatial_shape(self)
95 Consistency of shapes across images in the subject is checked first.
96 """
---> 97 return self.shape[1:]
98
99 @property
~/git/torchio/torchio/data/subject.py in shape(self)
85 Consistency of shapes across images in the subject is checked first.
86 """
---> 87 self.check_consistent_shape()
88 image = self.get_images(intensity_only=False)[0]
89 return image.shape
~/git/torchio/torchio/data/subject.py in check_consistent_shape(self)
135 f'\n{pprint.pformat(shapes_dict)}'
136 )
--> 137 raise ValueError(message)
138
139 def check_consistent_orientation(self) -> None:
ValueError: Images in subject have inconsistent shapes:
{'brain': (1, 193, 229, 193),
'eyes': (1, 193, 229, 193),
'pd': (1, 193, 229, 193),
't1': (1, 193, 229, 193),
't2': (1, 193, 229, 193),
'tissues': (3, 193, 229, 193)}
|
ValueError
|
def add_image(self, image: Image, image_name: str) -> None:
self[image_name] = image
self.update_attributes()
|
def add_image(self, image, image_name):
self[image_name] = image
self.update_attributes()
|
https://github.com/fepegar/torchio/issues/265
|
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-2-6b7dc2edb3cc> in <module>
----> 1 icbm.spatial_shape
~/git/torchio/torchio/data/subject.py in spatial_shape(self)
95 Consistency of shapes across images in the subject is checked first.
96 """
---> 97 return self.shape[1:]
98
99 @property
~/git/torchio/torchio/data/subject.py in shape(self)
85 Consistency of shapes across images in the subject is checked first.
86 """
---> 87 self.check_consistent_shape()
88 image = self.get_images(intensity_only=False)[0]
89 return image.shape
~/git/torchio/torchio/data/subject.py in check_consistent_shape(self)
135 f'\n{pprint.pformat(shapes_dict)}'
136 )
--> 137 raise ValueError(message)
138
139 def check_consistent_orientation(self) -> None:
ValueError: Images in subject have inconsistent shapes:
{'brain': (1, 193, 229, 193),
'eyes': (1, 193, 229, 193),
'pd': (1, 193, 229, 193),
't1': (1, 193, 229, 193),
't2': (1, 193, 229, 193),
'tissues': (3, 193, 229, 193)}
|
ValueError
|
def apply_transform(self, sample: Subject) -> dict:
sample.check_consistent_spatial_shape()
params = self.get_params(
self.scales,
self.degrees,
self.translation,
self.isotropic,
)
scaling_params, rotation_params, translation_params = params
for image in self.get_images(sample):
if image[TYPE] != INTENSITY:
interpolation = Interpolation.NEAREST
else:
interpolation = self.interpolation
if image.is_2d():
scaling_params[0] = 1
rotation_params[-2:] = 0
if self.use_image_center:
center = image.get_center(lps=True)
else:
center = None
transformed_tensors = []
for tensor in image[DATA]:
transformed_tensor = self.apply_affine_transform(
tensor,
image[AFFINE],
scaling_params.tolist(),
rotation_params.tolist(),
translation_params.tolist(),
interpolation,
center_lps=center,
)
transformed_tensors.append(transformed_tensor)
image[DATA] = torch.stack(transformed_tensors)
random_parameters_dict = {
"scaling": scaling_params,
"rotation": rotation_params,
"translation": translation_params,
}
sample.add_transform(self, random_parameters_dict)
return sample
|
def apply_transform(self, sample: Subject) -> dict:
sample.check_consistent_shape()
params = self.get_params(
self.scales,
self.degrees,
self.translation,
self.isotropic,
)
scaling_params, rotation_params, translation_params = params
for image in self.get_images(sample):
if image[TYPE] != INTENSITY:
interpolation = Interpolation.NEAREST
else:
interpolation = self.interpolation
if image.is_2d():
scaling_params[0] = 1
rotation_params[-2:] = 0
if self.use_image_center:
center = image.get_center(lps=True)
else:
center = None
transformed_tensors = []
for tensor in image[DATA]:
transformed_tensor = self.apply_affine_transform(
tensor,
image[AFFINE],
scaling_params.tolist(),
rotation_params.tolist(),
translation_params.tolist(),
interpolation,
center_lps=center,
)
transformed_tensors.append(transformed_tensor)
image[DATA] = torch.stack(transformed_tensors)
random_parameters_dict = {
"scaling": scaling_params,
"rotation": rotation_params,
"translation": translation_params,
}
sample.add_transform(self, random_parameters_dict)
return sample
|
https://github.com/fepegar/torchio/issues/265
|
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-2-6b7dc2edb3cc> in <module>
----> 1 icbm.spatial_shape
~/git/torchio/torchio/data/subject.py in spatial_shape(self)
95 Consistency of shapes across images in the subject is checked first.
96 """
---> 97 return self.shape[1:]
98
99 @property
~/git/torchio/torchio/data/subject.py in shape(self)
85 Consistency of shapes across images in the subject is checked first.
86 """
---> 87 self.check_consistent_shape()
88 image = self.get_images(intensity_only=False)[0]
89 return image.shape
~/git/torchio/torchio/data/subject.py in check_consistent_shape(self)
135 f'\n{pprint.pformat(shapes_dict)}'
136 )
--> 137 raise ValueError(message)
138
139 def check_consistent_orientation(self) -> None:
ValueError: Images in subject have inconsistent shapes:
{'brain': (1, 193, 229, 193),
'eyes': (1, 193, 229, 193),
'pd': (1, 193, 229, 193),
't1': (1, 193, 229, 193),
't2': (1, 193, 229, 193),
'tissues': (3, 193, 229, 193)}
|
ValueError
|
def apply_transform(self, sample: Subject) -> dict:
sample.check_consistent_spatial_shape()
bspline_params = self.get_params(
self.num_control_points,
self.max_displacement,
self.num_locked_borders,
)
for image in self.get_images(sample):
if image[TYPE] != INTENSITY:
interpolation = Interpolation.NEAREST
else:
interpolation = self.interpolation
if image.is_2d():
bspline_params[..., -3] = 0 # no displacement in LR axis
image[DATA] = self.apply_bspline_transform(
image[DATA],
image[AFFINE],
bspline_params,
interpolation,
)
random_parameters_dict = {"coarse_grid": bspline_params}
sample.add_transform(self, random_parameters_dict)
return sample
|
def apply_transform(self, sample: Subject) -> dict:
sample.check_consistent_shape()
bspline_params = self.get_params(
self.num_control_points,
self.max_displacement,
self.num_locked_borders,
)
for image in self.get_images(sample):
if image[TYPE] != INTENSITY:
interpolation = Interpolation.NEAREST
else:
interpolation = self.interpolation
if image.is_2d():
bspline_params[..., -3] = 0 # no displacement in LR axis
image[DATA] = self.apply_bspline_transform(
image[DATA],
image[AFFINE],
bspline_params,
interpolation,
)
random_parameters_dict = {"coarse_grid": bspline_params}
sample.add_transform(self, random_parameters_dict)
return sample
|
https://github.com/fepegar/torchio/issues/265
|
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-2-6b7dc2edb3cc> in <module>
----> 1 icbm.spatial_shape
~/git/torchio/torchio/data/subject.py in spatial_shape(self)
95 Consistency of shapes across images in the subject is checked first.
96 """
---> 97 return self.shape[1:]
98
99 @property
~/git/torchio/torchio/data/subject.py in shape(self)
85 Consistency of shapes across images in the subject is checked first.
86 """
---> 87 self.check_consistent_shape()
88 image = self.get_images(intensity_only=False)[0]
89 return image.shape
~/git/torchio/torchio/data/subject.py in check_consistent_shape(self)
135 f'\n{pprint.pformat(shapes_dict)}'
136 )
--> 137 raise ValueError(message)
138
139 def check_consistent_orientation(self) -> None:
ValueError: Images in subject have inconsistent shapes:
{'brain': (1, 193, 229, 193),
'eyes': (1, 193, 229, 193),
'pd': (1, 193, 229, 193),
't1': (1, 193, 229, 193),
't2': (1, 193, 229, 193),
'tissues': (3, 193, 229, 193)}
|
ValueError
|
def _get_sample_shape(sample: Subject) -> TypeTripletInt:
"""Return the shape of the first image in the sample."""
sample.check_consistent_spatial_shape()
for image_dict in sample.get_images(intensity_only=False):
data = image_dict.spatial_shape # remove channels dimension
break
return data
|
def _get_sample_shape(sample: Subject) -> TypeTripletInt:
"""Return the shape of the first image in the sample."""
sample.check_consistent_shape()
for image_dict in sample.get_images(intensity_only=False):
data = image_dict.spatial_shape # remove channels dimension
break
return data
|
https://github.com/fepegar/torchio/issues/265
|
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-2-6b7dc2edb3cc> in <module>
----> 1 icbm.spatial_shape
~/git/torchio/torchio/data/subject.py in spatial_shape(self)
95 Consistency of shapes across images in the subject is checked first.
96 """
---> 97 return self.shape[1:]
98
99 @property
~/git/torchio/torchio/data/subject.py in shape(self)
85 Consistency of shapes across images in the subject is checked first.
86 """
---> 87 self.check_consistent_shape()
88 image = self.get_images(intensity_only=False)[0]
89 return image.shape
~/git/torchio/torchio/data/subject.py in check_consistent_shape(self)
135 f'\n{pprint.pformat(shapes_dict)}'
136 )
--> 137 raise ValueError(message)
138
139 def check_consistent_orientation(self) -> None:
ValueError: Images in subject have inconsistent shapes:
{'brain': (1, 193, 229, 193),
'eyes': (1, 193, 229, 193),
'pd': (1, 193, 229, 193),
't1': (1, 193, 229, 193),
't2': (1, 193, 229, 193),
'tissues': (3, 193, 229, 193)}
|
ValueError
|
def __init__(
self,
num_ghosts: Union[int, Tuple[int, int]] = (4, 10),
axes: Union[int, Tuple[int, ...]] = (0, 1, 2),
intensity: Union[float, Tuple[float, float]] = (0.5, 1),
restore: float = 0.02,
p: float = 1,
seed: Optional[int] = None,
):
super().__init__(p=p, seed=seed)
if not isinstance(axes, tuple):
try:
axes = tuple(axes)
except TypeError:
axes = (axes,)
for axis in axes:
if axis not in (0, 1, 2):
raise ValueError(f'Axes must be in (0, 1, 2), not "{axes}"')
self.axes = axes
self.num_ghosts_range = self.parse_num_ghosts(num_ghosts)
self.intensity_range = self.parse_intensity(intensity)
if not 0 <= restore < 1:
message = f"Restore must be a number between 0 and 1, not {restore}"
raise ValueError(message)
self.restore = restore
|
def __init__(
self,
num_ghosts: Union[int, Tuple[int, int]] = (4, 10),
axes: Union[int, Tuple[int, ...]] = (0, 1, 2),
intensity: Union[float, Tuple[float, float]] = (0.5, 1),
restore: float = 0.02,
p: float = 1,
seed: Optional[int] = None,
):
super().__init__(p=p, seed=seed)
if not isinstance(axes, tuple):
try:
axes = tuple(axes)
except TypeError:
axes = (axes,)
for axis in axes:
if axis not in (0, 1, 2):
raise ValueError(f'Axes must be in (0, 1, 2), not "{axes}"')
self.axes = axes
if isinstance(num_ghosts, int):
self.num_ghosts_range = num_ghosts, num_ghosts
elif isinstance(num_ghosts, tuple) and len(num_ghosts) == 2:
self.num_ghosts_range = num_ghosts
self.intensity_range = self.parse_range(intensity, "intensity")
for n in self.intensity_range:
if n < 0:
message = f"Intensity must be a positive number, not {n}"
raise ValueError(message)
if not 0 <= restore < 1:
message = f"Restore must be a number between 0 and 1, not {restore}"
raise ValueError(message)
self.restore = restore
|
https://github.com/fepegar/torchio/issues/218
|
Traceback (most recent call last):
File "/opt/anaconda3/lib/python3.7/site-packages/IPython/core/interactiveshell.py", line 3326, in run_code
exec(code_obj, self.user_global_ns, self.user_ns)
File "<ipython-input-3-57113dda3e8e>", line 2, in <module>
tranform = torchio.transforms.RandomGhosting(num_ghosts=10, intensity=1)
File ".../torchio/torchio/transforms/augmentation/intensity/random_ghosting.py", line 63, in __init__
raise ValueError(message)
ValueError: Intensity must be a positive number, not -1
|
ValueError
|
def __getitem__(self, index: int) -> dict:
if not isinstance(index, int):
raise ValueError(f'Index "{index}" must be int, not {type(index)}')
subject = self.subjects[index]
sample = copy.deepcopy(subject)
sample.load()
# Apply transform (this is usually the bottleneck)
if self._transform is not None:
sample = self._transform(sample)
return sample
|
def __getitem__(self, index: int) -> dict:
if not isinstance(index, int):
raise ValueError(f'Index "{index}" must be int, not {type(index)}')
subject = self.subjects[index]
sample = copy.deepcopy(subject)
# Apply transform (this is usually the bottleneck)
if self._transform is not None:
sample = self._transform(sample)
return sample
|
https://github.com/fepegar/torchio/issues/209
|
torch.Size([1, 348, 272, 108])
torch.Size([1, 348, 272, 108])
Traceback (most recent call last):
File "Code/non_local_feature_alignment/dataset_manager.py", line 88, in <module>
print(b["img"][torchio.DATA])
KeyError: 'data'
|
KeyError
|
def load(self) -> Tuple[torch.Tensor, np.ndarray]:
r"""Load the image from disk.
The file is expected to be monomodal/grayscale and 2D or 3D.
A channels dimension is added to the tensor.
Returns:
Tuple containing a 4D data tensor of size
:math:`(1, D_{in}, H_{in}, W_{in})`
and a 2D 4x4 affine matrix
"""
if self.path is None:
return
tensor, affine = read_image(self.path)
# https://github.com/pytorch/pytorch/issues/9410#issuecomment-404968513
tensor = tensor[(None,) * (3 - tensor.ndim)] # force to be 3D
# Remove next line and uncomment the two following ones once/if this issue
# gets fixed:
# https://github.com/pytorch/pytorch/issues/29010
# See also https://discuss.pytorch.org/t/collating-named-tensors/78650/4
tensor = tensor.unsqueeze(0) # add channels dimension
# name_dimensions(tensor, affine)
# tensor = tensor.align_to('channels', ...)
if self.check_nans and torch.isnan(tensor).any():
warnings.warn(f'NaNs found in file "{self.path}"')
self[DATA] = tensor
self[AFFINE] = affine
self._loaded = True
|
def load(self) -> Tuple[torch.Tensor, np.ndarray]:
r"""Load the image from disk.
The file is expected to be monomodal/grayscale and 2D or 3D.
A channels dimension is added to the tensor.
Returns:
Tuple containing a 4D data tensor of size
:math:`(1, D_{in}, H_{in}, W_{in})`
and a 2D 4x4 affine matrix
"""
if self.path is None:
raise RuntimeError("No path provided for instance of Image")
tensor, affine = read_image(self.path)
# https://github.com/pytorch/pytorch/issues/9410#issuecomment-404968513
tensor = tensor[(None,) * (3 - tensor.ndim)] # force to be 3D
# Remove next line and uncomment the two following ones once/if this issue
# gets fixed:
# https://github.com/pytorch/pytorch/issues/29010
# See also https://discuss.pytorch.org/t/collating-named-tensors/78650/4
tensor = tensor.unsqueeze(0) # add channels dimension
# name_dimensions(tensor, affine)
# tensor = tensor.align_to('channels', ...)
if self.check_nans and torch.isnan(tensor).any():
warnings.warn(f'NaNs found in file "{self.path}"')
self[DATA] = tensor
self[AFFINE] = affine
self._loaded = True
|
https://github.com/fepegar/torchio/issues/209
|
torch.Size([1, 348, 272, 108])
torch.Size([1, 348, 272, 108])
Traceback (most recent call last):
File "Code/non_local_feature_alignment/dataset_manager.py", line 88, in <module>
print(b["img"][torchio.DATA])
KeyError: 'data'
|
KeyError
|
def __init__(self, *args, **kwargs):
scene.visuals.Line.__init__(self, *args, **kwargs)
self.unfreeze()
# initialize point markers
self.markers = scene.visuals.Markers(parent=self)
self.marker_colors = np.ones((len(self.pos), 4), dtype=np.float32)
self.markers.set_data(pos=self.pos, symbol="s", edge_color="red", size=6)
self.selected_point = None
self.selected_index = -1
# snap grid size
self.gridsize = 10
self.freeze()
|
def __init__(self, *args, **kwargs):
scene.visuals.Line.__init__(self, *args, **kwargs)
# initialize point markers
self.markers = scene.visuals.Markers()
self.marker_colors = np.ones((len(self.pos), 4), dtype=np.float32)
self.markers.set_data(pos=self.pos, symbol="s", edge_color="red", size=6)
self.selected_point = None
self.selected_index = -1
# snap grid size
self.gridsize = 10
|
https://github.com/vispy/vispy/issues/1455
|
Traceback (most recent call last):
File "/home/........../basics/visuals/line_draw.py", line 178, in <module>
win = Canvas()
File "/home/........../basics/visuals/line_draw.py", line 154, in __init__
self.pos = np.zeros((n, 3), dtype=np.float32)
File "/home/........../lib/python3.6/site-packages/vispy/util/frozen.py", line 16, in __setattr__
'attributes' % (key, self))
AttributeError: 'pos' is not an attribute of class <Canvas (PyQt5) at 0x7f4334088f98>. Call "unfreeze()" to allow addition of new attributes
Process finished with exit code 1
|
AttributeError
|
def select_point(self, pos_scene, radius=5):
"""
Get line point close to mouse pointer and its index
Parameters
----------
event : the mouse event being processed
radius : scalar
max. distance in pixels between mouse and line point to be accepted
return: (numpy.array, int)
picked point and index of the point in the pos array
"""
# project mouse radius from screen coordinates to document coordinates
mouse_radius = 6
# print("Mouse radius in document units: ", mouse_radius)
# find first point within mouse_radius
index = 0
for p in self.pos:
if np.linalg.norm(pos_scene[:3] - p) < mouse_radius:
# print p, index
# point found, return point and its index
return p, index
index += 1
# no point found, return None
return None, -1
|
def select_point(self, event, radius=5):
"""
Get line point close to mouse pointer and its index
Parameters
----------
event : the mouse event being processed
radius : scalar
max. distance in pixels between mouse and line point to be accepted
return: (numpy.array, int)
picked point and index of the point in the pos array
"""
# position in scene/document coordinates
pos_scene = event.pos[:3]
# project mouse radius from screen coordinates to document coordinates
mouse_radius = (
event.visual_to_canvas.imap(np.array([radius, radius, radius]))
- event.visual_to_canvas.imap(np.array([0, 0, 0]))
)[0]
# print("Mouse radius in document units: ", mouse_radius)
# find first point within mouse_radius
index = 0
for p in self.pos:
if np.linalg.norm(pos_scene - p) < mouse_radius:
# print p, index
# point found, return point and its index
return p, index
index += 1
# no point found, return None
return None, -1
|
https://github.com/vispy/vispy/issues/1455
|
Traceback (most recent call last):
File "/home/........../basics/visuals/line_draw.py", line 178, in <module>
win = Canvas()
File "/home/........../basics/visuals/line_draw.py", line 154, in __init__
self.pos = np.zeros((n, 3), dtype=np.float32)
File "/home/........../lib/python3.6/site-packages/vispy/util/frozen.py", line 16, in __setattr__
'attributes' % (key, self))
AttributeError: 'pos' is not an attribute of class <Canvas (PyQt5) at 0x7f4334088f98>. Call "unfreeze()" to allow addition of new attributes
Process finished with exit code 1
|
AttributeError
|
def on_mouse_press(self, pos_scene):
# self.print_mouse_event(event, 'Mouse press')
# pos_scene = event.pos[:3]
# find closest point to mouse and select it
self.selected_point, self.selected_index = self.select_point(pos_scene)
# if no point was clicked add a new one
if self.selected_point is None:
print("adding point", len(self.pos))
self._pos = np.append(self.pos, [pos_scene[:3]], axis=0)
self.set_data(pos=self.pos)
self.marker_colors = np.ones((len(self.pos), 4), dtype=np.float32)
self.selected_point = self.pos[-1]
self.selected_index = len(self.pos) - 1
# update markers and highlights
self.update_markers(self.selected_index)
|
def on_mouse_press(self, event):
self.print_mouse_event(event, "Mouse press")
pos_scene = event.pos[:3]
# find closest point to mouse and select it
self.selected_point, self.selected_index = self.select_point(event)
# if no point was clicked add a new one
if self.selected_point is None:
print("adding point", len(self.pos))
self._pos = np.append(self.pos, [pos_scene], axis=0)
self.set_data(pos=self.pos)
self.marker_colors = np.ones((len(self.pos), 4), dtype=np.float32)
self.selected_point = self.pos[-1]
self.selected_index = len(self.pos) - 1
# update markers and highlights
self.update_markers(self.selected_index)
|
https://github.com/vispy/vispy/issues/1455
|
Traceback (most recent call last):
File "/home/........../basics/visuals/line_draw.py", line 178, in <module>
win = Canvas()
File "/home/........../basics/visuals/line_draw.py", line 154, in __init__
self.pos = np.zeros((n, 3), dtype=np.float32)
File "/home/........../lib/python3.6/site-packages/vispy/util/frozen.py", line 16, in __setattr__
'attributes' % (key, self))
AttributeError: 'pos' is not an attribute of class <Canvas (PyQt5) at 0x7f4334088f98>. Call "unfreeze()" to allow addition of new attributes
Process finished with exit code 1
|
AttributeError
|
def on_mouse_move(self, pos_scene):
if self.selected_point is not None:
# update selected point to new position given by mouse
self.selected_point[0] = round(pos_scene[0] / self.gridsize) * self.gridsize
self.selected_point[1] = round(pos_scene[1] / self.gridsize) * self.gridsize
self.set_data(pos=self.pos)
self.update_markers(self.selected_index)
|
def on_mouse_move(self, event):
# left mouse button
if event.button == 1:
# self.print_mouse_event(event, 'Mouse drag')
if self.selected_point is not None:
pos_scene = event.pos
# update selected point to new position given by mouse
self.selected_point[0] = round(pos_scene[0] / self.gridsize) * self.gridsize
self.selected_point[1] = round(pos_scene[1] / self.gridsize) * self.gridsize
self.set_data(pos=self.pos)
self.update_markers(self.selected_index)
else:
# if no button is pressed, just highlight the marker that would be
# selected on click
hl_point, hl_index = self.select_point(event)
self.update_markers(hl_index, highlight_color=(0.5, 0.5, 1.0, 1.0))
self.update()
|
https://github.com/vispy/vispy/issues/1455
|
Traceback (most recent call last):
File "/home/........../basics/visuals/line_draw.py", line 178, in <module>
win = Canvas()
File "/home/........../basics/visuals/line_draw.py", line 154, in __init__
self.pos = np.zeros((n, 3), dtype=np.float32)
File "/home/........../lib/python3.6/site-packages/vispy/util/frozen.py", line 16, in __setattr__
'attributes' % (key, self))
AttributeError: 'pos' is not an attribute of class <Canvas (PyQt5) at 0x7f4334088f98>. Call "unfreeze()" to allow addition of new attributes
Process finished with exit code 1
|
AttributeError
|
def __init__(self):
scene.SceneCanvas.__init__(self, keys="interactive", size=(800, 800))
# Create some initial points
n = 7
self.unfreeze()
self.pos = np.zeros((n, 3), dtype=np.float32)
self.pos[:, 0] = np.linspace(-50, 50, n)
self.pos[:, 1] = np.random.normal(size=n, scale=10, loc=0)
# create new editable line
self.line = EditLineVisual(
pos=self.pos, color="w", width=3, antialias=True, method="gl"
)
self.view = self.central_widget.add_view()
self.view.camera = scene.PanZoomCamera(rect=(-100, -100, 200, 200), aspect=1.0)
# the left mouse button pan has to be disabled in the camera, as it
# interferes with dragging line points
# Proposed change in camera: make mouse buttons configurable
self.view.camera._viewbox.events.mouse_move.disconnect(
self.view.camera.viewbox_mouse_event
)
self.view.add(self.line)
self.show()
self.selected_point = None
scene.visuals.GridLines(parent=self.view.scene)
self.freeze()
|
def __init__(self):
scene.SceneCanvas.__init__(self, keys="interactive", size=(800, 800))
# Create some initial points
n = 7
self.pos = np.zeros((n, 3), dtype=np.float32)
self.pos[:, 0] = np.linspace(-50, 50, n)
self.pos[:, 1] = np.random.normal(size=n, scale=10, loc=0)
# create new editable line
self.line = EditLineVisual(
pos=self.pos, color="w", width=3, antialias=True, method="gl"
)
self.view = self.central_widget.add_view()
self.view.camera = scene.PanZoomCamera(rect=(-100, -100, 200, 200), aspect=1.0)
# the left mouse button pan has to be disabled in the camera, as it
# interferes with dragging line points
# Proposed change in camera: make mouse buttons configurable
self.view.camera._viewbox.events.mouse_move.disconnect(
self.view.camera.viewbox_mouse_event
)
self.view.add(self.line)
self.show()
self.selected_point = None
scene.visuals.GridLines(parent=self.view.scene)
|
https://github.com/vispy/vispy/issues/1455
|
Traceback (most recent call last):
File "/home/........../basics/visuals/line_draw.py", line 178, in <module>
win = Canvas()
File "/home/........../basics/visuals/line_draw.py", line 154, in __init__
self.pos = np.zeros((n, 3), dtype=np.float32)
File "/home/........../lib/python3.6/site-packages/vispy/util/frozen.py", line 16, in __setattr__
'attributes' % (key, self))
AttributeError: 'pos' is not an attribute of class <Canvas (PyQt5) at 0x7f4334088f98>. Call "unfreeze()" to allow addition of new attributes
Process finished with exit code 1
|
AttributeError
|
def _use(self, backend_name=None):
"""Select a backend by name. See class docstring for details."""
# See if we're in a specific testing mode, if so DONT check to see
# if it's a valid backend. If it isn't, it's a good thing we
# get an error later because we should have decorated our test
# with requires_application()
test_name = os.getenv("_VISPY_TESTING_APP", None)
# Check whether the given name is valid
if backend_name is not None:
if backend_name.lower() == "default":
backend_name = None # Explicitly use default, avoid using test
elif backend_name.lower() not in BACKENDMAP:
raise ValueError(
"backend_name must be one of %s or None, not "
"%r" % (BACKEND_NAMES, backend_name)
)
elif test_name is not None:
backend_name = test_name.lower()
assert backend_name in BACKENDMAP
# Should we try and load any backend, or just this specific one?
try_others = backend_name is None
# Get backends to try ...
imported_toolkits = [] # Backends for which the native lib is imported
backends_to_try = []
if not try_others:
# We should never hit this, since we check above
assert backend_name.lower() in BACKENDMAP.keys()
# Add it
backends_to_try.append(backend_name.lower())
else:
# See if a backend is loaded
for name, module_name, native_module_name in CORE_BACKENDS:
if native_module_name and native_module_name in sys.modules:
imported_toolkits.append(name.lower())
backends_to_try.append(name.lower())
# See if a default is given
default_backend = config["default_backend"].lower()
if default_backend.lower() in BACKENDMAP.keys():
if default_backend not in backends_to_try:
backends_to_try.append(default_backend)
# After this, try each one
for name, module_name, native_module_name in CORE_BACKENDS:
name = name.lower()
if name not in backends_to_try:
backends_to_try.append(name)
# Now try each one
for key in backends_to_try:
name, module_name, native_module_name = BACKENDMAP[key]
TRIED_BACKENDS.append(name)
mod_name = "backends." + module_name
__import__(mod_name, globals(), level=1)
mod = getattr(backends, module_name)
if not mod.available:
msg = 'Could not import backend "%s":\n%s' % (name, str(mod.why_not))
if not try_others:
# Fail if user wanted to use a specific backend
raise RuntimeError(msg)
elif key in imported_toolkits:
# Warn if were unable to use an already imported toolkit
msg = (
"Although %s is already imported, the %s backend "
'could not\nbe used ("%s"). \nNote that running '
"multiple GUI toolkits simultaneously can cause "
"side effects." % (native_module_name, name, str(mod.why_not))
)
logger.warning(msg)
else:
# Inform otherwise
logger.info(msg)
else:
# Success!
self._backend_module = mod
logger.debug("Selected backend %s" % module_name)
break
else:
raise RuntimeError(
"Could not import any of the backends. "
"You need to install any of %s. We recommend "
"PyQt" % [b[0] for b in CORE_BACKENDS]
)
# Store classes for app backend and canvas backend
self._backend = self.backend_module.ApplicationBackend()
|
def _use(self, backend_name=None):
"""Select a backend by name. See class docstring for details."""
# See if we're in a specific testing mode, if so DONT check to see
# if it's a valid backend. If it isn't, it's a good thing we
# get an error later because we should have decorated our test
# with requires_application()
test_name = os.getenv("_VISPY_TESTING_APP", None)
# Check whether the given name is valid
if backend_name is not None:
if backend_name.lower() == "default":
backend_name = None # Explicitly use default, avoid using test
elif backend_name.lower() not in BACKENDMAP:
raise ValueError(
"backend_name must be one of %s or None, not "
"%r" % (BACKEND_NAMES, backend_name)
)
elif test_name is not None:
backend_name = test_name.lower()
assert backend_name in BACKENDMAP
# Should we try and load any backend, or just this specific one?
try_others = backend_name is None
# Get backends to try ...
imported_toolkits = [] # Backends for which the native lib is imported
backends_to_try = []
if not try_others:
# We should never hit this, since we check above
assert backend_name.lower() in BACKENDMAP.keys()
# Add it
backends_to_try.append(backend_name.lower())
else:
# See if a backend is loaded
for name, module_name, native_module_name in CORE_BACKENDS:
if native_module_name and native_module_name in sys.modules:
imported_toolkits.append(name.lower())
backends_to_try.append(name.lower())
# See if a default is given
default_backend = config["default_backend"].lower()
if default_backend.lower() in BACKENDMAP.keys():
if default_backend not in backends_to_try:
backends_to_try.append(default_backend)
# After this, try each one
for name, module_name, native_module_name in CORE_BACKENDS:
name = name.lower()
if name not in backends_to_try:
backends_to_try.append(name)
# Now try each one
for key in backends_to_try:
name, module_name, native_module_name = BACKENDMAP[key]
TRIED_BACKENDS.append(name)
mod_name = "backends." + module_name
__import__(mod_name, globals(), level=1)
mod = getattr(backends, module_name)
if not mod.available:
msg = 'Could not import backend "%s":\n%s' % (name, str(mod.why_not))
if not try_others:
# Fail if user wanted to use a specific backend
raise RuntimeError(msg)
elif key in imported_toolkits:
# Warn if were unable to use an already imported toolkit
msg = (
"Although %s is already imported, the %s backend "
'could not\nbe used ("%s"). \nNote that running '
"multiple GUI toolkits simultaneously can cause "
"side effects." % (native_module_name, name, str(mod.why_not))
)
logger.warning(msg)
else:
# Inform otherwise
logger.info(msg)
else:
# Success!
self._backend_module = mod
logger.debug("Selected backend %s" % module_name)
break
else:
raise RuntimeError("Could not import any of the backends.")
# Store classes for app backend and canvas backend
self._backend = self.backend_module.ApplicationBackend()
|
https://github.com/vispy/vispy/issues/582
|
RuntimeError Traceback (most recent call last)
<ipython-input-2-b622cb67980d> in <module>()
----> 1 c = app.Canvas(keys='interactive')
/usr/local/lib/python2.7/dist-packages/vispy-0.3.0-py2.7.egg/vispy/app/canvas.pyc in __init__(self, title, size, position, show, autoswap, app, create_native, init_gloo, vsync, resizable, decorate, fullscreen, context, keys, parent)
134 # Get app instance
135 if app is None:
--> 136 self._app = use_app()
137 elif isinstance(app, Application):
138 self._app = app
/usr/local/lib/python2.7/dist-packages/vispy-0.3.0-py2.7.egg/vispy/app/_default_app.pyc in use_app(backend_name)
37
38 # Create default app
---> 39 default_app = Application(backend_name)
40 return default_app
41
/usr/local/lib/python2.7/dist-packages/vispy-0.3.0-py2.7.egg/vispy/app/application.pyc in __init__(self, backend_name)
46 self._backend_module = None
47 self._backend = None
---> 48 self._use(backend_name)
49
50 def __repr__(self):
/usr/local/lib/python2.7/dist-packages/vispy-0.3.0-py2.7.egg/vispy/app/application.pyc in _use(self, backend_name)
177 break
178 else:
--> 179 raise RuntimeError('Could not import any of the backends.')
180
181 # Store classes for app backend and canvas backend
RuntimeError: Could not import any of the backends.
|
RuntimeError
|
def _set_keys(self, keys):
if keys is not None:
if isinstance(keys, string_types):
if keys != "interactive":
raise ValueError(
'keys, if string, must be "interactive", not %s' % (keys,)
)
def toggle_fs():
self.fullscreen = not self.fullscreen
keys = dict(escape="close", F11=toggle_fs)
else:
keys = {}
if not isinstance(keys, dict):
raise TypeError("keys must be a dict, str, or None")
if len(keys) > 0:
# ensure all are callable
for key, val in keys.items():
if isinstance(val, string_types):
new_val = getattr(self, val, None)
if new_val is None:
raise ValueError("value %s is not an attribute of Canvas" % val)
val = new_val
if not hasattr(val, "__call__"):
raise TypeError("Entry for key %s is not callable" % key)
# convert to lower-case representation
keys.pop(key)
keys[key.lower()] = val
self._keys_check = keys
def keys_check(event):
if event.key is not None:
use_name = event.key.name.lower()
if use_name in self._keys_check:
self._keys_check[use_name]()
self.events.key_press.connect(keys_check, ref=True)
|
def _set_keys(self, keys):
if keys is not None:
if isinstance(keys, string_types):
if keys != "interactive":
raise ValueError(
'keys, if string, must be "interactive", not %s' % (keys,)
)
def toggle_fs():
self.fullscreen = not self.fullscreen
keys = dict(escape="close", F11=toggle_fs)
else:
keys = {}
if not isinstance(keys, dict):
raise TypeError("keys must be a dict, str, or None")
if len(keys) > 0:
# ensure all are callable
for key, val in keys.items():
if isinstance(val, string_types):
new_val = getattr(self, val, None)
if new_val is None:
raise ValueError("value %s is not an attribute of Canvas" % val)
val = new_val
if not hasattr(val, "__call__"):
raise TypeError("Entry for key %s is not callable" % key)
# convert to lower-case representation
keys.pop(key)
keys[key.lower()] = val
self._keys_check = keys
def keys_check(event):
use_name = event.key.name.lower()
if use_name in self._keys_check:
self._keys_check[use_name]()
self.events.key_press.connect(keys_check, ref=True)
|
https://github.com/vispy/vispy/issues/542
|
WARNING: Traceback (most recent call last):
File ".\examples\demo\gloo\brain.py", line 156, in <module>
app.run()
File "D:\Git\vispy\vispy\app\_default_app.py", line 54, in run
return default_app.run()
File "D:\Git\vispy\vispy\app\application.py", line 88, in run
return self._backend._vispy_run()
File "D:\Git\vispy\vispy\app\backends\_qt.py", line 175, in _vispy_run
return app.exec_()
File "D:\Git\vispy\vispy\app\backends\_qt.py", line 391, in keyPressEvent
self._keyEvent(self._vispy_canvas.events.key_press, ev)
File "D:\Git\vispy\vispy\app\backends\_qt.py", line 406, in _keyEvent
func(native=ev, key=key, text=text_type(ev.text()), modifiers=mod)
File "D:\Git\vispy\vispy\util\event.py", line 418, in __call__
self._invoke_callback(cb, event)
File "D:\Git\vispy\vispy\util\event.py", line 446, in _invoke_callback
self, cb_event=(cb, event))
<< caught exception here: >>
File "D:\Git\vispy\vispy\util\event.py", line 435, in _invoke_callback
cb(event)
File "D:\Git\vispy\vispy\app\canvas.py", line 220, in keys_check
use_name = event.key.name.lower()
AttributeError: 'NoneType' object has no attribute 'name'
WARNING: Error invoking callback <function keys_check at 0x00000000067C73C8> for
event: <KeyEvent blocked=False handled=False key=None modifiers=() native=<PyQt
4.QtGui.QKeyEvent object at 0x0000000007230BF8> source=<Vispy canvas (PyQt4 (qt)
backend) at 0x72a10f0L> sources=[<Vispy canvas (PyQt4 (qt) backend) at 0x72a10f
0L>] text=² type=key_press>
|
AttributeError
|
def keys_check(event):
if event.key is not None:
use_name = event.key.name.lower()
if use_name in self._keys_check:
self._keys_check[use_name]()
|
def keys_check(event):
use_name = event.key.name.lower()
if use_name in self._keys_check:
self._keys_check[use_name]()
|
https://github.com/vispy/vispy/issues/542
|
WARNING: Traceback (most recent call last):
File ".\examples\demo\gloo\brain.py", line 156, in <module>
app.run()
File "D:\Git\vispy\vispy\app\_default_app.py", line 54, in run
return default_app.run()
File "D:\Git\vispy\vispy\app\application.py", line 88, in run
return self._backend._vispy_run()
File "D:\Git\vispy\vispy\app\backends\_qt.py", line 175, in _vispy_run
return app.exec_()
File "D:\Git\vispy\vispy\app\backends\_qt.py", line 391, in keyPressEvent
self._keyEvent(self._vispy_canvas.events.key_press, ev)
File "D:\Git\vispy\vispy\app\backends\_qt.py", line 406, in _keyEvent
func(native=ev, key=key, text=text_type(ev.text()), modifiers=mod)
File "D:\Git\vispy\vispy\util\event.py", line 418, in __call__
self._invoke_callback(cb, event)
File "D:\Git\vispy\vispy\util\event.py", line 446, in _invoke_callback
self, cb_event=(cb, event))
<< caught exception here: >>
File "D:\Git\vispy\vispy\util\event.py", line 435, in _invoke_callback
cb(event)
File "D:\Git\vispy\vispy\app\canvas.py", line 220, in keys_check
use_name = event.key.name.lower()
AttributeError: 'NoneType' object has no attribute 'name'
WARNING: Error invoking callback <function keys_check at 0x00000000067C73C8> for
event: <KeyEvent blocked=False handled=False key=None modifiers=() native=<PyQt
4.QtGui.QKeyEvent object at 0x0000000007230BF8> source=<Vispy canvas (PyQt4 (qt)
backend) at 0x72a10f0L> sources=[<Vispy canvas (PyQt4 (qt) backend) at 0x72a10f
0L>] text=² type=key_press>
|
AttributeError
|
def checkerboard(grid_num=8, grid_size=32):
row_even = grid_num // 2 * [0, 1]
row_odd = grid_num // 2 * [1, 0]
Z = np.row_stack(grid_num // 2 * (row_even, row_odd)).astype(np.uint8)
return 255 * Z.repeat(grid_size, axis=0).repeat(grid_size, axis=1)
|
def checkerboard(grid_num=8, grid_size=32):
row_even = grid_num / 2 * [0, 1]
row_odd = grid_num / 2 * [1, 0]
Z = np.row_stack(grid_num / 2 * (row_even, row_odd)).astype(np.uint8)
return 255 * Z.repeat(grid_size, axis=0).repeat(grid_size, axis=1)
|
https://github.com/vispy/vispy/issues/525
|
WARNING: normal has not been bound
WARNING: color has not been bound
WARNING: Traceback (most recent call last):
File "textured_cube.py", line 102, in <module>
c.show()
File "/home/cyrille/git/vispy/vispy/app/canvas.py", line 320, in show
return self._backend._vispy_set_visible(visible)
File "/home/cyrille/git/vispy/vispy/app/backends/_qt.py", line 288, in _vispy_set_visible
self.showNormal()
File "/home/cyrille/git/vispy/vispy/app/backends/_qt.py", line 322, in initializeGL
self._vispy_canvas.events.initialize()
File "/home/cyrille/git/vispy/vispy/util/event.py", line 418, in __call__
self._invoke_callback(cb, event)
File "/home/cyrille/git/vispy/vispy/util/event.py", line 446, in _invoke_callback
self, cb_event=(cb, event))
<< caught exception here: >>
File "/home/cyrille/git/vispy/vispy/util/event.py", line 435, in _invoke_callback
cb(event)
File "textured_cube.py", line 73, in on_initialize
self.program['texture'] = checkerboard()
File "textured_cube.py", line 45, in checkerboard
row_even = grid_num / 2 * [0, 1]
TypeError: can't multiply sequence by non-int of type 'float'
WARNING: Error invoking callback <bound method Canvas.on_initialize of <Vispy canvas (PyQt4 (qt) backend) at 0x7fe29c5ce0f0>> for event: <Event blocked=False handled=False native=None source=<Vispy canvas (PyQt4 (qt) backend) at 0x7fe29c5ce0f0> sources=[<Vispy canvas (PyQt4 (qt) backend) at 0x7fe29c5ce0f0>] type=initialize>
WARNING: Traceback (most recent call last):
File "textured_cube.py", line 103, in <module>
app.run()
File "/home/cyrille/git/vispy/vispy/app/_default_app.py", line 54, in run
return default_app.run()
File "/home/cyrille/git/vispy/vispy/app/application.py", line 88, in run
return self._backend._vispy_run()
File "/home/cyrille/git/vispy/vispy/app/backends/_qt.py", line 175, in _vispy_run
return app.exec_()
File "/home/cyrille/git/vispy/vispy/app/backends/_qt.py", line 334, in paintGL
self._vispy_canvas.events.draw(region=None)
File "/home/cyrille/git/vispy/vispy/util/event.py", line 418, in __call__
self._invoke_callback(cb, event)
File "/home/cyrille/git/vispy/vispy/util/event.py", line 446, in _invoke_callback
self, cb_event=(cb, event))
<< caught exception here: >>
File "/home/cyrille/git/vispy/vispy/util/event.py", line 435, in _invoke_callback
cb(event)
File "textured_cube.py", line 83, in on_draw
self.program.draw('triangles', self.indices)
File "/home/cyrille/git/vispy/vispy/gloo/program.py", line 526, in draw
self.deactivate()
File "/home/cyrille/git/vispy/vispy/gloo/globject.py", line 61, in deactivate
self._deactivate()
File "/home/cyrille/git/vispy/vispy/gloo/program.py", line 225, in _deactivate
self._deactivate_variables()
File "/home/cyrille/git/vispy/vispy/gloo/program.py", line 324, in _deactivate_variables
uniform.deactivate()
File "/home/cyrille/git/vispy/vispy/gloo/globject.py", line 61, in deactivate
self._deactivate()
File "/home/cyrille/git/vispy/vispy/gloo/variable.py", line 232, in _deactivate
self.data.deactivate()
AttributeError: 'numpy.ndarray' object has no attribute 'deactivate'
WARNING: Error invoking callback <bound method Canvas.on_draw of <Vispy canvas (PyQt4 (qt) backend) at 0x7fe29c5ce0f0>> for event: <DrawEvent blocked=False handled=False native=None region=None source=<Vispy canvas (PyQt4 (qt) backend) at 0x7fe29c5ce0f0> sources=[<Vispy canvas (PyQt4 (qt) backend) at 0x7fe29c5ce0f0>] type=draw>
WARNING: Error invoking callback <bound method Canvas.on_draw of <Vispy canvas (PyQt4 (qt) backend) at 0x7fe29c5ce0f0>> repeat 2
WARNING: Error invoking callback <bound method Canvas.on_draw of <Vispy canvas (PyQt4 (qt) backend) at 0x7fe29c5ce0f0>> repeat 3
`̀``
|
TypeError
|
def _deactivate(self):
if self._gtype in (gl.GL_SAMPLER_2D, GL_SAMPLER_3D):
# gl.glActiveTexture(gl.GL_TEXTURE0 + self._unit)
if self.data is not None:
if isinstance(self._data, BaseTexture):
self.data.deactivate()
|
def _deactivate(self):
if self._gtype in (gl.GL_SAMPLER_2D, GL_SAMPLER_3D):
# gl.glActiveTexture(gl.GL_TEXTURE0 + self._unit)
if self.data is not None:
self.data.deactivate()
|
https://github.com/vispy/vispy/issues/525
|
WARNING: normal has not been bound
WARNING: color has not been bound
WARNING: Traceback (most recent call last):
File "textured_cube.py", line 102, in <module>
c.show()
File "/home/cyrille/git/vispy/vispy/app/canvas.py", line 320, in show
return self._backend._vispy_set_visible(visible)
File "/home/cyrille/git/vispy/vispy/app/backends/_qt.py", line 288, in _vispy_set_visible
self.showNormal()
File "/home/cyrille/git/vispy/vispy/app/backends/_qt.py", line 322, in initializeGL
self._vispy_canvas.events.initialize()
File "/home/cyrille/git/vispy/vispy/util/event.py", line 418, in __call__
self._invoke_callback(cb, event)
File "/home/cyrille/git/vispy/vispy/util/event.py", line 446, in _invoke_callback
self, cb_event=(cb, event))
<< caught exception here: >>
File "/home/cyrille/git/vispy/vispy/util/event.py", line 435, in _invoke_callback
cb(event)
File "textured_cube.py", line 73, in on_initialize
self.program['texture'] = checkerboard()
File "textured_cube.py", line 45, in checkerboard
row_even = grid_num / 2 * [0, 1]
TypeError: can't multiply sequence by non-int of type 'float'
WARNING: Error invoking callback <bound method Canvas.on_initialize of <Vispy canvas (PyQt4 (qt) backend) at 0x7fe29c5ce0f0>> for event: <Event blocked=False handled=False native=None source=<Vispy canvas (PyQt4 (qt) backend) at 0x7fe29c5ce0f0> sources=[<Vispy canvas (PyQt4 (qt) backend) at 0x7fe29c5ce0f0>] type=initialize>
WARNING: Traceback (most recent call last):
File "textured_cube.py", line 103, in <module>
app.run()
File "/home/cyrille/git/vispy/vispy/app/_default_app.py", line 54, in run
return default_app.run()
File "/home/cyrille/git/vispy/vispy/app/application.py", line 88, in run
return self._backend._vispy_run()
File "/home/cyrille/git/vispy/vispy/app/backends/_qt.py", line 175, in _vispy_run
return app.exec_()
File "/home/cyrille/git/vispy/vispy/app/backends/_qt.py", line 334, in paintGL
self._vispy_canvas.events.draw(region=None)
File "/home/cyrille/git/vispy/vispy/util/event.py", line 418, in __call__
self._invoke_callback(cb, event)
File "/home/cyrille/git/vispy/vispy/util/event.py", line 446, in _invoke_callback
self, cb_event=(cb, event))
<< caught exception here: >>
File "/home/cyrille/git/vispy/vispy/util/event.py", line 435, in _invoke_callback
cb(event)
File "textured_cube.py", line 83, in on_draw
self.program.draw('triangles', self.indices)
File "/home/cyrille/git/vispy/vispy/gloo/program.py", line 526, in draw
self.deactivate()
File "/home/cyrille/git/vispy/vispy/gloo/globject.py", line 61, in deactivate
self._deactivate()
File "/home/cyrille/git/vispy/vispy/gloo/program.py", line 225, in _deactivate
self._deactivate_variables()
File "/home/cyrille/git/vispy/vispy/gloo/program.py", line 324, in _deactivate_variables
uniform.deactivate()
File "/home/cyrille/git/vispy/vispy/gloo/globject.py", line 61, in deactivate
self._deactivate()
File "/home/cyrille/git/vispy/vispy/gloo/variable.py", line 232, in _deactivate
self.data.deactivate()
AttributeError: 'numpy.ndarray' object has no attribute 'deactivate'
WARNING: Error invoking callback <bound method Canvas.on_draw of <Vispy canvas (PyQt4 (qt) backend) at 0x7fe29c5ce0f0>> for event: <DrawEvent blocked=False handled=False native=None region=None source=<Vispy canvas (PyQt4 (qt) backend) at 0x7fe29c5ce0f0> sources=[<Vispy canvas (PyQt4 (qt) backend) at 0x7fe29c5ce0f0>] type=draw>
WARNING: Error invoking callback <bound method Canvas.on_draw of <Vispy canvas (PyQt4 (qt) backend) at 0x7fe29c5ce0f0>> repeat 2
WARNING: Error invoking callback <bound method Canvas.on_draw of <Vispy canvas (PyQt4 (qt) backend) at 0x7fe29c5ce0f0>> repeat 3
`̀``
|
TypeError
|
def on_draw(self, event):
self.framebuffer.activate()
set_viewport(0, 0, 512, 512)
clear(color=True, depth=True)
set_state(depth_test=True)
self.cube.draw("triangles", self.indices)
self.framebuffer.deactivate()
set_viewport(0, 0, *self.size)
clear(color=True)
set_state(depth_test=False)
self.quad.draw("triangle_strip")
|
def on_draw(self, event):
self.framebuffer.activate()
set_viewport(0, 0, 512, 512)
clear(color=True, depth=True)
set_state(depth_test=True)
self.cube.draw("triangles", self.indices)
self.framebuffer.deactivate()
clear(color=True)
set_state(depth_test=False)
self.quad.draw("triangle_strip")
|
https://github.com/vispy/vispy/issues/528
|
cyrille@Cyrille-ASUS:~/git/vispy/examples/demo/gloo$ python realtime_signals.py ERROR: Could not set variable 'a_position' with value [-0.0093575 0.1638512 0.15590386 ..., 0.07139178 -0.14649338
0.06221156]
Traceback (most recent call last):
File "realtime_signals.py", line 162, in <module>
c = Canvas()
File "realtime_signals.py", line 123, in __init__
self.program['a_position'] = y.ravel()
File "/home/cyrille/git/vispy/vispy/gloo/program.py", line 353, in __setitem__
self._attributes[name].set_data(data)
File "/home/cyrille/git/vispy/vispy/gloo/variable.py", line 326, in set_data
assert count == data.shape[1]
IndexError: tuple index out of range
|
IndexError
|
def __init__(self):
app.Canvas.__init__(self, title="Use your wheel to zoom!", keys="interactive")
self.program = gloo.Program(VERT_SHADER, FRAG_SHADER)
self.program["a_position"] = y.reshape(-1, 1)
self.program["a_color"] = color
self.program["a_index"] = index
self.program["u_scale"] = (1.0, 1.0)
self.program["u_size"] = (nrows, ncols)
self.program["u_n"] = n
self._timer = app.Timer("auto", connect=self.on_timer, start=True)
|
def __init__(self):
app.Canvas.__init__(self, title="Use your wheel to zoom!", keys="interactive")
self.program = gloo.Program(VERT_SHADER, FRAG_SHADER)
self.program["a_position"] = y.ravel()
self.program["a_color"] = color
self.program["a_index"] = index
self.program["u_scale"] = (1.0, 1.0)
self.program["u_size"] = (nrows, ncols)
self.program["u_n"] = n
self._timer = app.Timer("auto", connect=self.on_timer, start=True)
|
https://github.com/vispy/vispy/issues/528
|
cyrille@Cyrille-ASUS:~/git/vispy/examples/demo/gloo$ python realtime_signals.py ERROR: Could not set variable 'a_position' with value [-0.0093575 0.1638512 0.15590386 ..., 0.07139178 -0.14649338
0.06221156]
Traceback (most recent call last):
File "realtime_signals.py", line 162, in <module>
c = Canvas()
File "realtime_signals.py", line 123, in __init__
self.program['a_position'] = y.ravel()
File "/home/cyrille/git/vispy/vispy/gloo/program.py", line 353, in __setitem__
self._attributes[name].set_data(data)
File "/home/cyrille/git/vispy/vispy/gloo/variable.py", line 326, in set_data
assert count == data.shape[1]
IndexError: tuple index out of range
|
IndexError
|
def __init__(self):
app.Canvas.__init__(self, title="Spacy", keys="interactive")
self.size = 800, 600
self.program = gloo.Program(vertex, fragment)
self.view = np.eye(4, dtype=np.float32)
self.model = np.eye(4, dtype=np.float32)
self.projection = np.eye(4, dtype=np.float32)
self.timer = app.Timer("auto", connect=self.update, start=True)
# Set uniforms (some are set later)
self.program["u_model"] = self.model
self.program["u_view"] = self.view
# Set attributes
self.program["a_position"] = np.zeros((N, 3), np.float32)
self.program["a_offset"] = np.zeros((N, 1), np.float32)
# Init
self._timeout = 0
self._active_block = 0
for i in range(NBLOCKS):
self._generate_stars()
self._timeout = time.time() + SPEED
|
def __init__(self):
app.Canvas.__init__(self, title="Spacy", keys="interactive")
self.size = 800, 600
self.program = gloo.Program(vertex, fragment)
self.view = np.eye(4, dtype=np.float32)
self.model = np.eye(4, dtype=np.float32)
self.projection = np.eye(4, dtype=np.float32)
self.timer = app.Timer("auto", connect=self.update, start=True)
# Set uniforms (some are set later)
self.program["u_model"] = self.model
self.program["u_view"] = self.view
# Set attributes
self.program["a_position"] = np.zeros((N, 3), np.float32)
self.program["a_offset"] = np.zeros((N,), np.float32)
# Init
self._timeout = 0
self._active_block = 0
for i in range(NBLOCKS):
self._generate_stars()
self._timeout = time.time() + SPEED
|
https://github.com/vispy/vispy/issues/528
|
cyrille@Cyrille-ASUS:~/git/vispy/examples/demo/gloo$ python realtime_signals.py ERROR: Could not set variable 'a_position' with value [-0.0093575 0.1638512 0.15590386 ..., 0.07139178 -0.14649338
0.06221156]
Traceback (most recent call last):
File "realtime_signals.py", line 162, in <module>
c = Canvas()
File "realtime_signals.py", line 123, in __init__
self.program['a_position'] = y.ravel()
File "/home/cyrille/git/vispy/vispy/gloo/program.py", line 353, in __setitem__
self._attributes[name].set_data(data)
File "/home/cyrille/git/vispy/vispy/gloo/variable.py", line 326, in set_data
assert count == data.shape[1]
IndexError: tuple index out of range
|
IndexError
|
def _get_tick_frac_labels(self):
"""Get the major ticks, minor ticks, and major labels"""
minor_num = 4 # number of minor ticks per major division
if self.axis.scale_type == "linear":
domain = self.axis.domain
if domain[1] < domain[0]:
flip = True
domain = domain[::-1]
else:
flip = False
offset = domain[0]
scale = domain[1] - domain[0]
tr_sys = self.axis.transforms
length = self.axis.pos[1] - self.axis.pos[0] # in logical coords
n_inches = np.sqrt(np.sum(length**2)) / tr_sys.dpi
# major = np.linspace(domain[0], domain[1], num=11)
# major = MaxNLocator(10).tick_values(*domain)
major = _get_ticks_talbot(domain[0], domain[1], n_inches, 2)
labels = ["%g" % x for x in major]
majstep = major[1] - major[0]
minor = []
minstep = majstep / (minor_num + 1)
for i in major[:-1]:
minor.extend(np.linspace(i + minstep, i + majstep - minstep, (minor_num)))
major_frac = major / scale - offset
minor_frac = np.array(minor) / scale - offset
major_frac = major_frac[::-1] if flip else major_frac
use_mask = (major_frac > -0.0001) & (major_frac < 1.0001)
major_frac = major_frac[use_mask]
labels = [l for li, l in enumerate(labels) if use_mask[li]]
minor_frac = minor_frac[(minor_frac > -0.0001) & (minor_frac < 1.0001)]
elif self.axis.scale_type == "logarithmic":
return NotImplementedError
elif self.axis.scale_type == "power":
return NotImplementedError
return major_frac, minor_frac, labels
|
def _get_tick_frac_labels(self):
# This conditional is currently unnecessary since we only support
# linear, but eventually we will support others so we leave it in
if self.axis.scale_type == "linear":
major_num = 11 # maximum number of major ticks
minor_num = 4 # maximum number of minor ticks per major division
major, majstep = np.linspace(0, 1, num=major_num, retstep=True)
# XXX TODO: this should be better than just str(x)
labels = [str(x) for x in np.interp(major, [0, 1], self.axis.domain)]
# XXX TODO: make these nice numbers only
# - and faster! Potentially could draw in linspace across the whole
# axis and render them before the major ticks, so the overlap
# gets hidden. Might be messy. Benchmark trade-off of extra GL
# versus extra NumPy.
minor = []
for i in np.nditer(major[:-1]):
minor.extend(np.linspace(i, (i + majstep), (minor_num + 2))[1:-1])
# elif (self.scale_type == 'logarithmic'):
# return NotImplementedError
# elif (self.scale_type == 'power'):
# return NotImplementedError
return major, minor, labels
|
https://github.com/vispy/vispy/issues/7
|
Traceback (most recent call last):
File "oscilloscope.py", line 138, in on_paint
visual.draw(transform)
File "oscilloscope.py", line 79, in draw
self.program.uniforms['color'] = self.color
File "../vispy/oogl/program_inputs.py", line 43, in __setitem__
self._set(k, v)
File "../vispy/oogl/program_inputs.py", line 94, in _set
self._apply(name, value)
File "../vispy/oogl/program_inputs.py", line 198, in _apply
_name, length, type = gl.glGetActiveUniform(self._program.handle, loc)
File "/usr/locaL/Cellar/python/2.7.5/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/OpenGL/latebind.py", line 61, in __call__
return self.wrapperFunction( self.baseFunction, *args, **named )
File "/usr/locaL/Cellar/python/2.7.5/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/OpenGL/GL/VERSION/GL_2_0.py", line 213, in glGetActiveUniform
raise IndexError( 'Index %s out of range 0 to %i' % (index, max_index - 1, ) )
IndexError: Index 4 out of range 0 to 1
Error invoking callback for event: <PaintEvent blocked=False handled=False native=None region=None source=<__main__.Canvas object at 0x107548210> sources=[<__main__.Canvas object at 0x107548210>] type=paint>
(0.1, 1.0, 0.1, 1.953125000000001e-11)```
|
IndexError
|
def __init__(
self, nbins=10, steps=None, trim=True, integer=False, symmetric=False, prune=None
):
"""
Keyword args:
*nbins*
Maximum number of intervals; one less than max number of ticks.
*steps*
Sequence of nice numbers starting with 1 and ending with 10;
e.g., [1, 2, 4, 5, 10]
*integer*
If True, ticks will take only integer values.
*symmetric*
If True, autoscaling will result in a range symmetric
about zero.
*prune*
['lower' | 'upper' | 'both' | None]
Remove edge ticks -- useful for stacked or ganged plots
where the upper tick of one axes overlaps with the lower
tick of the axes above it.
If prune=='lower', the smallest tick will
be removed. If prune=='upper', the largest tick will be
removed. If prune=='both', the largest and smallest ticks
will be removed. If prune==None, no ticks will be removed.
"""
self._nbins = int(nbins)
self._trim = trim
self._integer = integer
self._symmetric = symmetric
if prune is not None and prune not in ["upper", "lower", "both"]:
raise ValueError("prune must be 'upper', 'lower', 'both', or None")
self._prune = prune
if steps is None:
steps = [1, 2, 2.5, 3, 4, 5, 6, 8, 10]
else:
if int(steps[-1]) != 10:
steps = list(steps)
steps.append(10)
self._steps = steps
self._integer = integer
if self._integer:
self._steps = [n for n in self._steps if divmod(n, 1)[1] < 0.001]
|
def __init__(self, axis):
self.axis = axis
|
https://github.com/vispy/vispy/issues/7
|
Traceback (most recent call last):
File "oscilloscope.py", line 138, in on_paint
visual.draw(transform)
File "oscilloscope.py", line 79, in draw
self.program.uniforms['color'] = self.color
File "../vispy/oogl/program_inputs.py", line 43, in __setitem__
self._set(k, v)
File "../vispy/oogl/program_inputs.py", line 94, in _set
self._apply(name, value)
File "../vispy/oogl/program_inputs.py", line 198, in _apply
_name, length, type = gl.glGetActiveUniform(self._program.handle, loc)
File "/usr/locaL/Cellar/python/2.7.5/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/OpenGL/latebind.py", line 61, in __call__
return self.wrapperFunction( self.baseFunction, *args, **named )
File "/usr/locaL/Cellar/python/2.7.5/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/OpenGL/GL/VERSION/GL_2_0.py", line 213, in glGetActiveUniform
raise IndexError( 'Index %s out of range 0 to %i' % (index, max_index - 1, ) )
IndexError: Index 4 out of range 0 to 1
Error invoking callback for event: <PaintEvent blocked=False handled=False native=None region=None source=<__main__.Canvas object at 0x107548210> sources=[<__main__.Canvas object at 0x107548210>] type=paint>
(0.1, 1.0, 0.1, 1.953125000000001e-11)```
|
IndexError
|
def handle_import_data(self, data):
"""Import additional data for tuning
Parameters
----------
data:
a list of dictionarys, each of which has at least two keys, 'parameter' and 'value'
Raises
------
AssertionError
data doesn't have required key 'parameter' and 'value'
"""
for entry in data:
entry["value"] = json_tricks.loads(entry["value"])
_completed_num = 0
for trial_info in data:
logger.info(
"Importing data, current processing progress %s / %s",
_completed_num,
len(data),
)
_completed_num += 1
assert "parameter" in trial_info
_params = trial_info["parameter"]
assert "value" in trial_info
_value = trial_info["value"]
if not _value:
logger.info(
"Useless trial data, value is %s, skip this trial data.", _value
)
continue
_value = extract_scalar_reward(_value)
budget_exist_flag = False
barely_params = dict()
for keys in _params:
if keys == _KEY:
_budget = _params[keys]
budget_exist_flag = True
else:
barely_params[keys] = _params[keys]
if not budget_exist_flag:
_budget = self.max_budget
logger.info('Set "TRIAL_BUDGET" value to %s (max budget)', self.max_budget)
if self.optimize_mode is OptimizeMode.Maximize:
reward = -_value
else:
reward = _value
self.cg.new_result(
loss=reward, budget=_budget, parameters=barely_params, update_model=True
)
logger.info("Successfully import tuning data to BOHB advisor.")
|
def handle_import_data(self, data):
"""Import additional data for tuning
Parameters
----------
data:
a list of dictionarys, each of which has at least two keys, 'parameter' and 'value'
Raises
------
AssertionError
data doesn't have required key 'parameter' and 'value'
"""
for entry in data:
entry["value"] = json_tricks.loads(entry["value"])
_completed_num = 0
for trial_info in data:
logger.info(
"Importing data, current processing progress %s / %s",
_completed_num,
len(data),
)
_completed_num += 1
assert "parameter" in trial_info
_params = trial_info["parameter"]
assert "value" in trial_info
_value = trial_info["value"]
if not _value:
logger.info(
"Useless trial data, value is %s, skip this trial data.", _value
)
continue
budget_exist_flag = False
barely_params = dict()
for keys in _params:
if keys == _KEY:
_budget = _params[keys]
budget_exist_flag = True
else:
barely_params[keys] = _params[keys]
if not budget_exist_flag:
_budget = self.max_budget
logger.info('Set "TRIAL_BUDGET" value to %s (max budget)', self.max_budget)
if self.optimize_mode is OptimizeMode.Maximize:
reward = -_value
else:
reward = _value
self.cg.new_result(
loss=reward, budget=_budget, parameters=barely_params, update_model=True
)
logger.info("Successfully import tuning data to BOHB advisor.")
|
https://github.com/microsoft/nni/issues/2140
|
[03/09/2020, 03:51:05 PM] INFO (nni.msg_dispatcher_base/MainThread) Start dispatcher
[03/09/2020, 03:51:05 PM] INFO (nni.tuner/MainThread) Load checkpoint ignored by tuner, checkpoint path: /home/arvoelke/nni/experiments/NdfeH2R1/checkpoint
[03/09/2020, 03:51:05 PM] INFO (nni.assessor/MainThread) Load checkpoint ignored by assessor, checkpoint path: /home/arvoelke/nni/experiments/NdfeH2R1/checkpoint
[03/09/2020, 03:51:06 PM] INFO (smac_AutoML/Thread-1) update search space in SMAC.
[03/09/2020, 03:51:06 PM] INFO (smac_AutoML/Thread-1) SMAC call: /home/arvoelke/anaconda3/envs/*/lib/python3.7/site-packages/nni/__main__.py --exp_params eyJhdXRob3JOYW1lIjoiYXJ2b2Vsa2UiLCJleHBlcmltZW50TmFtZSI6IkxNVSIsInRyaWFsQ29uY3VycmVuY3kiOjEsIm1heEV4ZWNEdXJhdGlvbiI6ODYzMTM2MDAsIm1heFRyaWFsTnVtIjo5OTk5OSwidHJhaW5pbmdTZXJ2aWNlUGxhdGZvcm0iOiJsb2NhbCIsInR1bmVyIjp7ImJ1aWx0aW5UdW5lck5hbWUiOiJTTUFDIiwiY2xhc3NBcmdzIjp7Im9wdGltaXplX21vZGUiOiJtaW5pbWl6ZSJ9LCJjaGVja3BvaW50RGlyIjoiL2hvbWUvYXJ2b2Vsa2Uvbm5pL2V4cGVyaW1lbnRzL05kZmVIMlIxL2NoZWNrcG9pbnQifSwiYXNzZXNzb3IiOnsiY29kZURpciI6Ii9ob21lL2Fydm9lbGtlL2dpdC9ubmktcHl0b3JjaC1rYWxkaS9jb25maWcvLi4iLCJjbGFzc0ZpbGVOYW1lIjoiYXNzZXNzb3IucHkiLCJjbGFzc05hbWUiOiJGaXhlZE1lZGlhbnN0b3BBc3Nlc3NvciIsImNsYXNzQXJncyI6eyJvcHRpbWl6ZV9tb2RlIjoibWluaW1pemUiLCJzdGFydF9zdGVwIjoyfSwiY2hlY2twb2ludERpciI6Ii9ob21lL2Fydm9lbGtlL25uaS9leHBlcmltZW50cy9OZGZlSDJSMS9jaGVja3BvaW50In0sInZlcnNpb25DaGVjayI6dHJ1ZSwiY2x1c3Rlck1ldGFEYXRhIjpbeyJrZXkiOiJjb2RlRGlyIiwidmFsdWUiOiIvaG9tZS9hcnZvZWxrZS9naXQvbm5pLXB5dG9yY2gta2FsZGkvY29uZmlnLy4uIn0seyJrZXkiOiJjb21tYW5kIiwidmFsdWUiOiJweXRob24gcnVuX25uaS5weSBMTVUifV19
[03/09/2020, 03:51:06 PM] PRINT INFO: Reading scenario file: scenario.txt
[03/09/2020, 03:51:06 PM] PRINT INFO: Output to smac3-output_2020-03-09_15:51:06_571519
[03/09/2020, 03:51:06 PM] PRINT INFO: Importing data, current processing progress 0 / 109
[03/09/2020, 03:51:06 PM] PRINT ERROR: unsupported operand type(s) for +: 'float' and 'collections.OrderedDict'
Traceback (most recent call last):
File "/home/arvoelke/anaconda3/envs/*/lib/python3.7/site-packages/nni/msg_dispatcher_base.py", line 90, in command_queue_worker
self.process_command(command, data)
File "/home/arvoelke/anaconda3/envs/*/lib/python3.7/site-packages/nni/msg_dispatcher_base.py", line 149, in process_command
command_handlers[command](data)
File "/home/arvoelke/anaconda3/envs/*/lib/python3.7/site-packages/nni/msg_dispatcher.py", line 118, in handle_import_data
self.tuner.import_data(data)
File "/home/arvoelke/anaconda3/envs/*/lib/python3.7/site-packages/nni/smac_tuner/smac_tuner.py", line 332, in import_data
self.smbo_solver.nni_smac_receive_first_run(config, _value)
File "/home/arvoelke/anaconda3/envs/*/lib/python3.7/site-packages/smac/optimizer/smbo.py", line 175, in nni_smac_receive_first_run
cost=reward, time=-1, status=StatusType.SUCCESS)
File "/home/arvoelke/anaconda3/envs/*/lib/python3.7/site-packages/smac/runhistory/runhistory.py", line 168, in add
self._add(k, v, status, origin)
File "/home/arvoelke/anaconda3/envs/*/lib/python3.7/site-packages/smac/runhistory/runhistory.py", line 197, in _add
self.incremental_update_cost(self.ids_config[k.config_id], v.cost)
File "/home/arvoelke/anaconda3/envs/*/lib/python3.7/site-packages/smac/runhistory/runhistory.py", line 256, in incremental_update_cost
(old_cost * n_runs) + cost) / (n_runs + 1)
TypeError: unsupported operand type(s) for +: 'float' and 'collections.OrderedDict'
[03/09/2020, 03:51:11 PM] PRINT INFO: Dispatcher exiting...
[03/09/2020, 03:51:14 PM] PRINT INFO: Terminated by NNI manager
|
TypeError
|
def import_data(self, data):
"""
Import additional data for tuning.
Parameters
----------
data : list of dict
Each of which has at least two keys, ``parameter`` and ``value``.
"""
_completed_num = 0
for trial_info in data:
self.logger.info(
"Importing data, current processing progress %s / %s",
_completed_num,
len(data),
)
# simply validate data format
assert "parameter" in trial_info
_params = trial_info["parameter"]
assert "value" in trial_info
_value = trial_info["value"]
if not _value:
self.logger.info(
"Useless trial data, value is %s, skip this trial data.", _value
)
continue
_value = extract_scalar_reward(_value)
# convert the keys in loguniform and categorical types
valid_entry = True
for key, value in _params.items():
if key in self.loguniform_key:
_params[key] = np.log(value)
elif key in self.categorical_dict:
if value in self.categorical_dict[key]:
_params[key] = self.categorical_dict[key].index(value)
else:
self.logger.info(
"The value %s of key %s is not in search space.",
str(value),
key,
)
valid_entry = False
break
if not valid_entry:
continue
# start import this data entry
_completed_num += 1
config = Configuration(self.cs, values=_params)
if self.optimize_mode is OptimizeMode.Maximize:
_value = -_value
if self.first_one:
self.smbo_solver.nni_smac_receive_first_run(config, _value)
self.first_one = False
else:
self.smbo_solver.nni_smac_receive_runs(config, _value)
self.logger.info(
"Successfully import data to smac tuner, total data: %d, imported data: %d.",
len(data),
_completed_num,
)
|
def import_data(self, data):
"""
Import additional data for tuning.
Parameters
----------
data : list of dict
Each of which has at least two keys, ``parameter`` and ``value``.
"""
_completed_num = 0
for trial_info in data:
self.logger.info(
"Importing data, current processing progress %s / %s",
_completed_num,
len(data),
)
# simply validate data format
assert "parameter" in trial_info
_params = trial_info["parameter"]
assert "value" in trial_info
_value = trial_info["value"]
if not _value:
self.logger.info(
"Useless trial data, value is %s, skip this trial data.", _value
)
continue
# convert the keys in loguniform and categorical types
valid_entry = True
for key, value in _params.items():
if key in self.loguniform_key:
_params[key] = np.log(value)
elif key in self.categorical_dict:
if value in self.categorical_dict[key]:
_params[key] = self.categorical_dict[key].index(value)
else:
self.logger.info(
"The value %s of key %s is not in search space.",
str(value),
key,
)
valid_entry = False
break
if not valid_entry:
continue
# start import this data entry
_completed_num += 1
config = Configuration(self.cs, values=_params)
if self.optimize_mode is OptimizeMode.Maximize:
_value = -_value
if self.first_one:
self.smbo_solver.nni_smac_receive_first_run(config, _value)
self.first_one = False
else:
self.smbo_solver.nni_smac_receive_runs(config, _value)
self.logger.info(
"Successfully import data to smac tuner, total data: %d, imported data: %d.",
len(data),
_completed_num,
)
|
https://github.com/microsoft/nni/issues/2140
|
[03/09/2020, 03:51:05 PM] INFO (nni.msg_dispatcher_base/MainThread) Start dispatcher
[03/09/2020, 03:51:05 PM] INFO (nni.tuner/MainThread) Load checkpoint ignored by tuner, checkpoint path: /home/arvoelke/nni/experiments/NdfeH2R1/checkpoint
[03/09/2020, 03:51:05 PM] INFO (nni.assessor/MainThread) Load checkpoint ignored by assessor, checkpoint path: /home/arvoelke/nni/experiments/NdfeH2R1/checkpoint
[03/09/2020, 03:51:06 PM] INFO (smac_AutoML/Thread-1) update search space in SMAC.
[03/09/2020, 03:51:06 PM] INFO (smac_AutoML/Thread-1) SMAC call: /home/arvoelke/anaconda3/envs/*/lib/python3.7/site-packages/nni/__main__.py --exp_params eyJhdXRob3JOYW1lIjoiYXJ2b2Vsa2UiLCJleHBlcmltZW50TmFtZSI6IkxNVSIsInRyaWFsQ29uY3VycmVuY3kiOjEsIm1heEV4ZWNEdXJhdGlvbiI6ODYzMTM2MDAsIm1heFRyaWFsTnVtIjo5OTk5OSwidHJhaW5pbmdTZXJ2aWNlUGxhdGZvcm0iOiJsb2NhbCIsInR1bmVyIjp7ImJ1aWx0aW5UdW5lck5hbWUiOiJTTUFDIiwiY2xhc3NBcmdzIjp7Im9wdGltaXplX21vZGUiOiJtaW5pbWl6ZSJ9LCJjaGVja3BvaW50RGlyIjoiL2hvbWUvYXJ2b2Vsa2Uvbm5pL2V4cGVyaW1lbnRzL05kZmVIMlIxL2NoZWNrcG9pbnQifSwiYXNzZXNzb3IiOnsiY29kZURpciI6Ii9ob21lL2Fydm9lbGtlL2dpdC9ubmktcHl0b3JjaC1rYWxkaS9jb25maWcvLi4iLCJjbGFzc0ZpbGVOYW1lIjoiYXNzZXNzb3IucHkiLCJjbGFzc05hbWUiOiJGaXhlZE1lZGlhbnN0b3BBc3Nlc3NvciIsImNsYXNzQXJncyI6eyJvcHRpbWl6ZV9tb2RlIjoibWluaW1pemUiLCJzdGFydF9zdGVwIjoyfSwiY2hlY2twb2ludERpciI6Ii9ob21lL2Fydm9lbGtlL25uaS9leHBlcmltZW50cy9OZGZlSDJSMS9jaGVja3BvaW50In0sInZlcnNpb25DaGVjayI6dHJ1ZSwiY2x1c3Rlck1ldGFEYXRhIjpbeyJrZXkiOiJjb2RlRGlyIiwidmFsdWUiOiIvaG9tZS9hcnZvZWxrZS9naXQvbm5pLXB5dG9yY2gta2FsZGkvY29uZmlnLy4uIn0seyJrZXkiOiJjb21tYW5kIiwidmFsdWUiOiJweXRob24gcnVuX25uaS5weSBMTVUifV19
[03/09/2020, 03:51:06 PM] PRINT INFO: Reading scenario file: scenario.txt
[03/09/2020, 03:51:06 PM] PRINT INFO: Output to smac3-output_2020-03-09_15:51:06_571519
[03/09/2020, 03:51:06 PM] PRINT INFO: Importing data, current processing progress 0 / 109
[03/09/2020, 03:51:06 PM] PRINT ERROR: unsupported operand type(s) for +: 'float' and 'collections.OrderedDict'
Traceback (most recent call last):
File "/home/arvoelke/anaconda3/envs/*/lib/python3.7/site-packages/nni/msg_dispatcher_base.py", line 90, in command_queue_worker
self.process_command(command, data)
File "/home/arvoelke/anaconda3/envs/*/lib/python3.7/site-packages/nni/msg_dispatcher_base.py", line 149, in process_command
command_handlers[command](data)
File "/home/arvoelke/anaconda3/envs/*/lib/python3.7/site-packages/nni/msg_dispatcher.py", line 118, in handle_import_data
self.tuner.import_data(data)
File "/home/arvoelke/anaconda3/envs/*/lib/python3.7/site-packages/nni/smac_tuner/smac_tuner.py", line 332, in import_data
self.smbo_solver.nni_smac_receive_first_run(config, _value)
File "/home/arvoelke/anaconda3/envs/*/lib/python3.7/site-packages/smac/optimizer/smbo.py", line 175, in nni_smac_receive_first_run
cost=reward, time=-1, status=StatusType.SUCCESS)
File "/home/arvoelke/anaconda3/envs/*/lib/python3.7/site-packages/smac/runhistory/runhistory.py", line 168, in add
self._add(k, v, status, origin)
File "/home/arvoelke/anaconda3/envs/*/lib/python3.7/site-packages/smac/runhistory/runhistory.py", line 197, in _add
self.incremental_update_cost(self.ids_config[k.config_id], v.cost)
File "/home/arvoelke/anaconda3/envs/*/lib/python3.7/site-packages/smac/runhistory/runhistory.py", line 256, in incremental_update_cost
(old_cost * n_runs) + cost) / (n_runs + 1)
TypeError: unsupported operand type(s) for +: 'float' and 'collections.OrderedDict'
[03/09/2020, 03:51:11 PM] PRINT INFO: Dispatcher exiting...
[03/09/2020, 03:51:14 PM] PRINT INFO: Terminated by NNI manager
|
TypeError
|
def _pack_parameter(
parameter_id, params, customized=False, trial_job_id=None, parameter_index=None
):
_trial_params[parameter_id] = params
ret = {
"parameter_id": parameter_id,
"parameter_source": "customized" if customized else "algorithm",
"parameters": params,
}
if trial_job_id is not None:
ret["trial_job_id"] = trial_job_id
if parameter_index is not None:
ret["parameter_index"] = parameter_index
else:
ret["parameter_index"] = 0
return to_json(ret)
|
def _pack_parameter(
parameter_id, params, customized=False, trial_job_id=None, parameter_index=None
):
_trial_params[parameter_id] = params
ret = {
"parameter_id": parameter_id,
"parameter_source": "customized" if customized else "algorithm",
"parameters": params,
}
if trial_job_id is not None:
ret["trial_job_id"] = trial_job_id
if parameter_index is not None:
ret["parameter_index"] = parameter_index
else:
ret["parameter_index"] = 0
return json_tricks.dumps(ret)
|
https://github.com/microsoft/nni/issues/1589
|
Using TensorFlow backend.
Traceback (most recent call last):
File "test.py", line 164, in <module>
train(ARGS, RECEIVED_PARAMS)
File "test.py", line 136, in train
validation_data=(x_test, y_test), callbacks=[SendMetrics(), TensorBoard(log_dir=TENSORBOARD_DIR)])
File "/home/msalz/venv_nni_dev/lib64/python3.6/site-packages/keras/engine/training.py", line 1178, in fit
validation_freq=validation_freq)
File "/home/msalz/venv_nni_dev/lib64/python3.6/site-packages/keras/engine/training_arrays.py", line 224, in fit_loop
callbacks.on_epoch_end(epoch, epoch_logs)
File "/home/msalz/venv_nni_dev/lib64/python3.6/site-packages/keras/callbacks.py", line 152, in on_epoch_end
callback.on_epoch_end(epoch, logs)
File "test.py", line 119, in on_epoch_end
nni.report_intermediate_result(logs["val_loss"])
File "/home/msalz/nni/build/nni/trial.py", line 81, in report_intermediate_result
'value': metric
File "/home/msalz/venv_nni_dev/lib64/python3.6/site-packages/json_tricks/nonp.py", line 99, in dumps
primitives=primitives, fallback_encoders=fallback_encoders, **jsonkwargs).encode(obj)
File "/usr/lib64/python3.6/json/encoder.py", line 199, in encode
chunks = self.iterencode(o, _one_shot=True)
File "/usr/lib64/python3.6/json/encoder.py", line 257, in iterencode
return _iterencode(o, 0)
ValueError: Out of range float values are not JSON compliant
|
ValueError
|
def request_next_parameter():
metric = to_json(
{
"trial_job_id": trial_env_vars.NNI_TRIAL_JOB_ID,
"type": "REQUEST_PARAMETER",
"sequence": 0,
"parameter_index": _param_index,
}
)
send_metric(metric)
|
def request_next_parameter():
metric = json_tricks.dumps(
{
"trial_job_id": trial_env_vars.NNI_TRIAL_JOB_ID,
"type": "REQUEST_PARAMETER",
"sequence": 0,
"parameter_index": _param_index,
}
)
send_metric(metric)
|
https://github.com/microsoft/nni/issues/1589
|
Using TensorFlow backend.
Traceback (most recent call last):
File "test.py", line 164, in <module>
train(ARGS, RECEIVED_PARAMS)
File "test.py", line 136, in train
validation_data=(x_test, y_test), callbacks=[SendMetrics(), TensorBoard(log_dir=TENSORBOARD_DIR)])
File "/home/msalz/venv_nni_dev/lib64/python3.6/site-packages/keras/engine/training.py", line 1178, in fit
validation_freq=validation_freq)
File "/home/msalz/venv_nni_dev/lib64/python3.6/site-packages/keras/engine/training_arrays.py", line 224, in fit_loop
callbacks.on_epoch_end(epoch, epoch_logs)
File "/home/msalz/venv_nni_dev/lib64/python3.6/site-packages/keras/callbacks.py", line 152, in on_epoch_end
callback.on_epoch_end(epoch, logs)
File "test.py", line 119, in on_epoch_end
nni.report_intermediate_result(logs["val_loss"])
File "/home/msalz/nni/build/nni/trial.py", line 81, in report_intermediate_result
'value': metric
File "/home/msalz/venv_nni_dev/lib64/python3.6/site-packages/json_tricks/nonp.py", line 99, in dumps
primitives=primitives, fallback_encoders=fallback_encoders, **jsonkwargs).encode(obj)
File "/usr/lib64/python3.6/json/encoder.py", line 199, in encode
chunks = self.iterencode(o, _one_shot=True)
File "/usr/lib64/python3.6/json/encoder.py", line 257, in iterencode
return _iterencode(o, 0)
ValueError: Out of range float values are not JSON compliant
|
ValueError
|
def report_intermediate_result(metric):
"""
Reports intermediate result to NNI.
Parameters
----------
metric:
serializable object.
"""
global _intermediate_seq
assert _params or trial_env_vars.NNI_PLATFORM is None, (
"nni.get_next_parameter() needs to be called before report_intermediate_result"
)
metric = to_json(
{
"parameter_id": _params["parameter_id"] if _params else None,
"trial_job_id": trial_env_vars.NNI_TRIAL_JOB_ID,
"type": "PERIODICAL",
"sequence": _intermediate_seq,
"value": metric,
}
)
_intermediate_seq += 1
platform.send_metric(metric)
|
def report_intermediate_result(metric):
"""
Reports intermediate result to NNI.
Parameters
----------
metric:
serializable object.
"""
global _intermediate_seq
assert _params or trial_env_vars.NNI_PLATFORM is None, (
"nni.get_next_parameter() needs to be called before report_intermediate_result"
)
metric = json_tricks.dumps(
{
"parameter_id": _params["parameter_id"] if _params else None,
"trial_job_id": trial_env_vars.NNI_TRIAL_JOB_ID,
"type": "PERIODICAL",
"sequence": _intermediate_seq,
"value": metric,
}
)
_intermediate_seq += 1
platform.send_metric(metric)
|
https://github.com/microsoft/nni/issues/1589
|
Using TensorFlow backend.
Traceback (most recent call last):
File "test.py", line 164, in <module>
train(ARGS, RECEIVED_PARAMS)
File "test.py", line 136, in train
validation_data=(x_test, y_test), callbacks=[SendMetrics(), TensorBoard(log_dir=TENSORBOARD_DIR)])
File "/home/msalz/venv_nni_dev/lib64/python3.6/site-packages/keras/engine/training.py", line 1178, in fit
validation_freq=validation_freq)
File "/home/msalz/venv_nni_dev/lib64/python3.6/site-packages/keras/engine/training_arrays.py", line 224, in fit_loop
callbacks.on_epoch_end(epoch, epoch_logs)
File "/home/msalz/venv_nni_dev/lib64/python3.6/site-packages/keras/callbacks.py", line 152, in on_epoch_end
callback.on_epoch_end(epoch, logs)
File "test.py", line 119, in on_epoch_end
nni.report_intermediate_result(logs["val_loss"])
File "/home/msalz/nni/build/nni/trial.py", line 81, in report_intermediate_result
'value': metric
File "/home/msalz/venv_nni_dev/lib64/python3.6/site-packages/json_tricks/nonp.py", line 99, in dumps
primitives=primitives, fallback_encoders=fallback_encoders, **jsonkwargs).encode(obj)
File "/usr/lib64/python3.6/json/encoder.py", line 199, in encode
chunks = self.iterencode(o, _one_shot=True)
File "/usr/lib64/python3.6/json/encoder.py", line 257, in iterencode
return _iterencode(o, 0)
ValueError: Out of range float values are not JSON compliant
|
ValueError
|
def report_final_result(metric):
"""
Reports final result to NNI.
Parameters
----------
metric:
serializable object.
"""
assert _params or trial_env_vars.NNI_PLATFORM is None, (
"nni.get_next_parameter() needs to be called before report_final_result"
)
metric = to_json(
{
"parameter_id": _params["parameter_id"] if _params else None,
"trial_job_id": trial_env_vars.NNI_TRIAL_JOB_ID,
"type": "FINAL",
"sequence": 0,
"value": metric,
}
)
platform.send_metric(metric)
|
def report_final_result(metric):
"""
Reports final result to NNI.
Parameters
----------
metric:
serializable object.
"""
assert _params or trial_env_vars.NNI_PLATFORM is None, (
"nni.get_next_parameter() needs to be called before report_final_result"
)
metric = json_tricks.dumps(
{
"parameter_id": _params["parameter_id"] if _params else None,
"trial_job_id": trial_env_vars.NNI_TRIAL_JOB_ID,
"type": "FINAL",
"sequence": 0,
"value": metric,
}
)
platform.send_metric(metric)
|
https://github.com/microsoft/nni/issues/1589
|
Using TensorFlow backend.
Traceback (most recent call last):
File "test.py", line 164, in <module>
train(ARGS, RECEIVED_PARAMS)
File "test.py", line 136, in train
validation_data=(x_test, y_test), callbacks=[SendMetrics(), TensorBoard(log_dir=TENSORBOARD_DIR)])
File "/home/msalz/venv_nni_dev/lib64/python3.6/site-packages/keras/engine/training.py", line 1178, in fit
validation_freq=validation_freq)
File "/home/msalz/venv_nni_dev/lib64/python3.6/site-packages/keras/engine/training_arrays.py", line 224, in fit_loop
callbacks.on_epoch_end(epoch, epoch_logs)
File "/home/msalz/venv_nni_dev/lib64/python3.6/site-packages/keras/callbacks.py", line 152, in on_epoch_end
callback.on_epoch_end(epoch, logs)
File "test.py", line 119, in on_epoch_end
nni.report_intermediate_result(logs["val_loss"])
File "/home/msalz/nni/build/nni/trial.py", line 81, in report_intermediate_result
'value': metric
File "/home/msalz/venv_nni_dev/lib64/python3.6/site-packages/json_tricks/nonp.py", line 99, in dumps
primitives=primitives, fallback_encoders=fallback_encoders, **jsonkwargs).encode(obj)
File "/usr/lib64/python3.6/json/encoder.py", line 199, in encode
chunks = self.iterencode(o, _one_shot=True)
File "/usr/lib64/python3.6/json/encoder.py", line 257, in iterencode
return _iterencode(o, 0)
ValueError: Out of range float values are not JSON compliant
|
ValueError
|
def parse_annotation_mutable_layers(code, lineno, nas_mode):
"""Parse the string of mutable layers in annotation.
Return a list of AST Expr nodes
code: annotation string (excluding '@')
nas_mode: the mode of NAS
"""
module = ast.parse(code)
assert type(module) is ast.Module, "internal error #1"
assert len(module.body) == 1, (
"Annotation mutable_layers contains more than one expression"
)
assert type(module.body[0]) is ast.Expr, "Annotation is not expression"
call = module.body[0].value
nodes = []
mutable_id = "mutable_block_" + str(lineno)
mutable_layer_cnt = 0
for arg in call.args:
fields = {
"layer_choice": False,
"fixed_inputs": False,
"optional_inputs": False,
"optional_input_size": False,
"layer_output": False,
}
for k, value in zip(arg.keys, arg.values):
if k.id == "layer_choice":
assert not fields["layer_choice"], "Duplicated field: layer_choice"
assert type(value) is ast.List, "Value of layer_choice should be a list"
call_funcs_keys = []
call_funcs_values = []
call_kwargs_values = []
for call in value.elts:
assert type(call) is ast.Call, (
"Element in layer_choice should be function call"
)
call_name = astor.to_source(call).strip()
call_funcs_keys.append(ast.Str(s=call_name))
call_funcs_values.append(call.func)
assert not call.args, (
"Number of args without keyword should be zero"
)
kw_args = []
kw_values = []
for kw in call.keywords:
kw_args.append(ast.Str(s=kw.arg))
kw_values.append(kw.value)
call_kwargs_values.append(ast.Dict(keys=kw_args, values=kw_values))
call_funcs = ast.Dict(keys=call_funcs_keys, values=call_funcs_values)
call_kwargs = ast.Dict(keys=call_funcs_keys, values=call_kwargs_values)
fields["layer_choice"] = True
elif k.id == "fixed_inputs":
assert not fields["fixed_inputs"], "Duplicated field: fixed_inputs"
assert type(value) is ast.List, "Value of fixed_inputs should be a list"
fixed_inputs = value
fields["fixed_inputs"] = True
elif k.id == "optional_inputs":
assert not fields["optional_inputs"], (
"Duplicated field: optional_inputs"
)
assert type(value) is ast.List, (
"Value of optional_inputs should be a list"
)
var_names = [
ast.Str(s=astor.to_source(var).strip()) for var in value.elts
]
optional_inputs = ast.Dict(keys=var_names, values=value.elts)
fields["optional_inputs"] = True
elif k.id == "optional_input_size":
assert not fields["optional_input_size"], (
"Duplicated field: optional_input_size"
)
assert type(value) is ast.Num or type(value) is ast.List, (
"Value of optional_input_size should be a number or list"
)
optional_input_size = value
fields["optional_input_size"] = True
elif k.id == "layer_output":
assert not fields["layer_output"], "Duplicated field: layer_output"
assert type(value) is ast.Name, (
"Value of layer_output should be ast.Name type"
)
layer_output = value
fields["layer_output"] = True
else:
raise AssertionError("Unexpected field in mutable layer")
# make call for this mutable layer
assert fields["layer_choice"], "layer_choice must exist"
assert fields["layer_output"], "layer_output must exist"
mutable_layer_id = "mutable_layer_" + str(mutable_layer_cnt)
mutable_layer_cnt += 1
target_call_attr = ast.Attribute(
value=ast.Name(id="nni", ctx=ast.Load()),
attr="mutable_layer",
ctx=ast.Load(),
)
target_call_args = [
ast.Str(s=mutable_id),
ast.Str(s=mutable_layer_id),
call_funcs,
call_kwargs,
]
if fields["fixed_inputs"]:
target_call_args.append(fixed_inputs)
else:
target_call_args.append(ast.List(elts=[]))
if fields["optional_inputs"]:
target_call_args.append(optional_inputs)
assert fields["optional_input_size"], (
"optional_input_size must exist when optional_inputs exists"
)
target_call_args.append(optional_input_size)
else:
target_call_args.append(ast.Dict(keys=[], values=[]))
target_call_args.append(ast.Num(n=0))
target_call_args.append(ast.Str(s=nas_mode))
if nas_mode in ["enas_mode", "oneshot_mode", "darts_mode"]:
target_call_args.append(ast.Name(id="tensorflow"))
target_call = ast.Call(
func=target_call_attr, args=target_call_args, keywords=[]
)
node = ast.Assign(targets=[layer_output], value=target_call)
nodes.append(node)
return nodes
|
def parse_annotation_mutable_layers(code, lineno, nas_mode):
"""Parse the string of mutable layers in annotation.
Return a list of AST Expr nodes
code: annotation string (excluding '@')
nas_mode: the mode of NAS
"""
module = ast.parse(code)
assert type(module) is ast.Module, "internal error #1"
assert len(module.body) == 1, (
"Annotation mutable_layers contains more than one expression"
)
assert type(module.body[0]) is ast.Expr, "Annotation is not expression"
call = module.body[0].value
nodes = []
mutable_id = "mutable_block_" + str(lineno)
mutable_layer_cnt = 0
for arg in call.args:
fields = {
"layer_choice": False,
"fixed_inputs": False,
"optional_inputs": False,
"optional_input_size": False,
"layer_output": False,
}
for k, value in zip(arg.keys, arg.values):
if k.id == "layer_choice":
assert not fields["layer_choice"], "Duplicated field: layer_choice"
assert type(value) is ast.List, "Value of layer_choice should be a list"
call_funcs_keys = []
call_funcs_values = []
call_kwargs_values = []
for call in value.elts:
assert type(call) is ast.Call, (
"Element in layer_choice should be function call"
)
call_name = astor.to_source(call).strip()
call_funcs_keys.append(ast.Str(s=call_name))
call_funcs_values.append(call.func)
assert not call.args, (
"Number of args without keyword should be zero"
)
kw_args = []
kw_values = []
for kw in call.keywords:
kw_args.append(ast.Str(s=kw.arg))
kw_values.append(kw.value)
call_kwargs_values.append(ast.Dict(keys=kw_args, values=kw_values))
call_funcs = ast.Dict(keys=call_funcs_keys, values=call_funcs_values)
call_kwargs = ast.Dict(keys=call_funcs_keys, values=call_kwargs_values)
fields["layer_choice"] = True
elif k.id == "fixed_inputs":
assert not fields["fixed_inputs"], "Duplicated field: fixed_inputs"
assert type(value) is ast.List, "Value of fixed_inputs should be a list"
fixed_inputs = value
fields["fixed_inputs"] = True
elif k.id == "optional_inputs":
assert not fields["optional_inputs"], (
"Duplicated field: optional_inputs"
)
assert type(value) is ast.List, (
"Value of optional_inputs should be a list"
)
var_names = [
ast.Str(s=astor.to_source(var).strip()) for var in value.elts
]
optional_inputs = ast.Dict(keys=var_names, values=value.elts)
fields["optional_inputs"] = True
elif k.id == "optional_input_size":
assert not fields["optional_input_size"], (
"Duplicated field: optional_input_size"
)
assert type(value) is ast.Num or type(value) is ast.List, (
"Value of optional_input_size should be a number or list"
)
optional_input_size = value
fields["optional_input_size"] = True
elif k.id == "layer_output":
assert not fields["layer_output"], "Duplicated field: layer_output"
assert type(value) is ast.Name, (
"Value of layer_output should be ast.Name type"
)
layer_output = value
fields["layer_output"] = True
else:
raise AssertionError("Unexpected field in mutable layer")
# make call for this mutable layer
assert fields["layer_choice"], "layer_choice must exist"
assert fields["layer_output"], "layer_output must exist"
mutable_layer_id = "mutable_layer_" + str(mutable_layer_cnt)
mutable_layer_cnt += 1
target_call_attr = ast.Attribute(
value=ast.Name(id="nni", ctx=ast.Load()),
attr="mutable_layer",
ctx=ast.Load(),
)
target_call_args = [
ast.Str(s=mutable_id),
ast.Str(s=mutable_layer_id),
call_funcs,
call_kwargs,
]
if fields["fixed_inputs"]:
target_call_args.append(fixed_inputs)
else:
target_call_args.append(ast.List(elts=[]))
if fields["optional_inputs"]:
target_call_args.append(optional_inputs)
assert fields["optional_input_size"], (
"optional_input_size must exist when optional_inputs exists"
)
target_call_args.append(optional_input_size)
else:
target_call_args.append(ast.Dict(keys=[], values=[]))
target_call_args.append(ast.Num(n=0))
target_call_args.append(ast.Str(s=nas_mode))
if nas_mode in ["enas_mode", "oneshot_mode", "darts_mode"]:
target_call_args.append(ast.Name(id="tensorflow"))
target_call = ast.Call(
func=target_call_attr, args=target_call_args, keywords=[]
)
node = ast.Assign(targets=[layer_output], value=target_call)
nodes.append(node)
return nodes
|
https://github.com/microsoft/nni/issues/1560
|
Traceback (most recent call last):
File "/data/hdd3/yugzh/ENAS_NLP/venv/lib/python3.5/site-packages/nni_annotation/specific_code_generator.py", line 355, in parse
transformer.visit(ast_tree)
File "/data/hdd3/yugzh/ENAS_NLP/venv/lib/python3.5/site-packages/nni_annotation/specific_code_generator.py", line 278, in visit
return self._visit_children(node)
File "/data/hdd3/yugzh/ENAS_NLP/venv/lib/python3.5/site-packages/nni_annotation/specific_code_generator.py", line 333, in _visit_children
self.generic_visit(node)
File "/usr/lib/python3.5/ast.py", line 300, in generic_visit
value = self.visit(value)
File "/data/hdd3/yugzh/ENAS_NLP/venv/lib/python3.5/site-packages/nni_annotation/specific_code_generator.py", line 295, in visit
return self._visit_children(node)
File "/data/hdd3/yugzh/ENAS_NLP/venv/lib/python3.5/site-packages/nni_annotation/specific_code_generator.py", line 333, in _visit_children
self.generic_visit(node)
File "/usr/lib/python3.5/ast.py", line 300, in generic_visit
value = self.visit(value)
File "/data/hdd3/yugzh/ENAS_NLP/venv/lib/python3.5/site-packages/nni_annotation/specific_code_generator.py", line 295, in visit
return self._visit_children(node)
File "/data/hdd3/yugzh/ENAS_NLP/venv/lib/python3.5/site-packages/nni_annotation/specific_code_generator.py", line 333, in _visit_children
self.generic_visit(node)
File "/usr/lib/python3.5/ast.py", line 300, in generic_visit
value = self.visit(value)
File "/data/hdd3/yugzh/ENAS_NLP/venv/lib/python3.5/site-packages/nni_annotation/specific_code_generator.py", line 295, in visit
return self._visit_children(node)
File "/data/hdd3/yugzh/ENAS_NLP/venv/lib/python3.5/site-packages/nni_annotation/specific_code_generator.py", line 333, in _visit_children
self.generic_visit(node)
File "/usr/lib/python3.5/ast.py", line 300, in generic_visit
value = self.visit(value)
File "/data/hdd3/yugzh/ENAS_NLP/venv/lib/python3.5/site-packages/nni_annotation/specific_code_generator.py", line 286, in visit
return self._visit_string(node)
File "/data/hdd3/yugzh/ENAS_NLP/venv/lib/python3.5/site-packages/nni_annotation/specific_code_generator.py", line 328, in _visit_string
raise AssertionError('Unexpected annotation function')
AssertionError: Unexpected annotation function
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/data/hdd3/yugzh/ENAS_NLP/venv/lib/python3.5/site-packages/nni_annotation/__init__.py", line 145, in _generate_specific_file
annotated_code = specific_code_generator.parse(src.read(), para_cfg["parameters"], module)
File "/data/hdd3/yugzh/ENAS_NLP/venv/lib/python3.5/site-packages/nni_annotation/specific_code_generator.py", line 357, in parse
raise RuntimeError('%d: %s' % (ast_tree.last_line, exc.args[0]))
AttributeError: 'Module' object has no attribute 'last_line'
|
AssertionError
|
def _visit_string(self, node):
string = node.value.s
if string.startswith("@nni."):
self.annotated = True
else:
return node # not an annotation, ignore it
if string.startswith("@nni.get_next_parameter"):
deprecated_message = (
"'@nni.get_next_parameter' is deprecated in annotation due to inconvenience. "
"Please remove this line in the trial code."
)
print_warning(deprecated_message)
return ast.Expr(
value=ast.Call(
func=ast.Name(id="print", ctx=ast.Load()),
args=[ast.Str(s="Get next parameter here...")],
keywords=[],
)
)
if string.startswith("@nni.training_update"):
return ast.Expr(
value=ast.Call(
func=ast.Name(id="print", ctx=ast.Load()),
args=[ast.Str(s="Training update here...")],
keywords=[],
)
)
if string.startswith("@nni.report_intermediate_result"):
module = ast.parse(string[1:])
arg = module.body[0].value.args[0]
return ast.Expr(
value=ast.Call(
func=ast.Name(id="print", ctx=ast.Load()),
args=[ast.Str(s="nni.report_intermediate_result: "), arg],
keywords=[],
)
)
if string.startswith("@nni.report_final_result"):
module = ast.parse(string[1:])
arg = module.body[0].value.args[0]
return ast.Expr(
value=ast.Call(
func=ast.Name(id="print", ctx=ast.Load()),
args=[ast.Str(s="nni.report_final_result: "), arg],
keywords=[],
)
)
if string.startswith("@nni.mutable_layers"):
return parse_annotation_mutable_layers(string[1:], node.lineno)
if string.startswith("@nni.variable") or string.startswith("@nni.function_choice"):
self.stack[-1] = string[1:] # mark that the next expression is annotated
return None
raise AssertionError("Unexpected annotation function")
|
def _visit_string(self, node):
string = node.value.s
if string.startswith("@nni."):
self.annotated = True
else:
return node # not an annotation, ignore it
if string.startswith("@nni.get_next_parameter"):
deprecated_message = "'@nni.get_next_parameter' is deprecated in annotation due to inconvenience. Please remove this line in the trial code."
print_warning(deprecated_message)
return ast.Expr(
value=ast.Call(
func=ast.Name(id="print", ctx=ast.Load()),
args=[ast.Str(s="Get next parameter here...")],
keywords=[],
)
)
if string.startswith("@nni.report_intermediate_result"):
module = ast.parse(string[1:])
arg = module.body[0].value.args[0]
return ast.Expr(
value=ast.Call(
func=ast.Name(id="print", ctx=ast.Load()),
args=[ast.Str(s="nni.report_intermediate_result: "), arg],
keywords=[],
)
)
if string.startswith("@nni.report_final_result"):
module = ast.parse(string[1:])
arg = module.body[0].value.args[0]
return ast.Expr(
value=ast.Call(
func=ast.Name(id="print", ctx=ast.Load()),
args=[ast.Str(s="nni.report_final_result: "), arg],
keywords=[],
)
)
if string.startswith("@nni.mutable_layers"):
return parse_annotation_mutable_layers(string[1:], node.lineno)
if string.startswith("@nni.variable") or string.startswith("@nni.function_choice"):
self.stack[-1] = string[1:] # mark that the next expression is annotated
return None
raise AssertionError("Unexpected annotation function")
|
https://github.com/microsoft/nni/issues/1560
|
Traceback (most recent call last):
File "/data/hdd3/yugzh/ENAS_NLP/venv/lib/python3.5/site-packages/nni_annotation/specific_code_generator.py", line 355, in parse
transformer.visit(ast_tree)
File "/data/hdd3/yugzh/ENAS_NLP/venv/lib/python3.5/site-packages/nni_annotation/specific_code_generator.py", line 278, in visit
return self._visit_children(node)
File "/data/hdd3/yugzh/ENAS_NLP/venv/lib/python3.5/site-packages/nni_annotation/specific_code_generator.py", line 333, in _visit_children
self.generic_visit(node)
File "/usr/lib/python3.5/ast.py", line 300, in generic_visit
value = self.visit(value)
File "/data/hdd3/yugzh/ENAS_NLP/venv/lib/python3.5/site-packages/nni_annotation/specific_code_generator.py", line 295, in visit
return self._visit_children(node)
File "/data/hdd3/yugzh/ENAS_NLP/venv/lib/python3.5/site-packages/nni_annotation/specific_code_generator.py", line 333, in _visit_children
self.generic_visit(node)
File "/usr/lib/python3.5/ast.py", line 300, in generic_visit
value = self.visit(value)
File "/data/hdd3/yugzh/ENAS_NLP/venv/lib/python3.5/site-packages/nni_annotation/specific_code_generator.py", line 295, in visit
return self._visit_children(node)
File "/data/hdd3/yugzh/ENAS_NLP/venv/lib/python3.5/site-packages/nni_annotation/specific_code_generator.py", line 333, in _visit_children
self.generic_visit(node)
File "/usr/lib/python3.5/ast.py", line 300, in generic_visit
value = self.visit(value)
File "/data/hdd3/yugzh/ENAS_NLP/venv/lib/python3.5/site-packages/nni_annotation/specific_code_generator.py", line 295, in visit
return self._visit_children(node)
File "/data/hdd3/yugzh/ENAS_NLP/venv/lib/python3.5/site-packages/nni_annotation/specific_code_generator.py", line 333, in _visit_children
self.generic_visit(node)
File "/usr/lib/python3.5/ast.py", line 300, in generic_visit
value = self.visit(value)
File "/data/hdd3/yugzh/ENAS_NLP/venv/lib/python3.5/site-packages/nni_annotation/specific_code_generator.py", line 286, in visit
return self._visit_string(node)
File "/data/hdd3/yugzh/ENAS_NLP/venv/lib/python3.5/site-packages/nni_annotation/specific_code_generator.py", line 328, in _visit_string
raise AssertionError('Unexpected annotation function')
AssertionError: Unexpected annotation function
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/data/hdd3/yugzh/ENAS_NLP/venv/lib/python3.5/site-packages/nni_annotation/__init__.py", line 145, in _generate_specific_file
annotated_code = specific_code_generator.parse(src.read(), para_cfg["parameters"], module)
File "/data/hdd3/yugzh/ENAS_NLP/venv/lib/python3.5/site-packages/nni_annotation/specific_code_generator.py", line 357, in parse
raise RuntimeError('%d: %s' % (ast_tree.last_line, exc.args[0]))
AttributeError: 'Module' object has no attribute 'last_line'
|
AssertionError
|
def log_trial(args):
"""'get trial log path"""
trial_id_path_dict = {}
trial_id_list = []
nni_config = Config(get_config_filename(args))
rest_port = nni_config.get_config("restServerPort")
rest_pid = nni_config.get_config("restServerPid")
if not detect_process(rest_pid):
print_error("Experiment is not running...")
return
running, response = check_rest_server_quick(rest_port)
if running:
response = rest_get(trial_jobs_url(rest_port), REST_TIME_OUT)
if response and check_response(response):
content = json.loads(response.text)
for trial in content:
trial_id_list.append(trial.get("id"))
if trial.get("logPath"):
trial_id_path_dict[trial.get("id")] = trial["logPath"]
else:
print_error("Restful server is not running...")
exit(1)
if args.trial_id:
if args.trial_id not in trial_id_list:
print_error(
"Trial id {0} not correct, please check your command!".format(
args.trial_id
)
)
exit(1)
if trial_id_path_dict.get(args.trial_id):
print_normal(
"id:" + args.trial_id + " path:" + trial_id_path_dict[args.trial_id]
)
else:
print_error("Log path is not available yet, please wait...")
exit(1)
else:
print_normal("All of trial log info:")
for key in trial_id_path_dict:
print_normal("id:" + key + " path:" + trial_id_path_dict[key])
if not trial_id_path_dict:
print_normal("None")
|
def log_trial(args):
"""'get trial log path"""
trial_id_path_dict = {}
nni_config = Config(get_config_filename(args))
rest_port = nni_config.get_config("restServerPort")
rest_pid = nni_config.get_config("restServerPid")
if not detect_process(rest_pid):
print_error("Experiment is not running...")
return
running, response = check_rest_server_quick(rest_port)
if running:
response = rest_get(trial_jobs_url(rest_port), REST_TIME_OUT)
if response and check_response(response):
content = json.loads(response.text)
for trial in content:
trial_id_path_dict[trial["id"]] = trial["logPath"]
else:
print_error("Restful server is not running...")
exit(1)
if args.id:
if args.trial_id:
if trial_id_path_dict.get(args.trial_id):
print_normal(
"id:" + args.trial_id + " path:" + trial_id_path_dict[args.trial_id]
)
else:
print_error("trial id is not valid.")
exit(1)
else:
print_error("please specific the trial id.")
exit(1)
else:
for key in trial_id_path_dict:
print("id:" + key + " path:" + trial_id_path_dict[key])
|
https://github.com/microsoft/nni/issues/1548
|
root# nnictl log trial
Traceback (most recent call last):
File "/root/.pyenv/versions/3.6.8/bin/nnictl", line 11, in <module>
sys.exit(parse_args())
File "/root/.pyenv/versions/3.6.8/lib/python3.6/site-packages/nni_cmd/nnictl.py", line 217, in parse_args
args.func(args)
File "/root/.pyenv/versions/3.6.8/lib/python3.6/site-packages/nni_cmd/nnictl_utils.py", line 366, in log_trial
trial_id_path_dict[trial['id']] = trial['logPath']
KeyError: 'logPath'
|
KeyError
|
def send_email_sendgrid(sender, subject, message, recipients, image_png):
import sendgrid as sendgrid_lib
client = sendgrid_lib.SendGridAPIClient(sendgrid().apikey)
to_send = sendgrid_lib.Mail(
from_email=sender, to_emails=recipients, subject=subject
)
if email().format == "html":
to_send.add_content(message, "text/html")
else:
to_send.add_content(message, "text/plain")
if image_png:
to_send.add_attachment(image_png)
client.send(to_send)
|
def send_email_sendgrid(sender, subject, message, recipients, image_png):
import sendgrid as sendgrid_lib
client = sendgrid_lib.SendGridClient(
sendgrid().username, sendgrid().password, raise_errors=True
)
to_send = sendgrid_lib.Mail()
to_send.add_to(recipients)
to_send.set_from(sender)
to_send.set_subject(subject)
if email().format == "html":
to_send.set_html(message)
else:
to_send.set_text(message)
if image_png:
to_send.add_attachment(image_png)
client.send(to_send)
|
https://github.com/spotify/luigi/issues/1956
|
Traceback (most recent call last):
File "/home/ubuntu/.virtualenvs/janrain/local/lib/python2.7/site-packages/luigi/retcodes.py", line 74, in run_with_retcodes
worker = luigi.interface._run(argv)['worker']
File "/home/ubuntu/.virtualenvs/janrain/local/lib/python2.7/site-packages/luigi/interface.py", line 237, in _run
return _schedule_and_run([cp.get_task_obj()], worker_scheduler_factory)
File "/home/ubuntu/.virtualenvs/janrain/local/lib/python2.7/site-packages/luigi/interface.py", line 196, in _schedule_and_run
success &= worker.run()
File "/home/ubuntu/.virtualenvs/janrain/local/lib/python2.7/site-packages/luigi/worker.py", line 1025, in run
self._handle_next_task()
File "/home/ubuntu/.virtualenvs/janrain/local/lib/python2.7/site-packages/luigi/worker.py", line 908, in _handle_next_task
self._email_task_failure(task, expl)
File "/home/ubuntu/.virtualenvs/janrain/local/lib/python2.7/site-packages/luigi/worker.py", line 540, in _email_task_failure
headline="A task failed when running. Most likely run() raised an exception.",
File "/home/ubuntu/.virtualenvs/janrain/local/lib/python2.7/site-packages/luigi/worker.py", line 547, in _email_error
notifications.send_error_email(formatted_subject, message, task.owner_email)
File "/home/ubuntu/.virtualenvs/janrain/local/lib/python2.7/site-packages/luigi/notifications.py", line 356, in send_error_email
recipients=recipients
File "/home/ubuntu/.virtualenvs/janrain/local/lib/python2.7/site-packages/luigi/notifications.py", line 328, in send_email
email_sender(sender, subject, message, recipients, image_png)
File "/home/ubuntu/.virtualenvs/janrain/local/lib/python2.7/site-packages/luigi/notifications.py", line 241, in send_email_sendgrid
client = sendgrid_lib.SendGridClient(
AttributeError: 'module' object has no attribute 'SendGridClient'
|
AttributeError
|
def find_spec(self, name, path, target=None):
# If jvm is not started then we just check against the TLDs
if not _jpype.isStarted():
base = name.partition(".")[0]
if not base in _JDOMAINS:
return None
raise ImportError("Attempt to create Java package '%s' without jvm" % name)
# Check for aliases
if name in _JDOMAINS:
jname = _JDOMAINS[name]
if not _jpype.isPackage(jname):
raise ImportError(
"Java package '%s' not found, requested by alias '%s'" % (jname, name)
)
ms = _ModuleSpec(name, self)
ms._jname = jname
return ms
# Check if it is a TLD
parts = name.rpartition(".")
# Use the parent module to simplify name mangling
if not parts[1] and _jpype.isPackage(parts[2]):
ms = _ModuleSpec(name, self)
ms._jname = name
return ms
if not parts[1] and not _jpype.isPackage(parts[0]):
return None
base = sys.modules.get(parts[0], None)
if not base or not isinstance(base, _jpype._JPackage):
return None
# Support for external modules in java tree
name = unwrap(name)
for customizer in _CUSTOMIZERS:
if customizer.canCustomize(name):
return customizer.getSpec(name)
# Using isPackage eliminates need for registering tlds
if not hasattr(base, parts[2]):
# If the base is a Java package and it wasn't found in the
# package using getAttr, then we need to emit an error
# so we produce a meaningful diagnositic.
try:
# Use forname because it give better diagnostics
_jpype.JClass("java.lang.Class").forName(name)
raise ImportError("Class `%s` was found but was not expected" % name)
# Not found is acceptable
except Exception as ex:
raise ImportError("Failed to import '%s'" % name) from ex
# Import the java module
return _ModuleSpec(name, self)
|
def find_spec(self, name, path, target=None):
# If jvm is not started then we just check against the TLDs
if not _jpype.isStarted():
base = name.partition(".")[0]
if not base in _JDOMAINS:
return None
raise ImportError("Attempt to create Java package '%s' without jvm" % name)
# Check for aliases
if name in _JDOMAINS:
jname = _JDOMAINS[name]
if not _jpype.isPackage(jname):
raise ImportError(
"Java package '%s' not found, requested by alias '%s'" % (jname, name)
)
ms = _ModuleSpec(name, self)
ms._jname = jname
return ms
# Check if it is a TLD
parts = name.rpartition(".")
# Use the parent module to simplify name mangling
if not parts[1] and _jpype.isPackage(parts[2]):
ms = _ModuleSpec(name, self)
ms._jname = name
return ms
if not parts[1] and not _jpype.isPackage(parts[0]):
return None
base = sys.modules.get(parts[0], None)
if not base or not isinstance(base, _jpype._JPackage):
return None
# Support for external modules in java tree
name = unwrap(name)
for customizer in _CUSTOMIZERS:
if customizer.canCustomize(name):
return customizer.getSpec(name)
# Using isPackage eliminates need for registering tlds
if not hasattr(base, parts[2]):
# If the base is a Java package and it wasn't found in the
# package using getAttr, then we need to emit an error
# so we produce a meaningful diagnositic.
try:
# Use forname because it give better diagnostics
cls = _jpype.JClass("java.lang.Class").forName(name)
return _jpype.JClass(cls)
# Not found is acceptable
except Exception as ex:
raise ImportError("Failed to import '%s'" % name) from ex
# Import the java module
return _ModuleSpec(name, self)
|
https://github.com/jpype-project/jpype/issues/838
|
$ export CLASSPATH=lib/lucene-core-8.6.0.jar
$ python
Python 3.8.2 (default, Jul 16 2020, 14:00:26)
[GCC 9.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
import jpype
import jpype.imports
print(jpype.getDefaultJVMPath())
/usr/lib/jvm/java-14-openjdk-amd64/lib/server/libjvm.so
jpype.startJVM(jpype.getDefaultJVMPath())
from org.apache.lucene.search import BooleanClause
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<frozen importlib._bootstrap>", line 991, in _find_and_load
File "<frozen importlib._bootstrap>", line 975, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 652, in _load_unlocked
AttributeError: type object 'org.apache.lucene.search.BooleanClause' has no attribute 'loader'
|
AttributeError
|
def find_spec(self, name, path, target=None):
# If jvm is not started then we just check against the TLDs
if not _jpype.isStarted():
base = name.partition(".")[0]
if not base in _JDOMAINS:
return None
raise ImportError("Attempt to create java modules without jvm")
# Check if it is a TLD
parts = name.rpartition(".")
if not parts[1] and _jpype.isPackage(parts[2]):
return _ModuleSpec(name, self)
if not parts[1] and not _jpype.isPackage(parts[0]):
return None
base = sys.modules.get(parts[0], None)
if not base or not isinstance(base, _jpype._JPackage):
return None
# Support for external modules in java tree
name = unwrap(name)
for customizer in _CUSTOMIZERS:
if customizer.canCustomize(name):
return customizer.getSpec(name)
# Using isPackage eliminates need for registering tlds
if not hasattr(base, parts[2]):
# If the base is a Java package and it wasn't found in the
# package using getAttr, then we need to emit an error
# so we produce a meaningful diagnositic.
try:
# Use forname because it give better diagnostics
cls = _jpype.JClass("java.lang.Class").forName(name)
return _jpype.JClass(cls)
# Not found is acceptable
except Exception as ex:
raise ImportError("Failed to import '%s'" % name) from ex
# Import the java module
return _ModuleSpec(name, self)
|
def find_spec(self, name, path, target=None):
# If jvm is not started then we just check against the TLDs
if not _jpype.isStarted():
base = name.partition(".")[0]
if not base in _JDOMAINS:
return None
raise ImportError("Attempt to create java modules without jvm")
# Check if it is a TLD
parts = name.rpartition(".")
if not parts[1] and _jpype.isPackage(parts[2]):
return _ModuleSpec(name, self)
if not parts[1] and not _jpype.isPackage(parts[0]):
return None
base = sys.modules.get(parts[0], None)
if not base or not isinstance(base, _jpype._JPackage):
return None
# Support for external modules in java tree
name = unwrap(name)
for customizer in _CUSTOMIZERS:
if customizer.canCustomize(name):
return customizer.getSpec(name)
# Using isPackage eliminates need for registering tlds
if not hasattr(base, parts[2]):
# If the base is a Java package and it wasn't found in the
# package using getAttr, then we need to emit an error
# so we produce a meaningful diagnositic.
try:
# Use forname because it give better diagnostics
cls = _java_lang_Class.forName(name)
return _jpype.JClass(cls)
# Not found is acceptable
except Exception as ex:
raise ImportError("Failed to import '%s'" % name) from ex
# Import the java module
return _ModuleSpec(name, self)
|
https://github.com/jpype-project/jpype/issues/748
|
Traceback (most recent call last):
File "/home/nelson85/env/python-ubuntu/lib/python3.6/site-packages/JPype1-0.7.6.dev0-py3.6-linux-x86_64.egg/jpype/imports.py", line 188, in find_spec
cls = _java_lang_Class.forName(name)
NameError: name '_java_lang_Class' is not defined
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "securePaths.py", line 61, in <module>
from gov.llnl.ernie.nucsafe import Translator
File "/home/nelson85/env/python-ubuntu/lib/python3.6/site-packages/JPype1-0.7.6.dev0-py3.6-linux-x86_64.egg/jpype/imports.py", line 192, in find_spec
raise ImportError("Failed to import '%s'" % name) from ex
ImportError: Failed to import 'gov.llnl.ernie.nucsafe'
|
NameError
|
def build(
serviceName,
version,
http=None,
discoveryServiceUrl=DISCOVERY_URI,
developerKey=None,
model=None,
requestBuilder=HttpRequest,
credentials=None,
cache_discovery=True,
cache=None,
client_options=None,
adc_cert_path=None,
adc_key_path=None,
num_retries=1,
):
"""Construct a Resource for interacting with an API.
Construct a Resource object for interacting with an API. The serviceName and
version are the names from the Discovery service.
Args:
serviceName: string, name of the service.
version: string, the version of the service.
http: httplib2.Http, An instance of httplib2.Http or something that acts
like it that HTTP requests will be made through.
discoveryServiceUrl: string, a URI Template that points to the location of
the discovery service. It should have two parameters {api} and
{apiVersion} that when filled in produce an absolute URI to the discovery
document for that service.
developerKey: string, key obtained from
https://code.google.com/apis/console.
model: googleapiclient.Model, converts to and from the wire format.
requestBuilder: googleapiclient.http.HttpRequest, encapsulator for an HTTP
request.
credentials: oauth2client.Credentials or
google.auth.credentials.Credentials, credentials to be used for
authentication.
cache_discovery: Boolean, whether or not to cache the discovery doc.
cache: googleapiclient.discovery_cache.base.CacheBase, an optional
cache object for the discovery documents.
client_options: Mapping object or google.api_core.client_options, client
options to set user options on the client. The API endpoint should be set
through client_options. client_cert_source is not supported, client cert
should be provided using client_encrypted_cert_source instead.
adc_cert_path: str, client certificate file path to save the application
default client certificate for mTLS. This field is required if you want to
use the default client certificate.
adc_key_path: str, client encrypted private key file path to save the
application default client encrypted private key for mTLS. This field is
required if you want to use the default client certificate.
num_retries: Integer, number of times to retry discovery with
randomized exponential backoff in case of intermittent/connection issues.
Returns:
A Resource object with methods for interacting with the service.
Raises:
google.auth.exceptions.MutualTLSChannelError: if there are any problems
setting up mutual TLS channel.
"""
params = {"api": serviceName, "apiVersion": version}
if http is None:
discovery_http = build_http()
else:
discovery_http = http
for discovery_url in _discovery_service_uri_options(discoveryServiceUrl, version):
requested_url = uritemplate.expand(discovery_url, params)
try:
content = _retrieve_discovery_doc(
requested_url,
discovery_http,
cache_discovery,
cache,
developerKey,
num_retries=num_retries,
)
return build_from_document(
content,
base=discovery_url,
http=http,
developerKey=developerKey,
model=model,
requestBuilder=requestBuilder,
credentials=credentials,
client_options=client_options,
adc_cert_path=adc_cert_path,
adc_key_path=adc_key_path,
)
except HttpError as e:
if e.resp.status == http_client.NOT_FOUND:
continue
else:
raise e
raise UnknownApiNameOrVersion("name: %s version: %s" % (serviceName, version))
|
def build(
serviceName,
version,
http=None,
discoveryServiceUrl=DISCOVERY_URI,
developerKey=None,
model=None,
requestBuilder=HttpRequest,
credentials=None,
cache_discovery=True,
cache=None,
client_options=None,
adc_cert_path=None,
adc_key_path=None,
num_retries=1,
):
"""Construct a Resource for interacting with an API.
Construct a Resource object for interacting with an API. The serviceName and
version are the names from the Discovery service.
Args:
serviceName: string, name of the service.
version: string, the version of the service.
http: httplib2.Http, An instance of httplib2.Http or something that acts
like it that HTTP requests will be made through.
discoveryServiceUrl: string, a URI Template that points to the location of
the discovery service. It should have two parameters {api} and
{apiVersion} that when filled in produce an absolute URI to the discovery
document for that service.
developerKey: string, key obtained from
https://code.google.com/apis/console.
model: googleapiclient.Model, converts to and from the wire format.
requestBuilder: googleapiclient.http.HttpRequest, encapsulator for an HTTP
request.
credentials: oauth2client.Credentials or
google.auth.credentials.Credentials, credentials to be used for
authentication.
cache_discovery: Boolean, whether or not to cache the discovery doc.
cache: googleapiclient.discovery_cache.base.CacheBase, an optional
cache object for the discovery documents.
client_options: Mapping object or google.api_core.client_options, client
options to set user options on the client. The API endpoint should be set
through client_options. client_cert_source is not supported, client cert
should be provided using client_encrypted_cert_source instead.
adc_cert_path: str, client certificate file path to save the application
default client certificate for mTLS. This field is required if you want to
use the default client certificate.
adc_key_path: str, client encrypted private key file path to save the
application default client encrypted private key for mTLS. This field is
required if you want to use the default client certificate.
num_retries: Integer, number of times to retry discovery with
randomized exponential backoff in case of intermittent/connection issues.
Returns:
A Resource object with methods for interacting with the service.
Raises:
google.auth.exceptions.MutualTLSChannelError: if there are any problems
setting up mutual TLS channel.
"""
params = {"api": serviceName, "apiVersion": version}
if http is None:
discovery_http = build_http()
else:
discovery_http = http
for discovery_url in (discoveryServiceUrl, V2_DISCOVERY_URI):
requested_url = uritemplate.expand(discovery_url, params)
try:
content = _retrieve_discovery_doc(
requested_url,
discovery_http,
cache_discovery,
cache,
developerKey,
num_retries=num_retries,
)
return build_from_document(
content,
base=discovery_url,
http=http,
developerKey=developerKey,
model=model,
requestBuilder=requestBuilder,
credentials=credentials,
client_options=client_options,
adc_cert_path=adc_cert_path,
adc_key_path=adc_key_path,
)
except HttpError as e:
if e.resp.status == http_client.NOT_FOUND:
continue
else:
raise e
raise UnknownApiNameOrVersion("name: %s version: %s" % (serviceName, version))
|
https://github.com/googleapis/google-api-python-client/issues/971
|
from googleapiclient.discovery import build
build("cloudresourcemanager", version=None)
Traceback (most recent call last):
...
googleapiclient.errors.HttpError:
<HttpError 400 when requesting https://www.googleapis.com/discovery/v1/apis/cloudresourcemanager//rest
returned "Request contains an invalid argument.">
|
googleapiclient.errors.HttpError
|
def print_table(response, title):
"""Prints out a response table.
Each row contains key(s), clicks, impressions, CTR, and average position.
Args:
response: The server response to be printed as a table.
title: The title of the table.
"""
print("\n --" + title + ":")
if "rows" not in response:
print("Empty response")
return
rows = response["rows"]
row_format = "{:<20}" + "{:>20}" * 4
print(row_format.format("Keys", "Clicks", "Impressions", "CTR", "Position"))
for row in rows:
keys = ""
# Keys are returned only if one or more dimensions are requested.
if "keys" in row:
keys = ",".join(row["keys"]).encode("utf-8").decode()
print(
row_format.format(
keys, row["clicks"], row["impressions"], row["ctr"], row["position"]
)
)
|
def print_table(response, title):
"""Prints out a response table.
Each row contains key(s), clicks, impressions, CTR, and average position.
Args:
response: The server response to be printed as a table.
title: The title of the table.
"""
print("\n --" + title + ":")
if "rows" not in response:
print("Empty response")
return
rows = response["rows"]
row_format = "{:<20}" + "{:>20}" * 4
print(row_format.format("Keys", "Clicks", "Impressions", "CTR", "Position"))
for row in rows:
keys = ""
# Keys are returned only if one or more dimensions are requested.
if "keys" in row:
keys = ",".join(row["keys"]).encode("utf-8")
print(
row_format.format(
keys, row["clicks"], row["impressions"], row["ctr"], row["position"]
)
)
|
https://github.com/googleapis/google-api-python-client/issues/732
|
Traceback (most recent call last):
File "search_analytics_api_sample.py", line 197, in <module>
main(sys.argv)
File "search_analytics_api_sample.py", line 71, in main
print_table(response, 'Available dates')
File "search_analytics_api_sample.py", line 194, in print_table
print(row_format.format(keys, row['clicks'], row['impressions'], row['ctr'], row['position']))
TypeError: unsupported format string passed to bytes.__format__
|
TypeError
|
def init(
argv, name, version, doc, filename, scope=None, parents=[], discovery_filename=None
):
"""A common initialization routine for samples.
Many of the sample applications do the same initialization, which has now
been consolidated into this function. This function uses common idioms found
in almost all the samples, i.e. for an API with name 'apiname', the
credentials are stored in a file named apiname.dat, and the
client_secrets.json file is stored in the same directory as the application
main file.
Args:
argv: list of string, the command-line parameters of the application.
name: string, name of the API.
version: string, version of the API.
doc: string, description of the application. Usually set to __doc__.
file: string, filename of the application. Usually set to __file__.
parents: list of argparse.ArgumentParser, additional command-line flags.
scope: string, The OAuth scope used.
discovery_filename: string, name of local discovery file (JSON). Use when discovery doc not available via URL.
Returns:
A tuple of (service, flags), where service is the service object and flags
is the parsed command-line flags.
"""
try:
from oauth2client import client
from oauth2client import file
from oauth2client import tools
except ImportError:
raise ImportError(
"googleapiclient.sample_tools requires oauth2client. Please install oauth2client and try again."
)
if scope is None:
scope = "https://www.googleapis.com/auth/" + name
# Parser command-line arguments.
parent_parsers = [tools.argparser]
parent_parsers.extend(parents)
parser = argparse.ArgumentParser(
description=doc,
formatter_class=argparse.RawDescriptionHelpFormatter,
parents=parent_parsers,
)
flags = parser.parse_args(argv[1:])
# Name of a file containing the OAuth 2.0 information for this
# application, including client_id and client_secret, which are found
# on the API Access tab on the Google APIs
# Console <http://code.google.com/apis/console>.
client_secrets = os.path.join(os.path.dirname(filename), "client_secrets.json")
# Set up a Flow object to be used if we need to authenticate.
flow = client.flow_from_clientsecrets(
client_secrets, scope=scope, message=tools.message_if_missing(client_secrets)
)
# Prepare credentials, and authorize HTTP object with them.
# If the credentials don't exist or are invalid run through the native client
# flow. The Storage object will ensure that if successful the good
# credentials will get written back to a file.
storage = file.Storage(name + ".dat")
credentials = storage.get()
if credentials is None or credentials.invalid:
credentials = tools.run_flow(flow, storage, flags)
http = credentials.authorize(http=build_http())
if discovery_filename is None:
# Construct a service object via the discovery service.
service = discovery.build(name, version, http=http)
else:
# Construct a service object using a local discovery document file.
with open(discovery_filename) as discovery_file:
service = discovery.build_from_document(
discovery_file.read(), base="https://www.googleapis.com/", http=http
)
return (service, flags)
|
def init(
argv, name, version, doc, filename, scope=None, parents=[], discovery_filename=None
):
"""A common initialization routine for samples.
Many of the sample applications do the same initialization, which has now
been consolidated into this function. This function uses common idioms found
in almost all the samples, i.e. for an API with name 'apiname', the
credentials are stored in a file named apiname.dat, and the
client_secrets.json file is stored in the same directory as the application
main file.
Args:
argv: list of string, the command-line parameters of the application.
name: string, name of the API.
version: string, version of the API.
doc: string, description of the application. Usually set to __doc__.
file: string, filename of the application. Usually set to __file__.
parents: list of argparse.ArgumentParser, additional command-line flags.
scope: string, The OAuth scope used.
discovery_filename: string, name of local discovery file (JSON). Use when discovery doc not available via URL.
Returns:
A tuple of (service, flags), where service is the service object and flags
is the parsed command-line flags.
"""
if scope is None:
scope = "https://www.googleapis.com/auth/" + name
# Parser command-line arguments.
parent_parsers = [tools.argparser]
parent_parsers.extend(parents)
parser = argparse.ArgumentParser(
description=doc,
formatter_class=argparse.RawDescriptionHelpFormatter,
parents=parent_parsers,
)
flags = parser.parse_args(argv[1:])
# Name of a file containing the OAuth 2.0 information for this
# application, including client_id and client_secret, which are found
# on the API Access tab on the Google APIs
# Console <http://code.google.com/apis/console>.
client_secrets = os.path.join(os.path.dirname(filename), "client_secrets.json")
# Set up a Flow object to be used if we need to authenticate.
flow = client.flow_from_clientsecrets(
client_secrets, scope=scope, message=tools.message_if_missing(client_secrets)
)
# Prepare credentials, and authorize HTTP object with them.
# If the credentials don't exist or are invalid run through the native client
# flow. The Storage object will ensure that if successful the good
# credentials will get written back to a file.
storage = file.Storage(name + ".dat")
credentials = storage.get()
if credentials is None or credentials.invalid:
credentials = tools.run_flow(flow, storage, flags)
http = credentials.authorize(http=build_http())
if discovery_filename is None:
# Construct a service object via the discovery service.
service = discovery.build(name, version, http=http)
else:
# Construct a service object using a local discovery document file.
with open(discovery_filename) as discovery_file:
service = discovery.build_from_document(
discovery_file.read(), base="https://www.googleapis.com/", http=http
)
return (service, flags)
|
https://github.com/googleapis/google-api-python-client/issues/524
|
Python 3.6.5 (default, Mar 31 2018, 13:05:03)
[GCC 4.2.1 Compatible Apple LLVM 9.1.0 (clang-902.0.39.1)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
from apiclient import discovery
Traceback (most recent call last):
File "/Users/eric/projects/google-api-python-client/googleapiclient/sample_tools.py", line 32, in <module>
from oauth2client import client
ModuleNotFoundError: No module named 'oauth2client'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/Users/eric/projects/google-api-python-client/apiclient/__init__.py", line 13, in <module>
from googleapiclient import sample_tools
File "/Users/eric/projects/google-api-python-client/googleapiclient/sample_tools.py", line 36, in <module>
raise ImportError('googleapiclient.sample_tools requires oauth2client. Please install oauth2client and try again.')
ImportError: googleapiclient.sample_tools requires oauth2client. Please install oauth2client and try again.
|
ModuleNotFoundError
|
def encode_block(self, obj):
"""
Parameters
----------
obj : AtomGroup or Universe
"""
traj = obj.universe.trajectory
ts = traj.ts
try:
molecule = ts.data["molecule"]
except KeyError:
raise_from(
NotImplementedError("MOL2Writer cannot currently write non MOL2 data"), None
)
# Need to remap atom indices to 1 based in this selection
mapping = {a: i for i, a in enumerate(obj.atoms, start=1)}
# Grab only bonds between atoms in the obj
# ie none that extend out of it
bondgroup = obj.bonds.atomgroup_intersection(obj, strict=True)
bonds = sorted((b[0], b[1], b.order) for b in bondgroup)
bond_lines = ["@<TRIPOS>BOND"]
bond_lines.extend(
"{0:>5} {1:>5} {2:>5} {3:>2}".format(bid, mapping[atom1], mapping[atom2], order)
for bid, (atom1, atom2, order) in enumerate(bonds, start=1)
)
bond_lines.append("\n")
bond_lines = "\n".join(bond_lines)
atom_lines = ["@<TRIPOS>ATOM"]
atom_lines.extend(
"{0:>4} {1:>4} {2:>13.4f} {3:>9.4f} {4:>9.4f} {5:>4} {6} {7} {8:>7.4f}".format(
mapping[a],
a.name,
a.position[0],
a.position[1],
a.position[2],
a.type,
a.resid,
a.resname,
a.charge,
)
for a in obj.atoms
)
atom_lines.append("\n")
atom_lines = "\n".join(atom_lines)
try:
substructure = ["@<TRIPOS>SUBSTRUCTURE\n"] + ts.data["substructure"]
except KeyError:
substructure = ""
check_sums = molecule[1].split()
check_sums[0], check_sums[1] = str(len(obj.atoms)), str(len(bondgroup))
# prevent behavior change between repeated calls
# see gh-2678
molecule_0_store = molecule[0]
molecule_1_store = molecule[1]
molecule[1] = "{0}\n".format(" ".join(check_sums))
molecule.insert(0, "@<TRIPOS>MOLECULE\n")
return_val = "".join(molecule) + atom_lines + bond_lines + "".join(substructure)
molecule[0] = molecule_0_store
molecule[1] = molecule_1_store
return return_val
|
def encode_block(self, obj):
"""
Parameters
----------
obj : AtomGroup or Universe
"""
traj = obj.universe.trajectory
ts = traj.ts
try:
molecule = ts.data["molecule"]
except KeyError:
raise_from(
NotImplementedError("MOL2Writer cannot currently write non MOL2 data"), None
)
# Need to remap atom indices to 1 based in this selection
mapping = {a: i for i, a in enumerate(obj.atoms, start=1)}
# Grab only bonds between atoms in the obj
# ie none that extend out of it
bondgroup = obj.bonds.atomgroup_intersection(obj, strict=True)
bonds = sorted((b[0], b[1], b.order) for b in bondgroup)
bond_lines = ["@<TRIPOS>BOND"]
bond_lines.extend(
"{0:>5} {1:>5} {2:>5} {3:>2}".format(bid, mapping[atom1], mapping[atom2], order)
for bid, (atom1, atom2, order) in enumerate(bonds, start=1)
)
bond_lines.append("\n")
bond_lines = "\n".join(bond_lines)
atom_lines = ["@<TRIPOS>ATOM"]
atom_lines.extend(
"{0:>4} {1:>4} {2:>13.4f} {3:>9.4f} {4:>9.4f} {5:>4} {6} {7} {8:>7.4f}".format(
mapping[a],
a.name,
a.position[0],
a.position[1],
a.position[2],
a.type,
a.resid,
a.resname,
a.charge,
)
for a in obj.atoms
)
atom_lines.append("\n")
atom_lines = "\n".join(atom_lines)
try:
substructure = ["@<TRIPOS>SUBSTRUCTURE\n"] + ts.data["substructure"]
except KeyError:
substructure = ""
check_sums = molecule[1].split()
check_sums[0], check_sums[1] = str(len(obj.atoms)), str(len(bondgroup))
molecule[1] = "{0}\n".format(" ".join(check_sums))
molecule.insert(0, "@<TRIPOS>MOLECULE\n")
return "".join(molecule) + atom_lines + bond_lines + "".join(substructure)
|
https://github.com/MDAnalysis/mdanalysis/issues/2678
|
import MDAnalysis as mda
mda.__version__
'0.20.1'
from MDAnalysis.tests.datafiles import mol2_molecules
u = mda.Universe(mol2_molecules)
u.atoms[:4].write('group1.mol2') # 👍
u.atoms[:4].write('group2.mol2') # 👎
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "MDAnalysis/core/groups.py", line 3123, in write
w.write(self.atoms)
File "MDAnalysis/coordinates/MOL2.py", line 373, in write
self.write_next_timestep(obj)
File "MDAnalysis/coordinates/MOL2.py", line 382, in write_next_timestep
block = self.encode_block(obj)
File "MDAnalysis/coordinates/MOL2.py", line 360, in encode_block
check_sums[0], check_sums[1] = str(len(obj.atoms)), str(len(bondgroup))
IndexError: list assignment index out of range
|
IndexError
|
def __init__(
self,
atomgroup,
reference=None,
select="all",
groupselections=None,
weights=None,
weights_groupselections=False,
tol_mass=0.1,
ref_frame=0,
**kwargs,
):
r"""Parameters
----------
atomgroup : AtomGroup or Universe
Group of atoms for which the RMSD is calculated. If a trajectory is
associated with the atoms then the computation iterates over the
trajectory.
reference : AtomGroup or Universe (optional)
Group of reference atoms; if ``None`` then the current frame of
`atomgroup` is used.
select : str or dict or tuple (optional)
The selection to operate on; can be one of:
1. any valid selection string for
:meth:`~MDAnalysis.core.groups.AtomGroup.select_atoms` that
produces identical selections in `atomgroup` and `reference`; or
2. a dictionary ``{'mobile': sel1, 'reference': sel2}`` where *sel1*
and *sel2* are valid selection strings that are applied to
`atomgroup` and `reference` respectively (the
:func:`MDAnalysis.analysis.align.fasta2select` function returns such
a dictionary based on a ClustalW_ or STAMP_ sequence alignment); or
3. a tuple ``(sel1, sel2)``
When using 2. or 3. with *sel1* and *sel2* then these selection strings
are applied to `atomgroup` and `reference` respectively and should
generate *groups of equivalent atoms*. *sel1* and *sel2* can each also
be a *list of selection strings* to generate a
:class:`~MDAnalysis.core.groups.AtomGroup` with defined atom order as
described under :ref:`ordered-selections-label`).
groupselections : list (optional)
A list of selections as described for `select`, with the difference
that these selections are *always applied to the full universes*,
i.e., ``atomgroup.universe.select_atoms(sel1)`` and
``reference.universe.select_atoms(sel2)``. Each selection describes
additional RMSDs to be computed *after the structures have been
superimposed* according to `select`. No additional fitting is
performed.The output contains one additional column for each
selection.
.. Note:: Experimental feature. Only limited error checking
implemented.
weights : {"mass", ``None``} or array_like (optional)
1. "mass" will use masses as weights for both `select` and `groupselections`.
2. ``None`` will weigh each atom equally for both `select` and `groupselections`.
3. If 1D float array of the same length as `atomgroup` is provided,
use each element of the `array_like` as a weight for the
corresponding atom in `select`, and assumes ``None`` for `groupselections`.
weights_groupselections : False or list of {"mass", ``None`` or array_like} (optional)
1. ``False`` will apply imposed weights to `groupselections` from ``weights`` option.
2. A list of {"mass", ``None`` or array_like} with the length of `groupselections`
will apply the weights to `groupselections` correspondingly.
tol_mass : float (optional)
Reject match if the atomic masses for matched atoms differ by more
than `tol_mass`.
ref_frame : int (optional)
frame index to select frame from `reference`
verbose : bool (optional)
Show detailed progress of the calculation if set to ``True``; the
default is ``False``.
Raises
------
SelectionError
If the selections from `atomgroup` and `reference` do not match.
TypeError
If `weights` or `weights_groupselections` is not of the appropriate type;
see also :func:`MDAnalysis.lib.util.get_weights`
ValueError
If `weights` are not compatible with `atomgroup` (not the same
length) or if it is not a 1D array (see
:func:`MDAnalysis.lib.util.get_weights`).
A :exc:`ValueError` is also raised if the length of `weights_groupselections`
are not compatible with `groupselections`.
Notes
-----
The root mean square deviation :math:`\rho(t)` of a group of :math:`N`
atoms relative to a reference structure as a function of time is
calculated as
.. math::
\rho(t) = \sqrt{\frac{1}{N} \sum_{i=1}^N w_i \left(\mathbf{x}_i(t)
- \mathbf{x}_i^{\text{ref}}\right)^2}
The weights :math:`w_i` are calculated from the input weights `weights`
:math:`w'_i` as relative to the mean of the input weights:
.. math::
w_i = \frac{w'_i}{\langle w' \rangle}
The selected coordinates from `atomgroup` are optimally superimposed
(translation and rotation) on the `reference` coordinates at each time step
as to minimize the RMSD. Douglas Theobald's fast QCP algorithm
[Theobald2005]_ is used for the rotational superposition and to calculate
the RMSD (see :mod:`MDAnalysis.lib.qcprot` for implementation details).
The class runs various checks on the input to ensure that the two atom
groups can be compared. This includes a comparison of atom masses (i.e.,
only the positions of atoms of the same mass will be considered to be
correct for comparison). If masses should not be checked, just set
`tol_mass` to a large value such as 1000.
.. _ClustalW: http://www.clustal.org/
.. _STAMP: http://www.compbio.dundee.ac.uk/manuals/stamp.4.2/
See Also
--------
rmsd
.. versionadded:: 0.7.7
.. versionchanged:: 0.8
`groupselections` added
.. versionchanged:: 0.16.0
Flexible weighting scheme with new `weights` keyword.
.. deprecated:: 0.16.0
Instead of ``mass_weighted=True`` (removal in 0.17.0) use new
``weights='mass'``; refactored to fit with AnalysisBase API
.. versionchanged:: 0.17.0
removed deprecated `mass_weighted` keyword; `groupselections`
are *not* rotationally superimposed any more.
.. versionchanged:: 1.0.0
`filename` keyword was removed.
"""
super(RMSD, self).__init__(atomgroup.universe.trajectory, **kwargs)
self.atomgroup = atomgroup
self.reference = reference if reference is not None else self.atomgroup
select = process_selection(select)
self.groupselections = (
[process_selection(s) for s in groupselections]
if groupselections is not None
else []
)
self.weights = weights
self.tol_mass = tol_mass
self.ref_frame = ref_frame
self.weights_groupselections = weights_groupselections
self.ref_atoms = self.reference.select_atoms(*select["reference"])
self.mobile_atoms = self.atomgroup.select_atoms(*select["mobile"])
if len(self.ref_atoms) != len(self.mobile_atoms):
err = (
"Reference and trajectory atom selections do "
"not contain the same number of atoms: "
"N_ref={0:d}, N_traj={1:d}".format(
self.ref_atoms.n_atoms, self.mobile_atoms.n_atoms
)
)
logger.exception(err)
raise SelectionError(err)
logger.info("RMS calculation for {0:d} atoms.".format(len(self.ref_atoms)))
mass_mismatches = (
np.absolute((self.ref_atoms.masses - self.mobile_atoms.masses)) > self.tol_mass
)
if np.any(mass_mismatches):
# diagnostic output:
logger.error("Atoms: reference | mobile")
for ar, at in zip(self.ref_atoms, self.mobile_atoms):
if ar.name != at.name:
logger.error(
"{0!s:>4} {1:3d} {2!s:>3} {3!s:>3} {4:6.3f}"
"| {5!s:>4} {6:3d} {7!s:>3} {8!s:>3}"
"{9:6.3f}".format(
ar.segid,
ar.resid,
ar.resname,
ar.name,
ar.mass,
at.segid,
at.resid,
at.resname,
at.name,
at.mass,
)
)
errmsg = (
"Inconsistent selections, masses differ by more than"
"{0:f}; mis-matching atoms"
"are shown above.".format(self.tol_mass)
)
logger.error(errmsg)
raise SelectionError(errmsg)
del mass_mismatches
# TODO:
# - make a group comparison a class that contains the checks above
# - use this class for the *select* group and the additional
# *groupselections* groups each a dict with reference/mobile
self._groupselections_atoms = [
{
"reference": self.reference.universe.select_atoms(*s["reference"]),
"mobile": self.atomgroup.universe.select_atoms(*s["mobile"]),
}
for s in self.groupselections
]
# sanity check
for igroup, (sel, atoms) in enumerate(
zip(self.groupselections, self._groupselections_atoms)
):
if len(atoms["mobile"]) != len(atoms["reference"]):
logger.exception("SelectionError: Group Selection")
raise SelectionError(
"Group selection {0}: {1} | {2}: Reference and trajectory "
"atom selections do not contain the same number of atoms: "
"N_ref={3}, N_traj={4}".format(
igroup,
sel["reference"],
sel["mobile"],
len(atoms["reference"]),
len(atoms["mobile"]),
)
)
# check weights type
if iterable(self.weights) and (
np.array(weights).dtype not in (np.dtype("float64"), np.dtype("int64"))
):
raise TypeError(
"weight should only be be 'mass', None or 1D float array."
"For weights on groupselections, use **weight_groupselections** "
)
if iterable(self.weights) or self.weights != "mass":
get_weights(self.mobile_atoms, self.weights)
if self.weights_groupselections:
if len(self.weights_groupselections) != len(self.groupselections):
raise ValueError(
"Length of weights_groupselections is not equal to "
"length of groupselections "
)
for weights, atoms, selection in zip(
self.weights_groupselections,
self._groupselections_atoms,
self.groupselections,
):
try:
if iterable(weights) or weights != "mass":
get_weights(atoms["mobile"], weights)
except Exception as e:
raise type(e)(str(e) + " happens in selection %s" % selection["mobile"])
|
def __init__(
self,
atomgroup,
reference=None,
select="all",
groupselections=None,
weights=None,
tol_mass=0.1,
ref_frame=0,
**kwargs,
):
r"""Parameters
----------
atomgroup : AtomGroup or Universe
Group of atoms for which the RMSD is calculated. If a trajectory is
associated with the atoms then the computation iterates over the
trajectory.
reference : AtomGroup or Universe (optional)
Group of reference atoms; if ``None`` then the current frame of
`atomgroup` is used.
select : str or dict or tuple (optional)
The selection to operate on; can be one of:
1. any valid selection string for
:meth:`~MDAnalysis.core.groups.AtomGroup.select_atoms` that
produces identical selections in `atomgroup` and `reference`; or
2. a dictionary ``{'mobile': sel1, 'reference': sel2}`` where *sel1*
and *sel2* are valid selection strings that are applied to
`atomgroup` and `reference` respectively (the
:func:`MDAnalysis.analysis.align.fasta2select` function returns such
a dictionary based on a ClustalW_ or STAMP_ sequence alignment); or
3. a tuple ``(sel1, sel2)``
When using 2. or 3. with *sel1* and *sel2* then these selection strings
are applied to `atomgroup` and `reference` respectively and should
generate *groups of equivalent atoms*. *sel1* and *sel2* can each also
be a *list of selection strings* to generate a
:class:`~MDAnalysis.core.groups.AtomGroup` with defined atom order as
described under :ref:`ordered-selections-label`).
groupselections : list (optional)
A list of selections as described for `select`, with the difference
that these selections are *always applied to the full universes*,
i.e., ``atomgroup.universe.select_atoms(sel1)`` and
``reference.universe.select_atoms(sel2)``. Each selection describes
additional RMSDs to be computed *after the structures have been
superimposed* according to `select`. No additional fitting is
performed.The output contains one additional column for each
selection.
.. Note:: Experimental feature. Only limited error checking
implemented.
weights : {"mass", ``None``} or array_like (optional)
choose weights. With ``"mass"`` uses masses as weights; with ``None``
weigh each atom equally. If a float array of the same length as
`atomgroup` is provided, use each element of the `array_like` as a
weight for the corresponding atom in `atomgroup`.
tol_mass : float (optional)
Reject match if the atomic masses for matched atoms differ by more
than `tol_mass`.
ref_frame : int (optional)
frame index to select frame from `reference`
verbose : bool (optional)
Show detailed progress of the calculation if set to ``True``; the
default is ``False``.
Raises
------
SelectionError
If the selections from `atomgroup` and `reference` do not match.
TypeError
If `weights` is not of the appropriate type; see also
:func:`MDAnalysis.lib.util.get_weights`
ValueError
If `weights` are not compatible with `atomgroup` (not the same
length) or if it is not a 1D array (see
:func:`MDAnalysis.lib.util.get_weights`).
A :exc:`ValueError` is also raised if `weights` are not compatible
with `groupselections`: only equal weights (``weights=None``) or
mass-weighted (``weights="mass"``) are supported for additional
`groupselections`.
Notes
-----
The root mean square deviation :math:`\rho(t)` of a group of :math:`N`
atoms relative to a reference structure as a function of time is
calculated as
.. math::
\rho(t) = \sqrt{\frac{1}{N} \sum_{i=1}^N w_i \left(\mathbf{x}_i(t)
- \mathbf{x}_i^{\text{ref}}\right)^2}
The weights :math:`w_i` are calculated from the input weights `weights`
:math:`w'_i` as relative to the mean of the input weights:
.. math::
w_i = \frac{w'_i}{\langle w' \rangle}
The selected coordinates from `atomgroup` are optimally superimposed
(translation and rotation) on the `reference` coordinates at each time step
as to minimize the RMSD. Douglas Theobald's fast QCP algorithm
[Theobald2005]_ is used for the rotational superposition and to calculate
the RMSD (see :mod:`MDAnalysis.lib.qcprot` for implementation details).
The class runs various checks on the input to ensure that the two atom
groups can be compared. This includes a comparison of atom masses (i.e.,
only the positions of atoms of the same mass will be considered to be
correct for comparison). If masses should not be checked, just set
`tol_mass` to a large value such as 1000.
.. _ClustalW: http://www.clustal.org/
.. _STAMP: http://www.compbio.dundee.ac.uk/manuals/stamp.4.2/
See Also
--------
rmsd
.. versionadded:: 0.7.7
.. versionchanged:: 0.8
`groupselections` added
.. versionchanged:: 0.16.0
Flexible weighting scheme with new `weights` keyword.
.. deprecated:: 0.16.0
Instead of ``mass_weighted=True`` (removal in 0.17.0) use new
``weights='mass'``; refactored to fit with AnalysisBase API
.. versionchanged:: 0.17.0
removed deprecated `mass_weighted` keyword; `groupselections`
are *not* rotationally superimposed any more.
.. versionchanged:: 1.0.0
`filename` keyword was removed.
"""
super(RMSD, self).__init__(atomgroup.universe.trajectory, **kwargs)
self.atomgroup = atomgroup
self.reference = reference if reference is not None else self.atomgroup
select = process_selection(select)
self.groupselections = (
[process_selection(s) for s in groupselections]
if groupselections is not None
else []
)
self.weights = weights
self.tol_mass = tol_mass
self.ref_frame = ref_frame
self.ref_atoms = self.reference.select_atoms(*select["reference"])
self.mobile_atoms = self.atomgroup.select_atoms(*select["mobile"])
if len(self.ref_atoms) != len(self.mobile_atoms):
err = (
"Reference and trajectory atom selections do "
"not contain the same number of atoms: "
"N_ref={0:d}, N_traj={1:d}".format(
self.ref_atoms.n_atoms, self.mobile_atoms.n_atoms
)
)
logger.exception(err)
raise SelectionError(err)
logger.info("RMS calculation for {0:d} atoms.".format(len(self.ref_atoms)))
mass_mismatches = (
np.absolute((self.ref_atoms.masses - self.mobile_atoms.masses)) > self.tol_mass
)
if np.any(mass_mismatches):
# diagnostic output:
logger.error("Atoms: reference | mobile")
for ar, at in zip(self.ref_atoms, self.mobile_atoms):
if ar.name != at.name:
logger.error(
"{0!s:>4} {1:3d} {2!s:>3} {3!s:>3} {4:6.3f}"
"| {5!s:>4} {6:3d} {7!s:>3} {8!s:>3}"
"{9:6.3f}".format(
ar.segid,
ar.resid,
ar.resname,
ar.name,
ar.mass,
at.segid,
at.resid,
at.resname,
at.name,
at.mass,
)
)
errmsg = (
"Inconsistent selections, masses differ by more than"
"{0:f}; mis-matching atoms"
"are shown above.".format(self.tol_mass)
)
logger.error(errmsg)
raise SelectionError(errmsg)
del mass_mismatches
# TODO:
# - make a group comparison a class that contains the checks above
# - use this class for the *select* group and the additional
# *groupselections* groups each a dict with reference/mobile
self._groupselections_atoms = [
{
"reference": self.reference.universe.select_atoms(*s["reference"]),
"mobile": self.atomgroup.universe.select_atoms(*s["mobile"]),
}
for s in self.groupselections
]
# sanity check
for igroup, (sel, atoms) in enumerate(
zip(self.groupselections, self._groupselections_atoms)
):
if len(atoms["mobile"]) != len(atoms["reference"]):
logger.exception("SelectionError: Group Selection")
raise SelectionError(
"Group selection {0}: {1} | {2}: Reference and trajectory "
"atom selections do not contain the same number of atoms: "
"N_ref={3}, N_traj={4}".format(
igroup,
sel["reference"],
sel["mobile"],
len(atoms["reference"]),
len(atoms["mobile"]),
)
)
# Explicitly check for "mass" because this option CAN
# be used with groupselection. (get_weights() returns the mass array
# for "mass")
if not iterable(self.weights) and self.weights == "mass":
pass
else:
self.weights = get_weights(self.mobile_atoms, self.weights)
# cannot use arbitrary weight array (for superposition) with
# groupselections because arrays will not match
if len(self.groupselections) > 0 and (
iterable(self.weights) or self.weights not in ("mass", None)
):
raise ValueError(
"groupselections can only be combined with "
"weights=None or weights='mass', not a weight "
"array."
)
|
https://github.com/MDAnalysis/mdanalysis/issues/2429
|
import MDAnalysis as mda
from MDAnalysis.tests.datafiles import PSF, DCD, CRD
import MDAnalysis.analysis.rms as rms
mda.__version__
'0.20.1'
u = mda.Universe(PSF, DCD) # closed AdK (PDB ID: 1AKE)
R = rms.RMSD(u, # universe to align
... u, # reference universe or atomgroup
... select='backbone', # group to superimpose and calculate RMSD
... groupselections=["all"], # groups for RMSD
... weights='mass') # weights for each atom
R.run()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "MDAnalysis/analysis/base.py", line 197, in run
self._single_frame()
File "MDAnalysis/analysis/rms.py", line 638, in _single_frame
center=False, superposition=False)
File "MDAnalysis/analysis/rms.py", line 247, in rmsd
raise ValueError('weights must have same length as a and b')
ValueError: weights must have same length as a and b
|
ValueError
|
def _prepare(self):
self._n_atoms = self.mobile_atoms.n_atoms
if not self.weights_groupselections:
if not iterable(self.weights): # apply 'mass' or 'None' to groupselections
self.weights_groupselections = [self.weights] * len(self.groupselections)
else:
self.weights_groupselections = [None] * len(self.groupselections)
for igroup, (weights, atoms) in enumerate(
zip(self.weights_groupselections, self._groupselections_atoms)
):
if str(weights) == "mass":
self.weights_groupselections[igroup] = atoms["mobile"].masses
if weights is not None:
self.weights_groupselections[igroup] = np.asarray(
self.weights_groupselections[igroup], dtype=np.float64
) / np.mean(self.weights_groupselections[igroup])
# add the array of weights to weights_select
self.weights_select = get_weights(self.mobile_atoms, self.weights)
self.weights_ref = get_weights(self.ref_atoms, self.weights)
if self.weights_select is not None:
self.weights_select = np.asarray(
self.weights_select, dtype=np.float64
) / np.mean(self.weights_select)
self.weights_ref = np.asarray(self.weights_ref, dtype=np.float64) / np.mean(
self.weights_ref
)
current_frame = self.reference.universe.trajectory.ts.frame
try:
# Move to the ref_frame
# (coordinates MUST be stored in case the ref traj is advanced
# elsewhere or if ref == mobile universe)
self.reference.universe.trajectory[self.ref_frame]
self._ref_com = self.ref_atoms.center(self.weights_ref)
# makes a copy
self._ref_coordinates = self.ref_atoms.positions - self._ref_com
if self._groupselections_atoms:
self._groupselections_ref_coords64 = [
(
self.reference.select_atoms(*s["reference"]).positions.astype(
np.float64
)
)
for s in self.groupselections
]
finally:
# Move back to the original frame
self.reference.universe.trajectory[current_frame]
self._ref_coordinates64 = self._ref_coordinates.astype(np.float64)
if self._groupselections_atoms:
# Only carry out a rotation if we want to calculate secondary
# RMSDs.
# R: rotation matrix that aligns r-r_com, x~-x~com
# (x~: selected coordinates, x: all coordinates)
# Final transformed traj coordinates: x' = (x-x~_com)*R + ref_com
self._rot = np.zeros(9, dtype=np.float64) # allocate space
self._R = self._rot.reshape(3, 3)
else:
self._rot = None
self.rmsd = np.zeros((self.n_frames, 3 + len(self._groupselections_atoms)))
self._pm.format = (
"RMSD {rmsd:5.2f} A at frame {step:5d}/{numsteps} [{percentage:5.1f}%]"
)
self._mobile_coordinates64 = self.mobile_atoms.positions.copy().astype(np.float64)
|
def _prepare(self):
self._n_atoms = self.mobile_atoms.n_atoms
if not iterable(self.weights) and self.weights == "mass":
self.weights = self.ref_atoms.masses
if self.weights is not None:
self.weights = np.asarray(self.weights, dtype=np.float64) / np.mean(
self.weights
)
current_frame = self.reference.universe.trajectory.ts.frame
try:
# Move to the ref_frame
# (coordinates MUST be stored in case the ref traj is advanced
# elsewhere or if ref == mobile universe)
self.reference.universe.trajectory[self.ref_frame]
self._ref_com = self.ref_atoms.center(self.weights)
# makes a copy
self._ref_coordinates = self.ref_atoms.positions - self._ref_com
if self._groupselections_atoms:
self._groupselections_ref_coords64 = [
(
self.reference.select_atoms(*s["reference"]).positions.astype(
np.float64
)
)
for s in self.groupselections
]
finally:
# Move back to the original frame
self.reference.universe.trajectory[current_frame]
self._ref_coordinates64 = self._ref_coordinates.astype(np.float64)
if self._groupselections_atoms:
# Only carry out a rotation if we want to calculate secondary
# RMSDs.
# R: rotation matrix that aligns r-r_com, x~-x~com
# (x~: selected coordinates, x: all coordinates)
# Final transformed traj coordinates: x' = (x-x~_com)*R + ref_com
self._rot = np.zeros(9, dtype=np.float64) # allocate space
self._R = self._rot.reshape(3, 3)
else:
self._rot = None
self.rmsd = np.zeros((self.n_frames, 3 + len(self._groupselections_atoms)))
self._pm.format = (
"RMSD {rmsd:5.2f} A at frame {step:5d}/{numsteps} [{percentage:5.1f}%]"
)
self._mobile_coordinates64 = self.mobile_atoms.positions.copy().astype(np.float64)
|
https://github.com/MDAnalysis/mdanalysis/issues/2429
|
import MDAnalysis as mda
from MDAnalysis.tests.datafiles import PSF, DCD, CRD
import MDAnalysis.analysis.rms as rms
mda.__version__
'0.20.1'
u = mda.Universe(PSF, DCD) # closed AdK (PDB ID: 1AKE)
R = rms.RMSD(u, # universe to align
... u, # reference universe or atomgroup
... select='backbone', # group to superimpose and calculate RMSD
... groupselections=["all"], # groups for RMSD
... weights='mass') # weights for each atom
R.run()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "MDAnalysis/analysis/base.py", line 197, in run
self._single_frame()
File "MDAnalysis/analysis/rms.py", line 638, in _single_frame
center=False, superposition=False)
File "MDAnalysis/analysis/rms.py", line 247, in rmsd
raise ValueError('weights must have same length as a and b')
ValueError: weights must have same length as a and b
|
ValueError
|
def _single_frame(self):
mobile_com = self.mobile_atoms.center(self.weights_select).astype(np.float64)
self._mobile_coordinates64[:] = self.mobile_atoms.positions
self._mobile_coordinates64 -= mobile_com
self.rmsd[self._frame_index, :2] = self._ts.frame, self._trajectory.time
if self._groupselections_atoms:
# superimpose structures: MDAnalysis qcprot needs Nx3 coordinate
# array with float64 datatype (float32 leads to errors up to 1e-3 in
# RMSD). Note that R is defined in such a way that it acts **to the
# left** so that we can easily use broadcasting and save one
# expensive numpy transposition.
self.rmsd[self._frame_index, 2] = qcp.CalcRMSDRotationalMatrix(
self._ref_coordinates64,
self._mobile_coordinates64,
self._n_atoms,
self._rot,
self.weights_select,
)
self._R[:, :] = self._rot.reshape(3, 3)
# Transform each atom in the trajectory (use inplace ops to
# avoid copying arrays) (Marginally (~3%) faster than
# "ts.positions[:] = (ts.positions - x_com) * R + ref_com".)
self._ts.positions[:] -= mobile_com
# R acts to the left & is broadcasted N times.
self._ts.positions[:] = np.dot(self._ts.positions, self._R)
self._ts.positions += self._ref_com
# 2) calculate secondary RMSDs (without any further
# superposition)
for igroup, (refpos, atoms) in enumerate(
zip(self._groupselections_ref_coords64, self._groupselections_atoms), 3
):
self.rmsd[self._frame_index, igroup] = rmsd(
refpos,
atoms["mobile"].positions,
weights=self.weights_groupselections[igroup - 3],
center=False,
superposition=False,
)
else:
# only calculate RMSD by setting the Rmatrix to None (no need
# to carry out the rotation as we already get the optimum RMSD)
self.rmsd[self._frame_index, 2] = qcp.CalcRMSDRotationalMatrix(
self._ref_coordinates64,
self._mobile_coordinates64,
self._n_atoms,
None,
self.weights_select,
)
self._pm.rmsd = self.rmsd[self._frame_index, 2]
|
def _single_frame(self):
mobile_com = self.mobile_atoms.center(self.weights).astype(np.float64)
self._mobile_coordinates64[:] = self.mobile_atoms.positions
self._mobile_coordinates64 -= mobile_com
self.rmsd[self._frame_index, :2] = self._ts.frame, self._trajectory.time
if self._groupselections_atoms:
# superimpose structures: MDAnalysis qcprot needs Nx3 coordinate
# array with float64 datatype (float32 leads to errors up to 1e-3 in
# RMSD). Note that R is defined in such a way that it acts **to the
# left** so that we can easily use broadcasting and save one
# expensive numpy transposition.
self.rmsd[self._frame_index, 2] = qcp.CalcRMSDRotationalMatrix(
self._ref_coordinates64,
self._mobile_coordinates64,
self._n_atoms,
self._rot,
self.weights,
)
self._R[:, :] = self._rot.reshape(3, 3)
# Transform each atom in the trajectory (use inplace ops to
# avoid copying arrays) (Marginally (~3%) faster than
# "ts.positions[:] = (ts.positions - x_com) * R + ref_com".)
self._ts.positions[:] -= mobile_com
# R acts to the left & is broadcasted N times.
self._ts.positions[:] = np.dot(self._ts.positions, self._R)
self._ts.positions += self._ref_com
# 2) calculate secondary RMSDs (without any further
# superposition)
for igroup, (refpos, atoms) in enumerate(
zip(self._groupselections_ref_coords64, self._groupselections_atoms), 3
):
self.rmsd[self._frame_index, igroup] = rmsd(
refpos,
atoms["mobile"].positions,
weights=self.weights,
center=False,
superposition=False,
)
else:
# only calculate RMSD by setting the Rmatrix to None (no need
# to carry out the rotation as we already get the optimum RMSD)
self.rmsd[self._frame_index, 2] = qcp.CalcRMSDRotationalMatrix(
self._ref_coordinates64,
self._mobile_coordinates64,
self._n_atoms,
None,
self.weights,
)
self._pm.rmsd = self.rmsd[self._frame_index, 2]
|
https://github.com/MDAnalysis/mdanalysis/issues/2429
|
import MDAnalysis as mda
from MDAnalysis.tests.datafiles import PSF, DCD, CRD
import MDAnalysis.analysis.rms as rms
mda.__version__
'0.20.1'
u = mda.Universe(PSF, DCD) # closed AdK (PDB ID: 1AKE)
R = rms.RMSD(u, # universe to align
... u, # reference universe or atomgroup
... select='backbone', # group to superimpose and calculate RMSD
... groupselections=["all"], # groups for RMSD
... weights='mass') # weights for each atom
R.run()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "MDAnalysis/analysis/base.py", line 197, in run
self._single_frame()
File "MDAnalysis/analysis/rms.py", line 638, in _single_frame
center=False, superposition=False)
File "MDAnalysis/analysis/rms.py", line 247, in rmsd
raise ValueError('weights must have same length as a and b')
ValueError: weights must have same length as a and b
|
ValueError
|
def _get_dh_pairs(self):
"""Finds donor-hydrogen pairs.
Returns
-------
donors, hydrogens: AtomGroup, AtomGroup
AtomGroups corresponding to all donors and all hydrogens. AtomGroups are ordered such that, if zipped, will
produce a list of donor-hydrogen pairs.
"""
# If donors_sel is not provided, use topology to find d-h pairs
if not self.donors_sel:
# We're using u._topology.bonds rather than u.bonds as it is a million times faster to access.
# This is because u.bonds also calculates properties of each bond (e.g bond length).
# See https://github.com/MDAnalysis/mdanalysis/issues/2396#issuecomment-596251787
if not (
hasattr(self.u._topology, "bonds")
and len(self.u._topology.bonds.values) != 0
):
raise NoDataError(
"Cannot assign donor-hydrogen pairs via topology as no bond information is present. "
"Please either: load a topology file with bond information; use the guess_bonds() "
"topology guesser; or set HydrogenBondAnalysis.donors_sel so that a distance cutoff "
"can be used."
)
hydrogens = self.u.select_atoms(self.hydrogens_sel)
donors = sum(h.bonded_atoms[0] for h in hydrogens)
# Otherwise, use d_h_cutoff as a cutoff distance
else:
hydrogens = self.u.select_atoms(self.hydrogens_sel)
donors = self.u.select_atoms(self.donors_sel)
donors_indices, hydrogen_indices = capped_distance(
donors.positions,
hydrogens.positions,
max_cutoff=self.d_h_cutoff,
box=self.u.dimensions,
return_distances=False,
).T
donors = donors[donors_indices]
hydrogens = hydrogens[hydrogen_indices]
return donors, hydrogens
|
def _get_dh_pairs(self):
"""Finds donor-hydrogen pairs.
Returns
-------
donors, hydrogens: AtomGroup, AtomGroup
AtomGroups corresponding to all donors and all hydrogens. AtomGroups are ordered such that, if zipped, will
produce a list of donor-hydrogen pairs.
"""
# If donors_sel is not provided, use topology to find d-h pairs
if not self.donors_sel:
if len(self.u.bonds) == 0:
raise Exception(
"Cannot assign donor-hydrogen pairs via topology as no bonded information is present. "
"Please either: load a topology file with bonded information; use the guess_bonds() "
"topology guesser; or set HydrogenBondAnalysis.donors_sel so that a distance cutoff "
"can be used."
)
hydrogens = self.u.select_atoms(self.hydrogens_sel)
donors = sum(h.bonded_atoms[0] for h in hydrogens)
# Otherwise, use d_h_cutoff as a cutoff distance
else:
hydrogens = self.u.select_atoms(self.hydrogens_sel)
donors = self.u.select_atoms(self.donors_sel)
donors_indices, hydrogen_indices = capped_distance(
donors.positions,
hydrogens.positions,
max_cutoff=self.d_h_cutoff,
box=self.u.dimensions,
return_distances=False,
).T
donors = donors[donors_indices]
hydrogens = hydrogens[hydrogen_indices]
return donors, hydrogens
|
https://github.com/MDAnalysis/mdanalysis/issues/2396
|
---------------------------------------------------------------------------
KeyError Traceback (most recent call last)
~/anaconda3/envs/py36/lib/python3.6/site-packages/MDAnalysis/core/universe.py in __getattr__(self, key)
509 try:
--> 510 segment = self._instant_selectors[key]
511 except KeyError:
KeyError: 'bonds'
During handling of the above exception, another exception occurred:
AttributeError Traceback (most recent call last)
<ipython-input-12-763dc8d4b193> in <module>
----> 1 gro.bonds
~/anaconda3/envs/py36/lib/python3.6/site-packages/MDAnalysis/core/universe.py in __getattr__(self, key)
510 segment = self._instant_selectors[key]
511 except KeyError:
--> 512 raise AttributeError('No attribute "{}".'.format(key))
513 else:
514 warnings.warn("Instant selector Universe.<segid> "
AttributeError: No attribute "bonds".
|
KeyError
|
def _single_frame(self):
box = self._ts.dimensions
# Update donor-hydrogen pairs if necessary
if self.update_selections:
self._donors, self._hydrogens = self._get_dh_pairs()
# find D and A within cutoff distance of one another
# min_cutoff = 1.0 as an atom cannot form a hydrogen bond with itself
d_a_indices, d_a_distances = capped_distance(
self._donors.positions,
self._acceptors.positions,
max_cutoff=self.d_a_cutoff,
min_cutoff=1.0,
box=box,
return_distances=True,
)
# Remove D-A pairs more than d_a_cutoff away from one another
tmp_donors = self._donors[d_a_indices.T[0]]
tmp_hydrogens = self._hydrogens[d_a_indices.T[0]]
tmp_acceptors = self._acceptors[d_a_indices.T[1]]
# Find D-H-A angles greater than d_h_a_angle_cutoff
d_h_a_angles = np.rad2deg(
calc_angles(
tmp_donors.positions,
tmp_hydrogens.positions,
tmp_acceptors.positions,
box=box,
)
)
hbond_indices = np.where(d_h_a_angles > self.d_h_a_angle)[0]
# Retrieve atoms, distances and angles of hydrogen bonds
hbond_donors = tmp_donors[hbond_indices]
hbond_hydrogens = tmp_hydrogens[hbond_indices]
hbond_acceptors = tmp_acceptors[hbond_indices]
hbond_distances = d_a_distances[hbond_indices]
hbond_angles = d_h_a_angles[hbond_indices]
# Store data on hydrogen bonds found at this frame
self.hbonds[0].extend(np.full_like(hbond_donors, self._ts.frame))
self.hbonds[1].extend(hbond_donors.indices)
self.hbonds[2].extend(hbond_hydrogens.indices)
self.hbonds[3].extend(hbond_acceptors.indices)
self.hbonds[4].extend(hbond_distances)
self.hbonds[5].extend(hbond_angles)
|
def _single_frame(self):
box = self._ts.dimensions
# Update donor-hydrogen pairs if necessary
if self.update_selections:
self._donors, self._hydrogens = self._get_dh_pairs()
# find D and A within cutoff distance of one another
# min_cutoff = 1.0 as an atom cannot form a hydrogen bond with itself
d_a_indices, d_a_distances = capped_distance(
self._donors.positions,
self._acceptors.positions,
max_cutoff=self.d_a_cutoff,
min_cutoff=1.0,
box=box,
return_distances=True,
)
# Remove D-A pairs more than d_a_cutoff away from one another
tmp_donors = self._donors[d_a_indices.T[0]]
tmp_hydrogens = self._hydrogens[d_a_indices.T[0]]
tmp_acceptors = self._acceptors[d_a_indices.T[1]]
# Find D-H-A angles greater than d_h_a_angle_cutoff
d_h_a_angles = np.rad2deg(
calc_angles(
tmp_donors.positions,
tmp_hydrogens.positions,
tmp_acceptors.positions,
box=box,
)
)
hbond_indices = np.where(d_h_a_angles > self.d_h_a_angle)[0]
# Retrieve atoms, distances and angles of hydrogen bonds
hbond_donors = tmp_donors[hbond_indices]
hbond_hydrogens = tmp_hydrogens[hbond_indices]
hbond_acceptors = tmp_acceptors[hbond_indices]
hbond_distances = d_a_distances[hbond_indices]
hbond_angles = d_h_a_angles[hbond_indices]
# Store data on hydrogen bonds found at this frame
self.hbonds[0].extend(np.full_like(hbond_donors, self._ts.frame))
self.hbonds[1].extend(hbond_donors.ids)
self.hbonds[2].extend(hbond_hydrogens.ids)
self.hbonds[3].extend(hbond_acceptors.ids)
self.hbonds[4].extend(hbond_distances)
self.hbonds[5].extend(hbond_angles)
|
https://github.com/MDAnalysis/mdanalysis/issues/2396
|
---------------------------------------------------------------------------
KeyError Traceback (most recent call last)
~/anaconda3/envs/py36/lib/python3.6/site-packages/MDAnalysis/core/universe.py in __getattr__(self, key)
509 try:
--> 510 segment = self._instant_selectors[key]
511 except KeyError:
KeyError: 'bonds'
During handling of the above exception, another exception occurred:
AttributeError Traceback (most recent call last)
<ipython-input-12-763dc8d4b193> in <module>
----> 1 gro.bonds
~/anaconda3/envs/py36/lib/python3.6/site-packages/MDAnalysis/core/universe.py in __getattr__(self, key)
510 segment = self._instant_selectors[key]
511 except KeyError:
--> 512 raise AttributeError('No attribute "{}".'.format(key))
513 else:
514 warnings.warn("Instant selector Universe.<segid> "
AttributeError: No attribute "bonds".
|
KeyError
|
def __init__(self, universe, selection1, selection2, t0, tf, dtmax):
self.universe = universe
self.selection1 = selection1
self.selection2 = selection2
self.t0 = t0
self.tf = tf - 1
self.dtmax = dtmax
self.timeseries = None
|
def __init__(self, universe, selection1, selection2, t0, tf, dtmax, nproc=1):
self.universe = universe
self.selection1 = selection1
self.selection2 = selection2
self.t0 = t0
self.tf = tf - 1
self.dtmax = dtmax
self.nproc = nproc
self.timeseries = None
|
https://github.com/MDAnalysis/mdanalysis/issues/2511
|
ts= 1
ts= 2
/biggin/b131/bioc1523/software/anaconda/python3.6/2019.7/envs/all/lib/python3.7/site-packages/MDAnalysis/analysis/base.py:116: DeprecationWarning: Setting the following kwargs should be done in the run() method: start, stop
DeprecationWarning)
/biggin/b131/bioc1523/software/anaconda/python3.6/2019.7/envs/all/lib/python3.7/site-packages/MDAnalysis/analysis/base.py:116: DeprecationWarning: Setting the following kwargs should be done in the run() method: start, stop
DeprecationWarning)
/biggin/b131/bioc1523/software/anaconda/python3.6/2019.7/envs/all/lib/python3.7/site-packages/MDAnalysis/analysis/hbonds/hbond_analysis.py:650: SelectionWarning: No acceptors found in selection 2. You might have to specify a custom 'acceptors' keyword. Selection will update so continuing with fingers crossed.
warnings.warn(errmsg, category=SelectionWarning)
/biggin/b131/bioc1523/software/anaconda/python3.6/2019.7/envs/all/lib/python3.7/site-packages/MDAnalysis/analysis/hbonds/hbond_analysis.py:650: SelectionWarning: No donors found in selection 2. You might have to specify a custom 'donors' keyword. Selection will update so continuing with fingers crossed.
warnings.warn(errmsg, category=SelectionWarning)
error
trying again
Process Process-1:
/biggin/b131/bioc1523/software/anaconda/python3.6/2019.7/envs/all/lib/python3.7/site-packages/MDAnalysis/analysis/hbonds/hbond_analysis.py:650: SelectionWarning: No acceptors found in selection 2. You might have to specify a custom 'acceptors' keyword. Selection will update so continuing with fingers crossed.
warnings.warn(errmsg, category=SelectionWarning)
/biggin/b131/bioc1523/software/anaconda/python3.6/2019.7/envs/all/lib/python3.7/site-packages/MDAnalysis/analysis/hbonds/hbond_analysis.py:650: SelectionWarning: No donors found in selection 2. You might have to specify a custom 'donors' keyword. Selection will update so continuing with fingers crossed.
warnings.warn(errmsg, category=SelectionWarning)
Process Process-2:
Traceback (most recent call last):
File "/biggin/b131/bioc1523/software/anaconda/python3.6/2019.7/envs/all/lib/python3.7/multiprocessing/process.py", line 297, in _bootstrap
self.run()
File "/biggin/b131/bioc1523/software/anaconda/python3.6/2019.7/envs/all/lib/python3.7/multiprocessing/process.py", line 99, in run
self._target(*self._args, **self._kwargs)
File "/biggin/b131/bioc1523/software/anaconda/python3.6/2019.7/envs/all/lib/python3.7/site-packages/MDAnalysis/analysis/waterdynamics.py", line 659, in _HBA
sys.stdout.flush()
NameError: name 'sys' is not defined
Traceback (most recent call last):
File "/biggin/b131/bioc1523/software/anaconda/python3.6/2019.7/envs/all/lib/python3.7/site-packages/MDAnalysis/analysis/waterdynamics.py", line 653, in _HBA
h.run(verbose=verbose)
File "/biggin/b131/bioc1523/software/anaconda/python3.6/2019.7/envs/all/lib/python3.7/site-packages/MDAnalysis/analysis/hbonds/hbond_analysis.py", line 936, in run
verbose=kwargs.get('verbose', False))
File "/biggin/b131/bioc1523/software/anaconda/python3.6/2019.7/envs/all/lib/python3.7/site-packages/MDAnalysis/lib/log.py", line 350, in __init__
assert numsteps > 0, "numsteps step must be >0"
AssertionError: numsteps step must be >0
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/biggin/b131/bioc1523/software/anaconda/python3.6/2019.7/envs/all/lib/python3.7/multiprocessing/process.py", line 297, in _bootstrap
self.run()
File "/biggin/b131/bioc1523/software/anaconda/python3.6/2019.7/envs/all/lib/python3.7/multiprocessing/process.py", line 99, in run
self._target(*self._args, **self._kwargs)
File "/biggin/b131/bioc1523/software/anaconda/python3.6/2019.7/envs/all/lib/python3.7/site-packages/MDAnalysis/analysis/waterdynamics.py", line 658, in _HBA
sys.stdout.flush()
NameError: name 'sys' is not defined
|
NameError
|
def run(self, **kwargs):
"""Analyze trajectory and produce timeseries"""
h_list = MDAnalysis.analysis.hbonds.HydrogenBondAnalysis(
self.universe, self.selection1, self.selection2, distance=3.5, angle=120.0
)
h_list.run(**kwargs)
self.timeseries = self._getGraphics(h_list.timeseries, self.t0, self.tf, self.dtmax)
|
def run(self, **kwargs):
"""Analyze trajectory and produce timeseries"""
h_list = []
i = 0
if self.nproc > 1:
while i < len(self.universe.trajectory):
jobs = []
k = i
for j in range(self.nproc):
# start
print("ts=", i + 1)
if i >= len(self.universe.trajectory):
break
conn_parent, conn_child = multiprocessing.Pipe(False)
while True:
try:
# new thread
jobs.append(
(
multiprocessing.Process(
target=self._HBA,
args=(
self.universe.trajectory[i],
conn_child,
self.universe,
self.selection1,
self.selection2,
),
),
conn_parent,
)
)
break
except:
print("error in jobs.append")
jobs[j][0].start()
i = i + 1
for j in range(self.nproc):
if k >= len(self.universe.trajectory):
break
rec01 = jobs[j][1]
received = rec01.recv()
h_list.append(received)
jobs[j][0].join()
k += 1
self.timeseries = self._getGraphics(h_list, 0, self.tf - 1, self.dtmax)
else:
h_list = MDAnalysis.analysis.hbonds.HydrogenBondAnalysis(
self.universe, self.selection1, self.selection2, distance=3.5, angle=120.0
)
h_list.run(**kwargs)
self.timeseries = self._getGraphics(
h_list.timeseries, self.t0, self.tf, self.dtmax
)
|
https://github.com/MDAnalysis/mdanalysis/issues/2511
|
ts= 1
ts= 2
/biggin/b131/bioc1523/software/anaconda/python3.6/2019.7/envs/all/lib/python3.7/site-packages/MDAnalysis/analysis/base.py:116: DeprecationWarning: Setting the following kwargs should be done in the run() method: start, stop
DeprecationWarning)
/biggin/b131/bioc1523/software/anaconda/python3.6/2019.7/envs/all/lib/python3.7/site-packages/MDAnalysis/analysis/base.py:116: DeprecationWarning: Setting the following kwargs should be done in the run() method: start, stop
DeprecationWarning)
/biggin/b131/bioc1523/software/anaconda/python3.6/2019.7/envs/all/lib/python3.7/site-packages/MDAnalysis/analysis/hbonds/hbond_analysis.py:650: SelectionWarning: No acceptors found in selection 2. You might have to specify a custom 'acceptors' keyword. Selection will update so continuing with fingers crossed.
warnings.warn(errmsg, category=SelectionWarning)
/biggin/b131/bioc1523/software/anaconda/python3.6/2019.7/envs/all/lib/python3.7/site-packages/MDAnalysis/analysis/hbonds/hbond_analysis.py:650: SelectionWarning: No donors found in selection 2. You might have to specify a custom 'donors' keyword. Selection will update so continuing with fingers crossed.
warnings.warn(errmsg, category=SelectionWarning)
error
trying again
Process Process-1:
/biggin/b131/bioc1523/software/anaconda/python3.6/2019.7/envs/all/lib/python3.7/site-packages/MDAnalysis/analysis/hbonds/hbond_analysis.py:650: SelectionWarning: No acceptors found in selection 2. You might have to specify a custom 'acceptors' keyword. Selection will update so continuing with fingers crossed.
warnings.warn(errmsg, category=SelectionWarning)
/biggin/b131/bioc1523/software/anaconda/python3.6/2019.7/envs/all/lib/python3.7/site-packages/MDAnalysis/analysis/hbonds/hbond_analysis.py:650: SelectionWarning: No donors found in selection 2. You might have to specify a custom 'donors' keyword. Selection will update so continuing with fingers crossed.
warnings.warn(errmsg, category=SelectionWarning)
Process Process-2:
Traceback (most recent call last):
File "/biggin/b131/bioc1523/software/anaconda/python3.6/2019.7/envs/all/lib/python3.7/multiprocessing/process.py", line 297, in _bootstrap
self.run()
File "/biggin/b131/bioc1523/software/anaconda/python3.6/2019.7/envs/all/lib/python3.7/multiprocessing/process.py", line 99, in run
self._target(*self._args, **self._kwargs)
File "/biggin/b131/bioc1523/software/anaconda/python3.6/2019.7/envs/all/lib/python3.7/site-packages/MDAnalysis/analysis/waterdynamics.py", line 659, in _HBA
sys.stdout.flush()
NameError: name 'sys' is not defined
Traceback (most recent call last):
File "/biggin/b131/bioc1523/software/anaconda/python3.6/2019.7/envs/all/lib/python3.7/site-packages/MDAnalysis/analysis/waterdynamics.py", line 653, in _HBA
h.run(verbose=verbose)
File "/biggin/b131/bioc1523/software/anaconda/python3.6/2019.7/envs/all/lib/python3.7/site-packages/MDAnalysis/analysis/hbonds/hbond_analysis.py", line 936, in run
verbose=kwargs.get('verbose', False))
File "/biggin/b131/bioc1523/software/anaconda/python3.6/2019.7/envs/all/lib/python3.7/site-packages/MDAnalysis/lib/log.py", line 350, in __init__
assert numsteps > 0, "numsteps step must be >0"
AssertionError: numsteps step must be >0
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/biggin/b131/bioc1523/software/anaconda/python3.6/2019.7/envs/all/lib/python3.7/multiprocessing/process.py", line 297, in _bootstrap
self.run()
File "/biggin/b131/bioc1523/software/anaconda/python3.6/2019.7/envs/all/lib/python3.7/multiprocessing/process.py", line 99, in run
self._target(*self._args, **self._kwargs)
File "/biggin/b131/bioc1523/software/anaconda/python3.6/2019.7/envs/all/lib/python3.7/site-packages/MDAnalysis/analysis/waterdynamics.py", line 658, in _HBA
sys.stdout.flush()
NameError: name 'sys' is not defined
|
NameError
|
def _determine_method(
reference, configuration, max_cutoff, min_cutoff=None, box=None, method=None
):
"""Guesses the fastest method for capped distance calculations based on the
size of the coordinate sets and the relative size of the target volume.
Parameters
----------
reference : numpy.ndarray
Reference coordinate array with shape ``(3,)`` or ``(n, 3)``.
configuration : numpy.ndarray
Configuration coordinate array with shape ``(3,)`` or ``(m, 3)``.
max_cutoff : float
Maximum cutoff distance between `reference` and `configuration`
coordinates.
min_cutoff : float, optional
Minimum cutoff distance between `reference` and `configuration`
coordinates.
box : numpy.ndarray
The unitcell dimensions of the system, which can be orthogonal or
triclinic and must be provided in the same format as returned by
:attr:`MDAnalysis.coordinates.base.Timestep.dimensions`:\n
``[lx, ly, lz, alpha, beta, gamma]``.
method : {'bruteforce', 'nsgrid', 'pkdtree'}, optional
Keyword to override the automatic guessing of the employed search
method.
Returns
-------
function : callable
The function implementing the guessed (or deliberatly chosen) method.
"""
methods = {
"bruteforce": _bruteforce_capped,
"pkdtree": _pkdtree_capped,
"nsgrid": _nsgrid_capped,
}
if method is not None:
return methods[method.lower()]
if len(reference) < 10 or len(configuration) < 10:
return methods["bruteforce"]
elif len(reference) * len(configuration) >= 1e8:
# CAUTION : for large datasets, shouldnt go into 'bruteforce'
# in any case. Arbitrary number, but can be characterized
return methods["nsgrid"]
else:
if box is None:
min_dim = np.array([reference.min(axis=0), configuration.min(axis=0)])
max_dim = np.array([reference.max(axis=0), configuration.max(axis=0)])
size = max_dim.max(axis=0) - min_dim.min(axis=0)
elif np.all(box[3:] == 90.0):
size = box[:3]
else:
tribox = triclinic_vectors(box)
size = tribox.max(axis=0) - tribox.min(axis=0)
if np.any(max_cutoff > 0.3 * size):
return methods["bruteforce"]
else:
return methods["nsgrid"]
|
def _determine_method(
reference, configuration, max_cutoff, min_cutoff=None, box=None, method=None
):
"""Guesses the fastest method for capped distance calculations based on the
size of the coordinate sets and the relative size of the target volume.
Parameters
----------
reference : numpy.ndarray
Reference coordinate array with shape ``(3,)`` or ``(n, 3)``.
configuration : numpy.ndarray
Configuration coordinate array with shape ``(3,)`` or ``(m, 3)``.
max_cutoff : float
Maximum cutoff distance between `reference` and `configuration`
coordinates.
min_cutoff : float, optional
Minimum cutoff distance between `reference` and `configuration`
coordinates.
box : numpy.ndarray, None (default None)
The unitcell dimensions of the system, which can be orthogonal or
triclinic and must be provided in the same format as returned by
:attr:`MDAnalysis.coordinates.base.Timestep.dimensions`:\n
``[lx, ly, lz, alpha, beta, gamma]``.
method : {'bruteforce', 'nsgrid', 'pkdtree', None} (default None)
Keyword to override the automatic guessing of the employed search
method.
Returns
-------
function : callable
The function implementing the guessed (or deliberatly chosen) method.
"""
methods = {
"bruteforce": _bruteforce_capped,
"pkdtree": _pkdtree_capped,
"nsgrid": _nsgrid_capped,
}
if method is not None:
return methods[method.lower()]
if len(reference) < 10 or len(configuration) < 10:
return methods["bruteforce"]
elif len(reference) * len(configuration) >= 1e8:
# CAUTION : for large datasets, shouldnt go into 'bruteforce'
# in any case. Arbitrary number, but can be characterized
return methods["nsgrid"]
else:
if box is None:
min_dim = np.array([reference.min(axis=0), configuration.min(axis=0)])
max_dim = np.array([reference.max(axis=0), configuration.max(axis=0)])
size = max_dim.max(axis=0) - min_dim.min(axis=0)
elif np.all(box[3:] == 90.0):
size = box[:3]
else:
tribox = triclinic_vectors(box)
size = tribox.max(axis=0) - tribox.min(axis=0)
if np.any(max_cutoff > 0.3 * size):
return methods["bruteforce"]
else:
return methods["nsgrid"]
|
https://github.com/MDAnalysis/mdanalysis/issues/2361
|
from MDAnalysis.tests.datafiles import *
pdbqt = mda.Universe(PDBQT_input)
pdb = mda.Universe(PDB)
pdb.atoms.icodes
array(['', '', '', ..., '', '', ''], dtype=object)
pdbqt.atoms.icodes
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/Users/lily/anaconda3/envs/mdanalysis/lib/python3.7/site-packages/MDAnalysis/core/groups.py", line 2278, in __getattr__
cls=self.__class__.__name__, attr=attr))
AttributeError: AtomGroup has no attribute icodes
|
AttributeError
|
def parse(self, **kwargs):
"""Parse PSF file into Topology
Returns
-------
MDAnalysis *Topology* object
"""
# Open and check psf validity
with openany(self.filename) as psffile:
header = next(psffile)
if not header.startswith("PSF"):
err = "{0} is not valid PSF file (header = {1})".format(
self.filename, header
)
logger.error(err)
raise ValueError(err)
header_flags = header[3:].split()
if "NAMD" in header_flags:
self._format = "NAMD" # NAMD/VMD
elif "EXT" in header_flags:
self._format = "EXTENDED" # CHARMM
else:
self._format = "STANDARD" # CHARMM
next(psffile)
title = next(psffile).split()
if not (title[1] == "!NTITLE"):
err = "{0} is not a valid PSF file".format(self.filename)
logger.error(err)
raise ValueError(err)
# psfremarks = [psffile.next() for i in range(int(title[0]))]
for _ in range(int(title[0])):
next(psffile)
logger.debug("PSF file {0}: format {1}".format(self.filename, self._format))
# Atoms first and mandatory
top = self._parse_sec(psffile, ("NATOM", 1, 1, self._parseatoms))
# Then possibly other sections
sections = (
# ("atoms", ("NATOM", 1, 1, self._parseatoms)),
(Bonds, ("NBOND", 2, 4, self._parsesection)),
(Angles, ("NTHETA", 3, 3, self._parsesection)),
(Dihedrals, ("NPHI", 4, 2, self._parsesection)),
(Impropers, ("NIMPHI", 4, 2, self._parsesection)),
# ("donors", ("NDON", 2, 4, self._parsesection)),
# ("acceptors", ("NACC", 2, 4, self._parsesection))
)
try:
for attr, info in sections:
next(psffile)
top.add_TopologyAttr(attr(self._parse_sec(psffile, info)))
except StopIteration:
# Reached the end of the file before we expected
pass
return top
|
def parse(self, **kwargs):
"""Parse PSF file into Topology
Returns
-------
MDAnalysis *Topology* object
"""
# Open and check psf validity
with openany(self.filename) as psffile:
header = next(psffile)
if not header.startswith("PSF"):
err = "{0} is not valid PSF file (header = {1})".format(
self.filename, header
)
logger.error(err)
raise ValueError(err)
header_flags = header[3:].split()
if "NAMD" in header_flags:
self._format = "NAMD" # NAMD/VMD
elif "EXT" in header_flags:
self._format = "EXTENDED" # CHARMM
else:
self._format = "STANDARD" # CHARMM
next(psffile)
title = next(psffile).split()
if not (title[1] == "!NTITLE"):
err = "{0} is not a valid PSF file".format(psffile.name)
logger.error(err)
raise ValueError(err)
# psfremarks = [psffile.next() for i in range(int(title[0]))]
for _ in range(int(title[0])):
next(psffile)
logger.debug("PSF file {0}: format {1}".format(psffile.name, self._format))
# Atoms first and mandatory
top = self._parse_sec(psffile, ("NATOM", 1, 1, self._parseatoms))
# Then possibly other sections
sections = (
# ("atoms", ("NATOM", 1, 1, self._parseatoms)),
(Bonds, ("NBOND", 2, 4, self._parsesection)),
(Angles, ("NTHETA", 3, 3, self._parsesection)),
(Dihedrals, ("NPHI", 4, 2, self._parsesection)),
(Impropers, ("NIMPHI", 4, 2, self._parsesection)),
# ("donors", ("NDON", 2, 4, self._parsesection)),
# ("acceptors", ("NACC", 2, 4, self._parsesection))
)
try:
for attr, info in sections:
next(psffile)
top.add_TopologyAttr(attr(self._parse_sec(psffile, info)))
except StopIteration:
# Reached the end of the file before we expected
pass
return top
|
https://github.com/MDAnalysis/mdanalysis/issues/2232
|
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-6-90d681257701> in <module>
----> 1 u = mda.Universe(RNA_PSF)
/Volumes/Data/oliver/Biop/Projects/Methods/MDAnalysis/mdanalysis/package/MDAnalysis/core/universe.py in __init__(self, *args, **kwargs)
290 try:
291 with parser(self.filename) as p:
--> 292 self._topology = p.parse(**kwargs)
293 except (IOError, OSError) as err:
294 # There are 2 kinds of errors that might be raised here:
/Volumes/Data/oliver/Biop/Projects/Methods/MDAnalysis/mdanalysis/package/MDAnalysis/topology/PSFParser.py in parse(self, **kwargs)
130 next(psffile)
131 logger.debug("PSF file {0}: format {1}"
--> 132 "".format(psffile.name, self._format))
133
134 # Atoms first and mandatory
AttributeError: 'BZ2File' object has no attribute 'name'
|
AttributeError
|
def select_atoms(self, sel, *othersel, **selgroups):
"""Select :class:`Atoms<Atom>` using a selection string.
Returns an :class:`AtomGroup` with :class:`Atoms<Atom>` sorted according
to their index in the topology (this is to ensure that there are no
duplicates, which can happen with complicated selections).
Raises
------
TypeError
If the arbitrary groups passed are not of type
:class:`MDAnalysis.core.groups.AtomGroup`
Examples
--------
All simple selection listed below support multiple arguments which are
implicitly combined with an or operator. For example
>>> sel = universe.select_atoms('resname MET GLY')
is equivalent to
>>> sel = universe.select_atoms('resname MET or resname GLY')
Will select all atoms with a residue name of either MET or GLY.
Subselections can be grouped with parentheses.
>>> sel = universe.select_atoms("segid DMPC and not ( name H* O* )")
>>> sel
<AtomGroup with 3420 atoms>
Existing :class:`AtomGroup` objects can be passed as named arguments,
which will then be available to the selection parser.
>>> universe.select_atoms("around 10 group notHO", notHO=sel)
<AtomGroup with 1250 atoms>
Selections can be set to update automatically on frame change, by
setting the `updating` keyword argument to `True`. This will return
a :class:`UpdatingAtomGroup` which can represent the solvation shell
around another object.
>>> universe.select_atoms("resname SOL and around 2.0 protein", updating=True)
<Updating AtomGroup with 100 atoms>
Notes
-----
If exact ordering of atoms is required (for instance, for
:meth:`~AtomGroup.angle` or :meth:`~AtomGroup.dihedral` calculations)
then one supplies selections *separately* in the required order. Also,
when multiple :class:`AtomGroup` instances are concatenated with the
``+`` operator, then the order of :class:`Atom` instances is preserved
and duplicates are *not* removed.
See Also
--------
:ref:`selection-commands-label` for further details and examples.
.. rubric:: Selection syntax
The selection parser understands the following CASE SENSITIVE
*keywords*:
**Simple selections**
protein, backbone, nucleic, nucleicbackbone
selects all atoms that belong to a standard set of residues;
a protein is identfied by a hard-coded set of residue names so
it may not work for esoteric residues.
segid *seg-name*
select by segid (as given in the topology), e.g. ``segid 4AKE``
or ``segid DMPC``
resid *residue-number-range*
resid can take a single residue number or a range of numbers. A
range consists of two numbers separated by a colon (inclusive)
such as ``resid 1:5``. A residue number ("resid") is taken
directly from the topology.
If icodes are present in the topology, then these will be
taken into account. Ie 'resid 163B' will only select resid
163 with icode B while 'resid 163' will select only residue 163.
Range selections will also respect icodes, so 'resid 162-163B'
will select all residues in 162 and those in 163 up to icode B.
resnum *resnum-number-range*
resnum is the canonical residue number; typically it is set to
the residue id in the original PDB structure.
resname *residue-name*
select by residue name, e.g. ``resname LYS``
name *atom-name*
select by atom name (as given in the topology). Often, this is
force field dependent. Example: ``name CA`` (for Cα atoms)
or ``name OW`` (for SPC water oxygen)
type *atom-type*
select by atom type; this is either a string or a number and
depends on the force field; it is read from the topology file
(e.g. the CHARMM PSF file contains numeric atom types). It has
non-sensical values when a PDB or GRO file is used as a topology
atom *seg-name* *residue-number* *atom-name*
a selector for a single atom consisting of segid resid atomname,
e.g. ``DMPC 1 C2`` selects the C2 carbon of the first residue of
the DMPC segment
altloc *alternative-location*
a selection for atoms where alternative locations are available,
which is often the case with high-resolution crystal structures
e.g. `resid 4 and resname ALA and altloc B` selects only the
atoms of ALA-4 that have an altloc B record.
moltype *molecule-type*
select by molecule type, e.g. ``moltype Protein_A``. At the
moment, only the TPR format defines the molecule type.
record_type *record_type*
for selecting either ATOM or HETATM from PDB-like files.
e.g. ``select_atoms('name CA and not record_type HETATM')``
**Boolean**
not
all atoms not in the selection, e.g. ``not protein`` selects
all atoms that aren't part of a protein
and, or
combine two selections according to the rules of boolean
algebra, e.g. ``protein and not resname ALA LYS``
selects all atoms that belong to a protein, but are not in a
lysine or alanine residue
**Geometric**
around *distance* *selection*
selects all atoms a certain cutoff away from another selection,
e.g. ``around 3.5 protein`` selects all atoms not belonging to
protein that are within 3.5 Angstroms from the protein
point *x* *y* *z* *distance*
selects all atoms within a cutoff of a point in space, make sure
coordinate is separated by spaces,
e.g. ``point 5.0 5.0 5.0 3.5`` selects all atoms within 3.5
Angstroms of the coordinate (5.0, 5.0, 5.0)
prop [abs] *property* *operator* *value*
selects atoms based on position, using *property* **x**, **y**,
or **z** coordinate. Supports the **abs** keyword (for absolute
value) and the following *operators*: **<, >, <=, >=, ==, !=**.
For example, ``prop z >= 5.0`` selects all atoms with z
coordinate greater than 5.0; ``prop abs z <= 5.0`` selects all
atoms within -5.0 <= z <= 5.0.
sphzone *radius* *selection*
Selects all atoms that are within *radius* of the center of
geometry of *selection*
sphlayer *inner radius* *outer radius* *selection*
Similar to sphzone, but also excludes atoms that are within
*inner radius* of the selection COG
cyzone *externalRadius* *zMax* *zMin* *selection*
selects all atoms within a cylindric zone centered in the
center of geometry (COG) of a given selection,
e.g. ``cyzone 15 4 -8 protein and resid 42`` selects the
center of geometry of protein and resid 42, and creates a
cylinder of external radius 15 centered on the COG. In z, the
cylinder extends from 4 above the COG to 8 below. Positive
values for *zMin*, or negative ones for *zMax*, are allowed.
cylayer *innerRadius* *externalRadius* *zMax* *zMin* *selection*
selects all atoms within a cylindric layer centered in the
center of geometry (COG) of a given selection,
e.g. ``cylayer 5 10 10 -8 protein`` selects the center of
geometry of protein, and creates a cylindrical layer of inner
radius 5, external radius 10 centered on the COG. In z, the
cylinder extends from 10 above the COG to 8 below. Positive
values for *zMin*, or negative ones for *zMax*, are allowed.
**Connectivity**
byres *selection*
selects all atoms that are in the same segment and residue as
selection, e.g. specify the subselection after the byres keyword
bonded *selection*
selects all atoms that are bonded to selection
eg: ``select name H and bonded name O`` selects only hydrogens
bonded to oxygens
**Index**
bynum *index-range*
selects all atoms within a range of (1-based) inclusive indices,
e.g. ``bynum 1`` selects the first atom in the universe;
``bynum 5:10`` selects atoms 5 through 10 inclusive. All atoms
in the :class:`~MDAnalysis.core.universe.Universe` are
consecutively numbered, and the index runs from 1 up to the
total number of atoms.
**Preexisting selections**
group `group-name`
selects the atoms in the :class:`AtomGroup` passed to the
function as an argument named `group-name`. Only the atoms
common to `group-name` and the instance
:meth:`~MDAnalysis.core.groups.AtomGroup.select_atoms`
was called from will be considered, unless ``group`` is
preceded by the ``global`` keyword. `group-name` will be
included in the parsing just by comparison of atom indices.
This means that it is up to the user to make sure the
`group-name` group was defined in an appropriate
:class:`~MDAnalysis.core.universe.Universe`.
global *selection*
by default, when issuing
:meth:`~MDAnalysis.core.groups.AtomGroup.select_atoms` from an
:class:`~MDAnalysis.core.groups.AtomGroup`, selections and
subselections are returned intersected with the atoms of that
instance. Prefixing a selection term with ``global`` causes its
selection to be returned in its entirety. As an example, the
``global`` keyword allows for
``lipids.select_atoms("around 10 global protein")`` --- where
``lipids`` is a group that does not contain any proteins. Were
``global`` absent, the result would be an empty selection since
the ``protein`` subselection would itself be empty. When issuing
:meth:`~MDAnalysis.core.groups.AtomGroup.select_atoms` from a
:class:`~MDAnalysis.core.universe.Universe`, ``global`` is
ignored.
**Dynamic selections**
If :meth:`~MDAnalysis.core.groups.AtomGroup.select_atoms` is
invoked with named argument `updating` set to `True`, an
:class:`~MDAnalysis.core.groups.UpdatingAtomGroup` instance will be
returned, instead of a regular
:class:`~MDAnalysis.core.groups.AtomGroup`. It behaves just like
the latter, with the difference that the selection expressions are
re-evaluated every time the trajectory frame changes (this happens
lazily, only when the
:class:`~MDAnalysis.core.groups.UpdatingAtomGroup` is accessed so
that there is no redundant updating going on).
Issuing an updating selection from an already updating group will
cause later updates to also reflect the updating of the base group.
A non-updating selection or a slicing operation made on an
:class:`~MDAnalysis.core.groups.UpdatingAtomGroup` will return a
static :class:`~MDAnalysis.core.groups.AtomGroup`, which will no
longer update across frames.
.. versionchanged:: 0.7.4 Added *resnum* selection.
.. versionchanged:: 0.8.1 Added *group* and *fullgroup* selections.
.. deprecated:: 0.11 The use of *fullgroup* has been deprecated in favor
of the equivalent *global group* selections.
.. versionchanged:: 0.13.0 Added *bonded* selection.
.. versionchanged:: 0.16.0 Resid selection now takes icodes into account
where present.
.. versionchanged:: 0.16.0 Updating selections now possible by setting
the `updating` argument.
.. versionchanged:: 0.17.0 Added *moltype* and *molnum* selections.
.. versionchanged:: 0.19.0
Added strict type checking for passed groups.
Added periodic kwarg (default True)
.. versionchanged:: 0.19.2
Empty sel string now returns an empty Atom group.
"""
if not sel:
warnings.warn(
"Empty string to select atoms, empty group returned.", UserWarning
)
return self[[]]
# once flags removed, replace with default=True
periodic = selgroups.pop("periodic", flags["use_periodic_selections"])
updating = selgroups.pop("updating", False)
sel_strs = (sel,) + othersel
for group, thing in selgroups.items():
if not isinstance(thing, AtomGroup):
raise TypeError(
"Passed groups must be AtomGroups. "
"You provided {} for group '{}'".format(thing.__class__.__name__, group)
)
selections = tuple(
(selection.Parser.parse(s, selgroups, periodic=periodic) for s in sel_strs)
)
if updating:
atomgrp = UpdatingAtomGroup(self, selections, sel_strs)
else:
# Apply the first selection and sum to it
atomgrp = sum(
[sel.apply(self) for sel in selections[1:]], selections[0].apply(self)
)
return atomgrp
|
def select_atoms(self, sel, *othersel, **selgroups):
"""Select :class:`Atoms<Atom>` using a selection string.
Returns an :class:`AtomGroup` with :class:`Atoms<Atom>` sorted according
to their index in the topology (this is to ensure that there are no
duplicates, which can happen with complicated selections).
Raises
------
TypeError
If the arbitrary groups passed are not of type
:class:`MDAnalysis.core.groups.AtomGroup`
Examples
--------
All simple selection listed below support multiple arguments which are
implicitly combined with an or operator. For example
>>> sel = universe.select_atoms('resname MET GLY')
is equivalent to
>>> sel = universe.select_atoms('resname MET or resname GLY')
Will select all atoms with a residue name of either MET or GLY.
Subselections can be grouped with parentheses.
>>> sel = universe.select_atoms("segid DMPC and not ( name H* O* )")
>>> sel
<AtomGroup with 3420 atoms>
Existing :class:`AtomGroup` objects can be passed as named arguments,
which will then be available to the selection parser.
>>> universe.select_atoms("around 10 group notHO", notHO=sel)
<AtomGroup with 1250 atoms>
Selections can be set to update automatically on frame change, by
setting the `updating` keyword argument to `True`. This will return
a :class:`UpdatingAtomGroup` which can represent the solvation shell
around another object.
>>> universe.select_atoms("resname SOL and around 2.0 protein", updating=True)
<Updating AtomGroup with 100 atoms>
Notes
-----
If exact ordering of atoms is required (for instance, for
:meth:`~AtomGroup.angle` or :meth:`~AtomGroup.dihedral` calculations)
then one supplies selections *separately* in the required order. Also,
when multiple :class:`AtomGroup` instances are concatenated with the
``+`` operator, then the order of :class:`Atom` instances is preserved
and duplicates are *not* removed.
See Also
--------
:ref:`selection-commands-label` for further details and examples.
.. rubric:: Selection syntax
The selection parser understands the following CASE SENSITIVE
*keywords*:
**Simple selections**
protein, backbone, nucleic, nucleicbackbone
selects all atoms that belong to a standard set of residues;
a protein is identfied by a hard-coded set of residue names so
it may not work for esoteric residues.
segid *seg-name*
select by segid (as given in the topology), e.g. ``segid 4AKE``
or ``segid DMPC``
resid *residue-number-range*
resid can take a single residue number or a range of numbers. A
range consists of two numbers separated by a colon (inclusive)
such as ``resid 1:5``. A residue number ("resid") is taken
directly from the topology.
If icodes are present in the topology, then these will be
taken into account. Ie 'resid 163B' will only select resid
163 with icode B while 'resid 163' will select only residue 163.
Range selections will also respect icodes, so 'resid 162-163B'
will select all residues in 162 and those in 163 up to icode B.
resnum *resnum-number-range*
resnum is the canonical residue number; typically it is set to
the residue id in the original PDB structure.
resname *residue-name*
select by residue name, e.g. ``resname LYS``
name *atom-name*
select by atom name (as given in the topology). Often, this is
force field dependent. Example: ``name CA`` (for Cα atoms)
or ``name OW`` (for SPC water oxygen)
type *atom-type*
select by atom type; this is either a string or a number and
depends on the force field; it is read from the topology file
(e.g. the CHARMM PSF file contains numeric atom types). It has
non-sensical values when a PDB or GRO file is used as a topology
atom *seg-name* *residue-number* *atom-name*
a selector for a single atom consisting of segid resid atomname,
e.g. ``DMPC 1 C2`` selects the C2 carbon of the first residue of
the DMPC segment
altloc *alternative-location*
a selection for atoms where alternative locations are available,
which is often the case with high-resolution crystal structures
e.g. `resid 4 and resname ALA and altloc B` selects only the
atoms of ALA-4 that have an altloc B record.
moltype *molecule-type*
select by molecule type, e.g. ``moltype Protein_A``. At the
moment, only the TPR format defines the molecule type.
**Boolean**
not
all atoms not in the selection, e.g. ``not protein`` selects
all atoms that aren't part of a protein
and, or
combine two selections according to the rules of boolean
algebra, e.g. ``protein and not resname ALA LYS``
selects all atoms that belong to a protein, but are not in a
lysine or alanine residue
**Geometric**
around *distance* *selection*
selects all atoms a certain cutoff away from another selection,
e.g. ``around 3.5 protein`` selects all atoms not belonging to
protein that are within 3.5 Angstroms from the protein
point *x* *y* *z* *distance*
selects all atoms within a cutoff of a point in space, make sure
coordinate is separated by spaces,
e.g. ``point 5.0 5.0 5.0 3.5`` selects all atoms within 3.5
Angstroms of the coordinate (5.0, 5.0, 5.0)
prop [abs] *property* *operator* *value*
selects atoms based on position, using *property* **x**, **y**,
or **z** coordinate. Supports the **abs** keyword (for absolute
value) and the following *operators*: **<, >, <=, >=, ==, !=**.
For example, ``prop z >= 5.0`` selects all atoms with z
coordinate greater than 5.0; ``prop abs z <= 5.0`` selects all
atoms within -5.0 <= z <= 5.0.
sphzone *radius* *selection*
Selects all atoms that are within *radius* of the center of
geometry of *selection*
sphlayer *inner radius* *outer radius* *selection*
Similar to sphzone, but also excludes atoms that are within
*inner radius* of the selection COG
cyzone *externalRadius* *zMax* *zMin* *selection*
selects all atoms within a cylindric zone centered in the
center of geometry (COG) of a given selection,
e.g. ``cyzone 15 4 -8 protein and resid 42`` selects the
center of geometry of protein and resid 42, and creates a
cylinder of external radius 15 centered on the COG. In z, the
cylinder extends from 4 above the COG to 8 below. Positive
values for *zMin*, or negative ones for *zMax*, are allowed.
cylayer *innerRadius* *externalRadius* *zMax* *zMin* *selection*
selects all atoms within a cylindric layer centered in the
center of geometry (COG) of a given selection,
e.g. ``cylayer 5 10 10 -8 protein`` selects the center of
geometry of protein, and creates a cylindrical layer of inner
radius 5, external radius 10 centered on the COG. In z, the
cylinder extends from 10 above the COG to 8 below. Positive
values for *zMin*, or negative ones for *zMax*, are allowed.
**Connectivity**
byres *selection*
selects all atoms that are in the same segment and residue as
selection, e.g. specify the subselection after the byres keyword
bonded *selection*
selects all atoms that are bonded to selection
eg: ``select name H and bonded name O`` selects only hydrogens
bonded to oxygens
**Index**
bynum *index-range*
selects all atoms within a range of (1-based) inclusive indices,
e.g. ``bynum 1`` selects the first atom in the universe;
``bynum 5:10`` selects atoms 5 through 10 inclusive. All atoms
in the :class:`~MDAnalysis.core.universe.Universe` are
consecutively numbered, and the index runs from 1 up to the
total number of atoms.
**Preexisting selections**
group `group-name`
selects the atoms in the :class:`AtomGroup` passed to the
function as an argument named `group-name`. Only the atoms
common to `group-name` and the instance
:meth:`~MDAnalysis.core.groups.AtomGroup.select_atoms`
was called from will be considered, unless ``group`` is
preceded by the ``global`` keyword. `group-name` will be
included in the parsing just by comparison of atom indices.
This means that it is up to the user to make sure the
`group-name` group was defined in an appropriate
:class:`~MDAnalysis.core.universe.Universe`.
global *selection*
by default, when issuing
:meth:`~MDAnalysis.core.groups.AtomGroup.select_atoms` from an
:class:`~MDAnalysis.core.groups.AtomGroup`, selections and
subselections are returned intersected with the atoms of that
instance. Prefixing a selection term with ``global`` causes its
selection to be returned in its entirety. As an example, the
``global`` keyword allows for
``lipids.select_atoms("around 10 global protein")`` --- where
``lipids`` is a group that does not contain any proteins. Were
``global`` absent, the result would be an empty selection since
the ``protein`` subselection would itself be empty. When issuing
:meth:`~MDAnalysis.core.groups.AtomGroup.select_atoms` from a
:class:`~MDAnalysis.core.universe.Universe`, ``global`` is
ignored.
**Dynamic selections**
If :meth:`~MDAnalysis.core.groups.AtomGroup.select_atoms` is
invoked with named argument `updating` set to `True`, an
:class:`~MDAnalysis.core.groups.UpdatingAtomGroup` instance will be
returned, instead of a regular
:class:`~MDAnalysis.core.groups.AtomGroup`. It behaves just like
the latter, with the difference that the selection expressions are
re-evaluated every time the trajectory frame changes (this happens
lazily, only when the
:class:`~MDAnalysis.core.groups.UpdatingAtomGroup` is accessed so
that there is no redundant updating going on).
Issuing an updating selection from an already updating group will
cause later updates to also reflect the updating of the base group.
A non-updating selection or a slicing operation made on an
:class:`~MDAnalysis.core.groups.UpdatingAtomGroup` will return a
static :class:`~MDAnalysis.core.groups.AtomGroup`, which will no
longer update across frames.
.. versionchanged:: 0.7.4 Added *resnum* selection.
.. versionchanged:: 0.8.1 Added *group* and *fullgroup* selections.
.. deprecated:: 0.11 The use of *fullgroup* has been deprecated in favor
of the equivalent *global group* selections.
.. versionchanged:: 0.13.0 Added *bonded* selection.
.. versionchanged:: 0.16.0 Resid selection now takes icodes into account
where present.
.. versionchanged:: 0.16.0 Updating selections now possible by setting
the `updating` argument.
.. versionchanged:: 0.17.0 Added *moltype* and *molnum* selections.
.. versionchanged:: 0.19.0
Added strict type checking for passed groups.
Added periodic kwarg (default True)
.. versionchanged:: 0.19.2
Empty sel string now returns an empty Atom group.
"""
if not sel:
warnings.warn(
"Empty string to select atoms, empty group returned.", UserWarning
)
return self[[]]
# once flags removed, replace with default=True
periodic = selgroups.pop("periodic", flags["use_periodic_selections"])
updating = selgroups.pop("updating", False)
sel_strs = (sel,) + othersel
for group, thing in selgroups.items():
if not isinstance(thing, AtomGroup):
raise TypeError(
"Passed groups must be AtomGroups. "
"You provided {} for group '{}'".format(thing.__class__.__name__, group)
)
selections = tuple(
(selection.Parser.parse(s, selgroups, periodic=periodic) for s in sel_strs)
)
if updating:
atomgrp = UpdatingAtomGroup(self, selections, sel_strs)
else:
# Apply the first selection and sum to it
atomgrp = sum(
[sel.apply(self) for sel in selections[1:]], selections[0].apply(self)
)
return atomgrp
|
https://github.com/MDAnalysis/mdanalysis/issues/1926
|
TypeError Traceback (most recent call last)
<ipython-input-5-ec0d294eeb2a> in <module>()
1 u = mda.Universe(PDB)
----> 2 ref = u.copy()
~/anaconda3/lib/python3.6/site-packages/MDAnalysis/core/universe.py in copy(self)
337 def copy(self):
338 """Return an independent copy of this Universe"""
--> 339 new = self.__class__(self._topology.copy())
340 new.trajectory = self.trajectory.copy()
341 return new
~/anaconda3/lib/python3.6/site-packages/MDAnalysis/core/universe.py in __init__(self, *args, **kwargs)
300
301 # generate and populate Universe version of each class
--> 302 self._generate_from_topology()
303
304 # Load coordinates
~/anaconda3/lib/python3.6/site-packages/MDAnalysis/core/universe.py in _generate_from_topology(self)
348 # Put Group level stuff from topology into class
349 for attr in self._topology.attrs:
--> 350 self._process_attr(attr)
351
352 # Generate atoms, residues and segments.
~/anaconda3/lib/python3.6/site-packages/MDAnalysis/core/universe.py in _process_attr(self, attr)
848 'segment': self._topology.n_segments}
849 logger.debug("_process_attr: Adding {0} to topology".format(attr))
--> 850 if (attr.per_object is not None and len(attr) != n_dict[attr.per_object]):
851 raise ValueError('Length of {attr} does not'
852 ' match number of {obj}s.\n'
~/anaconda3/lib/python3.6/site-packages/MDAnalysis/core/topologyattrs.py in __len__(self)
262 def __len__(self):
263 """Length of the TopologyAttr at its intrinsic level."""
--> 264 return len(self.values)
265
266 def __getitem__(self, group):
TypeError: len() of unsized object
|
TypeError
|
def atoms(self):
"""An :class:`AtomGroup` of :class:`Atoms<Atom>` present in this
:class:`ResidueGroup`.
The :class:`Atoms<Atom>` are ordered locally by :class:`Residue` in the
:class:`ResidueGroup`. Duplicates are *not* removed.
"""
# If indices is an empty list np.concatenate will fail (Issue #1999).
try:
ag = self.universe.atoms[np.concatenate(self.indices)]
except ValueError:
ag = self.universe.atoms[self.indices]
# If the ResidueGroup is known to be unique, this also holds for the
# atoms therein, since atoms can only belong to one residue at a time.
# On the contrary, if the ResidueGroup is not unique, this does not
# imply non-unique atoms, since residues might be empty.
try:
if self._cache["isunique"]:
ag._cache["isunique"] = True
ag._cache["unique"] = ag
except KeyError:
pass
return ag
|
def atoms(self):
"""An :class:`AtomGroup` of :class:`Atoms<Atom>` present in this
:class:`ResidueGroup`.
The :class:`Atoms<Atom>` are ordered locally by :class:`Residue` in the
:class:`ResidueGroup`. Duplicates are *not* removed.
"""
ag = self.universe.atoms[np.concatenate(self.indices)]
# If the ResidueGroup is known to be unique, this also holds for the
# atoms therein, since atoms can only belong to one residue at a time.
# On the contrary, if the ResidueGroup is not unique, this does not
# imply non-unique atoms, since residues might be empty.
try:
if self._cache["isunique"]:
ag._cache["isunique"] = True
ag._cache["unique"] = ag
except KeyError:
pass
return ag
|
https://github.com/MDAnalysis/mdanalysis/issues/1999
|
ValueError: need at least one array to concatenate
Traceback (most recent call last):
File "/usr/lib/python2.7/site-packages/IPython/core/interactiveshell.py", line 2878, in run_code
exec(code_obj, self.user_global_ns, self.user_ns)
File "<ipython-input-5-6c6c6b22c39a>", line 1, in <module>
sel.select_atoms('resname SOL').residues.atoms
File "/home/dresio/software/virtualenvs/charm-mda/lib/python2.7/site-packages/MDAnalysis/core/groups.py", line 2340, in atoms
return self.universe.atoms[np.concatenate(self.indices)]
ValueError: need at least one array to concatenate
|
ValueError
|
def atoms(self):
"""An :class:`AtomGroup` of :class:`Atoms<Atom>` present in this
:class:`SegmentGroup`.
The :class:`Atoms<Atom>` are ordered locally by :class:`Residue`, which
are further ordered by :class:`Segment` in the :class:`SegmentGroup`.
Duplicates are *not* removed.
"""
# If indices is an empty list np.concatenate will fail (Issue #1999).
try:
ag = self.universe.atoms[np.concatenate(self.indices)]
except ValueError:
ag = self.universe.atoms[self.indices]
# If the SegmentGroup is known to be unique, this also holds for the
# residues therein, and thus, also for the atoms in those residues.
# On the contrary, if the SegmentGroup is not unique, this does not
# imply non-unique atoms, since segments or residues might be empty.
try:
if self._cache["isunique"]:
ag._cache["isunique"] = True
ag._cache["unique"] = ag
except KeyError:
pass
return ag
|
def atoms(self):
"""An :class:`AtomGroup` of :class:`Atoms<Atom>` present in this
:class:`SegmentGroup`.
The :class:`Atoms<Atom>` are ordered locally by :class:`Residue`, which
are further ordered by :class:`Segment` in the :class:`SegmentGroup`.
Duplicates are *not* removed.
"""
ag = self.universe.atoms[np.concatenate(self.indices)]
# If the SegmentGroup is known to be unique, this also holds for the
# residues therein, and thus, also for the atoms in those residues.
# On the contrary, if the SegmentGroup is not unique, this does not
# imply non-unique atoms, since segments or residues might be empty.
try:
if self._cache["isunique"]:
ag._cache["isunique"] = True
ag._cache["unique"] = ag
except KeyError:
pass
return ag
|
https://github.com/MDAnalysis/mdanalysis/issues/1999
|
ValueError: need at least one array to concatenate
Traceback (most recent call last):
File "/usr/lib/python2.7/site-packages/IPython/core/interactiveshell.py", line 2878, in run_code
exec(code_obj, self.user_global_ns, self.user_ns)
File "<ipython-input-5-6c6c6b22c39a>", line 1, in <module>
sel.select_atoms('resname SOL').residues.atoms
File "/home/dresio/software/virtualenvs/charm-mda/lib/python2.7/site-packages/MDAnalysis/core/groups.py", line 2340, in atoms
return self.universe.atoms[np.concatenate(self.indices)]
ValueError: need at least one array to concatenate
|
ValueError
|
def check_slice_indices(self, start, stop, step):
"""Check frame indices are valid and clip to fit trajectory.
The usage follows standard Python conventions for :func:`range` but see
the warning below.
Parameters
----------
start : int or None
Starting frame index (inclusive). ``None`` corresponds to the default
of 0, i.e., the initial frame.
stop : int or None
Last frame index (exclusive). ``None`` corresponds to the default
of n_frames, i.e., it includes the last frame of the trajectory.
step : int or None
step size of the slice, ``None`` corresponds to the default of 1, i.e,
include every frame in the range `start`, `stop`.
Returns
-------
start, stop, step : tuple (int, int, int)
Integers representing the slice
Warning
-------
The returned values `start`, `stop` and `step` give the expected result
when passed in :func:`range` but gives unexpected behavior when passed
in a :class:`slice` when ``stop=None`` and ``step=-1``
This can be a problem for downstream processing of the output from this
method. For example, slicing of trajectories is implemented by passing
the values returned by :meth:`check_slice_indices` to :func:`range` ::
range(start, stop, step)
and using them as the indices to randomly seek to. On the other hand,
in :class:`MDAnalysis.analysis.base.AnalysisBase` the values returned
by :meth:`check_slice_indices` are used to splice the trajectory by
creating a :class:`slice` instance ::
slice(start, stop, step)
This creates a discrepancy because these two lines are not equivalent::
range(10, -1, -1) # [10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0]
range(10)[slice(10, -1, -1)] # []
"""
slice_dict = {"start": start, "stop": stop, "step": step}
for varname, var in slice_dict.items():
if isinstance(var, numbers.Integral):
slice_dict[varname] = int(var)
elif var is None:
pass
else:
raise TypeError("{0} is not an integer".format(varname))
start = slice_dict["start"]
stop = slice_dict["stop"]
step = slice_dict["step"]
if step == 0:
raise ValueError("Step size is zero")
nframes = len(self)
step = step or 1
if start is None:
start = 0 if step > 0 else nframes - 1
elif start < 0:
start += nframes
if start < 0:
start = 0
if step < 0 and start >= nframes:
start = nframes - 1
if stop is None:
stop = nframes if step > 0 else -1
elif stop < 0:
stop += nframes
if step > 0 and stop > nframes:
stop = nframes
return start, stop, step
|
def check_slice_indices(self, start, stop, step):
"""Check frame indices are valid and clip to fit trajectory.
The usage follows standard Python conventions for :func:`range` but see
the warning below.
Parameters
----------
start : int or None
Starting frame index (inclusive). ``None`` corresponds to the default
of 0, i.e., the initial frame.
stop : int or None
Last frame index (exclusive). ``None`` corresponds to the default
of n_frames, i.e., it includes the last frame of the trajectory.
step : int or None
step size of the slice, ``None`` corresponds to the default of 1, i.e,
include every frame in the range `start`, `stop`.
Returns
-------
start, stop, step : tuple (int, int, int)
Integers representing the slice
Warning
-------
The returned values `start`, `stop` and `step` give the expected result
when passed in :func:`range` but gives unexpected behavior when passed
in a :class:`slice` when ``stop=None`` and ``step=-1``
This can be a problem for downstream processing of the output from this
method. For example, slicing of trajectories is implemented by passing
the values returned by :meth:`check_slice_indices` to :func:`range` ::
range(start, stop, step)
and using them as the indices to randomly seek to. On the other hand,
in :class:`MDAnalysis.analysis.base.AnalysisBase` the values returned
by :meth:`check_slice_indices` are used to splice the trajectory by
creating a :class:`slice` instance ::
slice(start, stop, step)
This creates a discrepancy because these two lines are not equivalent::
range(10, -1, -1) # [10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0]
range(10)[slice(10, -1, -1)] # []
"""
slice_dict = {"start": start, "stop": stop, "step": step}
for varname, var in slice_dict.items():
if isinstance(var, numbers.Integral):
slice_dict[varname] = int(var)
elif var is None:
pass
else:
raise TypeError("{0} is not an integer".format(varname))
start = slice_dict["start"]
stop = slice_dict["stop"]
step = slice_dict["step"]
if step == 0:
raise ValueError("Step size is zero")
nframes = len(self)
step = step or 1
if start is None:
start = 0 if step > 0 else nframes - 1
elif start < 0:
start += nframes
if start < 0:
start = 0
if step < 0 and start > nframes:
start = nframes - 1
if stop is None:
stop = nframes if step > 0 else -1
elif stop < 0:
stop += nframes
if step > 0 and stop > nframes:
stop = nframes
return start, stop, step
|
https://github.com/MDAnalysis/mdanalysis/issues/1944
|
---------------------------------------------------------------------------
EOFError Traceback (most recent call last)
<ipython-input-6-20f70c5f75bf> in <module>()
1 print(list(range(10)[10:0:-1]))
----> 2 print([ts.frame for ts in u.trajectory[10:0:-1]])
<ipython-input-6-20f70c5f75bf> in <listcomp>(.0)
1 print(list(range(10)[10:0:-1]))
----> 2 print([ts.frame for ts in u.trajectory[10:0:-1]])
~/dev/mdanalysis/package/MDAnalysis/coordinates/base.py in _sliced_iter(self, start, stop, step)
1376 try:
1377 for i in range(start, stop, step):
-> 1378 yield self._read_frame_with_aux(i)
1379 self.rewind()
1380 except TypeError: # if _read_frame not implemented
~/dev/mdanalysis/package/MDAnalysis/coordinates/base.py in _read_frame_with_aux(self, frame)
1358 def _read_frame_with_aux(self, frame):
1359 """Move to *frame*, updating ts with trajectory and auxiliary data."""
-> 1360 ts = self._read_frame(frame)
1361 for aux in self.aux_list:
1362 ts = self._auxs[aux].update_ts(ts)
~/dev/mdanalysis/package/MDAnalysis/coordinates/XDR.py in _read_frame(self, i)
232 self._frame = i - 1
233 try:
--> 234 self._xdr.seek(i)
235 timestep = self._read_next_timestep()
236 except IOError:
~/dev/mdanalysis/package/MDAnalysis/lib/formats/libmdaxdr.pyx in MDAnalysis.lib.formats.libmdaxdr._XDRFile.seek()
EOFError: Trying to seek over max number of frames
|
EOFError
|
def _single_frame(self):
index = self._frame_index
mobile_com = self.mobile_atoms.center(self._weights)
mobile_coordinates = self.mobile_atoms.positions - mobile_com
mobile_atoms, self.rmsd[index] = _fit_to(
mobile_coordinates,
self._ref_coordinates,
self.mobile,
mobile_com,
self._ref_com,
self._weights,
)
# write whole aligned input trajectory system
self._writer.write(mobile_atoms)
|
def _single_frame(self):
index = self._ts.frame
mobile_com = self.mobile_atoms.center(self._weights)
mobile_coordinates = self.mobile_atoms.positions - mobile_com
mobile_atoms, self.rmsd[index] = _fit_to(
mobile_coordinates,
self._ref_coordinates,
self.mobile,
mobile_com,
self._ref_com,
self._weights,
)
# write whole aligned input trajectory system
self._writer.write(mobile_atoms)
|
https://github.com/MDAnalysis/mdanalysis/issues/1714
|
---------------------------------------------------------------------------
IndexError Traceback (most recent call last)
<ipython-input-5-95462d8a76b2> in <module>()
----> 1 align.AlignTraj(u, ref, select='all', step=5, filename='test.xtc').run()
~/anaconda3/lib/python3.6/site-packages/MDAnalysis/analysis/base.py in run(self)
179 self._ts = ts
180 # logger.info("--> Doing frame {} of {}".format(i+1, self.n_frames))
--> 181 self._single_frame()
182 self._pm.echo(self._frame_index)
183 logger.info("Finishing up")
~/anaconda3/lib/python3.6/site-packages/MDAnalysis/analysis/align.py in _single_frame(self)
668 self.mobile,
669 mobile_com,
--> 670 self._ref_com, self._weights)
671 # write whole aligned input trajectory system
672 self._writer.write(mobile_atoms)
IndexError: index 20 is out of bounds for axis 0 with size 20
|
IndexError
|
def __init__(
self,
atomgroup,
reference=None,
select="all",
groupselections=None,
filename="rmsd.dat",
weights=None,
tol_mass=0.1,
ref_frame=0,
**kwargs,
):
r"""
Parameters
----------
atomgroup : AtomGroup or Universe
Group of atoms for which the RMSD is calculated. If a trajectory is
associated with the atoms then the computation iterates over the
trajectory.
reference : AtomGroup or Universe (optional)
Group of reference atoms; if ``None`` then the current frame of
`atomgroup` is used.
select : str or dict or tuple (optional)
The selection to operate on; can be one of:
1. any valid selection string for
:meth:`~MDAnalysis.core.groups.AtomGroup.select_atoms` that
produces identical selections in `atomgroup` and `reference`; or
2. a dictionary ``{'mobile': sel1, 'reference': sel2}`` where *sel1*
and *sel2* are valid selection strings that are applied to
`atomgroup` and `reference` respectively (the
:func:`MDAnalysis.analysis.align.fasta2select` function returns such
a dictionary based on a ClustalW_ or STAMP_ sequence alignment); or
3. a tuple ``(sel1, sel2)``
When using 2. or 3. with *sel1* and *sel2* then these selection strings
are applied to `atomgroup` and `reference` respectively and should
generate *groups of equivalent atoms*. *sel1* and *sel2* can each also
be a *list of selection strings* to generate a
:class:`~MDAnalysis.core.groups.AtomGroup` with defined atom order as
described under :ref:`ordered-selections-label`).
groupselections : list (optional)
A list of selections as described for `select`. Each selection
describes additional RMSDs to be computed *after the
structures have been superimposed* according to `select`. No
additional fitting is performed.The output contains one
additional column for each selection.
.. Note:: Experimental feature. Only limited error checking
implemented.
start : int (optional)
starting frame, default None becomes 0.
stop : int (optional)
Frame index to stop analysis. Default: None becomes
n_frames. Iteration stops *before* this frame number,
which means that the trajectory would be read until the end.
step : int (optional)
step between frames, default ``None`` becomes 1.
filename : str (optional)
write RMSD into file with :meth:`RMSD.save`
weights : {"mass", ``None``} or array_like (optional)
choose weights. With ``"mass"`` uses masses as weights; with ``None``
weigh each atom equally. If a float array of the same length as
`atomgroup` is provided, use each element of the `array_like` as a
weight for the corresponding atom in `atomgroup`.
tol_mass : float (optional)
Reject match if the atomic masses for matched atoms differ by more
than `tol_mass`.
ref_frame : int (optional)
frame index to select frame from `reference`
Raises
------
SelectionError
If the selections from `atomgroup` and `reference` do not match.
TypeError
If `weights` is not of the appropriate type; see also
:func:`MDAnalysis.lib.util.get_weights`
ValueError
If `weights` are not compatible with `atomgroup` (not the same
length) or if it is not a 1D array (see
:func:`MDAnalysis.lib.util.get_weights`).
If `weights` are not compatible with `groupselections`: only equal
weights (``weights=None``) or mass-weighted (``weights="mass"``)
are supported for additional `groupselections`.
Notes
-----
The root mean square deviation of a group of :math:`N` atoms relative to a
reference structure as a function of time is calculated as
.. math::
\rho(t) = \sqrt{\frac{1}{N} \sum_{i=1}^N w_i \left(\mathbf{x}_i(t)
- \mathbf{x}_i^{\text{ref}}\right)^2}
The selected coordinates from `atomgroup` are optimally superimposed
(translation and rotation) on the `reference` coordinates at each time step
as to minimize the RMSD. Douglas Theobald's fast QCP algorithm
[Theobald2005]_ is used for the rotational superposition and to calculate
the RMSD (see :mod:`MDAnalysis.lib.qcprot` for implementation details).
The class runs various checks on the input to ensure that the two atom
groups can be compared. This includes a comparison of atom masses (i.e.,
only the positions of atoms of the same mass will be considered to be
correct for comparison). If masses should not be checked, just set
`tol_mass` to a large value such as 1000.
.. _ClustalW: http://www.clustal.org/
.. _STAMP: http://www.compbio.dundee.ac.uk/manuals/stamp.4.2/
.. versionadded:: 0.7.7
.. versionchanged:: 0.8
`groupselections` added
.. versionchanged:: 0.16.0
Flexible weighting scheme with new `weights` keyword.
.. deprecated:: 0.16.0
Instead of ``mass_weighted=True`` (removal in 0.17.0) use new
``weights='mass'``; refactored to fit with AnalysisBase API
.. versionchanged:: 0.17.0
removed deprecated `mass_weighted` keyword; `groupselections`
are *not* rotationally superimposed any more.
"""
super(RMSD, self).__init__(atomgroup.universe.trajectory, **kwargs)
self.universe = atomgroup.universe
self.reference = reference if reference is not None else self.universe
select = process_selection(select)
self.groupselections = (
[process_selection(s) for s in groupselections]
if groupselections is not None
else []
)
self.weights = weights
self.tol_mass = tol_mass
self.ref_frame = ref_frame
self.filename = filename
self.ref_atoms = self.reference.select_atoms(*select["reference"])
self.mobile_atoms = self.universe.select_atoms(*select["mobile"])
if len(self.ref_atoms) != len(self.mobile_atoms):
err = (
"Reference and trajectory atom selections do "
"not contain the same number of atoms: "
"N_ref={0:d}, N_traj={1:d}".format(
self.ref_atoms.n_atoms, self.mobile_atoms.n_atoms
)
)
logger.exception(err)
raise SelectionError(err)
logger.info("RMS calculation for {0:d} atoms.".format(len(self.ref_atoms)))
mass_mismatches = (
np.absolute((self.ref_atoms.masses - self.mobile_atoms.masses)) > self.tol_mass
)
if np.any(mass_mismatches):
# diagnostic output:
logger.error("Atoms: reference | mobile")
for ar, at in zip(self.ref_atoms, self.mobile_atoms):
if ar.name != at.name:
logger.error(
"{0!s:>4} {1:3d} {2!s:>3} {3!s:>3} {4:6.3f}"
"| {5!s:>4} {6:3d} {7!s:>3} {8!s:>3}"
"{9:6.3f}".format(
ar.segid,
ar.resid,
ar.resname,
ar.name,
ar.mass,
at.segid,
at.resid,
at.resname,
at.name,
at.mass,
)
)
errmsg = (
"Inconsistent selections, masses differ by more than"
"{0:f}; mis-matching atoms"
"are shown above.".format(self.tol_mass)
)
logger.error(errmsg)
raise SelectionError(errmsg)
del mass_mismatches
# TODO:
# - make a group comparison a class that contains the checks above
# - use this class for the *select* group and the additional
# *groupselections* groups each a dict with reference/mobile
self._groupselections_atoms = [
{
"reference": self.reference.select_atoms(*s["reference"]),
"mobile": self.universe.select_atoms(*s["mobile"]),
}
for s in self.groupselections
]
# sanity check
for igroup, (sel, atoms) in enumerate(
zip(self.groupselections, self._groupselections_atoms)
):
if len(atoms["mobile"]) != len(atoms["reference"]):
logger.exception("SelectionError: Group Selection")
raise SelectionError(
"Group selection {0}: {1} | {2}: Reference and trajectory "
"atom selections do not contain the same number of atoms: "
"N_ref={3}, N_traj={4}".format(
igroup,
sel["reference"],
sel["mobile"],
len(atoms["reference"]),
len(atoms["mobile"]),
)
)
# Explicitly check for "mass" because this option CAN
# be used with groupselection. (get_weights() returns the mass array
# for "mass")
if self.weights != "mass":
self.weights = get_weights(self.mobile_atoms, self.weights)
# cannot use arbitrary weight array (for superposition) with
# groupselections because arrays will not match
if len(self.groupselections) > 0 and self.weights not in ("mass", None):
raise ValueError(
"groupselections can only be combined with "
"weights=None or weights='mass', not a weight "
"array."
)
# initialized to note for testing the save function
self.rmsd = None
|
def __init__(
self,
atomgroup,
reference=None,
select="all",
groupselections=None,
filename="rmsd.dat",
weights=None,
tol_mass=0.1,
ref_frame=0,
**kwargs,
):
r"""
Parameters
----------
atomgroup : AtomGroup or Universe
Group of atoms for which the RMSD is calculated. If a trajectory is
associated with the atoms then the computation iterates over the
trajectory.
reference : AtomGroup or Universe (optional)
Group of reference atoms; if ``None`` then the current frame of
`atomgroup` is used.
select : str or dict or tuple (optional)
The selection to operate on; can be one of:
1. any valid selection string for
:meth:`~MDAnalysis.core.groups.AtomGroup.select_atoms` that
produces identical selections in `atomgroup` and `reference`; or
2. a dictionary ``{'mobile': sel1, 'reference': sel2}`` where *sel1*
and *sel2* are valid selection strings that are applied to
`atomgroup` and `reference` respectively (the
:func:`MDAnalysis.analysis.align.fasta2select` function returns such
a dictionary based on a ClustalW_ or STAMP_ sequence alignment); or
3. a tuple ``(sel1, sel2)``
When using 2. or 3. with *sel1* and *sel2* then these selection strings
are applied to `atomgroup` and `reference` respectively and should
generate *groups of equivalent atoms*. *sel1* and *sel2* can each also
be a *list of selection strings* to generate a
:class:`~MDAnalysis.core.groups.AtomGroup` with defined atom order as
described under :ref:`ordered-selections-label`).
groupselections : list (optional)
A list of selections as described for `select`. Each selection
describes additional RMSDs to be computed *after the
structures have been superimposed* according to `select`. No
additional fitting is performed.The output contains one
additional column for each selection.
.. Note:: Experimental feature. Only limited error checking
implemented.
start : int (optional)
starting frame, default None becomes 0.
stop : int (optional)
Frame index to stop analysis. Default: None becomes
n_frames. Iteration stops *before* this frame number,
which means that the trajectory would be read until the end.
step : int (optional)
step between frames, default ``None`` becomes 1.
filename : str (optional)
write RMSD into file with :meth:`RMSD.save`
weights : {"mass", ``None``} or array_like (optional)
choose weights. With ``"mass"`` uses masses as weights; with ``None``
weigh each atom equally. If a float array of the same length as
`atomgroup` is provided, use each element of the `array_like` as a
weight for the corresponding atom in `atomgroup`.
tol_mass : float (optional)
Reject match if the atomic masses for matched atoms differ by more
than `tol_mass`.
ref_frame : int (optional)
frame index to select frame from `reference`
Raises
------
SelectionError
If the selections from `atomgroup` and `reference` do not match.
TypeError
If `weights` is not of the appropriate type; see
:func:`MDAnalysis.lib.util.get_weights`
ValueError
If `weights` are not compatible with `groupselections`: only equal
weights (``weights=None``) or mass-weighted (``weights="mass"``)
is supported.
Notes
-----
The root mean square deviation of a group of :math:`N` atoms relative to a
reference structure as a function of time is calculated as
.. math::
\rho(t) = \sqrt{\frac{1}{N} \sum_{i=1}^N w_i \left(\mathbf{x}_i(t)
- \mathbf{x}_i^{\text{ref}}\right)^2}
The selected coordinates from `atomgroup` are optimally superimposed
(translation and rotation) on the `reference` coordinates at each time step
as to minimize the RMSD. Douglas Theobald's fast QCP algorithm
[Theobald2005]_ is used for the rotational superposition and to calculate
the RMSD (see :mod:`MDAnalysis.lib.qcprot` for implementation details).
The class runs various checks on the input to ensure that the two atom
groups can be compared. This includes a comparison of atom masses (i.e.,
only the positions of atoms of the same mass will be considered to be
correct for comparison). If masses should not be checked, just set
`tol_mass` to a large value such as 1000.
.. _ClustalW: http://www.clustal.org/
.. _STAMP: http://www.compbio.dundee.ac.uk/manuals/stamp.4.2/
.. versionadded:: 0.7.7
.. versionchanged:: 0.8
`groupselections` added
.. versionchanged:: 0.16.0
Flexible weighting scheme with new `weights` keyword.
.. deprecated:: 0.16.0
Instead of ``mass_weighted=True`` (removal in 0.17.0) use new
``weights='mass'``; refactored to fit with AnalysisBase API
.. versionchanged:: 0.17.0
removed deprecated `mass_weighted` keyword; `groupselections`
are *not* rotationally superimposed any more.
"""
super(RMSD, self).__init__(atomgroup.universe.trajectory, **kwargs)
self.universe = atomgroup.universe
self.reference = reference if reference is not None else self.universe
select = process_selection(select)
self.groupselections = (
[process_selection(s) for s in groupselections]
if groupselections is not None
else []
)
self.weights = weights
self.tol_mass = tol_mass
self.ref_frame = ref_frame
self.filename = filename
self.ref_atoms = self.reference.select_atoms(*select["reference"])
self.mobile_atoms = self.universe.select_atoms(*select["mobile"])
if len(self.ref_atoms) != len(self.mobile_atoms):
err = (
"Reference and trajectory atom selections do "
"not contain the same number of atoms: "
"N_ref={0:d}, N_traj={1:d}".format(
self.ref_atoms.n_atoms, self.mobile_atoms.n_atoms
)
)
logger.exception(err)
raise SelectionError(err)
logger.info("RMS calculation for {0:d} atoms.".format(len(self.ref_atoms)))
mass_mismatches = (
np.absolute((self.ref_atoms.masses - self.mobile_atoms.masses)) > self.tol_mass
)
if np.any(mass_mismatches):
# diagnostic output:
logger.error("Atoms: reference | mobile")
for ar, at in zip(self.ref_atoms, self.mobile_atoms):
if ar.name != at.name:
logger.error(
"{0!s:>4} {1:3d} {2!s:>3} {3!s:>3} {4:6.3f}"
"| {5!s:>4} {6:3d} {7!s:>3} {8!s:>3}"
"{9:6.3f}".format(
ar.segid,
ar.resid,
ar.resname,
ar.name,
ar.mass,
at.segid,
at.resid,
at.resname,
at.name,
at.mass,
)
)
errmsg = (
"Inconsistent selections, masses differ by more than"
"{0:f}; mis-matching atoms"
"are shown above.".format(self.tol_mass)
)
logger.error(errmsg)
raise SelectionError(errmsg)
del mass_mismatches
# TODO:
# - make a group comparison a class that contains the checks above
# - use this class for the *select* group and the additional
# *groupselections* groups each a dict with reference/mobile
self._groupselections_atoms = [
{
"reference": self.reference.select_atoms(*s["reference"]),
"mobile": self.universe.select_atoms(*s["mobile"]),
}
for s in self.groupselections
]
# sanity check
for igroup, (sel, atoms) in enumerate(
zip(self.groupselections, self._groupselections_atoms)
):
if len(atoms["mobile"]) != len(atoms["reference"]):
logger.exception("SelectionError: Group Selection")
raise SelectionError(
"Group selection {0}: {1} | {2}: Reference and trajectory "
"atom selections do not contain the same number of atoms: "
"N_ref={3}, N_traj={4}".format(
igroup,
sel["reference"],
sel["mobile"],
len(atoms["reference"]),
len(atoms["mobile"]),
)
)
# Explicitly check for "mass" because this option CAN
# be used with groupselection. (get_weights() returns the mass array
# for "mass")
if self.weights != "mass":
self.weights = get_weights(self.mobile_atoms, self.weights)
# cannot use arbitrary weight array (for superposition) with
# groupselections because arrays will not match
if len(self.groupselections) > 0 and self.weights not in ("mass", None):
raise ValueError(
"groupselections can only be combined with "
"weights=None or weights='mass', not a weight "
"array."
)
# initialized to note for testing the save function
self.rmsd = None
|
https://github.com/MDAnalysis/mdanalysis/issues/1487
|
R = MDAnalysis.analysis.rms.RMSD(atomgroup=u, reference=u, select='backbone', groupselections=['backbone and resid 1:10','backbone and resid 10:20'])
R.run()
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-5-e54575231350> in <module>()
1 R = MDAnalysis.analysis.rms.RMSD(atomgroup=u, reference=u, select='backbone', groupselections=['backbone and resid 1:10','backbone and resid 10:20'], FIX = False)
----> 2 R.run()
/home/user/anaconda3/envs/py27/lib/python2.7/site-packages/MDAnalysis/analysis/base.pyc in run(self)
179 self._ts = ts
180 # logger.info("--> Doing frame {} of {}".format(i+1, self.n_frames))
--> 181 self._single_frame()
182 self._pm.echo(self._frame_index)
183 logger.info("Finishing up")
/home/user/anaconda3/envs/py27/lib/python2.7/site-packages/MDAnalysis/analysis/rms.pyc in _single_frame(self)
542 self._ts.positions = (self._ts.positions * self._R)
543 else:
--> 544 self._ts.positions[:,:] = (self._mobile_coordinates64[:] * self._R)
545 self._ts.positions[:] += self._ref_com
546
ValueError: could not broadcast input array from shape (855,3) into shape (3341,3)
|
ValueError
|
def get_weights(atoms, weights):
"""Check that a `weights` argument is compatible with `atoms`.
Parameters
----------
atoms : AtomGroup or array_like
The atoms that the `weights` should be applied to. Typically this
is a :class:`AtomGroup` but because only the length is compared,
any sequence for which ``len(atoms)`` is defined is acceptable.
weights : {"mass", None} or array_like
All MDAnalysis functions or classes understand "mass" and will then
use ``atoms.masses``. ``None`` indicates equal weights for all atoms.
Using an ``array_like`` assigns a custom weight to each element of
`atoms`.
Returns
-------
weights : array_like or None
If "mass" was selected, ``atoms.masses`` is returned, otherwise the
value of `weights` (which can be ``None``).
Raises
------
TypeError
If `weights` is not one of the allowed values or if "mass" is
selected but ``atoms.masses`` is not available.
ValueError
If `weights` is not a 1D array with the same length as
`atoms`, then the exception is raised. :exc:`TypeError` is
also raised if ``atoms.masses`` is not defined.
"""
if weights == "mass":
try:
weights = atoms.masses
except AttributeError:
raise TypeError("weights='mass' selected but atoms.masses is missing")
if iterable(weights):
if len(np.asarray(weights).shape) != 1:
raise ValueError(
"weights must be a 1D array, not with shape {0}".format(
np.asarray(weights).shape
)
)
elif len(weights) != len(atoms):
raise ValueError(
"weights (length {0}) must be of same length as the atoms ({1})".format(
len(weights), len(atoms)
)
)
elif weights is not None:
raise TypeError(
"weights must be {'mass', None} or an iterable of the "
"same size as the atomgroup."
)
return weights
|
def get_weights(atoms, weights):
"""Check that a `weights` argument is compatible with `atoms`.
Parameters
----------
atoms : AtomGroup or array_like
The atoms that the `weights` should be applied to. Typically this
is a :class:`AtomGroup` but because only the length is compared,
any sequence for which ``len(atoms)`` is defined is acceptable.
weights : {"mass", None} or array_like
All MDAnalysis functions or classes understand "mass" and will then
use ``atoms.masses``. ``None`` indicates equal weights for all atoms.
Using an ``array_like`` assigns a custom weight to each element of
`atoms`.
Returns
-------
weights : array_like or None
If "mass" was selected, ``atoms.masses`` is returned, otherwise the
value of `weights` (which can be ``None``).
Raises
------
TypeError
If `weights` is not one of the allowed values or if it is not a 1D
array with the same length as `atoms`, then the exception is raised.
:exc:`TypeError` is also raised if ``atoms.masses`` is not defined.
"""
if weights == "mass":
try:
weights = atoms.masses
except AttributeError:
raise TypeError("weights='mass' selected but atoms.masses is missing")
if iterable(weights):
if len(weights) != len(atoms):
raise TypeError(
"weights (length {0}) must be of same length as the atoms ({1})".format(
len(weights), len(atoms)
)
)
elif len(np.asarray(weights).shape) != 1:
raise TypeError(
"weights must be a 1D array, not with shape {0}".format(
np.asarray(weights).shape
)
)
elif weights is not None:
raise TypeError(
"weights must be {'mass', None} or an iterable of the "
"same size as the atomgroup."
)
return weights
|
https://github.com/MDAnalysis/mdanalysis/issues/1487
|
R = MDAnalysis.analysis.rms.RMSD(atomgroup=u, reference=u, select='backbone', groupselections=['backbone and resid 1:10','backbone and resid 10:20'])
R.run()
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-5-e54575231350> in <module>()
1 R = MDAnalysis.analysis.rms.RMSD(atomgroup=u, reference=u, select='backbone', groupselections=['backbone and resid 1:10','backbone and resid 10:20'], FIX = False)
----> 2 R.run()
/home/user/anaconda3/envs/py27/lib/python2.7/site-packages/MDAnalysis/analysis/base.pyc in run(self)
179 self._ts = ts
180 # logger.info("--> Doing frame {} of {}".format(i+1, self.n_frames))
--> 181 self._single_frame()
182 self._pm.echo(self._frame_index)
183 logger.info("Finishing up")
/home/user/anaconda3/envs/py27/lib/python2.7/site-packages/MDAnalysis/analysis/rms.pyc in _single_frame(self)
542 self._ts.positions = (self._ts.positions * self._R)
543 else:
--> 544 self._ts.positions[:,:] = (self._mobile_coordinates64[:] * self._R)
545 self._ts.positions[:] += self._ref_com
546
ValueError: could not broadcast input array from shape (855,3) into shape (3341,3)
|
ValueError
|
def _single_frame(self):
mobile_com = self.mobile_atoms.center(self.weights).astype(np.float64)
self._mobile_coordinates64[:] = self.mobile_atoms.positions
self._mobile_coordinates64 -= mobile_com
self.rmsd[self._frame_index, :2] = self._ts.frame, self._trajectory.time
if self._groupselections_atoms:
# superimpose structures: MDAnalysis qcprot needs Nx3 coordinate
# array with float64 datatype (float32 leads to errors up to 1e-3 in
# RMSD). Note that R is defined in such a way that it acts **to the
# left** so that we can easily use broadcasting and save one
# expensive numpy transposition.
self.rmsd[self._frame_index, 2] = qcp.CalcRMSDRotationalMatrix(
self._ref_coordinates_64,
self._mobile_coordinates64,
self._n_atoms,
self._rot,
self.weights,
)
self._R[:, :] = self._rot.reshape(3, 3)
# Transform each atom in the trajectory (use inplace ops to
# avoid copying arrays) (Marginally (~3%) faster than
# "ts.positions[:] = (ts.positions - x_com) * R + ref_com".)
self._ts.positions[:] -= mobile_com
# R acts to the left & is broadcasted N times.
self._ts.positions[:, :] = self._ts.positions * self._R
self._ts.positions[:] += self._ref_com
# 2) calculate secondary RMSDs
for igroup, (refpos, atoms) in enumerate(
zip(self._groupselections_ref_coords_64, self._groupselections_atoms), 3
):
self.rmsd[self._frame_index, igroup] = qcp.CalcRMSDRotationalMatrix(
refpos,
atoms["mobile"].positions.astype(np.float64),
atoms["mobile"].n_atoms,
None,
self.weights,
)
else:
# only calculate RMSD by setting the Rmatrix to None (no need
# to carry out the rotation as we already get the optimum RMSD)
self.rmsd[self._frame_index, 2] = qcp.CalcRMSDRotationalMatrix(
self._ref_coordinates_64,
self._mobile_coordinates64,
self._n_atoms,
None,
self.weights,
)
self._pm.rmsd = self.rmsd[self._frame_index, 2]
|
def _single_frame(self):
mobile_com = self.mobile_atoms.center(self.weights).astype(np.float64)
self._mobile_coordinates64[:] = self.mobile_atoms.positions
self._mobile_coordinates64 -= mobile_com
self.rmsd[self._frame_index, :2] = self._ts.frame, self._trajectory.time
if self._groupselections_atoms:
# superimpose structures: MDAnalysis qcprot needs Nx3 coordinate
# array with float64 datatype (float32 leads to errors up to 1e-3 in
# RMSD). Note that R is defined in such a way that it acts **to the
# left** so that we can easily use broadcasting and save one
# expensive numpy transposition.
self.rmsd[self._frame_index, 2] = qcp.CalcRMSDRotationalMatrix(
self._ref_coordinates_64,
self._mobile_coordinates64,
self._n_atoms,
self._rot,
self.weights,
)
self._R[:, :] = self._rot.reshape(3, 3)
# Transform each atom in the trajectory (use inplace ops to
# avoid copying arrays) (Marginally (~3%) faster than
# "ts.positions[:] = (ts.positions - x_com) * R + ref_com".)
self._ts.positions[:] -= mobile_com
# R acts to the left & is broadcasted N times.
self._ts.positions[:, :] = self._mobile_coordinates64[:] * self._R
self._ts.positions[:] += self._ref_com
# 2) calculate secondary RMSDs
for igroup, (refpos, atoms) in enumerate(
zip(self._groupselections_ref_coords_64, self._groupselections_atoms), 3
):
self.rmsd[self._frame_index, igroup] = qcp.CalcRMSDRotationalMatrix(
refpos,
atoms["mobile"].positions.astype(np.float64),
atoms["mobile"].n_atoms,
None,
self.weights,
)
else:
# only calculate RMSD by setting the Rmatrix to None (no need
# to carry out the rotation as we already get the optimum RMSD)
self.rmsd[self._frame_index, 2] = qcp.CalcRMSDRotationalMatrix(
self._ref_coordinates_64,
self._mobile_coordinates64,
self._n_atoms,
None,
self.weights,
)
self._pm.rmsd = self.rmsd[self._frame_index, 2]
|
https://github.com/MDAnalysis/mdanalysis/issues/1487
|
R = MDAnalysis.analysis.rms.RMSD(atomgroup=u, reference=u, select='backbone', groupselections=['backbone and resid 1:10','backbone and resid 10:20'])
R.run()
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-5-e54575231350> in <module>()
1 R = MDAnalysis.analysis.rms.RMSD(atomgroup=u, reference=u, select='backbone', groupselections=['backbone and resid 1:10','backbone and resid 10:20'], FIX = False)
----> 2 R.run()
/home/user/anaconda3/envs/py27/lib/python2.7/site-packages/MDAnalysis/analysis/base.pyc in run(self)
179 self._ts = ts
180 # logger.info("--> Doing frame {} of {}".format(i+1, self.n_frames))
--> 181 self._single_frame()
182 self._pm.echo(self._frame_index)
183 logger.info("Finishing up")
/home/user/anaconda3/envs/py27/lib/python2.7/site-packages/MDAnalysis/analysis/rms.pyc in _single_frame(self)
542 self._ts.positions = (self._ts.positions * self._R)
543 else:
--> 544 self._ts.positions[:,:] = (self._mobile_coordinates64[:] * self._R)
545 self._ts.positions[:] += self._ref_com
546
ValueError: could not broadcast input array from shape (855,3) into shape (3341,3)
|
ValueError
|
def __init__(self, trzfilename, n_atoms=None, **kwargs):
"""Creates a TRZ Reader
Parameters
----------
trzfilename : str
name of input file
n_atoms : int
number of atoms in trajectory, must be taken from topology file!
convert_units : bool (optional)
converts units to MDAnalysis defaults
"""
super(TRZReader, self).__init__(trzfilename, **kwargs)
if n_atoms is None:
raise ValueError("TRZReader requires the n_atoms keyword")
self.trzfile = util.anyopen(self.filename, "rb")
self._cache = dict()
self._n_atoms = n_atoms
self._read_trz_header()
self.ts = Timestep(
self.n_atoms,
velocities=True,
forces=self.has_force,
reader=self,
**self._ts_kwargs,
)
# structured dtype of a single trajectory frame
readarg = str(n_atoms) + "<f4"
frame_contents = [
("p1", "<i4"),
("nframe", "<i4"),
("ntrj", "<i4"),
("natoms", "<i4"),
("treal", "<f8"),
("p2", "<2i4"),
("box", "<9f8"),
("p3", "<2i4"),
("pressure", "<f8"),
("ptensor", "<6f8"),
("p4", "<3i4"),
("etot", "<f8"),
("ptot", "<f8"),
("ek", "<f8"),
("T", "<f8"),
("p5", "<6i4"),
("rx", readarg),
("pad2", "<2i4"),
("ry", readarg),
("pad3", "<2i4"),
("rz", readarg),
("pad4", "<2i4"),
("vx", readarg),
("pad5", "<2i4"),
("vy", readarg),
("pad6", "<2i4"),
("vz", readarg),
]
if not self.has_force:
frame_contents += [("pad7", "<i4")]
else:
frame_contents += [
("pad7", "<2i4"),
("fx", readarg),
("pad8", "<2i4"),
("fy", readarg),
("pad9", "<2i4"),
("fz", readarg),
("pad10", "<i4"),
]
self._dtype = np.dtype(frame_contents)
self._read_next_timestep()
|
def __init__(self, trzfilename, n_atoms=None, **kwargs):
"""Creates a TRZ Reader
Parameters
----------
trzfilename : str
name of input file
n_atoms : int
number of atoms in trajectory, must be taken from topology file!
convert_units : bool (optional)
converts units to MDAnalysis defaults
"""
super(TRZReader, self).__init__(trzfilename, **kwargs)
if n_atoms is None:
raise ValueError("TRZReader requires the n_atoms keyword")
self.trzfile = util.anyopen(self.filename, "rb")
self._cache = dict()
self._n_atoms = n_atoms
self._read_trz_header()
self.ts = Timestep(
self.n_atoms,
velocities=True,
forces=self.has_force,
reader=self,
**self._ts_kwargs,
)
# structured dtype of a single trajectory frame
readarg = str(n_atoms) + "f4"
frame_contents = [
("p1", "i4"),
("nframe", "i4"),
("ntrj", "i4"),
("natoms", "i4"),
("treal", "f8"),
("p2", "2i4"),
("box", "9f8"),
("p3", "2i4"),
("pressure", "f8"),
("ptensor", "6f8"),
("p4", "3i4"),
("etot", "f8"),
("ptot", "f8"),
("ek", "f8"),
("T", "f8"),
("p5", "6i4"),
("rx", readarg),
("pad2", "2i4"),
("ry", readarg),
("pad3", "2i4"),
("rz", readarg),
("pad4", "2i4"),
("vx", readarg),
("pad5", "2i4"),
("vy", readarg),
("pad6", "2i4"),
("vz", readarg),
]
if not self.has_force:
frame_contents += [("pad7", "i4")]
else:
frame_contents += [
("pad7", "2i4"),
("fx", readarg),
("pad8", "2i4"),
("fy", readarg),
("pad9", "2i4"),
("fz", readarg),
("pad10", "i4"),
]
self._dtype = np.dtype(frame_contents)
self._read_next_timestep()
|
https://github.com/MDAnalysis/mdanalysis/issues/1424
|
Traceback (most recent call last):
File "/usr/lib/python2.7/site-packages/nose/case.py", line 381, in setUp
try_run(self.inst, ('setup', 'setUp'))
File "/usr/lib/python2.7/site-packages/nose/util.py", line 471, in try_run
return func()
File "/builddir/build/BUILD/MDAnalysis-0.16.1/MDAnalysisTests-0.16.1/MDAnalysisTests/analysis/test_hydrogenbondautocorrel.py", line 37, in setUp
u = self.u = mda.Universe(TRZ_psf, TRZ)
File "/builddir/build/BUILDROOT/python-MDAnalysis-0.16.1-2.fc27.ppc64/usr/lib64/python2.7/site-packages/MDAnalysis/core/universe.py", line 278, in __init__
self.load_new(coordinatefile, **kwargs)
File "/builddir/build/BUILDROOT/python-MDAnalysis-0.16.1-2.fc27.ppc64/usr/lib64/python2.7/site-packages/MDAnalysis/core/universe.py", line 426, in load_new
self.trajectory = reader(filename, **kwargs)
File "/builddir/build/BUILDROOT/python-MDAnalysis-0.16.1-2.fc27.ppc64/usr/lib64/python2.7/site-packages/MDAnalysis/coordinates/TRZ.py", line 175, in __init__
self._read_trz_header()
File "/builddir/build/BUILDROOT/python-MDAnalysis-0.16.1-2.fc27.ppc64/usr/lib64/python2.7/site-packages/MDAnalysis/coordinates/TRZ.py", line 242, in _read_trz_header
raise IOError
IOError:
|
IOError
|
def _read_trz_header(self):
"""Reads the header of the trz trajectory"""
self._headerdtype = np.dtype(
[
("p1", "<i4"),
("title", "80c"),
("p2", "<2i4"),
("force", "<i4"),
("p3", "<i4"),
]
)
data = np.fromfile(self.trzfile, dtype=self._headerdtype, count=1)
self.title = "".join(c.decode("utf-8") for c in data["title"][0]).strip()
if data["force"] == 10:
self.has_force = False
elif data["force"] == 20:
self.has_force = True
else:
raise IOError
|
def _read_trz_header(self):
"""Reads the header of the trz trajectory"""
self._headerdtype = np.dtype(
[("p1", "i4"), ("title", "80c"), ("p2", "2i4"), ("force", "i4"), ("p3", "i4")]
)
data = np.fromfile(self.trzfile, dtype=self._headerdtype, count=1)
self.title = "".join(c.decode("utf-8") for c in data["title"][0]).strip()
if data["force"] == 10:
self.has_force = False
elif data["force"] == 20:
self.has_force = True
else:
raise IOError
|
https://github.com/MDAnalysis/mdanalysis/issues/1424
|
Traceback (most recent call last):
File "/usr/lib/python2.7/site-packages/nose/case.py", line 381, in setUp
try_run(self.inst, ('setup', 'setUp'))
File "/usr/lib/python2.7/site-packages/nose/util.py", line 471, in try_run
return func()
File "/builddir/build/BUILD/MDAnalysis-0.16.1/MDAnalysisTests-0.16.1/MDAnalysisTests/analysis/test_hydrogenbondautocorrel.py", line 37, in setUp
u = self.u = mda.Universe(TRZ_psf, TRZ)
File "/builddir/build/BUILDROOT/python-MDAnalysis-0.16.1-2.fc27.ppc64/usr/lib64/python2.7/site-packages/MDAnalysis/core/universe.py", line 278, in __init__
self.load_new(coordinatefile, **kwargs)
File "/builddir/build/BUILDROOT/python-MDAnalysis-0.16.1-2.fc27.ppc64/usr/lib64/python2.7/site-packages/MDAnalysis/core/universe.py", line 426, in load_new
self.trajectory = reader(filename, **kwargs)
File "/builddir/build/BUILDROOT/python-MDAnalysis-0.16.1-2.fc27.ppc64/usr/lib64/python2.7/site-packages/MDAnalysis/coordinates/TRZ.py", line 175, in __init__
self._read_trz_header()
File "/builddir/build/BUILDROOT/python-MDAnalysis-0.16.1-2.fc27.ppc64/usr/lib64/python2.7/site-packages/MDAnalysis/coordinates/TRZ.py", line 242, in _read_trz_header
raise IOError
IOError:
|
IOError
|
def __init__(self, filename, n_atoms, title="TRZ", convert_units=None):
"""Create a TRZWriter
Parameters
----------
filename : str
name of output file
n_atoms : int
number of atoms in trajectory
title : str (optional)
title of the trajectory; the title must be 80 characters or
shorter, a longer title raises a ValueError exception.
convert_units : bool (optional)
units are converted to the MDAnalysis base format; ``None`` selects
the value of :data:`MDAnalysis.core.flags` ['convert_lengths'].
(see :ref:`flags-label`)
"""
self.filename = filename
if n_atoms is None:
raise ValueError("TRZWriter requires the n_atoms keyword")
if n_atoms == 0:
raise ValueError("TRZWriter: no atoms in output trajectory")
self.n_atoms = n_atoms
if len(title) > 80:
raise ValueError("TRZWriter: 'title' must be 80 characters of shorter")
if convert_units is None:
convert_units = flags["convert_lengths"]
self.convert_units = convert_units
self.trzfile = util.anyopen(self.filename, "wb")
self._writeheader(title)
floatsize = str(n_atoms) + "<f4"
self.frameDtype = np.dtype(
[
("p1a", "<i4"),
("nframe", "<i4"),
("ntrj", "<i4"),
("natoms", "<i4"),
("treal", "<f8"),
("p1b", "<i4"),
("p2a", "<i4"),
("box", "<9f8"),
("p2b", "<i4"),
("p3a", "<i4"),
("pressure", "<f8"),
("ptensor", "<6f8"),
("p3b", "<i4"),
("p4a", "<i4"),
("six", "<i4"),
("etot", "<f8"),
("ptot", "<f8"),
("ek", "<f8"),
("T", "<f8"),
("blanks", "<2f8"),
("p4b", "<i4"),
("p5a", "<i4"),
("rx", floatsize),
("p5b", "<i4"),
("p6a", "<i4"),
("ry", floatsize),
("p6b", "<i4"),
("p7a", "<i4"),
("rz", floatsize),
("p7b", "<i4"),
("p8a", "<i4"),
("vx", floatsize),
("p8b", "<i4"),
("p9a", "<i4"),
("vy", floatsize),
("p9b", "<i4"),
("p10a", "<i4"),
("vz", floatsize),
("p10b", "<i4"),
]
)
|
def __init__(self, filename, n_atoms, title="TRZ", convert_units=None):
"""Create a TRZWriter
Parameters
----------
filename : str
name of output file
n_atoms : int
number of atoms in trajectory
title : str (optional)
title of the trajectory; the title must be 80 characters or
shorter, a longer title raises a ValueError exception.
convert_units : bool (optional)
units are converted to the MDAnalysis base format; ``None`` selects
the value of :data:`MDAnalysis.core.flags` ['convert_lengths'].
(see :ref:`flags-label`)
"""
self.filename = filename
if n_atoms is None:
raise ValueError("TRZWriter requires the n_atoms keyword")
if n_atoms == 0:
raise ValueError("TRZWriter: no atoms in output trajectory")
self.n_atoms = n_atoms
if len(title) > 80:
raise ValueError("TRZWriter: 'title' must be 80 characters of shorter")
if convert_units is None:
convert_units = flags["convert_lengths"]
self.convert_units = convert_units
self.trzfile = util.anyopen(self.filename, "wb")
self._writeheader(title)
floatsize = str(n_atoms) + "f4"
self.frameDtype = np.dtype(
[
("p1a", "i4"),
("nframe", "i4"),
("ntrj", "i4"),
("natoms", "i4"),
("treal", "f8"),
("p1b", "i4"),
("p2a", "i4"),
("box", "9f8"),
("p2b", "i4"),
("p3a", "i4"),
("pressure", "f8"),
("ptensor", "6f8"),
("p3b", "i4"),
("p4a", "i4"),
("six", "i4"),
("etot", "f8"),
("ptot", "f8"),
("ek", "f8"),
("T", "f8"),
("blanks", "2f8"),
("p4b", "i4"),
("p5a", "i4"),
("rx", floatsize),
("p5b", "i4"),
("p6a", "i4"),
("ry", floatsize),
("p6b", "i4"),
("p7a", "i4"),
("rz", floatsize),
("p7b", "i4"),
("p8a", "i4"),
("vx", floatsize),
("p8b", "i4"),
("p9a", "i4"),
("vy", floatsize),
("p9b", "i4"),
("p10a", "i4"),
("vz", floatsize),
("p10b", "i4"),
]
)
|
https://github.com/MDAnalysis/mdanalysis/issues/1424
|
Traceback (most recent call last):
File "/usr/lib/python2.7/site-packages/nose/case.py", line 381, in setUp
try_run(self.inst, ('setup', 'setUp'))
File "/usr/lib/python2.7/site-packages/nose/util.py", line 471, in try_run
return func()
File "/builddir/build/BUILD/MDAnalysis-0.16.1/MDAnalysisTests-0.16.1/MDAnalysisTests/analysis/test_hydrogenbondautocorrel.py", line 37, in setUp
u = self.u = mda.Universe(TRZ_psf, TRZ)
File "/builddir/build/BUILDROOT/python-MDAnalysis-0.16.1-2.fc27.ppc64/usr/lib64/python2.7/site-packages/MDAnalysis/core/universe.py", line 278, in __init__
self.load_new(coordinatefile, **kwargs)
File "/builddir/build/BUILDROOT/python-MDAnalysis-0.16.1-2.fc27.ppc64/usr/lib64/python2.7/site-packages/MDAnalysis/core/universe.py", line 426, in load_new
self.trajectory = reader(filename, **kwargs)
File "/builddir/build/BUILDROOT/python-MDAnalysis-0.16.1-2.fc27.ppc64/usr/lib64/python2.7/site-packages/MDAnalysis/coordinates/TRZ.py", line 175, in __init__
self._read_trz_header()
File "/builddir/build/BUILDROOT/python-MDAnalysis-0.16.1-2.fc27.ppc64/usr/lib64/python2.7/site-packages/MDAnalysis/coordinates/TRZ.py", line 242, in _read_trz_header
raise IOError
IOError:
|
IOError
|
def _writeheader(self, title):
hdt = np.dtype(
[
("pad1", "<i4"),
("title", "80c"),
("pad2", "<i4"),
("pad3", "<i4"),
("nrec", "<i4"),
("pad4", "<i4"),
]
)
out = np.zeros((), dtype=hdt)
out["pad1"], out["pad2"] = 80, 80
out["title"] = title + " " * (80 - len(title))
out["pad3"], out["pad4"] = 4, 4
out["nrec"] = 10
out.tofile(self.trzfile)
|
def _writeheader(self, title):
hdt = np.dtype(
[
("pad1", "i4"),
("title", "80c"),
("pad2", "i4"),
("pad3", "i4"),
("nrec", "i4"),
("pad4", "i4"),
]
)
out = np.zeros((), dtype=hdt)
out["pad1"], out["pad2"] = 80, 80
out["title"] = title + " " * (80 - len(title))
out["pad3"], out["pad4"] = 4, 4
out["nrec"] = 10
out.tofile(self.trzfile)
|
https://github.com/MDAnalysis/mdanalysis/issues/1424
|
Traceback (most recent call last):
File "/usr/lib/python2.7/site-packages/nose/case.py", line 381, in setUp
try_run(self.inst, ('setup', 'setUp'))
File "/usr/lib/python2.7/site-packages/nose/util.py", line 471, in try_run
return func()
File "/builddir/build/BUILD/MDAnalysis-0.16.1/MDAnalysisTests-0.16.1/MDAnalysisTests/analysis/test_hydrogenbondautocorrel.py", line 37, in setUp
u = self.u = mda.Universe(TRZ_psf, TRZ)
File "/builddir/build/BUILDROOT/python-MDAnalysis-0.16.1-2.fc27.ppc64/usr/lib64/python2.7/site-packages/MDAnalysis/core/universe.py", line 278, in __init__
self.load_new(coordinatefile, **kwargs)
File "/builddir/build/BUILDROOT/python-MDAnalysis-0.16.1-2.fc27.ppc64/usr/lib64/python2.7/site-packages/MDAnalysis/core/universe.py", line 426, in load_new
self.trajectory = reader(filename, **kwargs)
File "/builddir/build/BUILDROOT/python-MDAnalysis-0.16.1-2.fc27.ppc64/usr/lib64/python2.7/site-packages/MDAnalysis/coordinates/TRZ.py", line 175, in __init__
self._read_trz_header()
File "/builddir/build/BUILDROOT/python-MDAnalysis-0.16.1-2.fc27.ppc64/usr/lib64/python2.7/site-packages/MDAnalysis/coordinates/TRZ.py", line 242, in _read_trz_header
raise IOError
IOError:
|
IOError
|
def parse(self):
"""Parse atom information from PQR file *filename*.
Returns
-------
A MDAnalysis Topology object
"""
serials = []
names = []
resnames = []
chainIDs = []
resids = []
icodes = []
charges = []
radii = []
with openany(self.filename, "r") as f:
for line in f:
if line.startswith(("ATOM", "HETATM")):
fields = line.split()
try:
(
recordName,
serial,
name,
resName,
chainID,
resSeq,
x,
y,
z,
charge,
radius,
) = fields
except ValueError:
# files without the chainID
(
recordName,
serial,
name,
resName,
resSeq,
x,
y,
z,
charge,
radius,
) = fields
chainID = "SYSTEM"
try:
resid = int(resSeq)
except ValueError:
# has icode present
resid = int(resSeq[:-1])
icode = resSeq[-1]
else:
icode = ""
serials.append(serial)
names.append(name)
resnames.append(resName)
resids.append(resid)
icodes.append(icode)
charges.append(charge)
radii.append(radius)
chainIDs.append(chainID)
n_atoms = len(serials)
atomtypes = guessers.guess_types(names)
masses = guessers.guess_masses(atomtypes)
attrs = []
attrs.append(Atomids(np.array(serials, dtype=np.int32)))
attrs.append(Atomnames(np.array(names, dtype=object)))
attrs.append(Charges(np.array(charges, dtype=np.float32)))
attrs.append(Atomtypes(atomtypes, guessed=True))
attrs.append(Masses(masses, guessed=True))
attrs.append(Radii(np.array(radii, dtype=np.float32)))
resids = np.array(resids, dtype=np.int32)
icodes = np.array(icodes, dtype=object)
resnames = np.array(resnames, dtype=object)
chainIDs = np.array(chainIDs, dtype=object)
residx, (resids, resnames, icodes, chainIDs) = change_squash(
(resids, resnames, icodes, chainIDs), (resids, resnames, icodes, chainIDs)
)
n_residues = len(resids)
attrs.append(Resids(resids))
attrs.append(Resnums(resids.copy()))
attrs.append(Resnames(resnames))
attrs.append(ICodes(icodes))
segidx, chainIDs = squash_by(chainIDs)[:2]
n_segments = len(chainIDs)
attrs.append(Segids(chainIDs))
top = Topology(
n_atoms,
n_residues,
n_segments,
attrs=attrs,
atom_resindex=residx,
residue_segindex=segidx,
)
return top
|
def parse(self):
"""Parse atom information from PQR file *filename*.
Returns
-------
A MDAnalysis Topology object
"""
serials = []
names = []
resnames = []
chainIDs = []
resids = []
charges = []
radii = []
with openany(self.filename, "r") as f:
for line in f:
if line.startswith(("ATOM", "HETATM")):
fields = line.split()
try:
(
recordName,
serial,
name,
resName,
chainID,
resSeq,
x,
y,
z,
charge,
radius,
) = fields
except ValueError:
# files without the chainID
(
recordName,
serial,
name,
resName,
resSeq,
x,
y,
z,
charge,
radius,
) = fields
chainID = "SYSTEM"
serials.append(serial)
names.append(name)
resnames.append(resName)
resids.append(resSeq)
charges.append(charge)
radii.append(radius)
chainIDs.append(chainID)
n_atoms = len(serials)
atomtypes = guessers.guess_types(names)
masses = guessers.guess_masses(atomtypes)
attrs = []
attrs.append(Atomids(np.array(serials, dtype=np.int32)))
attrs.append(Atomnames(np.array(names, dtype=object)))
attrs.append(Charges(np.array(charges, dtype=np.float32)))
attrs.append(Atomtypes(atomtypes, guessed=True))
attrs.append(Masses(masses, guessed=True))
attrs.append(Radii(np.array(radii, dtype=np.float32)))
resids = np.array(resids, dtype=np.int32)
resnames = np.array(resnames, dtype=object)
chainIDs = np.array(chainIDs, dtype=object)
residx, resids, (resnames, chainIDs) = squash_by(resids, resnames, chainIDs)
n_residues = len(resids)
attrs.append(Resids(resids))
attrs.append(Resnums(resids.copy()))
attrs.append(Resnames(resnames))
segidx, chainIDs = squash_by(chainIDs)[:2]
n_segments = len(chainIDs)
attrs.append(Segids(chainIDs))
top = Topology(
n_atoms,
n_residues,
n_segments,
attrs=attrs,
atom_resindex=residx,
residue_segindex=segidx,
)
return top
|
https://github.com/MDAnalysis/mdanalysis/issues/1317
|
ValueError Traceback (most recent call last)
<ipython-input-92-bd129cc992c5> in <module>()
----> 1 mda.Universe('1A2C.pqr')
/nfs/homes/kreidy/.local/lib/python2.7/site-packages/MDAnalysis/core/universe.pyc in __init__(self, *args, **kwargs)
246 raise ValueError("Failed to construct topology from file {0}"
247 " with parser {1} \n"
--> 248 "Error: {2}".format(self.filename, parser, err))
249
250 # generate and populate Universe version of each class
ValueError: Failed to construct topology from file 1A2C.pqr with parser <class 'MDAnalysis.topology.PQRParser.PQRParser'>
Error: invalid literal for long() with base 10: '36A'
|
ValueError
|
def __init__(
self,
traj,
reference=None,
select="all",
groupselections=None,
filename="rmsd.dat",
mass_weighted=False,
tol_mass=0.1,
ref_frame=0,
):
"""Setting up the RMSD analysis.
The RMSD will be computed between *select* and *reference* for
all frames in the trajectory in *universe*.
Parameters
----------
traj : :class:`MDAnalysis.Universe`
universe that contains a trajectory
reference : :class:`MDAnalysis.Universe` (optional)
reference coordinates, if ``None`` current frame of *traj* is used
select : str / dict / tuple (optional)
The selection to operate on; can be one of:
1. any valid selection string for
:meth:`~MDAnalysis.core.AtomGroup.AtomGroup.select_atoms` that
produces identical selections in *mobile* and *reference*; or
2. a dictionary ``{'mobile':sel1, 'reference':sel2}`` (the
:func:`MDAnalysis.analysis.align.fasta2select` function returns
such a dictionary based on a ClustalW_ or STAMP_ sequence
alignment); or
3. a tuple ``(sel1, sel2)``
When using 2. or 3. with *sel1* and *sel2* then these selections
can also each be a list of selection strings (to generate a
AtomGroup with defined atom order as described under
:ref:`ordered-selections-label`).
groupselections : list (optional)
A list of selections as described for *select*. Each selection
describes additional RMSDs to be computed *after the structures
have be superpositioned* according to *select*. The output contains
one additional column for each selection. [``None``]
.. Note:: Experimental feature. Only limited error checking
implemented.
filename : str (optional)
write RSMD into file file :meth:`RMSD.save`
mass_weighted : bool (optional)
do a mass-weighted RMSD fit
tol_mass : float (optional)
Reject match if the atomic masses for matched atoms differ by more
than `tol_mass`
ref_frame : int (optional)
frame index to select frame from `reference`
.. _ClustalW: http://www.clustal.org/
.. _STAMP: http://www.compbio.dundee.ac.uk/manuals/stamp.4.2/
.. versionadded:: 0.7.7
.. versionchanged:: 0.8
*groupselections* added
"""
self.universe = traj
if reference is None:
self.reference = self.universe
else:
self.reference = reference
self.select = process_selection(select)
if groupselections is not None:
self.groupselections = [process_selection(s) for s in groupselections]
else:
self.groupselections = []
self.mass_weighted = mass_weighted
self.tol_mass = tol_mass
self.ref_frame = ref_frame
self.filename = filename
self.ref_atoms = self.reference.select_atoms(*self.select["reference"])
self.traj_atoms = self.universe.select_atoms(*self.select["mobile"])
if len(self.ref_atoms) != len(self.traj_atoms):
errmsg = (
"Reference and trajectory atom selections do "
+ "not contain the same number of atoms: "
+ "N_ref={0:d}, N_traj={1:d}".format(
self.ref_atoms.n_atoms, self.traj_atoms.n_atoms
)
)
logger.exception(errmsg)
raise SelectionError(errmsg)
logger.info("RMS calculation for {0:d} atoms.".format(len(self.ref_atoms)))
mass_mismatches = (
np.absolute(self.ref_atoms.masses - self.traj_atoms.masses) > self.tol_mass
)
if np.any(mass_mismatches):
# diagnostic output:
logger.error("Atoms: reference | trajectory")
for ar, at in zip(self.ref_atoms, self.traj_atoms):
if ar.name != at.name:
logger.error(
"{0!s:>4} {1:3d} {2!s:>3} {3!s:>3} {4:6.3f} | {5!s:>4} {6:3d} {7!s:>3} {8!s:>3} {9:6.3f}".format(
ar.segid,
ar.resid,
ar.resname,
ar.name,
ar.mass,
at.segid,
at.resid,
at.resname,
at.name,
at.mass,
)
)
errmsg = (
"Inconsistent selections, masses differ by more than"
+ "{0:f}; mis-matching atoms are shown above.".format(self.tol_mass)
)
logger.error(errmsg)
raise SelectionError(errmsg)
del mass_mismatches
# TODO:
# - make a group comparison a class that contains the checks above
# - use this class for the *select* group and the additional
# *groupselections* groups each a dict with reference/mobile
self.groupselections_atoms = [
{
"reference": self.reference.select_atoms(*s["reference"]),
"mobile": self.universe.select_atoms(*s["mobile"]),
}
for s in self.groupselections
]
# sanity check
for igroup, (sel, atoms) in enumerate(
zip(self.groupselections, self.groupselections_atoms)
):
if len(atoms["mobile"]) != len(atoms["reference"]):
logger.exception("SelectionError: Group Selection")
raise SelectionError(
"Group selection {0}: {1} | {2}: Reference and trajectory "
"atom selections do not contain the same number of atoms: "
"N_ref={3}, N_traj={4}".format(
igroup,
sel["reference"],
sel["mobile"],
len(atoms["reference"]),
len(atoms["mobile"]),
)
)
self.rmsd = None
|
def __init__(
self,
traj,
reference=None,
select="all",
groupselections=None,
filename="rmsd.dat",
mass_weighted=False,
tol_mass=0.1,
ref_frame=0,
):
"""Setting up the RMSD analysis.
The RMSD will be computed between *select* and *reference* for
all frames in the trajectory in *universe*.
Parameters
----------
traj : :class:`MDAnalysis.Universe`
universe that contains a trajectory
reference : :class:`MDAnalysis.Universe` (optional)
reference coordinates, if ``None`` current frame of *traj* is used
select : str / dict / tuple (optional)
The selection to operate on; can be one of:
1. any valid selection string for
:meth:`~MDAnalysis.core.AtomGroup.AtomGroup.select_atoms` that
produces identical selections in *mobile* and *reference*; or
2. a dictionary ``{'mobile':sel1, 'reference':sel2}`` (the
:func:`MDAnalysis.analysis.align.fasta2select` function returns
such a dictionary based on a ClustalW_ or STAMP_ sequence
alignment); or
3. a tuple ``(sel1, sel2)``
When using 2. or 3. with *sel1* and *sel2* then these selections
can also each be a list of selection strings (to generate a
AtomGroup with defined atom order as described under
:ref:`ordered-selections-label`).
groupselections : list (optional)
A list of selections as described for *select*. Each selection
describes additional RMSDs to be computed *after the structures
have be superpositioned* according to *select*. The output contains
one additional column for each selection. [``None``]
.. Note:: Experimental feature. Only limited error checking
implemented.
filename : str (optional)
write RSMD into file file :meth:`RMSD.save`
mass_weighted : bool (optional)
do a mass-weighted RMSD fit
tol_mass : float (optional)
Reject match if the atomic masses for matched atoms differ by more
than `tol_mass`
ref_frame : int (optional)
frame index to select frame from `reference`
.. _ClustalW: http://www.clustal.org/
.. _STAMP: http://www.compbio.dundee.ac.uk/manuals/stamp.4.2/
.. versionadded:: 0.7.7
.. versionchanged:: 0.8
*groupselections* added
"""
self.universe = traj
if reference is None:
self.reference = self.universe
else:
self.reference = reference
self.select = _process_selection(select)
if groupselections is not None:
self.groupselections = [_process_selection(s) for s in groupselections]
else:
self.groupselections = []
self.mass_weighted = mass_weighted
self.tol_mass = tol_mass
self.ref_frame = ref_frame
self.filename = filename
self.ref_atoms = self.reference.select_atoms(*self.select["reference"])
self.traj_atoms = self.universe.select_atoms(*self.select["mobile"])
if len(self.ref_atoms) != len(self.traj_atoms):
logger.exception()
raise SelectionError(
"Reference and trajectory atom selections do "
"not contain the same number of atoms: "
"N_ref={0:d}, N_traj={1:d}".format(
self.ref_atoms.n_atoms, self.traj_atoms.n_atoms
)
)
logger.info("RMS calculation for {0:d} atoms.".format(len(self.ref_atoms)))
mass_mismatches = (
np.absolute(self.ref_atoms.masses - self.traj_atoms.masses) > self.tol_mass
)
if np.any(mass_mismatches):
# diagnostic output:
logger.error("Atoms: reference | trajectory")
for ar, at in zip(self.ref_atoms, self.traj_atoms):
if ar.name != at.name:
logger.error(
"{0!s:>4} {1:3d} {2!s:>3} {3!s:>3} {4:6.3f} | {5!s:>4} {6:3d} {7!s:>3} {8!s:>3} {9:6.3f}".format(
ar.segid,
ar.resid,
ar.resname,
ar.name,
ar.mass,
at.segid,
at.resid,
at.resname,
at.name,
at.mass,
)
)
errmsg = "Inconsistent selections, masses differ by more than {0:f}; mis-matching atoms are shown above.".format(
self.tol_mass
)
logger.error(errmsg)
raise SelectionError(errmsg)
del mass_mismatches
# TODO:
# - make a group comparison a class that contains the checks above
# - use this class for the *select* group and the additional
# *groupselections* groups each a dict with reference/mobile
self.groupselections_atoms = [
{
"reference": self.reference.select_atoms(*s["reference"]),
"mobile": self.universe.select_atoms(*s["mobile"]),
}
for s in self.groupselections
]
# sanity check
for igroup, (sel, atoms) in enumerate(
zip(self.groupselections, self.groupselections_atoms)
):
if len(atoms["mobile"]) != len(atoms["reference"]):
logger.exception()
raise SelectionError(
"Group selection {0}: {1} | {2}: Reference and trajectory "
"atom selections do not contain the same number of atoms: "
"N_ref={3}, N_traj={4}".format(
igroup,
sel["reference"],
sel["mobile"],
len(atoms["reference"]),
len(atoms["mobile"]),
)
)
self.rmsd = None
|
https://github.com/MDAnalysis/mdanalysis/issues/897
|
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
<ipython-input-6-be62615c3dd7> in <module>()
----> 1 R = RMSD(u, select="protein and name CA")
/nfs/homes2/oliver/Library/python/mdanalysis/package/MDAnalysis/analysis/rms.pyc in __init__(self, traj, reference, select, groupselections, filename, mass_weighted, tol_mass, ref_frame)
339 else:
340 self.reference = reference
--> 341 self.select = _process_selection(select)
342 if groupselections is not None:
343 self.groupselections = [_process_selection(s) for s in groupselections]
NameError: global name '_process_selection' is not defined
|
NameError
|
def _complete_for_arg(
self,
arg_action: argparse.Action,
text: str,
line: str,
begidx: int,
endidx: int,
consumed_arg_values: Dict[str, List[str]],
*,
cmd_set: Optional[CommandSet] = None,
) -> List[str]:
"""
Tab completion routine for an argparse argument
:return: list of completions
:raises: CompletionError if the completer or choices function this calls raises one
"""
# Check if the arg provides choices to the user
if arg_action.choices is not None:
arg_choices = arg_action.choices
else:
arg_choices = getattr(arg_action, ATTR_CHOICES_CALLABLE, None)
if arg_choices is None:
return []
# If we are going to call a completer/choices function, then set up the common arguments
args = []
kwargs = {}
if isinstance(arg_choices, ChoicesCallable):
if arg_choices.is_method:
# figure out what class the completer was defined in
completer_class = get_defining_class(arg_choices.to_call)
# Was there a defining class identified? If so, is it a sub-class of CommandSet?
if completer_class is not None and issubclass(completer_class, CommandSet):
# Since the completer function is provided as an unbound function, we need to locate the instance
# of the CommandSet to pass in as `self` to emulate a bound method call.
# We're searching for candidates that match the completer function's parent type in this order:
# 1. Does the CommandSet registered with the command's argparser match as a subclass?
# 2. Do any of the registered CommandSets in the Cmd2 application exactly match the type?
# 3. Is there a registered CommandSet that is is the only matching subclass?
# Now get the CommandSet associated with the current command/subcommand argparser
parser_cmd_set = getattr(
self._parser, constants.PARSER_ATTR_COMMANDSET, cmd_set
)
if isinstance(parser_cmd_set, completer_class):
# Case 1: Parser's CommandSet is a sub-class of the completer function's CommandSet
cmd_set = parser_cmd_set
else:
# Search all registered CommandSets
cmd_set = None
candidate_sets = [] # type: List[CommandSet]
for installed_cmd_set in self._cmd2_app._installed_command_sets:
if type(installed_cmd_set) == completer_class:
# Case 2: CommandSet is an exact type match for the completer's CommandSet
cmd_set = installed_cmd_set
break
# Add candidate for Case 3:
if isinstance(installed_cmd_set, completer_class):
candidate_sets.append(installed_cmd_set)
if cmd_set is None and len(candidate_sets) == 1:
# Case 3: There exists exactly 1 CommandSet that is a subclass of the completer's CommandSet
cmd_set = candidate_sets[0]
if cmd_set is None:
# No cases matched, raise an error
raise CompletionError(
"Could not find CommandSet instance matching defining type for completer"
)
args.append(cmd_set)
args.append(self._cmd2_app)
# Check if arg_choices.to_call expects arg_tokens
to_call_params = inspect.signature(arg_choices.to_call).parameters
if ARG_TOKENS in to_call_params:
# Merge self._parent_tokens and consumed_arg_values
arg_tokens = {**self._parent_tokens, **consumed_arg_values}
# Include the token being completed
arg_tokens.setdefault(arg_action.dest, [])
arg_tokens[arg_action.dest].append(text)
# Add the namespace to the keyword arguments for the function we are calling
kwargs[ARG_TOKENS] = arg_tokens
# Check if the argument uses a specific tab completion function to provide its choices
if isinstance(arg_choices, ChoicesCallable) and arg_choices.is_completer:
args.extend([text, line, begidx, endidx])
results = arg_choices.to_call(*args, **kwargs)
# Otherwise use basic_complete on the choices
else:
# Check if the choices come from a function
if isinstance(arg_choices, ChoicesCallable) and not arg_choices.is_completer:
arg_choices = arg_choices.to_call(*args, **kwargs)
# Since arg_choices can be any iterable type, convert to a list
arg_choices = list(arg_choices)
# If these choices are numbers, and have not yet been sorted, then sort them now
if not self._cmd2_app.matches_sorted and all(
isinstance(x, numbers.Number) for x in arg_choices
):
arg_choices.sort()
self._cmd2_app.matches_sorted = True
# Since choices can be various types like int, we must convert them to strings
for index, choice in enumerate(arg_choices):
if not isinstance(choice, str):
arg_choices[index] = str(choice)
# Filter out arguments we already used
used_values = consumed_arg_values.get(arg_action.dest, [])
arg_choices = [choice for choice in arg_choices if choice not in used_values]
# Do tab completion on the choices
results = basic_complete(text, line, begidx, endidx, arg_choices)
return self._format_completions(arg_action, results)
|
def _complete_for_arg(
self,
arg_action: argparse.Action,
text: str,
line: str,
begidx: int,
endidx: int,
consumed_arg_values: Dict[str, List[str]],
*,
cmd_set: Optional[CommandSet] = None,
) -> List[str]:
"""
Tab completion routine for an argparse argument
:return: list of completions
:raises: CompletionError if the completer or choices function this calls raises one
"""
# Check if the arg provides choices to the user
if arg_action.choices is not None:
arg_choices = arg_action.choices
else:
arg_choices = getattr(arg_action, ATTR_CHOICES_CALLABLE, None)
if arg_choices is None:
return []
# If we are going to call a completer/choices function, then set up the common arguments
args = []
kwargs = {}
if isinstance(arg_choices, ChoicesCallable):
if arg_choices.is_method:
cmd_set = getattr(self._parser, constants.PARSER_ATTR_COMMANDSET, cmd_set)
if cmd_set is not None:
if isinstance(cmd_set, CommandSet):
# If command is part of a CommandSet, `self` should be the CommandSet and Cmd will be next
if cmd_set is not None:
args.append(cmd_set)
args.append(self._cmd2_app)
# Check if arg_choices.to_call expects arg_tokens
to_call_params = inspect.signature(arg_choices.to_call).parameters
if ARG_TOKENS in to_call_params:
# Merge self._parent_tokens and consumed_arg_values
arg_tokens = {**self._parent_tokens, **consumed_arg_values}
# Include the token being completed
arg_tokens.setdefault(arg_action.dest, [])
arg_tokens[arg_action.dest].append(text)
# Add the namespace to the keyword arguments for the function we are calling
kwargs[ARG_TOKENS] = arg_tokens
# Check if the argument uses a specific tab completion function to provide its choices
if isinstance(arg_choices, ChoicesCallable) and arg_choices.is_completer:
args.extend([text, line, begidx, endidx])
results = arg_choices.to_call(*args, **kwargs)
# Otherwise use basic_complete on the choices
else:
# Check if the choices come from a function
if isinstance(arg_choices, ChoicesCallable) and not arg_choices.is_completer:
arg_choices = arg_choices.to_call(*args, **kwargs)
# Since arg_choices can be any iterable type, convert to a list
arg_choices = list(arg_choices)
# If these choices are numbers, and have not yet been sorted, then sort them now
if not self._cmd2_app.matches_sorted and all(
isinstance(x, numbers.Number) for x in arg_choices
):
arg_choices.sort()
self._cmd2_app.matches_sorted = True
# Since choices can be various types like int, we must convert them to strings
for index, choice in enumerate(arg_choices):
if not isinstance(choice, str):
arg_choices[index] = str(choice)
# Filter out arguments we already used
used_values = consumed_arg_values.get(arg_action.dest, [])
arg_choices = [choice for choice in arg_choices if choice not in used_values]
# Do tab completion on the choices
results = basic_complete(text, line, begidx, endidx, arg_choices)
return self._format_completions(arg_action, results)
|
https://github.com/python-cmd2/cmd2/issues/967
|
Traceback (most recent call last):
File "/home/martin/.virtualenvs/pyos/lib/python3.8/site-packages/cmd2/cmd2.py", line 1707, in complete
self._completion_for_command(text, line, begidx, endidx, shortcut_to_restore)
File "/home/martin/.virtualenvs/pyos/lib/python3.8/site-packages/cmd2/cmd2.py", line 1592, in _completion_for_command
self.completion_matches = self._redirect_complete(text, line, begidx, endidx, compfunc)
File "/home/martin/.virtualenvs/pyos/lib/python3.8/site-packages/cmd2/cmd2.py", line 1385, in _redirect_complete
return compfunc(text, line, begidx, endidx)
File "/home/martin/.virtualenvs/pyos/lib/python3.8/site-packages/cmd2/cmd2.py", line 1757, in _complete_argparse_command
return completer.complete_command(tokens_to_parse, text, line, begidx, endidx, cmd_set=cmd_set)
File "/home/martin/.virtualenvs/pyos/lib/python3.8/site-packages/cmd2/argparse_completer.py", line 429, in complete_command
completion_results = self._complete_for_arg(pos_arg_state.action, text, line,
File "/home/martin/.virtualenvs/pyos/lib/python3.8/site-packages/cmd2/argparse_completer.py", line 596, in _complete_for_arg
results = arg_choices.to_call(*args, **kwargs)
TypeError: path_complete() takes 5 positional arguments but 6 were given
|
TypeError
|
def _initialize_history(self, hist_file):
"""Initialize history using history related attributes
This function can determine whether history is saved in the prior text-based
format (one line of input is stored as one line in the file), or the new-as-
of-version 0.9.13 pickle based format.
History created by versions <= 0.9.12 is in readline format, i.e. plain text files.
Initializing history does not effect history files on disk, versions >= 0.9.13 always
write history in the pickle format.
"""
self.history = History()
# with no persistent history, nothing else in this method is relevant
if not hist_file:
self.persistent_history_file = hist_file
return
hist_file = os.path.abspath(os.path.expanduser(hist_file))
# on Windows, trying to open a directory throws a permission
# error, not a `IsADirectoryError`. So we'll check it ourselves.
if os.path.isdir(hist_file):
msg = "Persistent history file '{}' is a directory"
self.perror(msg.format(hist_file))
return
# Create the directory for the history file if it doesn't already exist
hist_file_dir = os.path.dirname(hist_file)
try:
os.makedirs(hist_file_dir, exist_ok=True)
except OSError as ex:
msg = "Error creating persistent history file directory '{}': {}".format(
hist_file_dir, ex
)
self.pexcept(msg)
return
# first we try and unpickle the history file
history = History()
try:
with open(hist_file, "rb") as fobj:
history = pickle.load(fobj)
except (
AttributeError,
EOFError,
FileNotFoundError,
ImportError,
IndexError,
KeyError,
ValueError,
pickle.UnpicklingError,
):
# If any of these errors occur when attempting to unpickle, just use an empty history
pass
except OSError as ex:
msg = "Can not read persistent history file '{}': {}"
self.pexcept(msg.format(hist_file, ex))
return
self.history = history
self.history.start_session()
self.persistent_history_file = hist_file
# populate readline history
if rl_type != RlType.NONE:
last = None
for item in history:
# Break the command into its individual lines
for line in item.raw.splitlines():
# readline only adds a single entry for multiple sequential identical lines
# so we emulate that behavior here
if line != last:
readline.add_history(line)
last = line
# register a function to write history at save
# if the history file is in plain text format from 0.9.12 or lower
# this will fail, and the history in the plain text file will be lost
import atexit
atexit.register(self._persist_history)
|
def _initialize_history(self, hist_file):
"""Initialize history using history related attributes
This function can determine whether history is saved in the prior text-based
format (one line of input is stored as one line in the file), or the new-as-
of-version 0.9.13 pickle based format.
History created by versions <= 0.9.12 is in readline format, i.e. plain text files.
Initializing history does not effect history files on disk, versions >= 0.9.13 always
write history in the pickle format.
"""
self.history = History()
# with no persistent history, nothing else in this method is relevant
if not hist_file:
self.persistent_history_file = hist_file
return
hist_file = os.path.abspath(os.path.expanduser(hist_file))
# on Windows, trying to open a directory throws a permission
# error, not a `IsADirectoryError`. So we'll check it ourselves.
if os.path.isdir(hist_file):
msg = "Persistent history file '{}' is a directory"
self.perror(msg.format(hist_file))
return
# Create the directory for the history file if it doesn't already exist
hist_file_dir = os.path.dirname(hist_file)
try:
os.makedirs(hist_file_dir, exist_ok=True)
except OSError as ex:
msg = "Error creating persistent history file directory '{}': {}".format(
hist_file_dir, ex
)
self.pexcept(msg)
return
# first we try and unpickle the history file
history = History()
try:
with open(hist_file, "rb") as fobj:
history = pickle.load(fobj)
except (
AttributeError,
EOFError,
FileNotFoundError,
ImportError,
IndexError,
KeyError,
pickle.UnpicklingError,
):
# If any non-operating system error occurs when attempting to unpickle, just use an empty history
pass
except OSError as ex:
msg = "Can not read persistent history file '{}': {}"
self.pexcept(msg.format(hist_file, ex))
return
self.history = history
self.history.start_session()
self.persistent_history_file = hist_file
# populate readline history
if rl_type != RlType.NONE:
last = None
for item in history:
# Break the command into its individual lines
for line in item.raw.splitlines():
# readline only adds a single entry for multiple sequential identical lines
# so we emulate that behavior here
if line != last:
readline.add_history(line)
last = line
# register a function to write history at save
# if the history file is in plain text format from 0.9.12 or lower
# this will fail, and the history in the plain text file will be lost
import atexit
atexit.register(self._persist_history)
|
https://github.com/python-cmd2/cmd2/issues/785
|
Traceback (most recent call last):
File "C:\Users\mattthk4\AppData\Local\Programs\Python\Python36\lib\runpy.py", line 193, in _run_module_as_main
"__main__", mod_spec)
File "C:\Users\mattthk4\AppData\Local\Programs\Python\Python36\lib\runpy.py", line 85, in _run_code
exec(code, run_globals)
File "C:\Users\mattthk4\cvenv\Scripts\cdata-cli.exe\__main__.py", line 9, in <module>
File "c:\users\mattthk4\cvenv\lib\site-packages\cbash\cdata_cli.py", line 1193, in main
app = CmdLineApp(debug=args.debug)
File "c:\users\mattthk4\cvenv\lib\site-packages\cbash\cdata_cli.py", line 274, in __init__
cmd2.Cmd.__init__(self, persistent_history_file=self.HIST_FILE)
File "c:\users\mattthk4\cvenv\lib\site-packages\cmd2\cmd2.py", line 429, in __init__
self._initialize_history(persistent_history_file)
File "c:\users\mattthk4\cvenv\lib\site-packages\cmd2\cmd2.py", line 3602, in _initialize_history
history = pickle.load(fobj)
ValueError: invalid literal for int() with base 10: 'et projects --p\n'
|
ValueError
|
def _get_pointer(self):
try:
if self.thisdir.files[self._pointer] != self._pointed_obj:
try:
self._pointer = self.thisdir.files.index(self._pointed_obj)
except ValueError:
self._set_pointer(self._pointer)
except (TypeError, IndexError):
pass
return self._pointer
|
def _get_pointer(self):
if (
self.thisdir is not None
and self.thisdir.files[self._pointer] != self._pointed_obj
):
try:
self._pointer = self.thisdir.files.index(self._pointed_obj)
except ValueError:
self._pointed_obj = self.thisdir.files[self._pointer]
return self._pointer
|
https://github.com/ranger/ranger/issues/2136
|
$ ranger --clean (ai)
ranger version: ranger-master
Python version: 3.8.6 (default, Sep 30 2020, 04:00:38) [GCC 10.2.0]
Locale: en_GB.UTF-8
Traceback (most recent call last):
File "/usr/lib/python3.8/site-packages/ranger/core/main.py", line 203, in main
fm.loop()
File "/usr/lib/python3.8/site-packages/ranger/core/fm.py", line 382, in loop
ui.handle_input()
File "/usr/lib/python3.8/site-packages/ranger/gui/ui.py", line 276, in handle_input
self.handle_key(key)
File "/usr/lib/python3.8/site-packages/ranger/gui/ui.py", line 206, in handle_key
elif not DisplayableContainer.press(self, key):
File "/usr/lib/python3.8/site-packages/ranger/gui/displayable.py", line 272, in press
focused_obj.press(key)
File "/usr/lib/python3.8/site-packages/ranger/gui/widgets/console.py", line 189, in press
self.type_key(key)
File "/usr/lib/python3.8/site-packages/ranger/gui/widgets/console.py", line 218, in type_key
self.on_line_change()
File "/usr/lib/python3.8/site-packages/ranger/gui/widgets/console.py", line 514, in on_line_change
self.execute(cmd)
File "/usr/lib/python3.8/site-packages/ranger/gui/widgets/console.py", line 454, in execute
cmd.execute()
File "/usr/lib/python3.8/site-packages/ranger/config/commands.py", line 1521, in execute
self.fm.move(right=1)
File "/usr/lib/python3.8/site-packages/ranger/core/actions.py", line 499, in move
self.thistab.enter_dir(tfile)
File "/usr/lib/python3.8/site-packages/ranger/core/tab.py", line 183, in enter_dir
self.thisfile = self.thisdir.pointed_obj
File "/usr/lib/python3.8/site-packages/ranger/core/tab.py", line 52, in _set_thisfile
self.fm.signal_emit('move', previous=previous, new=value, tab=self)
File "/usr/lib/python3.8/site-packages/ranger/ext/signals.py", line 268, in signal_emit
fnc(signal)
File "/usr/lib/python3.8/site-packages/ranger/core/tab.py", line 40, in _set_thisfile_from_signal
self.pointer = self.thisdir.pointer
File "/usr/lib/python3.8/site-packages/ranger/core/tab.py", line 72, in _set_pointer
self._pointed_obj = self.thisdir.files[self._pointer]
TypeError: 'NoneType' object is not subscriptable
ranger crashed. Please report this traceback at:
https://github.com/ranger/ranger/issues
|
TypeError
|
def _set_pointer(self, value):
self._pointer = value
try:
self._pointed_obj = self.thisdir.files[self._pointer]
except (TypeError, IndexError):
pass
|
def _set_pointer(self, value):
self._pointer = value
try:
self._pointed_obj = self.thisdir.files[self._pointer]
except TypeError:
pass
except IndexError:
pass
|
https://github.com/ranger/ranger/issues/2136
|
$ ranger --clean (ai)
ranger version: ranger-master
Python version: 3.8.6 (default, Sep 30 2020, 04:00:38) [GCC 10.2.0]
Locale: en_GB.UTF-8
Traceback (most recent call last):
File "/usr/lib/python3.8/site-packages/ranger/core/main.py", line 203, in main
fm.loop()
File "/usr/lib/python3.8/site-packages/ranger/core/fm.py", line 382, in loop
ui.handle_input()
File "/usr/lib/python3.8/site-packages/ranger/gui/ui.py", line 276, in handle_input
self.handle_key(key)
File "/usr/lib/python3.8/site-packages/ranger/gui/ui.py", line 206, in handle_key
elif not DisplayableContainer.press(self, key):
File "/usr/lib/python3.8/site-packages/ranger/gui/displayable.py", line 272, in press
focused_obj.press(key)
File "/usr/lib/python3.8/site-packages/ranger/gui/widgets/console.py", line 189, in press
self.type_key(key)
File "/usr/lib/python3.8/site-packages/ranger/gui/widgets/console.py", line 218, in type_key
self.on_line_change()
File "/usr/lib/python3.8/site-packages/ranger/gui/widgets/console.py", line 514, in on_line_change
self.execute(cmd)
File "/usr/lib/python3.8/site-packages/ranger/gui/widgets/console.py", line 454, in execute
cmd.execute()
File "/usr/lib/python3.8/site-packages/ranger/config/commands.py", line 1521, in execute
self.fm.move(right=1)
File "/usr/lib/python3.8/site-packages/ranger/core/actions.py", line 499, in move
self.thistab.enter_dir(tfile)
File "/usr/lib/python3.8/site-packages/ranger/core/tab.py", line 183, in enter_dir
self.thisfile = self.thisdir.pointed_obj
File "/usr/lib/python3.8/site-packages/ranger/core/tab.py", line 52, in _set_thisfile
self.fm.signal_emit('move', previous=previous, new=value, tab=self)
File "/usr/lib/python3.8/site-packages/ranger/ext/signals.py", line 268, in signal_emit
fnc(signal)
File "/usr/lib/python3.8/site-packages/ranger/core/tab.py", line 40, in _set_thisfile_from_signal
self.pointer = self.thisdir.pointer
File "/usr/lib/python3.8/site-packages/ranger/core/tab.py", line 72, in _set_pointer
self._pointed_obj = self.thisdir.files[self._pointer]
TypeError: 'NoneType' object is not subscriptable
ranger crashed. Please report this traceback at:
https://github.com/ranger/ranger/issues
|
TypeError
|
def _set_pointer(self, value):
self._pointer = value
try:
self._pointed_obj = self.thisdir.files[self._pointer]
except (TypeError, IndexError):
pass
|
def _set_pointer(self, value):
self._pointer = value
self._pointed_obj = self.thisdir.files[self._pointer]
|
https://github.com/ranger/ranger/issues/2071
|
Message Log:
21:48:19 INFO ranger version: ranger-master
21:48:19 INFO Python version: 3.8.5 (default, Jul 27 2020, 08:42:51) [GCC 10.1.0]
21:48:19 INFO Locale: en_US.UTF-8
21:48:19 INFO Process ID: 73182
21:48:25 ERRO Notification: 'NoneType' object is not subscriptable
21:48:25 ERRO 'NoneType' object is not subscriptable
Traceback (most recent call last):
File "/home/kevin/.local/lib/python3.8/site-packages/ranger/core/actions.py", line 269, in execute_console
cmd.execute()
File "/home/kevin/.config/ranger/plugins/fzf_find.py", line 31, in execute
self.fm.cd(fzf_file)
File "/home/kevin/.local/lib/python3.8/site-packages/ranger/core/actions.py", line 600, in cd
self.enter_dir(path, remember=remember)
File "/home/kevin/.local/lib/python3.8/site-packages/ranger/core/actions.py", line 585, in enter_dir
result = self.thistab.enter_dir(path, history=history)
File "/home/kevin/.local/lib/python3.8/site-packages/ranger/core/tab.py", line 183, in enter_dir
self.thisfile = self.thisdir.pointed_obj
File "/home/kevin/.local/lib/python3.8/site-packages/ranger/core/tab.py", line 52, in _set_thisfile
self.fm.signal_emit('move', previous=previous, new=value, tab=self)
File "/home/kevin/.local/lib/python3.8/site-packages/ranger/ext/signals.py", line 268, in signal_emit
fnc(signal)
File "/home/kevin/.local/lib/python3.8/site-packages/ranger/core/tab.py", line 40, in _set_thisfile_from_signal
self.pointer = self.thisdir.pointer
File "/home/kevin/.local/lib/python3.8/site-packages/ranger/core/tab.py", line 72, in _set_pointer
self._pointed_obj = self.thisdir.files[self._pointer]
TypeError: 'NoneType' object is not subscriptable
|
TypeError
|
def _set_pointer(self, value):
self._pointer = value
try:
self._pointed_obj = self.thisdir.files[self._pointer]
except TypeError:
pass
except IndexError:
pass
|
def _set_pointer(self, value):
self._pointer = value
try:
self._pointed_obj = self.thisdir.files[self._pointer]
except TypeError:
pass
|
https://github.com/ranger/ranger/issues/2173
|
ranger version: ranger-master
Python version: 3.9.0 (default, Oct 7 2020, 23:09:01) [GCC 10.2.0]
Locale: en_IN.UTF-8
Traceback (most recent call last):
File "/usr/lib/python3.9/site-packages/ranger/core/main.py", line 203, in main
fm.loop()
File "/usr/lib/python3.9/site-packages/ranger/core/fm.py", line 382, in loop
ui.handle_input()
File "/usr/lib/python3.9/site-packages/ranger/gui/ui.py", line 271, in handle_input
self.handle_mouse()
File "/usr/lib/python3.9/site-packages/ranger/gui/ui.py", line 197, in handle_mouse
DisplayableContainer.click(self, event)
File "/usr/lib/python3.9/site-packages/ranger/gui/displayable.py", line 284, in click
if displayable.click(event):
File "/usr/lib/python3.9/site-packages/ranger/gui/widgets/view_base.py", line 188, in click
if DisplayableContainer.click(self, event):
File "/usr/lib/python3.9/site-packages/ranger/gui/displayable.py", line 284, in click
if displayable.click(event):
File "/usr/lib/python3.9/site-packages/ranger/gui/widgets/browsercolumn.py", line 86, in click
self.fm.enter_dir(self.target.path)
File "/usr/lib/python3.9/site-packages/ranger/core/actions.py", line 585, in enter_dir
result = self.thistab.enter_dir(path, history=history)
File "/usr/lib/python3.9/site-packages/ranger/core/tab.py", line 183, in enter_dir
self.thisfile = self.thisdir.pointed_obj
File "/usr/lib/python3.9/site-packages/ranger/core/tab.py", line 52, in _set_thisfile
self.fm.signal_emit('move', previous=previous, new=value, tab=self)
File "/usr/lib/python3.9/site-packages/ranger/ext/signals.py", line 268, in signal_emit
fnc(signal)
File "/usr/lib/python3.9/site-packages/ranger/core/tab.py", line 40, in _set_thisfile_from_signal
self.pointer = self.thisdir.pointer
File "/usr/lib/python3.9/site-packages/ranger/core/tab.py", line 72, in _set_pointer
self._pointed_obj = self.thisdir.files[self._pointer]
IndexError: list index out of range
|
IndexError
|
def sha512_encode(path, inode=None):
if inode is None:
inode = stat(path).st_ino
inode_path = "{0}{1}".format(str(inode), path)
if PY3:
inode_path = inode_path.encode("utf-8", "backslashreplace")
return "{0}.jpg".format(sha512(inode_path).hexdigest())
|
def sha512_encode(path, inode=None):
if inode is None:
inode = stat(path).st_ino
inode_path = "{0}{1}".format(str(inode), path)
if PY3:
inode_path = inode_path.encode("utf-8", "backslashescape")
return "{0}.jpg".format(sha512(inode_path).hexdigest())
|
https://github.com/ranger/ranger/issues/2119
|
ranger version: ranger-master
Python version: 3.8.5 (default, Sep 5 2020, 10:50:12) [GCC 10.2.0]
Locale: de_DE.UTF-8
Current file: '/path/to/my/file/Arbeitszeitnachweis_f\udc81r Minijobber_2020_09.PDF'
Traceback (most recent call last):
File "/usr/lib/python3.8/site-packages/ranger/core/main.py", line 203, in main
fm.loop()
File "/usr/lib/python3.8/site-packages/ranger/core/fm.py", line 376, in loop
ui.redraw()
File "/usr/lib/python3.8/site-packages/ranger/gui/ui.py", line 343, in redraw
self.draw()
File "/usr/lib/python3.8/site-packages/ranger/gui/ui.py", line 370, in draw
DisplayableContainer.draw(self)
File "/usr/lib/python3.8/site-packages/ranger/gui/displayable.py", line 257, in draw
displayable.draw()
File "/usr/lib/python3.8/site-packages/ranger/gui/widgets/view_miller.py", line 100, in draw
DisplayableContainer.draw(self)
File "/usr/lib/python3.8/site-packages/ranger/gui/displayable.py", line 257, in draw
displayable.draw()
File "/usr/lib/python3.8/site-packages/ranger/gui/widgets/browsercolumn.py", line 187, in draw
self._draw_file()
File "/usr/lib/python3.8/site-packages/ranger/gui/widgets/browsercolumn.py", line 206, in _draw_file
path = self.target.get_preview_source(self.wid, self.hei)
File "/usr/lib/python3.8/site-packages/ranger/container/file.py", line 96, in get_preview_source
return self.fm.get_preview(self, width, height)
File "/usr/lib/python3.8/site-packages/ranger/core/actions.py", line 1101, in get_preview
self.sha512_encode(path, inode=fobj.stat.st_ino)
File "/usr/lib/python3.8/site-packages/ranger/core/actions.py", line 1028, in sha512_encode
inode_path = inode_path.encode('utf-8', 'backslashescape')
LookupError: unknown error handler name 'backslashescape'
ranger crashed. Please report this traceback at:
https://github.com/ranger/ranger/issues
|
LookupError
|
def sha512_encode(path, inode=None):
if inode is None:
inode = stat(path).st_ino
inode_path = "{0}{1}".format(str(inode), path)
if PY3:
inode_path = inode_path.encode("utf-8", "backslashescape")
return "{0}.jpg".format(sha512(inode_path).hexdigest())
|
def sha512_encode(path, inode=None):
if inode is None:
inode = stat(path).st_ino
sha = sha512("{0}{1}".format(path, str(inode)).encode("utf-8", "backslashescape"))
return "{0}.jpg".format(sha.hexdigest())
|
https://github.com/ranger/ranger/issues/2054
|
Current file: '/tmp/example_f\xc2\xa1le.txt'
Traceback (most recent call last):
File "/home/panta/programs/ranger/ranger/core/main.py", line 203, in main
fm.loop()
File "/home/panta/programs/ranger/ranger/core/fm.py", line 376, in loop
ui.redraw()
File "/home/panta/programs/ranger/ranger/gui/ui.py", line 343, in redraw
self.draw()
File "/home/panta/programs/ranger/ranger/gui/ui.py", line 370, in draw
DisplayableContainer.draw(self)
File "/home/panta/programs/ranger/ranger/gui/displayable.py", line 257, in draw
displayable.draw()
File "/home/panta/programs/ranger/ranger/gui/widgets/view_miller.py", line 100, in draw
DisplayableContainer.draw(self)
File "/home/panta/programs/ranger/ranger/gui/displayable.py", line 257, in draw
displayable.draw()
File "/home/panta/programs/ranger/ranger/gui/widgets/browsercolumn.py", line 187, in draw
self._draw_file()
File "/home/panta/programs/ranger/ranger/gui/widgets/browsercolumn.py", line 206, in _draw_file
path = self.target.get_preview_source(self.wid, self.hei)
File "/home/panta/programs/ranger/ranger/container/file.py", line 96, in get_preview_source
return self.fm.get_preview(self, width, height)
File "/home/panta/programs/ranger/ranger/core/actions.py", line 1102, in get_preview
self.sha512_encode(path, inode=fobj.stat.st_ino)
File "/home/panta/programs/ranger/ranger/core/actions.py", line 1028, in sha512_encode
"{0}{1}".format(path, str(inode)).encode('utf-8', 'backslashescape')
UnicodeDecodeError: 'ascii' codec can't decode byte 0xc2 in position 14: ordinal not in range(128)
|
UnicodeDecodeError
|
def enter_dir(self, path, history=True):
"""Enter given path"""
# TODO: Ensure that there is always a self.thisdir
if path is None:
return None
path = str(path)
# clear filter in the folder we're leaving
if self.fm.settings.clear_filters_on_dir_change and self.thisdir:
self.thisdir.filter = None
self.thisdir.refilter()
previous = self.thisdir
# get the absolute path
path = normpath(join(self.path, expanduser(path)))
selectfile = None
if not isdir(path):
selectfile = path
path = dirname(path)
new_thisdir = self.fm.get_directory(path)
try:
os.chdir(path)
except OSError:
return True
self.path = path
self.thisdir = new_thisdir
self.thisdir.load_content_if_outdated()
# build the pathway, a tuple of directory objects which lie
# on the path to the current directory.
if path == "/":
self.pathway = (self.fm.get_directory("/"),)
else:
pathway = []
currentpath = "/"
for comp in path.split("/"):
currentpath = join(currentpath, comp)
pathway.append(self.fm.get_directory(currentpath))
self.pathway = tuple(pathway)
self.assign_cursor_positions_for_subdirs()
# set the current file.
self.thisdir.sort_directories_first = self.fm.settings.sort_directories_first
self.thisdir.sort_reverse = self.fm.settings.sort_reverse
self.thisdir.sort_if_outdated()
if selectfile:
self.thisdir.move_to_obj(selectfile)
if previous and previous.path != path:
self.thisfile = self.thisdir.pointed_obj
else:
# This avoids setting self.pointer (through the 'move' signal) and
# is required so that you can use enter_dir when switching tabs
# without messing up the pointer.
self._thisfile = self.thisdir.pointed_obj
if history:
self.history.add(new_thisdir)
self.fm.signal_emit("cd", previous=previous, new=self.thisdir)
return True
|
def enter_dir(self, path, history=True):
"""Enter given path"""
# TODO: Ensure that there is always a self.thisdir
if path is None:
return None
path = str(path)
# clear filter in the folder we're leaving
if self.fm.settings.clear_filters_on_dir_change and self.thisdir:
self.thisdir.filter = None
self.thisdir.refilter()
previous = self.thisdir
# get the absolute path
path = normpath(join(self.path, expanduser(path)))
if not isdir(path):
return False
new_thisdir = self.fm.get_directory(path)
try:
os.chdir(path)
except OSError:
return True
self.path = path
self.thisdir = new_thisdir
self.thisdir.load_content_if_outdated()
# build the pathway, a tuple of directory objects which lie
# on the path to the current directory.
if path == "/":
self.pathway = (self.fm.get_directory("/"),)
else:
pathway = []
currentpath = "/"
for comp in path.split("/"):
currentpath = join(currentpath, comp)
pathway.append(self.fm.get_directory(currentpath))
self.pathway = tuple(pathway)
self.assign_cursor_positions_for_subdirs()
# set the current file.
self.thisdir.sort_directories_first = self.fm.settings.sort_directories_first
self.thisdir.sort_reverse = self.fm.settings.sort_reverse
self.thisdir.sort_if_outdated()
if previous and previous.path != path:
self.thisfile = self.thisdir.pointed_obj
else:
# This avoids setting self.pointer (through the 'move' signal) and
# is required so that you can use enter_dir when switching tabs
# without messing up the pointer.
self._thisfile = self.thisdir.pointed_obj
if history:
self.history.add(new_thisdir)
self.fm.signal_emit("cd", previous=previous, new=self.thisdir)
return True
|
https://github.com/ranger/ranger/issues/1386
|
$ ranger/ranger.py ~/.config/neofetch/config.conf
ranger version: ranger-master 1.9.2
Python version: 2.7.15+ (default, Oct 2 2018, 22:12:08) [GCC 8.2.0]
Locale: None.None
Traceback (most recent call last):
File "/home/techtonik/p/ranger/ranger/core/main.py", line 195, in main
fm.loop()
File "/home/techtonik/p/ranger/ranger/core/fm.py", line 398, in loop
ui.redraw()
File "/home/techtonik/p/ranger/ranger/gui/ui.py", line 342, in redraw
self.finalize()
File "/home/techtonik/p/ranger/ranger/gui/ui.py", line 397, in finalize
DisplayableContainer.finalize(self)
File "/home/techtonik/p/ranger/ranger/gui/displayable.py", line 264, in finalize
displayable.finalize()
File "/home/techtonik/p/ranger/ranger/gui/widgets/view_base.py", line 60, in finalize
- self.main_column.scroll_begin
AttributeError: 'NoneType' object has no attribute 'pointer'
ranger crashed. Please report this traceback at:
https://github.com/ranger/ranger/issues
|
AttributeError
|
def __init__(self, env=None, fm=None): # pylint: disable=super-init-not-called
self.keybuffer = KeyBuffer()
self.keymaps = KeyMaps(self.keybuffer)
self.redrawlock = threading.Event()
self.redrawlock.set()
self.titlebar = None
self._viewmode = None
self.taskview = None
self.status = None
self.console = None
self.pager = None
self.multiplexer = None
self._draw_title = None
self._tmux_automatic_rename = None
self._multiplexer_title = None
self._multiplexer_title = None
self.browser = None
if fm is not None:
self.fm = fm
|
def __init__(self, env=None, fm=None): # pylint: disable=super-init-not-called
self.keybuffer = KeyBuffer()
self.keymaps = KeyMaps(self.keybuffer)
self.redrawlock = threading.Event()
self.redrawlock.set()
self.titlebar = None
self._viewmode = None
self.taskview = None
self.status = None
self.console = None
self.pager = None
self.multiplexer = None
self._draw_title = None
self._tmux_automatic_rename = None
self._tmux_title = None
self._screen_title = None
self.browser = None
if fm is not None:
self.fm = fm
|
https://github.com/ranger/ranger/issues/1805
|
ranger version: ranger 1.9.3
Python version: 3.7.3 (default, Apr 3 2019, 05:39:12) [GCC 8.2.0]
Locale: en_US.UTF-8
Traceback (most recent call last):
File "/home/pi/.local/lib/python3.7/site-packages/ranger/core/main.py", line 171, in main
fm.initialize()
File "/home/pi/.local/lib/python3.7/site-packages/ranger/core/fm.py", line 132, in initialize
self.ui.initialize()
File "/home/pi/.local/lib/python3.7/site-packages/ranger/gui/ui.py", line 127, in initialize
self.handle_multiplexer()
File "/home/pi/.local/lib/python3.7/site-packages/ranger/gui/ui.py", line 500, in handle_multiplexer
['screen', '-Q', 'title']).strip()
File "/home/pi/.local/lib/python3.7/site-packages/ranger/ext/spawn.py", line 35, in check_output
process = Popen(popenargs, stderr=fd_devnull, **kwargs)
File "/usr/lib/python3.7/subprocess.py", line 775, in __init__
restore_signals, start_new_session)
File "/usr/lib/python3.7/subprocess.py", line 1522, in _execute_child
raise child_exception_type(errno_num, err_msg, err_filename)
FileNotFoundError: [Errno 2] No such file or directory: 'screen': 'screen'
ranger crashed. Please report this traceback at:
https://github.com/ranger/ranger/issu
|
FileNotFoundError
|
def handle_multiplexer(self):
if self.settings.update_tmux_title and not self._multiplexer_title:
try:
if _in_tmux():
# Stores the automatic-rename setting
# prints out a warning if allow-rename isn't set in tmux
try:
tmux_allow_rename = check_output(
["tmux", "show-window-options", "-v", "allow-rename"]
).strip()
except CalledProcessError:
tmux_allow_rename = "off"
if tmux_allow_rename == "off":
self.fm.notify("Warning: allow-rename not set in Tmux!", bad=True)
else:
self._multiplexer_title = check_output(
["tmux", "display-message", "-p", "#W"]
).strip()
self._tmux_automatic_rename = check_output(
["tmux", "show-window-options", "-v", "automatic-rename"]
).strip()
if self._tmux_automatic_rename == "on":
check_output(
["tmux", "set-window-option", "automatic-rename", "off"]
)
elif _in_screen():
# Stores the screen window name before renaming it
# gives out a warning if $TERM is not "screen"
self._multiplexer_title = check_output(
["screen", "-Q", "title"]
).strip()
except CalledProcessError:
self.fm.notify(
"Couldn't access previous multiplexer window"
" name, won't be able to restore.",
bad=False,
)
if not self._multiplexer_title:
self._multiplexer_title = os.environ.get("SHELL", "shell").split("/")[-1]
sys.stdout.write("\033kranger\033\\")
sys.stdout.flush()
|
def handle_multiplexer(self):
if self.settings.update_tmux_title:
if "TMUX" in os.environ:
# Stores the automatic-rename setting
# prints out a warning if the allow-rename in tmux is not set
tmux_allow_rename = check_output(
["tmux", "show-window-options", "-v", "allow-rename"]
).strip()
if tmux_allow_rename == "off":
self.fm.notify("Warning: allow-rename not set in Tmux!", bad=True)
elif self._tmux_title is None:
self._tmux_title = check_output(
["tmux", "display-message", "-p", "#W"]
).strip()
else:
try:
self._tmux_automatic_rename = check_output(
["tmux", "show-window-options", "-v", "automatic-rename"]
).strip()
if self._tmux_automatic_rename == "on":
check_output(
["tmux", "set-window-option", "automatic-rename", "off"]
)
except CalledProcessError:
pass
elif "screen" in os.environ["TERM"] and self._screen_title is None:
# Stores the screen window name before renaming it
# gives out a warning if $TERM is not "screen"
try:
self._screen_title = check_output(
["screen", "-Q", "title"], shell=True
).strip()
except CalledProcessError:
self._screen_title = None
sys.stdout.write("\033kranger\033\\")
sys.stdout.flush()
|
https://github.com/ranger/ranger/issues/1805
|
ranger version: ranger 1.9.3
Python version: 3.7.3 (default, Apr 3 2019, 05:39:12) [GCC 8.2.0]
Locale: en_US.UTF-8
Traceback (most recent call last):
File "/home/pi/.local/lib/python3.7/site-packages/ranger/core/main.py", line 171, in main
fm.initialize()
File "/home/pi/.local/lib/python3.7/site-packages/ranger/core/fm.py", line 132, in initialize
self.ui.initialize()
File "/home/pi/.local/lib/python3.7/site-packages/ranger/gui/ui.py", line 127, in initialize
self.handle_multiplexer()
File "/home/pi/.local/lib/python3.7/site-packages/ranger/gui/ui.py", line 500, in handle_multiplexer
['screen', '-Q', 'title']).strip()
File "/home/pi/.local/lib/python3.7/site-packages/ranger/ext/spawn.py", line 35, in check_output
process = Popen(popenargs, stderr=fd_devnull, **kwargs)
File "/usr/lib/python3.7/subprocess.py", line 775, in __init__
restore_signals, start_new_session)
File "/usr/lib/python3.7/subprocess.py", line 1522, in _execute_child
raise child_exception_type(errno_num, err_msg, err_filename)
FileNotFoundError: [Errno 2] No such file or directory: 'screen': 'screen'
ranger crashed. Please report this traceback at:
https://github.com/ranger/ranger/issu
|
FileNotFoundError
|
def restore_multiplexer_name(self):
if self._multiplexer_title:
try:
if _in_tmux():
if self._tmux_automatic_rename:
check_output(
[
"tmux",
"set-window-option",
"automatic-rename",
self._tmux_automatic_rename,
]
)
else:
check_output(
["tmux", "set-window-option", "-u", "automatic-rename"]
)
except CalledProcessError:
self.fm.notify("Could not restore multiplexer window name!", bad=True)
sys.stdout.write("\033k{}\033\\".format(self._multiplexer_title))
sys.stdout.flush()
|
def restore_multiplexer_name(self):
try:
if "TMUX" in os.environ:
if self._tmux_automatic_rename:
check_output(
[
"tmux",
"set-window-option",
"automatic-rename",
self._tmux_automatic_rename,
]
)
else:
check_output(["tmux", "set-window-option", "-u", "automatic-rename"])
if self._tmux_title:
check_output(["tmux", "rename-window", self._tmux_title])
elif "screen" in os.environ["TERM"] and self._screen_title:
check_output(["screen", "-X", "title", self._screen_title])
except CalledProcessError:
self.fm.notify("Could not restore window-name!", bad=True)
|
https://github.com/ranger/ranger/issues/1805
|
ranger version: ranger 1.9.3
Python version: 3.7.3 (default, Apr 3 2019, 05:39:12) [GCC 8.2.0]
Locale: en_US.UTF-8
Traceback (most recent call last):
File "/home/pi/.local/lib/python3.7/site-packages/ranger/core/main.py", line 171, in main
fm.initialize()
File "/home/pi/.local/lib/python3.7/site-packages/ranger/core/fm.py", line 132, in initialize
self.ui.initialize()
File "/home/pi/.local/lib/python3.7/site-packages/ranger/gui/ui.py", line 127, in initialize
self.handle_multiplexer()
File "/home/pi/.local/lib/python3.7/site-packages/ranger/gui/ui.py", line 500, in handle_multiplexer
['screen', '-Q', 'title']).strip()
File "/home/pi/.local/lib/python3.7/site-packages/ranger/ext/spawn.py", line 35, in check_output
process = Popen(popenargs, stderr=fd_devnull, **kwargs)
File "/usr/lib/python3.7/subprocess.py", line 775, in __init__
restore_signals, start_new_session)
File "/usr/lib/python3.7/subprocess.py", line 1522, in _execute_child
raise child_exception_type(errno_num, err_msg, err_filename)
FileNotFoundError: [Errno 2] No such file or directory: 'screen': 'screen'
ranger crashed. Please report this traceback at:
https://github.com/ranger/ranger/issu
|
FileNotFoundError
|
def main(
# pylint: disable=too-many-locals,too-many-return-statements
# pylint: disable=too-many-branches,too-many-statements
):
"""initialize objects and run the filemanager"""
import ranger.api
from ranger.container.settings import Settings
from ranger.core.shared import FileManagerAware, SettingsAware
from ranger.core.fm import FM
from ranger.ext.logutils import setup_logging
from ranger.ext.openstruct import OpenStruct
ranger.args = args = parse_arguments()
ranger.arg = OpenStruct(args.__dict__) # COMPAT
setup_logging(debug=args.debug, logfile=args.logfile)
for line in VERSION_MSG:
LOG.info(line)
LOG.info("Process ID: %s", os.getpid())
try:
locale.setlocale(locale.LC_ALL, "")
except locale.Error:
print("Warning: Unable to set locale. Expect encoding problems.")
# so that programs can know that ranger spawned them:
level = "RANGER_LEVEL"
if level in os.environ and os.environ[level].isdigit():
os.environ[level] = str(int(os.environ[level]) + 1)
else:
os.environ[level] = "1"
if "SHELL" not in os.environ:
os.environ["SHELL"] = "sh"
LOG.debug("cache dir: '%s'", args.cachedir)
LOG.debug("config dir: '%s'", args.confdir)
LOG.debug("data dir: '%s'", args.datadir)
if args.copy_config is not None:
fm = FM()
fm.copy_config_files(args.copy_config)
return 0
if args.list_tagged_files:
if args.clean:
print("Can't access tag data in clean mode", file=sys.stderr)
return 1
fm = FM()
try:
if sys.version_info[0] >= 3:
fobj = open(fm.datapath("tagged"), "r", errors="replace")
else:
fobj = open(fm.datapath("tagged"), "r")
except OSError as ex:
print("Unable to open `tagged` data file: {0}".format(ex), file=sys.stderr)
return 1
for line in fobj.readlines():
if len(line) > 2 and line[1] == ":":
if line[0] in args.list_tagged_files:
sys.stdout.write(line[2:])
elif line and "*" in args.list_tagged_files:
sys.stdout.write(line)
return 0
SettingsAware.settings_set(Settings())
if args.selectfile:
args.selectfile = os.path.abspath(args.selectfile)
args.paths.insert(0, os.path.dirname(args.selectfile))
paths = get_paths(args)
paths_inaccessible = []
for path in paths:
try:
path_abs = os.path.abspath(path)
except OSError:
paths_inaccessible += [path]
continue
if not os.access(path_abs, os.F_OK):
paths_inaccessible += [path]
if paths_inaccessible:
print("Inaccessible paths: {0}".format(paths), file=sys.stderr)
return 1
profile = None
exit_msg = ""
exit_code = 0
try: # pylint: disable=too-many-nested-blocks
# Initialize objects
fm = FM(paths=paths)
FileManagerAware.fm_set(fm)
load_settings(fm, args.clean)
if args.show_only_dirs:
from ranger.container.directory import InodeFilterConstants
fm.settings.global_inode_type_filter = InodeFilterConstants.DIRS
if args.list_unused_keys:
from ranger.ext.keybinding_parser import special_keys, reversed_special_keys
maps = fm.ui.keymaps["browser"]
for key in sorted(special_keys.values(), key=str):
if key not in maps:
print("<%s>" % reversed_special_keys[key])
for key in range(33, 127):
if key not in maps:
print(chr(key))
return 0
if not sys.stdin.isatty():
sys.stderr.write("Error: Must run ranger from terminal\n")
raise SystemExit(1)
if fm.username == "root":
fm.settings.preview_files = False
fm.settings.use_preview_script = False
LOG.info("Running as root, disabling the file previews.")
if not args.debug:
from ranger.ext import curses_interrupt_handler
curses_interrupt_handler.install_interrupt_handler()
# Create cache directory
if fm.settings.preview_images and fm.settings.use_preview_script:
if not os.path.exists(args.cachedir):
os.makedirs(args.cachedir)
if not args.clean:
# Create data directory
if not os.path.exists(args.datadir):
os.makedirs(args.datadir)
# Restore saved tabs
tabs_datapath = fm.datapath("tabs")
if (
fm.settings.save_tabs_on_exit
and os.path.exists(tabs_datapath)
and not args.paths
):
try:
with open(tabs_datapath, "r") as fobj:
tabs_saved = fobj.read().partition("\0\0")
fm.start_paths += tabs_saved[0].split("\0")
if tabs_saved[-1]:
with open(tabs_datapath, "w") as fobj:
fobj.write(tabs_saved[-1])
else:
os.remove(tabs_datapath)
except OSError as ex:
LOG.error("Unable to restore saved tabs")
LOG.exception(ex)
# Run the file manager
fm.initialize()
ranger.api.hook_init(fm)
fm.ui.initialize()
if args.selectfile:
fm.select_file(args.selectfile)
if args.cmd:
for command in args.cmd:
fm.execute_console(command)
if ranger.args.profile:
import cProfile
import pstats
ranger.__fm = fm # pylint: disable=protected-access
profile_file = tempfile.gettempdir() + "/ranger_profile"
cProfile.run("ranger.__fm.loop()", profile_file)
profile = pstats.Stats(profile_file, stream=sys.stderr)
else:
fm.loop()
except Exception: # pylint: disable=broad-except
import traceback
ex_traceback = traceback.format_exc()
exit_msg += "\n".join(VERSION_MSG) + "\n"
try:
exit_msg += "Current file: {0}\n".format(repr(fm.thisfile.path))
except Exception: # pylint: disable=broad-except
pass
exit_msg += """
{0}
ranger crashed. Please report this traceback at:
https://github.com/ranger/ranger/issues
""".format(ex_traceback)
exit_code = 1
except SystemExit as ex:
if ex.code is not None:
if not isinstance(ex.code, int):
exit_msg = ex.code
exit_code = 1
else:
exit_code = ex.code
finally:
if exit_msg:
LOG.critical(exit_msg)
try:
fm.ui.destroy()
except (AttributeError, NameError):
pass
# If profiler is enabled print the stats
if ranger.args.profile and profile:
profile.strip_dirs().sort_stats("cumulative").print_callees()
# print the exit message if any
if exit_msg:
sys.stderr.write(exit_msg)
return exit_code # pylint: disable=lost-exception
|
def main(
# pylint: disable=too-many-locals,too-many-return-statements
# pylint: disable=too-many-branches,too-many-statements
):
"""initialize objects and run the filemanager"""
import ranger.api
from ranger.container.settings import Settings
from ranger.core.shared import FileManagerAware, SettingsAware
from ranger.core.fm import FM
from ranger.ext.logutils import setup_logging
from ranger.ext.openstruct import OpenStruct
ranger.args = args = parse_arguments()
ranger.arg = OpenStruct(args.__dict__) # COMPAT
setup_logging(debug=args.debug, logfile=args.logfile)
for line in VERSION_MSG:
LOG.info(line)
LOG.info("Process ID: %s", os.getpid())
try:
locale.setlocale(locale.LC_ALL, "")
except locale.Error:
print("Warning: Unable to set locale. Expect encoding problems.")
# so that programs can know that ranger spawned them:
level = "RANGER_LEVEL"
if level in os.environ and os.environ[level].isdigit():
os.environ[level] = str(int(os.environ[level]) + 1)
else:
os.environ[level] = "1"
if "SHELL" not in os.environ:
os.environ["SHELL"] = "sh"
LOG.debug("cache dir: '%s'", args.cachedir)
LOG.debug("config dir: '%s'", args.confdir)
LOG.debug("data dir: '%s'", args.datadir)
if args.copy_config is not None:
fm = FM()
fm.copy_config_files(args.copy_config)
return 0
if args.list_tagged_files:
if args.clean:
print("Can't access tag data in clean mode", file=sys.stderr)
return 1
fm = FM()
try:
if sys.version_info[0] >= 3:
fobj = open(fm.datapath("tagged"), "r", errors="replace")
else:
fobj = open(fm.datapath("tagged"), "r")
except OSError as ex:
print("Unable to open `tagged` data file: {0}".format(ex), file=sys.stderr)
return 1
for line in fobj.readlines():
if len(line) > 2 and line[1] == ":":
if line[0] in args.list_tagged_files:
sys.stdout.write(line[2:])
elif line and "*" in args.list_tagged_files:
sys.stdout.write(line)
return 0
SettingsAware.settings_set(Settings())
if args.selectfile:
args.selectfile = os.path.abspath(args.selectfile)
args.paths.insert(0, os.path.dirname(args.selectfile))
paths = __get_paths(args)
paths_inaccessible = []
for path in paths:
try:
path_abs = os.path.abspath(path)
except OSError:
paths_inaccessible += [path]
continue
if not os.access(path_abs, os.F_OK):
paths_inaccessible += [path]
if paths_inaccessible:
print("Inaccessible paths: {0}".format(paths), file=sys.stderr)
return 1
profile = None
exit_msg = ""
exit_code = 0
try: # pylint: disable=too-many-nested-blocks
# Initialize objects
fm = FM(paths=paths)
FileManagerAware.fm_set(fm)
load_settings(fm, args.clean)
if args.show_only_dirs:
from ranger.container.directory import InodeFilterConstants
fm.settings.global_inode_type_filter = InodeFilterConstants.DIRS
if args.list_unused_keys:
from ranger.ext.keybinding_parser import special_keys, reversed_special_keys
maps = fm.ui.keymaps["browser"]
for key in sorted(special_keys.values(), key=str):
if key not in maps:
print("<%s>" % reversed_special_keys[key])
for key in range(33, 127):
if key not in maps:
print(chr(key))
return 0
if not sys.stdin.isatty():
sys.stderr.write("Error: Must run ranger from terminal\n")
raise SystemExit(1)
if fm.username == "root":
fm.settings.preview_files = False
fm.settings.use_preview_script = False
LOG.info("Running as root, disabling the file previews.")
if not args.debug:
from ranger.ext import curses_interrupt_handler
curses_interrupt_handler.install_interrupt_handler()
# Create cache directory
if fm.settings.preview_images and fm.settings.use_preview_script:
if not os.path.exists(args.cachedir):
os.makedirs(args.cachedir)
if not args.clean:
# Create data directory
if not os.path.exists(args.datadir):
os.makedirs(args.datadir)
# Restore saved tabs
tabs_datapath = fm.datapath("tabs")
if (
fm.settings.save_tabs_on_exit
and os.path.exists(tabs_datapath)
and not args.paths
):
try:
with open(tabs_datapath, "r") as fobj:
tabs_saved = fobj.read().partition("\0\0")
fm.start_paths += tabs_saved[0].split("\0")
if tabs_saved[-1]:
with open(tabs_datapath, "w") as fobj:
fobj.write(tabs_saved[-1])
else:
os.remove(tabs_datapath)
except OSError as ex:
LOG.error("Unable to restore saved tabs")
LOG.exception(ex)
# Run the file manager
fm.initialize()
ranger.api.hook_init(fm)
fm.ui.initialize()
if args.selectfile:
fm.select_file(args.selectfile)
if args.cmd:
for command in args.cmd:
fm.execute_console(command)
if ranger.args.profile:
import cProfile
import pstats
ranger.__fm = fm # pylint: disable=protected-access
profile_file = tempfile.gettempdir() + "/ranger_profile"
cProfile.run("ranger.__fm.loop()", profile_file)
profile = pstats.Stats(profile_file, stream=sys.stderr)
else:
fm.loop()
except Exception: # pylint: disable=broad-except
import traceback
ex_traceback = traceback.format_exc()
exit_msg += "\n".join(VERSION_MSG) + "\n"
try:
exit_msg += "Current file: {0}\n".format(repr(fm.thisfile.path))
except Exception: # pylint: disable=broad-except
pass
exit_msg += """
{0}
ranger crashed. Please report this traceback at:
https://github.com/ranger/ranger/issues
""".format(ex_traceback)
exit_code = 1
except SystemExit as ex:
if ex.code is not None:
if not isinstance(ex.code, int):
exit_msg = ex.code
exit_code = 1
else:
exit_code = ex.code
finally:
if exit_msg:
LOG.critical(exit_msg)
try:
fm.ui.destroy()
except (AttributeError, NameError):
pass
# If profiler is enabled print the stats
if ranger.args.profile and profile:
profile.strip_dirs().sort_stats("cumulative").print_callees()
# print the exit message if any
if exit_msg:
sys.stderr.write(exit_msg)
return exit_code # pylint: disable=lost-exception
|
https://github.com/ranger/ranger/issues/1052
|
shell-init: error retrieving current directory: getcwd: cannot access parent directories: No such file or directory
shell-init: error retrieving current directory: getcwd: cannot access parent directories: No such file or directory
Traceback (most recent call last):
File "/github/ranger/ranger.py", line 36, in <module>
sys.exit(ranger.main()) # pylint: disable=no-member
File "/github/ranger/ranger/core/main.py", line 99, in main
paths = [os.environ.get('PWD', os.getcwd())]
OSError: [Errno 2] No such file or directory
|
OSError
|
def data_status_root(self):
statuses = set()
# Paths with status
lines = self._run(["status"]).split("\n")
lines = list(filter(None, lines))
if not lines:
return "sync"
for line in lines:
code = line[0]
if code == " ":
continue
statuses.add(self._status_translate(code))
for status in self.DIRSTATUSES:
if status in statuses:
return status
return "sync"
|
def data_status_root(self):
statuses = set()
# Paths with status
lines = self._run(["status"]).split("\n")
if not lines:
return "sync"
for line in lines:
code = line[0]
if code == " ":
continue
statuses.add(self._status_translate(code))
for status in self.DIRSTATUSES:
if status in statuses:
return status
return "sync"
|
https://github.com/ranger/ranger/issues/1296
|
Message Log:
11:12:54 INFO ranger version: ranger-master 1.9.1
11:12:54 INFO Python version: 3.6.5 (default, Aug 9 2018, 08:22:49) [GCC 5.5.0]
11:12:54 INFO Locale: en_GB.UTF-8
11:12:54 INFO Process ID: 27126
11:12:56 ERRO Notification: VCS Exception: View log for more info
11:12:56 ERRO string index out of range
Traceback (most recent call last):
File "/usr/lib64/python3.6/site-packages/ranger/ext/vcs/vcs.py", line 472, in run
self._queue_process()
File "/usr/lib64/python3.6/site-packages/ranger/ext/vcs/vcs.py", line 452, in _queue_process
has_vcschild = self._update_subroots(dirobj.files_all)
File "/usr/lib64/python3.6/site-packages/ranger/ext/vcs/vcs.py", line 414, in _update_subroots
if not rootvcs.init_root():
File "/usr/lib64/python3.6/site-packages/ranger/ext/vcs/vcs.py", line 237, in init_root
self.obj.vcsstatus = self.data_status_root()
File "/usr/lib64/python3.6/site-packages/ranger/ext/vcs/svn.py", line 104, in data_status_root
code = line[0]
IndexError: string index out of range
|
IndexError
|
def data_status_subpaths(self):
statuses = {}
# Paths with status
lines = self._run(["status"]).split("\n")
lines = list(filter(None, lines))
for line in lines:
code, path = line[0], line[8:]
if code == " ":
continue
statuses[os.path.normpath(path)] = self._status_translate(code)
return statuses
|
def data_status_subpaths(self):
statuses = {}
# Paths with status
lines = self._run(["status"]).split("\n")
for line in lines:
code, path = line[0], line[8:]
if code == " ":
continue
statuses[os.path.normpath(path)] = self._status_translate(code)
return statuses
|
https://github.com/ranger/ranger/issues/1296
|
Message Log:
11:12:54 INFO ranger version: ranger-master 1.9.1
11:12:54 INFO Python version: 3.6.5 (default, Aug 9 2018, 08:22:49) [GCC 5.5.0]
11:12:54 INFO Locale: en_GB.UTF-8
11:12:54 INFO Process ID: 27126
11:12:56 ERRO Notification: VCS Exception: View log for more info
11:12:56 ERRO string index out of range
Traceback (most recent call last):
File "/usr/lib64/python3.6/site-packages/ranger/ext/vcs/vcs.py", line 472, in run
self._queue_process()
File "/usr/lib64/python3.6/site-packages/ranger/ext/vcs/vcs.py", line 452, in _queue_process
has_vcschild = self._update_subroots(dirobj.files_all)
File "/usr/lib64/python3.6/site-packages/ranger/ext/vcs/vcs.py", line 414, in _update_subroots
if not rootvcs.init_root():
File "/usr/lib64/python3.6/site-packages/ranger/ext/vcs/vcs.py", line 237, in init_root
self.obj.vcsstatus = self.data_status_root()
File "/usr/lib64/python3.6/site-packages/ranger/ext/vcs/svn.py", line 104, in data_status_root
code = line[0]
IndexError: string index out of range
|
IndexError
|
def main(
# pylint: disable=too-many-locals,too-many-return-statements
# pylint: disable=too-many-branches,too-many-statements
):
"""initialize objects and run the filemanager"""
import ranger.api
from ranger.container.settings import Settings
from ranger.core.shared import FileManagerAware, SettingsAware
from ranger.core.fm import FM
from ranger.ext.logutils import setup_logging
from ranger.ext.openstruct import OpenStruct
ranger.args = args = parse_arguments()
ranger.arg = OpenStruct(args.__dict__) # COMPAT
setup_logging(debug=args.debug, logfile=args.logfile)
for line in VERSION_MSG:
LOG.info(line)
LOG.info("Process ID: %s", os.getpid())
try:
locale.setlocale(locale.LC_ALL, "")
except locale.Error:
print("Warning: Unable to set locale. Expect encoding problems.")
# so that programs can know that ranger spawned them:
level = "RANGER_LEVEL"
if level in os.environ and os.environ[level].isdigit():
os.environ[level] = str(int(os.environ[level]) + 1)
else:
os.environ[level] = "1"
if "SHELL" not in os.environ:
os.environ["SHELL"] = "sh"
LOG.debug("cache dir: '%s'", args.cachedir)
LOG.debug("config dir: '%s'", args.confdir)
LOG.debug("data dir: '%s'", args.datadir)
if args.copy_config is not None:
fm = FM()
fm.copy_config_files(args.copy_config)
return 0
if args.list_tagged_files:
if args.clean:
print("Can't access tag data in clean mode", file=sys.stderr)
return 1
fm = FM()
try:
if sys.version_info[0] >= 3:
fobj = open(fm.datapath("tagged"), "r", errors="replace")
else:
fobj = open(fm.datapath("tagged"), "r")
except OSError as ex:
print("Unable to open `tagged` data file: {0}".format(ex), file=sys.stderr)
return 1
for line in fobj.readlines():
if len(line) > 2 and line[1] == ":":
if line[0] in args.list_tagged_files:
sys.stdout.write(line[2:])
elif line and "*" in args.list_tagged_files:
sys.stdout.write(line)
return 0
SettingsAware.settings_set(Settings())
if args.selectfile:
args.selectfile = os.path.abspath(args.selectfile)
args.paths.insert(0, os.path.dirname(args.selectfile))
if args.paths:
paths = [p[7:] if p.startswith("file:///") else p for p in args.paths]
else:
paths = [os.environ.get("PWD", os.getcwd())]
paths_inaccessible = []
for path in paths:
try:
path_abs = os.path.abspath(path)
except OSError:
paths_inaccessible += [path]
continue
if not os.access(path_abs, os.F_OK):
paths_inaccessible += [path]
if paths_inaccessible:
print("Inaccessible paths: {0}".format(paths), file=sys.stderr)
return 1
profile = None
exit_msg = ""
exit_code = 0
try: # pylint: disable=too-many-nested-blocks
# Initialize objects
fm = FM(paths=paths)
FileManagerAware.fm_set(fm)
load_settings(fm, args.clean)
if args.show_only_dirs:
from ranger.container.directory import InodeFilterConstants
fm.settings.global_inode_type_filter = InodeFilterConstants.DIRS
if args.list_unused_keys:
from ranger.ext.keybinding_parser import special_keys, reversed_special_keys
maps = fm.ui.keymaps["browser"]
for key in sorted(special_keys.values(), key=str):
if key not in maps:
print("<%s>" % reversed_special_keys[key])
for key in range(33, 127):
if key not in maps:
print(chr(key))
return 0
if not sys.stdin.isatty():
sys.stderr.write("Error: Must run ranger from terminal\n")
raise SystemExit(1)
if fm.username == "root":
fm.settings.preview_files = False
fm.settings.use_preview_script = False
LOG.info("Running as root, disabling the file previews.")
if not args.debug:
from ranger.ext import curses_interrupt_handler
curses_interrupt_handler.install_interrupt_handler()
# Create cache directory
if fm.settings.preview_images and fm.settings.use_preview_script:
if not os.path.exists(args.cachedir):
os.makedirs(args.cachedir)
if not args.clean:
# Create data directory
if not os.path.exists(args.datadir):
os.makedirs(args.datadir)
# Restore saved tabs
tabs_datapath = fm.datapath("tabs")
if (
fm.settings.save_tabs_on_exit
and os.path.exists(tabs_datapath)
and not args.paths
):
try:
with open(tabs_datapath, "r") as fobj:
tabs_saved = fobj.read().partition("\0\0")
fm.start_paths += tabs_saved[0].split("\0")
if tabs_saved[-1]:
with open(tabs_datapath, "w") as fobj:
fobj.write(tabs_saved[-1])
else:
os.remove(tabs_datapath)
except OSError as ex:
LOG.error("Unable to restore saved tabs")
LOG.exception(ex)
# Run the file manager
fm.initialize()
ranger.api.hook_init(fm)
fm.ui.initialize()
if args.selectfile:
fm.select_file(args.selectfile)
if args.cmd:
fm.enter_dir(fm.thistab.path)
for command in args.cmd:
fm.execute_console(command)
if ranger.args.profile:
import cProfile
import pstats
ranger.__fm = fm # pylint: disable=protected-access
profile_file = tempfile.gettempdir() + "/ranger_profile"
cProfile.run("ranger.__fm.loop()", profile_file)
profile = pstats.Stats(profile_file, stream=sys.stderr)
else:
fm.loop()
except Exception: # pylint: disable=broad-except
import traceback
ex_traceback = traceback.format_exc()
exit_msg += "\n".join(VERSION_MSG) + "\n"
try:
exit_msg += "Current file: {0}\n".format(repr(fm.thisfile.path))
except Exception: # pylint: disable=broad-except
pass
exit_msg += """
{0}
ranger crashed. Please report this traceback at:
https://github.com/ranger/ranger/issues
""".format(ex_traceback)
exit_code = 1
except SystemExit as ex:
if ex.code is not None:
if not isinstance(ex.code, int):
exit_msg = ex.code
exit_code = 1
else:
exit_code = ex.code
finally:
if exit_msg:
LOG.critical(exit_msg)
try:
fm.ui.destroy()
except (AttributeError, NameError):
pass
# If profiler is enabled print the stats
if ranger.args.profile and profile:
profile.strip_dirs().sort_stats("cumulative").print_callees()
# print the exit message if any
if exit_msg:
sys.stderr.write(exit_msg)
return exit_code # pylint: disable=lost-exception
|
def main(
# pylint: disable=too-many-locals,too-many-return-statements
# pylint: disable=too-many-branches,too-many-statements
):
"""initialize objects and run the filemanager"""
import ranger.api
from ranger.container.settings import Settings
from ranger.core.shared import FileManagerAware, SettingsAware
from ranger.core.fm import FM
from ranger.ext.logutils import setup_logging
from ranger.ext.openstruct import OpenStruct
ranger.args = args = parse_arguments()
ranger.arg = OpenStruct(args.__dict__) # COMPAT
setup_logging(debug=args.debug, logfile=args.logfile)
for line in VERSION_MSG:
LOG.info(line)
LOG.info("Process ID: %s", os.getpid())
try:
locale.setlocale(locale.LC_ALL, "")
except locale.Error:
print("Warning: Unable to set locale. Expect encoding problems.")
# so that programs can know that ranger spawned them:
level = "RANGER_LEVEL"
if level in os.environ and os.environ[level].isdigit():
os.environ[level] = str(int(os.environ[level]) + 1)
else:
os.environ[level] = "1"
if "SHELL" not in os.environ:
os.environ["SHELL"] = "sh"
LOG.debug("cache dir: '%s'", args.cachedir)
LOG.debug("config dir: '%s'", args.confdir)
LOG.debug("data dir: '%s'", args.datadir)
if args.copy_config is not None:
fm = FM()
fm.copy_config_files(args.copy_config)
return 0
if args.list_tagged_files:
if args.clean:
print("Can't access tag data in clean mode", file=sys.stderr)
return 1
fm = FM()
try:
if sys.version_info[0] >= 3:
fobj = open(fm.datapath("tagged"), "r", errors="replace")
else:
fobj = open(fm.datapath("tagged"), "r")
except OSError as ex:
print("Unable to open `tagged` data file: {0}".format(ex), file=sys.stderr)
return 1
for line in fobj.readlines():
if len(line) > 2 and line[1] == ":":
if line[0] in args.list_tagged_files:
sys.stdout.write(line[2:])
elif line and "*" in args.list_tagged_files:
sys.stdout.write(line)
return 0
SettingsAware.settings_set(Settings())
if args.selectfile:
args.selectfile = os.path.abspath(args.selectfile)
args.paths.insert(0, os.path.dirname(args.selectfile))
if args.paths:
paths = [p[7:] if p.startswith("file:///") else p for p in args.paths]
else:
paths = [os.environ.get("PWD", os.getcwd())]
paths_inaccessible = []
for path in paths:
try:
path_abs = os.path.abspath(path)
except OSError:
paths_inaccessible += [path]
continue
if not os.access(path_abs, os.F_OK):
paths_inaccessible += [path]
if paths_inaccessible:
print("Inaccessible paths: {0}".format(paths), file=sys.stderr)
return 1
profile = None
exit_msg = ""
exit_code = 0
try: # pylint: disable=too-many-nested-blocks
# Initialize objects
fm = FM(paths=paths)
FileManagerAware.fm_set(fm)
load_settings(fm, args.clean)
if args.show_only_dirs:
from ranger.container.directory import InodeFilterConstants
fm.settings.global_inode_type_filter = InodeFilterConstants.DIRS
if args.list_unused_keys:
from ranger.ext.keybinding_parser import special_keys, reversed_special_keys
maps = fm.ui.keymaps["browser"]
for key in sorted(special_keys.values(), key=str):
if key not in maps:
print("<%s>" % reversed_special_keys[key])
for key in range(33, 127):
if key not in maps:
print(chr(key))
return 0
if not sys.stdin.isatty():
sys.stderr.write("Error: Must run ranger from terminal\n")
raise SystemExit(1)
if fm.username == "root":
fm.settings.preview_files = False
fm.settings.use_preview_script = False
LOG.info("Running as root, disabling the file previews.")
if not args.debug:
from ranger.ext import curses_interrupt_handler
curses_interrupt_handler.install_interrupt_handler()
# Create cache directory
if fm.settings.preview_images and fm.settings.use_preview_script:
if not os.path.exists(args.cachedir):
os.makedirs(args.cachedir)
if not args.clean:
# Create data directory
if not os.path.exists(args.datadir):
os.makedirs(args.datadir)
# Restore saved tabs
tabs_datapath = fm.datapath("tabs")
if (
fm.settings.save_tabs_on_exit
and os.path.exists(tabs_datapath)
and not args.paths
):
try:
with open(tabs_datapath, "r") as fobj:
tabs_saved = fobj.read().partition("\0\0")
fm.start_paths += tabs_saved[0].split("\0")
if tabs_saved[-1]:
with open(tabs_datapath, "w") as fobj:
fobj.write(tabs_saved[-1])
else:
os.remove(tabs_datapath)
except OSError as ex:
LOG.error("Unable to restore saved tabs")
LOG.exception(ex)
# Run the file manager
fm.initialize()
ranger.api.hook_init(fm)
fm.ui.initialize()
if args.selectfile:
fm.select_file(args.selectfile)
if args.cmd:
for command in args.cmd:
fm.execute_console(command)
if ranger.args.profile:
import cProfile
import pstats
ranger.__fm = fm # pylint: disable=protected-access
profile_file = tempfile.gettempdir() + "/ranger_profile"
cProfile.run("ranger.__fm.loop()", profile_file)
profile = pstats.Stats(profile_file, stream=sys.stderr)
else:
fm.loop()
except Exception: # pylint: disable=broad-except
import traceback
ex_traceback = traceback.format_exc()
exit_msg += "\n".join(VERSION_MSG) + "\n"
try:
exit_msg += "Current file: {0}\n".format(repr(fm.thisfile.path))
except Exception: # pylint: disable=broad-except
pass
exit_msg += """
{0}
ranger crashed. Please report this traceback at:
https://github.com/ranger/ranger/issues
""".format(ex_traceback)
exit_code = 1
except SystemExit as ex:
if ex.code is not None:
if not isinstance(ex.code, int):
exit_msg = ex.code
exit_code = 1
else:
exit_code = ex.code
finally:
if exit_msg:
LOG.critical(exit_msg)
try:
fm.ui.destroy()
except (AttributeError, NameError):
pass
# If profiler is enabled print the stats
if ranger.args.profile and profile:
profile.strip_dirs().sort_stats("cumulative").print_callees()
# print the exit message if any
if exit_msg:
sys.stderr.write(exit_msg)
return exit_code # pylint: disable=lost-exception
|
https://github.com/ranger/ranger/issues/1137
|
16:15:22 ERRO Notification: 'NoneType' object has no attribute 'unload'
16:15:22 ERRO 'NoneType' object has no attribute 'unload'
Traceback (most recent call last):
File "/usr/lib/python3.6/site-packages/ranger/core/actions.py", line 251, in execute_console
cmd.execute()
File "/usr/lib/python3.6/site-packages/ranger/config/commands.py", line 1574, in execute
self.fm.thisdir.unload()
AttributeError: 'NoneType' object has no attribute 'unload'
|
AttributeError
|
def _get_best_study_config(self):
metadata = {
"best_trial_number": self.study.best_trial.number,
"best_trial_evaluation": self.study.best_value,
}
pipeline_config = dict()
for k, v in self.study.user_attrs.items():
if k.startswith("pykeen_"):
metadata[k[len("pykeen_") :]] = v
elif k in {"metric"}:
continue
else:
pipeline_config[k] = v
for field in dataclasses.fields(self.objective):
field_value = getattr(self.objective, field.name)
if not field_value:
continue
if field.name.endswith("_kwargs"):
logger.debug(
f"saving pre-specified field in pipeline config: {field.name}={field_value}"
)
pipeline_config[field.name] = field_value
elif field.name in {"training", "testing", "validation"}:
pipeline_config[field.name] = (
field_value if isinstance(field_value, str) else USER_DEFINED_CODE
)
for k, v in self.study.best_params.items():
sk, ssk = k.split(".")
sk = f"{sk}_kwargs"
if sk not in pipeline_config:
pipeline_config[sk] = {}
logger.debug(f"saving optimized field in pipeline config: {sk}.{ssk}={v}")
pipeline_config[sk][ssk] = v
for k in ("stopper", "stopper_kwargs"):
if k in pipeline_config:
v = pipeline_config.pop(k)
metadata[f"_{k}_removed_comment"] = f"{k} config removed after HPO: {v}"
stopped_epoch = self.study.best_trial.user_attrs.get(STOPPED_EPOCH_KEY)
if stopped_epoch is not None:
old_num_epochs = pipeline_config["training_kwargs"]["num_epochs"]
metadata["_stopper_comment"] = (
f"While the original config had {old_num_epochs},"
f" early stopping will now switch it to {int(stopped_epoch)}"
)
pipeline_config["training_kwargs"]["num_epochs"] = int(stopped_epoch)
return dict(metadata=metadata, pipeline=pipeline_config)
|
def _get_best_study_config(self):
metadata = {
"best_trial_number": self.study.best_trial.number,
"best_trial_evaluation": self.study.best_value,
}
pipeline_config = dict()
for k, v in self.study.user_attrs.items():
if k.startswith("pykeen_"):
metadata[k[len("pykeen_") :]] = v
elif k in {"metric"}:
continue
else:
pipeline_config[k] = v
for field in dataclasses.fields(self.objective):
if (
not field.name.endswith("_kwargs")
and field.name
not in {
"training",
"testing",
"validation",
}
) or field.name in {"metric"}:
continue
field_kwargs = getattr(self.objective, field.name)
if field_kwargs:
logger.debug(
f"saving pre-specified field in pipeline config: {field.name}={field_kwargs}"
)
pipeline_config[field.name] = field_kwargs
for k, v in self.study.best_params.items():
sk, ssk = k.split(".")
sk = f"{sk}_kwargs"
if sk not in pipeline_config:
pipeline_config[sk] = {}
logger.debug(f"saving optimized field in pipeline config: {sk}.{ssk}={v}")
pipeline_config[sk][ssk] = v
for k in ("stopper", "stopper_kwargs"):
if k in pipeline_config:
v = pipeline_config.pop(k)
metadata[f"_{k}_removed_comment"] = f"{k} config removed after HPO: {v}"
stopped_epoch = self.study.best_trial.user_attrs.get(STOPPED_EPOCH_KEY)
if stopped_epoch is not None:
old_num_epochs = pipeline_config["training_kwargs"]["num_epochs"]
metadata["_stopper_comment"] = (
f"While the original config had {old_num_epochs},"
f" early stopping will now switch it to {int(stopped_epoch)}"
)
pipeline_config["training_kwargs"]["num_epochs"] = int(stopped_epoch)
return dict(metadata=metadata, pipeline=pipeline_config)
|
https://github.com/pykeen/pykeen/issues/230
|
Traceback (most recent call last):
File "hpo_run.py", line 80, in <module>
run_hpo_search(args)
File "hpo_run.py", line 62, in run_hpo_search
hpo_pipeline_result.save_to_directory('study')
File "/home/user/anaconda3/envs/pykeen/lib/python3.8/site-packages/pykeen/hpo/hpo.py", line 327, in save_to_directory
json.dump(self._get_best_study_config(), file, indent=2, sort_keys=True)
File "/home/user/anaconda3/envs/pykeen/lib/python3.8/json/__init__.py", line 179, in dump
for chunk in iterable:
File "/home/user/anaconda3/envs/pykeen/lib/python3.8/json/encoder.py", line 431, in _iterencode
yield from _iterencode_dict(o, _current_indent_level)
File "/home/user/anaconda3/envs/pykeen/lib/python3.8/json/encoder.py", line 405, in _iterencode_dict
yield from chunks
File "/home/user/anaconda3/envs/pykeen/lib/python3.8/json/encoder.py", line 405, in _iterencode_dict
yield from chunks
File "/home/user/anaconda3/envs/pykeen/lib/python3.8/json/encoder.py", line 438, in _iterencode
o = _default(o)
File "/home/user/anaconda3/envs/pykeen/lib/python3.8/json/encoder.py", line 179, in default
raise TypeError(f'Object of type {o.__class__.__name__} '
TypeError: Object of type TriplesFactory is not JSON serializable
|
TypeError
|
def hpo_pipeline(
*,
# 1. Dataset
dataset: Union[None, str, Dataset, Type[Dataset]] = None,
dataset_kwargs: Optional[Mapping[str, Any]] = None,
training: Union[None, str, TriplesFactory] = None,
testing: Union[None, str, TriplesFactory] = None,
validation: Union[None, str, TriplesFactory] = None,
evaluation_entity_whitelist: Optional[Collection[str]] = None,
evaluation_relation_whitelist: Optional[Collection[str]] = None,
# 2. Model
model: Union[str, Type[Model]],
model_kwargs: Optional[Mapping[str, Any]] = None,
model_kwargs_ranges: Optional[Mapping[str, Any]] = None,
# 3. Loss
loss: Union[None, str, Type[Loss]] = None,
loss_kwargs: Optional[Mapping[str, Any]] = None,
loss_kwargs_ranges: Optional[Mapping[str, Any]] = None,
# 4. Regularizer
regularizer: Union[None, str, Type[Regularizer]] = None,
regularizer_kwargs: Optional[Mapping[str, Any]] = None,
regularizer_kwargs_ranges: Optional[Mapping[str, Any]] = None,
# 5. Optimizer
optimizer: Union[None, str, Type[Optimizer]] = None,
optimizer_kwargs: Optional[Mapping[str, Any]] = None,
optimizer_kwargs_ranges: Optional[Mapping[str, Any]] = None,
# 6. Training Loop
training_loop: Union[None, str, Type[TrainingLoop]] = None,
negative_sampler: Union[None, str, Type[NegativeSampler]] = None,
negative_sampler_kwargs: Optional[Mapping[str, Any]] = None,
negative_sampler_kwargs_ranges: Optional[Mapping[str, Any]] = None,
# 7. Training
training_kwargs: Optional[Mapping[str, Any]] = None,
training_kwargs_ranges: Optional[Mapping[str, Any]] = None,
stopper: Union[None, str, Type[Stopper]] = None,
stopper_kwargs: Optional[Mapping[str, Any]] = None,
# 8. Evaluation
evaluator: Union[None, str, Type[Evaluator]] = None,
evaluator_kwargs: Optional[Mapping[str, Any]] = None,
evaluation_kwargs: Optional[Mapping[str, Any]] = None,
metric: Optional[str] = None,
# 9. Tracking
result_tracker: Union[None, str, Type[ResultTracker]] = None,
result_tracker_kwargs: Optional[Mapping[str, Any]] = None,
# 6. Misc
device: Union[None, str, torch.device] = None,
# Optuna Study Settings
storage: Union[None, str, BaseStorage] = None,
sampler: Union[None, str, Type[BaseSampler]] = None,
sampler_kwargs: Optional[Mapping[str, Any]] = None,
pruner: Union[None, str, Type[BasePruner]] = None,
pruner_kwargs: Optional[Mapping[str, Any]] = None,
study_name: Optional[str] = None,
direction: Optional[str] = None,
load_if_exists: bool = False,
# Optuna Optimization Settings
n_trials: Optional[int] = None,
timeout: Optional[int] = None,
n_jobs: Optional[int] = None,
save_model_directory: Optional[str] = None,
) -> HpoPipelineResult:
"""Train a model on the given dataset.
:param dataset:
The name of the dataset (a key from :data:`pykeen.datasets.datasets`) or the :class:`pykeen.datasets.Dataset`
instance. Alternatively, the training triples factory (``training``), testing triples factory (``testing``),
and validation triples factory (``validation``; optional) can be specified.
:param dataset_kwargs:
The keyword arguments passed to the dataset upon instantiation
:param training:
A triples factory with training instances or path to the training file if a a dataset was not specified
:param testing:
A triples factory with test instances or path to the test file if a dataset was not specified
:param validation:
A triples factory with validation instances or path to the validation file if a dataset was not specified
:param evaluation_entity_whitelist:
Optional restriction of evaluation to triples containing *only* these entities. Useful if the downstream task
is only interested in certain entities, but the relational patterns with other entities improve the entity
embedding quality. Passed to :func:`pykeen.pipeline.pipeline`.
:param evaluation_relation_whitelist:
Optional restriction of evaluation to triples containing *only* these relations. Useful if the downstream task
is only interested in certain relation, but the relational patterns with other relations improve the entity
embedding quality. Passed to :func:`pykeen.pipeline.pipeline`.
:param model:
The name of the model or the model class to pass to :func:`pykeen.pipeline.pipeline`
:param model_kwargs:
Keyword arguments to pass to the model class on instantiation
:param model_kwargs_ranges:
Strategies for optimizing the models' hyper-parameters to override
the defaults
:param loss:
The name of the loss or the loss class to pass to :func:`pykeen.pipeline.pipeline`
:param loss_kwargs:
Keyword arguments to pass to the loss on instantiation
:param loss_kwargs_ranges:
Strategies for optimizing the losses' hyper-parameters to override
the defaults
:param regularizer:
The name of the regularizer or the regularizer class to pass to :func:`pykeen.pipeline.pipeline`
:param regularizer_kwargs:
Keyword arguments to pass to the regularizer on instantiation
:param regularizer_kwargs_ranges:
Strategies for optimizing the regularizers' hyper-parameters to override
the defaults
:param optimizer:
The name of the optimizer or the optimizer class. Defaults to :class:`torch.optim.Adagrad`.
:param optimizer_kwargs:
Keyword arguments to pass to the optimizer on instantiation
:param optimizer_kwargs_ranges:
Strategies for optimizing the optimizers' hyper-parameters to override
the defaults
:param training_loop:
The name of the training approach (``'slcwa'`` or ``'lcwa'``) or the training loop class
to pass to :func:`pykeen.pipeline.pipeline`
:param negative_sampler:
The name of the negative sampler (``'basic'`` or ``'bernoulli'``) or the negative sampler class
to pass to :func:`pykeen.pipeline.pipeline`. Only allowed when training with sLCWA.
:param negative_sampler_kwargs:
Keyword arguments to pass to the negative sampler class on instantiation
:param negative_sampler_kwargs_ranges:
Strategies for optimizing the negative samplers' hyper-parameters to override
the defaults
:param training_kwargs:
Keyword arguments to pass to the training loop's train function on call
:param training_kwargs_ranges:
Strategies for optimizing the training loops' hyper-parameters to override
the defaults. Can not specify ranges for batch size if early stopping is enabled.
:param stopper:
What kind of stopping to use. Default to no stopping, can be set to 'early'.
:param stopper_kwargs:
Keyword arguments to pass to the stopper upon instantiation.
:param evaluator:
The name of the evaluator or an evaluator class. Defaults to :class:`pykeen.evaluation.RankBasedEvaluator`.
:param evaluator_kwargs:
Keyword arguments to pass to the evaluator on instantiation
:param evaluation_kwargs:
Keyword arguments to pass to the evaluator's evaluate function on call
:param result_tracker:
The ResultsTracker class or name
:param result_tracker_kwargs:
The keyword arguments passed to the results tracker on instantiation
:param metric:
The metric to optimize over. Defaults to ``adjusted_mean_rank``.
:param direction:
The direction of optimization. Because the default metric is ``adjusted_mean_rank``,
the default direction is ``minimize``.
:param n_jobs: The number of parallel jobs. If this argument is set to :obj:`-1`, the number is
set to CPU counts. If none, defaults to 1.
.. note::
The remaining parameters are passed to :func:`optuna.study.create_study`
or :meth:`optuna.study.Study.optimize`.
"""
sampler_cls = get_sampler_cls(sampler)
pruner_cls = get_pruner_cls(pruner)
if direction is None:
direction = "minimize"
study = create_study(
storage=storage,
sampler=sampler_cls(**(sampler_kwargs or {})),
pruner=pruner_cls(**(pruner_kwargs or {})),
study_name=study_name,
direction=direction,
load_if_exists=load_if_exists,
)
# 0. Metadata/Provenance
study.set_user_attr("pykeen_version", get_version())
study.set_user_attr("pykeen_git_hash", get_git_hash())
# 1. Dataset
_set_study_dataset(
study=study,
dataset=dataset,
dataset_kwargs=dataset_kwargs,
training=training,
testing=testing,
validation=validation,
)
# 2. Model
model: Type[Model] = get_model_cls(model)
study.set_user_attr("model", normalize_string(model.__name__))
logger.info(f"Using model: {model}")
# 3. Loss
loss: Type[Loss] = model.loss_default if loss is None else get_loss_cls(loss)
study.set_user_attr("loss", normalize_string(loss.__name__, suffix=_LOSS_SUFFIX))
logger.info(f"Using loss: {loss}")
# 4. Regularizer
regularizer: Type[Regularizer] = (
model.regularizer_default
if regularizer is None
else get_regularizer_cls(regularizer)
)
study.set_user_attr("regularizer", regularizer.get_normalized_name())
logger.info(f"Using regularizer: {regularizer}")
# 5. Optimizer
optimizer: Type[Optimizer] = get_optimizer_cls(optimizer)
study.set_user_attr("optimizer", normalize_string(optimizer.__name__))
logger.info(f"Using optimizer: {optimizer}")
# 6. Training Loop
training_loop: Type[TrainingLoop] = get_training_loop_cls(training_loop)
study.set_user_attr("training_loop", training_loop.get_normalized_name())
logger.info(f"Using training loop: {training_loop}")
if training_loop is SLCWATrainingLoop:
negative_sampler: Optional[Type[NegativeSampler]] = get_negative_sampler_cls(
negative_sampler
)
study.set_user_attr("negative_sampler", negative_sampler.get_normalized_name())
logger.info(f"Using negative sampler: {negative_sampler}")
else:
negative_sampler: Optional[Type[NegativeSampler]] = None
# 7. Training
stopper: Type[Stopper] = get_stopper_cls(stopper)
if (
stopper is EarlyStopper
and training_kwargs_ranges
and "epochs" in training_kwargs_ranges
):
raise ValueError("can not use early stopping while optimizing epochs")
# 8. Evaluation
evaluator: Type[Evaluator] = get_evaluator_cls(evaluator)
study.set_user_attr("evaluator", evaluator.get_normalized_name())
logger.info(f"Using evaluator: {evaluator}")
if metric is None:
metric = "adjusted_mean_rank"
study.set_user_attr("metric", metric)
logger.info(f"Attempting to {direction} {metric}")
# 9. Tracking
result_tracker: Type[ResultTracker] = get_result_tracker_cls(result_tracker)
objective = Objective(
# 1. Dataset
dataset=dataset,
dataset_kwargs=dataset_kwargs,
training=training,
testing=testing,
validation=validation,
evaluation_entity_whitelist=evaluation_entity_whitelist,
evaluation_relation_whitelist=evaluation_relation_whitelist,
# 2. Model
model=model,
model_kwargs=model_kwargs,
model_kwargs_ranges=model_kwargs_ranges,
# 3. Loss
loss=loss,
loss_kwargs=loss_kwargs,
loss_kwargs_ranges=loss_kwargs_ranges,
# 4. Regularizer
regularizer=regularizer,
regularizer_kwargs=regularizer_kwargs,
regularizer_kwargs_ranges=regularizer_kwargs_ranges,
# 5. Optimizer
optimizer=optimizer,
optimizer_kwargs=optimizer_kwargs,
optimizer_kwargs_ranges=optimizer_kwargs_ranges,
# 6. Training Loop
training_loop=training_loop,
negative_sampler=negative_sampler,
negative_sampler_kwargs=negative_sampler_kwargs,
negative_sampler_kwargs_ranges=negative_sampler_kwargs_ranges,
# 7. Training
training_kwargs=training_kwargs,
training_kwargs_ranges=training_kwargs_ranges,
stopper=stopper,
stopper_kwargs=stopper_kwargs,
# 8. Evaluation
evaluator=evaluator,
evaluator_kwargs=evaluator_kwargs,
evaluation_kwargs=evaluation_kwargs,
# 9. Tracker
result_tracker=result_tracker,
result_tracker_kwargs=result_tracker_kwargs,
# Optuna Misc.
metric=metric,
save_model_directory=save_model_directory,
# Pipeline Misc.
device=device,
)
# Invoke optimization of the objective function.
study.optimize(
objective,
n_trials=n_trials,
timeout=timeout,
n_jobs=n_jobs or 1,
)
return HpoPipelineResult(
study=study,
objective=objective,
)
|
def hpo_pipeline(
*,
# 1. Dataset
dataset: Union[None, str, Dataset, Type[Dataset]] = None,
dataset_kwargs: Optional[Mapping[str, Any]] = None,
training: Union[None, str, TriplesFactory] = None,
testing: Union[None, str, TriplesFactory] = None,
validation: Union[None, str, TriplesFactory] = None,
evaluation_entity_whitelist: Optional[Collection[str]] = None,
evaluation_relation_whitelist: Optional[Collection[str]] = None,
# 2. Model
model: Union[str, Type[Model]],
model_kwargs: Optional[Mapping[str, Any]] = None,
model_kwargs_ranges: Optional[Mapping[str, Any]] = None,
# 3. Loss
loss: Union[None, str, Type[Loss]] = None,
loss_kwargs: Optional[Mapping[str, Any]] = None,
loss_kwargs_ranges: Optional[Mapping[str, Any]] = None,
# 4. Regularizer
regularizer: Union[None, str, Type[Regularizer]] = None,
regularizer_kwargs: Optional[Mapping[str, Any]] = None,
regularizer_kwargs_ranges: Optional[Mapping[str, Any]] = None,
# 5. Optimizer
optimizer: Union[None, str, Type[Optimizer]] = None,
optimizer_kwargs: Optional[Mapping[str, Any]] = None,
optimizer_kwargs_ranges: Optional[Mapping[str, Any]] = None,
# 6. Training Loop
training_loop: Union[None, str, Type[TrainingLoop]] = None,
negative_sampler: Union[None, str, Type[NegativeSampler]] = None,
negative_sampler_kwargs: Optional[Mapping[str, Any]] = None,
negative_sampler_kwargs_ranges: Optional[Mapping[str, Any]] = None,
# 7. Training
training_kwargs: Optional[Mapping[str, Any]] = None,
training_kwargs_ranges: Optional[Mapping[str, Any]] = None,
stopper: Union[None, str, Type[Stopper]] = None,
stopper_kwargs: Optional[Mapping[str, Any]] = None,
# 8. Evaluation
evaluator: Union[None, str, Type[Evaluator]] = None,
evaluator_kwargs: Optional[Mapping[str, Any]] = None,
evaluation_kwargs: Optional[Mapping[str, Any]] = None,
metric: Optional[str] = None,
# 9. Tracking
result_tracker: Union[None, str, Type[ResultTracker]] = None,
result_tracker_kwargs: Optional[Mapping[str, Any]] = None,
# 6. Misc
device: Union[None, str, torch.device] = None,
# Optuna Study Settings
storage: Union[None, str, BaseStorage] = None,
sampler: Union[None, str, Type[BaseSampler]] = None,
sampler_kwargs: Optional[Mapping[str, Any]] = None,
pruner: Union[None, str, Type[BasePruner]] = None,
pruner_kwargs: Optional[Mapping[str, Any]] = None,
study_name: Optional[str] = None,
direction: Optional[str] = None,
load_if_exists: bool = False,
# Optuna Optimization Settings
n_trials: Optional[int] = None,
timeout: Optional[int] = None,
n_jobs: Optional[int] = None,
save_model_directory: Optional[str] = None,
) -> HpoPipelineResult:
"""Train a model on the given dataset.
:param dataset:
The name of the dataset (a key from :data:`pykeen.datasets.datasets`) or the :class:`pykeen.datasets.Dataset`
instance. Alternatively, the training triples factory (``training``), testing triples factory (``testing``),
and validation triples factory (``validation``; optional) can be specified.
:param dataset_kwargs:
The keyword arguments passed to the dataset upon instantiation
:param training:
A triples factory with training instances or path to the training file if a a dataset was not specified
:param testing:
A triples factory with test instances or path to the test file if a dataset was not specified
:param validation:
A triples factory with validation instances or path to the validation file if a dataset was not specified
:param evaluation_entity_whitelist:
Optional restriction of evaluation to triples containing *only* these entities. Useful if the downstream task
is only interested in certain entities, but the relational patterns with other entities improve the entity
embedding quality. Passed to :func:`pykeen.pipeline.pipeline`.
:param evaluation_relation_whitelist:
Optional restriction of evaluation to triples containing *only* these relations. Useful if the downstream task
is only interested in certain relation, but the relational patterns with other relations improve the entity
embedding quality. Passed to :func:`pykeen.pipeline.pipeline`.
:param model:
The name of the model or the model class to pass to :func:`pykeen.pipeline.pipeline`
:param model_kwargs:
Keyword arguments to pass to the model class on instantiation
:param model_kwargs_ranges:
Strategies for optimizing the models' hyper-parameters to override
the defaults
:param loss:
The name of the loss or the loss class to pass to :func:`pykeen.pipeline.pipeline`
:param loss_kwargs:
Keyword arguments to pass to the loss on instantiation
:param loss_kwargs_ranges:
Strategies for optimizing the losses' hyper-parameters to override
the defaults
:param regularizer:
The name of the regularizer or the regularizer class to pass to :func:`pykeen.pipeline.pipeline`
:param regularizer_kwargs:
Keyword arguments to pass to the regularizer on instantiation
:param regularizer_kwargs_ranges:
Strategies for optimizing the regularizers' hyper-parameters to override
the defaults
:param optimizer:
The name of the optimizer or the optimizer class. Defaults to :class:`torch.optim.Adagrad`.
:param optimizer_kwargs:
Keyword arguments to pass to the optimizer on instantiation
:param optimizer_kwargs_ranges:
Strategies for optimizing the optimizers' hyper-parameters to override
the defaults
:param training_loop:
The name of the training approach (``'slcwa'`` or ``'lcwa'``) or the training loop class
to pass to :func:`pykeen.pipeline.pipeline`
:param negative_sampler:
The name of the negative sampler (``'basic'`` or ``'bernoulli'``) or the negative sampler class
to pass to :func:`pykeen.pipeline.pipeline`. Only allowed when training with sLCWA.
:param negative_sampler_kwargs:
Keyword arguments to pass to the negative sampler class on instantiation
:param negative_sampler_kwargs_ranges:
Strategies for optimizing the negative samplers' hyper-parameters to override
the defaults
:param training_kwargs:
Keyword arguments to pass to the training loop's train function on call
:param training_kwargs_ranges:
Strategies for optimizing the training loops' hyper-parameters to override
the defaults. Can not specify ranges for batch size if early stopping is enabled.
:param stopper:
What kind of stopping to use. Default to no stopping, can be set to 'early'.
:param stopper_kwargs:
Keyword arguments to pass to the stopper upon instantiation.
:param evaluator:
The name of the evaluator or an evaluator class. Defaults to :class:`pykeen.evaluation.RankBasedEvaluator`.
:param evaluator_kwargs:
Keyword arguments to pass to the evaluator on instantiation
:param evaluation_kwargs:
Keyword arguments to pass to the evaluator's evaluate function on call
:param result_tracker:
The ResultsTracker class or name
:param result_tracker_kwargs:
The keyword arguments passed to the results tracker on instantiation
:param metric:
The metric to optimize over. Defaults to ``adjusted_mean_rank``.
:param direction:
The direction of optimization. Because the default metric is ``adjusted_mean_rank``,
the default direction is ``minimize``.
:param n_jobs: The number of parallel jobs. If this argument is set to :obj:`-1`, the number is
set to CPU counts. If none, defaults to 1.
.. note::
The remaining parameters are passed to :func:`optuna.study.create_study`
or :meth:`optuna.study.Study.optimize`.
"""
sampler_cls = get_sampler_cls(sampler)
pruner_cls = get_pruner_cls(pruner)
if direction is None:
direction = "minimize"
study = create_study(
storage=storage,
sampler=sampler_cls(**(sampler_kwargs or {})),
pruner=pruner_cls(**(pruner_kwargs or {})),
study_name=study_name,
direction=direction,
load_if_exists=load_if_exists,
)
# 0. Metadata/Provenance
study.set_user_attr("pykeen_version", get_version())
study.set_user_attr("pykeen_git_hash", get_git_hash())
# 1. Dataset
study.set_user_attr(
"dataset",
_get_dataset_name(
dataset=dataset,
dataset_kwargs=dataset_kwargs,
training=training,
testing=testing,
validation=validation,
),
)
# 2. Model
model: Type[Model] = get_model_cls(model)
study.set_user_attr("model", normalize_string(model.__name__))
logger.info(f"Using model: {model}")
# 3. Loss
loss: Type[Loss] = model.loss_default if loss is None else get_loss_cls(loss)
study.set_user_attr("loss", normalize_string(loss.__name__, suffix=_LOSS_SUFFIX))
logger.info(f"Using loss: {loss}")
# 4. Regularizer
regularizer: Type[Regularizer] = (
model.regularizer_default
if regularizer is None
else get_regularizer_cls(regularizer)
)
study.set_user_attr("regularizer", regularizer.get_normalized_name())
logger.info(f"Using regularizer: {regularizer}")
# 5. Optimizer
optimizer: Type[Optimizer] = get_optimizer_cls(optimizer)
study.set_user_attr("optimizer", normalize_string(optimizer.__name__))
logger.info(f"Using optimizer: {optimizer}")
# 6. Training Loop
training_loop: Type[TrainingLoop] = get_training_loop_cls(training_loop)
study.set_user_attr("training_loop", training_loop.get_normalized_name())
logger.info(f"Using training loop: {training_loop}")
if training_loop is SLCWATrainingLoop:
negative_sampler: Optional[Type[NegativeSampler]] = get_negative_sampler_cls(
negative_sampler
)
study.set_user_attr("negative_sampler", negative_sampler.get_normalized_name())
logger.info(f"Using negative sampler: {negative_sampler}")
else:
negative_sampler: Optional[Type[NegativeSampler]] = None
# 7. Training
stopper: Type[Stopper] = get_stopper_cls(stopper)
if (
stopper is EarlyStopper
and training_kwargs_ranges
and "epochs" in training_kwargs_ranges
):
raise ValueError("can not use early stopping while optimizing epochs")
# 8. Evaluation
evaluator: Type[Evaluator] = get_evaluator_cls(evaluator)
study.set_user_attr("evaluator", evaluator.get_normalized_name())
logger.info(f"Using evaluator: {evaluator}")
if metric is None:
metric = "adjusted_mean_rank"
study.set_user_attr("metric", metric)
logger.info(f"Attempting to {direction} {metric}")
# 9. Tracking
result_tracker: Type[ResultTracker] = get_result_tracker_cls(result_tracker)
objective = Objective(
# 1. Dataset
dataset=dataset,
dataset_kwargs=dataset_kwargs,
training=training,
testing=testing,
validation=validation,
evaluation_entity_whitelist=evaluation_entity_whitelist,
evaluation_relation_whitelist=evaluation_relation_whitelist,
# 2. Model
model=model,
model_kwargs=model_kwargs,
model_kwargs_ranges=model_kwargs_ranges,
# 3. Loss
loss=loss,
loss_kwargs=loss_kwargs,
loss_kwargs_ranges=loss_kwargs_ranges,
# 4. Regularizer
regularizer=regularizer,
regularizer_kwargs=regularizer_kwargs,
regularizer_kwargs_ranges=regularizer_kwargs_ranges,
# 5. Optimizer
optimizer=optimizer,
optimizer_kwargs=optimizer_kwargs,
optimizer_kwargs_ranges=optimizer_kwargs_ranges,
# 6. Training Loop
training_loop=training_loop,
negative_sampler=negative_sampler,
negative_sampler_kwargs=negative_sampler_kwargs,
negative_sampler_kwargs_ranges=negative_sampler_kwargs_ranges,
# 7. Training
training_kwargs=training_kwargs,
training_kwargs_ranges=training_kwargs_ranges,
stopper=stopper,
stopper_kwargs=stopper_kwargs,
# 8. Evaluation
evaluator=evaluator,
evaluator_kwargs=evaluator_kwargs,
evaluation_kwargs=evaluation_kwargs,
# 9. Tracker
result_tracker=result_tracker,
result_tracker_kwargs=result_tracker_kwargs,
# Optuna Misc.
metric=metric,
save_model_directory=save_model_directory,
# Pipeline Misc.
device=device,
)
# Invoke optimization of the objective function.
study.optimize(
objective,
n_trials=n_trials,
timeout=timeout,
n_jobs=n_jobs or 1,
)
return HpoPipelineResult(
study=study,
objective=objective,
)
|
https://github.com/pykeen/pykeen/issues/230
|
Traceback (most recent call last):
File "hpo_run.py", line 80, in <module>
run_hpo_search(args)
File "hpo_run.py", line 62, in run_hpo_search
hpo_pipeline_result.save_to_directory('study')
File "/home/user/anaconda3/envs/pykeen/lib/python3.8/site-packages/pykeen/hpo/hpo.py", line 327, in save_to_directory
json.dump(self._get_best_study_config(), file, indent=2, sort_keys=True)
File "/home/user/anaconda3/envs/pykeen/lib/python3.8/json/__init__.py", line 179, in dump
for chunk in iterable:
File "/home/user/anaconda3/envs/pykeen/lib/python3.8/json/encoder.py", line 431, in _iterencode
yield from _iterencode_dict(o, _current_indent_level)
File "/home/user/anaconda3/envs/pykeen/lib/python3.8/json/encoder.py", line 405, in _iterencode_dict
yield from chunks
File "/home/user/anaconda3/envs/pykeen/lib/python3.8/json/encoder.py", line 405, in _iterencode_dict
yield from chunks
File "/home/user/anaconda3/envs/pykeen/lib/python3.8/json/encoder.py", line 438, in _iterencode
o = _default(o)
File "/home/user/anaconda3/envs/pykeen/lib/python3.8/json/encoder.py", line 179, in default
raise TypeError(f'Object of type {o.__class__.__name__} '
TypeError: Object of type TriplesFactory is not JSON serializable
|
TypeError
|
def pipeline( # noqa: C901
*,
# 1. Dataset
dataset: Union[None, str, Dataset, Type[Dataset]] = None,
dataset_kwargs: Optional[Mapping[str, Any]] = None,
training: Union[None, TriplesFactory, str] = None,
testing: Union[None, TriplesFactory, str] = None,
validation: Union[None, TriplesFactory, str] = None,
evaluation_entity_whitelist: Optional[Collection[str]] = None,
evaluation_relation_whitelist: Optional[Collection[str]] = None,
# 2. Model
model: Union[str, Type[Model]],
model_kwargs: Optional[Mapping[str, Any]] = None,
# 3. Loss
loss: Union[None, str, Type[Loss]] = None,
loss_kwargs: Optional[Mapping[str, Any]] = None,
# 4. Regularizer
regularizer: Union[None, str, Type[Regularizer]] = None,
regularizer_kwargs: Optional[Mapping[str, Any]] = None,
# 5. Optimizer
optimizer: Union[None, str, Type[Optimizer]] = None,
optimizer_kwargs: Optional[Mapping[str, Any]] = None,
clear_optimizer: bool = True,
# 6. Training Loop
training_loop: Union[None, str, Type[TrainingLoop]] = None,
negative_sampler: Union[None, str, Type[NegativeSampler]] = None,
negative_sampler_kwargs: Optional[Mapping[str, Any]] = None,
# 7. Training (ronaldo style)
training_kwargs: Optional[Mapping[str, Any]] = None,
stopper: Union[None, str, Type[Stopper]] = None,
stopper_kwargs: Optional[Mapping[str, Any]] = None,
# 8. Evaluation
evaluator: Union[None, str, Type[Evaluator]] = None,
evaluator_kwargs: Optional[Mapping[str, Any]] = None,
evaluation_kwargs: Optional[Mapping[str, Any]] = None,
# 9. Tracking
result_tracker: Union[None, str, Type[ResultTracker]] = None,
result_tracker_kwargs: Optional[Mapping[str, Any]] = None,
# Misc
automatic_memory_optimization: bool = True,
metadata: Optional[Dict[str, Any]] = None,
device: Union[None, str, torch.device] = None,
random_seed: Optional[int] = None,
use_testing_data: bool = True,
) -> PipelineResult:
"""Train and evaluate a model.
:param dataset:
The name of the dataset (a key from :data:`pykeen.datasets.datasets`) or the :class:`pykeen.datasets.Dataset`
instance. Alternatively, the training triples factory (``training``), testing triples factory (``testing``),
and validation triples factory (``validation``; optional) can be specified.
:param dataset_kwargs:
The keyword arguments passed to the dataset upon instantiation
:param training:
A triples factory with training instances or path to the training file if a a dataset was not specified
:param testing:
A triples factory with training instances or path to the test file if a dataset was not specified
:param validation:
A triples factory with validation instances or path to the validation file if a dataset was not specified
:param evaluation_entity_whitelist:
Optional restriction of evaluation to triples containing *only* these entities. Useful if the downstream task
is only interested in certain entities, but the relational patterns with other entities improve the entity
embedding quality.
:param evaluation_relation_whitelist:
Optional restriction of evaluation to triples containing *only* these relations. Useful if the downstream task
is only interested in certain relation, but the relational patterns with other relations improve the entity
embedding quality.
:param model:
The name of the model or the model class
:param model_kwargs:
Keyword arguments to pass to the model class on instantiation
:param loss:
The name of the loss or the loss class.
:param loss_kwargs:
Keyword arguments to pass to the loss on instantiation
:param regularizer:
The name of the regularizer or the regularizer class.
:param regularizer_kwargs:
Keyword arguments to pass to the regularizer on instantiation
:param optimizer:
The name of the optimizer or the optimizer class. Defaults to :class:`torch.optim.Adagrad`.
:param optimizer_kwargs:
Keyword arguments to pass to the optimizer on instantiation
:param clear_optimizer:
Whether to delete the optimizer instance after training. As the optimizer might have additional memory
consumption due to e.g. moments in Adam, this is the default option. If you want to continue training, you
should set it to False, as the optimizer's internal parameter will get lost otherwise.
:param training_loop:
The name of the training loop's training approach (``'slcwa'`` or ``'lcwa'``) or the training loop class.
Defaults to :class:`pykeen.training.SLCWATrainingLoop`.
:param negative_sampler:
The name of the negative sampler (``'basic'`` or ``'bernoulli'``) or the negative sampler class.
Only allowed when training with sLCWA.
Defaults to :class:`pykeen.sampling.BasicNegativeSampler`.
:param negative_sampler_kwargs:
Keyword arguments to pass to the negative sampler class on instantiation
:param training_kwargs:
Keyword arguments to pass to the training loop's train function on call
:param stopper:
What kind of stopping to use. Default to no stopping, can be set to 'early'.
:param stopper_kwargs:
Keyword arguments to pass to the stopper upon instantiation.
:param evaluator:
The name of the evaluator or an evaluator class. Defaults to :class:`pykeen.evaluation.RankBasedEvaluator`.
:param evaluator_kwargs:
Keyword arguments to pass to the evaluator on instantiation
:param evaluation_kwargs:
Keyword arguments to pass to the evaluator's evaluate function on call
:param result_tracker:
The ResultsTracker class or name
:param result_tracker_kwargs:
The keyword arguments passed to the results tracker on instantiation
:param metadata:
A JSON dictionary to store with the experiment
:param use_testing_data:
If true, use the testing triples. Otherwise, use the validation triples. Defaults to true - use testing triples.
"""
if training_kwargs is None:
training_kwargs = {}
# To allow resuming training from a checkpoint when using a pipeline, the pipeline needs to obtain the
# used random_seed to ensure reproducible results
checkpoint_name = training_kwargs.get("checkpoint_name")
if checkpoint_name is not None:
checkpoint_directory = pathlib.Path(
training_kwargs.get("checkpoint_directory", PYKEEN_CHECKPOINTS)
)
checkpoint_directory.mkdir(parents=True, exist_ok=True)
checkpoint_path = checkpoint_directory / checkpoint_name
if checkpoint_path.is_file():
checkpoint_dict = torch.load(checkpoint_path)
random_seed = checkpoint_dict["random_seed"]
logger.info("loaded random seed %s from checkpoint.", random_seed)
# We have to set clear optimizer to False since training should be continued
clear_optimizer = False
else:
logger.info(
f"=> no training loop checkpoint file found at '{checkpoint_path}'. Creating a new file."
)
if random_seed is None:
random_seed = random_non_negative_int()
logger.warning(
f"No random seed is specified. Setting to {random_seed}."
)
elif random_seed is None:
random_seed = random_non_negative_int()
logger.warning(f"No random seed is specified. Setting to {random_seed}.")
set_random_seed(random_seed)
result_tracker_cls: Type[ResultTracker] = get_result_tracker_cls(result_tracker)
result_tracker = result_tracker_cls(**(result_tracker_kwargs or {}))
if not metadata:
metadata = {}
title = metadata.get("title")
# Start tracking
result_tracker.start_run(run_name=title)
device = resolve_device(device)
dataset_instance: Dataset = get_dataset(
dataset=dataset,
dataset_kwargs=dataset_kwargs,
training=training,
testing=testing,
validation=validation,
)
if dataset is not None:
result_tracker.log_params(dict(dataset=dataset_instance.get_normalized_name()))
else: # means that dataset was defined by triples factories
result_tracker.log_params(
dict(
dataset=USER_DEFINED_CODE,
training=training if isinstance(training, str) else USER_DEFINED_CODE,
testing=testing if isinstance(training, str) else USER_DEFINED_CODE,
validation=validation
if isinstance(training, str)
else USER_DEFINED_CODE,
)
)
training, testing, validation = (
dataset_instance.training,
dataset_instance.testing,
dataset_instance.validation,
)
# evaluation restriction to a subset of entities/relations
if any(
f is not None
for f in (evaluation_entity_whitelist, evaluation_relation_whitelist)
):
testing = testing.new_with_restriction(
entities=evaluation_entity_whitelist,
relations=evaluation_relation_whitelist,
)
if validation is not None:
validation = validation.new_with_restriction(
entities=evaluation_entity_whitelist,
relations=evaluation_relation_whitelist,
)
if model_kwargs is None:
model_kwargs = {}
model_kwargs.update(preferred_device=device)
model_kwargs.setdefault("random_seed", random_seed)
if regularizer is not None:
# FIXME this should never happen.
if "regularizer" in model_kwargs:
logger.warning(
"Can not specify regularizer in kwargs and model_kwargs. removing from model_kwargs"
)
del model_kwargs["regularizer"]
regularizer_cls: Type[Regularizer] = get_regularizer_cls(regularizer)
model_kwargs["regularizer"] = regularizer_cls(
device=device,
**(regularizer_kwargs or {}),
)
if loss is not None:
if "loss" in model_kwargs: # FIXME
logger.warning(
"duplicate loss in kwargs and model_kwargs. removing from model_kwargs"
)
del model_kwargs["loss"]
loss_cls = get_loss_cls(loss)
_loss = loss_cls(**(loss_kwargs or {}))
model_kwargs.setdefault("loss", _loss)
model = get_model_cls(model)
model_instance: Model = model(
triples_factory=training,
**model_kwargs,
)
# Log model parameters
result_tracker.log_params(
params=dict(cls=model.__name__, kwargs=model_kwargs), prefix="model"
)
optimizer = get_optimizer_cls(optimizer)
training_loop = get_training_loop_cls(training_loop)
if optimizer_kwargs is None:
optimizer_kwargs = {}
# Log optimizer parameters
result_tracker.log_params(
params=dict(cls=optimizer.__name__, kwargs=optimizer_kwargs), prefix="optimizer"
)
optimizer_instance = optimizer(
params=model_instance.get_grad_params(),
**optimizer_kwargs,
)
result_tracker.log_params(
params=dict(cls=training_loop.__name__), prefix="training_loop"
)
if negative_sampler is None:
training_loop_instance: TrainingLoop = training_loop(
model=model_instance,
optimizer=optimizer_instance,
automatic_memory_optimization=automatic_memory_optimization,
)
elif training_loop is not SLCWATrainingLoop:
raise ValueError("Can not specify negative sampler with LCWA")
else:
negative_sampler = get_negative_sampler_cls(negative_sampler)
result_tracker.log_params(
params=dict(cls=negative_sampler.__name__, kwargs=negative_sampler_kwargs),
prefix="negative_sampler",
)
training_loop_instance: TrainingLoop = SLCWATrainingLoop(
model=model_instance,
optimizer=optimizer_instance,
automatic_memory_optimization=automatic_memory_optimization,
negative_sampler_cls=negative_sampler,
negative_sampler_kwargs=negative_sampler_kwargs,
)
evaluator = get_evaluator_cls(evaluator)
if evaluator_kwargs is None:
evaluator_kwargs = {}
evaluator_kwargs.setdefault(
"automatic_memory_optimization", automatic_memory_optimization
)
evaluator_instance: Evaluator = evaluator(**evaluator_kwargs)
if evaluation_kwargs is None:
evaluation_kwargs = {}
# Stopping
if "stopper" in training_kwargs and stopper is not None:
raise ValueError("Specified stopper in training_kwargs and as stopper")
if "stopper" in training_kwargs:
stopper = training_kwargs.pop("stopper")
if stopper_kwargs is None:
stopper_kwargs = {}
# Load the evaluation batch size for the stopper, if it has been set
_evaluation_batch_size = evaluation_kwargs.get("batch_size")
if _evaluation_batch_size is not None:
stopper_kwargs.setdefault("evaluation_batch_size", _evaluation_batch_size)
# By default there's a stopper that does nothing interesting
stopper_cls: Type[Stopper] = get_stopper_cls(stopper)
stopper: Stopper = stopper_cls(
model=model_instance,
evaluator=evaluator_instance,
evaluation_triples_factory=validation,
result_tracker=result_tracker,
**stopper_kwargs,
)
training_kwargs.setdefault("num_epochs", 5)
training_kwargs.setdefault("batch_size", 256)
result_tracker.log_params(params=training_kwargs, prefix="training")
# Add logging for debugging
logging.debug("Run Pipeline based on following config:")
if dataset is not None:
logging.debug(f"dataset: {dataset}")
logging.debug(f"dataset_kwargs: {dataset_kwargs}")
else:
logging.debug("training: %s", training)
logging.debug("testing: %s", testing)
if validation:
logging.debug("validation: %s", validation)
logging.debug(f"model: {model}")
logging.debug(f"model_kwargs: {model_kwargs}")
logging.debug(f"loss: {loss}")
logging.debug(f"loss_kwargs: {loss_kwargs}")
logging.debug(f"regularizer: {regularizer}")
logging.debug(f"regularizer_kwargs: {regularizer_kwargs}")
logging.debug(f"optimizer: {optimizer}")
logging.debug(f"optimizer_kwargs: {optimizer_kwargs}")
logging.debug(f"training_loop: {training_loop}")
logging.debug(f"negative_sampler: {negative_sampler}")
logging.debug(f"_negative_sampler_kwargs: {negative_sampler_kwargs}")
logging.debug(f"_training_kwargs: {training_kwargs}")
logging.debug(f"stopper: {stopper}")
logging.debug(f"stopper_kwargs: {stopper_kwargs}")
logging.debug(f"evaluator: {evaluator}")
logging.debug(f"evaluator_kwargs: {evaluator_kwargs}")
# Train like Cristiano Ronaldo
training_start_time = time.time()
losses = training_loop_instance.train(
stopper=stopper,
result_tracker=result_tracker,
clear_optimizer=clear_optimizer,
**training_kwargs,
)
training_end_time = time.time() - training_start_time
if use_testing_data:
mapped_triples = testing.mapped_triples
else:
mapped_triples = validation.mapped_triples
# Evaluate
# Reuse optimal evaluation parameters from training if available
if (
evaluator_instance.batch_size is not None
or evaluator_instance.slice_size is not None
):
evaluation_kwargs["batch_size"] = evaluator_instance.batch_size
evaluation_kwargs["slice_size"] = evaluator_instance.slice_size
# Add logging about evaluator for debugging
logging.debug("Evaluation will be run with following parameters:")
logging.debug(f"evaluation_kwargs: {evaluation_kwargs}")
evaluate_start_time = time.time()
metric_results: MetricResults = evaluator_instance.evaluate(
model=model_instance,
mapped_triples=mapped_triples,
**evaluation_kwargs,
)
evaluate_end_time = time.time() - evaluate_start_time
result_tracker.log_metrics(
metrics=metric_results.to_dict(),
step=training_kwargs.get("num_epochs"),
)
result_tracker.end_run()
return PipelineResult(
random_seed=random_seed,
model=model_instance,
training_loop=training_loop_instance,
losses=losses,
stopper=stopper,
metric_results=metric_results,
metadata=metadata,
train_seconds=training_end_time,
evaluate_seconds=evaluate_end_time,
)
|
def pipeline( # noqa: C901
*,
# 1. Dataset
dataset: Union[None, str, Dataset, Type[Dataset]] = None,
dataset_kwargs: Optional[Mapping[str, Any]] = None,
training: Union[None, TriplesFactory, str] = None,
testing: Union[None, TriplesFactory, str] = None,
validation: Union[None, TriplesFactory, str] = None,
evaluation_entity_whitelist: Optional[Collection[str]] = None,
evaluation_relation_whitelist: Optional[Collection[str]] = None,
# 2. Model
model: Union[str, Type[Model]],
model_kwargs: Optional[Mapping[str, Any]] = None,
# 3. Loss
loss: Union[None, str, Type[Loss]] = None,
loss_kwargs: Optional[Mapping[str, Any]] = None,
# 4. Regularizer
regularizer: Union[None, str, Type[Regularizer]] = None,
regularizer_kwargs: Optional[Mapping[str, Any]] = None,
# 5. Optimizer
optimizer: Union[None, str, Type[Optimizer]] = None,
optimizer_kwargs: Optional[Mapping[str, Any]] = None,
clear_optimizer: bool = True,
# 6. Training Loop
training_loop: Union[None, str, Type[TrainingLoop]] = None,
negative_sampler: Union[None, str, Type[NegativeSampler]] = None,
negative_sampler_kwargs: Optional[Mapping[str, Any]] = None,
# 7. Training (ronaldo style)
training_kwargs: Optional[Mapping[str, Any]] = None,
stopper: Union[None, str, Type[Stopper]] = None,
stopper_kwargs: Optional[Mapping[str, Any]] = None,
# 8. Evaluation
evaluator: Union[None, str, Type[Evaluator]] = None,
evaluator_kwargs: Optional[Mapping[str, Any]] = None,
evaluation_kwargs: Optional[Mapping[str, Any]] = None,
# 9. Tracking
result_tracker: Union[None, str, Type[ResultTracker]] = None,
result_tracker_kwargs: Optional[Mapping[str, Any]] = None,
# Misc
automatic_memory_optimization: bool = True,
metadata: Optional[Dict[str, Any]] = None,
device: Union[None, str, torch.device] = None,
random_seed: Optional[int] = None,
use_testing_data: bool = True,
) -> PipelineResult:
"""Train and evaluate a model.
:param dataset:
The name of the dataset (a key from :data:`pykeen.datasets.datasets`) or the :class:`pykeen.datasets.Dataset`
instance. Alternatively, the training triples factory (``training``), testing triples factory (``testing``),
and validation triples factory (``validation``; optional) can be specified.
:param dataset_kwargs:
The keyword arguments passed to the dataset upon instantiation
:param training:
A triples factory with training instances or path to the training file if a a dataset was not specified
:param testing:
A triples factory with training instances or path to the test file if a dataset was not specified
:param validation:
A triples factory with validation instances or path to the validation file if a dataset was not specified
:param evaluation_entity_whitelist:
Optional restriction of evaluation to triples containing *only* these entities. Useful if the downstream task
is only interested in certain entities, but the relational patterns with other entities improve the entity
embedding quality.
:param evaluation_relation_whitelist:
Optional restriction of evaluation to triples containing *only* these relations. Useful if the downstream task
is only interested in certain relation, but the relational patterns with other relations improve the entity
embedding quality.
:param model:
The name of the model or the model class
:param model_kwargs:
Keyword arguments to pass to the model class on instantiation
:param loss:
The name of the loss or the loss class.
:param loss_kwargs:
Keyword arguments to pass to the loss on instantiation
:param regularizer:
The name of the regularizer or the regularizer class.
:param regularizer_kwargs:
Keyword arguments to pass to the regularizer on instantiation
:param optimizer:
The name of the optimizer or the optimizer class. Defaults to :class:`torch.optim.Adagrad`.
:param optimizer_kwargs:
Keyword arguments to pass to the optimizer on instantiation
:param clear_optimizer:
Whether to delete the optimizer instance after training. As the optimizer might have additional memory
consumption due to e.g. moments in Adam, this is the default option. If you want to continue training, you
should set it to False, as the optimizer's internal parameter will get lost otherwise.
:param training_loop:
The name of the training loop's training approach (``'slcwa'`` or ``'lcwa'``) or the training loop class.
Defaults to :class:`pykeen.training.SLCWATrainingLoop`.
:param negative_sampler:
The name of the negative sampler (``'basic'`` or ``'bernoulli'``) or the negative sampler class.
Only allowed when training with sLCWA.
Defaults to :class:`pykeen.sampling.BasicNegativeSampler`.
:param negative_sampler_kwargs:
Keyword arguments to pass to the negative sampler class on instantiation
:param training_kwargs:
Keyword arguments to pass to the training loop's train function on call
:param stopper:
What kind of stopping to use. Default to no stopping, can be set to 'early'.
:param stopper_kwargs:
Keyword arguments to pass to the stopper upon instantiation.
:param evaluator:
The name of the evaluator or an evaluator class. Defaults to :class:`pykeen.evaluation.RankBasedEvaluator`.
:param evaluator_kwargs:
Keyword arguments to pass to the evaluator on instantiation
:param evaluation_kwargs:
Keyword arguments to pass to the evaluator's evaluate function on call
:param result_tracker:
The ResultsTracker class or name
:param result_tracker_kwargs:
The keyword arguments passed to the results tracker on instantiation
:param metadata:
A JSON dictionary to store with the experiment
:param use_testing_data:
If true, use the testing triples. Otherwise, use the validation triples. Defaults to true - use testing triples.
"""
if training_kwargs is None:
training_kwargs = {}
# To allow resuming training from a checkpoint when using a pipeline, the pipeline needs to obtain the
# used random_seed to ensure reproducible results
checkpoint_name = training_kwargs.get("checkpoint_name")
if checkpoint_name is not None:
checkpoint_directory = pathlib.Path(
training_kwargs.get("checkpoint_directory", PYKEEN_CHECKPOINTS)
)
checkpoint_directory.mkdir(parents=True, exist_ok=True)
checkpoint_path = checkpoint_directory / checkpoint_name
if checkpoint_path.is_file():
checkpoint_dict = torch.load(checkpoint_path)
random_seed = checkpoint_dict["random_seed"]
logger.info("loaded random seed %s from checkpoint.", random_seed)
# We have to set clear optimizer to False since training should be continued
clear_optimizer = False
else:
logger.info(
f"=> no training loop checkpoint file found at '{checkpoint_path}'. Creating a new file."
)
if random_seed is None:
random_seed = random_non_negative_int()
logger.warning(
f"No random seed is specified. Setting to {random_seed}."
)
elif random_seed is None:
random_seed = random_non_negative_int()
logger.warning(f"No random seed is specified. Setting to {random_seed}.")
set_random_seed(random_seed)
result_tracker_cls: Type[ResultTracker] = get_result_tracker_cls(result_tracker)
result_tracker = result_tracker_cls(**(result_tracker_kwargs or {}))
if not metadata:
metadata = {}
title = metadata.get("title")
# Start tracking
result_tracker.start_run(run_name=title)
device = resolve_device(device)
dataset_instance: Dataset = get_dataset(
dataset=dataset,
dataset_kwargs=dataset_kwargs,
training=training,
testing=testing,
validation=validation,
)
if dataset is not None:
result_tracker.log_params(dict(dataset=dataset_instance.get_normalized_name()))
else: # means that dataset was defined by triples factories
result_tracker.log_params(dict(dataset="<user defined>"))
training, testing, validation = (
dataset_instance.training,
dataset_instance.testing,
dataset_instance.validation,
)
# evaluation restriction to a subset of entities/relations
if any(
f is not None
for f in (evaluation_entity_whitelist, evaluation_relation_whitelist)
):
testing = testing.new_with_restriction(
entities=evaluation_entity_whitelist,
relations=evaluation_relation_whitelist,
)
if validation is not None:
validation = validation.new_with_restriction(
entities=evaluation_entity_whitelist,
relations=evaluation_relation_whitelist,
)
if model_kwargs is None:
model_kwargs = {}
model_kwargs.update(preferred_device=device)
model_kwargs.setdefault("random_seed", random_seed)
if regularizer is not None:
# FIXME this should never happen.
if "regularizer" in model_kwargs:
logger.warning(
"Can not specify regularizer in kwargs and model_kwargs. removing from model_kwargs"
)
del model_kwargs["regularizer"]
regularizer_cls: Type[Regularizer] = get_regularizer_cls(regularizer)
model_kwargs["regularizer"] = regularizer_cls(
device=device,
**(regularizer_kwargs or {}),
)
if loss is not None:
if "loss" in model_kwargs: # FIXME
logger.warning(
"duplicate loss in kwargs and model_kwargs. removing from model_kwargs"
)
del model_kwargs["loss"]
loss_cls = get_loss_cls(loss)
_loss = loss_cls(**(loss_kwargs or {}))
model_kwargs.setdefault("loss", _loss)
model = get_model_cls(model)
model_instance: Model = model(
triples_factory=training,
**model_kwargs,
)
# Log model parameters
result_tracker.log_params(
params=dict(cls=model.__name__, kwargs=model_kwargs), prefix="model"
)
optimizer = get_optimizer_cls(optimizer)
training_loop = get_training_loop_cls(training_loop)
if optimizer_kwargs is None:
optimizer_kwargs = {}
# Log optimizer parameters
result_tracker.log_params(
params=dict(cls=optimizer.__name__, kwargs=optimizer_kwargs), prefix="optimizer"
)
optimizer_instance = optimizer(
params=model_instance.get_grad_params(),
**optimizer_kwargs,
)
result_tracker.log_params(
params=dict(cls=training_loop.__name__), prefix="training_loop"
)
if negative_sampler is None:
training_loop_instance: TrainingLoop = training_loop(
model=model_instance,
optimizer=optimizer_instance,
automatic_memory_optimization=automatic_memory_optimization,
)
elif training_loop is not SLCWATrainingLoop:
raise ValueError("Can not specify negative sampler with LCWA")
else:
negative_sampler = get_negative_sampler_cls(negative_sampler)
result_tracker.log_params(
params=dict(cls=negative_sampler.__name__, kwargs=negative_sampler_kwargs),
prefix="negative_sampler",
)
training_loop_instance: TrainingLoop = SLCWATrainingLoop(
model=model_instance,
optimizer=optimizer_instance,
automatic_memory_optimization=automatic_memory_optimization,
negative_sampler_cls=negative_sampler,
negative_sampler_kwargs=negative_sampler_kwargs,
)
evaluator = get_evaluator_cls(evaluator)
if evaluator_kwargs is None:
evaluator_kwargs = {}
evaluator_kwargs.setdefault(
"automatic_memory_optimization", automatic_memory_optimization
)
evaluator_instance: Evaluator = evaluator(**evaluator_kwargs)
if evaluation_kwargs is None:
evaluation_kwargs = {}
# Stopping
if "stopper" in training_kwargs and stopper is not None:
raise ValueError("Specified stopper in training_kwargs and as stopper")
if "stopper" in training_kwargs:
stopper = training_kwargs.pop("stopper")
if stopper_kwargs is None:
stopper_kwargs = {}
# Load the evaluation batch size for the stopper, if it has been set
_evaluation_batch_size = evaluation_kwargs.get("batch_size")
if _evaluation_batch_size is not None:
stopper_kwargs.setdefault("evaluation_batch_size", _evaluation_batch_size)
# By default there's a stopper that does nothing interesting
stopper_cls: Type[Stopper] = get_stopper_cls(stopper)
stopper: Stopper = stopper_cls(
model=model_instance,
evaluator=evaluator_instance,
evaluation_triples_factory=validation,
result_tracker=result_tracker,
**stopper_kwargs,
)
training_kwargs.setdefault("num_epochs", 5)
training_kwargs.setdefault("batch_size", 256)
result_tracker.log_params(params=training_kwargs, prefix="training")
# Add logging for debugging
logging.debug("Run Pipeline based on following config:")
if dataset is not None:
logging.debug(f"dataset: {dataset}")
logging.debug(f"dataset_kwargs: {dataset_kwargs}")
else:
logging.debug("training: %s", training)
logging.debug("testing: %s", testing)
if validation:
logging.debug("validation: %s", validation)
logging.debug(f"model: {model}")
logging.debug(f"model_kwargs: {model_kwargs}")
logging.debug(f"loss: {loss}")
logging.debug(f"loss_kwargs: {loss_kwargs}")
logging.debug(f"regularizer: {regularizer}")
logging.debug(f"regularizer_kwargs: {regularizer_kwargs}")
logging.debug(f"optimizer: {optimizer}")
logging.debug(f"optimizer_kwargs: {optimizer_kwargs}")
logging.debug(f"training_loop: {training_loop}")
logging.debug(f"negative_sampler: {negative_sampler}")
logging.debug(f"_negative_sampler_kwargs: {negative_sampler_kwargs}")
logging.debug(f"_training_kwargs: {training_kwargs}")
logging.debug(f"stopper: {stopper}")
logging.debug(f"stopper_kwargs: {stopper_kwargs}")
logging.debug(f"evaluator: {evaluator}")
logging.debug(f"evaluator_kwargs: {evaluator_kwargs}")
# Train like Cristiano Ronaldo
training_start_time = time.time()
losses = training_loop_instance.train(
stopper=stopper,
result_tracker=result_tracker,
clear_optimizer=clear_optimizer,
**training_kwargs,
)
training_end_time = time.time() - training_start_time
if use_testing_data:
mapped_triples = testing.mapped_triples
else:
mapped_triples = validation.mapped_triples
# Evaluate
# Reuse optimal evaluation parameters from training if available
if (
evaluator_instance.batch_size is not None
or evaluator_instance.slice_size is not None
):
evaluation_kwargs["batch_size"] = evaluator_instance.batch_size
evaluation_kwargs["slice_size"] = evaluator_instance.slice_size
# Add logging about evaluator for debugging
logging.debug("Evaluation will be run with following parameters:")
logging.debug(f"evaluation_kwargs: {evaluation_kwargs}")
evaluate_start_time = time.time()
metric_results: MetricResults = evaluator_instance.evaluate(
model=model_instance,
mapped_triples=mapped_triples,
**evaluation_kwargs,
)
evaluate_end_time = time.time() - evaluate_start_time
result_tracker.log_metrics(
metrics=metric_results.to_dict(),
step=training_kwargs.get("num_epochs"),
)
result_tracker.end_run()
return PipelineResult(
random_seed=random_seed,
model=model_instance,
training_loop=training_loop_instance,
losses=losses,
stopper=stopper,
metric_results=metric_results,
metadata=metadata,
train_seconds=training_end_time,
evaluate_seconds=evaluate_end_time,
)
|
https://github.com/pykeen/pykeen/issues/230
|
Traceback (most recent call last):
File "hpo_run.py", line 80, in <module>
run_hpo_search(args)
File "hpo_run.py", line 62, in run_hpo_search
hpo_pipeline_result.save_to_directory('study')
File "/home/user/anaconda3/envs/pykeen/lib/python3.8/site-packages/pykeen/hpo/hpo.py", line 327, in save_to_directory
json.dump(self._get_best_study_config(), file, indent=2, sort_keys=True)
File "/home/user/anaconda3/envs/pykeen/lib/python3.8/json/__init__.py", line 179, in dump
for chunk in iterable:
File "/home/user/anaconda3/envs/pykeen/lib/python3.8/json/encoder.py", line 431, in _iterencode
yield from _iterencode_dict(o, _current_indent_level)
File "/home/user/anaconda3/envs/pykeen/lib/python3.8/json/encoder.py", line 405, in _iterencode_dict
yield from chunks
File "/home/user/anaconda3/envs/pykeen/lib/python3.8/json/encoder.py", line 405, in _iterencode_dict
yield from chunks
File "/home/user/anaconda3/envs/pykeen/lib/python3.8/json/encoder.py", line 438, in _iterencode
o = _default(o)
File "/home/user/anaconda3/envs/pykeen/lib/python3.8/json/encoder.py", line 179, in default
raise TypeError(f'Object of type {o.__class__.__name__} '
TypeError: Object of type TriplesFactory is not JSON serializable
|
TypeError
|
def summary_str(self, title: Optional[str] = None, end="\n") -> str:
"""Make a summary string of all of the factories."""
rows = self._summary_rows()
n_triples = sum(count for *_, count in rows)
rows.append(("Total", "-", "-", n_triples))
t = tabulate(rows, headers=["Name", "Entities", "Relations", "Triples"])
return f"{title or self.__class__.__name__} (create_inverse_triples={self.create_inverse_triples})\n{t}{end}"
|
def summary_str(self, end="\n") -> str:
"""Make a summary string of all of the factories."""
rows = self._summary_rows()
n_triples = sum(count for *_, count in rows)
rows.append(("Total", "-", "-", n_triples))
t = tabulate(rows, headers=["Name", "Entities", "Relations", "Triples"])
return f"{self.__class__.__name__} (create_inverse_triples={self.create_inverse_triples})\n{t}{end}"
|
https://github.com/pykeen/pykeen/issues/146
|
Traceback (most recent call last):
File "/Users/cthoyt/dev/pykeen/scratch/tst.py", line 49, in <module>
main()
File "/Users/cthoyt/dev/pykeen/scratch/tst.py", line 36, in main
testing.new_with_restriction(relations=evaluation_relation_whitelist)
File "/Users/cthoyt/dev/pykeen/src/pykeen/triples/triples_factory.py", line 641, in new_with_restriction
relations = list(relations) + list(map(self.relation_to_inverse.__getitem__, relations))
KeyError: 'accusation_inverse'
|
KeyError
|
def summarize(self, title: Optional[str] = None, file=None) -> None:
"""Print a summary of the dataset."""
print(self.summary_str(title=title), file=file)
|
def summarize(self) -> None:
"""Print a summary of the dataset."""
print(self.summary_str())
|
https://github.com/pykeen/pykeen/issues/146
|
Traceback (most recent call last):
File "/Users/cthoyt/dev/pykeen/scratch/tst.py", line 49, in <module>
main()
File "/Users/cthoyt/dev/pykeen/scratch/tst.py", line 36, in main
testing.new_with_restriction(relations=evaluation_relation_whitelist)
File "/Users/cthoyt/dev/pykeen/src/pykeen/triples/triples_factory.py", line 641, in new_with_restriction
relations = list(relations) + list(map(self.relation_to_inverse.__getitem__, relations))
KeyError: 'accusation_inverse'
|
KeyError
|
def _load(self) -> None:
self._training = TriplesFactory.from_path(
path=self.training_path,
create_inverse_triples=self.create_inverse_triples,
)
self._testing = TriplesFactory.from_path(
path=self.testing_path,
entity_to_id=self._training.entity_to_id, # share entity index with training
relation_to_id=self._training.relation_to_id, # share relation index with training
create_inverse_triples=self.create_inverse_triples,
)
|
def _load(self) -> None:
self._training = TriplesFactory.from_path(
path=self.training_path,
create_inverse_triples=self.create_inverse_triples,
)
self._testing = TriplesFactory.from_path(
path=self.testing_path,
entity_to_id=self._training.entity_to_id, # share entity index with training
relation_to_id=self._training.relation_to_id, # share relation index with training
)
|
https://github.com/pykeen/pykeen/issues/146
|
Traceback (most recent call last):
File "/Users/cthoyt/dev/pykeen/scratch/tst.py", line 49, in <module>
main()
File "/Users/cthoyt/dev/pykeen/scratch/tst.py", line 36, in main
testing.new_with_restriction(relations=evaluation_relation_whitelist)
File "/Users/cthoyt/dev/pykeen/src/pykeen/triples/triples_factory.py", line 641, in new_with_restriction
relations = list(relations) + list(map(self.relation_to_inverse.__getitem__, relations))
KeyError: 'accusation_inverse'
|
KeyError
|
def _load_validation(self) -> None:
# don't call this function by itself. assumes called through the `validation`
# property and the _training factory has already been loaded
self._validation = TriplesFactory.from_path(
path=self.validation_path,
entity_to_id=self._training.entity_to_id, # share entity index with training
relation_to_id=self._training.relation_to_id, # share relation index with training
create_inverse_triples=self.create_inverse_triples,
)
|
def _load_validation(self) -> None:
# don't call this function by itself. assumes called through the `validation`
# property and the _training factory has already been loaded
self._validation = TriplesFactory.from_path(
path=self.validation_path,
entity_to_id=self._training.entity_to_id, # share entity index with training
relation_to_id=self._training.relation_to_id, # share relation index with training
)
|
https://github.com/pykeen/pykeen/issues/146
|
Traceback (most recent call last):
File "/Users/cthoyt/dev/pykeen/scratch/tst.py", line 49, in <module>
main()
File "/Users/cthoyt/dev/pykeen/scratch/tst.py", line 36, in main
testing.new_with_restriction(relations=evaluation_relation_whitelist)
File "/Users/cthoyt/dev/pykeen/src/pykeen/triples/triples_factory.py", line 641, in new_with_restriction
relations = list(relations) + list(map(self.relation_to_inverse.__getitem__, relations))
KeyError: 'accusation_inverse'
|
KeyError
|
def generate_triples(
num_entities: int = 33,
num_relations: int = 7,
num_triples: int = 101,
compact: bool = True,
random_state: TorchRandomHint = None,
) -> torch.LongTensor:
"""Generate random triples in a torch tensor."""
random_state = ensure_torch_random_state(random_state)
rv = torch.stack(
[
torch.randint(num_entities, size=(num_triples,), generator=random_state),
torch.randint(num_relations, size=(num_triples,), generator=random_state),
torch.randint(num_entities, size=(num_triples,), generator=random_state),
],
dim=1,
)
if compact:
new_entity_id = {entity: i for i, entity in enumerate(sorted(get_entities(rv)))}
new_relation_id = {
relation: i for i, relation in enumerate(sorted(get_relations(rv)))
}
rv = torch.as_tensor(
data=[
[new_entity_id[h], new_relation_id[r], new_entity_id[t]]
for h, r, t in rv.tolist()
],
dtype=torch.long,
)
return rv
|
def generate_triples(
num_entities: int = 33,
num_relations: int = 7,
num_triples: int = 101,
compact: bool = True,
random_state: RandomHint = None,
) -> np.ndarray:
"""Generate random triples."""
random_state = ensure_random_state(random_state)
rv = np.stack(
[
random_state.randint(num_entities, size=(num_triples,)),
random_state.randint(num_relations, size=(num_triples,)),
random_state.randint(num_entities, size=(num_triples,)),
],
axis=1,
)
if compact:
new_entity_id = {entity: i for i, entity in enumerate(sorted(get_entities(rv)))}
new_relation_id = {
relation: i for i, relation in enumerate(sorted(get_relations(rv)))
}
rv = np.asarray(
[
[new_entity_id[h], new_relation_id[r], new_entity_id[t]]
for h, r, t in rv
],
dtype=int,
)
return rv
|
https://github.com/pykeen/pykeen/issues/146
|
Traceback (most recent call last):
File "/Users/cthoyt/dev/pykeen/scratch/tst.py", line 49, in <module>
main()
File "/Users/cthoyt/dev/pykeen/scratch/tst.py", line 36, in main
testing.new_with_restriction(relations=evaluation_relation_whitelist)
File "/Users/cthoyt/dev/pykeen/src/pykeen/triples/triples_factory.py", line 641, in new_with_restriction
relations = list(relations) + list(map(self.relation_to_inverse.__getitem__, relations))
KeyError: 'accusation_inverse'
|
KeyError
|
def generate_labeled_triples(
num_entities: int = 33,
num_relations: int = 7,
num_triples: int = 101,
random_state: TorchRandomHint = None,
) -> np.ndarray:
"""Generate labeled random triples."""
mapped_triples = generate_triples(
num_entities=num_entities,
num_relations=num_relations,
num_triples=num_triples,
compact=False,
random_state=random_state,
)
entity_id_to_label = _make_id_to_labels(num_entities)
relation_id_to_label = _make_id_to_labels(num_relations)
return np.asarray(
[
(
entity_id_to_label[h],
relation_id_to_label[r],
entity_id_to_label[t],
)
for h, r, t in mapped_triples
],
dtype=str,
)
|
def generate_labeled_triples(
num_entities: int = 33,
num_relations: int = 7,
num_triples: int = 101,
random_state: RandomHint = None,
) -> np.ndarray:
"""Generate labeled random triples."""
t = generate_triples(
num_entities=num_entities,
num_relations=num_relations,
num_triples=num_triples,
compact=False,
random_state=random_state,
)
entity_id_to_label = _make_id_to_labels(num_entities)
relation_id_to_label = _make_id_to_labels(num_relations)
return np.asarray(
[
(
entity_id_to_label[h],
relation_id_to_label[r],
entity_id_to_label[t],
)
for h, r, t in t
],
dtype=str,
)
|
https://github.com/pykeen/pykeen/issues/146
|
Traceback (most recent call last):
File "/Users/cthoyt/dev/pykeen/scratch/tst.py", line 49, in <module>
main()
File "/Users/cthoyt/dev/pykeen/scratch/tst.py", line 36, in main
testing.new_with_restriction(relations=evaluation_relation_whitelist)
File "/Users/cthoyt/dev/pykeen/src/pykeen/triples/triples_factory.py", line 641, in new_with_restriction
relations = list(relations) + list(map(self.relation_to_inverse.__getitem__, relations))
KeyError: 'accusation_inverse'
|
KeyError
|
def generate_triples_factory(
num_entities: int = 33,
num_relations: int = 7,
num_triples: int = 101,
random_state: TorchRandomHint = None,
create_inverse_triples: bool = False,
) -> TriplesFactory:
"""Generate a triples factory with random triples."""
mapped_triples = generate_triples(
num_entities=num_entities,
num_relations=num_relations,
num_triples=num_triples,
random_state=random_state,
)
return TriplesFactory(
entity_to_id=_make_label_to_ids(num_entities),
relation_to_id=_make_label_to_ids(num_relations),
mapped_triples=mapped_triples,
create_inverse_triples=create_inverse_triples,
)
|
def generate_triples_factory(
num_entities: int = 33,
num_relations: int = 7,
num_triples: int = 101,
random_state: RandomHint = None,
create_inverse_triples: bool = False,
) -> TriplesFactory:
"""Generate a triples factory with random triples."""
triples = generate_labeled_triples(
num_entities=num_entities,
num_relations=num_relations,
num_triples=num_triples,
random_state=random_state,
)
return TriplesFactory.from_labeled_triples(
triples=triples,
create_inverse_triples=create_inverse_triples,
)
|
https://github.com/pykeen/pykeen/issues/146
|
Traceback (most recent call last):
File "/Users/cthoyt/dev/pykeen/scratch/tst.py", line 49, in <module>
main()
File "/Users/cthoyt/dev/pykeen/scratch/tst.py", line 36, in main
testing.new_with_restriction(relations=evaluation_relation_whitelist)
File "/Users/cthoyt/dev/pykeen/src/pykeen/triples/triples_factory.py", line 641, in new_with_restriction
relations = list(relations) + list(map(self.relation_to_inverse.__getitem__, relations))
KeyError: 'accusation_inverse'
|
KeyError
|
def __init__(
self,
triples_factory: TriplesFactory,
minimum_frequency: Optional[float] = None,
symmetric: bool = True,
):
"""Index the inverse frequencies and the inverse relations in the triples factory.
:param triples_factory: The triples factory to index.
:param minimum_frequency: The minimum overlap between two relations' triples to consider them as inverses. The
default value, 0.97, is taken from `Toutanova and Chen (2015) <https://www.aclweb.org/anthology/W15-4007/>`_,
who originally described the generation of FB15k-237.
"""
self.triples_factory = triples_factory
if minimum_frequency is None:
minimum_frequency = 0.97
self.minimum_frequency = minimum_frequency
# compute similarities
if symmetric:
rel, inv = triples_factory_to_sparse_matrices(triples_factory=triples_factory)
self.candidate_duplicate_relations = get_candidate_pairs(
a=rel, threshold=self.minimum_frequency
)
self.candidate_inverse_relations = get_candidate_pairs(
a=rel, b=inv, threshold=self.minimum_frequency
)
else:
raise NotImplementedError
logger.info(
f"identified {len(self.candidate_duplicate_relations)} candidate duplicate relationships"
f" at similarity > {self.minimum_frequency} in {self.triples_factory}.",
)
logger.info(
f"identified {len(self.candidate_inverse_relations)} candidate inverse pairs"
f" at similarity > {self.minimum_frequency} in {self.triples_factory}",
)
self.candidates = set(self.candidate_duplicate_relations).union(
self.candidate_inverse_relations
)
sizes = dict(zip(*triples_factory.mapped_triples[:, 1].unique(return_counts=True)))
self.relations_to_delete = _select_by_most_pairs(
components=_get_connected_components(
pairs=((a, b) for (s, a, b) in self.candidates if (a != b))
),
size=sizes,
)
logger.info(
f"identified {len(self.candidates)} from {self.triples_factory} to delete"
)
|
def __init__(
self,
triples_factory: TriplesFactory,
minimum_frequency: Optional[float] = None,
symmetric: bool = True,
use_tqdm: bool = True,
use_multiprocessing: bool = False,
):
"""Index the inverse frequencies and the inverse relations in the triples factory.
:param triples_factory: The triples factory to index.
:param minimum_frequency: The minimum overlap between two relations' triples to consider them as inverses. The
default value, 0.97, is taken from `Toutanova and Chen (2015) <https://www.aclweb.org/anthology/W15-4007/>`_,
who originally described the generation of FB15k-237.
"""
self.triples_factory = triples_factory
if minimum_frequency is None:
minimum_frequency = 0.97
self.minimum_frequency = minimum_frequency
if use_multiprocessing:
use_tqdm = False
self.candidate_duplicate_relations = get_candidate_duplicate_relations(
triples_factory=self.triples_factory,
minimum_frequency=self.minimum_frequency,
symmetric=symmetric,
use_tqdm=use_tqdm,
use_multiprocessing=use_multiprocessing,
)
logger.info(
f"identified {len(self.candidate_duplicate_relations)} candidate duplicate relationships"
f" at similarity > {self.minimum_frequency} in {self.triples_factory}.",
)
self.duplicate_relations_to_delete = {
r for r, _ in self.candidate_duplicate_relations
}
self.candidate_inverse_relations = get_candidate_inverse_relations(
triples_factory=self.triples_factory,
minimum_frequency=self.minimum_frequency,
symmetric=symmetric,
use_tqdm=use_tqdm,
use_multiprocessing=use_multiprocessing,
)
logger.info(
f"identified {len(self.candidate_inverse_relations)} candidate inverse pairs"
f" at similarity > {self.minimum_frequency} in {self.triples_factory}",
)
if symmetric:
self.inverses = dict(
tuple(sorted(k)) for k in self.candidate_inverse_relations.keys()
)
self.inverse_relations_to_delete = set(self.inverses.values())
else:
self.mutual_inverse = set()
self.not_mutual_inverse = set()
for r1, r2 in self.candidate_inverse_relations:
if (r2, r1) in self.candidate_inverse_relations:
self.mutual_inverse.add((r1, r2))
else:
self.not_mutual_inverse.add((r1, r2))
logger.info(
f"{len(self.mutual_inverse)} are mutual inverse ({len(self.mutual_inverse) // 2}"
f" relations) and {len(self.not_mutual_inverse)} non-mutual inverse.",
)
# basically take all candidates
self.inverses = dict(self.candidate_inverse_relations.keys())
self.inverse_relations_to_delete = prioritize_mapping(
self.candidate_inverse_relations
)
logger.info(
f"identified {len(self.inverse_relations_to_delete)} from {self.triples_factory} to delete"
)
|
https://github.com/pykeen/pykeen/issues/146
|
Traceback (most recent call last):
File "/Users/cthoyt/dev/pykeen/scratch/tst.py", line 49, in <module>
main()
File "/Users/cthoyt/dev/pykeen/scratch/tst.py", line 36, in main
testing.new_with_restriction(relations=evaluation_relation_whitelist)
File "/Users/cthoyt/dev/pykeen/src/pykeen/triples/triples_factory.py", line 641, in new_with_restriction
relations = list(relations) + list(map(self.relation_to_inverse.__getitem__, relations))
KeyError: 'accusation_inverse'
|
KeyError
|
def apply(self, triples_factory: TriplesFactory) -> TriplesFactory:
"""Make a new triples factory containing neither duplicate nor inverse relationships."""
return triples_factory.new_with_restriction(
relations=self.relations_to_delete, invert_relation_selection=True
)
|
def apply(self, triples_factory: TriplesFactory) -> TriplesFactory:
"""Make a new triples factory containing neither duplicate nor inverse relationships."""
return triples_factory.new_without_relations(self.relations_to_delete)
|
https://github.com/pykeen/pykeen/issues/146
|
Traceback (most recent call last):
File "/Users/cthoyt/dev/pykeen/scratch/tst.py", line 49, in <module>
main()
File "/Users/cthoyt/dev/pykeen/scratch/tst.py", line 36, in main
testing.new_with_restriction(relations=evaluation_relation_whitelist)
File "/Users/cthoyt/dev/pykeen/src/pykeen/triples/triples_factory.py", line 641, in new_with_restriction
relations = list(relations) + list(map(self.relation_to_inverse.__getitem__, relations))
KeyError: 'accusation_inverse'
|
KeyError
|
def unleak(
train: TriplesFactory,
*triples_factories: TriplesFactory,
n: Union[None, int, float] = None,
minimum_frequency: Optional[float] = None,
) -> Iterable[TriplesFactory]:
"""Unleak a train, test, and validate triples factory.
:param train: The target triples factory
:param triples_factories: All other triples factories (test, validate, etc.)
:param n: Either the (integer) number of top relations to keep or the (float) percentage of top relationships
to keep. If left none, frequent relations are not removed.
:param minimum_frequency: The minimum overlap between two relations' triples to consider them as inverses or
duplicates. The default value, 0.97, is taken from
`Toutanova and Chen (2015) <https://www.aclweb.org/anthology/W15-4007/>`_, who originally described the generation
of FB15k-237.
"""
if n is not None:
frequent_relations = train.get_most_frequent_relations(n=n)
logger.info(f"keeping most frequent relations from {train}")
train = train.new_with_restriction(relations=frequent_relations)
triples_factories = [
triples_factory.new_with_restriction(relations=frequent_relations)
for triples_factory in triples_factories
]
# Calculate which relations are the inverse ones
sealant = Sealant(train, minimum_frequency=minimum_frequency)
if not sealant.relations_to_delete:
logger.info(f"no relations to delete identified from {train}")
else:
train = sealant.apply(train)
triples_factories = [
sealant.apply(triples_factory) for triples_factory in triples_factories
]
return reindex(train, *triples_factories)
|
def unleak(
train: TriplesFactory,
*triples_factories: TriplesFactory,
n: Union[None, int, float] = None,
minimum_frequency: Optional[float] = None,
) -> Iterable[TriplesFactory]:
"""Unleak a train, test, and validate triples factory.
:param train: The target triples factory
:param triples_factories: All other triples factories (test, validate, etc.)
:param n: Either the (integer) number of top relations to keep or the (float) percentage of top relationships
to keep. If left none, frequent relations are not removed.
:param minimum_frequency: The minimum overlap between two relations' triples to consider them as inverses or
duplicates. The default value, 0.97, is taken from
`Toutanova and Chen (2015) <https://www.aclweb.org/anthology/W15-4007/>`_, who originally described the generation
of FB15k-237.
"""
if n is not None:
frequent_relations = train.get_most_frequent_relations(n=n)
logger.info(f"keeping most frequent relations from {train}")
train = train.new_with_relations(frequent_relations)
triples_factories = [
triples_factory.new_with_relations(frequent_relations)
for triples_factory in triples_factories
]
# Calculate which relations are the inverse ones
sealant = Sealant(train, minimum_frequency=minimum_frequency)
if not sealant.relations_to_delete:
logger.info(f"no relations to delete identified from {train}")
else:
train = sealant.apply(train)
triples_factories = [
sealant.apply(triples_factory) for triples_factory in triples_factories
]
return reindex(train, *triples_factories)
|
https://github.com/pykeen/pykeen/issues/146
|
Traceback (most recent call last):
File "/Users/cthoyt/dev/pykeen/scratch/tst.py", line 49, in <module>
main()
File "/Users/cthoyt/dev/pykeen/scratch/tst.py", line 36, in main
testing.new_with_restriction(relations=evaluation_relation_whitelist)
File "/Users/cthoyt/dev/pykeen/src/pykeen/triples/triples_factory.py", line 641, in new_with_restriction
relations = list(relations) + list(map(self.relation_to_inverse.__getitem__, relations))
KeyError: 'accusation_inverse'
|
KeyError
|
def reindex(*triples_factories: TriplesFactory) -> List[TriplesFactory]:
"""Reindex a set of triples factories."""
# get entities and relations occurring in triples
all_triples = torch.cat(
[factory.mapped_triples for factory in triples_factories], dim=0
)
# generate ID translation and new label to Id mappings
one_factory = triples_factories[0]
(entity_to_id, entity_id_translation), (relation_to_id, relation_id_translation) = [
_generate_compact_vectorized_lookup(
ids=all_triples[:, cols],
label_to_id=label_to_id,
)
for cols, label_to_id in (
([0, 2], one_factory.entity_to_id),
(1, one_factory.relation_to_id),
)
]
return [
TriplesFactory(
entity_to_id=entity_to_id,
relation_to_id=relation_to_id,
mapped_triples=_translate_triples(
triples=factory.mapped_triples,
entity_translation=entity_id_translation,
relation_translation=relation_id_translation,
),
create_inverse_triples=factory.create_inverse_triples,
)
for factory in triples_factories
]
|
def reindex(*triples_factories: TriplesFactory) -> List[TriplesFactory]:
"""Reindex a set of triples factories."""
triples = np.concatenate(
[triples_factory.triples for triples_factory in triples_factories],
axis=0,
)
entity_to_id = create_entity_mapping(triples)
relation_to_id = create_relation_mapping(set(triples[:, 1]))
return [
TriplesFactory.from_labeled_triples(
triples=triples_factory.triples,
entity_to_id=entity_to_id,
relation_to_id=relation_to_id,
# FIXME doesn't carry flag of create_inverse_triples through
)
for triples_factory in triples_factories
]
|
https://github.com/pykeen/pykeen/issues/146
|
Traceback (most recent call last):
File "/Users/cthoyt/dev/pykeen/scratch/tst.py", line 49, in <module>
main()
File "/Users/cthoyt/dev/pykeen/scratch/tst.py", line 36, in main
testing.new_with_restriction(relations=evaluation_relation_whitelist)
File "/Users/cthoyt/dev/pykeen/src/pykeen/triples/triples_factory.py", line 641, in new_with_restriction
relations = list(relations) + list(map(self.relation_to_inverse.__getitem__, relations))
KeyError: 'accusation_inverse'
|
KeyError
|
def _main():
"""Test unleaking FB15K.
Run with ``python -m pykeen.triples.leakage``.
"""
from pykeen.datasets import get_dataset
logging.basicConfig(format="pykeen: %(message)s", level=logging.INFO)
fb15k = get_dataset(dataset="fb15k")
fb15k.summarize()
n = 401 # magic 401 from the paper
train, test, validate = unleak(fb15k.training, fb15k.testing, fb15k.validation, n=n)
print()
EagerDataset(train, test, validate).summarize(title="FB15k (cleaned)")
fb15k237 = get_dataset(dataset="fb15k237")
print("\nSummary FB15K-237")
fb15k237.summarize()
|
def _main():
"""Test unleaking FB15K.
Run with ``python -m pykeen.triples.leakage``.
"""
from pykeen.datasets import get_dataset
logging.basicConfig(format="pykeen: %(message)s", level=logging.INFO)
print("Summary FB15K")
fb15k = get_dataset(dataset="fb15k")
summarize(fb15k.training, fb15k.testing, fb15k.validation)
print("\nSummary FB15K (cleaned)")
n = 401 # magic 401 from the paper
train, test, validate = unleak(fb15k.training, fb15k.testing, fb15k.validation, n=n)
summarize(train, test, validate)
print("\nSummary FB15K-237")
fb15k237 = get_dataset(dataset="fb15k237")
summarize(fb15k237.training, fb15k237.testing, fb15k237.validation)
|
https://github.com/pykeen/pykeen/issues/146
|
Traceback (most recent call last):
File "/Users/cthoyt/dev/pykeen/scratch/tst.py", line 49, in <module>
main()
File "/Users/cthoyt/dev/pykeen/scratch/tst.py", line 36, in main
testing.new_with_restriction(relations=evaluation_relation_whitelist)
File "/Users/cthoyt/dev/pykeen/src/pykeen/triples/triples_factory.py", line 641, in new_with_restriction
relations = list(relations) + list(map(self.relation_to_inverse.__getitem__, relations))
KeyError: 'accusation_inverse'
|
KeyError
|
def from_labeled_triples(
cls,
triples: LabeledTriples,
create_inverse_triples: bool = False,
entity_to_id: Optional[EntityMapping] = None,
relation_to_id: Optional[RelationMapping] = None,
compact_id: bool = True,
filter_out_candidate_inverse_relations: bool = True,
) -> "TriplesFactory":
"""
Create a new triples factory from label-based triples.
:param triples: shape: (n, 3), dtype: str
The label-based triples.
:param create_inverse_triples:
Whether to create inverse triples.
:param entity_to_id:
The mapping from entity labels to ID. If None, create a new one from the triples.
:param relation_to_id:
The mapping from relations labels to ID. If None, create a new one from the triples.
:param compact_id:
Whether to compact IDs such that the IDs are consecutive.
:param filter_out_candidate_inverse_relations:
Whether to remove triples with relations with the inverse suffix.
:return:
A new triples factory.
"""
# Check if the triples are inverted already
# We re-create them pure index based to ensure that _all_ inverse triples are present and that they are
# contained if and only if create_inverse_triples is True.
if filter_out_candidate_inverse_relations:
unique_relations, inverse = np.unique(triples[:, 1], return_inverse=True)
suspected_to_be_inverse_relations = {
r for r in unique_relations if r.endswith(INVERSE_SUFFIX)
}
if len(suspected_to_be_inverse_relations) > 0:
logger.warning(
f"Some triples already have the inverse relation suffix {INVERSE_SUFFIX}. "
f"Re-creating inverse triples to ensure consistency. You may disable this behaviour by passing "
f"filter_out_candidate_inverse_relations=False",
)
relation_ids_to_remove = [
i
for i, r in enumerate(unique_relations.tolist())
if r in suspected_to_be_inverse_relations
]
mask = np.isin(
element=inverse, test_elements=relation_ids_to_remove, invert=True
)
logger.info(f"keeping {mask.sum() / mask.shape[0]} triples.")
triples = triples[mask]
# Generate entity mapping if necessary
if entity_to_id is None:
entity_to_id = create_entity_mapping(triples=triples)
if compact_id:
entity_to_id = compact_mapping(mapping=entity_to_id)[0]
# Generate relation mapping if necessary
if relation_to_id is None:
relation_to_id = create_relation_mapping(triples[:, 1])
if compact_id:
relation_to_id = compact_mapping(mapping=relation_to_id)[0]
# Map triples of labels to triples of IDs.
mapped_triples = _map_triples_elements_to_ids(
triples=triples,
entity_to_id=entity_to_id,
relation_to_id=relation_to_id,
)
return cls(
entity_to_id=entity_to_id,
relation_to_id=relation_to_id,
mapped_triples=mapped_triples,
create_inverse_triples=create_inverse_triples,
)
|
def from_labeled_triples(
cls,
triples: LabeledTriples,
create_inverse_triples: bool = False,
entity_to_id: Optional[EntityMapping] = None,
relation_to_id: Optional[RelationMapping] = None,
compact_id: bool = True,
) -> "TriplesFactory":
"""
Create a new triples factory from label-based triples.
:param triples: shape: (n, 3), dtype: str
The label-based triples.
:param create_inverse_triples:
Whether to create inverse triples.
:param entity_to_id:
The mapping from entity labels to ID. If None, create a new one from the triples.
:param relation_to_id:
The mapping from relations labels to ID. If None, create a new one from the triples.
:param compact_id:
Whether to compact IDs such that the IDs are consecutive.
:return:
A new triples factory.
"""
relations = triples[:, 1]
unique_relations = set(relations)
# Check if the triples are inverted already
relations_already_inverted = cls._check_already_inverted_relations(unique_relations)
# TODO: invert triples id-based
if create_inverse_triples or relations_already_inverted:
create_inverse_triples = True
if relations_already_inverted:
logger.info(
f"Some triples already have suffix {INVERSE_SUFFIX}. "
f"Creating TriplesFactory based on inverse triples",
)
relation_to_inverse = {
re.sub(
"_inverse$", "", relation
): f"{re.sub('_inverse$', '', relation)}{INVERSE_SUFFIX}"
for relation in unique_relations
}
else:
relation_to_inverse = {
relation: f"{relation}{INVERSE_SUFFIX}" for relation in unique_relations
}
inverse_triples = np.stack(
[
triples[:, 2],
np.array(
[relation_to_inverse[relation] for relation in relations],
dtype=np.str,
),
triples[:, 0],
],
axis=-1,
)
# extend original triples with inverse ones
triples = np.concatenate([triples, inverse_triples], axis=0)
else:
create_inverse_triples = False
relation_to_inverse = None
# Generate entity mapping if necessary
if entity_to_id is None:
entity_to_id = create_entity_mapping(triples=triples)
if compact_id:
entity_to_id = compact_mapping(mapping=entity_to_id)[0]
# Generate relation mapping if necessary
if relation_to_id is None:
if create_inverse_triples:
relation_to_id = create_relation_mapping(
set(relation_to_inverse.keys()).union(
set(relation_to_inverse.values())
),
)
else:
relation_to_id = create_relation_mapping(unique_relations)
if compact_id:
relation_to_id = compact_mapping(mapping=relation_to_id)[0]
# Map triples of labels to triples of IDs.
mapped_triples = _map_triples_elements_to_ids(
triples=triples,
entity_to_id=entity_to_id,
relation_to_id=relation_to_id,
)
return cls(
entity_to_id=entity_to_id,
relation_to_id=relation_to_id,
_triples=triples,
mapped_triples=mapped_triples,
relation_to_inverse=relation_to_inverse,
)
|
https://github.com/pykeen/pykeen/issues/146
|
Traceback (most recent call last):
File "/Users/cthoyt/dev/pykeen/scratch/tst.py", line 49, in <module>
main()
File "/Users/cthoyt/dev/pykeen/scratch/tst.py", line 36, in main
testing.new_with_restriction(relations=evaluation_relation_whitelist)
File "/Users/cthoyt/dev/pykeen/src/pykeen/triples/triples_factory.py", line 641, in new_with_restriction
relations = list(relations) + list(map(self.relation_to_inverse.__getitem__, relations))
KeyError: 'accusation_inverse'
|
KeyError
|
def num_relations(self) -> int: # noqa: D401
"""The number of unique relations."""
if self.create_inverse_triples:
return 2 * self.real_num_relations
return self.real_num_relations
|
def num_relations(self) -> int: # noqa: D401
"""The number of unique relations."""
return len(self.relation_to_id)
|
https://github.com/pykeen/pykeen/issues/146
|
Traceback (most recent call last):
File "/Users/cthoyt/dev/pykeen/scratch/tst.py", line 49, in <module>
main()
File "/Users/cthoyt/dev/pykeen/scratch/tst.py", line 36, in main
testing.new_with_restriction(relations=evaluation_relation_whitelist)
File "/Users/cthoyt/dev/pykeen/src/pykeen/triples/triples_factory.py", line 641, in new_with_restriction
relations = list(relations) + list(map(self.relation_to_inverse.__getitem__, relations))
KeyError: 'accusation_inverse'
|
KeyError
|
def triples(self) -> np.ndarray: # noqa: D401
"""The labeled triples, a 3-column matrix where each row are the head label, relation label, then tail label."""
logger.warning(
"Reconstructing all label-based triples. This is expensive and rarely needed."
)
return self.label_triples(self.mapped_triples)
|
def triples(self) -> np.ndarray: # noqa: D401
"""The labeled triples."""
# TODO: Deprecation warning. Will be replaced by re-constructing them from ID-based + mapping soon.
return self._triples
|
https://github.com/pykeen/pykeen/issues/146
|
Traceback (most recent call last):
File "/Users/cthoyt/dev/pykeen/scratch/tst.py", line 49, in <module>
main()
File "/Users/cthoyt/dev/pykeen/scratch/tst.py", line 36, in main
testing.new_with_restriction(relations=evaluation_relation_whitelist)
File "/Users/cthoyt/dev/pykeen/src/pykeen/triples/triples_factory.py", line 641, in new_with_restriction
relations = list(relations) + list(map(self.relation_to_inverse.__getitem__, relations))
KeyError: 'accusation_inverse'
|
KeyError
|
def get_inverse_relation_id(self, relation: Union[str, int]) -> int:
"""Get the inverse relation identifier for the given relation."""
if not self.create_inverse_triples:
raise ValueError("Can not get inverse triple, they have not been created.")
relation = next(iter(self.relations_to_ids(relations=[relation])))
return self._get_inverse_relation_id(relation)
|
def get_inverse_relation_id(self, relation: str) -> int:
"""Get the inverse relation identifier for the given relation."""
if not self.create_inverse_triples:
raise ValueError("Can not get inverse triple, they have not been created.")
inverse_relation = self.relation_to_inverse[relation]
return self.relation_to_id[inverse_relation]
|
https://github.com/pykeen/pykeen/issues/146
|
Traceback (most recent call last):
File "/Users/cthoyt/dev/pykeen/scratch/tst.py", line 49, in <module>
main()
File "/Users/cthoyt/dev/pykeen/scratch/tst.py", line 36, in main
testing.new_with_restriction(relations=evaluation_relation_whitelist)
File "/Users/cthoyt/dev/pykeen/src/pykeen/triples/triples_factory.py", line 641, in new_with_restriction
relations = list(relations) + list(map(self.relation_to_inverse.__getitem__, relations))
KeyError: 'accusation_inverse'
|
KeyError
|
def create_slcwa_instances(self) -> Instances:
"""Create sLCWA instances for this factory's triples."""
return SLCWAInstances(
mapped_triples=self._add_inverse_triples_if_necessary(
mapped_triples=self.mapped_triples
)
)
|
def create_slcwa_instances(self) -> Instances:
"""Create sLCWA instances for this factory's triples."""
return SLCWAInstances(mapped_triples=self.mapped_triples)
|
https://github.com/pykeen/pykeen/issues/146
|
Traceback (most recent call last):
File "/Users/cthoyt/dev/pykeen/scratch/tst.py", line 49, in <module>
main()
File "/Users/cthoyt/dev/pykeen/scratch/tst.py", line 36, in main
testing.new_with_restriction(relations=evaluation_relation_whitelist)
File "/Users/cthoyt/dev/pykeen/src/pykeen/triples/triples_factory.py", line 641, in new_with_restriction
relations = list(relations) + list(map(self.relation_to_inverse.__getitem__, relations))
KeyError: 'accusation_inverse'
|
KeyError
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.