<commit_before>#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# Author:  <NAME> <<EMAIL>>

import abc
import h5py
import numbers
import numpy as np

from fsc.export import export

from ..io._serialize_mapping import subscribe_serialize
from ._base import KpointsBase
from ..io import _hdf5_utils

@export
@subscribe_serialize('kpoints_path')
class KpointsPath(KpointsBase):
    """
    Defines a k-point path, with labelled vertices.

    :param vertices: Defines the special k-point vertices used in the path.
    :type vertices: dict

    :param paths: List of k-point paths. Each k-point path is a list of vertices. Only vertices within the same path are connected.
    :type paths: list

    :param points_per_line: Number of k-points in each line connecting two k-points. This can be either an integer (in which case all lines contain the same number of k-points, or a nested list of integers (corresponding to each line).
    :type points_per_line: int, list
    """
    def __init__(self, *, vertices, paths, points_per_line=100):
        self.vertices = {str(label): np.array(kpt) for label, kpt in vertices.items()}
        self.paths = [[str(p) for p in single_path] for single_path in paths]
        all_vertices = set()
        for path in self.paths:
            all_vertices.update(path)
        unknown_vertices = all_vertices - self.vertices.keys()
        if unknown_vertices:
            raise ValueError("Unrecognized {} {} in 'path'. Vertices must be defined in the 'vertices' mapping.".format('vertex' if len(unknown_vertices) == 1 else 'vertices', ', '.join(str(x) for x in unknown_vertices)))
        if isinstance(points_per_line, numbers.Integral):
            self.points_per_line = [
                [points_per_line] * (len(single_path) - 1)
                for single_path in self.paths
            ]
        else:
            self.points_per_line = list(points_per_line)
        for single_path, path_num_points in zip(self.paths, self.points_per_line):
            if len(single_path) < 2:
                raise ValueError('Each individual k-point path must have at least 2 vertices.')
            if len(single_path) != len(path_num_points) + 1:
                raise ValueError("Invalid number of entries in 'points_per_line'.")
            if any(num_points < 2 for num_points in path_num_points):
                raise ValueError("The number of k-points in each line must be at least 2.")

    @property
    def kpoints_explicit(self):
        kpoints = []
        for single_path, path_num_points in zip(self.paths, self.points_per_line):
            kpoints.extend(self._path_kpoints(single_path, path_num_points))
        return np.array(kpoints)

    def _path_kpoints(self, path, num_kpoints):
        kpoints = [self.vertices[path[0]]]
        for start, end, n_points in zip(path, path[1:], num_kpoints):
            steps = np.linspace(0, 1, n_points)[1:]
            k_start = self.vertices[start]
            k_end = self.vertices[end]
            kpoints.extend([(1 - s) * k_start + s * k_end for s in steps])
        return kpoints

    def __iter__(self):
        return iter(self.kpoints_explicit)

    def __array__(self):
        return self.kpoints_explicit

    def to_hdf5(self, hdf5_handle):<commit_msg>Remove duplicate functions from KpointsBase in KpointsPath<commit_after>#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# Author:  <NAME> <<EMAIL>>

import abc
import h5py
import numbers
import numpy as np

from fsc.export import export

from ..io._serialize_mapping import subscribe_serialize
from ._base import KpointsBase
from ..io import _hdf5_utils

@export
@subscribe_serialize('kpoints_path')
class KpointsPath(KpointsBase):
    """
    Defines a k-point path, with labelled vertices.

    :param vertices: Defines the special k-point vertices used in the path.
    :type vertices: dict

    :param paths: List of k-point paths. Each k-point path is a list of vertices. Only vertices within the same path are connected.
    :type paths: list

    :param points_per_line: Number of k-points in each line connecting two k-points. This can be either an integer (in which case all lines contain the same number of k-points, or a nested list of integers (corresponding to each line).
    :type points_per_line: int, list
    """
    def __init__(self, *, vertices, paths, points_per_line=100):
        self.vertices = {str(label): np.array(kpt) for label, kpt in vertices.items()}
        self.paths = [[str(p) for p in single_path] for single_path in paths]
        all_vertices = set()
        for path in self.paths:
            all_vertices.update(path)
        unknown_vertices = all_vertices - self.vertices.keys()
        if unknown_vertices:
            raise ValueError("Unrecognized {} {} in 'path'. Vertices must be defined in the 'vertices' mapping.".format('vertex' if len(unknown_vertices) == 1 else 'vertices', ', '.join(str(x) for x in unknown_vertices)))
        if isinstance(points_per_line, numbers.Integral):
            self.points_per_line = [
                [points_per_line] * (len(single_path) - 1)
                for single_path in self.paths
            ]
        else:
            self.points_per_line = list(points_per_line)
        for single_path, path_num_points in zip(self.paths, self.points_per_line):
            if len(single_path) < 2:
                raise ValueError('Each individual k-point path must have at least 2 vertices.')
            if len(single_path) != len(path_num_points) + 1:
                raise ValueError("Invalid number of entries in 'points_per_line'.")
            if any(num_points < 2 for num_points in path_num_points):
                raise ValueError("The number of k-points in each line must be at least 2.")

    @property
    def kpoints_explicit(self):
        kpoints = []
        for single_path, path_num_points in zip(self.paths, self.points_per_line):
            kpoints.extend(self._path_kpoints(single_path, path_num_points))
        return np.array(kpoints)

    def _path_kpoints(self, path, num_kpoints):
        kpoints = [self.vertices[path[0]]]
        for start, end, n_points in zip(path, path[1:], num_kpoints):
            steps = np.linspace(0, 1, n_points)[1:]
            k_start = self.vertices[start]
            k_end = self.vertices[end]
            kpoints.extend([(1 - s) * k_start + s * k_end for s in steps])
        return kpoints

    def to_hdf5(self, hdf5_handle):
{'fraction_non_alphanumeric': 0.0960820895522388, 'fraction_numerical': 0.0065298507462686565, 'mean_word_length': 2.817804154302671, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5269041', 'n_tokens_mistral': 1964, 'n_tokens_neox': 1917, 'n_words': 607}