<commit_before># -*- coding: utf-8 -*-
# /***************************************************************************
# Irmt
#                                 A QGIS plugin
# OpenQuake Integrated Risk Modelling Toolkit
#                              -------------------
#        begin                : 2013-10-24
#        copyright            : (C) 2014 by GEM Foundation
#        email                : <EMAIL>
# ***************************************************************************/
#
# OpenQuake is free software: you can redistribute it and/or modify it
# under the terms of the GNU Affero General Public License as published
# by the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# OpenQuake is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with OpenQuake.  If not, see <http://www.gnu.org/licenses/>.

import numpy
from qgis.core import QgsFeature, QgsGeometry, QgsPoint
from svir.dialogs.load_output_as_layer_dialog import LoadOutputAsLayerDialog
from svir.calculations.calculate_utils import add_numeric_attribute
from svir.utilities.utils import (LayerEditingManager,
                                  log_msg,
                                  WaitCursorManager,
                                  )
from svir.utilities.shared import DEBUG


class LoadUhsAsLayerDialog(LoadOutputAsLayerDialog):
    """
    Modal dialog to load uniform hazard spectra from an oq-engine output,
    as layer
    """

    def __init__(self, iface, viewer_dock, output_type='uhs',
                 path=None, mode=None):
        assert output_type == 'uhs'
        LoadOutputAsLayerDialog.__init__(
            self, iface, viewer_dock, output_type, path, mode)
        self.setWindowTitle(
            'Load uniform hazard spectra from NPZ, as layer')
        self.create_load_selected_only_ckb()
        self.create_rlz_selector()
        self.create_poe_selector()
        if self.path:
            self.npz_file = numpy.load(self.path, 'r')
            self.populate_out_dep_widgets()
        self.adjustSize()
        self.set_ok_button()

    def set_ok_button(self):
        self.ok_button.setEnabled(
            bool(self.path) and self.poe_cbx.currentIndex() != -1)

    def populate_rlz_cbx(self):
        self.rlzs = [key for key in sorted(self.npz_file['all'].dtype.names)
                     if key not in ('lon', 'lat')]
        self.rlz_cbx.clear()
        self.rlz_cbx.setEnabled(True)
        self.rlz_cbx.addItems(self.rlzs)

    def show_num_sites(self):
        # NOTE: we are assuming all realizations have the same number of sites,
        #       which currently is always true.
        #       If different realizations have a different number of sites, we
        #       need to move this block of code inside on_rlz_changed()
        rlz_data = self.npz_file['all'][self.rlz_cbx.currentText()]
        self.rlz_num_sites_lbl.setText(self.num_sites_msg % rlz_data.shape)

    def on_rlz_changed(self):
        self.dataset = self.npz_file['all'][self.rlz_cbx.currentText()]
        self.poes = self.dataset.dtype.names
        self.poe_cbx.clear()
        self.poe_cbx.setEnabled(True)
        self.poe_cbx.addItems(self.poes)
        self.set_ok_button()

    def build_layer_name(self, rlz, **kwargs):
        poe = kwargs['poe']
        layer_name = "uhs_%s_poe-%s" % (rlz, poe)
        return layer_name

    def get_field_names(self, **kwargs):
        poe = kwargs['poe']
        field_names = self.dataset[poe].dtype.names
        return field_names

    def add_field_to_layer(self, field_name):
        # NOTE: add_numeric_attribute uses LayerEditingManager
        added_field_name = add_numeric_attribute(
            field_name, self.layer)
        return added_field_name

    def read_npz_into_layer(self, field_names, **kwargs):
        poe = kwargs['poe']
        with LayerEditingManager(self.layer, 'Reading npz', DEBUG):
            feats = []
            for row_id, row in enumerate(self.dataset):
                # add a feature
                feat = QgsFeature(self.layer.pendingFields())
                for field_name_idx, field_name in enumerate(field_names):
                    if field_name in ['lon', 'lat']:
                        continue
                    value = float(row[poe][field_name_idx])
                    feat.setAttribute(field_name, value)
                lon = self.npz_file['all']['lon'][row_id]
                lat = self.npz_file['all']['lat'][row_id]
                feat.setGeometry(QgsGeometry.fromPoint(QgsPoint(lon, lat)))
                feats.append(feat)
            added_ok = self.layer.addFeatures(feats, makeSelected=False)
            if not added_ok:<commit_msg>Read lons and lats only once also in uhs<commit_after># -*- coding: utf-8 -*-
