<commit_before>#!/usr/bin/env python
# -*- coding: utf-8 -*-
from __future__ import absolute_import, print_function

__author__ = "<NAME>"

import numpy as np
from ph_unfolder.analysis.smearing import Smearing
from ph_unfolder.file_io import read_band_hdf5


class DensityExtractor(object):
    def __init__(self,
                 filename=None,
                 outfile=None,
                 function="gaussian",
                 fmin=0.0,
                 fmax=10.0,
                 fpitch=0.05,
                 sigma=1.0,
                 weight_label="pr_weights"):

        print("# sigma:", sigma)

        self._smearing = Smearing(
            function_name=function,
            sigma=sigma,
            xmin=fmin,
            xmax=fmax,
            xpitch=fpitch,
        )

        self._weight_label = weight_label

        self._outfile = outfile

        if filename is not None:
            self.load_data(filename)

    def load_data(self, filename):
        print("# Reading band.hdf5: ", end="")
        self._band_data = read_band_hdf5(filename)
        print("Finished")
        return self

    def run(self):
        weight_label = self._weight_label
        print("# weight: {}".format(weight_label))

        distances   = self._band_data["distances"]
        frequencies = self._band_data["frequencies"]
        weights     = self._band_data[weight_label]
        nqstars     = self._band_data["nqstars"]
        num_irs_list = self._band_data["num_irs"]
        if "eigenvectors_data" in self._band_data:
            eigenvectors_data = self._band_data["eigenvectors_data"]
            print_density = self.print_partial_density
        elif weight_label == "rot_pr_weights":
            print_density = self.print_partial_density
        else:
            eigenvectors_data = None
            print_density = self.print_total_density

        filename = self._outfile
        with open(filename, "w") as f:
            npath, nqpoint = frequencies.shape[:2]
            for ipath in range(npath):
                for i, d in enumerate(distances[ipath]):
                    if weight_label == "rot_pr_weights":
                        num_irs = num_irs_list[ipath, i]
                        weights_data = weights[ipath, i, :, :num_irs]
                    else:
                        weights_data = weights[ipath, i]

                    self.calculate_density(
                        d,
                        nqstar=nqstars[ipath, i],<commit_msg>Fix to read num_irs in hdf5 file only when it exists<commit_after>#!/usr/bin/env python
# -*- coding: utf-8 -*-
from __future__ import absolute_import, print_function

__author__ = "<NAME>"

import numpy as np
from ph_unfolder.analysis.smearing import Smearing
from ph_unfolder.file_io import read_band_hdf5


class DensityExtractor(object):
    def __init__(self,
                 filename=None,
                 outfile=None,
                 function="gaussian",
                 fmin=0.0,
                 fmax=10.0,
                 fpitch=0.05,
                 sigma=1.0,
                 weight_label="pr_weights"):

        print("# sigma:", sigma)

        self._smearing = Smearing(
            function_name=function,
            sigma=sigma,
            xmin=fmin,
            xmax=fmax,
            xpitch=fpitch,
        )

        self._weight_label = weight_label

        self._outfile = outfile

        if filename is not None:
            self.load_data(filename)

    def load_data(self, filename):
        print("# Reading band.hdf5: ", end="")
        self._band_data = read_band_hdf5(filename)
        print("Finished")
        return self

    def run(self):
        weight_label = self._weight_label
        print("# weight: {}".format(weight_label))

        distances   = self._band_data["distances"]
        frequencies = self._band_data["frequencies"]
        weights     = self._band_data[weight_label]
        nqstars     = self._band_data["nqstars"]
        if "num_irs" in self._band_data:
            num_irs_list = self._band_data["num_irs"]
        if "eigenvectors_data" in self._band_data:
            eigenvectors_data = self._band_data["eigenvectors_data"]
            print_density = self.print_partial_density
        elif weight_label == "rot_pr_weights":
            print_density = self.print_partial_density
        else:
            eigenvectors_data = None
            print_density = self.print_total_density

        filename = self._outfile
        with open(filename, "w") as f:
            npath, nqpoint = frequencies.shape[:2]
            for ipath in range(npath):
                for i, d in enumerate(distances[ipath]):
                    if weight_label == "rot_pr_weights":
                        num_irs = num_irs_list[ipath, i]
                        weights_data = weights[ipath, i, :, :num_irs]
                    else:
                        weights_data = weights[ipath, i]

                    self.calculate_density(
                        d,
                        nqstar=nqstars[ipath, i],
{'fraction_non_alphanumeric': 0.09105304829770387, 'fraction_numerical': 0.00613618368962787, 'mean_word_length': 1.7551799345692476, 'pattern_counts': {'":': 4, '<': 5, '<?xml version=': 0, '>': 5, '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': '1444491', 'n_tokens_mistral': 1480, 'n_tokens_neox': 1419, 'n_words': 291}