def test_plot_multi_segments(self, mocker, band_segments, kwargs, expected_labels): suptitle = mocker.patch.object(matplotlib.figure.Figure, 'suptitle') fig = plot_1d(band_segments, **kwargs) legend = fig.axes[0].get_legend() if expected_labels is None: assert legend == None else: for text, label in zip(legend.get_texts(), expected_labels): assert text.get_text() == label # Ensure legend only on first subplot for ax in fig.axes[1:]: assert ax.get_legend() == None if 'x_label' in kwargs: assert fig.axes[-1].get_xlabel() == kwargs['x_label'] if 'y_label' in kwargs: assert fig.axes[-1].get_ylabel() == kwargs['y_label'] if 'title' in kwargs: suptitle.assert_called_once_with(kwargs['title']) for ax in fig.axes[:-1]: if 'y_min' in kwargs: assert ax.get_ylim()[0] == pytest.approx(kwargs.get('y_min')) if 'y_max' in kwargs: assert ax.get_ylim()[0] == pytest.approx(kwargs.get('y_max'))
def test_plot_single(self, mocker, spectrum, labels, kwargs): core = self.mock_core(mocker) fig = plot_1d(spectrum, labels=labels, **kwargs) # Check args were as expected assert core.call_args[0][0] == spectrum assert core.call_args[0][1] in fig.axes assert core.call_args[0][2] == labels assert core.call_args[1] == kwargs plt.close(fig)
def main(params: List[str] = None): args = get_args(get_parser(), params) data = load_data_from_file(args.filename) if isinstance(data, euphonic.ForceConstants): frequencies_only = not args.reorder # Need eigenvectors to reorder print("Force Constants data was loaded. Getting band path...") q_distance = _get_q_distance(args.length_unit, args.q_spacing) (modes, x_tick_labels, split_args) = _bands_from_force_constants( data, q_distance=q_distance, frequencies_only=frequencies_only, **_calc_modes_kwargs(args)) elif isinstance(data, euphonic.QpointPhononModes): print("Phonon band data was loaded.") modes = data split_args = {'btol': args.btol} x_tick_labels = None modes.frequencies_unit = args.energy_unit print("Mapping modes to 1D band-structure") if args.reorder: modes.reorder_frequencies() spectrum = modes.get_dispersion() if args.y_label is None: y_label = f"Energy / {spectrum.y_data.units:~P}" else: y_label = args.y_label if args.x_label is None: x_label = "" else: x_label = args.x_label if x_tick_labels: spectrum.x_tick_labels = x_tick_labels spectra = spectrum.split(**split_args) _ = plot_1d(spectra, title=args.title, x_label=x_label, y_label=y_label, y_min=args.e_min, y_max=args.e_max, lw=1.0) matplotlib_save_or_show(save_filename=args.save_to)
def main(): args = get_parser().parse_args() filename = args.file temperature = args.temperature * ureg['K'] summary_name = os.path.basename(filename) path = os.path.dirname(filename) force_constants = ForceConstants.from_phonopy(path=path, summary_name=summary_name) dos_collection = {} for npts in args.npts: if args.neutron: dos = sample_sphere_structure_factor(force_constants, args.q, sampling=args.sampling, temperature=temperature, npts=npts, jitter=args.jitter) else: dos = sample_sphere_dos(force_constants, args.q, sampling=args.sampling, npts=npts, jitter=args.jitter) broad_dos = dos.broaden(1 * ureg('meV'), shape='lorentz') dos_collection.update({npts: broad_dos}) label_list, dos_list = map(list, zip(*dos_collection.items())) fig = plot_1d(dos_list, y_min=0, labels=label_list, title=summary_name) plt.show()
def main(params: List[str] = None): parser = get_parser() args = get_args(parser, params) data = load_data_from_file(args.filename) mode_widths = None if isinstance(data, euphonic.ForceConstants): recip_length_unit = euphonic.ureg(f'1 / {args.length_unit}') grid_spec = _grid_spec_from_args(data.crystal, grid=args.grid, grid_spacing=(args.grid_spacing * recip_length_unit)) print("Force Constants data was loaded. Calculating phonon modes " "on {} q-point grid...".format(' x '.join( [str(x) for x in grid_spec]))) if args.adaptive: if args.shape != 'gauss': raise ValueError('Currently only Gaussian shape is supported ' 'with adaptive broadening') cmkwargs = _calc_modes_kwargs(args) cmkwargs['return_mode_widths'] = True modes, mode_widths = data.calculate_qpoint_frequencies( mp_grid(grid_spec), **cmkwargs) if args.energy_broadening: mode_widths *= args.energy_broadening else: modes = data.calculate_qpoint_frequencies( mp_grid(grid_spec), **_calc_modes_kwargs(args)) elif isinstance(data, euphonic.QpointPhononModes): print("Phonon band data was loaded.") modes = data modes.frequencies_unit = args.energy_unit ebins, energy_unit = _get_energy_bins_and_units(args.energy_unit, modes, args.ebins, emin=args.e_min, emax=args.e_max) dos = modes.calculate_dos(ebins, mode_widths=mode_widths) if args.energy_broadening and not args.adaptive: dos = dos.broaden(args.energy_broadening * energy_unit, shape=args.shape) if args.x_label is None: x_label = f"Energy / {dos.x_data.units:~P}" else: x_label = args.x_label if args.y_label is None: y_label = "" else: y_label = args.y_label fig = plot_1d(dos, title=args.title, x_label=x_label, y_label=y_label, y_min=0, lw=1.0) matplotlib_save_or_show(save_filename=args.save_to)
def test_plot_with_incorrect_labels_raises_valueerror(self, band_segments): with pytest.raises(ValueError): fig = plot_1d(band_segments, labels=['Band A', 'Band B'])
def test_plot_badunits(self, spec1_units, band_segments): spec1, spec2 = band_segments spec1.x_data_unit, spec1.y_data_unit = spec1_units spec2.x_data_unit, spec2.y_data_unit = ('angstrom^-1', 'meV') with pytest.raises(ValueError): plot_1d([spec1, spec2])