# /***************************************************************************
# Irmt
#                                 A QGIS plugin
# OpenQuake Integrated Risk Modelling Toolkit
#                              -------------------
#        begin                : 2013-10-24
#        copyright            : (C) 2014 by GEM Foundation
#        email                : <EMAIL>
# ***************************************************************************/
#
# OpenQuake is free software: you can redistribute it and/or modify it
# under the terms of the GNU Affero General Public License as published
# by the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# OpenQuake is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with OpenQuake.  If not, see <http://www.gnu.org/licenses/>.

import numpy
from qgis.core import QgsFeature, QgsGeometry, QgsPoint
from svir.dialogs.load_output_as_layer_dialog import LoadOutputAsLayerDialog
from svir.calculations.calculate_utils import add_numeric_attribute
from svir.utilities.utils import (LayerEditingManager,
                                  log_msg,
                                  WaitCursorManager,
                                  )
from svir.utilities.shared import DEBUG


class LoadUhsAsLayerDialog(LoadOutputAsLayerDialog):
    """
    Modal dialog to load uniform hazard spectra from an oq-engine output,
    as layer
    """

    def __init__(self, iface, viewer_dock, output_type='uhs',
                 path=None, mode=None):
        assert output_type == 'uhs'
        LoadOutputAsLayerDialog.__init__(
            self, iface, viewer_dock, output_type, path, mode)
        self.setWindowTitle(
            'Load uniform hazard spectra from NPZ, as layer')
        self.create_load_selected_only_ckb()
        self.create_rlz_selector()
        self.create_poe_selector()
        if self.path:
            self.npz_file = numpy.load(self.path, 'r')
            self.populate_out_dep_widgets()
        self.adjustSize()
        self.set_ok_button()

    def set_ok_button(self):
        self.ok_button.setEnabled(
            bool(self.path) and self.poe_cbx.currentIndex() != -1)

    def populate_rlz_cbx(self):
        self.rlzs = [key for key in sorted(self.npz_file['all'].dtype.names)
                     if key not in ('lon', 'lat')]
        self.rlz_cbx.clear()
        self.rlz_cbx.setEnabled(True)
        self.rlz_cbx.addItems(self.rlzs)

    def show_num_sites(self):
        # NOTE: we are assuming all realizations have the same number of sites,
        #       which currently is always true.
        #       If different realizations have a different number of sites, we
        #       need to move this block of code inside on_rlz_changed()
        rlz_data = self.npz_file['all'][self.rlz_cbx.currentText()]
        self.rlz_num_sites_lbl.setText(self.num_sites_msg % rlz_data.shape)

    def on_rlz_changed(self):
        self.dataset = self.npz_file['all'][self.rlz_cbx.currentText()]
        self.poes = self.dataset.dtype.names
        self.poe_cbx.clear()
        self.poe_cbx.setEnabled(True)
        self.poe_cbx.addItems(self.poes)
        self.set_ok_button()

    def build_layer_name(self, rlz, **kwargs):
        poe = kwargs['poe']
        layer_name = "uhs_%s_poe-%s" % (rlz, poe)
        return layer_name

    def get_field_names(self, **kwargs):
        poe = kwargs['poe']
        field_names = self.dataset[poe].dtype.names
        return field_names

    def add_field_to_layer(self, field_name):
        # NOTE: add_numeric_attribute uses LayerEditingManager
        added_field_name = add_numeric_attribute(
            field_name, self.layer)
        return added_field_name

    def read_npz_into_layer(self, field_names, **kwargs):
        poe = kwargs['poe']
        with LayerEditingManager(self.layer, 'Reading npz', DEBUG):
            lons = self.npz_file['all']['lon']
            lats = self.npz_file['all']['lat']
            feats = []
            for row_idx, row in enumerate(self.dataset):
                # add a feature
                feat = QgsFeature(self.layer.pendingFields())
                for field_name_idx, field_name in enumerate(field_names):
                    value = float(row[poe][field_name_idx])
                    feat.setAttribute(field_name, value)
                feat.setGeometry(QgsGeometry.fromPoint(
                    QgsPoint(lons[row_idx], lats[row_idx])))
                feats.append(feat)
            added_ok = self.layer.addFeatures(feats, makeSelected=False)
            if not added_ok:
{'fraction_non_alphanumeric': 0.12014134275618374, 'fraction_numerical': 0.003028773346794548, 'mean_word_length': 2.339851652056642, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 6, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2221437', 'n_tokens_mistral': 2933, 'n_tokens_neox': 2827, 'n_words': 718}