def test_component_for_duck_typing(): view = NGLWidget() traj = pt.load(nv.datafiles.PDB) view.add_component(get_fn('tz2.pdb')) view.add_component(get_fn('tz2_2.pdb.gz')) view.add_trajectory(nv.PyTrajTrajectory(traj)) view.component_0.add_representation('cartoon') c0 = view[0] c1 = view[1] assert hasattr(view, 'component_0') assert hasattr(view, 'component_1') assert hasattr(view, 'trajectory_0') assert hasattr(view.trajectory_0, 'n_frames') assert hasattr(view.trajectory_0, 'get_coordinates') assert hasattr(view.trajectory_0, 'get_structure_string') c0.show() c0.hide() view.remove_component(c0.id) assert not hasattr(view, 'component_2') # negative indexing assert view[-1]._index == c1._index
def test_component_for_duck_typing(): # FIXME: deprecate duck typing? # syntax looks ugly. view = NGLWidget() traj = pt.load(nv.datafiles.PDB) # add 3 components (trajectory is a component) view.add_component(get_fn('tz2.pdb')) view.add_component(get_fn('tz2_2.pdb.gz')) view.add_trajectory(nv.PyTrajTrajectory(traj)) view.component_0.add_representation('cartoon') c0 = view[0] c1 = view[1] assert hasattr(view, 'component_0') assert hasattr(view, 'component_1') assert hasattr(view, 'trajectory_0') assert hasattr(view.trajectory_0, 'n_frames') assert hasattr(view.trajectory_0, 'get_coordinates') assert hasattr(view.trajectory_0, 'get_structure_string') c0.show() c0.hide() # 2 components left view.remove_component(c0.id) # c1 become 1st component assert not hasattr(view, 'component_2') assert len(view._ngl_component_ids) == 2 # negative indexing assert view[0]._index == c1._index
def test_handling_n_components_changed(): view = nv.NGLWidget() n_traj = nv.PyTrajTrajectory(pt.load(nv.datafiles.PDB)) view.add_trajectory(n_traj) # fake updating n_components and _repr_dict from front-end view._ngl_repr_dict = REPR_DICT view.n_components = 1 view.player.widget_repr = view.player._make_widget_repr() view.remove_component(n_traj.id) # fake updating n_components from front-end view._ngl_repr_dict = {'c0': {}} view.n_components = 0
def test_load_data(): view = nv.show_pytraj(pt.datafiles.load_tz2()) # load blob with ext blob = open(nv.datafiles.PDB).read() view._load_data(blob, ext='pdb') # raise if passing blob but does not provide ext nt.assert_raises(ValueError, view._load_data, blob) # load PyTrajectory t0 = nv.PyTrajTrajectory(pt.datafiles.load_ala3()) view._load_data(t0)
def test_component_for_duck_typing(): view = NGLWidget() traj = pt.load(nv.datafiles.PDB) view.add_component('data/tz2.pdb') view.add_component('data/tz2_2.pdb.gz') view.add_trajectory(nv.PyTrajTrajectory(traj)) c0 = view[0] c1 = view[1] nt.assert_true(hasattr(view, 'component_0')) nt.assert_true(hasattr(view, 'component_1')) nt.assert_true(hasattr(view, 'trajectory_0')) nt.assert_true(hasattr(view.trajectory_0, 'n_frames')) nt.assert_true(hasattr(view.trajectory_0, 'get_coordinates')) nt.assert_true(hasattr(view.trajectory_0, 'get_structure_string')) c0.show() c0.hide() view.remove_component(c0.id) nt.assert_false(hasattr(view, 'component_2'))
def test_load_data(): view = nv.show_pytraj(pt.datafiles.load_tz2()) # load blob with ext blob = open(nv.datafiles.PDB).read() view._load_data(blob, ext='pdb') # raise if passing blob but does not provide ext with pytest.raises(ValueError): view._load_data(blob) # raise if passing dummy name with pytest.raises(NameError): view._load_data(hahahaha) # load PyTrajectory t0 = nv.PyTrajTrajectory(pt.datafiles.load_ala3()) view._load_data(t0) # load current folder view._load_data(get_fn('tz2.pdb'))
def test_loaded_attribute(): traj = pt.datafiles.load_tz2() structure = nv.FileStructure(nv.datafiles.PDB) # False, empty constructor view = nv.NGLWidget() view.loaded = False view.add_structure(structure) view.add_trajectory(traj) view # False, constructor with a single Structure view = nv.NGLWidget(structure) view.loaded = False view.add_trajectory(traj) view # True view = nv.NGLWidget() view.loaded = True view.add_structure(structure) view.add_trajectory(traj) view # False then True, empty constructor view = nv.NGLWidget() view.loaded = False view.add_structure(structure) view.loaded = True view.add_trajectory(traj) view # False then True, constructor with a Trajectory view = nv.NGLWidget(nv.PyTrajTrajectory(traj)) view.loaded = False view.add_structure(structure) view.loaded = True view.add_trajectory(traj) view
def test_trajectory_show_hide_sending_cooridnates(): view = NGLWidget() traj0 = pt.datafiles.load_tz2() traj1 = pt.datafiles.load_trpcage() view.add_trajectory(nv.PyTrajTrajectory(traj0)) view.add_trajectory(nv.PyTrajTrajectory(traj1)) for traj in view._trajlist: nt.assert_true(traj.shown) view.frame = 1 def copy_coordinate_dict(view): # make copy to avoid memory free return dict((k, v.copy()) for k, v in view.coordinates_dict.items()) coordinates_dict = copy_coordinate_dict(view) aa_eq(coordinates_dict[0], traj0[1].xyz) aa_eq(coordinates_dict[1], traj1[1].xyz) # hide 0 view.hide([ 0, ]) nt.assert_false(view._trajlist[0].shown) nt.assert_true(view._trajlist[1].shown) # update frame so view can update its coordinates view.frame = 2 coordinates_dict = copy_coordinate_dict(view) nt.assert_equal(coordinates_dict[0].shape[0], 0) aa_eq(coordinates_dict[1], traj1[2].xyz) # hide 0, 1 view.hide([0, 1]) nt.assert_false(view._trajlist[0].shown) nt.assert_false(view._trajlist[1].shown) view.frame = 3 coordinates_dict = copy_coordinate_dict(view) nt.assert_equal(coordinates_dict[0].shape[0], 0) nt.assert_equal(coordinates_dict[1].shape[0], 0) # slicing, show only component 1 view[1].show() view.frame = 0 nt.assert_false(view._trajlist[0].shown) nt.assert_true(view._trajlist[1].shown) coordinates_dict = copy_coordinate_dict(view) nt.assert_equal(coordinates_dict[0].shape[0], 0) aa_eq(coordinates_dict[1], traj1[0].xyz) # show all view[1].show() view[0].show() view.frame = 1 nt.assert_true(view._trajlist[0].shown) nt.assert_true(view._trajlist[1].shown) coordinates_dict = copy_coordinate_dict(view) aa_eq(coordinates_dict[0], traj0[1].xyz) aa_eq(coordinates_dict[1], traj1[1].xyz) # hide all view[1].hide() view[0].hide() view.frame = 2 nt.assert_false(view._trajlist[0].shown) nt.assert_false(view._trajlist[1].shown) coordinates_dict = copy_coordinate_dict(view) nt.assert_equal(coordinates_dict[0].shape[0], 0) nt.assert_equal(coordinates_dict[1].shape[0], 0)
def test_interpolate(): # dummy test traj = pt.datafiles.load_tz2() ngl_traj = nv.PyTrajTrajectory(traj) interpolate.linear(0, 0.4, ngl_traj, step=1)
def test_trajectory_show_hide_sending_cooridnates(): view = NGLWidget() traj0 = pt.datafiles.load_tz2() traj1 = pt.datafiles.load_trpcage() view.add_trajectory(nv.PyTrajTrajectory(traj0)) view.add_trajectory(nv.PyTrajTrajectory(traj1)) for traj in view._trajlist: assert traj.shown view.frame = 1 def copy_coordinate_dict(view): # make copy to avoid memory free return {k: v.copy() for k, v in view._coordinates_dict.items()} coordinates_dict = copy_coordinate_dict(view) aa_eq(coordinates_dict[0], traj0[1].xyz) aa_eq(coordinates_dict[1], traj1[1].xyz) # hide 0 view.hide([ 0, ]) assert not view._trajlist[0].shown assert view._trajlist[1].shown # update frame so view can update its coordinates view.frame = 2 coordinates_dict = copy_coordinate_dict(view) assert coordinates_dict[0].shape[0] == 0 aa_eq(coordinates_dict[1], traj1[2].xyz) # hide 0, 1 view.hide([0, 1]) assert not view._trajlist[0].shown assert not view._trajlist[1].shown view.frame = 3 coordinates_dict = copy_coordinate_dict(view) assert coordinates_dict[0].shape[0] == 0 assert coordinates_dict[1].shape[0] == 0 # slicing, show only component 1 view[1].show() view.frame = 0 assert not view._trajlist[0].shown assert view._trajlist[1].shown coordinates_dict = copy_coordinate_dict(view) assert coordinates_dict[0].shape[0] == 0 aa_eq(coordinates_dict[1], traj1[0].xyz) # show all view[1].show() view[0].show() view.show(indices='all') view.show(indices=[ 0, ]) view.show(indices=[0, 1]) view.frame = 1 assert view._trajlist[1].shown coordinates_dict = copy_coordinate_dict(view) aa_eq(coordinates_dict[0], traj0[1].xyz) aa_eq(coordinates_dict[1], traj1[1].xyz) # hide all view[1].hide() view[0].hide() view.frame = 2 assert not view._trajlist[0].shown assert not view._trajlist[1].shown coordinates_dict = copy_coordinate_dict(view) assert coordinates_dict[0].shape[0] == 0 assert coordinates_dict[1].shape[0] == 0
def test_API_promise_to_have(): # for Jupyter notebook extension nv._jupyter_nbextension_paths() view = nv.demo() # trigger _set_size with patch.object(view, '_remote_call') as mock_call: view.layout.width = '100px' view.layout.height = '500px' mock_call.assert_called_with('setSize', args=['', '500px'], target='Widget') # Structure structure = nv.Structure() structure.get_structure_string assert hasattr(structure, 'id') assert hasattr(structure, 'ext') assert hasattr(structure, 'params') # Widget nv.NGLWidget._set_coordinates nv.NGLWidget.add_component nv.NGLWidget.add_trajectory nv.NGLWidget._coordinates_dict nv.NGLWidget.set_representations nv.NGLWidget.clear nv.NGLWidget.center # add component view.add_component('rcsb://1tsu.pdb') view.add_pdbid('1tsu') # display js_utils.clean_error_output() display(view.player.widget_repr) view.player._display() view._display_image() # show try: nv.show_pdbid('1tsu') except: pass nv.show_url('https://dummy.pdb') # other backends will be tested in other sections # constructor ngl_traj = nv.PyTrajTrajectory(pt.datafiles.load_ala3()) nv.NGLWidget(ngl_traj, parameters=dict(background_color='black')) nv.NGLWidget(ngl_traj, representations=[dict(type='cartoon', params={})]) view.parameters view.camera view.camera = 'perspective' view._request_stage_parameters() view._ngl_repr_dict = REPR_DICT view._handle_repr_dict_changed(dict(new=dict(c0={}))) # dummy class DummWidget(): value = '' view.player.picked_widget = DummWidget() view._update_background_color(change=dict(new='blue')) tab = view.player._display() view.player.widget_repr = view.player._make_widget_repr() view._handle_n_components_changed(change=dict(new=2, old=1)) view._handle_n_components_changed(change=dict(new=1, old=1)) view._handle_n_components_changed(change=dict(new=1, old=0)) view.on_loaded(change=dict(new=True)) view.on_loaded(change=dict(new=False)) view._first_time_loaded = False view view._first_time_loaded = True view view._init_gui = True view view._theme = 'dark' view view.display(gui=True, style='ngl') view.display(gui=False) view.display(gui=True, style='ipywidgets') view._set_sync_camera([view]) view._set_unsync_camera([view]) view._set_selection('.CA') view.color_by('atomindex') representations = [dict(type='cartoon', params=dict())] view.representations = representations repr_parameters = dict(opacity=0.3, params=dict()) view.update_representation(parameters=repr_parameters) view._remove_representation() view.clear() view.add_representation('surface', selection='*', useWorker=True) view.add_representation('surface', selection='*', component=1) view.center() view._on_render_image(change=dict(new='xyz')) view.render_image() view.render_image(frame=2) view.download_image() assert view._dry_run(view._set_sync_camera, [view])['methodName'] == 'setSyncCamera' msg = dict(type='request_frame', data=dict()) view._ngl_handle_msg(view, msg=msg, buffers=[]) msg = dict(type='repr_parameters', data=dict(name='hello')) view._ngl_handle_msg(view, msg=msg, buffers=[]) view.loaded = True msg = dict(type='request_loaded', data=True) view._ngl_handle_msg(view, msg=msg, buffers=[]) view.loaded = False msg = dict(type='request_loaded', data=True) view._ngl_handle_msg(view, msg=msg, buffers=[]) msg = dict(type='all_reprs_info', data=REPR_DICT) view._ngl_handle_msg(view, msg=msg, buffers=[]) msg = dict(type='stage_parameters', data=dict()) view._ngl_handle_msg(view, msg=msg, buffers=[]) # test negative frame (it will be set to self.count - 1) view.frame = -1 msg = dict(type='request_frame', data=dict()) # async_message msg = {'type': 'async_message', 'data': 'ok'} view._ngl_handle_msg(view, msg, []) # render_image r = view.render_image() Widget.widgets[r.model_id] = r msg = {'type': 'image_data', 'ID': r.model_id, 'data': b'YmxhIGJsYQ=='} view._ngl_handle_msg(view, msg, []) view.loaded = True view.show_only([ 0, ]) view._js_console() view._get_full_params() # iter for c in view: assert isinstance(c, nv.widget.ComponentViewer)
def test_API_promise_to_have(): # for Jupyter notebook extension nv._jupyter_nbextension_paths() view = nv.demo() # Structure structure = nv.Structure() structure.get_structure_string assert hasattr(structure, 'id') assert hasattr(structure, 'ext') assert hasattr(structure, 'params') # Widget nv.NGLWidget._set_coordinates nv.NGLWidget.add_component nv.NGLWidget.add_trajectory nv.NGLWidget.coordinates_dict nv.NGLWidget.set_representations nv.NGLWidget.clear nv.NGLWidget.center # add component view.add_component('rcsb://1tsu.pdb') view.add_pdbid('1tsu') # display js_utils.clean_error_output() display.display(view.player.widget_repr) view.player._display() view._display_image() # show try: nv.show_pdbid('1tsu') except: pass nv.show_url('https://dummy.pdb') # other backends will be tested in other sections # constructor ngl_traj = nv.PyTrajTrajectory(pt.datafiles.load_ala3()) nv.NGLWidget(ngl_traj, parameters=dict(background_color='black')) nv.NGLWidget(ngl_traj, representations=[dict(type='cartoon')]) view.parameters view.camera view.camera = 'perspective' view._request_stage_parameters() view._repr_dict = REPR_DICT view._handle_repr_dict_changed(dict(new=dict(c0={}))) # dummy class DummWidget(): value = '' view.player.picked_widget = DummWidget() view._update_background_color(change=dict(new='blue')) view.on_update_dragged_file(change=dict(new=2, old=1)) view.on_update_dragged_file(change=dict(new=1, old=1)) tab = view.player._display() view.player.widget_repr = view.player._make_widget_repr() view._handle_n_components_changed(change=dict(new=2, old=1)) view._handle_n_components_changed(change=dict(new=1, old=1)) view._handle_n_components_changed(change=dict(new=1, old=0)) view.on_loaded(change=dict(new=True)) view.on_loaded(change=dict(new=False)) view._refresh_render() view.sync_view() view._first_time_loaded = False view._ipython_display_() view._first_time_loaded = True view._ipython_display_() view._init_gui = True view._ipython_display_() view._theme = 'dark' view._ipython_display_() view.display(gui=True) view.display(gui=False) view.display(gui=True, use_box=True) view._set_draggable(True) view._set_draggable(False) view._set_sync_frame() view._set_sync_camera() view._set_spin([0, 1, 0], 0.5) view._set_selection('.CA') view.color_by('atomindex') representations = [dict(type='cartoon', params=dict())] view.representations = representations repr_parameters = dict(opacity=0.3, params=dict()) view.update_representation(parameters=repr_parameters) view._remove_representation() view.clear() view.add_representation('surface', selection='*', useWorker=True) view.add_representation('surface', selection='*', component=1) view.center() view._hold_image = True view._on_render_image(change=dict(new=u'xyz')) view._hold_image = False view._on_render_image(change=dict(new=u'xyz')) view.render_image() view.render_image(frame=2) view.download_image() msg = dict(type='request_frame', data=dict()) view._ngl_handle_msg(view, msg=msg, buffers=[]) msg = dict(type='repr_parameters', data=dict(name='hello')) view._ngl_handle_msg(view, msg=msg, buffers=[]) view.loaded = True msg = dict(type='request_loaded', data=True) view._ngl_handle_msg(view, msg=msg, buffers=[]) view.loaded = False msg = dict(type='request_loaded', data=True) view._ngl_handle_msg(view, msg=msg, buffers=[]) msg = dict(type='all_reprs_info', data=REPR_DICT) view._ngl_handle_msg(view, msg=msg, buffers=[]) msg = dict(type='stage_parameters', data=dict()) view._ngl_handle_msg(view, msg=msg, buffers=[]) # test negative frame (it will be set to self.count - 1) view.frame = -1 msg = dict(type='request_frame', data=dict()) view.loaded = True view.show_only([ 0, ]) view._js_console() view._get_full_params() view.detach(split=False) view.detach(split=True) # view._set_place_proxy(HBox()) # iter for c in view: assert isinstance(c, nv.widget.ComponentViewer)