content
stringlengths
0
1.05M
origin
stringclasses
2 values
type
stringclasses
2 values
from functools import partial from copy import copy import numpy as np from PyQt5.QtWidgets import ( QGridLayout, QHBoxLayout, QVBoxLayout, QGroupBox, QCheckBox, QComboBox, QScrollArea, QLabel, QSlider, ) from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg from matplotlib import pyplot as plt try: import biorbd except ImportError: import biorbd_casadi as biorbd class MuscleAnalyses: def __init__(self, parent, main_window, background_color=(0.5, 0.5, 0.5)): # Centralize the materials analyses_muscle_layout = QHBoxLayout(parent) # Get some aliases self.main_window = main_window self.model = self.main_window.model self.n_mus = self.model.nbMuscles() self.n_q = self.model.nbQ() # Add dof selector selector_layout = QVBoxLayout() analyses_muscle_layout.addLayout(selector_layout) text_dof = QLabel() text_dof.setText("DoF to run") text_dof.setPalette(self.main_window.palette_active) selector_layout.addWidget(text_dof) self.combobox_dof = QComboBox() selector_layout.addWidget(self.combobox_dof) self.combobox_dof.setPalette(self.main_window.palette_active) self.dof_mapping = dict() for cmp_dof, name in enumerate(self.model.nameDof()): self.combobox_dof.addItem(name.to_string()) self.dof_mapping[name.to_string()] = cmp_dof self.combobox_dof.currentIndexChanged.connect(self.__set_current_dof) # Set default value self.current_dof = self.combobox_dof.currentText() # Add the possibility to select from movement self.animation_checkbox = QCheckBox() selector_layout.addWidget(self.animation_checkbox) self.animation_checkbox.setText("From animation") self.animation_checkbox.setPalette(self.main_window.palette_inactive) self.animation_checkbox.setEnabled(False) self.animation_checkbox.stateChanged.connect(partial(self.update_all_graphs, False, False, False, False)) # Add plots analyses_layout = QGridLayout() analyses_muscle_layout.addLayout(analyses_layout) self.n_point_for_q = 50 # Add muscle length plot self.canvas_muscle_length = FigureCanvasQTAgg(plt.figure(facecolor=background_color)) analyses_layout.addWidget(self.canvas_muscle_length, 0, 0) self.ax_muscle_length = self.canvas_muscle_length.figure.subplots() self.ax_muscle_length.set_facecolor(background_color) self.ax_muscle_length.set_title("Muscle length") self.ax_muscle_length.set_ylabel("Length (m)") # Add moment arm plot self.canvas_moment_arm = FigureCanvasQTAgg(plt.figure(facecolor=background_color)) analyses_layout.addWidget(self.canvas_moment_arm, 0, 1) self.ax_moment_arm = self.canvas_moment_arm.figure.subplots() self.ax_moment_arm.set_facecolor(background_color) self.ax_moment_arm.set_title("Moment arm") self.ax_moment_arm.set_ylabel("Moment arm (m)") # Add passive forces self.canvas_passive_forces = FigureCanvasQTAgg(plt.figure(facecolor=background_color)) analyses_layout.addWidget(self.canvas_passive_forces, 1, 0) self.ax_passive_forces = self.canvas_passive_forces.figure.subplots() self.ax_passive_forces.set_facecolor(background_color) self.ax_passive_forces.set_title("Passive forces") self.ax_passive_forces.set_ylabel("Passive forces coeff") # Add active forces self.canvas_active_forces = FigureCanvasQTAgg(plt.figure(facecolor=background_color)) active_forces_layout = QHBoxLayout() analyses_layout.addLayout(active_forces_layout, 1, 1) active_forces_layout.addWidget(self.canvas_active_forces) self.ax_active_forces = self.canvas_active_forces.figure.subplots() self.ax_active_forces.set_facecolor(background_color) self.ax_active_forces.set_title("Active forces") self.ax_active_forces.set_ylabel("Active forces coeff") self.active_forces_slider = QSlider() active_forces_layout.addWidget(self.active_forces_slider) self.active_forces_slider.setPalette(self.main_window.palette_active) self.active_forces_slider.setMinimum(0) self.active_forces_slider.setMaximum(100) self.active_forces_slider.valueChanged.connect(partial(self.update_all_graphs, True, True, True, False)) # Add muscle selector radio_muscle_group = QGroupBox() muscle_layout = QVBoxLayout() self.muscle_mapping = dict() self.checkboxes_muscle = list() cmp_mus = 0 for group in range(self.model.nbMuscleGroups()): for mus in range(self.model.muscleGroup(group).nbMuscles()): # Map the name to the right numbers name = self.model.muscleGroup(group).muscle(mus).name().to_string() self.muscle_mapping[name] = (group, mus, cmp_mus) # Add the CheckBox self.checkboxes_muscle.append(QCheckBox()) self.checkboxes_muscle[cmp_mus].setPalette(self.main_window.palette_active) self.checkboxes_muscle[cmp_mus].setText(name) self.checkboxes_muscle[cmp_mus].toggled.connect( partial(self.update_all_graphs, False, False, False, False) ) muscle_layout.addWidget(self.checkboxes_muscle[cmp_mus]) # Add the plot to the axes self.ax_muscle_length.plot(np.nan, np.nan, "w") self.ax_moment_arm.plot(np.nan, np.nan, "w") self.ax_passive_forces.plot(np.nan, np.nan, "w") self.ax_active_forces.plot(np.nan, np.nan, "w") cmp_mus += 1 # Add vertical bar for position of current dof self.ax_muscle_length.plot(np.nan, np.nan, "k") self.ax_moment_arm.plot(np.nan, np.nan, "k") self.ax_passive_forces.plot(np.nan, np.nan, "k") self.ax_active_forces.plot(np.nan, np.nan, "k") radio_muscle_group.setLayout(muscle_layout) muscles_scroll = QScrollArea() muscles_scroll.setFrameShape(0) muscles_scroll.setWidgetResizable(True) muscles_scroll.setWidget(radio_muscle_group) selector_layout.addWidget(muscles_scroll) selector_layout.addStretch() def add_movement_to_dof_choice(self): self.animation_checkbox.setPalette(self.main_window.palette_active) self.animation_checkbox.setEnabled(True) self.n_point_for_q = self.main_window.animated_Q.shape[0] def __set_current_dof(self): self.current_dof = self.combobox_dof.currentText() self.update_all_graphs(False, False, False, False) def update_all_graphs(self, skip_muscle_length, skip_moment_arm, skip_passive_forces, skip_active_forces): x_axis, length, moment_arm, passive_forces, active_forces = self.__compute_all_values() self.__update_specific_plot( self.canvas_muscle_length, self.ax_muscle_length, x_axis, length, skip_muscle_length ) self.__update_specific_plot(self.canvas_moment_arm, self.ax_moment_arm, x_axis, moment_arm, skip_moment_arm) self.__update_specific_plot( self.canvas_passive_forces, self.ax_passive_forces, x_axis, passive_forces, skip_passive_forces ) self.__update_specific_plot( self.canvas_active_forces, self.ax_active_forces, x_axis, active_forces, skip_active_forces ) self.__update_graph_size() def __update_graph_size(self): self.ax_muscle_length.figure.tight_layout() self.ax_moment_arm.figure.tight_layout() self.ax_passive_forces.figure.tight_layout() self.ax_active_forces.figure.tight_layout() self.canvas_muscle_length.draw() self.canvas_moment_arm.draw() self.canvas_passive_forces.draw() self.canvas_active_forces.draw() def __compute_all_values(self): q_idx = self.dof_mapping[self.current_dof] x_axis, all_q = self.__generate_x_axis(q_idx) length = np.ndarray((self.n_point_for_q, self.n_mus)) moment_arm = np.ndarray((self.n_point_for_q, self.n_mus)) passive_forces = np.ndarray((self.n_point_for_q, self.n_mus)) active_forces = np.ndarray((self.n_point_for_q, self.n_mus)) emg = biorbd.State(0, self.active_forces_slider.value() / 100) for i, q_mod in enumerate(all_q): self.model.UpdateKinematicsCustom(biorbd.GeneralizedCoordinates(q_mod)) for m in range(self.n_mus): if self.checkboxes_muscle[m].isChecked(): mus_group_idx, mus_idx, cmp_mus = self.muscle_mapping[self.checkboxes_muscle[m].text()] mus = self.model.muscleGroup(mus_group_idx).muscle(mus_idx) mus.updateOrientations(self.model, q_mod, 1) muscles_length_jacobian = self.model.musclesLengthJacobian().to_array() length[i, m] = mus.length(self.model, q_mod, False) moment_arm[i, m] = -1 * muscles_length_jacobian[cmp_mus, q_idx] if mus.type() != biorbd.IDEALIZED_ACTUATOR: passive_forces[i, m] = biorbd.HillType(mus).FlPE() else: passive_forces[i, m] = 0 if mus.type() != biorbd.IDEALIZED_ACTUATOR: active_forces[i, m] = biorbd.HillType(mus).FlCE(emg) else: active_forces[i, m] = 0 return x_axis, length, moment_arm, passive_forces, active_forces def __update_specific_plot(self, canvas, ax, x, y, skip=False): # Plot all active muscles number_of_active = 0 for m in range(self.n_mus): if self.checkboxes_muscle[m].isChecked(): if not skip: ax.get_lines()[m].set_data(x, y[:, m]) number_of_active += 1 else: ax.get_lines()[m].set_data(np.nan, np.nan) # Empty the vertical bar (otherwise relim takes it in account ax.get_lines()[-1].set_data(np.nan, np.nan) # If there is no data skip relim and vertical bar adjustment if number_of_active != 0: # relim so the plot looks nice ax.relim() ax.autoscale(enable=True) # Adjust axis label (give a generic name) if self.animation_checkbox.isChecked(): ax.set_xlabel("Time frame") else: ax.set_xlabel("Along range") # Add vertical bar to show current dof (it must be done after relim so we know the new lims) q_idx = self.combobox_dof.currentIndex() if self.animation_checkbox.isChecked(): x = int(self.main_window.movement_slider[1].text()) - 1 # Frame label else: x = self.__get_q_from_slider()[q_idx] ax.get_lines()[-1].set_data([x, x], ax.get_ylim()) # Redraw graphs canvas.draw() def __get_q_from_slider(self): return copy(self.main_window.Q) def __generate_x_axis(self, q_idx): if self.animation_checkbox.isChecked(): q = self.main_window.animated_Q x = np.arange(q.shape[0]) else: q = np.tile(self.__get_q_from_slider(), (self.n_point_for_q, 1)) slider = self.main_window.sliders[self.combobox_dof.currentIndex()][1] q[:, q_idx] = np.linspace( slider.minimum() / self.main_window.double_factor, slider.maximum() / self.main_window.double_factor, self.n_point_for_q, ) x = q[:, q_idx] return x, q
nilq/baby-python
python
# # PySNMP MIB module RFC1407-MIB (http://snmplabs.com/pysmi) # ASN.1 source file:///Users/davwang4/Dev/mibs.snmplabs.com/asn1/RFC1407-MIB # Produced by pysmi-0.3.4 at Mon Apr 29 20:48:41 2019 # On host DAVWANG4-M-1475 platform Darwin version 18.5.0 by user davwang4 # Using Python version 3.7.3 (default, Mar 27 2019, 09:23:15) # Integer, ObjectIdentifier, OctetString = mibBuilder.importSymbols("ASN1", "Integer", "ObjectIdentifier", "OctetString") NamedValues, = mibBuilder.importSymbols("ASN1-ENUMERATION", "NamedValues") ConstraintsUnion, ValueRangeConstraint, ValueSizeConstraint, SingleValueConstraint, ConstraintsIntersection = mibBuilder.importSymbols("ASN1-REFINEMENT", "ConstraintsUnion", "ValueRangeConstraint", "ValueSizeConstraint", "SingleValueConstraint", "ConstraintsIntersection") InterfaceIndex, = mibBuilder.importSymbols("IF-MIB", "InterfaceIndex") PerfTotalCount, PerfIntervalCount, PerfCurrentCount = mibBuilder.importSymbols("PerfHist-TC-MIB", "PerfTotalCount", "PerfIntervalCount", "PerfCurrentCount") ModuleCompliance, ObjectGroup, NotificationGroup = mibBuilder.importSymbols("SNMPv2-CONF", "ModuleCompliance", "ObjectGroup", "NotificationGroup") Integer32, MibIdentifier, transmission, NotificationType, Gauge32, iso, Bits, MibScalar, MibTable, MibTableRow, MibTableColumn, TimeTicks, ObjectIdentity, IpAddress, Counter64, ModuleIdentity, Unsigned32, Counter32 = mibBuilder.importSymbols("SNMPv2-SMI", "Integer32", "MibIdentifier", "transmission", "NotificationType", "Gauge32", "iso", "Bits", "MibScalar", "MibTable", "MibTableRow", "MibTableColumn", "TimeTicks", "ObjectIdentity", "IpAddress", "Counter64", "ModuleIdentity", "Unsigned32", "Counter32") TimeStamp, DisplayString, TextualConvention, TruthValue = mibBuilder.importSymbols("SNMPv2-TC", "TimeStamp", "DisplayString", "TextualConvention", "TruthValue") ds3 = ModuleIdentity((1, 3, 6, 1, 2, 1, 10, 30)) ds3.setRevisions(('2004-09-08 00:00', '1998-08-01 21:30', '1993-01-25 20:28',)) if mibBuilder.loadTexts: ds3.setLastUpdated('200409080000Z') if mibBuilder.loadTexts: ds3.setOrganization('IETF AToM MIB Working Group') dsx3ConfigTable = MibTable((1, 3, 6, 1, 2, 1, 10, 30, 5), ) if mibBuilder.loadTexts: dsx3ConfigTable.setStatus('current') dsx3ConfigEntry = MibTableRow((1, 3, 6, 1, 2, 1, 10, 30, 5, 1), ).setIndexNames((0, "RFC1407-MIB", "dsx3LineIndex")) if mibBuilder.loadTexts: dsx3ConfigEntry.setStatus('current') dsx3LineIndex = MibTableColumn((1, 3, 6, 1, 2, 1, 10, 30, 5, 1, 1), InterfaceIndex()).setMaxAccess("readonly") if mibBuilder.loadTexts: dsx3LineIndex.setStatus('current') dsx3IfIndex = MibTableColumn((1, 3, 6, 1, 2, 1, 10, 30, 5, 1, 2), InterfaceIndex()).setMaxAccess("readonly") if mibBuilder.loadTexts: dsx3IfIndex.setStatus('deprecated') dsx3TimeElapsed = MibTableColumn((1, 3, 6, 1, 2, 1, 10, 30, 5, 1, 3), Integer32().subtype(subtypeSpec=ValueRangeConstraint(0, 899))).setMaxAccess("readonly") if mibBuilder.loadTexts: dsx3TimeElapsed.setStatus('current') dsx3ValidIntervals = MibTableColumn((1, 3, 6, 1, 2, 1, 10, 30, 5, 1, 4), Integer32().subtype(subtypeSpec=ValueRangeConstraint(0, 96))).setMaxAccess("readonly") if mibBuilder.loadTexts: dsx3ValidIntervals.setStatus('current') dsx3LineType = MibTableColumn((1, 3, 6, 1, 2, 1, 10, 30, 5, 1, 5), Integer32().subtype(subtypeSpec=ConstraintsUnion(SingleValueConstraint(1, 2, 3, 4, 5, 6, 7, 8, 9))).clone(namedValues=NamedValues(("dsx3other", 1), ("dsx3M23", 2), ("dsx3SYNTRAN", 3), ("dsx3CbitParity", 4), ("dsx3ClearChannel", 5), ("e3other", 6), ("e3Framed", 7), ("e3Plcp", 8), ("dsx3M13", 9)))).setMaxAccess("readwrite") if mibBuilder.loadTexts: dsx3LineType.setStatus('current') dsx3LineCoding = MibTableColumn((1, 3, 6, 1, 2, 1, 10, 30, 5, 1, 6), Integer32().subtype(subtypeSpec=ConstraintsUnion(SingleValueConstraint(1, 2, 3))).clone(namedValues=NamedValues(("dsx3Other", 1), ("dsx3B3ZS", 2), ("e3HDB3", 3)))).setMaxAccess("readwrite") if mibBuilder.loadTexts: dsx3LineCoding.setStatus('current') dsx3SendCode = MibTableColumn((1, 3, 6, 1, 2, 1, 10, 30, 5, 1, 7), Integer32().subtype(subtypeSpec=ConstraintsUnion(SingleValueConstraint(1, 2, 3, 4, 5, 6))).clone(namedValues=NamedValues(("dsx3SendNoCode", 1), ("dsx3SendLineCode", 2), ("dsx3SendPayloadCode", 3), ("dsx3SendResetCode", 4), ("dsx3SendDS1LoopCode", 5), ("dsx3SendTestPattern", 6)))).setMaxAccess("readwrite") if mibBuilder.loadTexts: dsx3SendCode.setStatus('current') dsx3CircuitIdentifier = MibTableColumn((1, 3, 6, 1, 2, 1, 10, 30, 5, 1, 8), DisplayString().subtype(subtypeSpec=ValueSizeConstraint(0, 255))).setMaxAccess("readwrite") if mibBuilder.loadTexts: dsx3CircuitIdentifier.setStatus('current') dsx3LoopbackConfig = MibTableColumn((1, 3, 6, 1, 2, 1, 10, 30, 5, 1, 9), Integer32().subtype(subtypeSpec=ConstraintsUnion(SingleValueConstraint(1, 2, 3, 4, 5, 6))).clone(namedValues=NamedValues(("dsx3NoLoop", 1), ("dsx3PayloadLoop", 2), ("dsx3LineLoop", 3), ("dsx3OtherLoop", 4), ("dsx3InwardLoop", 5), ("dsx3DualLoop", 6)))).setMaxAccess("readwrite") if mibBuilder.loadTexts: dsx3LoopbackConfig.setStatus('current') dsx3LineStatus = MibTableColumn((1, 3, 6, 1, 2, 1, 10, 30, 5, 1, 10), Integer32().subtype(subtypeSpec=ValueRangeConstraint(1, 4095))).setMaxAccess("readonly") if mibBuilder.loadTexts: dsx3LineStatus.setStatus('current') dsx3TransmitClockSource = MibTableColumn((1, 3, 6, 1, 2, 1, 10, 30, 5, 1, 11), Integer32().subtype(subtypeSpec=ConstraintsUnion(SingleValueConstraint(1, 2, 3))).clone(namedValues=NamedValues(("loopTiming", 1), ("localTiming", 2), ("throughTiming", 3)))).setMaxAccess("readwrite") if mibBuilder.loadTexts: dsx3TransmitClockSource.setStatus('current') dsx3InvalidIntervals = MibTableColumn((1, 3, 6, 1, 2, 1, 10, 30, 5, 1, 12), Integer32().subtype(subtypeSpec=ValueRangeConstraint(0, 96))).setMaxAccess("readonly") if mibBuilder.loadTexts: dsx3InvalidIntervals.setStatus('current') dsx3LineLength = MibTableColumn((1, 3, 6, 1, 2, 1, 10, 30, 5, 1, 13), Integer32().subtype(subtypeSpec=ValueRangeConstraint(0, 64000))).setUnits('meters').setMaxAccess("readwrite") if mibBuilder.loadTexts: dsx3LineLength.setStatus('current') dsx3LineStatusLastChange = MibTableColumn((1, 3, 6, 1, 2, 1, 10, 30, 5, 1, 14), TimeStamp()).setMaxAccess("readonly") if mibBuilder.loadTexts: dsx3LineStatusLastChange.setStatus('current') dsx3LineStatusChangeTrapEnable = MibTableColumn((1, 3, 6, 1, 2, 1, 10, 30, 5, 1, 15), Integer32().subtype(subtypeSpec=ConstraintsUnion(SingleValueConstraint(1, 2))).clone(namedValues=NamedValues(("enabled", 1), ("disabled", 2))).clone('disabled')).setMaxAccess("readwrite") if mibBuilder.loadTexts: dsx3LineStatusChangeTrapEnable.setStatus('current') dsx3LoopbackStatus = MibTableColumn((1, 3, 6, 1, 2, 1, 10, 30, 5, 1, 16), Integer32().subtype(subtypeSpec=ValueRangeConstraint(1, 127))).setMaxAccess("readonly") if mibBuilder.loadTexts: dsx3LoopbackStatus.setStatus('current') dsx3Channelization = MibTableColumn((1, 3, 6, 1, 2, 1, 10, 30, 5, 1, 17), Integer32().subtype(subtypeSpec=ConstraintsUnion(SingleValueConstraint(1, 2, 3))).clone(namedValues=NamedValues(("disabled", 1), ("enabledDs1", 2), ("enabledDs2", 3)))).setMaxAccess("readwrite") if mibBuilder.loadTexts: dsx3Channelization.setStatus('current') dsx3Ds1ForRemoteLoop = MibTableColumn((1, 3, 6, 1, 2, 1, 10, 30, 5, 1, 18), Integer32().subtype(subtypeSpec=ValueRangeConstraint(0, 29))).setMaxAccess("readwrite") if mibBuilder.loadTexts: dsx3Ds1ForRemoteLoop.setStatus('current') dsx3CurrentTable = MibTable((1, 3, 6, 1, 2, 1, 10, 30, 6), ) if mibBuilder.loadTexts: dsx3CurrentTable.setStatus('current') dsx3CurrentEntry = MibTableRow((1, 3, 6, 1, 2, 1, 10, 30, 6, 1), ).setIndexNames((0, "RFC1407-MIB", "dsx3CurrentIndex")) if mibBuilder.loadTexts: dsx3CurrentEntry.setStatus('current') dsx3CurrentIndex = MibTableColumn((1, 3, 6, 1, 2, 1, 10, 30, 6, 1, 1), InterfaceIndex()).setMaxAccess("readonly") if mibBuilder.loadTexts: dsx3CurrentIndex.setStatus('current') dsx3CurrentPESs = MibTableColumn((1, 3, 6, 1, 2, 1, 10, 30, 6, 1, 2), PerfCurrentCount()).setMaxAccess("readonly") if mibBuilder.loadTexts: dsx3CurrentPESs.setStatus('current') dsx3CurrentPSESs = MibTableColumn((1, 3, 6, 1, 2, 1, 10, 30, 6, 1, 3), PerfCurrentCount()).setMaxAccess("readonly") if mibBuilder.loadTexts: dsx3CurrentPSESs.setStatus('current') dsx3CurrentSEFSs = MibTableColumn((1, 3, 6, 1, 2, 1, 10, 30, 6, 1, 4), PerfCurrentCount()).setMaxAccess("readonly") if mibBuilder.loadTexts: dsx3CurrentSEFSs.setStatus('current') dsx3CurrentUASs = MibTableColumn((1, 3, 6, 1, 2, 1, 10, 30, 6, 1, 5), PerfCurrentCount()).setMaxAccess("readonly") if mibBuilder.loadTexts: dsx3CurrentUASs.setStatus('current') dsx3CurrentLCVs = MibTableColumn((1, 3, 6, 1, 2, 1, 10, 30, 6, 1, 6), PerfCurrentCount()).setMaxAccess("readonly") if mibBuilder.loadTexts: dsx3CurrentLCVs.setStatus('current') dsx3CurrentPCVs = MibTableColumn((1, 3, 6, 1, 2, 1, 10, 30, 6, 1, 7), PerfCurrentCount()).setMaxAccess("readonly") if mibBuilder.loadTexts: dsx3CurrentPCVs.setStatus('current') dsx3CurrentLESs = MibTableColumn((1, 3, 6, 1, 2, 1, 10, 30, 6, 1, 8), PerfCurrentCount()).setMaxAccess("readonly") if mibBuilder.loadTexts: dsx3CurrentLESs.setStatus('current') dsx3CurrentCCVs = MibTableColumn((1, 3, 6, 1, 2, 1, 10, 30, 6, 1, 9), PerfCurrentCount()).setMaxAccess("readonly") if mibBuilder.loadTexts: dsx3CurrentCCVs.setStatus('current') dsx3CurrentCESs = MibTableColumn((1, 3, 6, 1, 2, 1, 10, 30, 6, 1, 10), PerfCurrentCount()).setMaxAccess("readonly") if mibBuilder.loadTexts: dsx3CurrentCESs.setStatus('current') dsx3CurrentCSESs = MibTableColumn((1, 3, 6, 1, 2, 1, 10, 30, 6, 1, 11), PerfCurrentCount()).setMaxAccess("readonly") if mibBuilder.loadTexts: dsx3CurrentCSESs.setStatus('current') dsx3IntervalTable = MibTable((1, 3, 6, 1, 2, 1, 10, 30, 7), ) if mibBuilder.loadTexts: dsx3IntervalTable.setStatus('current') dsx3IntervalEntry = MibTableRow((1, 3, 6, 1, 2, 1, 10, 30, 7, 1), ).setIndexNames((0, "RFC1407-MIB", "dsx3IntervalIndex"), (0, "RFC1407-MIB", "dsx3IntervalNumber")) if mibBuilder.loadTexts: dsx3IntervalEntry.setStatus('current') dsx3IntervalIndex = MibTableColumn((1, 3, 6, 1, 2, 1, 10, 30, 7, 1, 1), InterfaceIndex()).setMaxAccess("readonly") if mibBuilder.loadTexts: dsx3IntervalIndex.setStatus('current') dsx3IntervalNumber = MibTableColumn((1, 3, 6, 1, 2, 1, 10, 30, 7, 1, 2), Integer32().subtype(subtypeSpec=ValueRangeConstraint(1, 96))).setMaxAccess("readonly") if mibBuilder.loadTexts: dsx3IntervalNumber.setStatus('current') dsx3IntervalPESs = MibTableColumn((1, 3, 6, 1, 2, 1, 10, 30, 7, 1, 3), PerfIntervalCount()).setMaxAccess("readonly") if mibBuilder.loadTexts: dsx3IntervalPESs.setStatus('current') dsx3IntervalPSESs = MibTableColumn((1, 3, 6, 1, 2, 1, 10, 30, 7, 1, 4), PerfIntervalCount()).setMaxAccess("readonly") if mibBuilder.loadTexts: dsx3IntervalPSESs.setStatus('current') dsx3IntervalSEFSs = MibTableColumn((1, 3, 6, 1, 2, 1, 10, 30, 7, 1, 5), PerfIntervalCount()).setMaxAccess("readonly") if mibBuilder.loadTexts: dsx3IntervalSEFSs.setStatus('current') dsx3IntervalUASs = MibTableColumn((1, 3, 6, 1, 2, 1, 10, 30, 7, 1, 6), PerfIntervalCount()).setMaxAccess("readonly") if mibBuilder.loadTexts: dsx3IntervalUASs.setStatus('current') dsx3IntervalLCVs = MibTableColumn((1, 3, 6, 1, 2, 1, 10, 30, 7, 1, 7), PerfIntervalCount()).setMaxAccess("readonly") if mibBuilder.loadTexts: dsx3IntervalLCVs.setStatus('current') dsx3IntervalPCVs = MibTableColumn((1, 3, 6, 1, 2, 1, 10, 30, 7, 1, 8), PerfIntervalCount()).setMaxAccess("readonly") if mibBuilder.loadTexts: dsx3IntervalPCVs.setStatus('current') dsx3IntervalLESs = MibTableColumn((1, 3, 6, 1, 2, 1, 10, 30, 7, 1, 9), PerfIntervalCount()).setMaxAccess("readonly") if mibBuilder.loadTexts: dsx3IntervalLESs.setStatus('current') dsx3IntervalCCVs = MibTableColumn((1, 3, 6, 1, 2, 1, 10, 30, 7, 1, 10), PerfIntervalCount()).setMaxAccess("readonly") if mibBuilder.loadTexts: dsx3IntervalCCVs.setStatus('current') dsx3IntervalCESs = MibTableColumn((1, 3, 6, 1, 2, 1, 10, 30, 7, 1, 11), PerfIntervalCount()).setMaxAccess("readonly") if mibBuilder.loadTexts: dsx3IntervalCESs.setStatus('current') dsx3IntervalCSESs = MibTableColumn((1, 3, 6, 1, 2, 1, 10, 30, 7, 1, 12), PerfIntervalCount()).setMaxAccess("readonly") if mibBuilder.loadTexts: dsx3IntervalCSESs.setStatus('current') dsx3IntervalValidData = MibTableColumn((1, 3, 6, 1, 2, 1, 10, 30, 7, 1, 13), TruthValue()).setMaxAccess("readonly") if mibBuilder.loadTexts: dsx3IntervalValidData.setStatus('current') dsx3TotalTable = MibTable((1, 3, 6, 1, 2, 1, 10, 30, 8), ) if mibBuilder.loadTexts: dsx3TotalTable.setStatus('current') dsx3TotalEntry = MibTableRow((1, 3, 6, 1, 2, 1, 10, 30, 8, 1), ).setIndexNames((0, "RFC1407-MIB", "dsx3TotalIndex")) if mibBuilder.loadTexts: dsx3TotalEntry.setStatus('current') dsx3TotalIndex = MibTableColumn((1, 3, 6, 1, 2, 1, 10, 30, 8, 1, 1), InterfaceIndex()).setMaxAccess("readonly") if mibBuilder.loadTexts: dsx3TotalIndex.setStatus('current') dsx3TotalPESs = MibTableColumn((1, 3, 6, 1, 2, 1, 10, 30, 8, 1, 2), PerfTotalCount()).setMaxAccess("readonly") if mibBuilder.loadTexts: dsx3TotalPESs.setStatus('current') dsx3TotalPSESs = MibTableColumn((1, 3, 6, 1, 2, 1, 10, 30, 8, 1, 3), PerfTotalCount()).setMaxAccess("readonly") if mibBuilder.loadTexts: dsx3TotalPSESs.setStatus('current') dsx3TotalSEFSs = MibTableColumn((1, 3, 6, 1, 2, 1, 10, 30, 8, 1, 4), PerfTotalCount()).setMaxAccess("readonly") if mibBuilder.loadTexts: dsx3TotalSEFSs.setStatus('current') dsx3TotalUASs = MibTableColumn((1, 3, 6, 1, 2, 1, 10, 30, 8, 1, 5), PerfTotalCount()).setMaxAccess("readonly") if mibBuilder.loadTexts: dsx3TotalUASs.setStatus('current') dsx3TotalLCVs = MibTableColumn((1, 3, 6, 1, 2, 1, 10, 30, 8, 1, 6), PerfTotalCount()).setMaxAccess("readonly") if mibBuilder.loadTexts: dsx3TotalLCVs.setStatus('current') dsx3TotalPCVs = MibTableColumn((1, 3, 6, 1, 2, 1, 10, 30, 8, 1, 7), PerfTotalCount()).setMaxAccess("readonly") if mibBuilder.loadTexts: dsx3TotalPCVs.setStatus('current') dsx3TotalLESs = MibTableColumn((1, 3, 6, 1, 2, 1, 10, 30, 8, 1, 8), PerfTotalCount()).setMaxAccess("readonly") if mibBuilder.loadTexts: dsx3TotalLESs.setStatus('current') dsx3TotalCCVs = MibTableColumn((1, 3, 6, 1, 2, 1, 10, 30, 8, 1, 9), PerfTotalCount()).setMaxAccess("readonly") if mibBuilder.loadTexts: dsx3TotalCCVs.setStatus('current') dsx3TotalCESs = MibTableColumn((1, 3, 6, 1, 2, 1, 10, 30, 8, 1, 10), PerfTotalCount()).setMaxAccess("readonly") if mibBuilder.loadTexts: dsx3TotalCESs.setStatus('current') dsx3TotalCSESs = MibTableColumn((1, 3, 6, 1, 2, 1, 10, 30, 8, 1, 11), PerfTotalCount()).setMaxAccess("readonly") if mibBuilder.loadTexts: dsx3TotalCSESs.setStatus('current') dsx3FarEndConfigTable = MibTable((1, 3, 6, 1, 2, 1, 10, 30, 9), ) if mibBuilder.loadTexts: dsx3FarEndConfigTable.setStatus('current') dsx3FarEndConfigEntry = MibTableRow((1, 3, 6, 1, 2, 1, 10, 30, 9, 1), ).setIndexNames((0, "RFC1407-MIB", "dsx3FarEndLineIndex")) if mibBuilder.loadTexts: dsx3FarEndConfigEntry.setStatus('current') dsx3FarEndLineIndex = MibTableColumn((1, 3, 6, 1, 2, 1, 10, 30, 9, 1, 1), InterfaceIndex()).setMaxAccess("readonly") if mibBuilder.loadTexts: dsx3FarEndLineIndex.setStatus('current') dsx3FarEndEquipCode = MibTableColumn((1, 3, 6, 1, 2, 1, 10, 30, 9, 1, 2), DisplayString().subtype(subtypeSpec=ValueSizeConstraint(0, 10))).setMaxAccess("readwrite") if mibBuilder.loadTexts: dsx3FarEndEquipCode.setStatus('current') dsx3FarEndLocationIDCode = MibTableColumn((1, 3, 6, 1, 2, 1, 10, 30, 9, 1, 3), DisplayString().subtype(subtypeSpec=ValueSizeConstraint(0, 11))).setMaxAccess("readwrite") if mibBuilder.loadTexts: dsx3FarEndLocationIDCode.setStatus('current') dsx3FarEndFrameIDCode = MibTableColumn((1, 3, 6, 1, 2, 1, 10, 30, 9, 1, 4), DisplayString().subtype(subtypeSpec=ValueSizeConstraint(0, 10))).setMaxAccess("readwrite") if mibBuilder.loadTexts: dsx3FarEndFrameIDCode.setStatus('current') dsx3FarEndUnitCode = MibTableColumn((1, 3, 6, 1, 2, 1, 10, 30, 9, 1, 5), DisplayString().subtype(subtypeSpec=ValueSizeConstraint(0, 6))).setMaxAccess("readwrite") if mibBuilder.loadTexts: dsx3FarEndUnitCode.setStatus('current') dsx3FarEndFacilityIDCode = MibTableColumn((1, 3, 6, 1, 2, 1, 10, 30, 9, 1, 6), DisplayString().subtype(subtypeSpec=ValueSizeConstraint(0, 38))).setMaxAccess("readwrite") if mibBuilder.loadTexts: dsx3FarEndFacilityIDCode.setStatus('current') dsx3FarEndCurrentTable = MibTable((1, 3, 6, 1, 2, 1, 10, 30, 10), ) if mibBuilder.loadTexts: dsx3FarEndCurrentTable.setStatus('current') dsx3FarEndCurrentEntry = MibTableRow((1, 3, 6, 1, 2, 1, 10, 30, 10, 1), ).setIndexNames((0, "RFC1407-MIB", "dsx3FarEndCurrentIndex")) if mibBuilder.loadTexts: dsx3FarEndCurrentEntry.setStatus('current') dsx3FarEndCurrentIndex = MibTableColumn((1, 3, 6, 1, 2, 1, 10, 30, 10, 1, 1), InterfaceIndex()).setMaxAccess("readonly") if mibBuilder.loadTexts: dsx3FarEndCurrentIndex.setStatus('current') dsx3FarEndTimeElapsed = MibTableColumn((1, 3, 6, 1, 2, 1, 10, 30, 10, 1, 2), Integer32().subtype(subtypeSpec=ValueRangeConstraint(0, 899))).setMaxAccess("readonly") if mibBuilder.loadTexts: dsx3FarEndTimeElapsed.setStatus('current') dsx3FarEndValidIntervals = MibTableColumn((1, 3, 6, 1, 2, 1, 10, 30, 10, 1, 3), Integer32().subtype(subtypeSpec=ValueRangeConstraint(0, 96))).setMaxAccess("readonly") if mibBuilder.loadTexts: dsx3FarEndValidIntervals.setStatus('current') dsx3FarEndCurrentCESs = MibTableColumn((1, 3, 6, 1, 2, 1, 10, 30, 10, 1, 4), PerfCurrentCount()).setMaxAccess("readonly") if mibBuilder.loadTexts: dsx3FarEndCurrentCESs.setStatus('current') dsx3FarEndCurrentCSESs = MibTableColumn((1, 3, 6, 1, 2, 1, 10, 30, 10, 1, 5), PerfCurrentCount()).setMaxAccess("readonly") if mibBuilder.loadTexts: dsx3FarEndCurrentCSESs.setStatus('current') dsx3FarEndCurrentCCVs = MibTableColumn((1, 3, 6, 1, 2, 1, 10, 30, 10, 1, 6), PerfCurrentCount()).setMaxAccess("readonly") if mibBuilder.loadTexts: dsx3FarEndCurrentCCVs.setStatus('current') dsx3FarEndCurrentUASs = MibTableColumn((1, 3, 6, 1, 2, 1, 10, 30, 10, 1, 7), PerfCurrentCount()).setMaxAccess("readonly") if mibBuilder.loadTexts: dsx3FarEndCurrentUASs.setStatus('current') dsx3FarEndInvalidIntervals = MibTableColumn((1, 3, 6, 1, 2, 1, 10, 30, 10, 1, 8), Integer32().subtype(subtypeSpec=ValueRangeConstraint(0, 96))).setMaxAccess("readonly") if mibBuilder.loadTexts: dsx3FarEndInvalidIntervals.setStatus('current') dsx3FarEndIntervalTable = MibTable((1, 3, 6, 1, 2, 1, 10, 30, 11), ) if mibBuilder.loadTexts: dsx3FarEndIntervalTable.setStatus('current') dsx3FarEndIntervalEntry = MibTableRow((1, 3, 6, 1, 2, 1, 10, 30, 11, 1), ).setIndexNames((0, "RFC1407-MIB", "dsx3FarEndIntervalIndex"), (0, "RFC1407-MIB", "dsx3FarEndIntervalNumber")) if mibBuilder.loadTexts: dsx3FarEndIntervalEntry.setStatus('current') dsx3FarEndIntervalIndex = MibTableColumn((1, 3, 6, 1, 2, 1, 10, 30, 11, 1, 1), InterfaceIndex()).setMaxAccess("readonly") if mibBuilder.loadTexts: dsx3FarEndIntervalIndex.setStatus('current') dsx3FarEndIntervalNumber = MibTableColumn((1, 3, 6, 1, 2, 1, 10, 30, 11, 1, 2), Integer32().subtype(subtypeSpec=ValueRangeConstraint(1, 96))).setMaxAccess("readonly") if mibBuilder.loadTexts: dsx3FarEndIntervalNumber.setStatus('current') dsx3FarEndIntervalCESs = MibTableColumn((1, 3, 6, 1, 2, 1, 10, 30, 11, 1, 3), PerfIntervalCount()).setMaxAccess("readonly") if mibBuilder.loadTexts: dsx3FarEndIntervalCESs.setStatus('current') dsx3FarEndIntervalCSESs = MibTableColumn((1, 3, 6, 1, 2, 1, 10, 30, 11, 1, 4), PerfIntervalCount()).setMaxAccess("readonly") if mibBuilder.loadTexts: dsx3FarEndIntervalCSESs.setStatus('current') dsx3FarEndIntervalCCVs = MibTableColumn((1, 3, 6, 1, 2, 1, 10, 30, 11, 1, 5), PerfIntervalCount()).setMaxAccess("readonly") if mibBuilder.loadTexts: dsx3FarEndIntervalCCVs.setStatus('current') dsx3FarEndIntervalUASs = MibTableColumn((1, 3, 6, 1, 2, 1, 10, 30, 11, 1, 6), PerfIntervalCount()).setMaxAccess("readonly") if mibBuilder.loadTexts: dsx3FarEndIntervalUASs.setStatus('current') dsx3FarEndIntervalValidData = MibTableColumn((1, 3, 6, 1, 2, 1, 10, 30, 11, 1, 7), TruthValue()).setMaxAccess("readonly") if mibBuilder.loadTexts: dsx3FarEndIntervalValidData.setStatus('current') dsx3FarEndTotalTable = MibTable((1, 3, 6, 1, 2, 1, 10, 30, 12), ) if mibBuilder.loadTexts: dsx3FarEndTotalTable.setStatus('current') dsx3FarEndTotalEntry = MibTableRow((1, 3, 6, 1, 2, 1, 10, 30, 12, 1), ).setIndexNames((0, "RFC1407-MIB", "dsx3FarEndTotalIndex")) if mibBuilder.loadTexts: dsx3FarEndTotalEntry.setStatus('current') dsx3FarEndTotalIndex = MibTableColumn((1, 3, 6, 1, 2, 1, 10, 30, 12, 1, 1), InterfaceIndex()).setMaxAccess("readonly") if mibBuilder.loadTexts: dsx3FarEndTotalIndex.setStatus('current') dsx3FarEndTotalCESs = MibTableColumn((1, 3, 6, 1, 2, 1, 10, 30, 12, 1, 2), PerfTotalCount()).setMaxAccess("readonly") if mibBuilder.loadTexts: dsx3FarEndTotalCESs.setStatus('current') dsx3FarEndTotalCSESs = MibTableColumn((1, 3, 6, 1, 2, 1, 10, 30, 12, 1, 3), PerfTotalCount()).setMaxAccess("readonly") if mibBuilder.loadTexts: dsx3FarEndTotalCSESs.setStatus('current') dsx3FarEndTotalCCVs = MibTableColumn((1, 3, 6, 1, 2, 1, 10, 30, 12, 1, 4), PerfTotalCount()).setMaxAccess("readonly") if mibBuilder.loadTexts: dsx3FarEndTotalCCVs.setStatus('current') dsx3FarEndTotalUASs = MibTableColumn((1, 3, 6, 1, 2, 1, 10, 30, 12, 1, 5), PerfTotalCount()).setMaxAccess("readonly") if mibBuilder.loadTexts: dsx3FarEndTotalUASs.setStatus('current') dsx3FracTable = MibTable((1, 3, 6, 1, 2, 1, 10, 30, 13), ) if mibBuilder.loadTexts: dsx3FracTable.setStatus('deprecated') dsx3FracEntry = MibTableRow((1, 3, 6, 1, 2, 1, 10, 30, 13, 1), ).setIndexNames((0, "RFC1407-MIB", "dsx3FracIndex"), (0, "RFC1407-MIB", "dsx3FracNumber")) if mibBuilder.loadTexts: dsx3FracEntry.setStatus('deprecated') dsx3FracIndex = MibTableColumn((1, 3, 6, 1, 2, 1, 10, 30, 13, 1, 1), Integer32().subtype(subtypeSpec=ValueRangeConstraint(1, 2147483647))).setMaxAccess("readonly") if mibBuilder.loadTexts: dsx3FracIndex.setStatus('deprecated') dsx3FracNumber = MibTableColumn((1, 3, 6, 1, 2, 1, 10, 30, 13, 1, 2), Integer32().subtype(subtypeSpec=ValueRangeConstraint(1, 31))).setMaxAccess("readonly") if mibBuilder.loadTexts: dsx3FracNumber.setStatus('deprecated') dsx3FracIfIndex = MibTableColumn((1, 3, 6, 1, 2, 1, 10, 30, 13, 1, 3), Integer32().subtype(subtypeSpec=ValueRangeConstraint(0, 2147483647))).setMaxAccess("readwrite") if mibBuilder.loadTexts: dsx3FracIfIndex.setStatus('deprecated') ds3Traps = MibIdentifier((1, 3, 6, 1, 2, 1, 10, 30, 15)) dsx3LineStatusChange = NotificationType((1, 3, 6, 1, 2, 1, 10, 30, 15, 0, 1)).setObjects(("RFC1407-MIB", "dsx3LineStatus"), ("RFC1407-MIB", "dsx3LineStatusLastChange")) if mibBuilder.loadTexts: dsx3LineStatusChange.setStatus('current') ds3Conformance = MibIdentifier((1, 3, 6, 1, 2, 1, 10, 30, 14)) ds3Groups = MibIdentifier((1, 3, 6, 1, 2, 1, 10, 30, 14, 1)) ds3Compliances = MibIdentifier((1, 3, 6, 1, 2, 1, 10, 30, 14, 2)) ds3Compliance = ModuleCompliance((1, 3, 6, 1, 2, 1, 10, 30, 14, 2, 1)).setObjects(("RFC1407-MIB", "ds3NearEndConfigGroup"), ("RFC1407-MIB", "ds3NearEndStatisticsGroup"), ("RFC1407-MIB", "ds3FarEndGroup"), ("RFC1407-MIB", "ds3NearEndOptionalTrapGroup"), ("RFC1407-MIB", "ds3NearEndOptionalConfigGroup")) if getattr(mibBuilder, 'version', (0, 0, 0)) > (4, 4, 0): ds3Compliance = ds3Compliance.setStatus('current') ds3NearEndConfigGroup = ObjectGroup((1, 3, 6, 1, 2, 1, 10, 30, 14, 1, 1)).setObjects(("RFC1407-MIB", "dsx3LineIndex"), ("RFC1407-MIB", "dsx3TimeElapsed"), ("RFC1407-MIB", "dsx3ValidIntervals"), ("RFC1407-MIB", "dsx3LineType"), ("RFC1407-MIB", "dsx3LineCoding"), ("RFC1407-MIB", "dsx3SendCode"), ("RFC1407-MIB", "dsx3CircuitIdentifier"), ("RFC1407-MIB", "dsx3LoopbackConfig"), ("RFC1407-MIB", "dsx3LineStatus"), ("RFC1407-MIB", "dsx3TransmitClockSource"), ("RFC1407-MIB", "dsx3InvalidIntervals"), ("RFC1407-MIB", "dsx3LineLength"), ("RFC1407-MIB", "dsx3LoopbackStatus"), ("RFC1407-MIB", "dsx3Channelization"), ("RFC1407-MIB", "dsx3Ds1ForRemoteLoop")) if getattr(mibBuilder, 'version', (0, 0, 0)) > (4, 4, 0): ds3NearEndConfigGroup = ds3NearEndConfigGroup.setStatus('current') ds3NearEndStatisticsGroup = ObjectGroup((1, 3, 6, 1, 2, 1, 10, 30, 14, 1, 2)).setObjects(("RFC1407-MIB", "dsx3CurrentIndex"), ("RFC1407-MIB", "dsx3CurrentPESs"), ("RFC1407-MIB", "dsx3CurrentPSESs"), ("RFC1407-MIB", "dsx3CurrentSEFSs"), ("RFC1407-MIB", "dsx3CurrentUASs"), ("RFC1407-MIB", "dsx3CurrentLCVs"), ("RFC1407-MIB", "dsx3CurrentPCVs"), ("RFC1407-MIB", "dsx3CurrentLESs"), ("RFC1407-MIB", "dsx3CurrentCCVs"), ("RFC1407-MIB", "dsx3CurrentCESs"), ("RFC1407-MIB", "dsx3CurrentCSESs"), ("RFC1407-MIB", "dsx3IntervalIndex"), ("RFC1407-MIB", "dsx3IntervalNumber"), ("RFC1407-MIB", "dsx3IntervalPESs"), ("RFC1407-MIB", "dsx3IntervalPSESs"), ("RFC1407-MIB", "dsx3IntervalSEFSs"), ("RFC1407-MIB", "dsx3IntervalUASs"), ("RFC1407-MIB", "dsx3IntervalLCVs"), ("RFC1407-MIB", "dsx3IntervalPCVs"), ("RFC1407-MIB", "dsx3IntervalLESs"), ("RFC1407-MIB", "dsx3IntervalCCVs"), ("RFC1407-MIB", "dsx3IntervalCESs"), ("RFC1407-MIB", "dsx3IntervalCSESs"), ("RFC1407-MIB", "dsx3IntervalValidData"), ("RFC1407-MIB", "dsx3TotalIndex"), ("RFC1407-MIB", "dsx3TotalPESs"), ("RFC1407-MIB", "dsx3TotalPSESs"), ("RFC1407-MIB", "dsx3TotalSEFSs"), ("RFC1407-MIB", "dsx3TotalUASs"), ("RFC1407-MIB", "dsx3TotalLCVs"), ("RFC1407-MIB", "dsx3TotalPCVs"), ("RFC1407-MIB", "dsx3TotalLESs"), ("RFC1407-MIB", "dsx3TotalCCVs"), ("RFC1407-MIB", "dsx3TotalCESs"), ("RFC1407-MIB", "dsx3TotalCSESs")) if getattr(mibBuilder, 'version', (0, 0, 0)) > (4, 4, 0): ds3NearEndStatisticsGroup = ds3NearEndStatisticsGroup.setStatus('current') ds3FarEndGroup = ObjectGroup((1, 3, 6, 1, 2, 1, 10, 30, 14, 1, 3)).setObjects(("RFC1407-MIB", "dsx3FarEndLineIndex"), ("RFC1407-MIB", "dsx3FarEndEquipCode"), ("RFC1407-MIB", "dsx3FarEndLocationIDCode"), ("RFC1407-MIB", "dsx3FarEndFrameIDCode"), ("RFC1407-MIB", "dsx3FarEndUnitCode"), ("RFC1407-MIB", "dsx3FarEndFacilityIDCode"), ("RFC1407-MIB", "dsx3FarEndCurrentIndex"), ("RFC1407-MIB", "dsx3FarEndTimeElapsed"), ("RFC1407-MIB", "dsx3FarEndValidIntervals"), ("RFC1407-MIB", "dsx3FarEndCurrentCESs"), ("RFC1407-MIB", "dsx3FarEndCurrentCSESs"), ("RFC1407-MIB", "dsx3FarEndCurrentCCVs"), ("RFC1407-MIB", "dsx3FarEndCurrentUASs"), ("RFC1407-MIB", "dsx3FarEndInvalidIntervals"), ("RFC1407-MIB", "dsx3FarEndIntervalIndex"), ("RFC1407-MIB", "dsx3FarEndIntervalNumber"), ("RFC1407-MIB", "dsx3FarEndIntervalCESs"), ("RFC1407-MIB", "dsx3FarEndIntervalCSESs"), ("RFC1407-MIB", "dsx3FarEndIntervalCCVs"), ("RFC1407-MIB", "dsx3FarEndIntervalUASs"), ("RFC1407-MIB", "dsx3FarEndIntervalValidData"), ("RFC1407-MIB", "dsx3FarEndTotalIndex"), ("RFC1407-MIB", "dsx3FarEndTotalCESs"), ("RFC1407-MIB", "dsx3FarEndTotalCSESs"), ("RFC1407-MIB", "dsx3FarEndTotalCCVs"), ("RFC1407-MIB", "dsx3FarEndTotalUASs")) if getattr(mibBuilder, 'version', (0, 0, 0)) > (4, 4, 0): ds3FarEndGroup = ds3FarEndGroup.setStatus('current') ds3DeprecatedGroup = ObjectGroup((1, 3, 6, 1, 2, 1, 10, 30, 14, 1, 4)).setObjects(("RFC1407-MIB", "dsx3IfIndex"), ("RFC1407-MIB", "dsx3FracIndex"), ("RFC1407-MIB", "dsx3FracNumber"), ("RFC1407-MIB", "dsx3FracIfIndex")) if getattr(mibBuilder, 'version', (0, 0, 0)) > (4, 4, 0): ds3DeprecatedGroup = ds3DeprecatedGroup.setStatus('deprecated') ds3NearEndOptionalConfigGroup = ObjectGroup((1, 3, 6, 1, 2, 1, 10, 30, 14, 1, 5)).setObjects(("RFC1407-MIB", "dsx3LineStatusLastChange"), ("RFC1407-MIB", "dsx3LineStatusChangeTrapEnable")) if getattr(mibBuilder, 'version', (0, 0, 0)) > (4, 4, 0): ds3NearEndOptionalConfigGroup = ds3NearEndOptionalConfigGroup.setStatus('current') ds3NearEndOptionalTrapGroup = NotificationGroup((1, 3, 6, 1, 2, 1, 10, 30, 14, 1, 6)).setObjects(("RFC1407-MIB", "dsx3LineStatusChange")) if getattr(mibBuilder, 'version', (0, 0, 0)) > (4, 4, 0): ds3NearEndOptionalTrapGroup = ds3NearEndOptionalTrapGroup.setStatus('current') mibBuilder.exportSymbols("RFC1407-MIB", ds3Compliances=ds3Compliances, dsx3FarEndFacilityIDCode=dsx3FarEndFacilityIDCode, dsx3IntervalNumber=dsx3IntervalNumber, dsx3IntervalCSESs=dsx3IntervalCSESs, dsx3CurrentUASs=dsx3CurrentUASs, dsx3IntervalSEFSs=dsx3IntervalSEFSs, dsx3CurrentCESs=dsx3CurrentCESs, ds3Traps=ds3Traps, dsx3TotalPCVs=dsx3TotalPCVs, dsx3FarEndTotalCSESs=dsx3FarEndTotalCSESs, dsx3TotalPESs=dsx3TotalPESs, dsx3FarEndEquipCode=dsx3FarEndEquipCode, dsx3LineCoding=dsx3LineCoding, dsx3CurrentLCVs=dsx3CurrentLCVs, ds3Compliance=ds3Compliance, dsx3FracEntry=dsx3FracEntry, dsx3IntervalLCVs=dsx3IntervalLCVs, dsx3FarEndIntervalEntry=dsx3FarEndIntervalEntry, dsx3FracTable=dsx3FracTable, ds3NearEndConfigGroup=ds3NearEndConfigGroup, dsx3CurrentIndex=dsx3CurrentIndex, dsx3TotalCSESs=dsx3TotalCSESs, dsx3LineStatus=dsx3LineStatus, dsx3LineStatusChangeTrapEnable=dsx3LineStatusChangeTrapEnable, dsx3IntervalCCVs=dsx3IntervalCCVs, dsx3IntervalEntry=dsx3IntervalEntry, dsx3IntervalUASs=dsx3IntervalUASs, dsx3FarEndCurrentEntry=dsx3FarEndCurrentEntry, ds3NearEndStatisticsGroup=ds3NearEndStatisticsGroup, dsx3FarEndIntervalIndex=dsx3FarEndIntervalIndex, dsx3ValidIntervals=dsx3ValidIntervals, dsx3FracNumber=dsx3FracNumber, dsx3CurrentCCVs=dsx3CurrentCCVs, dsx3CurrentTable=dsx3CurrentTable, dsx3FarEndTimeElapsed=dsx3FarEndTimeElapsed, dsx3TotalPSESs=dsx3TotalPSESs, dsx3FarEndCurrentTable=dsx3FarEndCurrentTable, dsx3FarEndTotalIndex=dsx3FarEndTotalIndex, dsx3ConfigTable=dsx3ConfigTable, dsx3CurrentPCVs=dsx3CurrentPCVs, dsx3FarEndValidIntervals=dsx3FarEndValidIntervals, dsx3LineStatusChange=dsx3LineStatusChange, dsx3IntervalTable=dsx3IntervalTable, dsx3Ds1ForRemoteLoop=dsx3Ds1ForRemoteLoop, dsx3ConfigEntry=dsx3ConfigEntry, dsx3TotalIndex=dsx3TotalIndex, dsx3FarEndIntervalUASs=dsx3FarEndIntervalUASs, dsx3FarEndConfigEntry=dsx3FarEndConfigEntry, ds3FarEndGroup=ds3FarEndGroup, dsx3CurrentPSESs=dsx3CurrentPSESs, dsx3IntervalPESs=dsx3IntervalPESs, dsx3TotalCCVs=dsx3TotalCCVs, dsx3FarEndCurrentCCVs=dsx3FarEndCurrentCCVs, dsx3LineIndex=dsx3LineIndex, dsx3IntervalPSESs=dsx3IntervalPSESs, dsx3FarEndLocationIDCode=dsx3FarEndLocationIDCode, dsx3FarEndInvalidIntervals=dsx3FarEndInvalidIntervals, dsx3FarEndTotalCESs=dsx3FarEndTotalCESs, dsx3FarEndIntervalCSESs=dsx3FarEndIntervalCSESs, dsx3IntervalPCVs=dsx3IntervalPCVs, dsx3FarEndIntervalCESs=dsx3FarEndIntervalCESs, dsx3CurrentSEFSs=dsx3CurrentSEFSs, dsx3TotalTable=dsx3TotalTable, dsx3FarEndLineIndex=dsx3FarEndLineIndex, dsx3TransmitClockSource=dsx3TransmitClockSource, dsx3CurrentPESs=dsx3CurrentPESs, dsx3LineStatusLastChange=dsx3LineStatusLastChange, dsx3FarEndUnitCode=dsx3FarEndUnitCode, dsx3FarEndIntervalCCVs=dsx3FarEndIntervalCCVs, dsx3FarEndCurrentUASs=dsx3FarEndCurrentUASs, dsx3IntervalIndex=dsx3IntervalIndex, dsx3FarEndFrameIDCode=dsx3FarEndFrameIDCode, dsx3CurrentCSESs=dsx3CurrentCSESs, dsx3IntervalCESs=dsx3IntervalCESs, dsx3CircuitIdentifier=dsx3CircuitIdentifier, ds3Conformance=ds3Conformance, ds3DeprecatedGroup=ds3DeprecatedGroup, dsx3FarEndCurrentIndex=dsx3FarEndCurrentIndex, dsx3FarEndIntervalValidData=dsx3FarEndIntervalValidData, dsx3FarEndTotalEntry=dsx3FarEndTotalEntry, dsx3FracIfIndex=dsx3FracIfIndex, dsx3FarEndTotalCCVs=dsx3FarEndTotalCCVs, dsx3TotalEntry=dsx3TotalEntry, dsx3LoopbackConfig=dsx3LoopbackConfig, dsx3FracIndex=dsx3FracIndex, dsx3FarEndIntervalNumber=dsx3FarEndIntervalNumber, ds3Groups=ds3Groups, dsx3IntervalLESs=dsx3IntervalLESs, dsx3TotalCESs=dsx3TotalCESs, PYSNMP_MODULE_ID=ds3, dsx3FarEndCurrentCESs=dsx3FarEndCurrentCESs, dsx3Channelization=dsx3Channelization, dsx3CurrentEntry=dsx3CurrentEntry, dsx3FarEndCurrentCSESs=dsx3FarEndCurrentCSESs, dsx3IfIndex=dsx3IfIndex, dsx3FarEndTotalUASs=dsx3FarEndTotalUASs, dsx3SendCode=dsx3SendCode, dsx3TotalUASs=dsx3TotalUASs, dsx3TimeElapsed=dsx3TimeElapsed, dsx3InvalidIntervals=dsx3InvalidIntervals, dsx3LineType=dsx3LineType, dsx3FarEndConfigTable=dsx3FarEndConfigTable, dsx3LoopbackStatus=dsx3LoopbackStatus, dsx3FarEndIntervalTable=dsx3FarEndIntervalTable, ds3NearEndOptionalTrapGroup=ds3NearEndOptionalTrapGroup, ds3NearEndOptionalConfigGroup=ds3NearEndOptionalConfigGroup, dsx3FarEndTotalTable=dsx3FarEndTotalTable, dsx3IntervalValidData=dsx3IntervalValidData, dsx3LineLength=dsx3LineLength, dsx3TotalSEFSs=dsx3TotalSEFSs, dsx3TotalLESs=dsx3TotalLESs, ds3=ds3, dsx3TotalLCVs=dsx3TotalLCVs, dsx3CurrentLESs=dsx3CurrentLESs)
nilq/baby-python
python
# coding: utf8 """语音结果和指令比对""" from aip import AipNlp from nlp.configs import APP_ID, API_KEY, SECRET_KEY def match(voice2text, command): """匹配指令""" client = AipNlp(APP_ID, API_KEY, SECRET_KEY) nlp_result_grnn = client.simnet(voice2text, command, {"model": "GRNN"}) if nlp_result_grnn.get("score", 0.5) >= 0.8: return True return False if __name__ == "__main__": print(match("关闭设备", "打开设备"))
nilq/baby-python
python
from setuptools import setup setup(name='Numpyextension', version='1.1.1', description='A library focused functions that only require numpy.', url='https://www.github.com/pmp47/Numpyextension', author='pmp47', author_email='phil@zeural.com', license='MIT', packages=['numpyextension'], install_requires=['numpy==1.17.1'], zip_safe=False, include_package_data=True, python_requires='>=3.6', package_data={'': ['data/*.*']} )
nilq/baby-python
python
from cfg import * import socket import re from commands import commands def send_message(message): s.send(bytes("PRIVMSG #" + NICK + " :" + message + "\r\n", "UTF-8")) s = socket.socket() s.connect((HOST, PORT)) s.send(bytes("PASS " + PASS + "\r\n", "UTF-8")) s.send(bytes("NICK " + NICK + "\r\n", "UTF-8")) s.send(bytes("JOIN #" + NICK + " \r\n", "UTF-8")) send_message("Bot has just been turned on") CHAT_MSG=re.compile(r"^:\w+!\w+@\w+\.tmi\.twitch\.tv PRIVMSG #\w+ :") while True: response = s.recv(1024).decode("utf-8") if response == "PING :tmi.twitch.tv\r\n": s.send("PONG :tmi.twitch.tv\r\n".encode("utf-8")) else: username = re.search(r"\w+", response).group(0) message = CHAT_MSG.sub("", response) print(response) for command in commands: if(message.strip() == command): send_message(commands[command])
nilq/baby-python
python
from db_facts.jinja_context import pull_jinja_context import unittest from unittest.mock import patch from .mock_dbcli_config import mock_dbcli_config @patch('db_facts.env_jinja_context.os') class TestEnvJinjaContext(unittest.TestCase): def test_with_env_set(self, mock_os): config = { 'jinja_context_name': 'env', } context, filters = pull_jinja_context(['whatever', 'whatever'], config, mock_dbcli_config) self.assertEqual(context['getenv'], mock_os.getenv)
nilq/baby-python
python
# Generated by Django 3.1.2 on 2020-10-11 09:14 from django.db import migrations, models class Migration(migrations.Migration): dependencies = [ ('trainer', '0025_user_data_adaptivity'), ] operations = [ migrations.AddField( model_name='user', name='gamification', field=models.IntegerField(choices=[(0, 'Level-System'), (1, 'Level-System + individuelles Ranking'), (1, 'Level-System + Gruppen-Ranking'), (1, 'Bayes')], db_index=True, default=0), ), migrations.AddField( model_name='user', name='gamification_group', field=models.CharField(db_index=True, max_length=32, null=True), ), ]
nilq/baby-python
python
#!/usr/bin/env python """ Gaussian Distribution based two-tail error data reduction data-processing pipeline for dataframes """ # imports import matplotlib.pyplot as plt import numpy as np from scipy import stats __author__ = "Rik Ghosh" __copyright__ = "Copyright 2021, The University of Texas at Austin" __credits__ = ["Soham Saha", "Larissa Franco"] __license__ = "MIT" __version__ = "1.0.2" __maintainer__ = "Rik Ghosh" __email__ = "rikghosh487@gmail.com" __status__ = "Production" def chop_tails(data, param, bins=25, factor=1, verbose=0): """ Selects a certain parameter from a pandas DataFrame and creates a Matplotlib histogram for the given data. Then it fits a Gaussian Distribution to perform statistical error reduction\n Args: ► `data` (pandas.core.frame.DataFrame): The pandas dataframe that contains the dataset\n ► `param` (str): An attribute present in the data parameter that will be processed in the algorithm\n ► `bins` (int, optional): Number of bins for the histogram. Defaults to `25`\n ► `factor` (float, optional): Scaling factor for standard deviation for Gaussian fitting. Must be a positive value. Defaults to `1`\n ► `verbose` (int, optional): Verbosity for the algorithm. Must be in the range [0,2]. Defaults to `0`\n `0` → No histogram or debug statements, data returned post processeing\n `1` → Histogram displayed and data returned post processing\n `2` → Histogram displayed, debug statements printed, and data returned\n Returns:\n ► data (pandas.core.frame.Dataframe): the same dataframe `data` after the required restrictions have been enforced via the Gaussian Distribution and `pthresh`\n Raises:\n `KeyError` → invalid parameter `param` `ValueError` → `bins < 0`\n `ValueError` → `factor < 0`\n `ValueError` → `verbosity != 0` or `1` or `2`\n """ # scope constants raw_array = None acceptable_verbose = [0, 1, 2] # prerequirement checks try: raw_array = np.array(data[param]) except KeyError: print(f"The parameter {param} is not present in the dataframe. Please provide a valid parameter") if bins < 1: raise ValueError(f"Must have at least 1 bin.\nValue provided: {bins}") if factor < 0.: raise ValueError(f"factor must be a positive number.\nValue provided: {factor}") if verbose not in acceptable_verbose: raise ValueError(f"Verbosity can only be 0, 1, or 2.\nValue provided: {verbose}") mean = np.mean(raw_array) # mean std = np.std(raw_array) # standard deviation if verbose > 0: # histogram and fitting _, bins, _ = plt.hist(x=raw_array, bins=bins, density=True, histtype="step") # histogram if verbose == 2: print(f"\u03bc: {mean}, \u03c3: {std}") p = stats.norm.pdf(bins, mean, std) plt.plot(bins, p, color="crimson", linestyle="-", label="Gaussian Fit") plt.title(f"{param.upper()} Standardized Distribution with Gaussian Fit") plt.axvline(mean + factor * std, color="chocolate", linestyle="-.", label="Max Threshold") plt.axvline(mean - factor * std, color="navy", linestyle="-.", label="Min Threshold") plt.legend(loc="best") plt.xlabel(param) plt.ylabel("counts") plt.show() # only retain data within thresholds data = data[(data[param] <= mean + factor * std) & (data[param] >= mean - factor * std)] return data
nilq/baby-python
python
""" Module for version router Contains functions and api endpoints for version listing """ import sys from operator import attrgetter from typing import List from fastapi import APIRouter from tracktor.error import ItemNotFoundException from tracktor.models import VersionModel router = APIRouter(prefix="/versions", tags=["version"]) def _get_versions() -> List[VersionModel]: return [ VersionModel( version=x, changelog=attrgetter(f"{x}.__CHANGELOG__")(sys.modules["tracktor.routers"]), ) for x in dir(sys.modules[__package__]) if x.startswith("v") and x != "version" and x != "version_router" ] @router.get("/", response_model=List[VersionModel]) async def list_versions(): """ Request to list all versions """ return _get_versions() @router.get("/latest") async def latest_version(): """ Request to return the latest version """ try: return sorted([x.version for x in _get_versions()])[0] except IndexError as err: raise ItemNotFoundException from err
nilq/baby-python
python
import argon2 import b64 from typing import Optional, Dict, Union, List from securerandom import rand_bytes from kdfs.kdf import Kdf class Argon2Kdf(Kdf): @staticmethod def sensitive(): return Argon2Kdf(12, 2 * 1024 * 1024, 8, argon2.Type.ID, rand_bytes(32)) @staticmethod def fast(): return Argon2Kdf(2, 256 * 1024, 2, argon2.Type.ID, rand_bytes(16)) @staticmethod def type_to_str(type: argon2.Type): return { argon2.Type.I: "argon2i", argon2.Type.D: "argon2d", argon2.Type.ID: "argon2id" }[type] @staticmethod def str_to_type(type: str): try: return { "argon2i": argon2.Type.I, "argon2d": argon2.Type.D, "argon2id": argon2.Type.ID }[type] except KeyError: raise ValueError(f"Type must be one of ['argon2i', 'argon2d', 'argon2id'] (was '{type}')") @staticmethod def __type_to_int(type: argon2.Type): return { argon2.Type.I: 0, argon2.Type.D: 1, argon2.Type.ID: 2 }[type] @staticmethod def __int_to_type(type: int): try: return { 0: argon2.Type.I, 1: argon2.Type.D, 2: argon2.Type.ID }[type] except KeyError: raise ValueError(f"The Argon2 type must be 0x0, 0x1, or 0x2 (was {hex(type)})") @staticmethod def deserialize(props: Dict[str, Union[str, int, bool, None, Dict, List]]) -> "Argon2Kdf": ret = Argon2Kdf.sensitive() base_keys = set(ret.serialize().keys()) if not base_keys.issubset(props.keys()): raise ValueError(f"The properties dict is missing required keys {base_keys - props.keys()}") ret.type = Argon2Kdf.str_to_type(props["algorithm"]) ret.version = props["version"] ret.time_cost = props["time_cost"] ret.memory_cost = props["memory_cost"] ret.parallelism = props["parallelism"] ret.salt = b64.decode(props["salt"]) return ret def serialize(self) -> Dict[str, Union[str, int, bool, None, Dict, List]]: return { "algorithm": self.type_to_str(self.type), "version": self.version, "time_cost": self.time_cost, "memory_cost": self.memory_cost, "parallelism": self.parallelism, "salt": b64.encode(self.salt), } def derive(self, password: str, out_len: int) -> bytes: return argon2.low_level.hash_secret_raw(bytes(password, "utf-8"), self.salt, self.time_cost, self.memory_cost, self.parallelism, out_len, self.type, self.version) def __init__(self, time_cost: int, memory_cost: int, parallelism: int, type: argon2.Type, salt: Optional[bytes] = None, version: int = argon2.low_level.ARGON2_VERSION): self.time_cost = time_cost self.memory_cost = memory_cost self.parallelism = parallelism self.type = type self.salt = salt if salt is not None else rand_bytes(32) self.version = version
nilq/baby-python
python
#!/usr/bin/env python from __future__ import print_function import subprocess as sp import argparse import os import re def get_paths(root, extension): # scan directories paths = [] for root, dirs, files in os.walk(root): for name in files: if name.endswith(extension): paths.append(os.path.join(root, name)) paths.sort() return paths def print_title(title): print('# '+'-'*77+'\n'+'# '+title+'\n'+'# '+'-'*77) def dependencies(root, src_ext = '.cc'): print_title('find dependencies') # get paths paths = get_paths(root, src_ext) total_deps = 0 total_size = 0 for path in paths: # run gcc and get dependeny data command = 'g++ {0} -I{1} -MM'.format(path, root) output = sp.Popen([command], shell=True, stdout=sp.PIPE).communicate()[0] deps = len(output.replace('\\', '').split())-1 size = os.path.getsize(path) # code analysis print('{0:<60}: {1:6d}b {2:4d} deps'.format(path, size, deps)) total_deps += deps total_size += size print('Total {0:48d} files: {1:6d}b {2:4d} deps'.format(len(paths), total_size, total_deps)) def trailing_whitespaces(root, extensions): print_title('check for trailing whitespaces') paths = [] for ext in extensions: paths += get_paths(root, ext) paths.sort() num = 0 for path in paths: ifile = open(path, 'r') lines = ifile.readlines() ifile.close() for linenum, line in enumerate(lines): line = line[:-1] # strip newline if line and line[-1].isspace(): print('{0:<60}\tline {1:3d}'.format(path, linenum+1)) num += 1 print('Total trailing whitespaces: {0:5d}'.format(num)) def get_functions(path): class_name = "" with open(path, 'r') as f: m = re.search('class\s+(\w+)\s*[{|:]', f.read()) if m != None: class_name = m.group(1) print('file: {0}: class {1}'.format(path, class_name)) else: print('file: {0}: could not find class name'.format(path)) # run ctags to get functions/prototype names command = 'ctags -x --c++-kinds=pf --language-force=c++ --sort=no {0}'.format(path) output = sp.Popen([command], shell=True, stdout=sp.PIPE).communicate()[0] lines = [i for i in output.split('\n') if i] functions = [] for i in lines: m = re.search(r'(.*)\s+(function|prototype)\s+([0-9]+)\s+(.*?)\s+(.*)', i) if m == None: print('error while reading regex on ' + i) functions.append(m.groups()) return class_name, functions def lowercase_names(root, extensions): print_title('check if function names starts with lowercase') paths = [] # for given extensions, e.g. ['.cc', '.h'] for ext in extensions: paths += get_paths(root, ext) paths.sort() lower = 0 total = 0 for path in paths: class_name, functions = get_functions(path) total += len(functions) for function in functions: name = function[0] if name.find('operator') >= 0: total -= 1 elif name.find('get_') >= 0: total -= 1 elif name.find('set_') >= 0: total -= 1 elif name[0].islower(): lower += 1 print('\t'+name) perc = float(lower)/float(total)*100. print('Functions : {0:6d}\nLowercase : {1:6d}\nPercentage : {2:6.2f}%'.format(total, lower, perc)) def grep(root, extensions, pattern): print_title('grep pattern') paths = [] for ext in extensions: paths += get_paths(root, ext) paths.sort() total = 0 for path in paths: with open(path, 'r') as f: num = 0 print_path = False for line in f.readlines(): m = re.search(pattern, line) if m != None: total += 1 if not print_path: print('{0}'.format(path)) print_path = True print(' {0}'.format(line.strip())) print('Found {0:5d} occurencies of {1}'.format(total, pattern)) def self_contained_headers(root, extensions, flags): print_title('check if header files are self contained') paths = [] for ext in extensions: paths += get_paths(root, ext) paths.sort() num = 0 for path in paths: num += 1 f = open('tmp.cc', 'w') f.write('#include \"{0}\"\n'.format(path)) f.close() print('checking: {0}'.format(path)) command = 'g++ {0} -c tmp.cc'.format(flags) output = sp.Popen([command], shell=True, stdout=sp.PIPE).communicate()[0] os.remove('tmp.cc') os.remove('tmp.o') def unit_tests(root): print_title('create unit tests') cc_paths = get_paths(root, '.cc') h_paths = get_paths(root, '.h') num = 0 for header_file in h_paths: source_file = os.path.splitext(header_file)[0]+'.cc' test_file = os.path.splitext(header_file)[0]+'_test.cc' print(header_file) if test_file in cc_paths: print('\t{0:<64} exists'.format(test_file)) continue if source_file.endswith('_test.cc'): continue num +=1 class_name, functions = get_functions(header_file) s = "// automatic generated unit test file \n" s += '#include <gtest/gtest.h>\n\n' s += 'class {0}Test: public testing::Test {{\n'.format(class_name) s += ' protected:\n' # s += '\t{0} {1}();\n'.format(class_name,class_name.lower()) s += '\tvirtual void SetUp() {\n\t}\n' s += '\tvirtual void TearDown() {\n\t}\n' s += '};\n\n' for function in functions: name, kind, line_number, file_name, first_line = function s += '// test: {0}\n'.format(first_line) s += 'TEST_F({0}Test, {1}) {{\n}}\n\n'.format(class_name, name.strip()) ofile = open('msc/tests/'+os.path.split(test_file)[1], 'w') ofile.write(s) ofile.close() print('Remaining tests : {0}'.format(num)) if __name__ == '__main__': flags = '-Isrc -I/usr/include -I/usr/include/python2.6 -I/opt/acml4.4.0/gfortran32_mp/include -Wno-deprecated' parser = argparse.ArgumentParser() parser.add_argument('-d', '--deps', action = 'store_true', dest = 'deps', help = 'grep pattern') parser.add_argument('--lowercase', action = 'store_true', dest = 'fn', help = 'functions lowercase') parser.add_argument('-w', '--whitespaces', action = 'store_true', dest = 'w', help = 'trailing whitespaces') parser.add_argument('-g', '--grep', default = None, metavar = 'pattern', dest = 'greppat', help = 'grep pattern') parser.add_argument('-s', '--self-cont', action = 'store_true', dest = 'sc', help = 'self contained headers') parser.add_argument('-c', '--cxx-ext', default = '.cc', metavar = 'src_ext', dest = 'src_ext', help = 'source file extension') parser.add_argument('-i', '--inc-ext', default = '.h', metavar = 'inc_ext', dest = 'inc_ext', help = 'header file extension') parser.add_argument('-r', '--root', default = 'src', metavar = 'src', dest = 'src', help = 'root directory') parser.add_argument('-f', '--flags', default = flags, metavar = 'flags', dest = 'flags', help = 'compilation flags') parser.add_argument('--unit-tests', action = 'store_true', dest = 'unit_tests', help = 'create unit tests') args = parser.parse_args() src = args.src src_ext = args.src_ext inc_ext = args.inc_ext if args.deps: dependencies(args.src) if args.w: trailing_whitespaces(args.src, [src_ext, inc_ext]) if args.fn: lowercase_names(args.src, [inc_ext]) if args.greppat != None: grep(args.src, [src_ext, inc_ext], args.greppat) if args.sc: self_contained_headers(args.src, [inc_ext], args.flags) if args.unit_tests: unit_tests(args.src)
nilq/baby-python
python
from class_names import CLASS_NAMES import json import numpy as np import scipy import re FEATURES = [ 'calls_to_batchCancelOrders', 'calls_to_batchFillOrKillOrders', 'calls_to_batchFillOrders', 'calls_to_batchFillOrdersNoThrow', 'calls_to_cancelOrder', 'calls_to_cancelOrdersUpTo', # 'calls_to_cancelled', # 'calls_to_executeTransaction', 'calls_to_fillOrKillOrder', 'calls_to_fillOrder', 'calls_to_fillOrderNoThrow', # 'calls_to_filled', # 'calls_to_getAssetProxy', # 'calls_to_getOrderInfo', # 'calls_to_isValidSignature', 'calls_to_marketBuyOrders', 'calls_to_marketBuyOrdersNoThrow', 'calls_to_marketBuyOrdersWithEth', 'calls_to_marketSellOrders', 'calls_to_marketSellOrdersNoThrow', 'calls_to_marketSellOrdersWithEth', 'calls_to_matchOrders', # 'calls_to_orderEpoch', 'calls_to_preSign', # 'calls_to_registerAssetProxy', # 'calls_to_transferOwnership', 'calls_to_tx_batchFillOrders', 'calls_to_tx_cancelOrder', 'calls_to_tx_fillOrKillOrder', 'calls_to_tx_fillOrder', # 'max_calls', # 'total_calls', # 'total_fills', # 'total_orders', # 'unique_fee_recipients', # 'unique_makers', 'unique_senders' ] def softsign(x): return x / (1 + abs(x)) def parse_cluster_data_item(data): total_method_calls = sum(data['methods'].values()) max_method_calls = max(data['methods'].values()) total_senders = len([ a for a in data['senders'] if a != data['caller'] ]) total_fee_recipients = len(data['feeRecipients']) total_makers = len(data['makers']) return { 'caller': data['caller'], 'unique_senders': total_senders, 'unique_fee_recipients': total_fee_recipients, 'unique_makers': total_makers, 'total_calls': total_method_calls, 'total_orders': data['updateCount'], 'total_fills': data['fillCount'], 'max_calls': max_method_calls, # Calls are encoded as proportions of the total calls. **{ 'calls_to_%s' % k: v / total_method_calls for (k, v) in data['methods'].items() }, } def find_features(call_data): features = set() for data_item in call_data: features.update(data_item.keys()) return tuple(sorted(features)) def to_features(data_item): fields = { **data_item, 'unique_senders': softsign(data_item['unique_senders']), 'unique_fee_recipients': softsign(data_item['unique_fee_recipients']), 'unique_makers': softsign(data_item['unique_makers']), } return np.array([ fields[feature] if feature in fields else 0. for feature in FEATURES ]) def to_weight(data_item): return max(1, data_item['total_orders'], data_item['unique_senders']) def load_cluster_data(file): with open(file) as f: return [ parse_cluster_data_item(json.loads(line)) for line in f ] def label_to_classs_name(label): name = CLASS_NAMES[label % len(CLASS_NAMES)] if label >= 0 else 'WILDLINGS' if label >= len(CLASS_NAMES): name = '%s_%d' % (name, label // len(CLASS_NAMES)) return name def split_by_labels(call_data, labels, numeric=False): return { label if numeric else label_to_classs_name(label) : [ d for (d, dl) in zip(call_data, labels) if dl == label ] for label in labels } # Attenuates feature columns by normal distribution. def attenuate_values(values, factor=1): mean = np.mean(values) std = np.std(values) if std > 0: return [ n * ((1 - factor) + factor * scipy.stats.norm.pdf((n - mean) / std)) for n in values ] return values # Intelligently collapse a cluster's features into a single row. def collapse_features(cluster, weights, attenuate=0, brighten=0): cols = list(zip(*cluster)) if attenuate > 0: for i, col in enumerate(cols): cols[i] = attenuate_values(col, attenuate) call_features = frozenset(f for f in FEATURES if f.startswith('calls_to_')) calls_sum_max = sum( np.max(np.array(col) * weights) for (col, f) in zip(cols, FEATURES) if f in call_features ) def collapse_column(col, feature): if feature == 'unique_senders': return max(col) if feature == 'total_orders': return sum(col) if feature == 'total_fills': return sum(col) if feature.startswith('calls_to_'): if np.max(col) > 0: v = np.max([ n * w for (n, w) in zip(col, weights) ]) v = (v / calls_sum_max) ** (1 - brighten) return v return 0 return sum([ n * w for (n, w) in zip(col, weights) ]) / sum(weights) return np.array([ collapse_column(col, feature) for (col, feature) in zip(cols, FEATURES) ]) # Intelligently collapse all clusters. def collapse_clusters(call_data, labels, attenuate=0, brighten=0): return np.array([ collapse_features( [ to_features(row) for (row_label, row) in zip(labels, call_data) if row_label == label ], [ to_weight(row) for (row_label, row) in zip(labels, call_data) if row_label == label ], attenuate=attenuate, brighten=brighten, ) for label in sorted(frozenset(labels)) ])
nilq/baby-python
python
import argparse import base64 from googleapiclient import discovery from oauth2client.client import GoogleCredentials def get_args(): """ Collect command-line arguments and return the argument values :return: A set of three values: image file name (and path), the operation mode, and the max number of results """ parser = argparse.ArgumentParser(description='Call the Google Vision API to perform image analysis') # Add arguments parser.add_argument('-i', '--image', type=str, help='image file name', required=True) parser.add_argument('-m', '--mode', type=str, help='analysis mode: all, faces, landmark, labels, logos, text, or web', required=True) parser.add_argument('-r', '--results', type=int, help='max number of results (default is 5)', default=5) # Array for all arguments passed to script args = parser.parse_args() return args.image, args.mode, args.results def request_labels(photo_file, max_results=5): """ Request the Google service to analyze an image and return labels (i.e. tags identifying objects in an image) :param photo_file: The filename (or path) of the image in a local directory :param max_results: The requested maximum number of results :return: A list of tuples where each tuple includes a label and a confidence score. The list contains up to max_results number of elements """ credentials = GoogleCredentials.get_application_default() service = discovery.build('vision', 'v1', credentials=credentials) with open(photo_file, 'rb') as phf: image_content = base64.b64encode(phf.read()) service_request = service.images().annotate(body={ 'requests': [{'image': {'content': image_content.decode('UTF-8')}, 'features': [{'type': 'LABEL_DETECTION', 'maxResults': max_results}] }] }) response = service_request.execute() try: label_list = response['responses'][0]['labelAnnotations'] labels = map(lambda s: (s.get('description', 'no_description'), s.get('score', 'no_score')), label_list) return labels except KeyError: return [] def request_text(photo_file, max_results=5): """ Request the Google service to find text in an image :param photo_file: The filename (or path) of the image in a local directory :param max_results: The requested maximum number of results :return: A list of text entries found in the image Note: The argument max_results does not modify the number of results for text detection """ credentials = GoogleCredentials.get_application_default() service = discovery.build('vision', 'v1', credentials=credentials) with open(photo_file, 'rb') as phf: image_content = base64.b64encode(phf.read()) service_request = service.images().annotate(body={ 'requests': [{'image': {'content': image_content.decode('UTF-8')}, 'features': [{'type': 'TEXT_DETECTION', 'maxResults': max_results}] }] }) response = service_request.execute() text_list = response['responses'][0].get('textAnnotations', None) if text_list is None: return [] else: text_vec = map(lambda s: s['description'].strip().strip('\n'), text_list) return text_vec def request_faces(photo_file, max_results=5): """ Request the Google service to find faces in an image :param photo_file: The filename (or path) of the image in a local directory :param max_results: The requested maximum number of results :return: A list of JSON objects where each object describes a face. The JSON object includes the following elements: box: A list of four tuples describing the box coordinates for a face in the picture score: A confidence score for face detection joy: One of the variables in sentiment analysis of the face sorrow: One of the variables in sentiment analysis of the face anger: One of the variables in sentiment analysis of the face surprise: One of the variables in sentiment analysis of the face """ credentials = GoogleCredentials.get_application_default() service = discovery.build('vision', 'v1', credentials=credentials) with open(photo_file, 'rb') as phf: image_content = base64.b64encode(phf.read()) service_request = service.images().annotate(body={ 'requests': [{'image': {'content': image_content.decode('UTF-8')}, 'features': [{'type': 'FACE_DETECTION', 'maxResults': max_results}] }] }) response = service_request.execute() faces_list = response['responses'][0].get('faceAnnotations', None) if faces_list is None: return [] else: face_features = [] for face in faces_list: score = face["detectionConfidence"] joy = face["joyLikelihood"] sorrow = face["sorrowLikelihood"] surprise = face["surpriseLikelihood"] anger = face["angerLikelihood"] vertices = face["boundingPoly"]["vertices"] vert_list = map(lambda el: (el['x'], el['y']), vertices) face_obj = {'score': score, 'joy': joy, 'sorrow': sorrow, 'surprise': surprise, 'anger': anger, 'box': vert_list} face_features.append(face_obj) return face_features def request_logos(photo_file, max_results=5): """ Request the Google service to detect the presence of logos in an image :param photo_file: The filename (or path) of the image in a local directory :param max_results: The requested maximum number of results :return: A list of tuples where each tuple has text identifying the detected logo and a confidence score """ credentials = GoogleCredentials.get_application_default() service = discovery.build('vision', 'v1', credentials=credentials) with open(photo_file, 'rb') as phf: image_content = base64.b64encode(phf.read()) service_request = service.images().annotate(body={ 'requests': [{'image': {'content': image_content.decode('UTF-8')}, 'features': [{'type': 'LOGO_DETECTION', 'maxResults': max_results}] }] }) response = service_request.execute() logo_list = response['responses'][0].get('logoAnnotations', None) if logo_list is None: return [] else: logo_features = map(lambda s: (s.get("description", "no_description"), s.get("score", 'no_score')), logo_list) return logo_features def request_landmarks(photo_file, max_results=5): """ Request the Google service to detect the presence of landmarks in an image :param photo_file: The filename (or path) of the image in a local directory :param max_results: The requested maximum number of results :return: A list of tuples where each tuple has text identifying the detected landmark and a confidence score """ credentials = GoogleCredentials.get_application_default() service = discovery.build('vision', 'v1', credentials=credentials) with open(photo_file, 'rb') as phf: image_content = base64.b64encode(phf.read()) service_request = service.images().annotate(body={ 'requests': [{'image': {'content': image_content.decode('UTF-8')}, 'features': [{'type': 'LANDMARK_DETECTION', 'maxResults': max_results}] }] }) response = service_request.execute() landmark_list = response['responses'][0].get('landmarkAnnotations', None) if landmark_list is None: return [] else: landmarks = map(lambda s: (s.get("description", 'no_description'), s.get("score", 'no_score')), landmark_list) return landmarks def request_web_entities(photo_file, max_results=5): """ Request the Google service to detect related web entities and web pages :param photo_file: The filename (or path) of the image in a local directory :param max_results: The requested maximum number of results :return: This function returns two lists. The first is a list of web entities. The second is a list of related web pages. Each list is a collection of tuples. For web entities, each tuple includes the entity description, the entity mid, and the entity score. For web pages, each tuple includes the page url and the score. """ credentials = GoogleCredentials.get_application_default() service = discovery.build('vision', 'v1', credentials=credentials) with open(photo_file, 'rb') as phf: image_content = base64.b64encode(phf.read()) service_request = service.images().annotate(body={ 'requests': [{'image': {'content': image_content.decode('UTF-8')}, 'features': [{'type': 'WEB_DETECTION', 'maxResults': max_results}] }] }) response = service_request.execute() web_detection = response['responses'][0].get('webDetection', None) if web_detection is None: return [], [] else: web_entities = web_detection.get('webEntities', None) web_pages = web_detection.get('pagesWithMatchingImages', None) # Add s.get('entityId', 'no_entityId') in order to see the mid for these entities if web_entities is not None: entities = map(lambda s: (s.get('description', 'no_description'), s.get('score', 'no_score')), web_entities) else: entities = [] if web_pages is not None: pages = map(lambda s: (s.get('url', 'no_url'), s.get('score', 'no_score')), web_pages) else: pages = [] return entities, pages if __name__ == "__main__": # Get image file name and operation mode image, mode, mxres = get_args() if mode in ['labels', 'all']: results = request_labels(image, max_results=mxres) print "\n-------- labels -----------------" print "Number of labels: ", len(results) for i, res in enumerate(results): print str(i+1) + ") " + str(res) if mode in ['text', 'all']: results = request_text(image, max_results=mxres) print "\n-------- text -----------------" print "Number of text items: ", len(results) for i, res in enumerate(results): print str(i+1) + ") " + str(res) if mode in ['faces', 'all']: results = request_faces(image, max_results=mxres) print "\n--------- faces ---------------" print "Number of faces: ", len(results) for i, res in enumerate(results): print "\nface " + str(i+1) + ": " print res if mode in ['logos', 'all']: results = request_logos(image, max_results=mxres) print "\n--------- logos ---------------" print "Number of logos: ", len(results) for i, res in enumerate(results): print str(i+1) + ") " + str(res) if mode in ['landmarks', 'all']: results = request_landmarks(image, max_results=mxres) print "\n--------- landmarks ---------------" print "Number of landmarks: ", len(results) for i, res in enumerate(results): print str(i+1) + ") " + str(res) if mode in ['web', 'all']: results = request_web_entities(image, max_results=mxres) print "\n--------- web entities and pages ---------------" print 'Number of web entities: ', len(results[0]) for i, res in enumerate(results[0]): print str(i+1) + ") " + str(res) print "\nNumber of web pages: ", len(results[1]) for i, res in enumerate(results[1]): print str(i+1) + ") " + str(res)
nilq/baby-python
python
"""Data is typically multi-dimensional. :class:`~smif.metadata.spec.Spec` is used to describe each dataset which is supplied to - or output from - each :class:`~smif.model.model.Model` in a :class:`~smif.model.model.CompositeModel` """ from collections import defaultdict from smif.metadata.coordinates import Coordinates class Spec(object): """N-dimensional metadata. Spec labels each dimension with coordinates and enforces data type, units and absolute and expected ranges. The API here is modelled on :class:`xarray.DataArray`: dtype and shape describe a :class:`numpy.ndarray`; dims and coords follow the xarray conventions for labelled axes; and unit, abs_range and exp_range are introduced as supplementary metadata to help validate connections between models. Attributes ---------- name : str The name of the data that this spec describes description : str A human-friendly description dtype : str Data type for data values abs_range : tuple Absolute range of data values exp_range : tuple Expected range of data values shape : tuple[int] Tuple of dimension sizes ndim : int Number of dimensions dims : list[str] Dimension names coords : list[Coordinates] Dimension coordinate labels unit : str Units of data values Parameters ---------- name : str, optional Name to identifiy the variable described (typically an input, output or parameter) description : str, optional Short description dims : list[str], optional List of dimension names, must be provided if coords is a dict coords : list[Coordinates] or dict[str, list], optional A list of :class`Coordinates` or a dict mapping each dimension name to a list of names which label that dimension. dtype : str String suitable for contructing a simple :class:`numpy.dtype` abs_range : tuple, optional (min, max) absolute range for numeric values - can be used to raise errors exp_range : tuple, optional (min, max) expected range for numeric values - can be used to raise warnings unit : str, optional Unit to be used for data values """ def __init__(self, name=None, dims=None, coords=None, dtype=None, abs_range=None, exp_range=None, unit=None, description=None): self._name = name self._description = description # Coords may come as a dict, in which case dims must be provided to define order if isinstance(coords, dict): try: coords, dims = self._coords_from_dict(coords, dims) except (ValueError, KeyError) as error: msg = "Coordinate metadata incorrectly formatted for variable '{}': {}" raise ValueError(msg.format(self.name, error)) # Or as a list of Coordinates, in which case dims must not be provided elif isinstance(coords, list): coords, dims = self._coords_from_list(coords, dims) # Or if None, this spec describes a zero-dimensional parameter - single value else: coords, dims = [], [] self._dims = dims self._coords = coords if dtype is None: raise ValueError("Spec.dtype must be provided, in {}".format(self._name)) self._dtype = dtype if abs_range is not None: self._check_range(abs_range) self._abs_range = abs_range if exp_range is not None: self._check_range(exp_range) self._exp_range = exp_range self._unit = unit def _coords_from_list(self, coords, dims): """Set up coords and dims, checking for consistency """ for coord in coords: if not isinstance(coord, Coordinates): msg = "Spec.coords may be a dict[str,list] or a list[Coordinates], in {}" raise ValueError(msg.format(self._name)) if dims is not None: msg = "Spec.dims are derived from Spec.coords if provided as a list of " + \ "Coordinates, in {}" raise ValueError(msg.format(self._name)) dims = [coord.dim for coord in coords] if len(dims) != len(set(dims)): msg = "Spec cannot be created with duplicate dims, in {}" raise ValueError(msg.format(self._name)) return coords, dims def _coords_from_dict(self, coords, dims): """Set up coords and dims, checking for consistency """ if dims is None: msg = "Spec.dims must be specified if coords are provided as a dict, in {}" raise ValueError(msg.format(self._name)) if len(dims) != len(set(dims)): msg = "Spec cannot be created with duplicate dims, in {}" raise ValueError(msg.format(self._name)) if sorted(dims) != sorted(coords.keys()): msg = "Spec.dims must match the keys in coords, in {}" raise ValueError(msg.format(self._name)) coords = [Coordinates(dim, coords[dim]) for dim in dims] return coords, dims @classmethod def from_dict(cls, data_provided): """Create a Spec from a dict representation """ # default anything to None, let constructor handle essential missing values data = defaultdict(lambda: None) data.update(data_provided) spec = Spec( name=data['name'], description=data['description'], dims=data['dims'], coords=data['coords'], dtype=data['dtype'], abs_range=data['abs_range'], exp_range=data['exp_range'], unit=data['unit'] ) return spec def as_dict(self): """Serialise to dict representation """ return { 'name': self.name, 'description': self.description, 'dims': self._dims, 'coords': {c.name: c.ids for c in self._coords}, 'dtype': self._dtype, 'abs_range': self._abs_range, 'exp_range': self._exp_range, 'unit': self._unit } @property def name(self): """The name of the data that this spec describes. """ return self._name @name.setter def name(self, value): self._name = value @property def description(self): """A human-friendly description """ return self._description @property def dtype(self): """The dtype of the data that this spec describes. """ return self._dtype @property def abs_range(self): """The absolute range of data values that this spec describes. """ return self._abs_range @property def exp_range(self): """The expected range of data values that this spec describes. """ return self._exp_range @property def shape(self): """Tuple of dimension sizes. The shape of the data that this spec describes. """ return tuple(len(c.ids) for c in self._coords) @property def ndim(self): """The number of dimensions of the data that this spec describes. """ return len(self._coords) @property def dims(self): """Names for each dimension """ return list(self._dims) @property def coords(self): """Coordinate labels for each dimension. """ return list(self._coords) def dim_coords(self, dim: str): """Coordinates for a given dimension """ if not isinstance(dim, str): msg = "Expected string as argument, instead received {}" raise TypeError(msg.format(type(dim))) if dim not in self.dims: raise KeyError("Could not find dim '{}' in Spec '{}'".format(dim, self._name)) for coord in self._coords: if coord.dim == dim: return coord raise KeyError("Coords not found for dim '{}', in Spec '{}'".format(dim, self._name)) def dim_names(self, dim: str): """Names of each coordinate in a given dimension """ return self.dim_coords(dim).names def dim_elements(self, dim: str): """Elements of each coordinate in a given dimension """ return self.dim_coords(dim).elements @property def unit(self): """The unit for all data points. """ return self._unit def __eq__(self, other): return self.dtype == other.dtype \ and self.dims == other.dims \ and self.coords == other.coords \ and self.unit == other.unit def __hash__(self): return hash(( self.dtype, tuple(self.dims), tuple(self.coords), self.unit )) def __repr__(self): return "<Spec name='{}' dims='{}' unit='{}'>".format(self.name, self.dims, self.unit) def _check_range(self, range_): """Error if range is not a [min, max] list or tuple """ if not _is_sequence(range_): msg = "Spec range must be a list or tuple, got {} for {}" raise TypeError(msg.format(range_, self._name)) if len(range_) != 2: msg = "Spec range must have min and max values only, got {} for {}" raise ValueError(msg.format(range_, self._name)) min_, max_ = range_ if max_ < min_: msg = "Spec range min value must be smaller than max value, got {} for {}" raise ValueError(msg.format(range_, self._name)) def _is_sequence(obj): """Check for iterable object that is not a string ('strip' is a method on str) """ return not hasattr(obj, "strip") \ and (hasattr(obj, "__getitem__") or hasattr(obj, "__iter__"))
nilq/baby-python
python
#!/usr/bin/env python # Copyright 2021 Google Inc. All Rights Reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. """Google Analytics Data API sample application demonstrating the use of pagination to retrieve large result sets. See https://developers.google.com/analytics/devguides/reporting/data/v1/rest/v1beta/properties/runReport#body.request_body.FIELDS.offset for more information. """ # [START analyticsdata_run_report_with_pagination] from google.analytics.data_v1beta import BetaAnalyticsDataClient from google.analytics.data_v1beta.types import DateRange from google.analytics.data_v1beta.types import Dimension from google.analytics.data_v1beta.types import Metric from google.analytics.data_v1beta.types import RunReportRequest from run_report import print_run_report_response def run_sample(): """Runs the sample.""" # TODO(developer): Replace this variable with your Google Analytics 4 # property ID before running the sample. property_id = "YOUR-GA4-PROPERTY-ID" run_report_with_pagination(property_id) def run_report_with_pagination(property_id="YOUR-GA4-PROPERTY-ID"): """Runs a report several times, each time retrieving a portion of result using pagination.""" client = BetaAnalyticsDataClient() # [START analyticsdata_run_report_with_pagination_page1] request = RunReportRequest( property=f"properties/{property_id}", date_ranges=[DateRange(start_date="365daysAgo", end_date="yesterday")], dimensions=[ Dimension(name="firstUserSource"), Dimension(name="firstUserMedium"), Dimension(name="firstUserCampaignName"), ], metrics=[ Metric(name="sessions"), Metric(name="conversions"), Metric(name="totalRevenue"), ], limit=100000, offset=0, ) response = client.run_report(request) # [END analyticsdata_run_report_with_pagination_page1] print_run_report_response(response) # Run the same report with a different offset value to retrieve the second # page of a response. # [START analyticsdata_run_report_with_pagination_page2] request = RunReportRequest( property=f"properties/{property_id}", date_ranges=[DateRange(start_date="365daysAgo", end_date="yesterday")], dimensions=[ Dimension(name="firstUserSource"), Dimension(name="firstUserMedium"), Dimension(name="firstUserCampaignName"), ], metrics=[ Metric(name="sessions"), Metric(name="conversions"), Metric(name="totalRevenue"), ], limit=100000, offset=100000, ) response = client.run_report(request) # [END analyticsdata_run_report_with_pagination_page2] print_run_report_response(response) # [END analyticsdata_run_report_with_pagination] if __name__ == "__main__": run_sample()
nilq/baby-python
python
""" Transport for communicating with the LaunchKey API of HTTP""" import requests from .. import LAUNCHKEY_PRODUCTION from .base import APIResponse, APIErrorResponse class RequestsTransport(object): """ Transport class for performing HTTP based queries using the requests library. """ url = LAUNCHKEY_PRODUCTION testing = False verify_ssl = True def set_url(self, url, testing): """ :param url: Base url for the querying LaunchKey API :param testing: Boolean stating whether testing mode is being performed. This will determine whether SSL should be verified. """ self.url = url self.testing = testing self.verify_ssl = not self.testing @staticmethod def _parse_response(response): try: data = response.json() except ValueError: data = response.text try: response.raise_for_status() parsed_response = APIResponse(data, response.headers, response.status_code, raw_data=response.text) except requests.exceptions.HTTPError: if response.status_code < 500: parsed_response = APIErrorResponse(data, response.headers, response.status_code, response.reason, response.text) else: raise return parsed_response def get(self, path, headers=None, data=None): """ Performs an HTTP GET request against the LaunchKey API :param path: Path or endpoint that will be hit :param headers: Headers to add onto the request :param data: Dictionary or bytes to be sent in the query string for the request. :return: """ response = requests.get(self.url + path, params=data, headers=headers, verify=self.verify_ssl) return self._parse_response(response) def post(self, path, headers=None, data=None): """ Performs and HTTP POST request against the LaunchKey API :param path: Path or endpoint that will be hit :param headers: Headers to add onto the request :param data: Dictionary, bytes, or file-like object to send in the body of the request. :return: """ response = requests.post(self.url + path, data=data, headers=headers, verify=self.verify_ssl) return self._parse_response(response) def put(self, path, headers=None, data=None): """ Performs and HTTP PUT request against the LaunchKey API :param path: Path or endpoint that will be hit :param headers: Headers to add onto the request :param data: Dictionary, bytes, or file-like object to send in the body of the request. :return: """ response = requests.put(self.url + path, data=data, headers=headers, verify=self.verify_ssl) return self._parse_response(response) def delete(self, path, headers=None, data=None): """ Performs and HTTP DELETE request against the LaunchKey API :param path: Path or endpoint that will be hit :param headers: Headers to add onto the request :param data: Dictionary, bytes, or file-like object to send in the body of the request. :return: """ response = requests.delete(self.url + path, data=data, headers=headers, verify=self.verify_ssl) return self._parse_response(response) def patch(self, path, headers=None, data=None): """ Performs and HTTP PATCH request against the LaunchKey API :param path: Path or endpoint that will be hit :param headers: Headers to add onto the request :param data: Dictionary, bytes, or file-like object to send in the body of the request. :return: """ response = requests.patch(self.url + path, data=data, headers=headers, verify=self.verify_ssl) return self._parse_response(response)
nilq/baby-python
python
# Copyright (c) IOTIC LABS LIMITED. All rights reserved. Licensed under the Apache License, Version 2.0. from typing import Callable import base58 from iotics.lib.identity.api.advanced_api import AdvancedIdentityLocalApi from iotics.lib.identity.crypto.identity import make_identifier from iotics.lib.identity.crypto.issuer import Issuer from iotics.lib.identity.crypto.key_pair_secrets import DIDType, KeyPairSecrets, KeyPairSecretsHelper from iotics.lib.identity.crypto.proof import Proof from iotics.lib.identity.register.document_builder import RegisterDocumentBuilder from iotics.lib.identity.register.keys import RegisterDelegationProof, RegisterPublicKey def new_seed(length: int = 128) -> bytes: return AdvancedIdentityLocalApi.create_seed(length) def is_validator_run_success(validator: Callable, *args, **kwargs): """ Run a validation helper and ensure it has been run. By design the validators return nothing and they raise when something is invalid. For the valid case we want to highlight the fact the validator has been called returning True at the end of this helper.""" validator(*args, **kwargs) return True def get_delegation_proof(issuer: Issuer, key_pair_secrets: KeyPairSecrets, delegating_doc_id: str) -> Proof: return Proof.build(key_pair_secrets, issuer, content=delegating_doc_id.encode()) def get_delegation_register_proof(subject_key_pair_secrets: KeyPairSecrets, delegating_doc_id: str, subject_issuer: Issuer, deleg_key_name='#DelegKey') -> RegisterDelegationProof: proof = Proof.build(subject_key_pair_secrets, subject_issuer, content=delegating_doc_id.encode()) return RegisterDelegationProof.build(deleg_key_name, controller=subject_issuer, proof=proof.signature, revoked=False) def get_valid_document(seed: bytes, issuer_name: str, controller: Issuer = None): secrets = KeyPairSecrets.build(seed, 'iotics/0/something/twin') return get_valid_document_from_secret(secrets, issuer_name, controller) def get_valid_document_from_secret(secrets: KeyPairSecrets, issuer_name: str, controller: Issuer = None): public_base58 = KeyPairSecretsHelper.get_public_key_base58_from_key_pair_secrets(secrets) public_bytes = base58.b58decode(public_base58) doc_id = make_identifier(public_bytes) proof = Proof.build(secrets, Issuer.build(doc_id, issuer_name), content=doc_id.encode()) return RegisterDocumentBuilder() \ .add_public_key_obj(RegisterPublicKey(issuer_name, public_base58, revoked=False)) \ .build(doc_id, DIDType.TWIN, proof=proof.signature, revoked=False, controller=controller) def get_valid_delegated_doc_and_deleg_proof(seed: bytes, issuer_name: str, delegating_doc_id: str, deleg_name: str): secrets = KeyPairSecrets.build(seed, 'iotics/0/something/twindeleg') public_base58 = KeyPairSecretsHelper.get_public_key_base58_from_key_pair_secrets(secrets) public_bytes = base58.b58decode(public_base58) doc_id = make_identifier(public_bytes) issuer = Issuer.build(doc_id, issuer_name) proof = Proof.build(secrets, issuer, content=doc_id.encode()) deleg_key = get_delegation_register_proof(subject_key_pair_secrets=secrets, delegating_doc_id=delegating_doc_id, subject_issuer=Issuer.build(doc_id, issuer_name), deleg_key_name=deleg_name) delegated_doc = RegisterDocumentBuilder() \ .add_public_key_obj(RegisterPublicKey(issuer_name, public_base58, revoked=False)) \ .build(doc_id, DIDType.TWIN, proof=proof.signature, revoked=False) return delegated_doc, deleg_key
nilq/baby-python
python
import re # machine snapshot platforms LINUX = "LINUX" MACOS = "MACOS" WINDOWS = "WINDOWS" ANDROID = "ANDROID" IOS = "IOS" IPADOS = "IPADOS" TVOS = "TVOS" PLATFORM_CHOICES = ( (LINUX, 'Linux'), (MACOS, 'macOS'), (WINDOWS, 'Windows'), (ANDROID, 'Android'), (IOS, 'iOS'), (IPADOS, 'iPadOS'), (TVOS, 'tvOS'), ) PLATFORM_CHOICES_DICT = dict(PLATFORM_CHOICES) # machine snapshot types DESKTOP = "DESKTOP" LAPTOP = "LAPTOP" MOBILE = "MOBILE" SERVER = "SERVER" TABLET = "TABLET" TV = "TV" VM = "VM" TYPE_CHOICES = ( (DESKTOP, 'Desktop'), (LAPTOP, 'Laptop'), (MOBILE, 'Mobile'), (SERVER, 'Server'), (TABLET, 'Tablet'), (TV, 'TV'), (VM, 'Virtual machine'), ) TYPE_CHOICES_DICT = dict(TYPE_CHOICES) # utils HARDWARE_MODEL_SERIAL_MACHINE_TYPE_DICT = { 'appletv': TV, 'imac': DESKTOP, 'ipad': TABLET, 'iphone': MOBILE, 'macbook': LAPTOP, 'macmini': DESKTOP, 'macpro': DESKTOP, 'powermac': DESKTOP, 'vmware': VM, 'xserve': SERVER, } # source http://www.techrepublic.com/blog/data-center/mac-address-scorecard-for-common-virtual-machine-platforms/ # last check 20161215 KNOWN_VM_MAC_PREFIXES = { '0003FF', # Microsoft Corporation (Hyper-V, Virtual Server, Virtual PC) '005056', '000C29', '000569', # VMware, Inc. (VMware ESX 3, Server, Workstation, Player) '00163E', # Xensource, Inc. '001C42', # Parallels, Inc. '080027', # PCS Systemtechnik GmbH (VirtualBox) } def platform_with_os_name(os_name): if not os_name: return os_name = os_name.lower().replace(" ", "") if "macos" in os_name or "osx" in os_name: return MACOS elif "ios" in os_name: return IOS elif "ipados" in os_name: return IPADOS elif "tvos" in os_name: return TVOS elif "windows" in os_name: return WINDOWS else: for distro in ('centos', 'fedora', 'redhat', 'rehl', 'debian', 'ubuntu', 'gentoo', 'linux'): if distro in os_name: return LINUX def update_ms_tree_platform(tree): os_version_t = tree.get("os_version", {}) os_name = os_version_t.get("name") platform = platform_with_os_name(os_name) if platform: tree["platform"] = platform def update_ms_tree_type(tree): system_info_t = tree.get("system_info", {}) for attr in ("hardware_model", "hardware_serial"): val = system_info_t.get(attr) if val: val = val.lower() for prefix, ms_type in HARDWARE_MODEL_SERIAL_MACHINE_TYPE_DICT.items(): if val.startswith(prefix): tree["type"] = ms_type return network_interfaces = tree.get("network_interfaces") if network_interfaces and \ all(isinstance(ni.get("mac"), str) and ni["mac"].replace(":", "")[:6].upper() in KNOWN_VM_MAC_PREFIXES for ni in network_interfaces): tree["type"] = VM return cpu_brand = system_info_t.get("cpu_brand") if cpu_brand and "xeon" in cpu_brand.lower(): tree["type"] = SERVER def has_deb_packages(machine_snapshot): os_version = machine_snapshot.os_version if not os_version: return False os_name = os_version.name if not os_name: return False os_name = os_name.lower() return "ubuntu" in os_name or "debian" in os_name MACOS_BUILD_RE = re.compile(r"(?P<minor>[0-9]{1,2})(?P<patch_letter>[A-Z])[1-9]+[a-z]?") def macos_version_from_build(build): match = MACOS_BUILD_RE.match(build) if match: patch = ord(match.group("patch_letter")) - 65 minor = int(match.group("minor")) - 4 if minor < 8: # the patch letters are not always consecutive for older versions # probably because of the different architectures. raise ValueError("Cannot parse build str for macos < 10.8") if minor < 12: name = "OS X" else: name = "macOS" if minor >= 16: major = 11 minor = max(0, patch - 1) if build in ("20B29", "20B50", "20D74", "20D75"): patch = 1 elif build in ("20D80",): patch = 2 else: patch = 0 else: major = 10 return { "name": name, "major": major, "minor": minor, "patch": patch, "build": build } else: raise ValueError("Bad build number")
nilq/baby-python
python
from __future__ import absolute_import, print_function, division, unicode_literals import sys import os from subprocess import Popen, PIPE from time import time import numpy as np import pandas as pd from sklearn.model_selection import train_test_split from sklearn import datasets from mlfromscratch.util import ( normalize, logloss, roc_auc, normLL, ) from mlfromscratch.RegressionSGD import RegressionSGD from mlfromscratch.RegressionTree import RegressionTree from mlfromscratch.RandomForest import RandomForest from sklearn.linear_model import SGDClassifier from sklearn.tree import DecisionTreeClassifier from sklearn.ensemble import RandomForestClassifier def create_models(): "Return a list of untrained models" sgd_args = dict(learning_rate=0.02, minibatch=1, l2=0.0001, n_epochs=100, verbose=False, holdout_proportion=0.0, normalize_data=False) tree_args = dict(max_depth=6, min_samples_split=20, min_samples_leaf=10) n_forest_trees = 50 forest_args = dict(max_depth=4, max_features=12, min_samples_split=20, min_samples_leaf=10) # Note on the SGD args: # Minibatch sizes >1 work better, but sklearn's SGD solver doesn't # make it easy to use minibatches. I'm using minibatch=1 just to # make the comparison easy. # Sklearn also has an algo called LogisticRegression, but SGDClassifier # is more directly comparable to mine. return [ ( ("Logistic Regression", "from scratch"), RegressionSGD(loss="logloss", **sgd_args) ), ( ("Logistic Regression", "sklearn"), SGDClassifier(loss="log", penalty="l2", alpha=sgd_args["l2"], learning_rate="constant", eta0=sgd_args["learning_rate"], n_iter=sgd_args["n_epochs"]) ), ( ("Decision Tree", "from scratch"), RegressionTree(loss="logloss", **tree_args) ), ( ("Decision Tree", "sklearn"), DecisionTreeClassifier(criterion="entropy", **tree_args) ), ( ("Random Forest", "from scratch"), RandomForest(loss="logloss", num_trees=n_forest_trees, **forest_args) ), ( ("Random Forest", "sklearn"), RandomForestClassifier(criterion="entropy", n_estimators=n_forest_trees, **forest_args) ), # TODO: add gradient boosting when it's done ] def try_model(model, train_data, test_data, train_targets, test_targets): "Train a model, test it on holdout data, return metrics" start_train = time() try: # sklearn calls the method 'fit', I'm stubborn and call it train model.train(train_data, train_targets) except AttributeError: model.fit(train_data, train_targets) end_train = time() test_pred = model.predict_proba(test_data) # sklearn's output often has 2 columns, but since this is binary # prediction we only need 1. if len(test_pred.shape) == 2: test_pred = test_pred[:,1] end_pred = time() test_ll = logloss(test_targets, test_pred) test_roc = roc_auc(test_targets, test_pred) # for fun, let's look at training error also train_pred = model.predict_proba(train_data) if len(train_pred.shape) == 2: train_pred = train_pred[:,1] train_ll = logloss(train_targets, train_pred) train_roc = roc_auc(train_targets, train_pred) train_time = end_train - start_train pred_time = end_pred - end_train return test_ll, test_roc, train_ll, train_roc, train_time, pred_time def test_models(csv_name): metrics_cols = ["model", "source", "target", "roc_auc", "norm_ll", "train_roc_auc", "train_norm_ll", "train_time", "pred_time"] metrics_data = dict((k,[]) for k in metrics_cols) digits = datasets.load_digits() #has attributes digits.data, digits.target # for each target, run each model 3 times on different datasets for run in range(3): for target_val in range(10): # to see how these models compete on a wider variety of data, # let's get a different train/test split for each run np.random.seed(10 * run + target_val) (train_data, holdout_data, train_targets, holdout_targets ) = train_test_split( digits.data, np.array(digits.target == target_val, dtype=float), test_size=0.25 ) train_mean = np.mean(train_data, axis=0) train_std = np.std(train_data, axis=0) norm_train_data = normalize(train_data, train_mean, train_std) norm_holdout_data = normalize(holdout_data, train_mean, train_std) test_br = np.mean(holdout_targets) train_br = np.mean(train_targets) # create all models fresh, ready to be trained for (mod_name, source), mod in create_models(): ll, roc, train_ll, train_roc, ttime, ptime = try_model( mod, norm_train_data, norm_holdout_data, train_targets, holdout_targets ) metrics_data["model"].append(mod_name) metrics_data["source"].append(source) metrics_data["target"].append(target_val) metrics_data["roc_auc"].append(roc) metrics_data["norm_ll"].append(normLL(ll, test_br)) metrics_data["train_roc_auc"].append(train_roc) metrics_data["train_norm_ll"].append(normLL(train_ll, train_br)) metrics_data["train_time"].append(ttime) metrics_data["pred_time"].append(ptime) df = pd.DataFrame(metrics_data) df.to_csv(csv_name, index=False) print("Wrote {0:d} rows to {1}".format(df.shape[1], csv_name)) if __name__ == "__main__": csv_name = os.path.abspath(sys.argv[1]) if os.path.exists(csv_name): print("{0} exists, will skip re-creating that file".format(csv_name)) else: print("Benchmarking...") test_models(csv_name) dir_path = os.path.dirname(os.path.realpath(__file__)) r_path = os.path.join(dir_path, "compare_models.R") plots_dir = os.path.join(dir_path, "plots/") if not os.path.isdir(plots_dir): os.mkdir(plots_dir) r_cmd = ["Rscript", "--vanilla", r_path, csv_name, plots_dir] print(" ".join(r_cmd)) status = Popen(r_cmd, stderr=PIPE).wait() if status != 0: raise Exception("Status: {0}".format(status)) print("Wrote plots to {}".format(plots_dir))
nilq/baby-python
python
''' Module defining the toolkit in use. Created on Feb 13, 2019 @author: albertgo ''' import os # determine toolkit to be used when loading o molecules TOOLKIT = os.environ.get("CDDLIB_TOOLKIT",None) if not TOOLKIT: # default is openeye but try falling back on rdkit if oe is not available try: import openeye.oechem TOOLKIT = "openeye" except ModuleNotFoundError: TOOLKIT = "rdkit"
nilq/baby-python
python
__all__ = ['HighCardinalityStringIndexer'] import json import logging from typing import Dict, List import pyspark.sql.functions as F from pyspark import SparkContext, keyword_only from pyspark.ml import Estimator, Transformer from pyspark.ml.feature import IndexToString, StringIndexer from pyspark.ml.param import Param, Params, TypeConverters from pyspark.ml.param.shared import HasInputCols from pyspark.sql import Column, DataFrame from python_data_utils.spark.data.params import BaseParams def with_meta(self, alias, meta): """ In pyspark 2.1 there is no simple way to change the metdata of a column, that only became available in pyspark 2.2. This is a function that takes a column and modifies its metadata. :param self: A pyspark column :param alias: :param meta: New meta data for the column """ sc = SparkContext._active_spark_context jmeta = sc._gateway.jvm.org.apache.spark.sql.types.Metadata return Column(getattr(self._jc, "as")(alias, jmeta.fromJson(json.dumps(meta)))) class HighCardinalityStringIndexerModel(Transformer): """ A Transformer that transforms a DataFrame according to the logic obtained by fitting the HighCardinalityStringIndexer """ def __init__(self, dict_indexers: Dict, inputCols: List[str], outputCols: List[str], dropInputCols: bool = False, groupText: str = 'other', returnIndexed: bool = True) -> None: """ :param dict_indexers: A dictionary with each element being another dictionary containing an element 'indexer' with a StringIndexer object and an element 'n_to_keep' that indicates how many indexes to keep. :param inputCols: String columns that need to be indexed :param outputCols: :param dropInputCols: Should the input columns be dropped? :param groupText: String to use as replacement for the observations that need to be grouped. :param returnIndexed: If True, return the indexed columns. If False, return the columns with their String values, where only the grouped observations are changed. """ super().__init__() self.dict_indexers = dict_indexers self.inputCols = inputCols self.outputCols = outputCols self.dropInputCols = dropInputCols self.groupText = groupText self.returnIndexed = returnIndexed @staticmethod def __logger() -> logging.Logger: """ Returns a reference to the logger to be used in this class Storing the logger as an attribute, and then referring to it in functions, can get it on the closure. Resulting in a lock object related to the logger to be included, which isn't serializable. """ return logging.getLogger(__name__) def _transform(self, df) -> DataFrame: """ :param df: A pyspark.sql.dataframe.DataFrame """ # Apply string indexer for in_col, out_col in zip(self.inputCols, self.outputCols): self.__logger().info("Applying StringIndexer on col {}".format(in_col)) df = self.dict_indexers[in_col]['indexer'].transform(df) n_to_keep = self.dict_indexers[in_col]['n_to_keep'] # If elements occur below (threshold * number of rows), replace them with n_to_keep. this_meta = df.select(out_col).schema.fields[0].metadata if n_to_keep != len(this_meta['ml_attr']['vals']): this_meta['ml_attr']['vals'] = this_meta['ml_attr']['vals'][0:(n_to_keep + 1)] this_meta['ml_attr']['vals'][n_to_keep] = self.groupText self.__logger().info("Truncating number of categories of {} at {}".format(in_col, n_to_keep)) df = df.withColumn(out_col, F.when(F.col(out_col) >= n_to_keep, F.lit(n_to_keep)).otherwise( F.col(out_col))) # add the new indexed column with correct metadata, remove original indexed column. df = df.withColumn(out_col, with_meta(F.col(out_col), "", this_meta)) if not self.returnIndexed: for output_col in self.outputCols: df = df.withColumnRenamed(output_col, output_col + '_temp') df = IndexToString(inputCol=output_col + '_temp', outputCol=output_col).transform(df) df = df.drop(output_col + '_temp') if self.dropInputCols: df = df.drop(*self.inputCols) return df class HighCardinalityStringIndexer(Estimator, BaseParams, HasInputCols): """ This is a class that can be used in combination with HighCardinalityStringIndexerTransformer to simply reduce the cardinality of high-cardinality categorical features, while simultaneously indexing them to be ready for use in a machine learning algorithm. For each column, it replaces all observations that occur in less then a 'threshold' fraction of the rows in the dataframe with 'groupText'. It does so by calling pyspark.ml.feature.StringIndexer on the column, and subsequently replacing values and changing the metadata of the column. By also changing the metadata we ensure that we can later extract the text values from the indexed columns if desired. Example -------------------------------------------------------------------- >>> df = pd.DataFrame({'x1': ['a', 'b', 'a', 'b', 'c'], # a: 0.4, b: 0.4, c: 0.2 >>> 'x2': ['a', 'b', 'a', 'b', 'a'], # a: 0.6, b: 0.4, c: 0.0 >>> 'x3': ['a', 'a', 'a', 'a', 'a'], # a: 1.0, b: 0.0, c: 0.0 >>> 'x4': ['a', 'b', 'c', 'd', 'e']}) # a: 0.2, b: 0.2, c: 0.2, d: 0.2, e: 0.2 >>> >>> df = sqlContext.createDataFrame(df) >>> df.show() +---+---+---+---+ | x1| x2| x3| x4| +---+---+---+---+ | a| a| a| a| | b| b| a| b| | a| a| a| c| | b| b| a| d| | c| a| a| e| +---+---+---+---+ >>> # Replace all values that occur in less than 25% of the rows. >>> indexer = HighCardinalityStringIndexer(inputCols=df.columns, >>> outputCols=['ix_' + col for col in df_train.columns], >>> threshold=0.25).fit(df) >>> df = indexer.transform(df) >>> df.show() +---+---+---+---+-----+-----+-----+-----+ | x1| x2| x3| x4|ix_x1|ix_x2|ix_x3|ix_x4| +---+---+---+---+-----+-----+-----+-----+ | a| a| a| a| 0.0| 0.0| 0.0| 0.0| | b| b| a| b| 1.0| 1.0| 0.0| 0.0| | a| a| a| c| 0.0| 0.0| 0.0| 0.0| | b| b| a| d| 1.0| 1.0| 0.0| 0.0| | c| a| a| e| 2.0| 0.0| 0.0| 0.0| +---+---+---+---+-----+-----+-----+-----+ >>> # Optionally, obtain the labels after grouping >>> indexer = HighCardinalityStringIndexer(inputCols=df.columns, >>> outputCols=['grouped_' + col for col in df_train.columns], >>> threshold=0.25, >>> returnIndexed=False).fit(df) >>> df = indexer.transform(df) >>> df.show() +---+---+---+---+----------+----------+----------+----------+ | x1| x2| x3| x4|grouped_x1|grouped_x2|grouped_x3|grouped_x4| +---+---+---+---+----------+----------+----------+----------+ | a| a| a| a| a| a| a| other| | b| b| a| b| b| b| a| other| | a| a| a| c| a| a| a| other| | b| b| a| d| b| b| a| other| | c| a| a| e| other| a| a| other| +---+---+---+---+----------+----------+----------+----------+ """ outputCols = \ Param(Params._dummy(), "outputCols", "The output columns", typeConverter=TypeConverters.toListString) dropInputCols = \ Param(Params._dummy(), "dropInputCols", "Drop the input columns?", typeConverter=TypeConverters.toBoolean) threshold = \ Param(Params._dummy(), "threshold", "Group observations if they occur in less than threshold*100% of the rows", typeConverter=TypeConverters.toFloat) groupText = \ Param(Params._dummy(), "groupText", "The text to use to bin grouped observations", typeConverter=TypeConverters.toString) returnIndexed = \ Param(Params._dummy(), "returnIndexed", "Return the indexed columns, or their string representations?", typeConverter=TypeConverters.toBoolean) @keyword_only def __init__(self, inputCols: List[str], outputCols: List[str], dropInputCols: bool = False, threshold: float = .01, groupText: str = 'other', returnIndexed: bool = True) -> None: """ :param inputCols: String columns that need to be indexed :param dropInputCols: Should the input columns be dropped? :param threshold: Replace all observations that occur in less then a 'threshold' fraction of the rows. :param groupText: String to use as replacement for the observations that are binned because they occur in low frequency. :param returnIndexed: If True, return the indexed columns. If False, return the columns with their String values, where only the grouped observations are changed. """ super().__init__() self._setDefault(inputCols=None) self._setDefault(outputCols=None) self._setDefault(dropInputCols=False) self._setDefault(threshold=0.01) self._setDefault(groupText='other') self._setDefault(returnIndexed=True) kwargs = self._get_init_args_as_kwargs() self.setParams(**kwargs) @keyword_only def setParams(self, inputCols: List[str], outputCols: List[str], dropInputCols: bool = False, threshold: float = .01, groupText: str = 'other', returnIndexed: bool = True): kwargs = self._get_params_args_as_kwargs() return self._set(**kwargs) def getOutputCols(self) -> List[str]: return self.getOrDefault(self.outputCols) def getDropInputCols(self) -> bool: return self.getOrDefault(self.dropInputCols) def getThreshold(self) -> float: return self.getOrDefault(self.threshold) def getGroupText(self) -> str: return self.getOrDefault(self.groupText) def getReturnIndexed(self) -> bool: return self.getOrDefault(self.returnIndexed) @staticmethod def __logger() -> logging.Logger: """ Returns a reference to the logger to be used in this class Storing the logger as an attribute, and then referring to it in functions, can get it on the closure. Resulting in a lock object related to the logger to be included, which isn't serializable. """ return logging.getLogger(__name__) def _fit(self, df) -> HighCardinalityStringIndexerModel: """ :param df: A pyspark.sql.dataframe.DataFrame """ total = df.count() # For each column, calculate the number of unique elements to keep dict_indexers = {} for in_col, out_col in zip(self.getInputCols(), self.getOutputCols()): self.__logger().info("Fitting StringIndexer on '{}'".format(in_col)) string_indexer = StringIndexer(inputCol=in_col, outputCol=out_col, handleInvalid='skip').fit(df) self.__logger().info("Determining number of categories of '{}' to keep".format(in_col)) n_to_keep = df.groupby(in_col) \ .agg((F.count(in_col) / total).alias('perc')) \ .filter(F.col('perc') > self.getThreshold()) \ .count() self.__logger().info("Finished processing '{}'.".format(in_col)) if n_to_keep == 0: self.__logger().info("Every unique value of " "{} occurs less than fraction {} times count {}".format(in_col, self.getThreshold(), total) + "Therefore should exclude the column from the output") # TODO: exclude it dict_indexers[in_col] = {'indexer': string_indexer, 'n_to_keep': n_to_keep} return HighCardinalityStringIndexerModel( dict_indexers=dict_indexers, inputCols=self.getOrDefault(self.inputCols), outputCols=self.getOrDefault(self.outputCols), dropInputCols=self.getOrDefault(self.dropInputCols), groupText=self.getOrDefault(self.groupText), returnIndexed=self.getOrDefault(self.returnIndexed) )
nilq/baby-python
python
from pathlib import Path import pandas as pd import ibis import ibis.expr.operations as ops import ibis.expr.types as ir from ibis.backends.tests.base import BackendTest, RoundHalfToEven class TestConf(BackendTest, RoundHalfToEven): check_names = False additional_skipped_operations = frozenset({ops.StringSQLLike}) supported_to_timestamp_units = BackendTest.supported_to_timestamp_units | { 'ns' } supports_divide_by_zero = True returned_timestamp_unit = 'ns' @staticmethod def connect(data_directory: Path): return ibis.pandas.connect( dictionary={ 'functional_alltypes': pd.read_csv( str(data_directory / 'functional_alltypes.csv'), index_col=None, dtype={'bool_col': bool, 'string_col': str}, parse_dates=['timestamp_col'], encoding='utf-8', ), 'batting': pd.read_csv(str(data_directory / 'batting.csv')), 'awards_players': pd.read_csv( str(data_directory / 'awards_players.csv') ), } ) @property def functional_alltypes(self) -> ir.TableExpr: return self.connection.table("functional_alltypes") @property def batting(self) -> ir.TableExpr: return self.connection.table("batting") @property def awards_players(self) -> ir.TableExpr: return self.connection.table("awards_players")
nilq/baby-python
python
from selenium import webdriver # https://askubuntu.com/questions/870530/how-to-install-geckodriver-in-ubuntu def start_firefox(): my_browser = webdriver.Firefox() # send the browser to a specif URL my_browser.get('https://pitpietro.github.io/') # simulate the click on a link (and redirect to that link) about_element = my_browser.find_element_by_css_selector('div.list__item:nth-child(6) > article:nth-child(1) > h2:nth-child(1) > a:nth-child(1)') about_element.click() if __name__ == '__main__': start_firefox() exit(0)
nilq/baby-python
python
from indice_pollution.extensions import db class Zone(db.Model): __table_args__ = {"schema": "indice_schema"} id = db.Column(db.Integer, primary_key=True) type = db.Column(db.String) code = db.Column(db.String) @classmethod def get(cls, code, type_): return Zone.query.filter_by(code=code, type=type_).first()
nilq/baby-python
python
from django import template import datetime from cart.cart import Cart from cart.settings import CART_TEMPLATE_TAG_NAME register = template.Library() @register.simple_tag(takes_context=True, name=CART_TEMPLATE_TAG_NAME) def get_cart(context, session_key=None): """ Make the cart object available in template. Sample usage:: {% load carton_tags %} {% get_cart as cart %} {% for product in cart.products %} {{ product }} {% endfor %} """ request = context['request'] return Cart(request.session, session_key=session_key) #register.simple_tag(takes_context=True, name=CART_TEMPLATE_TAG_NAME)
nilq/baby-python
python
import unittest #import env from canvas import Canvas from color import Color class TestCanvas(unittest.TestCase): def test_canvas(self): c = Canvas(10, 20) self.assertEqual(c.width, 10) self.assertEqual(c.height, 20) def test_write_pixel(self): c = Canvas(10, 20) r = Color(1, 0, 0) c.write_pixel(2, 3, r) self.assertEqual(c.pixel_at(2, 3), r.rgb())
nilq/baby-python
python
""" Collect email information """ import os import subprocess import json import base64 from flask import current_app from pathlib import Path from ..functions import get_python_path class Email: """ Set up an email """ config_path = None __addresses = [] __ccs = [] __files = [] __html = '' __text = '' __subject = '' __data = {} send_as_one = None def __init__(self, send_as_one=False): self.config_path = None self.__addresses = [] self.__ccs = [] self.__files = [] self.__html = '' self.__text = '' self.__subject = '' self.__data = {} self.send_as_one = send_as_one if 'CONFIG_PATH' in current_app.config: self.config_path = current_app.config['CONFIG_PATH'] def add_address(self, address): """ Add email address """ self.__addresses.append(address) return self def add_addresses(self, addresses): """ Add addresses from array """ self.__addresses.extend(addresses) return self def add_cc(self, address): """ Add carbon copy """ self.__ccs.append(address) return self def add_ccs(self, addresses): """ Add carbon copy with array of addresses """ self.__ccs.extend(addresses) return self def add_file(self, absolute_file_path): """ Add attachment to email """ self.__files.append(absolute_file_path) return self def html(self, html): """ Set html content """ self.__html = html return self def text(self, text): """ Set text content """ self.__text = text return self def subject(self, subject): """ Set email subject """ self.__subject = subject return self def __create(self): """ Construct the email """ self.__data = json.dumps({ 'config_path': self.encode(self.config_path), 'subject': self.encode(self.__subject), 'text': self.encode(self.__text), 'html': self.encode(self.__html), 'files': self.__files, 'send_as_one': self.send_as_one, 'addresses': self.__addresses, 'ccs': self.__ccs, }) def encode(self, value): """ Encode parts of email to base64 for transfer """ return base64.b64encode(value.encode()).decode() def send(self): """ Construct and execute sendemail.py script Finds python binary by os.py, then uses the /usr/bin/python to execute email script """ self.__create() email_script = \ os.path.join(Path(__file__).parents[1], 'scripts', 'sendemail.py') if os.path.exists(email_script): subprocess.Popen( [get_python_path(), email_script, self.__data], stdin=None, stdout=None, stderr=None, close_fds=True)
nilq/baby-python
python
__version__ = "0.0.10" from .core import *
nilq/baby-python
python
""" Program to classify the snps based on the following: 1. whether are at stop codons 2. where are located on a functional gene sequence 4. whether are synonymous (S) or non-synonymous (N) 5. whether are fourfold degenerate sites (S) or non-degenerate sites (N) and count the number the number for each of the class, and will ulimately estimate the following: 1. Pi: Nucleotide diversity: 2*p*(1-p) where p is the alternative allele frequency 2. Pi(N)/Pi(S): Pi(non-degenerate sites) / Pi(fourfold degenerate sites) ??? 3. pN/pS: the ratio of non-synonymous to synonymous polymorphism rates """ from __future__ import division import re, vcf, copy, argparse import numpy as np from Bio import SeqIO from gff3 import Gff3 import pnps_dnds def parse_args(): """ Return dictionary of command line arguments """ parser = argparse.ArgumentParser( formatter_class=argparse.RawTextHelpFormatter, usage=argparse.SUPPRESS) parser.add_argument('program', help=argparse.SUPPRESS) parser.add_argument('--name', type=str, dest='name', required=True, help="""Name or identifier of the project""") parser.add_argument('--fna', type=str, dest='fna', required=True, help="""Path to consensus genome sequence""") parser.add_argument('--vcf', type=str, dest='vcf', required=True, help="""Path to core-genome SNPs""") parser.add_argument('--coords', type=str, dest='coords', default=None, help="""Path to core-genome coordinates""") parser.add_argument('--gff', type=str, dest='gff', default=None, help="""Path to gff file defining the CDS coordinates""") parser.add_argument('--with-mags', action='store_true', default=False, help="""The genome sequences or samples can be separated into MAGs and others""") parser.add_argument('--min-prev', type=float, dest='min_prev', default=None, help="""Minimal prevalence for SNP calling""") parser.add_argument('--with-header', action='store_true', default=False, help="""Output the header for the parameters""") parser.add_argument('--out', type=str, dest='out', default='/dev/stdout', help="""Path to output file (/dev/stdout)""") return vars(parser.parse_args()) def open_vcf_file(fpath, min_prev=None): """ * ``Record.CHROM``; string * ``Record.POS``; int * ``Record.ID``; None * ``Record.REF``; string * ``Record.ALT``; list * ``Record.QUAL``; None * ``Record.FILTER``; list * ``Record.INFO``; dictionary additional attributes: * ``Record.FORMAT``; string * ``Record.samples``; list * ``Record.genotype``; object """ vcf_reader = vcf.Reader(open(fpath, 'r')) raw_snps = [snp for snp in vcf_reader] sample_names = [sample.sample for sample in raw_snps[0].samples] n_sample = len(sample_names) vcf_snps = [] for snp in raw_snps: if min_prev is not None: prev = snp.INFO['NS']/n_sample if prev > min_prev: vcf_snps.append(snp) else: vcf_snps.append(snp) return vcf_snps def snp_stats(snps): n_snps = len(snps) bi_snps, tri_snps, quad_snps = classify_snps(snps) n_snps = len(snps) n_bi_snps = len(bi_snps) n_tri_snps = len(tri_snps) n_quad_snps = len(quad_snps) sample_names = [sample.sample for sample in snps[0].samples] n_sample = len(sample_names) values = [n_sample, n_snps, n_bi_snps, n_tri_snps, n_quad_snps] header = ['n_sample', 'n_snps', 'n_bi_snps', 'n_tri_snps', 'n_quad_snps'] return values, header def mag_snp_stats(snps): n_mag_snps = 0 are_mags = ['_' in sample.sample for sample in snps[0].samples] are_refs = ['_' not in sample.sample for sample in snps[0].samples] if np.all(are_mags): all_mags = 1 n_mag_snps = len(snps) else: all_mags = 0 ref_inds = [] for i, is_ref in enumerate(are_refs): if is_ref: ref_inds.append(i) static_sample = snps[0].samples[ref_inds[0]] for snp in snps: flgs = [] for ind in ref_inds[1:]: sample = snp.samples[ind] flgs.append(static_sample.data.GP1 == sample.data.GP1) flgs.append(static_sample.data.GP2 == sample.data.GP2) flgs.append(static_sample.data.GP3 == sample.data.GP3) flgs.append(static_sample.data.GP4 == sample.data.GP4) if not np.all(flgs): break if np.all(flgs): n_mag_snps = n_mag_snps + 1 else: pass values = [n_mag_snps, all_mags] header = ['n_mag_snps', 'all_mags'] return values, header def core_genome_stats(coords): n_cg_bloc = 0 cg_bloc_sizes = [] avg_cg_bloc_size = 0 mid_cg_bloc_size = 0 cg_bloc_dists = [] avg_cg_bloc_dist = 0 mid_cg_bloc_dist = 0 last_end = 0 with open(coords, "r") as fh: next(fh) for line in fh: items = line.rstrip().split("\t") cg_bloc_size = int(items[2]) - int(items[1])+1 cg_bloc_sizes.append(cg_bloc_size) if last_end > 0: cg_bloc_dist = int(items[1]) - last_end cg_bloc_dists.append(cg_bloc_dist) last_end = int(items[2]) n_cg_bloc = len(cg_bloc_sizes) cg_size = sum(cg_bloc_sizes) avg_cg_bloc_size = cg_size / n_cg_bloc mid_cg_bloc_size = sorted(cg_bloc_sizes)[int((n_cg_bloc-1)/2)] n_cg_dist = len(cg_bloc_dists) avg_cg_bloc_dist = sum(cg_bloc_dists)/n_cg_dist mid_cg_bloc_dist = sorted(cg_bloc_dists)[int((n_cg_dist-1)/2)] values = [cg_size, n_cg_bloc, avg_cg_bloc_size, mid_cg_bloc_size, avg_cg_bloc_dist, mid_cg_bloc_dist] header = ['cg_size', 'n_cg_bloc', 'avg_cg_bloc_size', 'mid_cg_bloc_size', 'avg_cg_bloc_dist', 'mid_cg_bloc_dist'] return values, header def classify_snps(snps): bi_snps = [] tri_snps = [] quad_snps = [] for snp in snps: if len(snp.ALT) == 1: bi_snps.append(snp) elif len(snp.ALT) == 2: tri_snps.append(snp) elif len(snp.ALT) == 3: quad_snps.append(snp) else: assert False return bi_snps, tri_snps, quad_snps def genic_stats(seq_recs, rec_table, cds_recs, snps): genic_region, genic_masks = get_genic_region(rec_table, cds_recs) genic_region_size = len(genic_region) bi_snps, tri_snps, quad_snps = classify_snps(snps) alt_rec_tb = get_alt_seq_recs(rec_table, bi_snps) alt_genic_region, alt_genic_masks = get_genic_region(alt_rec_tb, cds_recs) sample_names = [sample.sample for sample in snps[0].samples] n_sample = len(sample_names) genic_array = [ genic_masks[snp.CHROM][snp.POS] for snp in snps] genic_snp_counts = sum(genic_array) bi_genic_array = [ genic_masks[snp.CHROM][snp.POS] for snp in bi_snps] bi_genic_snp_counts = sum(bi_genic_array) # print genic_region # print len(genic_region) # print alt_genic_region # print len(alt_genic_region) pn, ps, dn, ds, pn2ps, dn2ds = pnps_dnds.pnps_dnds(genic_region, alt_genic_region) snps2codons = map_codons(rec_table, cds_recs, bi_snps) n_syn_snp = 0 n_nonsyn_snp = 0 n_premature_stop_snp = 0 n_stop_disrupt_snp = 0 pi_all = 0 pi_syn = 0 pi_nonsyn = 0 pi_4f_deg = 0 pi_non_deg = 0 for snp in bi_snps: pi_val = 2*float(snp.INFO['AF'][0])*(1-float(snp.INFO['AF'][0])) if snp.ID in snps2codons: codon_pair = snps2codons[snp.ID] if len(codon_pair) > 0: if not pnps_dnds.is_synonymous(codon_pair[0], codon_pair[1]): pi_nonsyn = pi_nonsyn + pi_val n_nonsyn_snp = n_nonsyn_snp + 1 if pnps_dnds.is_stop_codon(codon_pair[0]): n_stop_disrupt_snp = n_stop_disrupt_snp + 1 elif pnps_dnds.is_stop_codon(codon_pair[1]): n_premature_stop_snp = n_premature_stop_snp + 1 else: pass else: pi_syn = pi_syn + pi_val n_syn_snp = n_syn_snp + 1 if pnps_dnds.is_4f_deg(codon_pair[0], codon_pair[2]): pi_4f_deg = pi_4f_deg + pi_val if pnps_dnds.is_non_deg(codon_pair[0], codon_pair[2]): pi_non_deg = pi_non_deg + pi_val pi_all = pi_all + pi_val values = [genic_region_size, genic_snp_counts, bi_genic_snp_counts, pn2ps, pn, ps, dn2ds, dn, ds, n_syn_snp, n_nonsyn_snp, n_premature_stop_snp, n_stop_disrupt_snp, pi_all, pi_syn, pi_nonsyn, pi_4f_deg, pi_non_deg] header = ['genic_region_size', 'genic_snp_counts', 'bi_genic_snp_counts', 'pn2ps', 'pn', 'ps', 'dn2ds', 'dn', 'ds', 'n_syn_snp', 'n_nonsyn_snp', 'n_premature_stop_snp', 'n_stop_disrupt_snp', 'pi', 'pi_syn', 'pi_nonsyn', 'pi_4f_deg', 'pi_non_deg'] return values, header def get_alt_seq_recs(seq_rec_tb, snps): alt_rec_tb = copy.deepcopy(seq_rec_tb) for snp in snps: if seq_rec_tb[snp.CHROM][snp.POS] != snp.REF: alt_rec_tb[snp.CHROM] = str(alt_rec_tb[snp.CHROM][:snp.POS]) + str(snp.REF) + str(alt_rec_tb[snp.CHROM][snp.POS+1:]) elif seq_rec_tb[snp.CHROM][snp.POS] != snp.ALT[0]: alt_rec_tb[snp.CHROM] = str(alt_rec_tb[snp.CHROM][:snp.POS]) + str(snp.ALT[0]) + str(alt_rec_tb[snp.CHROM][snp.POS+1:]) else: print "{}: {} - {}: {}, {}, {}, {}, {}".format( snp.ID, snp.REF, snp.ALT[0], seq_rec_tb[snp.CHROM][snp.POS-2], seq_rec_tb[snp.CHROM][snp.POS-1], seq_rec_tb[snp.CHROM][snp.POS], seq_rec_tb[snp.CHROM][snp.POS+1], seq_rec_tb[snp.CHROM][snp.POS+2]) return alt_rec_tb ### map codons to snps def map_codons(seq_rec_tb, cds_recs, snps): codons = dict() for cds_rec in cds_recs: sid = cds_rec["seqid"] if sid not in codons: codons[sid] = dict() genic_region = seq_rec_tb[sid][cds_rec["start"]-1:cds_rec["end"]] for i in range(cds_rec["start"]-1, cds_rec["end"], 3): start_pos = i - cds_rec["start"] + 1 codon = genic_region[start_pos:start_pos+3] codons[sid][i] = "0.{}".format(codon) codons[sid][i+1] = "1.{}".format(codon) codons[sid][i+2] = "2.{}".format(codon) snps2codons = dict() n_nonsyn = 0 for snp in snps: sid = snp.CHROM snps2codons[snp.ID] = [] if sid in codons: pos = snp.POS if pos in codons[sid]: codon_code = codons[sid][pos] i, codon = codon_code.split(".") i = int(i) alt_codon = codon[:i]+str(snp.ALT[0])+codon[i+1:] if codon == alt_codon: alt_codon = codon[:i]+str(snp.REF)+codon[i+1:] snps2codons[snp.ID].append(codon) snps2codons[snp.ID].append(alt_codon) snps2codons[snp.ID].append(i) n_nonsyn = n_nonsyn + 1 # print [i, codon, alt_codon] return snps2codons ### get genic masks def get_genic_region(seq_rec_tb, cds_recs): genic_region = "" genic_masks = dict() for sid, seq in seq_rec_tb.iteritems(): genic_mask = np.repeat(False, len(seq_rec_tb[sid])) genic_masks[sid] = genic_mask for cds_rec in cds_recs: sid = cds_rec["seqid"] genic_masks[sid][cds_rec["start"]-1:cds_rec["end"]] = True genic_region = genic_region + seq_rec_tb[sid][cds_rec["start"]-1:cds_rec["end"]] return genic_region, genic_masks ### get pi genic diversity def get_pi(snps): all_pis = [] for snp in snps: pi = 2*snps.INFO['AF']*(1-snps.INFO['AF']) all_pis.append(pi) return all_pis def open_gff_file(gff): gff_hd = Gff3(gff) cds_recs = [] for line in gff_hd.lines[4:]: if 'seqid' in line and 'type' in line and 'strand' in line: if line['type'] == 'CDS' and line['strand'] == '+': cds_recs.append(line) print "number of cds: {}".format(len(cds_recs)) return cds_recs def get_gff_summary(cds_recs): n_cds = 0 cds_sizes = [] avg_cds_size = 0 mid_cds_size = 0 n_cds = len(cds_recs) for rec in cds_recs: cds_size = rec["end"] - rec["start"] + 1 cds_sizes.append(cds_size) if (cds_size % 3) != 0: print "odd cds with starting pos at: {}".format(rec["start"]) total_cds_size = sum(cds_sizes) avg_cds_size = total_cds_size / n_cds mid_cds_size = sorted(cds_sizes)[int((n_cds-1)/2)] return [n_cds, avg_cds_size, mid_cds_size] def get_seq_recs(fna): seq_recs = list(SeqIO.parse(fna, "fasta")) rec_table = dict() for rec in seq_recs: rec_table[rec.id] = str(rec.seq).upper() return seq_recs, rec_table def get_ref_genome_size(seq_recs): rg_size = 0 for rec in seq_recs: rg_size = rg_size + len(rec.seq) return rg_size def get_stats(args): seq_recs, rec_table = get_seq_recs(args['fna']) rg_size = get_ref_genome_size(seq_recs) header = ['name', 'rg_size'] params = [args['name'], rg_size] snps = open_vcf_file(args['vcf'], args['min_prev']) snp_values, snp_header = snp_stats(snps) params = params + snp_values header = header + snp_header if args['with_mags']: mag_snp_values, mag_snp_header = mag_snp_stats(snps) params = params + mag_snp_values header = header + mag_snp_header if args['coords'] is not None: core_genome_values, core_genome_header = core_genome_stats(args['coords']) params = params + core_genome_values header = header + core_genome_header if args['gff'] is not None: cds_recs = open_gff_file(args['gff']) genic_values, genic_header = genic_stats(seq_recs, rec_table, cds_recs, snps) params = params + genic_values header = header + genic_header return params, header def main(): args = parse_args() summary, header = get_stats(args) with open(args['out'], 'w') as fh: if args['with_header']: fh.write("\t".join([str(item) for item in header])+"\n") fh.write("\t".join([str(item) for item in summary])+"\n") if __name__ == "__main__": main()
nilq/baby-python
python
from typing import List def _get_data_array(filename: str) -> List[int]: lines = open(filename, 'r').readlines() ints = [int(i.strip()) for i in lines] return ints def _validate(factors: List[int], target: int) -> bool: sorted_factors = sorted(factors) left = 0 right = len(sorted_factors) - 1 current = sorted_factors[left] + sorted_factors[right] while (left != right): if current == target: return True elif current > target: right -= 1 elif current < target: left += 1 current = sorted_factors[left] + sorted_factors[right] return False if __name__ == '__main__': data = _get_data_array('./data_input.dat') for i in range(len(data) - 26): if not _validate(data[i:i+25], data[i+25]): print(f'Found a failure at index {i+25} which is {data[i+25]}') break
nilq/baby-python
python
class Solution: @lru_cache(None) def allPossibleFBT(self, n: int) -> List[Optional[TreeNode]]: if n % 2 == 0: return [] if n == 1: return [TreeNode(0)] ans = [] for leftCount in range(n): rightCount = n - 1 - leftCount for left in self.allPossibleFBT(leftCount): for right in self.allPossibleFBT(rightCount): ans.append(TreeNode(0)) ans[-1].left = left ans[-1].right = right return ans
nilq/baby-python
python
from pypy.lang.smalltalk import objspace space = objspace.ObjSpace() def ismetaclass(w_cls): # Heuristic to detect if this is a metaclass. Don't use apart # from in this test file, because classtable['w_Metaclass'] is # bogus after loading an image. return w_cls.w_class is space.classtable['w_Metaclass'] def test_every_class_is_an_instance_of_a_metaclass(): for (nm, w_cls) in space.classtable.items(): assert ismetaclass(w_cls) or ismetaclass(w_cls.w_class) def test_every_metaclass_inherits_from_class_and_behavior(): s_Class = space.classtable['w_Class'].as_class_get_shadow(space) s_Behavior = space.classtable['w_Behavior'].as_class_get_shadow(space) for (nm, w_cls) in space.classtable.items(): if ismetaclass(w_cls): shadow = w_cls.as_class_get_shadow(space) assert shadow.inherits_from(s_Class) assert s_Class.inherits_from(s_Behavior) def test_metaclass_of_metaclass_is_an_instance_of_metaclass(): w_Metaclass = space.classtable['w_Metaclass'] assert w_Metaclass.w_class.w_class is w_Metaclass
nilq/baby-python
python
import numpy as np import pandas as pd df = pd.DataFrame(np.arange(5*4).reshape(5,4)) df sampler = np.random.permutation(5) sampler df.take(sampler) sampler_col = np.random.permutation(4) sampler_col sampler_col = np.random.permutation(4) sampler_col df[sampler_col] df.sample(3) df.sample(3, axis=0) df.sample(3, axis=1) df.sample(5) choice = pd.Series([5,7,-1,6,4]) choice.sample(10, replace=True) choice.sample(10, replace=True)
nilq/baby-python
python
'''save/load from S3 routines ------- ''' from __future__ import print_function, division, unicode_literals from .omas_utils import * from .omas_core import save_omas_pkl, load_omas_pkl, ODS def _base_S3_uri(user): return 's3://omas3/{user}/'.format(user=user) # -------------------------------------------- # save and load OMAS with S3 # -------------------------------------------- def remote_uri(uri, filename, action): """ :param uri: uri of the container of the file :param filename: filename to act on :param action: must be one of [`up`, `down`, `list`, `del`] """ if not re.match('\w+://\w+.*', uri): return uri tmp = uri.split('://') system = tmp[0] location = '://'.join(tmp[1:]) if action not in ['down', 'up', 'list', 'del']: raise AttributeError('remote_uri action attribute must be one of [`up`, `down`, `list`, `del`]') if system == 's3': import boto3 from boto3.s3.transfer import TransferConfig s3bucket = location.split('/')[0] s3connection = boto3.resource('s3') s3filename = '/'.join(location.split('/')[1:]) if action == 'list': printd('Listing %s' % (uri), topic='s3') files = list(map(lambda x: x.key, s3connection.Bucket(s3bucket).objects.all())) s3filename = s3filename.strip('/') if s3filename: files = filter(lambda x: x.startswith(s3filename), files) return files if action == 'del': if filename is None: filename = s3filename.split('/')[-1] printd('Deleting %s' % uri, topic='s3') s3connection.Object(s3bucket, s3filename).delete() elif action == 'down': if filename is None: filename = s3filename.split('/')[-1] printd('Downloading %s to %s' % (uri, filename), topic='s3') obj = s3connection.Object(s3bucket, s3filename) if not os.path.exists(os.path.abspath(os.path.split(filename)[0])): os.makedirs(os.path.abspath(os.path.split(filename)[0])) obj.download_file(filename, Config=TransferConfig(use_threads=False)) elif action == 'up': printd('Uploading %s to %s' % (filename, uri), topic='s3') from botocore.exceptions import ClientError if s3filename.endswith('/'): s3filename += filename.split('/')[-1] try: s3connection.meta.client.head_bucket(Bucket=s3bucket) except ClientError as _excp: # If a client error is thrown, then check that it was a 404 error. # If it was a 404 error, then the bucket does not exist. error_code = int(_excp.response['Error']['Code']) if error_code == 404: s3connection.create_bucket(Bucket=s3bucket) else: raise bucket = s3connection.Bucket(s3bucket) with open(filename, 'rb') as data: bucket.put_object(Key=s3filename, Body=data) # , Metadata=meta) def save_omas_s3(ods, filename, user=os.environ.get('USER', 'dummy_user'), tmp_dir=omas_rcparams['tmp_imas_dir'], **kw): """ Save an OMAS object to pickle and upload it to S3 :param ods: OMAS data set :param filename: filename to save to :param user: username where to look for the file :param tmp_dir: temporary folder for storing S3 file on local workstation :param kw: arguments passed to the save_omas_pkl function """ printd('Saving to %s on S3' % (_base_S3_uri(user) + filename), topic='s3') if not os.path.exists(os.path.abspath(tmp_dir)): os.makedirs(os.path.abspath(tmp_dir)) save_omas_pkl(ods, os.path.abspath(tmp_dir) + os.sep + os.path.split(filename)[1], **kw) return remote_uri(_base_S3_uri(user), os.path.abspath(tmp_dir) + os.sep + os.path.split(filename)[1], 'up') def load_omas_s3(filename, user=os.environ.get('USER', 'dummy_user'), consistency_check=None, imas_version=None, tmp_dir=omas_rcparams['tmp_imas_dir']): """ Download an OMAS object from S3 and read it as pickle :param filename: filename to load from :param user: username where to look for the file :param consistency_check: verify that data is consistent with IMAS schema (skip if None) :param imas_version: imas version to use for consistency check (leave original if None) :param tmp_dir: temporary folder for storing S3 file on local workstation :return: OMAS data set """ printd('loading from %s on S3' % (_base_S3_uri(user) + filename), topic='s3') if not os.path.exists(os.path.abspath(tmp_dir)): os.makedirs(os.path.abspath(tmp_dir)) remote_uri(_base_S3_uri(user) + filename, os.path.abspath(tmp_dir) + os.sep + os.sep + os.path.split(filename)[1], 'down') return load_omas_pkl(os.path.abspath(tmp_dir) + os.sep + os.path.split(filename)[1], consistency_check=consistency_check, imas_version=imas_version) def list_omas_s3(user=''): """ List S3 content :param user: username where to look for the file :return: OMAS data set """ return remote_uri(_base_S3_uri(user), None, 'list') def del_omas_s3(filename, user=os.environ.get('USER', 'dummy_user')): """ Delete an OMAS object from S3 :param user: username where to look for the file :return: OMAS data set """ remote_uri(_base_S3_uri(user) + filename, None, 'del') def through_omas_s3(ods, method=['function', 'class_method'][1]): """ Test save and load S3 :param ods: ods :return: ods """ filename = 'test.pkl' if method == 'function': save_omas_s3(ods, filename, user='omas_test') ods1 = load_omas_s3(filename, user='omas_test') else: ods.save('s3', filename=filename, user='omas_test') ods1 = ODS().load('s3', filename=filename, user='omas_test') return ods1
nilq/baby-python
python
""" Pascal sections to be inserted in code. Copyright (C) 2019, Guillaume Gonnet This project is under the MIT license. """ from typing import List, Text def gen_bird_array(envs: List, name: Text): "Generate a static array for bird directions / species." upper = name.capitalize() count = sum(int("same-as" not in e) for e in envs) result = "k%s: array[0..%d, 0..9, 0..19] of integer = (" % ( upper, count-1) for i, e in enumerate(envs): if e.get("same-as"): continue result += "\n" + " " * 4 + "( // Environment %d. " % (i+1) for r in e["rounds"]: array = r[name] or [] array += [9] * (20 - len(array)) result += "\n" + " " * 8 + "(" + ", ".join(map(str, array)) + ")," result = result[:-1] + "\n" result += " " * 4 + ")," return result[:-1] + "\n);\n" def gen_species(R: List, **kargs): "Generate envirnoment hashes." return gen_bird_array(R, "directions") def gen_directions(R: List, **kargs): "Generate envirnoment hashes." return gen_bird_array(R, "species") def gen_hashes(R: List, **kargs): "Generate envirnoment hashes." count = sum(int("same-as" not in e) for e in R) hashes = (str(e.get("hash", 0)) for e in R if "same-as" not in e) result = "kEnvHashes: array[0..%d] of uint64 = (\n " % (count-1) result += ", ".join(hashes) result += "\n);" return result def gen_target_scores(R: List, Scores: List[int], **kargs): "Generate target scores." count = sum(int("same-as" not in e) for e in R) result = "kTargetScores: array[0..%d] of integer = (" % (count-1) result += ", ".join(str(s) for s in Scores) result += ");" return result
nilq/baby-python
python
from __future__ import annotations from typing import TYPE_CHECKING, Optional from ._base import TelegramObject if TYPE_CHECKING: # pragma: no cover from .photo_size import PhotoSize class VideoNote(TelegramObject): """ This object represents a video message (available in Telegram apps as of v.4.0). Source: https://core.telegram.org/bots/api#videonote """ file_id: str """Identifier for this file, which can be used to download or reuse the file""" file_unique_id: str """Unique identifier for this file, which is supposed to be the same over time and for different bots. Can't be used to download or reuse the file.""" length: int """Video width and height (diameter of the video message) as defined by sender""" duration: int """Duration of the video in seconds as defined by sender""" thumb: Optional[PhotoSize] = None """Video thumbnail""" file_size: Optional[int] = None """File size"""
nilq/baby-python
python
import datetime from django.db import models from django.utils import timezone #My comments: #Each model is represented by a class that subclasses django.db.models.Model. #Each model has a number of class variables, each of which represents a database field in the model. #Each field is represented by an instance of a Field class – e.g., CharField for character fields and DateTimeField for datetimes. #This tells Django what type of data each field holds. class Question(models.Model): question_text = models.CharField(max_length=200) pub_date = models.DateTimeField('date published') def __str__(self): return self.question_text def was_published_recently(self): now = timezone.now() return now - datetime.timedelta(days=1) <= self.pub_date <= now was_published_recently.admin_order_field = 'pub_date' was_published_recently.boolean = True was_published_recently.short_description = 'Published recently?' class Choice(models.Model): question = models.ForeignKey(Question, on_delete=models.CASCADE) choice_text = models.CharField(max_length=200) votes = models.IntegerField(default=0) def __str__(self): return self.choice_text
nilq/baby-python
python
''' Class to access documents and links stored in the hdf5 file. ''' import h5py class CorpusHDF5(): def __init__(self, path): self.f = h5py.File(path, 'r') def get_article_text(self, article_id): return self.f['text'][article_id] def get_article_title(self, article_id): return self.f['title'][article_id] def get_titles_pos(self): ''' Return a dictionary where the keys are articles' titles and the values are their offset in the data array. ''' return dict((el,i) for i,el in enumerate(self.f['title'].value)) def get_pos_titles(self): ''' Return a dictionary where the keys are the articles' offset in the data array and the values are their titles. ''' return dict((i,el) for i,el in enumerate(self.f['title'].value)) def get_text_iter(self): return self.f['text'] def get_title_iter(self): return self.f['title']
nilq/baby-python
python
str1 = 'Hello' print(str1.endswith('e', 0, 2)) print(str1.endswith('o', 0, 4)) #True (substring 'He' (0 to 1 index) ends with 'e') #False (substring 'Hell' (0 to 3 index) does not end with 'o')
nilq/baby-python
python
# # Copyright 2015, 2016 Human Longevity, Inc. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. # """ Pipe A "task" in disdat. This inherits from Luigi's Abstract Task Class The idea is that parameters are actually the parameters to the run function requires is the tasks that have to run before this task runs output() is basically a function that says, given the parameters, what is the output of this task. inputs() isn't used as much, but it says, here is a list of inputs I expect to be available before I run. author: Kenneth Yocum """ import os import time import luigi from disdat.pipe_base import PipeBase from disdat.db_link import DBLink from disdat.driver import DriverTask from disdat.fs import DisdatFS from disdat.common import BUNDLE_TAG_TRANSIENT, BUNDLE_TAG_PARAMS_PREFIX from disdat import logger as _logger class PipeTask(luigi.Task, PipeBase): """ user_arg_name: calling_task: driver_output_bundle: force: output_tags: incremental_push: incremental_pull: """ user_arg_name = luigi.Parameter(default=None, significant=False) # what the outputs are referred to by downstreams calling_task = luigi.Parameter(default=None, significant=False) # This is used for re-running in apply:resolve_bundle to manually see if # we need to re-run the root task. driver_output_bundle = luigi.Parameter(default='None', significant=False) force = luigi.BoolParameter(default=False, significant=False) output_tags = luigi.DictParameter(default={}, significant=True) # Each pipeline executes wrt a data context. data_context = luigi.Parameter(default=None, significant=False) # Each pipeline can be configured to commit and push intermediate values to the remote incremental_push = luigi.BoolParameter(default=False, significant=False) # Each pipeline can be configured to pull intermediate values on demand from the remote incremental_pull = luigi.BoolParameter(default=False, significant=False) def __init__(self, *args, **kwargs): """ This has the same signature as luigi.Task. Go through this class and get the set of params we define Args: *args: **kwargs: """ super(PipeTask, self).__init__(*args, **kwargs) # Instance variables to track various user wishes self.user_set_human_name = None # self.set_bundle_name() self.user_tags = {} # self.add_tags() self.add_deps = {} # self.add(_external)_dependency() self.db_targets = [] # Deprecating self._input_tags = {} # self.get_tags() of upstream tasks self._input_bundle_uuids = {} # self.get_bundle_uuid() of upstream tasks self._mark_force = False # self.mark_force() def bundle_outputs(self): """ Each pipe creates an output bundle name Idea: WorkflowName + task.task_id == [Pipe Class Name + params + hash] For now: Apply Output Bundle "-" Pipe Class Name """ output_bundles = [(self.pipe_id(), self.pfs.get_path_cache(self).uuid)] return output_bundles def bundle_inputs(self): """ Given this pipe, return the set of bundles created by the input pipes. Mirrors Luigi task.inputs() NOTE: Calls task.deps which calls task._requires which calls task.requires() :param pipe_task: A PipeTask or a DriverTask (both implement PipeBase) :return: [(bundle_name, uuid), ... ] """ input_tasks = self.deps() input_bundles = [(task.pipe_id(), self.pfs.get_path_cache(task).uuid) for task in input_tasks] return input_bundles def pipe_id(self): """ Given a pipe instance, return a unique string based on the class name and the parameters. This re-uses Luigi code for getting the unique string. NOTE: The PipeTask has a 'driver_output_bundle'. This the name of the pipline output bundle given by the user. Because this is a Luigi parameter, it is included in the Luigi self.task_id string and hash. So we do not have to append this separately. """ return self.task_id def pipeline_id(self): """ This is the "human readable" name; a "less unique" id than the unique id. The pipeline_id is well-defined for the output task -- it is the output bundle name. For intermediate outputs the pipeline_id defaults to the pipe_id(). Else, it may be set by the task author. Note: Should we provide an identify for which version of this pipe is running at which stage in the pipeline? Short answer, no. Imagine if we name with the pipeline bundle output name, branch index, and level index. In this case if anyone re-uses this output, the human_name for the bundle won't be meaningful. For the pipeline owner, it may also not be helpful. The system may also place different outputs at different times under those indices. Too complicated. Returns: (str) """ if self.driver_output_bundle is not None: return self.driver_output_bundle elif self.user_set_human_name is not None: return self.user_set_human_name else: id_parts = self.pipe_id().split('_') return "{}_{}".format(id_parts[0],id_parts[-1]) def get_hframe_uuid(self): """ Return the unique ID for this tasks current output hyperframe Returns: hframe_uuid (str): The unique identifier for this task's hyperframe """ pce = self.pfs.get_path_cache(self) assert (pce is not None) return pce.uuid def upstream_hframes(self): """ Convert upstream tasks to hyperframes, return list of hyperframes Returns: (:list:`hyperframe.HyperFrameRecord`): list of upstream hyperframes """ tasks = self.requires() hfrs = [] for t in tasks: hfid = t.get_hframe_uuid() hfrs.append(self.pfs.get_hframe_by_uuid(hfid, data_context=self.data_context)) return hfrs def requires(self): """ Return Tasks on which this task depends. Build them intelligently, however. 1.) The input_df so far stays the same for all upstream pipes. 2.) However, when we resolve the location of the outputs, we need to do so correctly. :return: """ kwargs = self.prepare_pipe_kwargs() self.add_deps.clear() self.pipe_requires(**kwargs) rslt = self.add_deps if len(self.add_deps) == 0: return [] tasks = [] for user_arg_name, cls_and_params in rslt.items(): pipe_class, params = cls_and_params[0], cls_and_params[1] assert isinstance(pipe_class, luigi.task_register.Register) # we propagate the same inputs and the same output dir for every upstream task! params.update({ 'user_arg_name': user_arg_name, 'calling_task': self, 'driver_output_bundle': None, # allow intermediate tasks pipe_id to be independent of root task. 'force': self.force, 'output_tags': dict({}), # do not pass output_tags up beyond root task 'data_context': self.data_context, # all operations wrt this context 'incremental_push': self.incremental_push, # propagate the choice to push incremental data. 'incremental_pull': self.incremental_pull # propagate the choice to incrementally pull data. }) tasks.append(pipe_class(**params)) return tasks def output(self): """ This is the *only* output function for all pipes. It declares the creation of the one HyperFrameRecord pb and that's it. Remember, has to be idempotent. Return: (list:str): """ return PipeBase.add_bundle_meta_files(self) def run(self): """ Call users run function. 1.) prepare the arguments 2.) run and gather user result 3.) interpret and wrap in a HyperFrame Returns: (`hyperframe.HyperFrame`): """ kwargs = self.prepare_pipe_kwargs(for_run=True) pce = self.pfs.get_path_cache(self) assert(pce is not None) """ NOTE: If a user changes a task param in run(), and that param parameterizes a dependency in requires(), then running requires() post run() will give different tasks. To be safe we record the inputs before run() """ cached_bundle_inputs = self.bundle_inputs() try: start = time.time() # P3 datetime.now().timestamp() user_rtn_val = self.pipe_run(**kwargs) stop = time.time() # P3 datetime.now().timestamp() except Exception as error: """ If user's pipe fails for any reason, remove bundle dir and raise """ try: _logger.error("User pipe_run encountered exception: {}".format(error)) PipeBase.rm_bundle_dir(pce.path, pce.uuid, self.db_targets) except OSError as ose: _logger.error("User pipe_run encountered error, and error on remove bundle: {}".format(ose)) raise try: presentation, frames = PipeBase.parse_return_val(pce.uuid, user_rtn_val, self.data_context) hfr = PipeBase.make_hframe(frames, pce.uuid, cached_bundle_inputs, self.pipeline_id(), self.pipe_id(), self, start_ts=start, stop_ts=stop, tags={"presentable": "True"}, presentation=presentation) # Add any output tags to the user tag dict if self.output_tags: self.user_tags.update(self.output_tags) # If this is the root_task, identify it as so in the tag dict if isinstance(self.calling_task, DriverTask): self.user_tags.update({'root_task': 'True'}) # Lastly add any parameters associated with this class as tags. # They are differentiated by a special prefix in the key self.user_tags.update(self._get_subcls_params()) # Overwrite the hyperframe tags with the complete set of tags hfr.replace_tags(self.user_tags) self.data_context.write_hframe(hfr) transient = False if hfr.get_tag(BUNDLE_TAG_TRANSIENT) is not None: transient = True if self.incremental_push and not transient: self.pfs.commit(None, None, uuid=pce.uuid, data_context=self.data_context) self.pfs.push(uuid=pce.uuid, data_context=self.data_context) except Exception as error: """ If we fail for any reason, remove bundle dir and raise """ PipeBase.rm_bundle_dir(pce.path, pce.uuid, self.db_targets) raise return hfr def _get_subcls_params(self): """ Given the child class, extract user defined Luigi parameters The right way to do this is to use vars(cls) and filter by Luigi Parameter types. Luigi get_params() gives us all parameters in the full class hierarchy. It would give us the parameters in this class as well. And then we'd have to do set difference. See luigi.Task.get_params() NOTE: We do NOT keep the parameter order maintained by Luigi. That's critical for Luigi creating the task_id. However, we can implicitly re-use that ordering if we re-instantiate the Luigi class. Args: self: The instance of the subclass. To get the normalized values for the Luigi Parameters Returns: dict: (BUNDLE_TAG_PARAM_PREFIX.<name>:'string value',...) """ # Don't need to bother with serializing parameters, just grab them # tags only need to get serialized into hyperframes and never recovered cls = self.__class__ params = {} for param in vars(cls): attribute = getattr(cls, param) if isinstance(attribute, luigi.Parameter): params["{}{}".format(BUNDLE_TAG_PARAMS_PREFIX, param)] = attribute.serialize(getattr(self, param)) return params def prepare_pipe_kwargs(self, for_run=False): """ Each upstream task produces a bundle. Prepare that bundle as input to the user's pipe_run function. Args: for_run (bool): prepare args for run -- at that point all upstream tasks have completed. Returns: (dict): A dictionary with the arguments. """ kwargs = dict() # Place upstream task outputs into the kwargs. Thus the user does not call # self.inputs(). If they did, they would get a list of output targets for the bundle # that isn't very helpful. if for_run: # Reset the stored tags, in case this instance is run multiple times. self._input_tags = {} self._input_bundle_uuids = {} upstream_tasks = [(t.user_arg_name, self.pfs.get_path_cache(t)) for t in self.requires()] for user_arg_name, pce in [u for u in upstream_tasks if u[1] is not None]: hfr = self.pfs.get_hframe_by_uuid(pce.uuid, data_context=self.data_context) assert hfr.is_presentable() # Download any data that is not local (the linked files are not present). # This is the default behavior when running in a container. # The non-default is to download and localize ALL bundles in the context before we run. # That's in-efficient. We only need meta-data to determine what to re-run. if self.incremental_pull: DisdatFS()._localize_hfr(hfr, pce.uuid, self.data_context) if pce.instance.user_arg_name in kwargs: _logger.warning('Task human name {} reused when naming task dependencies: Dependency hyperframe shadowed'.format(pce.instance.user_arg_name)) self._input_tags[user_arg_name] = hfr.tag_dict self._input_bundle_uuids[user_arg_name] = pce.uuid kwargs[user_arg_name] = self.data_context.present_hfr(hfr) return kwargs """ Pipes Interface -- A pipe implements these calls """ def pipe_requires(self, **kwargs): """ This is the place to put your pipeline dependencies. Place the upstream pipes in an array and a dict for their params Args: **kwargs: Returns: """ return None def pipe_run(self, **kwargs): """ There is only one default argument "input_df" in kwargs. The other keys in kwargs will be identical to your Luigi parameters specified in this class. The input_df has the data context identifiers, e.g., sampleName, sessionId, subjectId The input_df has the data in either jsonData or fileData. A sharded task will receive a subset of all possible inputs. Args: **kwargs: Returns: """ raise NotImplementedError() def add_dependency(self, name, task_class, params): """ Disdat Pipe API Function Add a task and its parameters to our requirements Args: name (str): Name of our upstream (also name of argument in downstream) task_class (:object): upstream task class params (:dict): Dictionary of Returns: None """ if not isinstance(params, dict): error = "add_dependency third argument must be a dictionary of parameters" raise Exception(error) assert (name not in self.add_deps) self.add_deps[name] = (task_class, params) return def add_external_dependency(self, name, task_class, params): """ Disdat Pipe API Function Add an external task and its parameters to our requirements. What this means is that there is no run function and, in that case, Luigi will ignore the results of task.deps() (which calls flatten(self.requires())). And what that means is that this requirement can only be satisfied by the bundle actually existing. Args: name (str): Name of our upstream (also name of argument in downstream) task_class (:object): upstream task class params (:dict): Dictionary of Returns: None """ if not isinstance(params, dict): error = "add_dependency third argument must be a dictionary of parameters" raise Exception(error) assert (name not in self.add_deps) self.add_deps[name] = (luigi.task.externalize(task_class), params) return def add_db_target(self, db_target): """ Every time the user creates a db target, we add it to the list of db_targets in this pipe. Note: We add through the DBTarget object create, not through pipe.create_db_target() in the case that people do some hacking and don't use that API. Args: db_target (`db_target.DBTarget`): Returns: None """ self.db_targets.append(db_target) def create_output_table(self, dsn, table_name, schema_name=None): """ Create an output table target. Use the target to parameterize queries with the target table name. Args: dsn (unicode): The dsn indicating the configuration to connect to the db table_name (unicode): The table name. schema_name (unicode): Optional force use of schema (default None) Returns: (`disdat.db_target.DBTarget`) """ target = DBLink(self, dsn, table_name, schema_name=schema_name) return target def create_output_file(self, filename): """ Disdat Pipe API Function Pass in the name of your file, and get back an object to which you can write. Under the hood, this is a Luigi.Target. Args: filename: The name of your file, not the path. Returns: (`luigi.Target`): """ return self.make_luigi_targets_from_basename(filename) def create_output_dir(self, dirname): """ Disdat Pipe API Function Given basename directory name, return a fully qualified path whose prefix is the local output directory for this bundle in the current context. This call creates the output directory as well. Args: dirname (str): The name of the output directory, i.e., "models" Returns: output_dir (str): Fully qualified path of a directory whose prefix is the bundle's local output directory. """ prefix_dir = self.get_output_dir() fqp = os.path.join(prefix_dir, dirname) try: os.makedirs(fqp) except IOError as why: _logger.error("Creating directory in bundle directory failed:".format(why)) return fqp def get_output_dir(self): """ Disdat Pipe API Function Retrieve the output directory for this task's bundle. You may place files directly into this directory. Returns: output_dir (str): The bundle's output directory """ # Find the path cache entry for this pipe to find its output path pce = self.pfs.get_path_cache(self) assert(pce is not None) return pce.path def set_bundle_name(self, human_name): """ Disdat Pipe API Function Set the human name for this bundle. If not called, then intermediate outputs will have human names identical to their process names. Args: human_name (str): The human name of this pipe's output bundle. Returns: None """ self.user_set_human_name = human_name def add_tags(self, tags): """ Disdat Pipe API Function Adds tags to bundle. Args: tags (dict (str, str)): key value pairs (string, string) Returns: None """ assert (isinstance(tags, dict)) self.user_tags.update(tags) def get_tags(self, user_arg_name): """ Disdat Pipe API Function Retrieve the tag dictionary from an upstream task. Args: user_arg_name (str): keyword arg name of input bundle data for which to return tags Returns: tags (dict (str, str)): key value pairs (string, string) """ assert user_arg_name in self._input_tags return self._input_tags[user_arg_name] def get_bundle_uuid(self, user_arg_name): """ Disdat Pipe API Function Retrieve the UUID from an upstream task. Args: user_arg_name (str): keyword arg name of input bundle data for which to return tags Returns: uuid (str) """ assert user_arg_name in self._input_bundle_uuids return self._input_bundle_uuids[user_arg_name] def mark_force(self): """ Disdat Pipe API Function Mark pipe to force recompution of this particular task. This means that Disdat/Luigi will always re-run this particular pipe / task. We mark the pipe with a particular flag so that apply.resolve_bundle() Returns: None """ self._mark_force = True def mark_transient(self): """ Disdat Pipe API Function Mark output bundle as transient. This means that during execution Disdat will not write (push) this bundle back to the remote. That only happens in two cases: 1.) Started the pipeline with incremental_push=True 2.) Running the pipeline in a container with no_push or no_push_intermediates False We mark the bundle with a tag. Incremental push investigates the tag before pushing. And the entrypoint investigates the tag if we are not pushing incrementally. Otherwise, normal push commands from the CLI or api will work, i.e., manual pushes continue to work. Returns: None """ self.add_tags({BUNDLE_TAG_TRANSIENT: 'True'})
nilq/baby-python
python
# coding=utf-8 """ OneForAll默认配置 """ import pathlib import warnings # 禁用所有警告信息 warnings.filterwarnings("ignore") # 路径设置 relative_directory = pathlib.Path(__file__).parent.parent # OneForAll代码相对路径 module_dir = relative_directory.joinpath('modules') # OneForAll模块目录 third_party_dir = relative_directory.joinpath('thirdparty') # 三方工具目录 data_storage_dir = relative_directory.joinpath('data') # 数据存放目录 result_save_dir = relative_directory.joinpath('results') # 结果保存目录 temp_save_dir = result_save_dir.joinpath('temp') # OneForAll入口参数设置 enable_check_version = True # 开启最新版本检查 enable_brute_module = True # 使用爆破模块(默认True) enable_dns_resolve = True # 使用DNS解析子域(默认True) enable_http_request = True # 使用HTTP请求子域(默认True) enable_finder_module = True # 开启finder模块,开启会从响应体和JS中再次发现子域(默认True) enable_altdns_module = True # 开启altdns模块,开启会利用置换技术重组子域再次发现新子域(默认True) enable_enrich_module = True # 开启enrich模块,开启会富化出信息,如ip的cdn,cidr,asn,org,addr和isp等信息 enable_banner_identify = True # 开启WEB指纹识别模块(默认True) enable_takeover_check = False # 开启子域接管风险检查(默认False) # 参数可选值有 'small', 'medium', 'large' http_request_port = 'small' # HTTP请求子域(默认 'small',探测80,443端口) # 参数可选值True,False分别表示导出存活,全部子域结果 result_export_alive = False # 只导出存活的子域结果(默认False) # 参数可选格式有 'csv', 'json' result_save_format = 'csv' # 子域结果保存文件格式(默认csv) # 参数path默认None使用OneForAll结果目录自动生成路径 result_save_path = None # 子域结果保存文件路径(默认None) # 收集模块设置 save_module_result = False # 保存各模块发现结果为json文件(默认False) enable_all_module = True # 启用所有收集模块(默认True) enable_partial_module = [] # 启用部分收集模块 必须禁用enable_all_module才能生效 # 只使用ask和baidu搜索引擎收集子域的示例 # enable_partial_module = ['modules.search.ask', 'modules.search.baidu'] module_thread_timeout = 90.0 # 每个收集模块线程超时时间(默认90秒) # 爆破模块设置 enable_wildcard_check = True # 开启泛解析检测(默认True) enable_wildcard_deal = True # 开启泛解析处理(默认True) brute_massdns_path = None # 默认None自动选择 如需填写请填写绝对路径 brute_status_format = 'ansi' # 爆破时状态输出格式(默认asni,可选json) brute_concurrent_num = 2000 # 并发查询数量(默认2000,最大推荐10000) brute_socket_num = 1 # 爆破时每个进程下的socket数量 brute_resolve_num = 15 # 解析失败时尝试换名称服务器重查次数 # 爆破所使用的字典路径(默认None则使用data/subdomains.txt,自定义字典请使用绝对路径) brute_wordlist_path = None # 域名的权威DNS名称服务器的保存路径 当域名开启了泛解析时会使用该名称服务器来进行A记录查询 authoritative_dns_path = data_storage_dir.joinpath('authoritative_dns.txt') enable_recursive_brute = False # 是否使用递归爆破(默认False) brute_recursive_depth = 2 # 递归爆破深度(默认2层) # 爆破下一层子域所使用的字典路径(默认None则使用data/subnames_next.txt,自定义字典请使用绝对路径) recursive_nextlist_path = None enable_check_dict = False # 是否开启字典配置检查提示(默认False) delete_generated_dict = True # 是否删除爆破时临时生成的字典(默认True) delete_massdns_result = True # 是否删除爆破时massdns输出的解析结果 (默认True) only_save_valid = True # 是否在处理爆破结果时只存入解析成功的子域 check_time = 10 # 检查字典配置停留时间(默认10秒) enable_fuzz = False # 是否使用fuzz模式枚举域名 fuzz_place = None # 指定爆破的位置 指定的位置用`@`表示 示例:www.@.example.com fuzz_rule = None # fuzz域名使用的正则表达式 示例:'[a-z][0-9]' 表示第一位是字母 第二位是数字 fuzz_list = None # fuzz域名使用的字典路径 brute_ip_blacklist = {'0.0.0.0', '0.0.0.1'} # IP黑名单 子域解析到IP黑名单则标记为非法子域 ip_appear_maximum = 100 # 多个子域解析到同一IP次数超过100次则标记为非法(泛解析)子域 # altdns模块设置 altdns_increase_num = True altdns_decrease_num = True altdns_replace_word = False altdns_insert_word = False altdns_add_word = False # banner识别模块设置 banner_process_number = 4 # 识别进程数量(默认4) # 代理设置 enable_request_proxy = False # 是否使用代理(全局开关,默认False) proxy_all_module = False # 代理所有模块 proxy_partial_module = ['GoogleQuery', 'AskSearch', 'DuckDuckGoSearch', 'GoogleAPISearch', 'GoogleSearch', 'YahooSearch', 'YandexSearch', 'CrossDomainXml', 'ContentSecurityPolicy'] # 代理自定义的模块 request_proxy_pool = [{'http': 'http://127.0.0.1:1080', 'https': 'https://127.0.0.1:1080'}] # 代理池 # request_proxy_pool = [{'http': 'socks5h://127.0.0.1:10808', # 'https': 'socks5h://127.0.0.1:10808'}] # 代理池 # 请求设置 request_thread_count = None # 请求线程数量(默认None,则根据情况自动设置) request_timeout_second = (13, 27) # 请求超时秒数(默认connect timout推荐略大于3秒) request_ssl_verify = False # 请求SSL验证(默认False) request_allow_redirect = True # 请求允许重定向(默认True) request_redirect_limit = 10 # 请求跳转限制(默认10次) # 默认请求头 可以在headers里添加自定义请求头 request_default_headers = { 'Accept': 'text/html,application/xhtml+xml,' 'application/xml;q=0.9,*/*;q=0.8', 'Accept-Encoding': 'gzip, deflate', 'Accept-Language': 'en-US,en;q=0.9,zh-CN;q=0.8,zh;q=0.7', 'Cache-Control': 'max-age=0', 'DNT': '1', 'Referer': 'https://www.google.com/', 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 ' '(KHTML, like Gecko) Chrome/76.0.3809.100 Safari/537.36', 'Upgrade-Insecure-Requests': '1', 'X-Forwarded-For': '127.0.0.1' } enable_random_ua = True # 使用随机UA(默认True,开启可以覆盖request_default_headers的UA) # 搜索模块设置 # 开启全量搜索会尽量去获取搜索引擎搜索的全部结果,不过搜索耗时可能会过长 enable_full_search = False # 启用全量搜索(默认False) enable_recursive_search = False # 递归搜索子域(默认False) search_recursive_times = 2 # 递归搜索层数(默认2) # DNS解析设置 resolver_nameservers = [ '223.5.5.5', # AliDNS '119.29.29.29', # DNSPod '114.114.114.114', # 114DNS '8.8.8.8', # Google DNS '1.1.1.1' # CloudFlare DNS ] # 指定查询的DNS域名服务器 resolver_timeout = 5.0 # 解析超时时间(默认5.0秒) resolver_lifetime = 10.0 # 解析存活时间(默认10.0秒) # 请求端口探测设置 # 你可以在端口列表添加自定义端口 small_ports = [80, 443] # 默认使用 medium_ports = [80, 443, 8000, 8080, 8443] # 注意:建议大厂的域名尽量不使用大端口范围,因为大厂的子域太多,加上使用大端口范围会导致生成的 # 请求上十万,百万,千万级,可能会导致内存不足程序奔溃,另外这样级别的请求量等待时间也是漫长的。 # OneForAll不是一个端口扫描工具,如果要扫端口建议使用nmap,zmap之类的工具。 large_ports = [80, 81, 280, 300, 443, 591, 593, 832, 888, 901, 981, 1010, 1080, 1100, 1241, 1311, 1352, 1434, 1521, 1527, 1582, 1583, 1944, 2082, 2082, 2086, 2087, 2095, 2096, 2222, 2301, 2480, 3000, 3128, 3333, 4000, 4001, 4002, 4100, 4125, 4243, 4443, 4444, 4567, 4711, 4712, 4848, 4849, 4993, 5000, 5104, 5108, 5432, 5555, 5800, 5801, 5802, 5984, 5985, 5986, 6082, 6225, 6346, 6347, 6443, 6480, 6543, 6789, 7000, 7001, 7002, 7396, 7474, 7674, 7675, 7777, 7778, 8000, 8001, 8002, 8003, 8004, 8005, 8006, 8008, 8009, 8010, 8014, 8042, 8069, 8075, 8080, 8081, 8083, 8088, 8090, 8091, 8092, 8093, 8016, 8118, 8123, 8172, 8181, 8200, 8222, 8243, 8280, 8281, 8333, 8384, 8403, 8443, 8500, 8530, 8531, 8800, 8806, 8834, 8880, 8887, 8888, 8910, 8983, 8989, 8990, 8991, 9000, 9043, 9060, 9080, 9090, 9091, 9200, 9294, 9295, 9443, 9444, 9800, 9981, 9988, 9990, 9999, 10000, 10880, 11371, 12043, 12046, 12443, 15672, 16225, 16080, 18091, 18092, 20000, 20720, 24465, 28017, 28080, 30821, 43110, 61600] ports = {'small': small_ports, 'medium': medium_ports, 'large': large_ports} common_subnames = {'i', 'w', 'm', 'en', 'us', 'zh', 'w3', 'app', 'bbs', 'web', 'www', 'job', 'docs', 'news', 'blog', 'data', 'help', 'live', 'mall', 'blogs', 'files', 'forum', 'store', 'mobile'} # 模块API配置 # Censys可以免费注册获取API:https://censys.io/api censys_api_id = '' censys_api_secret = '' # Binaryedge可以免费注册获取API:https://app.binaryedge.io/account/api # 免费的API有效期只有1个月,到期之后可以再次生成,每月可以查询250次。 binaryedge_api = '' # Chinaz可以免费注册获取API:http://api.chinaz.com/ApiDetails/Alexa chinaz_api = '' # Bing可以免费注册获取API:https://azure.microsoft.com/zh-cn/services/ # cognitive-services/bing-web-search-api/#web-json bing_api_id = '' bing_api_key = '' # SecurityTrails可以免费注册获取API:https://securitytrails.com/corp/api securitytrails_api = '' # https://fofa.so/api fofa_api_email = '' # fofa用户邮箱 fofa_api_key = '' # fofa用户key # Google可以免费注册获取API: # 免费的API只能查询前100条结果 # https://developers.google.com/custom-search/v1/overview#search_engine_id # 创建自定义搜索引擎后需要在响应的控制面板上启用Search the entire web google_api_id = '' # Google API自定义搜索引擎id # https://developers.google.com/custom-search/v1/overview#api_key google_api_key = '' # Google API自定义搜索key # https://api.passivetotal.org/api/docs/ riskiq_api_username = '' riskiq_api_key = '' # Shodan可以免费注册获取API: https://account.shodan.io/register # 免费的API限速1秒查询1次 shodan_api_key = '' # ThreatBook API 查询子域名需要收费 https://x.threatbook.cn/nodev4/vb4/myAPI threatbook_api_key = '' # VirusTotal可以免费注册获取API: https://developers.virustotal.com/reference virustotal_api_key = '' # https://www.zoomeye.org/doc?channel=api zoomeye_api_usermail = '' zoomeye_api_password = '' # Spyse可以免费注册获取API: https://spyse.com/ spyse_api_token = '' # https://www.circl.lu/services/passive-dns/ circl_api_username = '' circl_api_password = '' # https://www.dnsdb.info/ dnsdb_api_key = '' # ipv4info可以免费注册获取API: http://ipv4info.com/tools/api/ # 免费的API有效期只有2天,到期之后可以再次生成,每天可以查询50次。 ipv4info_api_key = '' # https://github.com/360netlab/flint # passivedns_api_addr默认空使用http://api.passivedns.cn # passivedns_api_token可为空 passivedns_api_addr = '' passivedns_api_token = '' # Github Token可以访问https://github.com/settings/tokens生成,user为Github用户名 # 用于子域接管和子域收集 github_api_user = '' github_api_token = '' # obtain Cloudflare API key from https://dash.cloudflare.com/profile/api-tokens cloudflare_api_token = '' # https://hunter.qianxin.com/home/userInfo hunter_api_key = ''
nilq/baby-python
python
import logging import math import sys from datetime import timedelta from typing import TextIO, Sequence, Tuple, NoReturn import attr import httpx from cached_property import cached_property from fire import Fire @attr.s(auto_attribs=True, kw_only=True) class BaseCommand: stream: TextIO = sys.stdout def _print(self, *args): print(*args, file=self.stream) @attr.s(auto_attribs=True, kw_only=True) class SpeedTestCommand(BaseCommand): url: str = 'http://httpbin.org/status/200' count: int = 2 _bad_response: httpx.Response = None @cached_property def deltas(self) -> Tuple[timedelta, ...]: deltas = [] for _ in range(2): response = httpx.get(self.url) if response.status_code != 200: self._bad_response = response break deltas.append(response.elapsed.microseconds) return deltas @cached_property def failure(self) -> str: if self._bad_response is None: return '' return '{code} {reason}'.format( code=self._bad_response.status_code, reason=self._bad_response.reason_phrase, ) @cached_property def mean(self) -> float: return sum(self.deltas) / len(self.deltas) @cached_property def deviations(self) -> Tuple[float, ...]: return tuple(self.mean - d for d in self.deltas) @cached_property def variance(self) -> float: return sum(d ** 2 for d in self.deviations) / len(self.deviations) @cached_property def stddev(self) -> float: return math.sqrt(self.variance) def _do(self) -> int: if self.failure: self._print(self.failure) return 1 self._print('mean:', self.mean) self._print('std dev:', self.stddev) def __call__(self) -> NoReturn: sys.exit(self._do()) @attr.s(auto_attribs=True) class Commands: log_level: str = 'WARNING' _registry = dict( speed=SpeedTestCommand, ) def __call__(self): logging.getLogger().setLevel(self.log_level) return self._registry def entrypoint(argv: Sequence[str]) -> int: try: Fire(Commands, command=argv) except SystemExit as exc: return exc.code if __name__ == "__main__": sys.exit(entrypoint(sys.argv[1:]))
nilq/baby-python
python
#!/usr/bin/python3 import rospy from std_msgs.msg import Int16MultiArray, String, Bool import numpy as np import pickle import os import yaml import h5py from utils.audio import get_mfcc from utils.model import get_deep_speaker from utils.utils import batch_cosine_similarity, dist2id n_embs = 0 X = [] y = [] def save_dataset(dataset_path): predictions = np.array(X) dt = h5py.special_dtype(vlen=str) labels = np.array(y, dtype=dt) with h5py.File(dataset_path, "w") as h5f : h5f.create_dataset("predictions", data=predictions) h5f.create_dataset("labels", data=labels) def load_dataset(dataset_path): predictions = [] labels = [] if os.path.isfile(dataset_path): with h5py.File(dataset_path, 'r') as dataset: predictions = dataset['predictions'][:].tolist() labels = dataset['labels'][:].tolist() return predictions, labels def get_model(path): REF_PATH = os.path.dirname(os.path.abspath(__file__)) return get_deep_speaker(os.path.join(REF_PATH, path)) def process_audio(data, sample_rate, num_fbanks): result = np.array(data) # to float32 result = result.astype(np.float32, order='C') / 32768.0 # Processing result = get_mfcc(result, sample_rate, num_fbanks) return result def get_label_from(prediction, identification_threshold): result = None if len(X) > 0: # Distance between the sample and the support set emb_voice = np.repeat(prediction, len(X), 0) cos_dist = batch_cosine_similarity(np.array(X), emb_voice) # Matching result = dist2id(cos_dist, y, identification_threshold, mode='avg') return result def callback(audio, sample_rate, num_fbanks, speaker_model, identification_threshold, sample_phrases, identity_publisher, sample_publisher, speaker_publisher): """ Callback called each time there is a new record. Parameters ---------- audio Audio source sample_rate The number of samples of audio recorded every second. num_fbanks Number of filter banks to apply speaker_model Deep model for speaker recognition identification_threshold The min value to assign a correct prediction """ processed_audio = process_audio(audio.data, sample_rate, num_fbanks) prediction = speaker_model.predict(np.expand_dims(processed_audio, 0)) id_label = get_label_from(prediction, identification_threshold) if len(X) == 0 or id_label is None: sample_publisher.publish(True) predictions = [] predictions.append(prediction[0]) speaker_publisher.publish("I don't recognize your voice, do you want to register?") response = rospy.wait_for_message("identity_text", String) if "yes" in response.data.lower(): speaker_publisher.publish("Repeat the following sentences.") for phrase in sample_phrases: speaker_publisher.publish(phrase) result = rospy.wait_for_message("identity_data", Int16MultiArray) processed_audio = process_audio(result.data, sample_rate, num_fbanks) prediction = speaker_model.predict(np.expand_dims(processed_audio, 0)) predictions.append(prediction[0]) speaker_publisher.publish("Perfect. Tell me your name to finish the registration.") name = rospy.wait_for_message("identity_text", String) X.extend(predictions) y.extend([name.data]*len(predictions)) sample_publisher.publish(False) identity_publisher.publish(name) else: identity_publisher.publish(id_label) print("The user is:", id_label) def init_node(node_name, dataset_path, identity_topic, sample_topic, output_topic): """ Init the node. Parameters ---------- node_name Name assigned to the node """ rospy.init_node(node_name, anonymous=True) identity_publisher = rospy.Publisher(identity_topic, String, queue_size=1) sample_publisher = rospy.Publisher(sample_topic, Bool, queue_size=1) speaker_publisher = rospy.Publisher(output_topic, String, queue_size=1) rospy.on_shutdown(lambda:save_dataset(dataset_path)) predictions, labels = load_dataset(dataset_path) X.extend(predictions) y.extend(labels) return identity_publisher, sample_publisher, speaker_publisher def listener(sample_rate, num_fbanks, model_path, sample_phrases, identity_publisher, sample_publisher, speaker_publisher, identification_threshold, data_topic): """ Main function of the node. Parameters ---------- sample_rate The number of samples of audio recorded every second. num_fbanks Number of filter banks to apply model_path Path to deep model identification_threshold The min value to assign a correct prediction data_topic Topic in which is published audio data """ speaker_model = get_model(model_path) rospy.Subscriber(data_topic, Int16MultiArray, lambda audio : callback(audio, sample_rate, num_fbanks, speaker_model, identification_threshold, sample_phrases, identity_publisher, sample_publisher, speaker_publisher)) rospy.spin() if __name__ == '__main__': REF_PATH = os.path.dirname(os.path.abspath(__file__)) with open(os.path.join(REF_PATH,'config.yml')) as file: config = yaml.full_load(file) node_name = config['nodes']['reidentification'] sample_rate = config['settings']['sampleRate'] num_fbanks = config['settings']['numFbanks'] model_path = config['models']['defaults'] identification_threshold = config['settings']['identificationThreshold'] data_topic = config['topics']['voiceData'] identity_topic = config['topics']['identity'] sample_topic = config['topics']['sample'] output_topic = config['topics']['outputText'] dataset_path = os.path.join(REF_PATH, config['models']['dataset']) sample_phrases = ["how are you?", "add bread to my shopping list","change my shopping list"] identity_publisher, sample_publisher, speaker_publisher = init_node(node_name, dataset_path, identity_topic, sample_topic, output_topic) listener(sample_rate, num_fbanks, model_path, sample_phrases, identity_publisher, sample_publisher, speaker_publisher, identification_threshold, data_topic)
nilq/baby-python
python
#!/user/bin/env python '''containsAlternativeLocations.py This filter return true if this structure contains an alternative location ''' __author__ = "Mars (Shih-Cheng) Huang" __maintainer__ = "Mars (Shih-Cheng) Huang" __email__ = "marshuang80@gmail.com" __version__ = "0.2.0" __status__ = "Done" class ContainsAlternativeLocations(object): def __call__(self, t): structure = t[1] for c in structure.alt_loc_list: if c != '': return True return False
nilq/baby-python
python
import cv2 import gym import numpy as np from gym import spaces from PIL import Image from gym.wrappers.monitoring.video_recorder import VideoRecorder # https://github.com/chris-chris/mario-rl-tutorial/ class MyDownSampleWrapper(gym.ObservationWrapper): def __init__(self, env, image_size): super(MyDownSampleWrapper, self).__init__(env) self._image_size = image_size # set up a new observation space self.observation_space = gym.spaces.Box( low=0, high=255, shape=(self._image_size[1], self._image_size[0], 1), dtype=np.uint8 ) def observation(self, frame): # convert the frame from RGB to gray scale frame = cv2.cvtColor(frame, cv2.COLOR_RGB2GRAY) # resize the frame to the expected shape. #cv2.INTER_AREA #INTER_NEAREST frame = cv2.resize(frame, self._image_size) #frame = cv2.resize(frame, self._image_size) return frame[:, :, np.newaxis] class FrameMemoryWrapper(gym.ObservationWrapper): """ Use 4 Frames in observation space. """ frame_queue = None def __init__(self, env=None): super(FrameMemoryWrapper, self).__init__(env) self.frame_nr = 4 self.frame_queue = [] self.observation_space = spaces.Box(low=0, high=255, shape=(84, 84, self.frame_nr)) def _observation(self, obs): return FrameMemoryWrapper.process(obs, self.frame_queue) def _reset(self): super().reset() print('clear') self.frame_queue.clear() @staticmethod def process(img, frame_queue): # first step if len(frame_queue) == 0: frame_queue.append(img) frame_queue.append(img) frame_queue.append(img) frame_queue.append(img) obs = np.concatenate(frame_queue[-4:], axis=2) #x_t = np.reshape(x_t, (84, 84, 1)) return obs.astype(np.uint8) class VideoRecorderWrapper(gym.ObservationWrapper): """ """ def __init__(self, env=None, path=None, training_start= None, freq_episode=100): super(VideoRecorderWrapper, self).__init__(env) self.episode = 0 self.env = env self.path= path self.training_start = training_start self.freq_episode = freq_episode self.rec = None self.rec_now = False def _observation(self, obs): if self.rec_now: self.rec.capture_frame() return obs def reset(self, **kwargs): observation = self.env.reset(**kwargs) self.episode += 1 if self.rec_now: print("Stop record episode {}".format(self.episode-1)) self.rec.close() self.rec_now = False if self.episode % self.freq_episode == 0: print("Start record episode {}".format(self.episode)) path = "{}/{}_{:0>5d}.mp4".format(self.path, self.training_start, self.episode) self.rec = VideoRecorder(self.env, path=path) self.rec_now = True return observation class EpisodicLifeEnv(gym.Wrapper): def __init__(self, env): """Make end-of-life == end-of-episode, but only reset on true game over. Done by DeepMind for the DQN and co. since it helps value estimation. """ gym.Wrapper.__init__(self, env) self.lives = 0 self.was_real_done = True def step(self, action): obs, reward, done, info = self.env.step(action) self.was_real_done = done # check current lives, make loss of life terminal, # then update lives to handle bonus lives lives = self.env.unwrapped._get_life() if lives < self.lives and lives > 0: # for Qbert sometimes we stay in lives == 0 condtion for a few frames # so its important to keep lives > 0, so that we only reset once # the environment advertises done. done = True self.lives = lives return obs, reward, done, info def reset(self, **kwargs): """Reset only when lives are exhausted. This way all states are still reachable even though lives are episodic, and the learner need not know about any of this behind-the-scenes. """ if self.was_real_done: obs = self.env.reset(**kwargs) #print("was_real_done", self.env.unwrapped._get_life()) else: # no-op step to advance from terminal/lost life state obs, _, _, _ = self.env.step(0) #print("nicht was_real_done", self.env.unwrapped._get_life()) self.lives = self.env.unwrapped._get_life() return obs class MyRewardWrapper(gym.Wrapper): def __init__(self, env): gym.Wrapper.__init__(self, env) self._x_position = 0 def step(self, action): obs, reward, done, info = self.env.step(action) _x_position = self.env.unwrapped._get_x_position() _reward = _x_position - self._x_position self._x_position = _x_position # resolve an issue where after death the x position resets. The x delta # is typically has at most magnitude of 3, 5 is a safe bound if _reward < -5 or _reward > 5: _reward = 0 reward = _reward #print('reward', reward) return obs, reward, done, info class CroppingWrapper(gym.ObservationWrapper): """ """ def __init__(self, env=None): super(CroppingWrapper, self).__init__(env) self.env = env self.new_size = (20,20) self.observation_space = gym.spaces.Box( low=0, high=255, shape=(self.new_size[1], self.new_size[0], 1), dtype=np.uint8 ) def _observation(self, obs): #x_t = cv2.resize(obs, (84, 84), interpolation=cv2.INTER_AREA) #x_t = np.reshape(x_t, (84, 84, 1)) #x_t = np.nan_to_num(x_t) #return x_t.astype(np.uint8) #obs.resize((32,32)) #size = (16,16) #obs.resize(size) obs = obs[10:30, 10:30, :] # 32x32 -> 20x20 obs = np.array(obs) #obs = obs.shape((20,20,1)) #obs2 = np.copy(obs) #obs2.resize(self.new_size) #img = Image.fromarray(obs) #cv2.imshow('image', obs2) #cv2.waitKey(1) #img.save('testaww_{}.png'.format(self.new_size)) return obs
nilq/baby-python
python
from CameraOrMarker import * class Scenarios: def __init__(self, scenario): self.num_generated_samples = 217 self.std_corners_f_image = 0.3 self.marker_len = 0.162717998 # generate_solve_pnp_inputs options self.use_sigma_points = False self.use_dist_params = True # Pick some camera calibration values self.camera_matrix = np.array([ [921.17070200000001, 0., 459.90435400000001], [0., 919.01837699999999, 351.23830099999998], [0., 0., 1.] ]) self.dist_coeffs = np.array([-0.033458000000000002, 0.105152, 0.001256, -0.0066470000000000001, 0.]) self.rpy_world_marker_a = (np.pi / 2., 0., -np.pi / 2.) self.xyz_world_marker_a = (0., -.5, 1.) self.rpy_world_marker_b = (np.pi / 2., 0., -np.pi / 2.) self.xyz_world_marker_b = (0., .5, 1.) self.rpy_world_marker = self.rpy_world_marker_a self.xyz_world_marker = self.xyz_world_marker_a self.rpy_world_camera_a = (-np.pi / 2, 0., -np.pi / 2) self.xyz_world_camera_a = (-2.5, -.5, 1.5) self.rpy_world_camera_b = (-np.pi / 2, 0., -np.pi / 2) self.xyz_world_camera_b = (-2.5, 0.5, .5) self.rpy_world_camera = self.rpy_world_camera_a self.xyz_world_camera = self.xyz_world_camera_a self.ident = "looking along x" if scenario == 1: self.rpy_world_marker_a = (0., 0., 0.) self.xyz_world_marker_a = (0., 0., 1.) self.rpy_world_marker_b = (0., 0., 0.) self.xyz_world_marker_b = (0., 0., 1.) self.rpy_world_camera = (0., 0., 0.) self.xyz_world_camera = (-1., 0., -1.5) self.ident = "looking up" @property def marker_a(self): return CameraOrMarker.marker_from_rpy(self.rpy_world_marker_a, self.xyz_world_marker_a) @property def marker_b(self): return CameraOrMarker.marker_from_rpy(self.rpy_world_marker_b, self.xyz_world_marker_b) @property def camera_a(self): return CameraOrMarker.camera_from_rpy(self.rpy_world_camera_a, self.xyz_world_camera_a) @property def camera_b(self): return CameraOrMarker.camera_from_rpy(self.rpy_world_camera_b, self.xyz_world_camera_b) def as_param_str(self): return "std_corners={} dist_params={} sigma_points={}".format( self.std_corners_f_image, self.use_dist_params, self.use_sigma_points)
nilq/baby-python
python
"""Test SQL database migrations.""" from pathlib import Path from typing import Generator import pytest import sqlalchemy from pytest_lazyfixture import lazy_fixture # type: ignore[import] from robot_server.persistence.database import create_sql_engine from robot_server.persistence.tables import ( migration_table, run_table, action_table, protocol_table, analysis_table, ) TABLES = [run_table, action_table, protocol_table, analysis_table] @pytest.fixture def database_v0(tmp_path: Path) -> Path: """Create a database matching schema version 0.""" db_path = tmp_path / "migration-test-v0.db" sql_engine = create_sql_engine(db_path) sql_engine.execute("DROP TABLE migration") sql_engine.execute("DROP TABLE run") sql_engine.execute( """ CREATE TABLE run ( id VARCHAR NOT NULL, created_at DATETIME NOT NULL, protocol_id VARCHAR, PRIMARY KEY (id), FOREIGN KEY(protocol_id) REFERENCES protocol (id) ) """ ) sql_engine.dispose() return db_path @pytest.fixture def database_v1(tmp_path: Path) -> Path: """Create a database matching schema version 1.""" db_path = tmp_path / "migration-test-v1.db" sql_engine = create_sql_engine(db_path) sql_engine.dispose() return db_path @pytest.fixture def subject(database_path: Path) -> Generator[sqlalchemy.engine.Engine, None, None]: """Get a SQLEngine test subject. The tests in this suite will use this SQLEngine to test that migrations happen properly. For other tests, the `sql_engine` fixture in `conftest.py` should be used, instead. """ engine = create_sql_engine(database_path) yield engine engine.dispose() @pytest.mark.parametrize( "database_path", [ lazy_fixture("database_v0"), lazy_fixture("database_v1"), ], ) def test_migration(subject: sqlalchemy.engine.Engine) -> None: """It should migrate a table.""" migrations = subject.execute(sqlalchemy.select(migration_table)).all() assert [m.version for m in migrations] == [1] # all table queries work without raising for table in TABLES: values = subject.execute(sqlalchemy.select(table)).all() assert values == []
nilq/baby-python
python
# coding: utf8 from __future__ import unicode_literals from ...symbols import LEMMA, PRON_LEMMA MORPH_RULES = { "PRON": { "jeg": {LEMMA: PRON_LEMMA, "PronType": "Prs", "Person": "One", "Number": "Sing", "Case": "Nom"}, "mig": {LEMMA: PRON_LEMMA, "PronType": "Prs", "Person": "One", "Number": "Sing", "Case": "Acc"}, "du": {LEMMA: PRON_LEMMA, "PronType": "Prs", "Person": "Two"}, "han": {LEMMA: PRON_LEMMA, "PronType": "Prs", "Person": "Three", "Number": "Sing", "Gender": "Masc", "Case": "Nom"}, "ham": {LEMMA: PRON_LEMMA, "PronType": "Prs", "Person": "Three", "Number": "Sing", "Gender": "Masc", "Case": "Acc"}, "hun": {LEMMA: PRON_LEMMA, "PronType": "Prs", "Person": "Three", "Number": "Sing", "Gender": "Fem", "Case": "Nom"}, "hende": {LEMMA: PRON_LEMMA, "PronType": "Prs", "Person": "Three", "Number": "Sing", "Gender": "Fem", "Case": "Acc"}, "den": {LEMMA: PRON_LEMMA, "PronType": "Prs", "Person": "Three", "Number": "Sing", "Gender": "Neut"}, "det": {LEMMA: PRON_LEMMA, "PronType": "Prs", "Person": "Three", "Number": "Sing", "Gender": "Neut"}, "vi": {LEMMA: PRON_LEMMA, "PronType": "Prs", "Person": "One", "Number": "Plur", "Case": "Nom"}, "os": {LEMMA: PRON_LEMMA, "PronType": "Prs", "Person": "One", "Number": "Plur", "Case": "Acc"}, "de": {LEMMA: PRON_LEMMA, "PronType": "Prs", "Person": "Three", "Number": "Plur", "Case": "Nom"}, "dem": {LEMMA: PRON_LEMMA, "PronType": "Prs", "Person": "Three", "Number": "Plur", "Case": "Acc"}, "min": {LEMMA: PRON_LEMMA, "PronType": "Prs", "Person": "One", "Number": "Sing", "Poss": "Yes", "Reflex": "Yes"}, "din": {LEMMA: PRON_LEMMA, "PronType": "Prs", "Person": "Two", "Number": "Sing", "Poss": "Yes", "Reflex": "Yes"}, "hans": {LEMMA: PRON_LEMMA, "PronType": "Prs", "Person": "Three", "Number": "Sing", "Gender": "Masc", "Poss": "Yes", "Reflex": "Yes"}, "hendes": {LEMMA: PRON_LEMMA, "PronType": "Prs", "Person": "Three", "Number": "Sing", "Gender": "Fem", "Poss": "Yes", "Reflex": "Yes"}, "dens": {LEMMA: PRON_LEMMA, "PronType": "Prs", "Person": "Three", "Number": "Sing", "Gender": "Neut", "Poss": "Yes", "Reflex": "Yes"}, "dets": {LEMMA: PRON_LEMMA, "PronType": "Prs", "Person": "Three", "Number": "Sing", "Gender": "Neut", "Poss": "Yes", "Reflex": "Yes"}, "vores": {LEMMA: PRON_LEMMA, "PronType": "Prs", "Person": "One", "Number": "Plur", "Poss": "Yes", "Reflex": "Yes"}, "deres": {LEMMA: PRON_LEMMA, "PronType": "Prs", "Person": "Three", "Number": "Plur", "Poss": "Yes", "Reflex": "Yes"}, }, "VERB": { "er": {LEMMA: "være", "VerbForm": "Fin", "Tense": "Pres"}, "var": {LEMMA: "være", "VerbForm": "Fin", "Tense": "Past"} } } for tag, rules in MORPH_RULES.items(): for key, attrs in dict(rules).items(): rules[key.title()] = attrs
nilq/baby-python
python
# vim: set sw=4 ts=4 expandtab : from const import * import math from scipy import stats def teller(state, nstate): nchain = nstate['chain'] # mimic network unpredictability using random variable df = int(math.log10(nchain['txpending'] + 10)) rv = stats.chi2(df) #lazy_txs = int(0.01 * nchain['txpending'] * rv.rvs() / df) lazy_txs = int(0.1 * nchain['txpending']) #lazy_txs = min(lazy_txs, nchain['txpending']) # compensate rv.rvs() tx_to_process = min( nchain['txpending'] - lazy_txs, param['blktxsize'] * config['stepblks'] ) nchain['stat_txproc'] = tx_to_process nchain['txpending'] -= tx_to_process # reward reward = int(tx_to_process * param['txreward']) #reward = int(tx_to_process * param['txreward'] * 10) #reward = int(config['stepblks']*10*moteperamo) nchain['coins'] += reward nchain['coins_active'] += reward def depleter(state, nstate): chain = state['chain'] # asset loss depletion = int(chain['coins_active'] * param['deplete_coin']) nstate['chain']['coins_active'] -= depletion nstate['chain']['coins_lost'] += depletion # tx loss txlost = int(chain['txpending'] * param['deplete_tx']) nstate['chain']['accum_txlost'] += txlost nstate['chain']['txpending'] -= txlost # invisible hand to intermediate supply and demand def invisible(state, nstate): chain = state['chain'] market = state['market'] # get usdperamo if len(hist['exch']) == 0: avg_exch = market['exchange_rate'] else: avg_exch = sum(hist['exch']) / len(hist['exch']) usdperamo = avg_exch # update market value tmp = market['value'] ## method 1 #tmp *= math.pow(param['growth_factor'], config['stepblks'] / BLKSDAY) #tmp = max(tmp, 0.001) #market['value'] = tmp ## method 2 #tmp *= math.pow(0.3, chain['blks'] / BLKSMONTH) #market['value'] += tmp ## method 3 x = chain['blks'] / BLKSMONTH value = param['f_gdp_month'][3] * x**3 \ + param['f_gdp_month'][2] * x**2 \ + param['f_gdp_month'][1] * x \ + param['f_gdp_month'][0] ## others #tmp *= math.log10(market['liveness'] + 1) + 1 #tmp *= param['growth_factor'] #tmp *= param['growth_factor'] / (fee_usd + param['feescale']) #tmp *= math.pow(param['growth_factor'], config['stepblks'] / BLKSMONTH) nstate['market']['value'] = max(value, 0) # update tx fee # estimate remaining blocks until all of the currently pending txs would be # processed # one param['feescale'] USD for one hour avg_pending = sum(hist['txpending']) / len(hist['txpending']) blks = avg_pending / param['blktxsize'] fee_usd = param['feescale'] * blks / BLKSHOUR fee = int(fee_usd / usdperamo * moteperamo) # update nstate['chain']['txfee'] = fee #smooth = max(int(config['smooth'] / config['stepblks']), 2) #chain['txfee'] = int((fee + (smooth-1)*chain['txfee']) / smooth) # TODO: consider these (when assessing demand): # - expected (real) rate of return # - current exchage rate # - expected exchange rate in the future # - current interest rate # - risk and uncertainty # - liquidity # update exchange rate demand = 0 supply = 0 ## money demand for market trade v = param['velocity'] coin_value = chain['coins_active'] / moteperamo * usdperamo market_value = market['value'] demand += market_value / v supply += coin_value ## money demand for storing value #sc = chain['stakes'] #demand += (market['interest_stake'] * sc) / market['interest_world'] - sc ## money demand from short-term negative feedback fb = chain['coins_active'] / moteperamo / 10 \ * (avg_exch - market['exchange_rate']) if fb > 0: demand += fb else: supply += -fb ## money demand from long-term expectation #f = chain['coins'] / moteperamo \ # * (market['exchange_rate'] - avg_exch) #if f > 0: # demand += f #else: # supply += -f ## sum up # avoid infinity demand = min(demand, chain['coins'] / moteperamo * usdperamo) # avoid divide-by-zero error supply = max(supply, DELTA_AMO) exch = demand / supply ## smoothing smooth = max(int(config['smooth'] / config['stepblks']), 2) old = market['exchange_rate'] nstate['market']['exchange_rate'] = (exch + (smooth-1)*old) / smooth hist_size = 1 hist = { 'txproc': [0], 'txpending': [0], 'txfee': [0], 'stakes': [0], 'exch': [], } def historian(state): hist['txproc'].append(state['chain']['stat_txproc']) hist['txpending'].append(state['chain']['txpending']) hist['txfee'].append(state['chain']['txfee']) hist['stakes'].append(state['chain']['stakes']) hist['exch'].append(state['market']['exchange_rate']) l = len(hist['txpending']) if l > hist_size: hist['txproc'] = hist['txproc'][l-hist_size:] hist['txpending'] = hist['txpending'][l-hist_size:] hist['txfee'] = hist['txfee'][l-hist_size:] hist['stakes'] = hist['stakes'][l-hist_size:] hist['exch'] = hist['exch'][l-hist_size:]
nilq/baby-python
python
from mechanize import Browser from bs4 import BeautifulSoup import sys import re br = Browser() br.set_handle_robots(False) br.addheaders = [("User-agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4275.0 Safari/537.36")] def product_search(keyword): br.open("https://www.amazon.co.jp/", timeout=10.0) br.select_form(action="/s/ref=nb_sb_noss") search_box = br.form.find_control(id="twotabsearchtextbox") search_box.value = keyword ret = br.submit() bs = BeautifulSoup(ret, 'html.parser') URLs = bs.select(".a-size-base.a-link-normal.a-text-normal") items = bs.select(".a-size-base-plus.a-color-base.a-text-normal") fees = bs.select(".a-size-base.a-link-normal.a-text-normal") images = bs.select(".a-section.aok-relative.s-image-square-aspect") url_list = [] item_list = [] fee_list = [] image_list = [] for image in images: image_list.append(image.find("img").get('src')) for item in items: item_list.append(item.string) for fee in fees: fee_list.append(fee.find("span", class_="a-price-whole").string) for URL in URLs: try: URL = re.search(r'dp(?:%2Fproduct-description)?%2F([0-9a-zA-Z]{10})', URL.get('href')).group().replace('%2F', '/') url_list.append(re.sub('^', 'https://www.amazon.co.jp/', URL)) except: URL = re.search(r'dp(?:%2Fproduct-description)?/([0-9a-zA-Z]{10})', URL.get('href')).group() url_list.append(re.sub('^', 'https://www.amazon.co.jp/', URL)) product_list = list(zip(item_list,fee_list,url_list,image_list)) print(product_list) product_search(sys.argv[1])
nilq/baby-python
python
from .test_languages import TestLanguages from .test_translator import TestTranslator
nilq/baby-python
python
from setuptools import setup, find_packages PACKAGES = find_packages() with open("README.md", "r") as fh: long_description = fh.read() setup(name='python-neurostore', version='0.1', description='NeuroStore API wrapper', long_description=long_description, long_description_content_type="text/markdown", url='http://github.com/neurostuff/python-neurostore', author='Alejandro de la Vega', author_email='aleph4@gmail.com', install_requires=['requests>=2.21', 'pyjwt~=1.7.1', 'requests-oauthlib'], license='MIT', packages=PACKAGES, classifiers=[ "Programming Language :: Python :: 3", "License :: OSI Approved :: MIT License", "Operating System :: OS Independent", ], zip_safe=False)
nilq/baby-python
python
#!/usr/bin/env python # ---------------------------------------------------------------------------- # Pyglet GLSL Demo Dot3 Bumpmap Shader on http://www.pythonstuff.org # pythonian_at_inode_dot_at (c) 2010 # # based on the "graphics.py" batch/VBO demo by # pyglet # Copyright (c) 2006-2008 Alex Holkner # All rights reserved. # # Redistribution and use in source and binary forms, with or without # modification, are permitted provided that the following conditions # are met: # # * Redistributions of source code must retain the above copyright # notice, this list of conditions and the following disclaimer. # * Redistributions in binary form must reproduce the above copyright # notice, this list of conditions and the following disclaimer in # the documentation and/or other materials provided with the # distribution. # * Neither the name of pyglet nor the names of its # contributors may be used to endorse or promote products # derived from this software without specific prior written # permission. # # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS # FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE # COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, # INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, # BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; # LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN # ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE # POSSIBILITY OF SUCH DAMAGE. # ---------------------------------------------------------------------------- '''This expands the previous example by parallax by heightfield mapping the bumpmapped texture Find more GLSL-examples http://www.pythonstuff.org ''' html = ''' <font size=+3 color=#FF3030> <b>Pyglet GLSL Parallax Mapping Demo</b> </font><br/> <font size=+2 color=#00FF60> P, O = Parallax inc/dec<br/> B = Bumpmap on/off<br/> ENTER = Shader on/off<br/> R = Reset<br/> Q, Esc = Quit<br/> F = Toggle Figure<br/> T = Toggle Texture<br/> W, S, A, D = Up, Down, Left, Right<br/> Space = Move/Stop<br/> Arrows = Move Light 0<br/> H = This Help<br/> </font> ''' from math import pi, sin, cos, sqrt from euclid import * import pyglet from pyglet.gl import * from pyglet.window import key from pyglet import image, resource from shader import Shader resource.path.append('textures') resource.reindex() texturecnt = 3 # Texturemap0.jpg = Colormap Texturemap1.jpg = Bumpmap Texturemap2.jpg = Heightmap try: # Try and create a window with multisampling (antialiasing) config = Config(sample_buffers=1, samples=4, depth_size=16, double_buffer=True,) window = pyglet.window.Window(resizable=True, config=config, vsync=False) # "vsync=False" to check the framerate except pyglet.window.NoSuchConfigException: # Fall back to no multisampling for old hardware window = pyglet.window.Window(resizable=True) label = pyglet.text.HTMLLabel(html, # location=location, width=window.width//2, multiline=True, anchor_x='center', anchor_y='center') fps_display = pyglet.clock.ClockDisplay() # see programming guide pg 48 @window.event def on_resize(width, height): if height==0: height=1 # Keep text vertically centered in the window label.y = window.height // 2 # Override the default on_resize handler to create a 3D projection glViewport(0, 0, width, height) glMatrixMode(GL_PROJECTION) glLoadIdentity() gluPerspective(60., width / float(height), .1, 1000.) glMatrixMode(GL_MODELVIEW) return pyglet.event.EVENT_HANDLED def update(dt): global autorotate global rot global dist if autorotate: rot += Vector3(0.1, 12, 5) * dt rot.x %= 360 rot.y %= 360 rot.z %= 360 pyglet.clock.schedule(update) def dismiss_dialog(dt): global showdialog showdialog = False pyglet.clock.schedule_once(dismiss_dialog, 10.0) # Define a simple function to create ctypes arrays of floats: def vec(*args): return (GLfloat * len(args))(*args) @window.event def on_draw(): glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT) glLoadIdentity() glLoadIdentity() glTranslatef(0.0, 0.0, dist); glRotatef(rot.x, 0, 0, 1) glRotatef(rot.y, 0, 1, 0) glRotatef(rot.z, 1, 0, 0) glPolygonMode(GL_FRONT, GL_FILL) if shaderon: # bind our shader shader.bind() shader.uniformi('toggletexture', toggletexture ) shader.uniformi('togglebump', togglebump ) shader.uniformf('parallaxheight', parallaxheight ) for i in range(texturecnt): glActiveTexture(GL_TEXTURE0+i) glEnable(GL_TEXTURE_2D) glBindTexture(GL_TEXTURE_2D, texture[i].id) shader.uniformi('my_color_texture[' + str(i) + ']',i ) if togglefigure: batch1.draw() else: batch2.draw() for i in range(texturecnt): glActiveTexture(GL_TEXTURE0+i) glDisable(GL_TEXTURE_2D) shader.unbind() else: if togglefigure: batch1.draw() else: batch2.draw() glActiveTexture(GL_TEXTURE0) glEnable(GL_TEXTURE_2D) glDisable(GL_LIGHTING) glDisable(GL_DEPTH_TEST) if showdialog: glLoadIdentity() glTranslatef(0, -200, -450) label.draw() glLoadIdentity() glTranslatef(250, -290, -500) fps_display.draw() glEnable(GL_DEPTH_TEST) glEnable(GL_LIGHTING) glDisable(GL_TEXTURE_2D) @window.event def on_key_press(symbol, modifiers): global autorotate global rot global dist global togglefigure global toggletexture global togglebump global parallaxheight global light0pos global light1pos global showdialog global shaderon if symbol == key.R: print 'Reset' rot = Vector3(0, 0, 0) elif symbol == key.ESCAPE or symbol == key.Q: print 'Good Bye !' # ESC would do it anyway, but not "Q" pyglet.app.exit() return pyglet.event.EVENT_HANDLED elif symbol == key.H: showdialog = not showdialog elif symbol == key.ENTER: print 'Shader toggle' shaderon = not shaderon elif symbol == key.SPACE: print 'Toggle autorotate' autorotate = not autorotate elif symbol == key.F: togglefigure = not togglefigure print 'Toggle Figure ', togglefigure elif symbol == key.B: togglebump = not togglebump print 'Toggle Bumpmap ', togglebump elif symbol == key.P: parallaxheight += 0.01 print 'Parallax Height now ', parallaxheight elif symbol == key.O: parallaxheight -= 0.01 if parallaxheight <= 0.0: parallaxheight = 0.0 print 'Parallax now OFF' else: print 'Parallax Height now ', parallaxheight elif symbol == key.PLUS: dist += 0.5 print 'Distance now ', dist elif symbol == key.MINUS: dist -= 0.5 print 'Distance now ', dist elif symbol == key.T: toggletexture = not toggletexture print 'Toggle Texture ', toggletexture elif symbol == key.A: print 'Stop left' if autorotate: autorotate = False else: rot.y += -rotstep rot.y %= 360 elif symbol == key.S: print 'Stop down' if autorotate: autorotate = False else: rot.z += rotstep rot.z %= 360 elif symbol == key.W: print 'Stop up' if autorotate: autorotate = False else: rot.z += -rotstep rot.z %= 360 elif symbol == key.D: print 'Stop right' if autorotate: autorotate = False else: rot.y += rotstep rot.y %= 360 elif symbol == key.LEFT: print 'Light0 rotate left' tmp = light0pos[0] light0pos[0] = tmp * cos( lightstep ) - light0pos[2] * sin( lightstep ) light0pos[2] = light0pos[2] * cos( lightstep ) + tmp * sin( lightstep ) glLoadIdentity() glLightfv(GL_LIGHT0, GL_POSITION, vec(*light0pos)) elif symbol == key.RIGHT: print 'Light0 rotate right' tmp = light0pos[0] light0pos[0] = tmp * cos( -lightstep ) - light0pos[2] * sin( -lightstep ) light0pos[2] = light0pos[2] * cos( -lightstep ) + tmp * sin( -lightstep ) glLoadIdentity() glLightfv(GL_LIGHT0, GL_POSITION, vec(*light0pos)) elif symbol == key.UP: print 'Light0 up' tmp = light0pos[1] light0pos[1] = tmp * cos( -lightstep ) - light0pos[2] * sin( -lightstep ) light0pos[2] = light0pos[2] * cos( -lightstep ) + tmp * sin( -lightstep ) glLoadIdentity() glLightfv(GL_LIGHT0, GL_POSITION, vec(*light0pos)) elif symbol == key.DOWN: print 'Light0 down' tmp = light0pos[1] light0pos[1] = tmp * cos( lightstep ) - light0pos[2] * sin( lightstep ) light0pos[2] = light0pos[2] * cos( lightstep ) + tmp * sin( lightstep ) glLoadIdentity() glLightfv(GL_LIGHT0, GL_POSITION, vec(*light0pos)) else: print 'OTHER KEY' def setup(): # One-time GL setup global light0pos global light1pos global toggletexture global togglebump global parallaxheight global texture light0pos = [20.0, 20.0, 20.0, 1.0] # positional light ! light1pos = [-20.0, -20.0, 20.0, 0.0] # infinitely away light ! glClearColor(1, 1, 1, 1) glColor4f(1.0, 1.0, 1.0, 1.0 ) glEnable(GL_DEPTH_TEST) glEnable(GL_CULL_FACE) texture = [] for i in range (texturecnt): texturefile = 'Texturemap' + str(i) + '.jpg' print "Loading Texture", texturefile textureSurface = pyglet.resource.texture(texturefile) texture.append( textureSurface.get_texture() ) glBindTexture(texture[i].target, texture[i].id) print "Texture ", i, " bound to ", texture[i].id # Uncomment this line for a wireframe view #glPolygonMode(GL_FRONT_AND_BACK, GL_LINE) glEnable(GL_BLEND) glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA) # Simple light setup. On Windows GL_LIGHT0 is enabled by default, # but this is not the case on Linux or Mac, so remember to always # include it. glEnable(GL_LIGHTING) glEnable(GL_LIGHT0) glEnable(GL_LIGHT1) glLightfv(GL_LIGHT0, GL_POSITION, vec(*light0pos)) glLightfv(GL_LIGHT0, GL_AMBIENT, vec(0.3, 0.3, 0.3, 1.0)) glLightfv(GL_LIGHT0, GL_DIFFUSE, vec(0.9, 0.9, 0.9, 1.0)) glLightfv(GL_LIGHT0, GL_SPECULAR, vec(1.0, 1.0, 1.0, 1.0)) glLightfv(GL_LIGHT1, GL_POSITION, vec(*light1pos)) glLightfv(GL_LIGHT1, GL_DIFFUSE, vec(.6, .6, .6, 1.0)) glLightfv(GL_LIGHT1, GL_SPECULAR, vec(1.0, 1.0, 1.0, 1.0)) glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, vec(0.5, 0.5, 0.5, 1.0)) glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, vec(1, 1, 1, 1)) glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, 50) # create our Phong Shader by Jerome GUINOT aka 'JeGX' - jegx [at] ozone3d [dot] net # see http://www.ozone3d.net/tutorials/glsl_lighting_phong.php shader = Shader([''' varying vec3 lightDir0, lightDir1, eyeVec; varying vec3 normal, tangent, binormal; void main() { // Create the Texture Space Matrix normal = normalize(gl_NormalMatrix * gl_Normal); tangent = normalize(gl_NormalMatrix * (gl_Color.rgb - 0.5)); binormal = cross(normal, tangent); mat3 TBNMatrix = mat3(tangent, binormal, normal); vec3 vVertex = vec3(gl_ModelViewMatrix * gl_Vertex); lightDir0 = vec3(gl_LightSource[0].position.xyz - vVertex) * TBNMatrix; lightDir1 = vec3(gl_LightSource[1].position.xyz - vVertex) * TBNMatrix; eyeVec = -vVertex * TBNMatrix; gl_Position = ftransform(); gl_TexCoord[0] = gl_TextureMatrix[0] * gl_MultiTexCoord0; } '''], [''' varying vec3 normal, lightDir0, lightDir1, eyeVec; uniform sampler2D my_color_texture['''+str(texturecnt)+''']; //0 = ColorMap, 1 = NormalMap, 2 = HeightMap uniform int toggletexture; // false/true uniform int togglebump; // false/true uniform float parallaxheight; void main (void) { // Compute parallax displaced texture coordinates vec3 eye = normalize(-eyeVec); vec2 offsetdir = vec2( eye.x, eye.y ); vec2 coords1 = gl_TexCoord[0].st; float height1 = parallaxheight * (texture2D( my_color_texture[2], coords1).r - 0.5); vec2 offset1 = height1 * offsetdir; vec2 coords2 = coords1 + offset1; float height2 = parallaxheight * (texture2D( my_color_texture[2], coords2).r - 0.5); //vec2 offset2 = height2 * offsetdir; vec2 newCoords = coords2; if ( length( offset1 ) > 0.001 ) // 5.0 * abs( height1 ) > abs( height2 ) ) newCoords = coords1 + (height2/height1) * offset1; vec4 texColor = vec4(texture2D(my_color_texture[0], newCoords).rgb, 1.0); vec3 norm = normalize( texture2D(my_color_texture[1], newCoords).rgb - 0.5); if ( toggletexture == 0 ) texColor = gl_FrontMaterial.ambient; vec4 final_color = (gl_FrontLightModelProduct.sceneColor * vec4(texColor.rgb,1.0)) + (gl_LightSource[0].ambient * vec4(texColor.rgb,1.0)) + (gl_LightSource[1].ambient * vec4(texColor.rgb,1.0)); vec3 N = (togglebump != 0) ? normalize(norm) : vec3(0.0, 0.0, 1.0 ); vec3 L0 = normalize(lightDir0); vec3 L1 = normalize(lightDir1); float lambertTerm0 = dot(N,L0); float lambertTerm1 = dot(N,L1); if(lambertTerm0 > 0.0) { final_color += gl_LightSource[0].diffuse * gl_FrontMaterial.diffuse * lambertTerm0; vec3 E = normalize(eyeVec); vec3 R = reflect(-L0, N); float specular = pow( max(dot(R, E), 0.0), gl_FrontMaterial.shininess ); final_color += gl_LightSource[0].specular * gl_FrontMaterial.specular * specular; } if(lambertTerm1 > 0.0) { final_color += gl_LightSource[1].diffuse * gl_FrontMaterial.diffuse * lambertTerm1; vec3 E = normalize(eyeVec); vec3 R = reflect(-L1, N); float specular = pow( max(dot(R, E), 0.0), gl_FrontMaterial.shininess ); final_color += gl_LightSource[1].specular * gl_FrontMaterial.specular * specular; } gl_FragColor = final_color; } ''']) class Torus(object): list = None def __init__(self, radius, inner_radius, slices, inner_slices, batch, group=None): # Create the vertex and normal arrays. vertices = [] normals = [] textureuvw = [] tangents = [] u_step = 2 * pi / (slices - 1) v_step = 2 * pi / (inner_slices - 1) u = 0. for i in range(slices): cos_u = cos(u) sin_u = sin(u) v = 0. for j in range(inner_slices): cos_v = cos(v) sin_v = sin(v) d = (radius + inner_radius * cos_v) x = d * cos_u y = inner_radius * sin_v z = -d * sin_u nx = cos_u * cos_v ny = sin_v nz = -sin_u * cos_v n = sqrt( nx * nx + ny * ny + nz * nz ) if n < 0.99 or n > 1.01: nx = nx / n ny = ny / n nz = nz / n print "Torus: N normalized" tx = -sin_u ty = 0 tz = -cos_u a = sqrt( tx * tx + ty * ty + tz * tz ) if a > 0.001: tx = tx / a ty = ty / a tz = tz / a vertices.extend([x, y, z]) normals.extend([nx, ny, nz]) textureuvw.extend([u / (2.0 * pi), v / (2.0 * pi), 0.0]) tangents.extend([ int(round(255 * (0.5 - 0.5 * tx))), int(round(255 * (0.5 - 0.5 * ty))), int(round(255 * (0.5 - 0.5 * tz))) ]) v += v_step u += u_step # Create a list of triangle indices. indices = [] for i in range(slices - 1): for j in range(inner_slices - 1): p = i * inner_slices + j indices.extend([p, p + inner_slices, p + inner_slices + 1]) indices.extend([p, p + inner_slices + 1, p + 1]) self.vertex_list = batch.add_indexed(len(vertices)//3, GL_TRIANGLES, group, indices, ('v3f/static', vertices), ('n3f/static', normals), ('t3f/static', textureuvw), ('c3B/static', tangents)) def delete(self): self.vertex_list.delete() class Sphere(object): list = None def __init__(self, radius, slices, batch, group=None): # Create the vertex and normal arrays. vertices = [] normals = [] textureuvw = [] tangents = [] u_step = 2 * pi / (slices - 1) v_step = pi / (slices - 1) u = 0. for i in range(slices): cos_u = cos(u) sin_u = sin(u) v = 0. for j in range(slices): cos_v = cos(v) sin_v = sin(v) nx = sin_v * cos_u ny = -cos_v nz = -sin_v * sin_u n = sqrt( nx * nx + ny * ny + nz * nz ) if n < 0.99 or n > 1.01: nx = nx / n ny = ny / n nz = nz / n print "Sphere: N normalized" tx = nz ty = 0 tz = -nx a = sqrt( tx * tx + ty * ty + tz * tz ) if a > 0.001: tx = tx / a ty = ty / a tz = tz / a x = radius * nx y = radius * ny z = radius * nz vertices.extend([x, y, z]) normals.extend([nx, ny, nz]) textureuvw.extend([u / (2 * pi), v / (pi), 0.0]) tangents.extend([ int(round(255 * (0.5 - 0.5 * tx))), int(round(255 * (0.5 - 0.5 * ty))), int(round(255 * (0.5 - 0.5 * tz))) ]) v += v_step u += u_step # Create a list of triangle indices. indices = [] for i in range(slices - 1): for j in range(slices - 1): p = i * slices + j indices.extend([p, p + slices, p + slices + 1]) indices.extend([p, p + slices + 1, p + 1]) self.vertex_list = batch.add_indexed(len(vertices)//3, GL_TRIANGLES, group, indices, ('v3f/static', vertices), ('n3f/static', normals), ('t3f/static', textureuvw), ('c3B/static', tangents)) def delete(self): self.vertex_list.delete() dist = -3.5 rot = Vector3(0, 0, 0) autorotate = True rotstep = 10 lightstep = 10 * pi/180 togglefigure = False toggletexture = True togglebump = True parallaxheight = 0.02 showdialog = True shaderon = True setup() batch1 = pyglet.graphics.Batch() torus = Torus(1, 0.3, 80, 25, batch=batch1) batch2 = pyglet.graphics.Batch() sphere = Sphere(1.2, 50, batch=batch2) pyglet.app.run() #thats all
nilq/baby-python
python
""" The wntr.metrics.hydraulic module contains hydraulic metrics. """ import wntr.network from wntr.network.graph import _all_simple_paths #from wntr.metrics.misc import _average_attribute import numpy as np import pandas as pd import networkx as nx import math from collections import Counter import logging logger = logging.getLogger(__name__) def fdv(node_results, average_times=False, average_nodes=False): """ Compute fraction delivered volume (FDV), equations modified from [1]. The metric can be averaged over times and/or nodes. Parameters ---------- node_results : pd.Panel A pandas Panel containing node results. Items axis = attributes, Major axis = times, Minor axis = node names FDV uses 'expected demand' and 'demand' attrbutes. average_times : bool (default = False) Flag to determine if calculations are to be averaged over each time step. If false, FDV calculations will be performed for each time step. If true, FDV calculations will be averaged over all time steps. average_nodes : bool (default = False) Flag to determine if calculations are to be averaged over each node. If false, FDV calculations will be performed for each node. If true, FDV calculations will be averaged over all nodes. Returns ------- fdv : pd.DataFrame, pd.Series, or scalar (depending on node and time averaging) Fraction of delivered volume References ---------- [1] Ostfeld A, Kogan D, Shamir U. (2002). Reliability simulation of water distribution systems - single and multiquality, Urban Water, 4, 53-61 """ exp_demand = _average_attribute(node_results['expected_demand'], average_times, average_nodes) act_received = _average_attribute(node_results['demand'], average_times, average_nodes) # Calculate FDV fdv = act_received / exp_demand # Replace NaNs (generated by nodes with 0 demand) try: fdv = fdv.fillna(1) except: if exp_demand == 0: fdv = 1 return fdv def fdd(node_results, Dstar, average_times=False, average_nodes=False): """ Compute fraction delivered demand (FDD), equations modified from [1]. The metric can be averaged over times and/or nodes. Parameters ---------- node_results : pd.Panel A pandas Panel containing node results. Items axis = attributes, Major axis = times, Minor axis = node names FDD uses 'expected demand' and 'demand' attrbutes. Dstar : float Threshold demand factor average_times : bool (default = False) Flag to determine if calculations are to be averaged over each time step. If false, FDV calculations will be performed for each time step. If true, FDV calculations will be averaged over all time steps. average_nodes : bool (default = False) Flag to determine if calculations are to be averaged over each node. If false, FDV calculations will be performed for each node. If true, FDV calculations will be averaged over all nodes. Returns ------- fdd : pd.DataFrame, pd.Series, or scalar (depending on node and time averaging) Fraction of delivered demand References ---------- [1] Ostfeld A, Kogan D, Shamir U. (2002). Reliability simulation of water distribution systems - single and multiquality, Urban Water, 4, 53-61 """ fdv_metric = fdv(node_results, average_times, average_nodes) # Calculate FDD fdd = (fdv_metric >= Dstar)+0 return fdd def _average_attribute(attribute, average_times, average_nodes): # Average for all times and nodes if average_times==False and average_nodes==False: pass # Average for all nodes (averaged over all times) if average_times==True and average_nodes==False: attribute = attribute.sum(axis=0) # Average for all time (averaged over all nodes) if average_times==False and average_nodes==True: attribute = attribute.sum(axis=1) # Average for scenario (averaged over all times and nodes) if average_times==True and average_nodes==True: attribute = attribute.sum().sum() return attribute def todini(node_results, link_results, wn, Pstar): """ Compute Todini index, equations from [1]. The Todini index is related to the capability of a system to overcome failures while still meeting demands and pressures at the nodes. The Todini index defines resilience at a specific time as a measure of surplus power at each node and measures relative energy redundancy. Parameters ---------- node_results : pd.Panel A pandas Panel containing node results. Items axis = attributes, Major axis = times, Minor axis = node names todini index uses 'head', 'pressure', and 'demand' attrbutes. link_results : pd.Panel A pandas Panel containing link results. Items axis = attributes, Major axis = times, Minor axis = link names todini index uses the 'flowrate' attrbute. wn : Water Network Model A water network model. The water network model is needed to find the start and end node to each pump. Pstar : float Pressure threshold. Returns ------- todini_index : pd.Series Time-series of Todini indexes References ----------- [1] Todini E. (2000). Looped water distribution networks design using a resilience index based heuristic approach. Urban Water, 2(2), 115-122. """ POut = {} PExp = {} PInRes = {} PInPump = {} for name, node in wn.nodes(wntr.network.Junction): h = np.array(node_results.loc['head',:,name]) # m p = np.array(node_results.loc['pressure',:,name]) e = h - p # m q = np.array(node_results.loc['demand',:,name]) # m3/s POut[name] = q*h PExp[name] = q*(Pstar+e) for name, node in wn.nodes(wntr.network.Reservoir): H = np.array(node_results.loc['head',:,name]) # m Q = np.array(node_results.loc['demand',:,name]) # m3/s PInRes[name] = -Q*H # switch sign on Q. for name, link in wn.links(wntr.network.Pump): start_node = link._start_node_name end_node = link._end_node_name h_start = np.array(node_results.loc['head',:,start_node]) # (m) h_end = np.array(node_results.loc['head',:,end_node]) # (m) h = h_start - h_end # (m) q = np.array(link_results.loc['flowrate',:,name]) # (m^3/s) PInPump[name] = q*(abs(h)) # assumes that pumps always add energy to the system todini_index = (sum(POut.values()) - sum(PExp.values()))/ \ (sum(PInRes.values()) + sum(PInPump.values()) - sum(PExp.values())) todini_index = pd.Series(data = todini_index.tolist(), index = node_results.major_axis) return todini_index def entropy(G, sources=None, sinks=None): """ Compute entropy, equations from [1]. Entropy is a measure of uncertainty in a random variable. In a water distribution network model, the random variable is flow in the pipes and entropy can be used to measure alternate flow paths when a network component fails. A network that carries maximum entropy flow is considered reliable with multiple alternate paths. Parameters ---------- G : NetworkX or WNTR graph Entropy is computed using a directed graph based on pipe flow direction. The 'weight' of each link is equal to the flow rate. sources : list of strings, optional (default = all reservoirs) List of node names to use as sources. sinks : list of strings, optional (default = all nodes) List of node names to use as sinks. Returns ------- S : dict Node entropy, {node name: entropy value} Shat : float System entropy References ----------- [1] Awumah K, Goulter I, Bhatt SK. (1990). Assessment of reliability in water distribution networks using entropy based measures. Stochastic Hydrology and Hydraulics, 4(4), 309-320 """ if G.is_directed() == False: return if sources is None: sources = [key for key,value in nx.get_node_attributes(G,'type').items() if value == 'reservoir' ] if sinks is None: sinks = G.nodes() S = {} Q = {} for nodej in sinks: if nodej in sources: S[nodej] = 0 # nodej is the source continue sp = [] # simple path if G.node[nodej]['type'] == 'junction': for source in sources: if nx.has_path(G, source, nodej): simple_paths = _all_simple_paths(G,source,target=nodej) sp = sp + ([p for p in simple_paths]) # all_simple_paths was modified to check 'has_path' in the # loop, but this is still slow for large networks # what if the network was skeletonized based on series pipes # that have the same flow direction? # what about duplicating paths that have pipes in series? #print j, nodeid, len(sp) if len(sp) == 0: S[nodej] = np.nan # nodej is not connected to any sources continue sp = np.array(sp) # Uj = set of nodes on the upstream ends of links incident on node j Uj = G.predecessors(nodej) # qij = flow in link from node i to node j qij = [] # aij = number of equivalnet independent paths through the link from node i to node j aij = [] for nodei in Uj: mask = np.array([nodei in path for path in sp]) # NDij = number of paths through the link from node i to node j NDij = sum(mask) if NDij == 0: continue temp = sp[mask] # MDij = links in the NDij path MDij = [(t[idx],t[idx+1]) for t in temp for idx in range(len(t)-1)] flow = 0 for link in G[nodei][nodej].keys(): flow = flow + G[nodei][nodej][link]['weight'] qij.append(flow) # dk = degree of link k in MDij dk = Counter() for elem in MDij: # divide by the numnber of links between two nodes dk[elem] += 1/len(G[elem[0]][elem[1]].keys()) V = np.array(list(dk.values())) aij.append(NDij*(1-float(sum(V - 1))/sum(V))) Q[nodej] = sum(qij) # Total flow into node j # Equation 7 S[nodej] = 0 for idx in range(len(qij)): if qij[idx]/Q[nodej] > 0: S[nodej] = S[nodej] - \ qij[idx]/Q[nodej]*math.log(qij[idx]/Q[nodej]) + \ qij[idx]/Q[nodej]*math.log(aij[idx]) Q0 = sum(nx.get_edge_attributes(G, 'weight').values()) # Equation 3 Shat = 0 for nodej in sinks: if not np.isnan(S[nodej]): if nodej not in sources: if Q[nodej]/Q0 > 0: Shat = Shat + \ (Q[nodej]*S[nodej])/Q0 - \ Q[nodej]/Q0*math.log(Q[nodej]/Q0) return [S, Shat]
nilq/baby-python
python
import asyncio from typing import Union from pynput.keyboard import Key, KeyCode, Listener from core_modules.macro_handler import MacroHandler from core_modules.tray import Tray from global_modules import logs class KeyboardHandler: def __init__(self, macro_handler: MacroHandler, tray: Tray, loop: asyncio.AbstractEventLoop): self.__macro_handler = macro_handler self.__tray = tray self.__loop = loop self.__running = {} self.__pressed = [] listener = Listener(on_press=self.__key_press_callback, on_release=self.__key_release_callback) listener.start() def __key_press_callback(self, key: Union[Key, KeyCode]): if not self.__is_key_pressed(self.__get_key_name(key)): self.__pressed.append(self.__get_key_name(key)) if not self.__tray.enabled: return key_universal = None if isinstance(key, Key): key_universal = key.name elif isinstance(key, KeyCode): if key.char is None: return else: key_universal = key.char if key_universal not in str(self.__macro_handler.actual_loaded.keys()): return for macro_key in self.__macro_handler.actual_loaded.keys(): if self.__is_dict_key_pressed(macro_key): macro = self.__macro_handler.actual_loaded[macro_key] if macro['loop']: key_running = f"{macro_key} {macro['callback']['location']}" if key_running in self.__running.keys(): task = self.__running[key_running]['task'] task.cancel() logs.info("keyboard_handler", f"Macro {macro['callback']['location']} stopped due to user " f"input") if macro['after']['func'] is not None: logs.info("keyboard_handler", f"After {macro['after']['location']} running") self.__loop.create_task(macro['after']['func']()) self.__running.pop(key_running) else: task = self.__loop.create_task(self.__create_macro_loop_task_builder(macro)) self.__running.update({key_running: {'task': task, 'location': macro['callback']['location']}}) else: self.__loop.create_task(self.__create_macro_task_builder(macro)) def __key_release_callback(self, key: Union[Key, KeyCode]): if self.__is_key_pressed(self.__get_key_name(key)): self.__pressed.remove(self.__get_key_name(key)) @staticmethod async def __create_macro_loop_task_builder(macro): coro = macro['callback']['func'] before = macro['before']['func'] if before: logs.info("keyboard_handler", f"Before {macro['before']['location']} running") await before() logs.info("keyboard_handler", f"Macro {macro['callback']['location']} running in loop") while True: await coro() await asyncio.sleep(0) # Needed or the program freeze @staticmethod async def __create_macro_task_builder(macro): coro = macro['callback']['func'] before = macro['before']['func'] after = macro['after']['func'] if before is not None: logs.info("keyboard_handler", f"Before {macro['before']['location']} running") await before() logs.info("keyboard_handler", f"Macro {macro['callback']['location']} running") await coro() if after is not None: logs.info("keyboard_handler", f"After {macro['after']['location']} running") await after() def __is_dict_key_pressed(self, dict_key: str) -> bool: sub_keys = dict_key.split("+") for i in sub_keys: call = True for j in i.split("."): if not self.__is_key_pressed(j): call = False if call: return True return False def __is_key_pressed(self, key: str) -> bool: return key in self.__pressed def update(self): if self.__macro_handler.just_updated_loaded: self.__macro_handler.just_updated_loaded = False running_bkp = self.__running.copy() for key, item in running_bkp.items(): task = self.__running[key]['task'] task.cancel() logs.info("keyboard_handler", f"Macro {self.__running[key]['location']} stopped running due to window " f"change") self.__running.pop(key) @staticmethod def __get_key_name(key: Union[Key, KeyCode]) -> str: if isinstance(key, Key): return key.name elif isinstance(key, KeyCode): if key.char is not None: return key.char else: return str(key.vk)
nilq/baby-python
python
import requests from slack.web.classes import messages def webhook_response(response_url: str, json=None): return requests.post(response_url, json=json) def basic_responder_response(text: str) -> messages.Message: return messages.Message(text=text)
nilq/baby-python
python
from sly import Lexer, Parser from os import path from defs import * global curr_file, curr_text, error_occurred, curr_namespace, reserved_names def syntax_error(line, msg=''): global error_occurred error_occurred = True print() if msg: print(f"Syntax Error in file {curr_file} line {line}:") print(f" {msg}") else: print(f"Syntax Error in file {curr_file} line {line}") def syntax_warning(line, is_error, msg=''): if is_error: global error_occurred error_occurred = True print() print(f"Syntax Warning in file {curr_file}", end="") if line is not None: print(f" line {line}", end="") if msg: print(f":") print(f" {msg}") else: print() class FJLexer(Lexer): tokens = {NS, DEF, REP, WFLIP, SEGMENT, RESERVE, ID, DOT_ID, NUMBER, STRING, LE, GE, EQ, NEQ, SHL, SHR, NL, SC} literals = {'=', '+', '-', '*', '/', '%', '(', ')', '$', '^', '|', '&', '?', ':', '<', '>', '"', '#', '{', '}', "@", ","} ignore_ending_comment = r'//.*' # Tokens DOT_ID = dot_id_re ID = id_re NUMBER = number_re STRING = string_re ID[r'def'] = DEF ID[r'rep'] = REP ID[r'ns'] = NS ID[r'wflip'] = WFLIP ID[r'segment'] = SEGMENT ID[r'reserve'] = RESERVE global reserved_names reserved_names = {DEF, REP, NS, WFLIP, SEGMENT, RESERVE} LE = "<=" GE = ">=" EQ = "==" NEQ = "!=" SHL = r'<<' SHR = r'>>' # Punctuations NL = r'[\r\n]' SC = r';' ignore = ' \t' def NUMBER(self, t): n = t.value if len(n) >= 2: if n[0] == "'": t.value = handle_char(n[1:-1])[0] elif n[1] in 'xX': t.value = int(n, 16) elif n[1] in 'bB': t.value = int(n, 2) else: t.value = int(n) else: t.value = int(t.value) return t def STRING(self, t): chars = [] s = t.value[1:-1] i = 0 while i < len(s): val, length = handle_char(s[i:]) chars.append(val) i += length t.value = sum(val << (i*8) for i, val in enumerate(chars)) return t def NL(self, t): self.lineno += 1 return t def error(self, t): global error_occurred error_occurred = True print() print(f"Lexing Error in file {curr_file} line {self.lineno}: {t.value[0]}") self.index += 1 class FJParser(Parser): tokens = FJLexer.tokens # TODO add Unary Minus (-), Unary Not (~). Maybe add logical or (||) and logical and (&&). Maybe handle power (**). precedence = ( ('right', '?', ':'), ('left', '|'), ('left', '^'), ('nonassoc', '<', '>', LE, GE), ('left', EQ, NEQ), ('left', '&'), ('left', SHL, SHR), ('left', '+', '-'), ('left', '*', '/', '%'), ('right', '#'), ) # debugfile = 'src/parser.out' def __init__(self, w, warning_as_errors, verbose=False): self.verbose = verbose self.defs = {'w': Expr(w)} self.warning_as_errors = warning_as_errors # [(params, quiet_params), statements, (curr_file, p.lineno, ns_name)] self.macros = {main_macro: [([], []), [], (None, None, '')]} def check_macro_name(self, name, line): global reserved_names base_name = self.ns_to_base_name(name[0]) if base_name in reserved_names: syntax_error(line, f'macro name can\'t be {name[0]} ({base_name} is a reserved name)!') if name in self.macros: _, _, (other_file, other_line, _) = self.macros[name] syntax_error(line, f'macro {name} is declared twice! ' f'also declared in file {other_file} (line {other_line}).') def check_params(self, ids, macro_name, line): for param_id in ids: if param_id in self.defs: syntax_error(line, f'parameter {param_id} in macro {macro_name[0]}({macro_name[1]}) ' f'is also defined as a constant variable (with value {self.defs[param_id]})') for i1 in range(len(ids)): for i2 in range(i1): if ids[i1] == ids[i2]: syntax_error(line, f'parameter {ids[i1]} in macro {macro_name[0]}({macro_name[1]}) ' f'is declared twice!') def check_label_usage(self, labels_used, labels_declared, params, externs, global_labels, line, macro_name): if global_labels & externs: syntax_error(line, f"In macro {macro_name[0]}({macro_name[1]}): " f"extern labels can't be global labels: " + ', '.join(global_labels & externs)) if global_labels & params: syntax_error(line, f"In macro {macro_name[0]}({macro_name[1]}): " f"extern labels can't be regular labels: " + ', '.join(global_labels & params)) if externs & params: syntax_error(line, f"In macro {macro_name[0]}({macro_name[1]}): " f"global labels can't be regular labels: " + ', '.join(externs & params)) # params.update([self.ns_full_name(p) for p in params]) # externs = set([self.ns_full_name(p) for p in externs]) # globals.update([self.ns_full_name(p) for p in globals]) unused_labels = params - labels_used.union(self.ns_to_base_name(label) for label in labels_declared) if unused_labels: syntax_warning(line, self.warning_as_errors, f"In macro {macro_name[0]}({macro_name[1]}): " f"unused labels: {', '.join(unused_labels)}.") bad_declarations = labels_declared - set(self.ns_full_name(label) for label in externs.union(params)) if bad_declarations: syntax_warning(line, self.warning_as_errors, f"In macro {macro_name[0]}({macro_name[1]}): " f"Declared a not extern/parameter label: {', '.join(bad_declarations)}.") bad_uses = labels_used - global_labels - params - set(labels_declared) - {'$'} if bad_uses: # print('\nused:', labels_used, 'globals:', globals, 'params:', params) syntax_warning(line, self.warning_as_errors, f"In macro {macro_name[0]}({macro_name[1]}): " f"Used a not global/parameter/declared-extern label: {', '.join(bad_uses)}.") @staticmethod def ns_name(): return '.'.join(curr_namespace) @staticmethod def ns_full_name(base_name): return '.'.join(curr_namespace + [base_name]) @staticmethod def dot_id_to_ns_full_name(p): base_name = p.DOT_ID without_dots = base_name.lstrip('.') if len(without_dots) == len(base_name): return base_name num_of_dots = len(base_name) - len(without_dots) if num_of_dots - 1 > len(curr_namespace): syntax_error(p.lineno, f'Used more leading dots than current namespace depth ' f'({num_of_dots}-1 > {len(curr_namespace)})') return '.'.join(curr_namespace[:len(curr_namespace)-(num_of_dots-1)] + [without_dots]) @staticmethod def ns_to_base_name(name): return name.split('.')[-1] def error(self, token): global error_occurred error_occurred = True print() print(f'Syntax Error in file {curr_file} line {token.lineno}, token=("{token.type}", {token.value})') @_('definable_line_statements') def program(self, p): ops = p.definable_line_statements self.macros[main_macro][1] = ops # labels_used, labels_declared = all_used_labels(ops) # bad_uses = labels_used - set(labels_declared) - {'$'} # if bad_uses: # syntax_warning(None, self.warning_as_errors, # f"Outside of macros: " # f"Used a not declared label: {', '.join(bad_uses)}.") @_('definable_line_statements NL definable_line_statement') def definable_line_statements(self, p): if p.definable_line_statement: return p.definable_line_statements + p.definable_line_statement return p.definable_line_statements @_('definable_line_statement') def definable_line_statements(self, p): if p.definable_line_statement: return p.definable_line_statement return [] @_('') def empty(self, p): return None @_('line_statement') def definable_line_statement(self, p): return p.line_statement @_('macro_def') def definable_line_statement(self, p): return [] @_('NS ID') def namespace(self, p): curr_namespace.append(p.ID) @_('namespace "{" NL definable_line_statements NL "}"') def definable_line_statement(self, p): curr_namespace.pop() return p.definable_line_statements @_('DEF ID macro_params "{" NL line_statements NL "}"') def macro_def(self, p): params, local_params, global_params, extern_params = p.macro_params name = (self.ns_full_name(p.ID), len(params)) self.check_macro_name(name, p.lineno) self.check_params(params + local_params, name, p.lineno) ops = p.line_statements self.check_label_usage(*all_used_labels(ops), set(params + local_params), set(extern_params), set(global_params), p.lineno, name) self.macros[name] = [(params, local_params), ops, (curr_file, p.lineno, self.ns_name())] return None @_('empty') def maybe_ids(self, p): return [] @_('IDs') def maybe_ids(self, p): return p.IDs @_('empty') def maybe_local_ids(self, p): return [] @_('"@" IDs') def maybe_local_ids(self, p): return p.IDs @_('empty') def maybe_extern_ids(self, p): return [] @_('empty') def maybe_global_ids(self, p): return [] @_('"<" ids') def maybe_global_ids(self, p): return p.ids @_('">" IDs') def maybe_extern_ids(self, p): return p.IDs @_('maybe_ids maybe_local_ids maybe_global_ids maybe_extern_ids') def macro_params(self, p): return p.maybe_ids, p.maybe_local_ids, p.maybe_global_ids, p.maybe_extern_ids @_('IDs "," ID') def IDs(self, p): return p.IDs + [p.ID] @_('ID') def IDs(self, p): return [p.ID] @_('line_statements NL line_statement') def line_statements(self, p): return p.line_statements + p.line_statement @_('line_statement') def line_statements(self, p): return p.line_statement # @_('empty') # def line_statements(self, p): # return [] @_('empty') def line_statement(self, p): return [] @_('statement') def line_statement(self, p): if p.statement: return [p.statement] return [] @_('label statement') def line_statement(self, p): if p.statement: return [p.label, p.statement] return [p.label] @_('label') def line_statement(self, p): return [p.label] @_('ID ":"') def label(self, p): return Op(OpType.Label, (self.ns_full_name(p.ID),), curr_file, p.lineno) @_('expr SC') def statement(self, p): return Op(OpType.FlipJump, (p.expr, next_address()), curr_file, p.lineno) @_('expr SC expr') def statement(self, p): return Op(OpType.FlipJump, (p.expr0, p.expr1), curr_file, p.lineno) @_('SC expr') def statement(self, p): return Op(OpType.FlipJump, (Expr(0), p.expr), curr_file, p.lineno) @_('SC') def statement(self, p): return Op(OpType.FlipJump, (Expr(0), next_address()), curr_file, p.lineno) @_('ID') def id(self, p): return p.ID, p.lineno @_('DOT_ID') def id(self, p): return self.dot_id_to_ns_full_name(p), p.lineno @_('ids "," id') def ids(self, p): return p.ids + [p.id[0]] @_('id') def ids(self, p): return [p.id[0]] @_('id') def statement(self, p): macro_name, lineno = p.id return Op(OpType.Macro, ((macro_name, 0), ), curr_file, lineno) @_('id expressions') def statement(self, p): macro_name, lineno = p.id return Op(OpType.Macro, ((macro_name, len(p.expressions)), *p.expressions), curr_file, lineno) @_('WFLIP expr "," expr') def statement(self, p): return Op(OpType.WordFlip, (p.expr0, p.expr1, next_address()), curr_file, p.lineno) @_('WFLIP expr "," expr "," expr') def statement(self, p): return Op(OpType.WordFlip, (p.expr0, p.expr1, p.expr2), curr_file, p.lineno) @_('ID "=" expr') def statement(self, p): name = self.ns_full_name(p.ID) if name in self.defs: syntax_error(p.lineno, f'Can\'t redeclare the variable "{name}".') if not p.expr.eval(self.defs, curr_file, p.lineno): self.defs[name] = p.expr return None syntax_error(p.lineno, f'Can\'t evaluate expression: {str(p.expr)}.') @_('REP "(" expr "," ID ")" id') def statement(self, p): macro_name, lineno = p.id return Op(OpType.Rep, (p.expr, p.ID, Op(OpType.Macro, ((macro_name, 0), ), curr_file, lineno)), curr_file, p.lineno) @_('REP "(" expr "," ID ")" id expressions') def statement(self, p): exps = p.expressions macro_name, lineno = p.id return Op(OpType.Rep, (p.expr, p.ID, Op(OpType.Macro, ((macro_name, len(exps)), *exps), curr_file, lineno)), curr_file, p.lineno) @_('SEGMENT expr') def statement(self, p): return Op(OpType.Segment, (p.expr,), curr_file, p.lineno) @_('RESERVE expr') def statement(self, p): return Op(OpType.Reserve, (p.expr,), curr_file, p.lineno) @_('expressions "," expr') def expressions(self, p): return p.expressions + [p.expr] @_('expr') def expressions(self, p): return [p.expr] @_('_expr') def expr(self, p): return p._expr[0] @_('_expr "+" _expr') def _expr(self, p): a, b = p._expr0[0], p._expr1[0] if a is int and b is int: return Expr(a + b), p.lineno return Expr(('+', (a, b))), p.lineno @_('_expr "-" _expr') def _expr(self, p): a, b = p._expr0[0], p._expr1[0] if a is int and b is int: return Expr(a - b), p.lineno return Expr(('-', (a, b))), p.lineno @_('_expr "*" _expr') def _expr(self, p): a, b = p._expr0[0], p._expr1[0] if a is int and b is int: return Expr(a * b), p.lineno return Expr(('*', (a, b))), p.lineno @_('"#" _expr') def _expr(self, p): a = p._expr[0] if a is int: return Expr(a.bit_length()), p.lineno return Expr(('#', (a,))), p.lineno @_('_expr "/" _expr') def _expr(self, p): a, b = p._expr0[0], p._expr1[0] if a is int and b is int: return Expr(a // b), p.lineno return Expr(('/', (a, b))), p.lineno @_('_expr "%" _expr') def _expr(self, p): a, b = p._expr0[0], p._expr1[0] if a is int and b is int: return Expr(a % b), p.lineno return Expr(('%', (a, b))), p.lineno @_('_expr SHL _expr') def _expr(self, p): a, b = p._expr0[0], p._expr1[0] if a is int and b is int: return Expr(a << b), p.lineno return Expr(('<<', (a, b))), p.lineno @_('_expr SHR _expr') def _expr(self, p): a, b = p._expr0[0], p._expr1[0] if a is int and b is int: return Expr(a >> b), p.lineno return Expr(('>>', (a, b))), p.lineno @_('_expr "^" _expr') def _expr(self, p): a, b = p._expr0[0], p._expr1[0] if a is int and b is int: return Expr(a ^ b), p.lineno return Expr(('^', (a, b))), p.lineno @_('_expr "|" _expr') def _expr(self, p): a, b = p._expr0[0], p._expr1[0] if a is int and b is int: return Expr(a | b), p.lineno return Expr(('|', (a, b))), p.lineno @_('_expr "&" _expr') def _expr(self, p): a, b = p._expr0[0], p._expr1[0] if a is int and b is int: return Expr(a & b), p.lineno return Expr(('&', (a, b))), p.lineno @_('_expr "?" _expr ":" _expr') def _expr(self, p): a, b, c = p._expr0[0], p._expr1[0], p._expr2[0] if a is int and b is int and c is int: return Expr(b if a else c), p.lineno return Expr(('?:', (a, b, c))), p.lineno @_('_expr "<" _expr') def _expr(self, p): a, b = p._expr0[0], p._expr1[0] if a is int and b is int: return Expr(1 if a < b else 0), p.lineno return Expr(('<', (a, b))), p.lineno @_('_expr ">" _expr') def _expr(self, p): a, b = p._expr0[0], p._expr1[0] if a is int and b is int: return Expr(1 if a > b else 0), p.lineno return Expr(('>', (a, b))), p.lineno @_('_expr LE _expr') def _expr(self, p): a, b = p._expr0[0], p._expr1[0] if a is int and b is int: return Expr(1 if a <= b else 0), p.lineno return Expr(('<=', (a, b))), p.lineno @_('_expr GE _expr') def _expr(self, p): a, b = p._expr0[0], p._expr1[0] if a is int and b is int: return Expr(1 if a >= b else 0), p.lineno return Expr(('>=', (a, b))), p.lineno @_('_expr EQ _expr') def _expr(self, p): a, b = p._expr0[0], p._expr1[0] if a is int and b is int: return Expr(1 if a == b else 0), p.lineno return Expr(('==', (a, b))), p.lineno @_('_expr NEQ _expr') def _expr(self, p): a, b = p._expr0[0], p._expr1[0] if a is int and b is int: return Expr(1 if a != b else 0), p.lineno return Expr(('!=', (a, b))), p.lineno @_('"(" _expr ")"') def _expr(self, p): return p._expr @_('NUMBER') def _expr(self, p): return Expr(p.NUMBER), p.lineno @_('STRING') def _expr(self, p): return Expr(p.STRING), p.lineno @_('"$"') def _expr(self, p): return next_address(), p.lineno @_('id') def _expr(self, p): id_str, lineno = p.id if id_str in self.defs: return self.defs[id_str], lineno return Expr(id_str), lineno def exit_if_errors(): if error_occurred: raise FJParsingException(f'Errors found in file {curr_file}. Assembly stopped.') def parse_macro_tree(input_files, w, warning_as_errors, verbose=False): global curr_file, curr_text, error_occurred, curr_namespace error_occurred = False lexer = FJLexer() parser = FJParser(w, warning_as_errors, verbose=verbose) for curr_file in input_files: if not path.isfile(curr_file): raise FJParsingException(f"No such file {curr_file}.") curr_text = open(curr_file, 'r').read() curr_namespace = [] lex_res = lexer.tokenize(curr_text) exit_if_errors() parser.parse(lex_res) exit_if_errors() return parser.macros
nilq/baby-python
python
#!/usr/bin/env python # coding: utf-8 # # ML Pipeline Preparation # Follow the instructions below to help you create your ML pipeline. # ### 1. Import libraries and load data from database. # - Import Python libraries # - Load dataset from database with [`read_sql_table`](https://pandas.pydata.org/pandas-docs/stable/generated/pandas.read_sql_table.html) # - Define feature and target variables X and Y # In[1]: # import libraries import pandas as pd import nltk import numpy as np from sklearn.datasets import make_multilabel_classification from sklearn.multioutput import MultiOutputClassifier from sklearn.neighbors import KNeighborsClassifier import nltk nltk.download(['punkt', 'wordnet']) import re import numpy as np import pandas as pd from nltk.tokenize import word_tokenize from nltk.stem import WordNetLemmatizer from sklearn.pipeline import Pipeline from sklearn.metrics import confusion_matrix from sklearn.model_selection import GridSearchCV from sklearn.metrics import confusion_matrix from sklearn.model_selection import train_test_split from sklearn.ensemble import RandomForestClassifier from sklearn.feature_extraction.text import CountVectorizer, TfidfTransformer # In[3]: # load data from database from sqlalchemy import create_engine engine = create_engine('sqlite:///InsertDatabaseName.db') df = pd.read_sql_table('disaster_messages', 'sqlite:///InsertDatabaseName.db') df.head() X = df['message'] Y = df[df.columns.difference(['id', 'message','original','genre'])] # ### 2. Write a tokenization function to process your text data # In[4]: url_regex = 'http[s]?://(?:[a-zA-Z]|[0-9]|[$-_@.&+]|[!*\(\),]|(?:%[0-9a-fA-F][0-9a-fA-F]))+' import re def tokenize(text): # get list of all urls using regex detected_urls = re.findall(url_regex, text) # replace each url in text string with placeholder for url in detected_urls: text = text.replace(url, "urlplaceholder") # tokenize text tokens = word_tokenize(text) # initiate lemmatizer lemmatizer = WordNetLemmatizer() # iterate through each token clean_tokens = [] for tok in tokens: # lemmatize, normalize case, and remove leading/trailing white space clean_tok = lemmatizer.lemmatize(tok).lower().strip() clean_tokens.append(clean_tok) return clean_tokens # ### 3. Build a machine learning pipeline # This machine pipeline should take in the `message` column as input and output classification results on the other 36 categories in the dataset. You may find the [MultiOutputClassifier](http://scikit-learn.org/stable/modules/generated/sklearn.multioutput.MultiOutputClassifier.html) helpful for predicting multiple target variables. # In[5]: X_train, X_test, y_train, y_test = train_test_split(X, Y) knn = KNeighborsClassifier(n_neighbors=-1) pipeline = Pipeline([ ('vect', CountVectorizer(tokenizer=tokenize)), ('tfidf', TfidfTransformer()), ('clf', MultiOutputClassifier(knn, n_jobs=-1)) ]) # ### 4. Train pipeline # - Split data into train and test sets # - Train pipeline # In[6]: pipeline.fit(X_train, y_train) # ### 5. Test your model # Report the f1 score, precision and recall for each output category of the dataset. You can do this by iterating through the columns and calling sklearn's `classification_report` on each. # In[ ]: y_pred = pipeline.predict(X_test) y_pred # ### 6. Improve your model # Use grid search to find better parameters. # In[ ]: parameters = { 'clf__n_estimators': [50, 100], 'clf__min_samples_split': [2, 3] ) } cv = GridSearchCV(pipeline, param_grid=parameters) # ### 7. Test your model # Show the accuracy, precision, and recall of the tuned model. # # Since this project focuses on code quality, process, and pipelines, there is no minimum performance metric needed to pass. However, make sure to fine tune your models for accuracy, precision and recall to make your project stand out - especially for your portfolio! # In[ ]: def display_results(cv, y_test, y_pred): labels = np.unique(y_pred) confusion_mat = confusion_matrix(y_test, y_pred, labels=labels) accuracy = (y_pred == y_test).mean() print("Labels:", labels) print("Confusion Matrix:\n", confusion_mat) print("Accuracy:", accuracy) print("\nBest Parameters:", cv.best_params_) display_results(cv, y_test, y_pred) # ### 8. Try improving your model further. Here are a few ideas: # * try other machine learning algorithms # * add other features besides the TF-IDF # In[ ]: # ### 9. Export your model as a pickle file # In[ ]: from sklearn.externals import joblib joblib_file = "joblib_model.pkl" joblib.dump(cv, joblib_file) # ### 10. Use this notebook to complete `train.py` # Use the template file attached in the Resources folder to write a script that runs the steps above to create a database and export a model based on a new dataset specified by the user. # In[ ]:
nilq/baby-python
python
#!/usr/bin/python # The templating engine and the parser for the dllup markup language are hereby # released open-source under the MIT License. # # Copyright (c) 2015 Daniel Lawrence Lu # Permission is hereby granted, free of charge, to any person obtaining a copy # of this software and associated documentation files (the "Software"), to deal # in the Software without restriction, including without limitation the rights # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell # copies of the Software, and to permit persons to whom the Software is # furnished to do so, subject to the following conditions: # # The above copyright notice and this permission notice shall be included in # all copies or substantial portions of the Software. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN # THE SOFTWARE. import dllup import hashlib import os import re import struct import time from operator import itemgetter RASTER_IMG = ['.png', '.jpg'] # navigation markup PORTFOLIO_NAV = '<a href="{child}/"><figure><img src="{pic}" alt="{child}"/><figcaption>{title} ({subtitle})</figcaption></figure></a>' BLOG_NAV = '<a href="{child}/"><span class="blogdate">{date}</span><span class="blogtitle">{title}</span></a>' ROOT_NAV = '<a href="/{child}/">{child}</a>' # the first breadcrumb BREAD = '<a href="/"><span id="dllu"><span style="display:none;">dllu</span><span id="D"></span><span id="L0"></span><span id="L1"></span><span id="U"></span></span></a><span>/</span>' BREAD_HERO = '<a href="/" id="hero-a"><span id="dllu-hero"><span style="display:none;">dllu</span><span id="D"></span><span id="L0"></span><span id="L1"></span><span id="U"></span></span></a>' # all consecutive breadcrumbs CRUMB = '<a href="{cpath}">{child}</a><span>/</span>' # page markup PAGE = '<!DOCTYPE html>\n{sig}\n{htmlhead}<nav id="breadcrumbs">{breadcrumbs}</nav><nav id="rootnav">{rootnav}</nav><nav id="{navtype}">{nav}</nav><main>{output}<footer><p>&copy; Daniel Lawrence Lu. Page generated on {time} by <a href="/programming/dllup/">dllup</a>. (<a href="{text}">text version</a>)</footer></main>{htmlfoot}' PAGE_HERO = PAGE.replace('id="breadcrumbs"', 'id="hero"') def readconfig(configpath): # Reads a config file which is a simple text file of key-value pairs. # One key-value pair per line, key (no whitespaces) is separated from # value by whitespace. # Valid keys are: type, root if not os.path.exists(configpath): return {} config = open(configpath).read() configsplit = [cc.split(None, 1) for cc in config.split('\n')] return {c[0]: c[1] for c in configsplit if len(c) >= 2} def recurse(path='', rootnav='', root=''): global htmlhead global htmlfoot children = os.listdir(path) folderdata = [ get_folderdata(os.path.join(path, c)) for c in children if os.path.isdir(os.path.join(path, c)) ] config = readconfig(os.path.join(path, 'config')) if 'root' in config: root = config['root'] navtype = config['type'] if 'type' in config else None # generate navigation markup nav = '' if navtype == 'blogposts': folderdata = sorted( [f for f in folderdata if 'date' in f], key=itemgetter('date'), reverse=True, ) elif navtype == 'portfolio': folderdata = sorted( [f for f in folderdata if 'subtitle' in f], key=itemgetter('subtitle'), reverse=True, ) else: folderdata = sorted([f for f in folderdata if 'child' in f], key=itemgetter('child')) for f in folderdata: try: if navtype == 'root': rootnav += ROOT_NAV.format(**f) elif navtype == 'blogposts': nav += BLOG_NAV.format(**f) elif navtype == 'portfolio': nav += PORTFOLIO_NAV.format(**f) except KeyError: pass # ignore folders without complete data breadcrumbs = crumbify(path) # recurse through children for child in children: cpath = os.path.join(path, child) if os.path.isdir(cpath): recurse(cpath, rootnav, root) if child[-4:] in RASTER_IMG and not '_600' in child: resize_images(path, child) pass elif child[-5:] == '.dllu': markup = open(os.path.join(path, child)).read() sig = '<!--%s-->' % hashlib.sha1( struct.pack('f', os.path.getmtime(cpath))).hexdigest() sig2 = None try: with open(os.path.join(path, child[:-5] + '.html')) as f: f.readline() sig2 = f.readline() except FileNotFoundError: pass if sig == sig2: continue output = dllup.parse(markup) f = open(os.path.join(path, child[:-5] + '.html'), 'w') PP = PAGE if path == '.': PP = PAGE_HERO ss = markup.split('\n===\n', 1) if len(ss) > 1: title = ss[0].strip() else: title = path.split('/')[-1] head = htmlhead.format(title=title) f.write( PP.format(htmlhead=head, htmlfoot=htmlfoot, breadcrumbs=breadcrumbs, rootnav=rootnav, navtype=navtype, output=output, time=time.strftime('%Y-%m-%d', time.gmtime()), child=child, nav=nav, sig=sig, text=child).replace( ' src="/', ' src="%s/' % root, ).replace( ' href="/', ' href="%s/' % root, )) f.close() def resize_images(path, child): filename = os.path.join(path, child) filename600 = os.path.join(path, child[:-4] + '_600' + child[-4:]) filename600x2 = os.path.join(path, child[:-4] + '_600@2x' + child[-4:]) for f in (filename600, filename600x2): scale = 600 if '@2x' in f: scale = 1200 if not os.path.exists(f): os.system(f'gm convert "{filename}" -resize {scale} "{f}"') def crumbify(path): if path == '.': return BREAD_HERO breadcrumbs = BREAD crumbs = '/' for crumb in path.split('/')[1:]: crumbs += crumb + '/' breadcrumbs += CRUMB.format(cpath=crumbs, child=crumb) return breadcrumbs def get_folderdata(path): if os.path.exists(os.path.join(path, 'private')): return {} folderdata = {'child': os.path.split(path)[1]} index = os.path.join(path, 'index.dllu') if os.path.exists(index): content = open(index).read().split('\n===\n', 1)[0] content = [d for d in content.split('\n') if d.strip() != ''] if len(content) >= 1: folderdata['title'] = dllup.parsetext(content[0]) if len(content) >= 2: folderdata['subtitle'] = dllup.parsetext(content[1]) else: return {} for extension in RASTER_IMG: if os.path.exists(path + extension): folderdata['pic'] = os.path.split(path)[1] + extension if re.match('y\d\d\d\dm\d\dd\d\d', os.path.split(path)[1]): folderdata['date'] = re.sub('m|d', '-', os.path.split(path)[1][1:]) return folderdata def main(): global htmlhead, htmlfoot htmlhead = open('html/head.html').read() htmlfoot = open('html/foot.html').read() cssname = 'dllu-%s.css' % hashlib.sha1( struct.pack('f', os.path.getmtime('css'))).hexdigest() os.system('sassc -t compressed css/dllu.scss > %s' % cssname) htmlhead = htmlhead.replace('dllu.css', cssname) recurse('.') if __name__ == '__main__': main()
nilq/baby-python
python
import argparse from datetime import datetime, timedelta import logging import os import pprint import random from config import DATETIME_FORMAT, EXPORT_PATH, GENDER_PREF_FUNCTIONS, AGE_PREF_FUNCTIONS,\ AGE_YOUNG_MID, AGE_MID_OLD, DEPARTMENTS, VENDORS, CATEGORIES, CATEGORIES_UNIQUE, DEVELOPMENT from converters.summary_con import SummaryCon from exporters.csv_exp import CsvExp from exporters.postgres_exp import PostgresExp from exporters.json_exp import JsonExp from generators.customers_gen import CustomersGen from generators.inventory_gen import InventoryGen from generators.products_gen import ProductsGen from generators.simple_gen import SimpleGen from generators.stores_gen import StoresGen from generators.customer_preferences_gen import CustomerPreferencesGen from generators.coupon_gen import CouponGen from generators.orders_gen import OrdersGen parser = argparse.ArgumentParser() parser.add_argument("-c", "--customers", type=int, help="number of customers") parser.add_argument("-d", "--departments", type=int, help="number of departments") parser.add_argument("-p", "--products", type=int, help="number of products") parser.add_argument("-C", "--coupons", type=int, help="number of coupons") parser.add_argument("-S", "--start", type=str, help=f"start date") parser.add_argument("-E", "--end", type=str, help=f"end date") parser.add_argument("-D", "--days", type=int, help="number of days if end not set") parser.add_argument("-P", "--path", type=str, help="path where to generate a files") parser.add_argument("-v", "--verbose", help="verbose", action='store_true') command_params = parser.parse_args() customers_nr = command_params.customers or 1000 departments_nr = len(DEPARTMENTS) products_nr = command_params.products or 3000 vendors_nr = len(VENDORS) categories_nr = len(CATEGORIES_UNIQUE) start = datetime.strptime(command_params.start or '2010-01-01 09:00:00', DATETIME_FORMAT) end = datetime.strptime(command_params.end, DATETIME_FORMAT)\ if command_params.end else start + timedelta(days=command_params.days or 365) path = command_params.path or EXPORT_PATH FORMAT = '%(message)s' logging.basicConfig(format=FORMAT, level=logging.INFO if not command_params.verbose else logging.DEBUG) logger = logging.getLogger('datagen') if __name__ == "__main__": if DEVELOPMENT: random.seed(5793) logger.info("Generating data:") logger.info(f" Customers: {customers_nr}") logger.info(f" Departments: {departments_nr}") logger.info(f" Products: {products_nr}") logger.info(f" Vendors: {vendors_nr}") logger.info(f" Categories: {categories_nr}") logger.info(f"to path: {path}") logger.info("-" * 30) # TODO: departments generator # dg = SimpleGen() # departments = dg.generate() # vg = SimpleGen(vendors_nr) # vendors = vg.generate() # cag = SimpleGen(categories_nr) # categories = cag.generate() cug = CustomersGen(customers_nr) customers = cug.generate() pg = ProductsGen(products_nr) products = pg.generate() ig = InventoryGen(products_nr) inventory = ig.generate() cpg = CustomerPreferencesGen(customers) customer_preferences = cpg.generate() cog = CouponGen(start, end, products_nr) coupons = cog.generate() og = OrdersGen(customer_preferences, products, coupons, start, end) orders, order_details = og.generate() logger.debug("-" * 30) logger.debug("SAMPLES:") logger.debug("Customers:") logger.debug(pprint.pformat(customers[:5])) logger.debug("-" * 10) logger.debug("Products:") logger.debug(pprint.pformat(products[:5])) logger.debug("-" * 10) logger.debug("Inventory:") logger.debug(pprint.pformat(inventory[:5])) logger.debug("-" * 10) logger.debug("Preferences:") logger.debug(pprint.pformat(customer_preferences[:3])) logger.debug("-" * 10) logger.debug("Coupons:") logger.debug(pprint.pformat(coupons[:5])) logger.debug("-" * 10) logger.debug("Orders:") logger.debug(pprint.pformat(orders[:5])) logger.info("-" * 30) logger.info("SUMMARY:") logger.debug(" Customers: " + str(len(customers))) logger.info(" Orders: " + str(len(orders))) logger.info(" Order_details: " + str(len(order_details))) logger.debug(" Departments: " + str(departments_nr)) # TODO: change to departments logger.debug(" Products: " + str(len(products))) logger.debug(" Vendors: " + str(vendors_nr)) logger.debug(" Categories: " + str(categories_nr)) logger.info(" Inventory: " + str(len(inventory))) logger.debug(" Customer_preferences: " + str(len(customer_preferences))) logger.info("-" * 30) # PostgresExp.exrpot( # path, # # Departments=departments, # Vendors=vendors, # Categories=categories, # Customers=customers, # Products=products, # Inventory=inventory, # Orders=orders, # OrderDetails=order_details # ) # JsonExp.export( # path, # filename='preferences.json', # indent=4, # MenPreferences={ # 'departments': cpg.reveal_general_preferences(GENDER_PREF_FUNCTIONS['M'], cpg.departments), # 'vendors': cpg.reveal_general_preferences(GENDER_PREF_FUNCTIONS['M'], cpg.vendorsd), # 'categories': cpg.reveal_general_preferences(GENDER_PREF_FUNCTIONS['M'], cpg.categories) # }, # WomenPreferences={ # 'departments': cpg.reveal_general_preferences(GENDER_PREF_FUNCTIONS['F'], cpg.departments), # 'vendors': cpg.reveal_general_preferences(GENDER_PREF_FUNCTIONS['F'], cpg.vendors), # 'categories': cpg.reveal_general_preferences(GENDER_PREF_FUNCTIONS['F'], cpg.categories) # }, # Young={ # 'departments': cpg.reveal_general_preferences(AGE_PREF_FUNCTIONS[f"0-{AGE_YOUNG_MID - 1}"], cpg.departments_shuffled), # 'vendors': cpg.reveal_general_preferences(AGE_PREF_FUNCTIONS[f"0-{AGE_YOUNG_MID - 1}"], cpg.vendors_shuffled), # 'categories': cpg.reveal_general_preferences(AGE_PREF_FUNCTIONS[f"0-{AGE_YOUNG_MID - 1}"], cpg.categories_shuffled) # }, # Mid={ # 'departments': cpg.reveal_general_preferences(AGE_PREF_FUNCTIONS[f"{AGE_YOUNG_MID}-{AGE_MID_OLD - 1}"], cpg.departments_shuffled), # 'vendors': cpg.reveal_general_preferences(AGE_PREF_FUNCTIONS[f"{AGE_YOUNG_MID}-{AGE_MID_OLD - 1}"], cpg.vendors_shuffled), # 'categories': cpg.reveal_general_preferences(AGE_PREF_FUNCTIONS[f"{AGE_YOUNG_MID}-{AGE_MID_OLD - 1}"], cpg.categories_shuffled) # }, # Old={ # 'departments': cpg.reveal_general_preferences(AGE_PREF_FUNCTIONS[f"{AGE_MID_OLD}-200"], cpg.departments_shuffled), # 'vendors': cpg.reveal_general_preferences(AGE_PREF_FUNCTIONS[f"{AGE_MID_OLD}-200"], cpg.vendors_shuffled), # 'categories': cpg.reveal_general_preferences(AGE_PREF_FUNCTIONS[f"{AGE_MID_OLD}-200"], cpg.categories_shuffled) # } # ) # sum_conv = SummaryCon(orders, order_details, customers, products, vendors_nr, departments_nr, categories_nr) # vendor_sum, department_sum, category_sum = sum_conv.convert() CsvExp.export(path, "customers", customers) CsvExp.export(path, "products", products) CsvExp.export(path, "inventory", inventory) CsvExp.export(path, "orders", orders) CsvExp.export(path, "order_details", order_details) # prepare coupons data coupon_product = [] for coupon in coupons: coupon_product += [{'coupon_id': coupon['id'], 'product_id': p} for p in coupon['products']] del(coupon['products']) CsvExp.export(path, "coupons", coupons) CsvExp.export(path, "coupon_product", coupon_product) JsonExp.export( path, filename='customer_preferences', indent=4, CustomerPreferences=customer_preferences ) logger.info("All done")
nilq/baby-python
python
from pathlib import Path class DocsTranslateBaseException(Exception): pass class UnknownServiceError(DocsTranslateBaseException): def __init__(self, service_name: str) -> None: super().__init__(f'{service_name} service is unknown') class ConfigurationError(DocsTranslateBaseException): def __init__(self, property_name: str) -> None: super().__init__( f'The setting "{property_name}" is missing. Check your config file or cli arguments' ) class ObjectNotFoundException(DocsTranslateBaseException): def __init__(self, obj: Path) -> None: super().__init__(f'{obj} not found') class NoApiKeyFileError(DocsTranslateBaseException): def __init__(self, api_key_path: Path) -> None: super().__init__( f'API_KEY file in location "{api_key_path}" not found\n' 'Provide API_KEY file path or create it, if not exist' ) class NoConfigFileError(DocsTranslateBaseException): def __init__(self, not_found_file: Path) -> None: super().__init__( f'No config file found. Create file {not_found_file} or pass custom file with `-c` param' ) class FileIsNotMarkdown(DocsTranslateBaseException): def __init__(self, not_md_obj: Path) -> None: super().__init__(f'{not_md_obj} is not a Markdown or a restructuredtext file!') class DocsFileNotFoundError(FileNotFoundError): def __init__(self, not_md_obj: Path) -> None: super().__init__(f'{not_md_obj} is not found Markdown or a restructuredtext files!') class FileCreateError(DocsTranslateBaseException): def __init__(self, not_md_obj: Path) -> None: super().__init__(f'{not_md_obj} is not create translate file!')
nilq/baby-python
python
# Copyright (c) Microsoft. All rights reserved. # Licensed under the MIT license. See LICENSE.md file in the project root # for full license information. # ============================================================================== import os, sys import numpy as np from cntk.device import set_default_device abs_path = os.path.dirname(os.path.abspath(__file__)) sys.path.append(os.path.join(abs_path, "..", "..", "..", "..", "Examples", "SequenceClassification", "SimpleExample", "Python")) from SequenceClassification import train_sequence_classifier TOLERANCE_ABSOLUTE = 1E-1 def test_seq_classification_error(device_id): from cntk.ops.tests.ops_test_utils import cntk_device set_default_device(cntk_device(device_id)) evaluation_avg, loss_avg = train_sequence_classifier() expected_avg = [0.51, 1.28] assert np.allclose([evaluation_avg, loss_avg], expected_avg, atol=TOLERANCE_ABSOLUTE)
nilq/baby-python
python
from __future__ import absolute_import, unicode_literals import logging import os import pytest from psd_tools.psd.filter_effects import (FilterEffects, FilterEffect, FilterEffectChannel, FilterEffectExtra) from ..utils import check_write_read, check_read_write, TEST_ROOT logger = logging.getLogger(__name__) @pytest.mark.parametrize('args', [ ('uuid', 1, (0, 0, 512, 512), 8, 3, [ FilterEffectChannel(1, 0, b'\x00'), FilterEffectChannel(1, 0, b'\x00'), FilterEffectChannel(1, 0, b'\x00'), FilterEffectChannel(1, 0, b'\x00'), FilterEffectChannel(1, 0, b'\x00'), ], None), ('uuid', 1, (0, 0, 512, 512), 8, 3, [ FilterEffectChannel(1, 0, b'\x00'), FilterEffectChannel(1, 0, b'\x00'), FilterEffectChannel(1, 0, b'\x00'), FilterEffectChannel(1, 0, b'\x00'), FilterEffectChannel(1, 0, b'\x00'), ], FilterEffectExtra(1, [0, 0, 512, 512], 0, b'\x00')), ('uuid', 1, (0, 0, 512, 512), 8, 3, [ FilterEffectChannel(1, 0, b'\x00'), FilterEffectChannel(1, 0, b'\x00'), FilterEffectChannel(1, 0, b'\x00'), FilterEffectChannel(1, 0, b'\x00'), FilterEffectChannel(1, 0, b'\x00'), ], FilterEffectExtra(0)), ]) def test_filter_effect(args): check_write_read(FilterEffect(*args)) @pytest.mark.parametrize('is_written, compression, data', [ (0, None, b''), (1, None, b''), (1, 0, b''), (1, 0, b'\x00'), ]) def test_filter_effect_channel(is_written, compression, data): check_write_read(FilterEffectChannel(is_written, compression, data)) @pytest.mark.parametrize('filename', [ 'filter_effects_1.dat', 'filter_effects_2.dat', ]) def test_filter_effects_rw(filename): filepath = os.path.join(TEST_ROOT, 'tagged_blocks', filename) with open(filepath, 'rb') as f: fixture = f.read() check_read_write(FilterEffects, fixture)
nilq/baby-python
python
from django import template from backoffice.models import * register = template.Library() @register.simple_tag def number_of_authors(request): qs = Author.objects.all() return qs.count() @register.simple_tag def number_of_questions(request): qs = Question.objects.all() return qs.count() @register.simple_tag def number_of_choices(request): qs = Choice.objects.all() return qs.count()
nilq/baby-python
python
#/usr/bin/python3 # -*- coding: utf-8 -*- import sys import getopt import re from itertools import * import time import json import csv import codecs import random as r import time import random import pandas as pd ##此程序用来将csv文件转成json格式
nilq/baby-python
python
import logging import typing import ckan.plugins.toolkit as toolkit logger = logging.getLogger(__name__) @toolkit.side_effect_free def dcpr_request_list(context: typing.Dict, data_dict: typing.Dict) -> typing.List: logger.debug("Inside the dcpr_request_list action") access_result = toolkit.check_access( "dcpr_request_list_auth", context, data_dict=data_dict ) logger.debug(f"access_result: {access_result}") fake_requests = [ {"name": "req1", "owner": "tester1"}, {"name": "req2", "owner": "tester1"}, {"name": "req3", "owner": "tester1"}, {"name": "req4", "owner": "tester2"}, ] result = [] current_user = context["auth_user_obj"] for dcpr_request in fake_requests: if dcpr_request["owner"] == current_user.name: result.append(dcpr_request) return result
nilq/baby-python
python
""" Unit testing ============ Note: Tests for `ensemble.control.operational.reference` """ # ============================================================================ # STANDARD IMPORTS # ============================================================================ import pytest import numpy as np from numpy.testing import assert_array_equal # ============================================================================ # INTERNAL IMPORTS # ============================================================================ from ensemble.control.operational.reference import ReferenceHeadway from ensemble.logic.platoon_states import ( StandAlone, Platooning, Joining, Splitting, ) # ============================================================================ # TESTS AND DEFINITIONS # ============================================================================ def test_splitting(): r = ReferenceHeadway() r.create_time_gap_hwy(Splitting()) assert pytest.approx(r.reference_headway[-1], 3 * 1.4) def test_joining(): r = ReferenceHeadway(gap0=2) r.create_time_gap_hwy(Joining()) assert r.reference_headway[-1] == 1.4
nilq/baby-python
python
"""Test parallel deployment.""" # pylint: disable=redefined-outer-name from __future__ import annotations import platform import shutil from pathlib import Path from typing import TYPE_CHECKING, Generator import pytest from runway._cli import cli if TYPE_CHECKING: from click.testing import CliRunner, Result CURRENT_DIR = Path(__file__).parent @pytest.fixture(scope="module") def deploy_result(cli_runner: CliRunner) -> Generator[Result, None, None]: """Execute `runway deploy` with `runway destory` as a cleanup step.""" yield cli_runner.invoke(cli, ["deploy"], env={"CI": "1"}) @pytest.fixture(scope="module") def destroy_result(cli_runner: CliRunner) -> Generator[Result, None, None]: """Execute `runway destroy`.""" yield cli_runner.invoke(cli, ["destroy"], env={"CI": "1"}) shutil.rmtree(CURRENT_DIR / "child-01.cfn" / ".runway", ignore_errors=True) shutil.rmtree(CURRENT_DIR / "child-02.cfn" / ".runway", ignore_errors=True) @pytest.mark.order("first") @pytest.mark.skipif( platform.system() != "Linux", reason="only runs consistently on Linux" ) def test_deploy_exit_code(deploy_result: Result) -> None: """Test deploy exit code.""" assert deploy_result.exit_code == 0 @pytest.mark.order(after="test_deploy_exit_code") @pytest.mark.skipif( platform.system() != "Linux", reason="only runs consistently on Linux" ) def test_deploy_log_messages(deploy_result: Result) -> None: """Test deploy log messages.""" assert ( "deployment_1:processing regions in parallel... (output will be interwoven)" in deploy_result.stdout ), f"expected not in stdout:\n{deploy_result.stdout}" @pytest.mark.order("last") @pytest.mark.skipif( platform.system() != "Linux", reason="only runs consistently on Linux" ) def test_destroy_exit_code(destroy_result: Result) -> None: """Test destory exit code.""" assert destroy_result.exit_code == 0
nilq/baby-python
python
import unittest from mock import patch from todo import Todo, TodoManager class TestTodo(unittest.TestCase): def test_default(self): todo = Todo('foo') self.assertEqual(todo.task, 'foo') self.assertFalse(todo.done) def test_default_done(self): todo = Todo('bar', True) self.assertEqual(todo.task, 'bar') self.assertTrue(todo.done) def test_toggle_done(self): todo = Todo('baz') todo.toggle() self.assertTrue(todo.done) class TestTodoManager(unittest.TestCase): def setUp(self): patch('todo.TodoManager._load').start() patch('todo.TodoManager._save').start() self.manager = TodoManager() self.manager.todos = [ Todo('foo'), Todo('bar') ] @patch('builtins.input', return_value='baz') def test_add(self, patch_raw_input): self.manager.add() self.assertEqual(self.manager.todos[2].task, 'baz') def test_delete(self): self.manager.delete(1) self.assertEqual(len(self.manager.todos), 1) def test_delete_all(self): self.manager.delete() self.assertEqual(len(self.manager.todos), 0) def test_toggle_done(self): self.manager.toggle_done(0) self.assertEqual(self.manager.todos[0].done, True) if __name__ == '__main__': unittest.main()
nilq/baby-python
python
from keras.preprocessing.text import Tokenizer from keras.preprocessing.sequence import pad_sequences import os import numpy as np # linear algebra import pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv) from tqdm import tqdm import math from sklearn.model_selection import train_test_split import pickle from keras.models import load_model def loadData(filename): file_ptr = open(filename, 'rb') loaded_obj = pickle.load(file_ptr) return loaded_obj embeddings_index = loadData("classifier_model/glove.840B.300d.pkl") # Convert values to embeddings def text_to_array(text): empyt_emb = np.zeros(300) text = text[:-1].split()[:30] embeds = [embeddings_index.get(x, empyt_emb) for x in text] embeds+= [empyt_emb] * (30 - len(embeds)) return np.array(embeds) # # train_vects = [text_to_array(X_text) for X_text in tqdm(train_df["question_text"])] # val_vects = np.array([text_to_array(X_text) for X_text in tqdm(val_df["question_text"][:3000])]) # val_y = np.array(val_df["target"][:3000]) model = load_model('classifier_model/my_modelcpu.h5') zz = ['I like to sleep',"that's cool other cultures are nice", "where is Geneva cats?", "What public figure defended New York in Januar"] valDF = pd.DataFrame() valDF['question_text'] = zz # prediction part batch_size = 256 def batch_gen(test_df): n_batches = math.ceil(len(test_df) / batch_size) for i in range(n_batches): texts = test_df.iloc[i*batch_size:(i+1)*batch_size, 0] text_arr = np.array([text_to_array(text) for text in texts]) yield text_arr # test_df = pd.read_csv("../input/quora-insincere-questions-classification/test.csv") test_df = valDF all_preds = [] for x in tqdm(batch_gen(test_df)): all_preds.extend(model.predict(x).flatten()) y_te = (np.array(all_preds) > 0.5).astype(np.int) print(y_te) print(valDF['question_text'])
nilq/baby-python
python
import scrapy from scrapy.crawler import CrawlerProcess class VersionSpider(scrapy.Spider): name = 'versions' custom_settings = { "FEED_FORMAT": "json", "FEED_URI": "data/%(name)s/%(time)s.json" } start_urls = ['https://docs.hortonworks.com/HDPDocuments/HDP3/HDP-3.1.0/release-notes/content/comp_versions.html', 'https://docs.hortonworks.com/HDPDocuments/HDP3/HDP-3.0.1/release-notes/content/comp_versions.html', 'https://docs.hortonworks.com/HDPDocuments/HDP3/HDP-3.0.0/release-notes/content/comp_versions.html', 'https://docs.hortonworks.com/HDPDocuments/HDP2/HDP-2.6.5/bk_release-notes/content/comp_versions.html', 'https://docs.hortonworks.com/HDPDocuments/HDP2/HDP-2.6.4/bk_release-notes/content/comp_versions.html', 'https://docs.hortonworks.com/HDPDocuments/HDP2/HDP-2.6.3/bk_release-notes/content/comp_versions.html', 'https://docs.hortonworks.com/HDPDocuments/HDP2/HDP-2.6.2/bk_release-notes/content/comp_versions.html', 'https://docs.hortonworks.com/HDPDocuments/HDP2/HDP-2.6.1/bk_release-notes/content/comp_versions.html', 'https://docs.hortonworks.com/HDPDocuments/HDP2/HDP-2.6.0/bk_release-notes/content/comp_versions.html', 'https://docs.hortonworks.com/HDPDocuments/HDP2/HDP-2.5.6/bk_release-notes/content/comp_versions.html', 'https://docs.hortonworks.com/HDPDocuments/HDP2/HDP-2.5.5/bk_release-notes/content/comp_versions.html', 'https://docs.hortonworks.com/HDPDocuments/HDP2/HDP-2.5.4/bk_release-notes/content/comp_versions.html', 'https://docs.hortonworks.com/HDPDocuments/HDP2/HDP-2.5.3/bk_release-notes/content/comp_versions.html', 'https://docs.hortonworks.com/HDPDocuments/HDP2/HDP-2.5.2/bk_release-notes/content/comp_versions.html', 'https://docs.hortonworks.com/HDPDocuments/HDP2/HDP-2.5.1/bk_release-notes/content/comp_versions.html', 'https://docs.hortonworks.com/HDPDocuments/HDP2/HDP-2.5.0/bk_release-notes/content/comp_versions.html', 'https://docs.hortonworks.com/HDPDocuments/HDP2/HDP-2.4.3/bk_HDP_RelNotes/content/ch_relnotes_v243.html', 'https://docs.hortonworks.com/HDPDocuments/HDP2/HDP-2.4.2/bk_HDP_RelNotes/content/ch_relnotes_v243.html', 'https://docs.hortonworks.com/HDPDocuments/HDP2/HDP-2.4.1/bk_HDP_RelNotes/content/ch_relnotes_v243.html', 'https://docs.hortonworks.com/HDPDocuments/HDP2/HDP-2.4.0/bk_HDP_RelNotes/content/ch_relnotes_v243.html'] def parse(self, response): if response.url.split("/")[4] == 'HDP3': all_ = response.xpath("//section[@class='section']/ul/li/p/text()").getall() if len(all_) == 0: all_ = response.xpath("//div[@class='body conbody']/ul/li/p/text()").getall() else: all_ = response.xpath("//div[@class='itemizedlist']/ul/li/p/text()").getall() # print(all_) for i in all_: if i.strip() == '': continue if i.strip().split().__len__() < 2: continue if i.strip().replace("Apache ","").split().__len__() < 2: continue yield {"Product": i.replace("Apache ","").split()[0], "Version": i.replace("Apache ","").split()[1].replace("+",""), "HDP": response.url.split("/")[5].split("-")[1]} spider = VersionSpider() process = CrawlerProcess() process.crawl(spider) process.start()
nilq/baby-python
python
from typing import Dict, Optional, Union from pyspark.sql import SparkSession, Column, DataFrame # noinspection PyUnresolvedReferences from pyspark.sql.functions import col from pyspark.sql.types import ( ArrayType, LongType, StringType, StructField, StructType, TimestampType, DataType, ) from spark_auto_mapper.automappers.automapper import AutoMapper from spark_auto_mapper.data_types.complex.complex_base import ( AutoMapperDataTypeComplexBase, ) from spark_auto_mapper.data_types.data_type_base import AutoMapperDataTypeBase from spark_auto_mapper.data_types.list import AutoMapperList from spark_auto_mapper.data_types.number import AutoMapperNumberDataType from spark_auto_mapper.data_types.text_like_base import AutoMapperTextLikeBase from spark_auto_mapper.helpers.automapper_helpers import AutoMapperHelpers as A from spark_auto_mapper.type_definitions.defined_types import AutoMapperDateInputType class MyProcessingStatusExtensionItem(AutoMapperDataTypeComplexBase): # noinspection PyPep8Naming def __init__( self, url: str, valueString: Optional[AutoMapperTextLikeBase] = None, valueDateTime: Optional[AutoMapperDateInputType] = None, ) -> None: super().__init__(url=url, valueString=valueString, valueDateTime=valueDateTime) class MyProcessingStatusExtension(AutoMapperDataTypeComplexBase): # noinspection PyPep8Naming def __init__( self, processing_status: AutoMapperTextLikeBase, request_id: AutoMapperTextLikeBase, date_processed: Optional[AutoMapperDateInputType] = None, ) -> None: definition_base_url = "https://raw.githubusercontent.com/imranq2/SparkAutoMapper.FHIR/main/StructureDefinition/" processing_status_extensions = [ MyProcessingStatusExtensionItem( url="processing_status", valueString=processing_status, ), MyProcessingStatusExtensionItem( url="request_id", valueString=request_id, ), ] if date_processed: processing_status_extensions.append( MyProcessingStatusExtensionItem( url="date_processed", valueDateTime=date_processed, ) ) self.extensions = processing_status_extensions super().__init__( url=definition_base_url, extension=AutoMapperList(processing_status_extensions), ) def include_null_properties(self, include_null_properties: bool) -> None: for item in self.extensions: item.include_null_properties( include_null_properties=include_null_properties ) def get_schema( self, include_extension: bool ) -> Optional[Union[StructType, DataType]]: return StructType( [ StructField("url", StringType()), StructField( "extension", ArrayType( StructType( [ StructField("url", StringType()), StructField("valueString", StringType()), StructField("valueDateTime", TimestampType()), ] ) ), ), ] ) def get_value( self, value: AutoMapperDataTypeBase, source_df: Optional[DataFrame], current_column: Optional[Column], ) -> Column: return super().get_value(value, source_df, current_column) class MyClass(AutoMapperDataTypeComplexBase): def __init__( self, name: AutoMapperTextLikeBase, age: AutoMapperNumberDataType, extension: AutoMapperList[MyProcessingStatusExtension], ) -> None: super().__init__(name=name, age=age, extension=extension) def get_schema( self, include_extension: bool ) -> Optional[Union[StructType, DataType]]: schema: StructType = StructType( [ StructField("name", StringType(), False), StructField("age", LongType(), True), ] ) return schema def test_auto_mapper_complex_with_extension(spark_session: SparkSession) -> None: # Arrange spark_session.createDataFrame( [ (1, "Qureshi", "Imran", 45), (2, "Vidal", "Michael", 35), ], ["member_id", "last_name", "first_name", "my_age"], ).createOrReplaceTempView("patients") source_df: DataFrame = spark_session.table("patients") df = source_df.select("member_id") df.createOrReplaceTempView("members") # Act mapper = AutoMapper( view="members", source_view="patients", keys=["member_id"], drop_key_columns=False, ).complex( MyClass( name=A.column("last_name"), age=A.number(A.column("my_age")), extension=AutoMapperList( [ MyProcessingStatusExtension( processing_status=A.text("foo"), request_id=A.text("bar"), date_processed=A.date("2021-01-01"), ) ] ), ) ) assert isinstance(mapper, AutoMapper) sql_expressions: Dict[str, Column] = mapper.get_column_specs(source_df=source_df) for column_name, sql_expression in sql_expressions.items(): print(f"{column_name}: {sql_expression}") result_df: DataFrame = mapper.transform(df=df) # Assert assert str(sql_expressions["name"]) == str( col("b.last_name").cast("string").alias("name") ) assert str(sql_expressions["age"]) == str(col("b.my_age").cast("long").alias("age")) result_df.printSchema() result_df.show(truncate=False) assert result_df.where("member_id == 1").select("name").collect()[0][0] == "Qureshi" assert dict(result_df.dtypes)["age"] in ("int", "long", "bigint")
nilq/baby-python
python
""" The MIT License (MIT) Originally in 2020, for Python 3.x Copyright (c) 2021 Panos Achlioptas (ai.stanford.edu/~optas) & Stanford Geometric Computing Lab """ import torch import numpy as np import pandas as pd from PIL import Image from torch.utils.data import Dataset, DataLoader from ..evaluation.emotion_alignment import image_to_emotion from ..emotions import emotion_to_int class AffectiveCaptionDataset(Dataset): """ Basically, an image, with a caption, and an indicated emotion. """ def __init__(self, image_files, tokens, emotions, n_emotions=9, img_transform=None, one_hot_emo=True): super(AffectiveCaptionDataset, self).__init__() self.image_files = image_files self.tokens = tokens self.emotions = emotions self.n_emotions = n_emotions self.img_transform = img_transform self.one_hot_emo = one_hot_emo def __getitem__(self, index): text = np.array(self.tokens[index]).astype(dtype=np.long) if self.image_files is not None: img = Image.open(self.image_files[index]) if img.mode is not 'RGB': img = img.convert('RGB') if self.img_transform is not None: img = self.img_transform(img) else: img = [] if self.n_emotions > 0: if self.one_hot_emo: emotion = np.zeros(self.n_emotions, dtype=np.float32) emotion[self.emotions[index]] = 1 else: emotion = self.emotions[index] else: emotion = [] res = {'image': img, 'emotion': emotion, 'tokens': text, 'index': index} return res def __len__(self): return len(self.tokens) class ImageClassificationDataset(Dataset): def __init__(self, image_files, labels=None, img_transform=None, rgb_only=True): super(ImageClassificationDataset, self).__init__() self.image_files = image_files self.labels = labels self.img_transform = img_transform self.rgb_only = rgb_only def __getitem__(self, index): img = Image.open(self.image_files[index]) if self.rgb_only and img.mode is not 'RGB': img = img.convert('RGB') if self.img_transform is not None: img = self.img_transform(img) label = [] if self.labels is not None: label = self.labels[index] res = {'image': img, 'label': label, 'index': index} return res def __len__(self): return len(self.image_files) def sub_sample_dataloader(dataloader, sample_size, seed=None, shuffle=False): """ Given any torch dataloader create a sub-sampled version of it. :param dataloader: :param sample_size: :param seed: :param shuffle: :return: dataloader of Subset """ dataset = dataloader.dataset n_total = len(dataset) if sample_size > n_total: raise ValueError if seed is not None: torch.manual_seed(seed) sb_dataset = torch.utils.data.random_split(dataset, [sample_size, n_total-sample_size])[0] bsize = min(dataloader.batch_size, sample_size) sample_loader = torch.utils.data.DataLoader(dataset=sb_dataset, batch_size=bsize, shuffle=shuffle, num_workers=dataloader.num_workers) return sample_loader def sub_index_affective_dataloader(affective_dataloader, indices, shuffle=False): """ Given a torch dataloader and a sequence of integers; extract the corresponding items of the carried dataset on the specific indices and make a new dataloader with them. :param affective_dataloader: torch.utils.data.DataLoader for AffectiveCaptionDataset :param indices: sequence of integers indexing the underlying dataset (dataframe). :param shuffle: shuffle the data of the resulting dataloader :return: dataloader of AffectiveCaptionDataset """ dataset = affective_dataloader.dataset r_img_files = dataset.image_files.iloc[indices].copy() r_tokens = dataset.tokens.iloc[indices].copy() r_emotions = dataset.emotions.iloc[indices].copy() r_img_files.reset_index(inplace=True, drop=True) r_tokens.reset_index(inplace=True, drop=True) r_emotions.reset_index(inplace=True, drop=True) r_dset = AffectiveCaptionDataset(image_files=r_img_files, tokens=r_tokens, emotions=r_emotions, img_transform=dataset.img_transform) batch_size = min(len(indices), affective_dataloader.batch_size) r_loader = torch.utils.data.DataLoader(r_dset, shuffle=shuffle, batch_size=batch_size, num_workers=affective_dataloader.num_workers) return r_loader def group_annotations_per_image(affective_dataset): """ Group the annotations per image. :param affective_dataset: an AffectiveCaptionDataset :return: for each image its tokens/emotions as pandas Dataframes """ df = pd.concat([affective_dataset.image_files, affective_dataset.tokens, affective_dataset.emotions], axis=1) tokens_grouped = df.groupby('image_files')['tokens_encoded'].apply(list).reset_index(name='tokens_encoded') emotion_grouped = df.groupby('image_files')['emotion_label'].apply(list).reset_index(name='emotion') assert all(tokens_grouped['image_files'] == emotion_grouped['image_files']) return tokens_grouped['image_files'], tokens_grouped, emotion_grouped def default_grounding_dataset_from_affective_loader(loader, img2emo_clf=None, device=None, n_workers=None): """ Convenience function. Given a loader carrying an affective dataset, make a new loader only w.r.t. unique images of the dataset, & optionally add to each image the emotion predicted by the img2emo_clf. The new loader can be used to sample utterances over the unique images. :param loader: :param img2emo_clf: :param device: :return: """ affective_dataset = loader.dataset img_files, tokens, emotions = group_annotations_per_image(affective_dataset) img_trans = affective_dataset.img_transform batch_size = loader.batch_size if n_workers is None: n_workers = loader.num_workers dummy = pd.Series(np.ones(len(img_files), dtype=int) * -1) # possibly predict grounding emotions if img2emo_clf is not None: temp_dataset = ImageClassificationDataset(image_files=img_files, img_transform=img_trans) img_dataloader = DataLoader(temp_dataset, batch_size, num_workers=n_workers) emo_pred_distribution = image_to_emotion(img2emo_clf, img_dataloader, device) grounding_emo = pd.Series(emo_pred_distribution.argmax(-1).tolist()) # use maximizer of emotions. else: grounding_emo = dummy new_dataset = AffectiveCaptionDataset(img_files, tokens=dummy, emotions=grounding_emo, img_transform=img_trans) new_loader = DataLoader(dataset=new_dataset, batch_size=batch_size, num_workers=n_workers) return new_loader def custom_grounding_dataset_similar_to_affective_loader(grounding_data_csv, loader, n_workers=None): """ Convenience function. Given a csv indicating (grounding) images on the hard-drive and a loader carrying an affective dataset, make a new loader with the csv images using the same configuration (e.g., img_transform) as the loader. :param grounding_data_csv: (csv filename) - has to have one column named "image_file" that corresponds to the file-names of the images. - (optionally) can have also a "grounding_emotion" column with values like "contentment" :param loader: :return: """ df = pd.read_csv(grounding_data_csv) image_files = df['image_file'] dummy = pd.Series(np.ones(len(image_files), dtype=int) * -1) if 'grounding_emotion' in df.columns: emotions = df.grounding_emotion.apply(emotion_to_int) else: emotions = dummy standard_dset = loader.dataset custom_dataset = AffectiveCaptionDataset(image_files, dummy, emotions=emotions, n_emotions=standard_dset.n_emotions, img_transform=standard_dset.img_transform, one_hot_emo=standard_dset.one_hot_emo) if n_workers is None: n_workers = loader.num_workers custom_data_loader = torch.utils.data.DataLoader(dataset=custom_dataset, batch_size=min(loader.batch_size, len(custom_dataset)), num_workers=n_workers) return custom_data_loader
nilq/baby-python
python
""" Last edited: November 12 2020 |br| @author: FINE Developer Team (FZJ IEK-3) """ from FINE.component import Component, ComponentModel from FINE import utils from tsam.timeseriesaggregation import TimeSeriesAggregation import pandas as pd import numpy as np import pyomo.environ as pyomo import pyomo.opt as opt import time import warnings warnings.filterwarnings("ignore", category=FutureWarning) warnings.filterwarnings("ignore", category=DeprecationWarning) warnings.filterwarnings("always", category=UserWarning) class EnergySystemModel: """ EnergySystemModel class The functionality provided by the EnergySystemModel class is fourfold:\n * With it, the **basic structure** (spatial and temporal resolution, considered commodities) of the investigated energy system is defined. * It serves as a **container for all components** investigated in the energy system model. These components, namely sources and sinks, conversion options, storage options, and transmission options (in the core module), can be added to an EnergySystemModel instance. * It provides the core functionality of **modeling and optimizing the energy system** based on the specified structure and components on the one hand and of specified simulation parameters on the other hand. * It **stores optimization results** which can then be post-processed with other modules. The parameters which are stored in an instance of the class refer to:\n * the modeled spatial representation of the energy system (**locations, lengthUnit**) * the modeled temporal representation of the energy system (**totalTimeSteps, hoursPerTimeStep, years, periods, periodsOrder, periodsOccurrences, timeStepsPerPeriod, interPeriodTimeSteps, isTimeSeriesDataClustered, typicalPeriods, tsaInstance, timeUnit**) * the considered commodities in the energy system (**commodities, commodityUnitsDict**) * the considered components in the energy system (**componentNames, componentModelingDict, costUnit**) * optimization related parameters (**pyM, solverSpecs**)\n The parameters are first set when a class instance is initiated. The parameters which are related to the components (e.g. componentNames) are complemented by adding the components to the class instance. Instances of this class provide functions for\n * adding components and their respective modeling classes (**add**) * clustering the time series data of all added components using the time series aggregation package tsam, cf. https://github.com/FZJ-IEK3-VSA/tsam (**cluster**) * optimizing the specified energy system (**optimize**), for which a pyomo concrete model instance is built and filled with \n (0) basic time sets, \n (1) sets, variables and constraints contributed by the component modeling classes, \n (2) basic, component overreaching constraints, and \n (3) an objective function. \n The pyomo instance is then optimized by a specified solver. The optimization results are processed once available. * getting components and their attributes (**getComponent, getCompAttr, getOptimizationSummary**) Last edited: November 12 2020 |br| @author: FINE Developer Team (FZJ IEK-3) """ def __init__(self, locations, commodities, commodityUnitsDict, numberOfTimeSteps=8760, hoursPerTimeStep=1, costUnit='1e9 Euro', lengthUnit='km', verboseLogLevel=0, balanceLimit=None, lowerBound=False): """ Constructor for creating an EnergySystemModel class instance **Required arguments:** :param locations: locations considered in the energy system :type locations: set of strings :param commodities: commodities considered in the energy system :type commodities: set of strings :param commodityUnitsDict: dictionary which assigns each commodity a quantitative unit per time (e.g. GW_el, GW_H2, Mio.t_CO2/h). The dictionary is used for results output. Note for advanced users: the scale of these units can influence the numerical stability of the optimization solver, cf. http://files.gurobi.com/Numerics.pdf where a reasonable range of model coefficients is suggested. :type commodityUnitsDict: dictionary of strings **Default arguments:** :param numberOfTimeSteps: number of time steps considered when modeling the energy system (for each time step, or each representative time step, variables and constraints are constituted). Together with the hoursPerTimeStep, the total number of hours considered can be derived. The total number of hours is again used for scaling the arising costs to the arising total annual costs (TAC) which are minimized during optimization. |br| * the default value is 8760. :type totalNumberOfHours: strictly positive integer :param hoursPerTimeStep: hours per time step |br| * the default value is 1 :type hoursPerTimeStep: strictly positive float :param costUnit: cost unit of all cost related values in the energy system. This argument sets the unit of all cost parameters which are given as an input to the EnergySystemModel instance (e.g. for the invest per capacity or the cost per operation). Note for advanced users: the scale of this unit can influence the numerical stability of the optimization solver, cf. http://files.gurobi.com/Numerics.pdf where a reasonable range of model coefficients is suggested. |br| * the default value is '10^9 Euro' (billion euros), which can be a suitable scale for national energy systems. :type costUnit: string :param lengthUnit: length unit for all length-related values in the energy system. Note for advanced users: the scale of this unit can influence the numerical stability of the optimization solver, cf. http://files.gurobi.com/Numerics.pdf where a reasonable range of model coefficients is suggested. |br| * the default value is 'km' (kilometers). :type lengthUnit: string :param verboseLogLevel: defines how verbose the console logging is:\n - 0: general model logging, warnings and optimization solver logging are displayed. - 1: warnings are displayed. - 2: no general model logging or warnings are displayed, the optimization solver logging is set to a minimum.\n Note: if required, the optimization solver logging can be separately enabled in the optimizationSpecs of the optimize function. |br| * the default value is 0 :type verboseLogLevel: integer (0, 1 or 2) :param balanceLimit: defines the balanceLimit constraint (various different balanceLimitIDs possible) for specific regions or the whole model. The balancelimitID can be assigned to various components of e.g. SourceSinkModel or TransmissionModel to limit the balance of production, consumption and im/export. If the balanceLimit is passed as pd.Series it will apply to the overall model, if it is passed as pd.Dataframe each column will apply to one region of the multi-node model. In the latter case, the number and names of the columns should match the regions/region names in the model. Each row contains an individual balanceLimitID as index and the corresponding values for the model (pd.Series) or regions (pd.Dataframe). Values are always given in the unit of the esM commodities unit. Note: If bounds for sinks shall be specified (e.g. min. export, max. sink volume), values must be defined as negative. Example: pd.DataFrame(columns=["Region1"], index=["electricity"], data=[1000]) |br| * the default value is None :type balanceLimit: pd.DataFrame or pd.Series :param lowerBound: defines whether a lowerBound or an upperBound is considered in the balanceLimitConstraint. By default an upperBound is considered. However, multiple cases can be considered: 1) Sources: a) LowerBound=False: UpperBound for commodity from SourceComponent (Define positive value in balanceLimit). Example: Limit CO2-Emission b) LowerBound=True: LowerBound for commodity from SourceComponent (Define positive value in balanceLimit). Example: Require minimum production from renewables. 2) Sinks: a) LowerBound=False: UpperBound in a mathematical sense for commodity from SinkComponent (Logically minimum limit for negative values, define negative value in balanceLimit). Example: Minimum export/consumption of hydrogen. b) LowerBound=True: LowerBound in a mathematical sense for commodity from SourceComponent (Logically maximum limit for negative values, define negative value in balanceLimit). Example: Define upper limit for Carbon Capture & Storage. |br| * the default value is False :type lowerBound: bool """ # Check correctness of inputs utils.checkEnergySystemModelInput(locations, commodities, commodityUnitsDict, numberOfTimeSteps, hoursPerTimeStep, costUnit, lengthUnit, balanceLimit) ################################################################################################################ # Spatial resolution parameters # ################################################################################################################ # The locations (set of string) name the considered locations in an energy system model instance. The parameter # is used throughout the build of the energy system model to validate inputs and declare relevant sets, # variables and constraints. # The length unit refers to the measure of length referred throughout the model. # The balanceLimit can be used to limit certain balanceLimitIDs defined in the components. self.locations, self.lengthUnit = locations, lengthUnit self.numberOfTimeSteps = numberOfTimeSteps self.balanceLimit = balanceLimit self.lowerBound = lowerBound ################################################################################################################ # Time series parameters # ################################################################################################################ # The totalTimeSteps parameter (list, ranging from 0 to the total numberOfTimeSteps-1) refers to the total # number of time steps considered when modeling the specified energy system. The parameter is used for # validating time series data input and for setting other time series parameters when modeling a full temporal # resolution. The hoursPerTimeStep parameter (float > 0) refers to the temporal length of a time step in the # totalTimeSteps. From the numberOfTimeSteps and the hoursPerTimeStep the numberOfYears parameter is computed. self.totalTimeSteps, self.hoursPerTimeStep = list(range(numberOfTimeSteps)), hoursPerTimeStep self.numberOfYears = numberOfTimeSteps * hoursPerTimeStep / 8760.0 # The periods parameter (list, [0] when considering a full temporal resolution, range of [0, ..., # totalNumberOfTimeSteps/numberOfTimeStepsPerPeriod] when applying time series aggregation) represents # the periods considered when modeling the energy system. Only one period exists when considering the full # temporal resolution. When applying time series aggregation, the full time series are broken down into # periods to which a typical period is assigned to. # These periods have an order which is stored in the periodsOrder parameter (list, [0] when considering a full # temporal resolution, [typicalPeriod(0), ... , # typicalPeriod(totalNumberOfTimeSteps/numberOfTimeStepsPerPeriod-1)] when applying time series aggregation). # The occurrences of these periods are stored in the periodsOccurrences parameter (list, [1] when considering a # full temporal resolution, [occurrences(0), ..., occurrences(numberOfTypicalPeriods-1)] when applying time # series aggregation). self.periods, self.periodsOrder, self.periodOccurrences = [0], [0], [1] self.timeStepsPerPeriod = list(range(numberOfTimeSteps)) self.interPeriodTimeSteps = list(range(int(len(self.totalTimeSteps) / len(self.timeStepsPerPeriod)) + 1)) # The isTimeSeriesDataClustered parameter is used to check data consistency. # It is set to True if the class' cluster function is called. It is set to False if a new component is added. # If the cluster function is called, the typicalPeriods parameter is set from None to # [0, ..., numberOfTypicalPeriods-1] and, if specified, the resulting TimeSeriesAggregation instance is stored # in the tsaInstance parameter (default None). # The time unit refers to time measure referred throughout the model. Currently, it has to be an hour 'h'. self.isTimeSeriesDataClustered, self.typicalPeriods, self.tsaInstance = False, None, None self.timeUnit = 'h' ################################################################################################################ # Commodity specific parameters # ################################################################################################################ # The commodities parameter is a set of strings which describes what commodities are considered in the energy # system, and hence, which commodity balances need to be considered in the energy system model and its # optimization. # The commodityUnitsDict parameter is a dictionary which assigns each considered commodity (string) a # unit (string) which can be used by results output functions. self.commodities = commodities self.commodityUnitsDict = commodityUnitsDict ################################################################################################################ # Component specific parameters # ################################################################################################################ # The componentNames parameter is a set of strings in which all in the EnergySystemModel instance considered # components are stored. It is used to check that all components have unique indices. # The componentModelingDict is a dictionary (modelingClass name: modelingClass instance) in which the in the # energy system considered modeling classes are stored (in which again the components modeled with the # modelingClass as well as the equations to model them with are stored). # The costUnit parameter (string) is the parameter in which all cost input parameter have to be specified. self.componentNames = {} self.componentModelingDict = {} self.costUnit = costUnit ################################################################################################################ # Optimization parameters # ################################################################################################################ # The pyM parameter is None when the EnergySystemModel is initialized. After calling the optimize function, # the pyM parameter stores a Concrete Pyomo Model instance which contains parameters, sets, variables, # constraints and objective required for the optimization set up and solving. # The solverSpecs parameter is a dictionary (string: param) which stores different parameters that are used # for solving the optimization problem. The parameters are: solver (string, solver which is used to solve # the optimization problem), optimizationSpecs (string, representing **kwargs for the solver), hasTSA (boolean, # indicating if time series aggregation is used for the optimization), buildtime (positive float, time needed # to declare the optimization problem in seconds), solvetime (positive float, time needed to solve the # optimization problem in seconds), runtime (positive float, runtime of the optimization run in seconds), # timeLimit (positive float or None, if specified, indicates the maximum allowed runtime of the solver), # threads (positive int, number of threads used for optimization, can depend on solver), logFileName # (string, name of logfile). # The objectiveValue parameter is None when the EnergySystemModel is initialized. After calling the # optimize function, the objective value (i.e. TAC of the analyzed energy system) is stored in the # objectiveValue parameter for easier access. self.pyM = None self.solverSpecs = {'solver': '', 'optimizationSpecs': '', 'hasTSA': False, 'buildtime': 0, 'solvetime': 0, 'runtime': 0, 'timeLimit': None, 'threads': 0, 'logFileName': ''} self.objectiveValue = None ################################################################################################################ # General model parameters # ################################################################################################################ # The verbose parameter defines how verbose the console logging is: 0: general model logging, warnings # and optimization solver logging are displayed, 1: warnings are displayed, 2: no general model logging or # warnings are displayed, the optimization solver logging is set to a minimum. # The optimization solver logging can be separately enabled in the optimizationSpecs of the optimize function. self.verbose = verboseLogLevel def add(self, component): """ Function for adding a component and, if required, its respective modeling class to the EnergySystemModel instance. The added component has to inherit from the FINE class Component. :param component: the component to be added :type component: An object which inherits from the FINE Component class """ if not issubclass(type(component), Component): raise TypeError('The added component has to inherit from the FINE class Component.') if not issubclass(component.modelingClass, ComponentModel): print(component.name, component.modelingClass, ComponentModel) raise TypeError('The added component has to inherit from the FINE class ComponentModel.') component.addToEnergySystemModel(self) def removeComponent(self, componentName, track=False): """ Function which removes a component from the energy system. :param componentName: name of the component that should be removed :type componentName: string :param track: specifies if the removed components should be tracked or not |br| * the default value is False :type track: boolean :returns: dictionary with the removed componentName and component instance if track is set to True else None. :rtype: dict or None """ # Test if component exists if componentName not in self.componentNames.keys(): raise ValueError('The component ' + componentName + ' cannot be found in the energy system model.\n' + 'The components considered in the model are: ' + str(self.componentNames.keys())) modelingClass = self.componentNames[componentName] removedComp = dict() # If track: Return a dictionary including the name of the removed component and the component instance if track: removedComp = dict({componentName : self.componentModelingDict[modelingClass].componentsDict.pop(componentName)}) # Remove component from the componentNames dict: del self.componentNames[componentName] # Test if all components of one modelingClass are removed. If so, remove modelingClass: if not self.componentModelingDict[modelingClass].componentsDict: # False if dict is empty del self.componentModelingDict[modelingClass] return removedComp else: # Remove component from the componentNames dict: del self.componentNames[componentName] # Remove component from the componentModelingDict: del self.componentModelingDict[modelingClass].componentsDict[componentName] # Test if all components of one modelingClass are removed. If so, remove modelingClass: if not self.componentModelingDict[modelingClass].componentsDict: # False if dict is empty del self.componentModelingDict[modelingClass] return None def getComponent(self, componentName): """ Function which returns a component of the energy system. :param componentName: name of the component that should be returned :type componentName: string :returns: the component which has the name componentName :rtype: Component """ if componentName not in self.componentNames.keys(): raise ValueError('The component ' + componentName + ' cannot be found in the energy system model.\n' + 'The components considered in the model are: ' + str(self.componentNames.keys())) modelingClass = self.componentNames[componentName] return self.componentModelingDict[modelingClass].componentsDict[componentName] def getComponentAttribute(self, componentName, attributeName): """ Function which returns an attribute of a component considered in the energy system. :param componentName: name of the component from which the attribute should be obtained :type componentName: string :param attributeName: name of the attribute that should be returned :type attributeName: string :returns: the attribute specified by the attributeName of the component with the name componentName :rtype: depends on the specified attribute """ return getattr(self.getComponent(componentName), attributeName) def getOptimizationSummary(self, modelingClass, outputLevel=0): """ Function which returns the optimization summary (design variables, aggregated operation variables, objective contributions) of a modeling class. :param modelingClass: name of the modeling class from which the optimization summary should be obtained :type modelingClass: string :param outputLevel: states the level of detail of the output summary: \n - 0: full optimization summary is returned \n - 1: full optimization summary is returned but rows in which all values are NaN (not a number) are dropped\n - 2: full optimization summary is returned but rows in which all values are NaN or 0 are dropped \n |br| * the default value is 0 :type outputLevel: integer (0, 1 or 2) :returns: the optimization summary of the requested modeling class :rtype: pandas DataFrame """ if outputLevel == 0: return self.componentModelingDict[modelingClass].optSummary elif outputLevel == 1: return self.componentModelingDict[modelingClass].optSummary.dropna(how='all') else: if outputLevel != 2 and self.verbose < 2: warnings.warn('Invalid input. An outputLevel parameter of 2 is assumed.') df = self.componentModelingDict[modelingClass].optSummary.dropna(how='all') return df.loc[((df != 0) & (~df.isnull())).any(axis=1)] def cluster(self, numberOfTypicalPeriods=7, numberOfTimeStepsPerPeriod=24, segmentation=False, numberOfSegmentsPerPeriod=24, clusterMethod='hierarchical', sortValues=True, storeTSAinstance=False, **kwargs): """ Cluster the time series data of all components considered in the EnergySystemModel instance and then stores the clustered data in the respective components. For this, the time series data is broken down into an ordered sequence of periods (e.g. 365 days) and to each period a typical period (e.g. 7 typical days with 24 hours) is assigned. Moreover, the time steps within the periods can further be clustered to bigger time steps with an irregular duration using the segmentation option. For the clustering itself, the tsam package is used (cf. https://github.com/FZJ-IEK3-VSA/tsam). Additional keyword arguments for the TimeSeriesAggregation instance can be added (facilitated by kwargs). As an example: it might be useful to add extreme periods to the clustered typical periods. Note: The segmentation option can be freely combined with all subclasses. However, an irregular time step length is not meaningful for the minimumDownTime and minimumUpTime in the conversionDynamic module, because the time would be different for each segment. The same holds true for the DSM module. **Default arguments:** :param numberOfTypicalPeriods: states the number of typical periods into which the time series data should be clustered. The number of time steps per period must be an integer multiple of the total number of considered time steps in the energy system. Note: Please refer to the tsam package documentation of the parameter noTypicalPeriods for more information. |br| * the default value is 7 :type numberOfTypicalPeriods: strictly positive integer :param numberOfTimeStepsPerPeriod: states the number of time steps per period |br| * the default value is 24 :type numberOfTimeStepsPerPeriod: strictly positive integer :param segmentation: states whether the typical periods should be further segmented to fewer time steps |br| * the default value is False :type segmentation: boolean :param numberOfSegmentsPerPeriod: states the number of segments per period |br| * the default value is 24 :type numberOfSegmentsPerPeriod: strictly positive integer :param clusterMethod: states the method which is used in the tsam package for clustering the time series data. Options are for example 'averaging','k_means','exact k_medoid' or 'hierarchical'. Note: Please refer to the tsam package documentation of the parameter clusterMethod for more information. |br| * the default value is 'hierarchical' :type clusterMethod: string :param sortValues: states if the algorithm in the tsam package should use (a) the sorted duration curves (-> True) or (b) the original profiles (-> False) of the time series data within a period for clustering. Note: Please refer to the tsam package documentation of the parameter sortValues for more information. |br| * the default value is True :type sortValues: boolean :param storeTSAinstance: states if the TimeSeriesAggregation instance created during clustering should be stored in the EnergySystemModel instance. |br| * the default value is False :type storeTSAinstance: boolean Last edited: November 12 2020 |br| @author: FINE Developer Team (FZJ IEK-3) """ # Check input arguments which have to fit the temporal representation of the energy system utils.checkClusteringInput(numberOfTypicalPeriods, numberOfTimeStepsPerPeriod, len(self.totalTimeSteps)) if segmentation: if numberOfSegmentsPerPeriod > numberOfTimeStepsPerPeriod: if self.verbose < 2: warnings.warn('The chosen number of segments per period exceeds the number of time steps per' 'period. The number of segments per period is set to the number of time steps per ' 'period.') numberOfSegmentsPerPeriod = numberOfTimeStepsPerPeriod hoursPerPeriod = int(numberOfTimeStepsPerPeriod*self.hoursPerTimeStep) timeStart = time.time() if segmentation: utils.output('\nClustering time series data with ' + str(numberOfTypicalPeriods) + ' typical periods and ' + str(numberOfTimeStepsPerPeriod) + ' time steps per period \nfurther clustered to ' + str(numberOfSegmentsPerPeriod) + ' segments per period...', self.verbose, 0) else: utils.output('\nClustering time series data with ' + str(numberOfTypicalPeriods) + ' typical periods and ' + str(numberOfTimeStepsPerPeriod) + ' time steps per period...', self.verbose, 0) # Format data to fit the input requirements of the tsam package: # (a) append the time series data from all components stored in all initialized modeling classes to a pandas # DataFrame with unique column names # (b) thereby collect the weights which should be considered for each time series as well in a dictionary timeSeriesData, weightDict = [], {} for mdlName, mdl in self.componentModelingDict.items(): for compName, comp in mdl.componentsDict.items(): compTimeSeriesData, compWeightDict = comp.getDataForTimeSeriesAggregation() if compTimeSeriesData is not None: timeSeriesData.append(compTimeSeriesData), weightDict.update(compWeightDict) timeSeriesData = pd.concat(timeSeriesData, axis=1) # Note: Sets index for the time series data. The index is of no further relevance in the energy system model. timeSeriesData.index = pd.date_range('2050-01-01 00:30:00', periods=len(self.totalTimeSteps), freq=(str(self.hoursPerTimeStep) + 'H'), tz='Europe/Berlin') # Cluster data with tsam package (the reindex call is here for reproducibility of TimeSeriesAggregation # call) depending on whether segmentation is activated or not timeSeriesData = timeSeriesData.reindex(sorted(timeSeriesData.columns), axis=1) if segmentation: clusterClass = TimeSeriesAggregation(timeSeries=timeSeriesData, noTypicalPeriods=numberOfTypicalPeriods, segmentation=segmentation, noSegments=numberOfSegmentsPerPeriod, hoursPerPeriod=hoursPerPeriod, clusterMethod=clusterMethod, sortValues=sortValues, weightDict=weightDict, **kwargs) # Convert the clustered data to a pandas DataFrame with the first index as typical period number and the # second index as segment number per typical period. data = pd.DataFrame.from_dict(clusterClass.clusterPeriodDict).reset_index(level=2, drop=True) # Get the length of each segment in each typical period with the first index as typical period number and # the second index as segment number per typical period. timeStepsPerSegment = pd.DataFrame.from_dict(clusterClass.segmentDurationDict)['Segment Duration'] else: clusterClass = TimeSeriesAggregation(timeSeries=timeSeriesData, noTypicalPeriods=numberOfTypicalPeriods, hoursPerPeriod=hoursPerPeriod, clusterMethod=clusterMethod, sortValues=sortValues, weightDict=weightDict, **kwargs) # Convert the clustered data to a pandas DataFrame with the first index as typical period number and the # second index as time step number per typical period. data = pd.DataFrame.from_dict(clusterClass.clusterPeriodDict) # Store the respective clustered time series data in the associated components for mdlName, mdl in self.componentModelingDict.items(): for compName, comp in mdl.componentsDict.items(): comp.setAggregatedTimeSeriesData(data) # Store time series aggregation parameters in class instance if storeTSAinstance: self.tsaInstance = clusterClass self.typicalPeriods = clusterClass.clusterPeriodIdx self.timeStepsPerPeriod = list(range(numberOfTimeStepsPerPeriod)) self.segmentation = segmentation if segmentation: self.segmentsPerPeriod = list(range(numberOfSegmentsPerPeriod)) self.timeStepsPerSegment = timeStepsPerSegment self.hoursPerSegment = self.hoursPerTimeStep * self.timeStepsPerSegment # Define start time hour of each segment in each typical period segmentStartTime = self.hoursPerSegment.groupby(level=0).cumsum() segmentStartTime.index = segmentStartTime.index.set_levels(segmentStartTime.index.levels[1] + 1, level=1) lvl0, lvl1 = segmentStartTime.index.levels segmentStartTime = segmentStartTime.reindex(pd.MultiIndex.from_product([lvl0, [0, *lvl1]])) segmentStartTime[segmentStartTime.index.get_level_values(1) == 0] = 0 self.segmentStartTime = segmentStartTime self.periods = list(range(int(len(self.totalTimeSteps) / len(self.timeStepsPerPeriod)))) self.interPeriodTimeSteps = list(range(int(len(self.totalTimeSteps) / len(self.timeStepsPerPeriod)) + 1)) self.periodsOrder = clusterClass.clusterOrder self.periodOccurrences = [(self.periodsOrder == tp).sum() for tp in self.typicalPeriods] # Set cluster flag to true (used to ensure consistently clustered time series data) self.isTimeSeriesDataClustered = True utils.output("\t\t(%.4f" % (time.time() - timeStart) + " sec)\n", self.verbose, 0) def declareTimeSets(self, pyM, timeSeriesAggregation, segmentation): """ Set and initialize basic time parameters and sets. :param pyM: a pyomo ConcreteModel instance which contains parameters, sets, variables, constraints and objective required for the optimization set up and solving. :type pyM: pyomo ConcreteModel :param timeSeriesAggregation: states if the optimization of the energy system model should be done with (a) the full time series (False) or (b) clustered time series data (True). |br| * the default value is False :type timeSeriesAggregation: boolean :param segmentation: states if the optimization of the energy system model based on clustered time series data should be done with (a) aggregated typical periods with the original time step length (False) or (b) aggregated typical periods with further segmented time steps (True). |br| * the default value is False :type segmentation: boolean """ # Store the information if aggregated time series data is considered for modeling the energy system in the pyomo # model instance and set the time series which is again considered for modeling in all components accordingly pyM.hasTSA = timeSeriesAggregation pyM.hasSegmentation = segmentation for mdl in self.componentModelingDict.values(): for comp in mdl.componentsDict.values(): comp.setTimeSeriesData(pyM.hasTSA) # Set the time set and the inter time steps set. The time set is a set of tuples. A tuple consists of two # entries, the first one indicates an index of a period and the second one indicates a time step inside that # period. If time series aggregation is not considered, only one period (period 0) exists and the time steps # range from 0 up until the specified number of total time steps - 1. Otherwise, the time set is initialized for # each typical period (0 ... numberOfTypicalPeriods-1) and the number of time steps per period (0 ... # numberOfTimeStepsPerPeriod-1). # The inter time steps set is a set of tuples as well, which again consist of two values. The first value again # indicates the period, however, the second one now refers to a point in time right before or after a time step # (or between two time steps). Hence, the second value reaches values from (0 ... numberOfTimeStepsPerPeriod). if not pyM.hasTSA: # Reset timeStepsPerPeriod in case it was overwritten by the clustering function self.timeStepsPerPeriod = self.totalTimeSteps self.interPeriodTimeSteps = list(range(int(len(self.totalTimeSteps) / len(self.timeStepsPerPeriod)) + 1)) self.periods = [0] self.periodsOrder = [0] self.periodOccurrences = [1] # Define sets def initTimeSet(pyM): return ((p, t) for p in self.periods for t in self.timeStepsPerPeriod) def initInterTimeStepsSet(pyM): return ((p, t) for p in self.periods for t in range(len(self.timeStepsPerPeriod) + 1)) else: if not pyM.hasSegmentation: utils.output('Time series aggregation specifications:\n' 'Number of typical periods:' + str(len(self.typicalPeriods)) + ', number of time steps per period:' + str(len(self.timeStepsPerPeriod)) + '\n', self.verbose, 0) # Define sets def initTimeSet(pyM): return ((p, t) for p in self.typicalPeriods for t in self.timeStepsPerPeriod) def initInterTimeStepsSet(pyM): return ((p, t) for p in self.typicalPeriods for t in range(len(self.timeStepsPerPeriod) + 1)) else: utils.output('Time series aggregation specifications:\n' 'Number of typical periods:' + str(len(self.typicalPeriods)) + ', number of time steps per period:' + str(len(self.timeStepsPerPeriod)) + ', number of segments per period:' + str(len(self.segmentsPerPeriod)) + '\n', self.verbose, 0) # Define sets def initTimeSet(pyM): return ((p, t) for p in self.typicalPeriods for t in self.segmentsPerPeriod) def initInterTimeStepsSet(pyM): return ((p, t) for p in self.typicalPeriods for t in range(len(self.segmentsPerPeriod) + 1)) # Initialize sets pyM.timeSet = pyomo.Set(dimen=2, initialize=initTimeSet) pyM.interTimeStepsSet = pyomo.Set(dimen=2, initialize=initInterTimeStepsSet) def declareBalanceLimitConstraint(self, pyM, timeSeriesAggregation): """ Declare balance limit constraint. Balance limit constraint can limit the exchange of commodities within the model or over the model region boundaries. See the documentation of the parameters for further explanation. In general the following equation applies: E_source - E_sink + E_exchange,in - E_exchange,out <= E_lim (self.LowerBound=False) E_source - E_sink + E_exchange,in - E_exchange,out >= E_lim (self.LowerBound=True) :param pyM: a pyomo ConcreteModel instance which contains parameters, sets, variables, constraints and objective required for the optimization set up and solving. :type pyM: pyomo ConcreteModel :param timeSeriesAggregation: states if the optimization of the energy system model should be done with (a) the full time series (False) or (b) clustered time series data (True). |br| * the default value is False :type timeSeriesAggregation: boolean """ balanceLimitDict = {} # 2 differentiations (or 4 cases). 1st: Locational or not; 2nd: lowerBound or not (lower bound) # DataFrame with locational input. Otherwise error is thrown in input check. if type(self.balanceLimit) == pd.DataFrame: for mdl_type, mdl in self.componentModelingDict.items(): if mdl_type=="SourceSinkModel" or mdl_type=="TransmissionModel": for compName, comp in mdl.componentsDict.items(): if comp.balanceLimitID is not None: [balanceLimitDict.setdefault((comp.balanceLimitID, loc), []).append(compName) for loc in self.locations] setattr(pyM, "balanceLimitDict", balanceLimitDict) def balanceLimitConstraint(pyM, ID, loc): # Check whether we want to consider an upper or lower bound. if not self.lowerBound: return sum(mdl.getBalanceLimitContribution(esM=self, pyM=pyM, ID=ID, timeSeriesAggregation=timeSeriesAggregation, loc=loc) for mdl_type, mdl in self.componentModelingDict.items() if ( mdl_type=="SourceSinkModel" or mdl_type=="TransmissionModel") ) <= self.balanceLimit.loc[ID, loc] else: return sum(mdl.getBalanceLimitContribution(esM=self, pyM=pyM, ID=ID, timeSeriesAggregation=timeSeriesAggregation, loc=loc) for mdl_type, mdl in self.componentModelingDict.items() if ( mdl_type=="SourceSinkModel" or mdl_type=="TransmissionModel") ) >= self.balanceLimit.loc[ID, loc] # Series as input. Whole model is considered. else: for mdl_type, mdl in self.componentModelingDict.items(): if mdl_type=="SourceSinkModel": for compName, comp in mdl.componentsDict.items(): if comp.balanceLimitID is not None: balanceLimitDict.setdefault((comp.balanceLimitID), []).append(compName) setattr(pyM, "balanceLimitDict", balanceLimitDict) def balanceLimitConstraint(pyM, ID): # Check wether we want to consider an upper or lower bound if not self.lowerBound: return sum(mdl.getBalanceLimitContribution(esM=self, pyM=pyM, ID=ID, timeSeriesAggregation=timeSeriesAggregation) for mdl_type, mdl in self.componentModelingDict.items() if ( mdl_type=="SourceSinkModel")) <= self.balanceLimit.loc[ID] else: return sum(mdl.getBalanceLimitContribution(esM=self, pyM=pyM, ID=ID, timeSeriesAggregation=timeSeriesAggregation) for mdl_type, mdl in self.componentModelingDict.items() if ( mdl_type == "SourceSinkModel")) >= self.balanceLimit.loc[ID] pyM.balanceLimitConstraint = \ pyomo.Constraint(pyM.balanceLimitDict.keys(), rule=balanceLimitConstraint) def declareSharedPotentialConstraints(self, pyM): """ Declare shared potential constraints, e.g. if a maximum potential of salt caverns has to be shared by salt cavern storing methane and salt caverns storing hydrogen. .. math:: \\underset{\\text{comp} \in \mathcal{C}^{ID}}{\sum} \\text{cap}^{comp}_{loc} / \\text{capMax}^{comp}_{loc} \leq 1 :param pyM: a pyomo ConcreteModel instance which contains parameters, sets, variables, constraints and objective required for the optimization set up and solving. :type pyM: pyomo ConcreteModel """ utils.output('Declaring shared potential constraint...', self.verbose, 0) # Create shared potential dictionary (maps a shared potential ID and a location to components who share the # potential) potentialDict = {} for mdl in self.componentModelingDict.values(): for compName, comp in mdl.componentsDict.items(): if comp.sharedPotentialID is not None: [potentialDict.setdefault((comp.sharedPotentialID, loc), []).append(compName) for loc in comp.locationalEligibility.index if comp.capacityMax[loc] != 0] pyM.sharedPotentialDict = potentialDict # Define and initialize constraints for each instance and location where components have to share an available # potential. Sum up the relative contributions to the shared potential and ensure that the total share is # <= 100%. For this, get the contributions to the shared potential for the corresponding ID and # location from each modeling class. def sharedPotentialConstraint(pyM, ID, loc): return sum(mdl.getSharedPotentialContribution(pyM, ID, loc) for mdl in self.componentModelingDict.values()) <= 1 pyM.ConstraintSharedPotentials = \ pyomo.Constraint(pyM.sharedPotentialDict.keys(), rule=sharedPotentialConstraint) def declareComponentLinkedQuantityConstraints(self, pyM): """ Declare linked component quantity constraint, e.g. if an engine (E-Motor) is built also a storage (Battery) and a vehicle body (e.g. BEV Car) needs to be built. Not the capacity of the components, but the number of the components is linked. :param pyM: a pyomo ConcreteModel instance which contains parameters, sets, variables, constraints and objective required for the optimization set up and solving. :type pyM: pyomo ConcreteModel """ utils.output('Declaring linked component quantity constraint...', self.verbose, 0) compDict = {} for mdl in self.componentModelingDict.values(): for compName, comp in mdl.componentsDict.items(): if comp.linkedQuantityID is not None: [compDict.setdefault((comp.linkedQuantityID, loc), []).append(compName) for loc in comp.locationalEligibility.index] pyM.linkedQuantityDict = compDict def linkedQuantityConstraint(pyM, ID, loc, compName1, compName2): abbrvName1 = self.componentModelingDict[self.componentNames[compName1]].abbrvName abbrvName2 = self.componentModelingDict[self.componentNames[compName2]].abbrvName capVar1 = getattr(pyM, 'cap_' + abbrvName1) capVar2 = getattr(pyM, 'cap_' + abbrvName2) capPPU1 = self.componentModelingDict[self.componentNames[compName1]].componentsDict[compName1].capacityPerPlantUnit capPPU2 = self.componentModelingDict[self.componentNames[compName2]].componentsDict[compName2].capacityPerPlantUnit return capVar1[loc, compName1] / capPPU1 == capVar2[loc, compName2] / capPPU2 for (i,j) in pyM.linkedQuantityDict.keys(): linkedQuantityList = [] linkedQuantityList.append((i, j)) setattr(pyM, 'ConstraintLinkedQuantity_' + str(i) + '_' + str(j),\ pyomo.Constraint(\ linkedQuantityList,\ pyM.linkedQuantityDict[i, j],\ pyM.linkedQuantityDict[i, j],\ rule=linkedQuantityConstraint)) def declareCommodityBalanceConstraints(self, pyM): """ Declare commodity balance constraints (one balance constraint for each commodity, location and time step) .. math:: \\underset{\\text{comp} \in \mathcal{C}^{comm}_{loc}}{\sum} \\text{C}^{comp,comm}_{loc,p,t} = 0 :param pyM: a pyomo ConcreteModel instance which contains parameters, sets, variables, constraints and objective required for the optimization set up and solving. :type pyM: pyomo ConcreteModel """ utils.output('Declaring commodity balances...', self.verbose, 0) # Declare and initialize a set that states for which location and commodity the commodity balance constraints # are non-trivial (i.e. not 0 == 0; trivial constraints raise errors in pyomo). def initLocationCommoditySet(pyM): return ((loc, commod) for loc in self.locations for commod in self.commodities if any([mdl.hasOpVariablesForLocationCommodity(self, loc, commod) for mdl in self.componentModelingDict.values()])) pyM.locationCommoditySet = pyomo.Set(dimen=2, initialize=initLocationCommoditySet) # Declare and initialize commodity balance constraints by checking for each location and commodity in the # locationCommoditySet and for each period and time step within the period if the commodity source and sink # terms add up to zero. For this, get the contribution to commodity balance from each modeling class. def commodityBalanceConstraint(pyM, loc, commod, p, t): return sum(mdl.getCommodityBalanceContribution(pyM, commod, loc, p, t) for mdl in self.componentModelingDict.values()) == 0 pyM.commodityBalanceConstraint = pyomo.Constraint(pyM.locationCommoditySet, pyM.timeSet, rule=commodityBalanceConstraint) def declareObjective(self, pyM): """ Declare the objective function by obtaining the contributions to the objective function from all modeling classes. Currently, the only objective function which can be selected is the sum of the total annual cost of all components. .. math:: z^* = \\min \\underset{comp \\in \\mathcal{C}}{\\sum} \\ \\underset{loc \\in \\mathcal{L}^{comp}}{\\sum} \\left( TAC_{loc}^{comp,cap} + TAC_{loc}^{comp,bin} + TAC_{loc}^{comp,op} \\right) Objective Function detailed: .. math:: :nowrap: \\begin{eqnarray*} z^* = \\min & & \\underset{comp \\in \\mathcal{C}}{\\sum} \\ \\underset{loc \\in \\mathcal{L}^{comp}}{\\sum} \\left[ \\text{F}^{comp,cap}_{loc} \\cdot \\left( \\frac{\\text{investPerCap}^{comp}_{loc}}{\\text{CCF}^{comp}_{loc}} \\right. + \\text{opexPerCap}^{comp}_{loc} \\right) \\cdot cap^{comp}_{loc} \\\\ & & + \\ \\text{F}^{comp,bin}_{loc} \\cdot \\left( \\frac{\\text{investIfBuilt}^{comp}_{loc}} {CCF^{comp}_{loc}} + \\text{opexIfBuilt}^{comp}_{loc} \\right) \\cdot bin^{comp}_{loc} \\\\ & & \\left. + \\left( \\underset{(p,t) \\in \\mathcal{P} \\times \\mathcal{T}}{\\sum} \\ \\underset{\\text{opType} \\in \\mathcal{O}^{comp}}{\\sum} \\text{factorPerOp}^{comp,opType}_{loc} \\cdot op^{comp,opType}_{loc,p,t} \\cdot \\frac{\\text{freq(p)}}{\\tau^{years}} \\right) \\right] \\end{eqnarray*} :param pyM: a pyomo ConcreteModel instance which contains parameters, sets, variables, constraints and objective required for the optimization set up and solving. :type pyM: pyomo ConcreteModel """ utils.output('Declaring objective function...', self.verbose, 0) def objective(pyM): TAC = sum(mdl.getObjectiveFunctionContribution(self, pyM) for mdl in self.componentModelingDict.values()) return TAC pyM.Obj = pyomo.Objective(rule=objective) def declareOptimizationProblem(self, timeSeriesAggregation=False, segmentation=False, relaxIsBuiltBinary=False): """ Declare the optimization problem belonging to the specified energy system for which a pyomo concrete model instance is built and filled with * basic time sets, * sets, variables and constraints contributed by the component modeling classes, * basic, component overreaching constraints, and * an objective function. **Default arguments:** :param timeSeriesAggregation: states if the optimization of the energy system model should be done with (a) the full time series (False) or (b) clustered time series data (True). |br| * the default value is False :type timeSeriesAggregation: boolean :param segmentation: states if the optimization of the energy system model based on clustered time series data should be done with (a) aggregated typical periods with the original time step length (False) or (b) aggregated typical periods with further segmented time steps (True). |br| * the default value is False :type segmentation: boolean :param relaxIsBuiltBinary: states if the optimization problem should be solved as a relaxed LP to get the lower bound of the problem. |br| * the default value is False :type declaresOptimizationProblem: boolean Last edited: March 26, 2020 |br| @author: FINE Developer Team (FZJ IEK-3) """ # Get starting time of the optimization to, later on, obtain the total run time of the optimize function call timeStart = time.time() # Check correctness of inputs utils.checkDeclareOptimizationProblemInput(timeSeriesAggregation, self.isTimeSeriesDataClustered) ################################################################################################################ # Initialize mathematical model (ConcreteModel) instance # ################################################################################################################ # Initialize a pyomo ConcreteModel which will be used to store the mathematical formulation of the model. # The ConcreteModel instance is stored in the EnergySystemModel instance, which makes it available for # post-processing or debugging. A pyomo Suffix with the name dual is declared to make dual values associated # to the model's constraints available after optimization. self.pyM = pyomo.ConcreteModel() pyM = self.pyM pyM.dual = pyomo.Suffix(direction=pyomo.Suffix.IMPORT) # Set time sets for the model instance self.declareTimeSets(pyM, timeSeriesAggregation, segmentation) ################################################################################################################ # Declare component specific sets, variables and constraints # ################################################################################################################ for key, mdl in self.componentModelingDict.items(): _t = time.time() utils.output('Declaring sets, variables and constraints for ' + key, self.verbose, 0) utils.output('\tdeclaring sets... ', self.verbose, 0), mdl.declareSets(self, pyM) utils.output('\tdeclaring variables... ', self.verbose, 0), mdl.declareVariables(self, pyM, relaxIsBuiltBinary) utils.output('\tdeclaring constraints... ', self.verbose, 0), mdl.declareComponentConstraints(self, pyM) utils.output('\t\t(%.4f' % (time.time() - _t) + ' sec)\n', self.verbose, 0) ################################################################################################################ # Declare cross-componential sets and constraints # ################################################################################################################ # Declare constraints for enforcing shared capacities _t = time.time() self.declareSharedPotentialConstraints(pyM) utils.output('\t\t(%.4f' % (time.time() - _t) + ' sec)\n', self.verbose, 0) # Declare constraints for linked quantities _t = time.time() self.declareComponentLinkedQuantityConstraints(pyM) utils.output('\t\t(%.4f' % (time.time() - _t) + ' sec)\n', self.verbose, 0) # Declare commodity balance constraints (one balance constraint for each commodity, location and time step) _t = time.time() self.declareCommodityBalanceConstraints(pyM) utils.output('\t\t(%.4f' % (time.time() - _t) + ' sec)\n', self.verbose, 0) # Declare constraint for balanceLimit _t = time.time() self.declareBalanceLimitConstraint(pyM, timeSeriesAggregation) utils.output('\t\t(%.4f' % (time.time() - _t) + ' sec)\n', self.verbose, 0) ################################################################################################################ # Declare objective function # ################################################################################################################ # Declare objective function by obtaining the contributions to the objective function from all modeling classes _t = time.time() self.declareObjective(pyM) utils.output('\t\t(%.4f' % (time.time() - _t) + ' sec)\n', self.verbose, 0) # Store the build time of the optimize function call in the EnergySystemModel instance self.solverSpecs['buildtime'] = time.time() - timeStart def optimize(self, declaresOptimizationProblem=True, relaxIsBuiltBinary=False, timeSeriesAggregation=False, logFileName='', threads=3, solver='None', timeLimit=None, optimizationSpecs='', warmstart=False): """ Optimize the specified energy system for which a pyomo ConcreteModel instance is built or called upon. A pyomo instance is optimized with the specified inputs, and the optimization results are further processed. **Default arguments:** :param declaresOptimizationProblem: states if the optimization problem should be declared (True) or not (False). (a) If true, the declareOptimizationProblem function is called and a pyomo ConcreteModel instance is built. (b) If false a previously declared pyomo ConcreteModel instance is used. |br| * the default value is True :type declaresOptimizationProblem: boolean :param relaxIsBuiltBinary: states if the optimization problem should be solved as a relaxed LP to get the lower bound of the problem. |br| * the default value is False :type declaresOptimizationProblem: boolean :param timeSeriesAggregation: states if the optimization of the energy system model should be done with (a) the full time series (False) or (b) clustered time series data (True). |br| * the default value is False :type timeSeriesAggregation: boolean :param segmentation: states if the optimization of the energy system model based on clustered time series data should be done with (a) aggregated typical periods with the original time step length (False) or (b) aggregated typical periods with further segmented time steps (True). |br| * the default value is False :type segmentation: boolean :param logFileName: logFileName is used for naming the log file of the optimization solver output if gurobi is used as the optimization solver. If the logFileName is given as an absolute path (e.g. logFileName = os.path.join(os.getcwd(), 'Results', 'logFileName.txt')) the log file will be stored in the specified directory. Otherwise, it will be stored by default in the directory where the executing python script is called. |br| * the default value is 'job' :type logFileName: string :param threads: number of computational threads used for solving the optimization (solver dependent input) if gurobi is used as the solver. A value of 0 results in using all available threads. If a value larger than the available number of threads are chosen, the value will reset to the maximum number of threads. |br| * the default value is 3 :type threads: positive integer :param solver: specifies which solver should solve the optimization problem (which of course has to be installed on the machine on which the model is run). |br| * the default value is 'gurobi' :type solver: string :param timeLimit: if not specified as None, indicates the maximum solve time of the optimization problem in seconds (solver dependent input). The use of this parameter is suggested when running models in runtime restricted environments (such as clusters with job submission systems). If the runtime limitation is triggered before an optimal solution is available, the best solution obtained up until then (if available) is processed. |br| * the default value is None :type timeLimit: strictly positive integer or None :param optimizationSpecs: specifies parameters for the optimization solver (see the respective solver documentation for more information). Example: 'LogToConsole=1 OptimalityTol=1e-6' |br| * the default value is an empty string ('') :type timeLimit: string :param warmstart: specifies if a warm start of the optimization should be considered (not always supported by the solvers). |br| * the default value is False :type warmstart: boolean Last edited: March 26, 2020 |br| @author: FINE Developer Team (FZJ IEK-3) """ if not timeSeriesAggregation: self.segmentation = False if declaresOptimizationProblem: self.declareOptimizationProblem(timeSeriesAggregation=timeSeriesAggregation, segmentation=self.segmentation, relaxIsBuiltBinary=relaxIsBuiltBinary) else: if self.pyM is None: raise TypeError('The optimization problem is not declared yet. Set the argument declaresOptimization' ' problem to True or call the declareOptimizationProblem function first.') # Get starting time of the optimization to, later on, obtain the total run time of the optimize function call timeStart = time.time() # Check correctness of inputs utils.checkOptimizeInput(timeSeriesAggregation, self.isTimeSeriesDataClustered, logFileName, threads, solver, timeLimit, optimizationSpecs, warmstart) # Store keyword arguments in the EnergySystemModel instance self.solverSpecs['logFileName'], self.solverSpecs['threads'] = logFileName, threads self.solverSpecs['solver'], self.solverSpecs['timeLimit'] = solver, timeLimit self.solverSpecs['optimizationSpecs'], self.solverSpecs['hasTSA'] = optimizationSpecs, timeSeriesAggregation # Check which solvers are available and choose default solver if no solver is specified explicitely # Order of possible solvers in solverList defines the priority of chosen default solver. solverList = ['gurobi', 'coincbc', 'glpk'] if solver != 'None': try: opt.SolverFactory(solver).available() except: solver = 'None' if solver == 'None': for nSolver in solverList: if solver == 'None': try: if opt.SolverFactory(nSolver).available(): solver = nSolver utils.output('Either solver not selected or specified solver not available.' + str(nSolver) + ' is set as solver.', self.verbose, 0) except: pass if solver == 'None': raise TypeError('At least one solver must be installed.' ' Have a look at the FINE documentation to see how to install possible solvers.' ' https://vsa-fine.readthedocs.io/en/latest/') ################################################################################################################ # Solve the specified optimization problem # ################################################################################################################ # Set which solver should solve the specified optimization problem optimizer = opt.SolverFactory(solver) # Set, if specified, the time limit if self.solverSpecs['timeLimit'] is not None and solver == 'gurobi': optimizer.options['timelimit'] = timeLimit # Set the specified solver options if 'LogToConsole=' not in optimizationSpecs and solver == "gurobi": if self.verbose == 2: optimizationSpecs += ' LogToConsole=0' # Solve optimization problem. The optimization solve time is stored and the solver information is printed. if solver=='gurobi': optimizer.set_options('Threads=' + str(threads) + ' logfile=' + logFileName + ' ' + optimizationSpecs) solver_info = optimizer.solve(self.pyM, warmstart=warmstart, tee=True) elif solver=="glpk": optimizer.set_options(optimizationSpecs) solver_info = optimizer.solve(self.pyM, tee=True) else: solver_info = optimizer.solve(self.pyM, tee=True) self.solverSpecs['solvetime'] = time.time() - timeStart utils.output(solver_info.solver(), self.verbose, 0), utils.output(solver_info.problem(), self.verbose, 0) utils.output('Solve time: ' + str(self.solverSpecs['solvetime']) + ' sec.', self.verbose, 0) ################################################################################################################ # Post-process optimization output # ################################################################################################################ _t = time.time() # Post-process the optimization output by differentiating between different solver statuses and termination # conditions. First, check if the status and termination_condition of the optimization are acceptable. # If not, no output is generated. # TODO check if this is still compatible with the latest pyomo version status, termCondition = solver_info.solver.status, solver_info.solver.termination_condition self.solverSpecs['status'] = str(status) self.solverSpecs['terminationCondition'] = str(termCondition) if status == opt.SolverStatus.error or status == opt.SolverStatus.aborted or status == opt.SolverStatus.unknown: utils.output('Solver status: ' + str(status) + ', termination condition: ' + str(termCondition) + '. No output is generated.', self.verbose, 0) elif solver_info.solver.termination_condition == opt.TerminationCondition.infeasibleOrUnbounded or \ solver_info.solver.termination_condition == opt.TerminationCondition.infeasible or \ solver_info.solver.termination_condition == opt.TerminationCondition.unbounded: utils.output('Optimization problem is ' + str(solver_info.solver.termination_condition) + '. No output is generated.', self.verbose, 0) else: # If the solver status is not okay (hence either has a warning, an error, was aborted or has an unknown # status), show a warning message. if not solver_info.solver.termination_condition == opt.TerminationCondition.optimal and self.verbose < 2: warnings.warn('Output is generated for a non-optimal solution.') utils.output("\nProcessing optimization output...", self.verbose, 0) # Declare component specific sets, variables and constraints w = str(len(max(self.componentModelingDict.keys()))+6) for key, mdl in self.componentModelingDict.items(): __t = time.time() mdl.setOptimalValues(self, self.pyM) outputString = ('for {:' + w + '}').format(key + ' ...') + "(%.4f" % (time.time() - __t) + "sec)" utils.output(outputString, self.verbose, 0) # Store the objective value in the EnergySystemModel instance. self.objectiveValue = self.pyM.Obj() utils.output('\t\t(%.4f' % (time.time() - _t) + ' sec)\n', self.verbose, 0) # Store the runtime of the optimize function call in the EnergySystemModel instance self.solverSpecs['runtime'] = self.solverSpecs['buildtime'] + time.time() - timeStart
nilq/baby-python
python
""" qstode.searcher ~~~~~~~~~~~~~~~ Whoosh search engine support. :copyright: (c) 2013 by Daniel Kertesz :license: BSD, see LICENSE for more details. """ import os import json import redis from whoosh.fields import ID, TEXT, KEYWORD, Schema from whoosh.analysis import RegexTokenizer, LowercaseFilter, CharsetFilter from whoosh.support.charset import accent_map from whoosh.index import create_in, open_dir, exists_in from whoosh.writing import AsyncWriter from whoosh.qparser import MultifieldParser from whoosh.sorting import Facets # Constants used in the Redis message queue OP_INDEX, OP_UPDATE, OP_DELETE = list(range(3)) # Queue names for Redis QUEUE_INDEX = "index_in" QUEUE_WORK = "index_work" def generate_schema(): """Generates the search engine schema""" text_analyzer = RegexTokenizer() | LowercaseFilter() | CharsetFilter(accent_map) schema = Schema( id=ID(stored=True, unique=True), title=TEXT(stored=False, analyzer=text_analyzer), tags=KEYWORD(stored=False, lowercase=True, commas=True), notes=TEXT(stored=False, analyzer=text_analyzer), ) return schema def create_document(bookmark): """Creates a Document (a dict) for the search engine""" return { "id": str(bookmark.id), "title": bookmark.title or "", "notes": bookmark.notes or "", "tags": ", ".join([tag.name for tag in bookmark.tags]), } def redis_connect(config): """Connects to a Redis database as specified by the dictionary `config`""" return redis.Redis( host=config.get("REDIS_HOST", "localhost"), port=config.get("REDIS_PORT", 6379), db=config.get("REDIS_DB", 0), password=config.get("REDIS_PASSWORD"), ) class WhooshSearcher(object): """Interface to a Whoosh based Search Engine""" # default search fields for user queries search_fields = ("notes", "title", "tags") def __init__(self, app=None, index_dir=None): self.app = app self.index_dir = index_dir self._ix = None self._redis = None @property def ix(self): """Lazy opening of the Whoosh index""" if self._ix is None: self._ix = self._open_index() return self._ix @property def redis(self): """Lazy opening of the Redis connection""" if self._redis is None: self._redis = redis_connect(self.app.config) return self._redis def init_app(self, app): """Initialize module and checks if the index exists""" self.app = app if "WHOOSH_INDEX_PATH" not in self.app.config: raise Exception("You must set the WHOOSH_INDEX_PATH option " "in the configuration") self.index_dir = self.app.config["WHOOSH_INDEX_PATH"] if not exists_in(self.index_dir): self.setup_index() def setup_index(self): """Create the index directory""" if not os.path.exists(self.index_dir): os.mkdir(self.index_dir) schema = generate_schema() self._ix = create_in(self.index_dir, schema) def _open_index(self): ix = open_dir(self.index_dir) return ix def get_async_writer(self): """Return an AsyncWriter; NOTE that we NEED thread support (i.e when you're running in uwsgi""" return AsyncWriter(self.ix) def push_add_bookmark(self, bookmark): """Pushes a 'add bookmark' operation to the Redis queue""" r = self.redis payload = json.dumps((OP_INDEX, bookmark.id)) r.rpush(QUEUE_INDEX, payload) def push_update_bookmark(self, bookmark): """Pushes a 'update bookmark' operation to the Redis queue""" self.push_add_bookmark(bookmark) def push_delete_bookmark(self, bookmark_id): """Pushes a 'delete bookmark' operation to the Redis queue""" r = self.redis payload = json.dumps((OP_DELETE, bookmark_id)) r.rpush(QUEUE_INDEX, payload) def add_bookmark(self, bookmark, writer=None): """Index a bookmark, updating it if it's already indexed; if you pass a `writer` object you are responsible for calling `commit()` at the end of the operations. If no `writer` is passed an AsyncWriter will be used. """ document = create_document(bookmark) if writer is None: writer = self.get_async_writer() writer.update_document(**document) writer.commit() else: writer.update_document(**document) def update_bookmark(self, bookmark, writer=None): """Reindex a Bookmark""" self.add_bookmark(bookmark, writer) def delete_bookmark(self, bookmark_id, writer=None): """Delete a Bookmark from the index""" _id = str(bookmark_id) if writer is None: writer = self.get_async_writer() writer.delete_by_term("id", _id) writer.commit() else: writer.delete_by_term("id", _id) def search(self, query, page=1, page_len=10, fields=None): """Returns the results of a search engine query ordered by Whoosh default ordering (?). :returns: a list of bookmark id (int) """ if fields is None: fields = tuple(self.search_fields) results = None with self.ix.searcher() as searcher: parser = MultifieldParser(fields, self.ix.schema) whoosh_query = parser.parse(query) facets = Facets() facets.add_field("tags", allow_overlap=True) # this can raise a ValueError search_results = searcher.search_page( whoosh_query, page, pagelen=page_len, groupedby=facets ) results = [int(result["id"]) for result in search_results] return results or []
nilq/baby-python
python
################################################################################ ## ## This library is free software; you can redistribute it and/or ## modify it under the terms of the GNU Lesser General Public ## License as published by the Free Software Foundation; either ## version 2.1 of the License, or (at your option) any later version. ## ## This library 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 ## Lesser General Public License for more details. ## ## You should have received a copy of the GNU Lesser General Public ## License along with this library; if not, write to the Free Software ## Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA ## ## (C) Copyrights Dr. Michel F. Sanner and TSRI 2016 ## ################################################################################ ######################################################################## # # Date: 2015 Authors: Michel Sanner # # sanner@scripps.edu # # The Scripps Research Institute (TSRI) # Molecular Graphics Lab # La Jolla, CA 92037, USA # # Copyright: Michel Sanner and TSRI 2015 # ######################################################################### # # $Header: /mnt/raid/services/cvs/python/packages/share1.5/mglutil/util/io.py,v 1.1.4.1 2017/07/26 22:31:38 annao Exp $ # # $Id: io.py,v 1.1.4.1 2017/07/26 22:31:38 annao Exp $ # class Stream: def __init__(self): self.lines = [] def write(self, line): self.lines.append(line) # helper class to make stdout set of lines look like a file that ProDy can parse class BufferAsFile: def __init__(self, lines): self.lines = lines def readlines(self): return self.lines
nilq/baby-python
python
import math from PyQt5 import QtCore from PyQt5.QtCore import QUrl, QObject, pyqtSignal from PyQt5.QtGui import QMouseEvent, QColor from PyQt5.QtMultimedia import QMediaPlayer, QMediaContent from PyQt5.QtMultimediaWidgets import QVideoWidget from PyQt5.QtWidgets import QDialog, QVBoxLayout, QWidget, QSlider, QGraphicsDropShadowEffect, QFrame from cvstudio.util import VideoUtilities class VideoPlayerWidgetSignals(QObject): video_position_changed_signal = pyqtSignal(int, int) video_duration_changed_signal = pyqtSignal(int) class VideoPlayer(QWidget): def __init__(self, parent=None): super(VideoPlayer, self).__init__(parent) self._source = None self._total_duration = 0 self.widget_layout = QVBoxLayout() self.media_player = QMediaPlayer(None, QMediaPlayer.VideoSurface) self.video_player = QVideoWidget() self.widget_layout.addWidget(self.video_player) self.media_player.setVideoOutput(self.video_player) # self.media_player.stateChanged.connect(self.mediaStateChanged) self.media_player.positionChanged.connect(self.on_positionChanged) self.signals = VideoPlayerWidgetSignals() self.media_player.durationChanged.connect(self.on_durationChanged) self.setLayout(self.widget_layout) print(self.media_player.duration()) @property def total_duration(self): return self._total_duration @total_duration.setter def total_duration(self, val): self._total_duration = val @property def source(self): return self._source @source.setter def source(self, value): self._source = value self._total_duration = math.floor(VideoUtilities.duration(self.source)) def play(self): if self._source: self.media_player.setMedia( QMediaContent(QUrl.fromLocalFile(self._source))) self.media_player.play() def resume(self): if self.media_player.state() == QMediaPlayer.PlayingState: self.media_player.pause() else: self.media_player.play() def stop(self): if self.media_player.state() == QMediaPlayer.PlayingState: self.media_player.stop() def go_to(self, second: int): if self.media_player: self.media_player.setPosition(second * 1000) @QtCore.pyqtSlot('qint64') def on_positionChanged(self, position): self.signals.video_position_changed_signal.emit(math.floor(position / 1000), self.total_duration) if self.media_player.state() == QMediaPlayer.StoppedState: if 0 <= position <= self.total_duration * 1000: self.media_player.play() @QtCore.pyqtSlot('qint64') def on_durationChanged(self, duration): self.signals.video_duration_changed_signal.emit(math.floor(duration / 1000)) class VideoViewerContainer(QWidget): def __init__(self, window: QDialog, parent=None): super(VideoViewerContainer, self).__init__(parent) self.setWindowFlags(QtCore.Qt.FramelessWindowHint) self.setAttribute(QtCore.Qt.WA_TranslucentBackground) self.resize(1024, 580) layout = QVBoxLayout(self) layout.addWidget(window) layout.setContentsMargins(0, 0, 6, 6) self.shadow = QGraphicsDropShadowEffect(self) self.shadow.setBlurRadius(50) self.shadow.setColor(QColor(138, 145, 140)) self.shadow.setOffset(8) window.setGraphicsEffect(self.shadow) class VideoDialog(QDialog): def __init__(self, video_path, parent=None): super(VideoDialog, self).__init__(parent) # self.setWindowFlags(QtCore.Qt.FramelessWindowHint) self.resize(400, 400) position = self.cursor().pos() position.setX(position.x()) position.setY(position.y()) self.move(position) # self.setWindowOpacity(0.9) self.setLayout(QVBoxLayout()) self.layout().setContentsMargins(0, 0, 0, 0) self.widget = QFrame() self.widget.setStyleSheet(''' QFrame{ border-style: outset; border-width: 1px; /*border-radius: 10px;*/ border-color: #B94129; } ''') # self.widget.setFrameStyle(QFrame.Box) self.widget.setLayout(QVBoxLayout()) self.widget.layout().setContentsMargins(10, 10, 10, 10) self.video_player = VideoPlayer() self.video_player.source = video_path self.video_player.play() self.video_player.signals.video_position_changed_signal.connect(self.video_position_changed) duration = self.video_player.total_duration self.video_duration_slider = QSlider(orientation=QtCore.Qt.Horizontal) self.video_duration_slider.setRange(0, duration) self.video_duration_slider.setTickInterval(5) self.video_duration_slider.sliderMoved.connect(self.slider_changed_handler) # self.video_duration_slider.setTickPosition(QSlider.TicksBelow) self.setMouseTracking(True) self.setWindowFlags(QtCore.Qt.Popup | QtCore.Qt.WindowStaysOnTopHint | QtCore.Qt.FramelessWindowHint | QtCore.Qt.X11BypassWindowManagerHint) # self.setAttribute(QtCore.Qt.WA_TranslucentBackground) self.widget.layout().addWidget(self.video_player) self.widget.layout().addWidget(self.video_duration_slider) self.layout().addWidget(self.widget) def setMouseTracking(self, flag): def set_mouse_tracking(parent): for child in parent.findChildren(QtCore.QObject): try: child.setMouseTracking(flag) except: pass set_mouse_tracking(child) QWidget.setMouseTracking(self, flag) set_mouse_tracking(self) def slider_changed_handler(self, change): self.video_player.go_to(change) def video_position_changed(self, current, total): self.video_duration_slider.setValue(current) def mouseMoveEvent(self, event: QMouseEvent) -> None: # print('mouseMoveEvent: x=%d, y=%d' % (event.x(), event.y())) if not self.rect().contains(event.pos()): self.close()
nilq/baby-python
python
# -*- coding: utf-8 -*- import abc from inspect import isfunction, signature from types import FunctionType from watson import di from watson.common.contextmanagers import suppress from watson.common import imports from watson.di.types import FUNCTION_TYPE class Base(di.ContainerAware, metaclass=abc.ABCMeta): """The base processor that all other processors should extend. When a processor is called from the container the following parameters are sent through with the event. - definition: The dict definition of the dependency - dependency: The name of the dependency Depending on the event, a different target will also be sent with the event. - watson.di.container.PRE_EVENT: The dict definition of the dependency - watson.di.container.POST_EVENT: The initialized dependency """ @abc.abstractmethod def __call__(self, event): raise NotImplementedError( 'The processor <{}> must implement __call__'.format(imports.get_qualified_name(self))) # pragma: no cover def get_args_kwargs(self, obj): args, kwargs = [], {} if isinstance(obj, dict): for key, val in obj.items(): kwargs[key] = get_param_from_container(val, self.container) elif isinstance(obj, list): for arg in obj: args.append(get_param_from_container(arg, self.container)) return args, kwargs class ConstructorInjection(Base): """Responsible for initializing the dependency. Responsible for initializing the dependency and injecting any required values into the constructor. Args: event (watson.events.types.Event): The event dispatched from the container. Returns: mixed: The dependency """ def instantiate(self, definition): item = definition['item'] if hasattr(item, '__ioc_definition__'): definition.update(item.__ioc_definition__) args, kwargs = [], {} is_lambda = definition.get('call_type', None) == FUNCTION_TYPE sig = signature(item) if 'container' in sig.parameters: kwargs['container'] = self.container if 'init' in definition: init = definition['init'] updated_args, updated_kwargs = self.get_args_kwargs(init) args.extend(updated_args) kwargs.update(updated_kwargs) if isfunction(init): sig = signature(init) if 'container' in sig.parameters: kwargs['container'] = self.container init = init(*args, **kwargs) definition['init'] = init if not is_lambda: args, kwargs = self.get_args_kwargs(init) item = item(*args, **kwargs) if is_lambda and isinstance(item, str): # Special case for items that might be retrieved via lambda expressions with suppress(Exception): definition['item'] = self.container.load_item_from_string(item) item, args, kwargs = self.instantiate(definition) return item, args, kwargs def __call__(self, event): definition = event.params['definition'] item, args, kwargs = self.instantiate(definition) return item class SetterInjection(Base): """Responsible for injecting required values into setter methods. Args: event (watson.events.types.Event): The event dispatched from the container. Returns: mixed: The dependency """ def __call__(self, event): item = event.target definition = event.params['definition'] if 'setter' in definition: for setter, args in definition['setter'].items(): method = getattr(item, setter) if isinstance(args, dict): kwargs = {arg: get_param_from_container( value, self.container) for arg, value in args.items()} method(**kwargs) elif isinstance(args, list): args = [get_param_from_container(arg, self.container) for arg in args] method(*args) else: method(get_param_from_container(args, self.container)) return item class AttributeInjection(Base): """Responsible for injecting required values into attributes. Args: event (watson.events.types.Event): The event dispatched from the container. Returns: mixed: The dependency """ def __call__(self, event): item = event.target if 'property' in event.params['definition']: for prop, value in event.params['definition']['property'].items(): setattr( item, prop, get_param_from_container( value, self.container)) return item class ContainerAware(Base): """Injects the container into a dependency. Responsible for injecting the container in any class that extends watson.di.ContainerAware. The container is then accessible via object.container Args: event (watson.events.types.Event): The event dispatched from the container. Returns: mixed: The dependency """ def __call__(self, event): item = event.target if isinstance(item, di.ContainerAware): item.container = self.container return item def get_param_from_container(param, container): """Internal function used by the container. Retrieve a parameter from the container, and determine whether or not that parameter is an existing dependency. Returns: mixed: The dependency (if param name is the same as a dependency), the param, or the value of the param. """ if param in container.params: param = container.params[param] if param in container: param = container.get(param) elif param in container: param = container.get(param) else: if isinstance(param, FunctionType): param = param(container) return param
nilq/baby-python
python
from __future__ import print_function import json as json_lib from threading import Lock import requests.adapters import py42.settings as settings from py42._internal.compat import str from py42._internal.compat import urljoin from py42._internal.compat import urlparse from py42.exceptions import raise_py42_error from py42.response import Py42Response from py42.settings import debug from py42.util import format_dict def _print_request(method, url, params=None, data=None): debug.logger.info(u"{}{}".format(str(method).ljust(8), url)) if params: debug.logger.debug(format_dict(params, u" params")) if data: debug.logger.debug(format_dict(data, u" data")) class Py42Session(object): def __init__(self, session, host_address, auth_handler=None): self._initialized = False self._needs_auth_renewal_check = False self._auth_lock = Lock() self._session = session adapter = requests.adapters.HTTPAdapter(pool_connections=20, pool_maxsize=20) if not host_address.startswith(u"http://") and not host_address.startswith( u"https://" ): host_address = u"https://{}".format(host_address) self._host_address = host_address self._auth_handler = auth_handler self._session.proxies = settings.proxies self._session.verify = settings.verify_ssl_certs self._session.mount(u"https://", adapter) self._session.mount(u"http://", adapter) self._host_address = host_address parsed_host = urlparse(self._host_address) host = parsed_host.netloc self._session.headers = { u"Accept": u"application/json", u"Content-Type": u"application/json", u"Host": host, u"User-Agent": settings.get_user_agent_string(), u"Accept-Encoding": u"gzip, deflate", u"Connection": u"keep-alive", } @property def host_address(self): return self._host_address @property def headers(self): return self._session.headers @property def cookies(self): return self._session.cookies @property def proxies(self): return self._session.proxies def get(self, url, **kwargs): return self.request(u"GET", url, **kwargs) def options(self, url, **kwargs): return self.request(u"OPTIONS", url, **kwargs) def head(self, url, **kwargs): return self.request(u"HEAD", url, **kwargs) def post(self, url, data=None, json=None, **kwargs): return self.request(u"POST", url, data=data, json=json, **kwargs) def put(self, url, data=None, **kwargs): return self.request(u"PUT", url, data=data, **kwargs) def patch(self, url, data=None, **kwargs): return self.request(u"PATCH", url, data=data, **kwargs) def delete(self, url, **kwargs): return self.request(u"DELETE", url, **kwargs) def request(self, method, url, **kwargs): try: url = urljoin(self._host_address, url) json = kwargs.get(u"json") if json is not None: kwargs[u"data"] = json_lib.dumps(_filter_out_none(json)) if u"json" in kwargs: del kwargs[u"json"] self._renew_authentication(use_cache=True) tries = 0 max_tries = 2 while tries < max_tries: response, unauthorized = self._try_make_request(method, url, **kwargs) tries += 1 if unauthorized and tries < max_tries: self._renew_authentication() continue if response.status_code >= 400: response.raise_for_status() if not kwargs.get(u"stream"): response.encoding = ( u"utf-8" # setting this manually speeds up read times ) return Py42Response(response) except requests.HTTPError as err: raise_py42_error(err) def _try_make_request( self, method, url, params=None, data=None, headers=None, cookies=None, files=None, auth=None, timeout=60, allow_redirects=True, proxies=None, hooks=None, stream=None, verify=None, cert=None, ): _print_request(method, url, params=params, data=data) response = self._session.request( method, url, params=params, data=data, headers=headers, cookies=cookies, files=files, auth=auth, timeout=timeout, allow_redirects=allow_redirects, proxies=proxies, hooks=hooks, stream=stream, verify=verify, cert=cert, ) unauthorized = ( self._auth_handler and self._auth_handler.response_indicates_unauthorized(response) ) return response, unauthorized def _renew_authentication(self, use_cache=False): if self._auth_handler: # if multiple threads try to authenticate at once, only the first one actually does. # the rest will just wait for that authentication to complete. self._needs_auth_renewal_check = True with self._auth_lock: # only get new credentials if this is the first time or we want fresh ones should_renew = ( not self._initialized or not use_cache ) and self._needs_auth_renewal_check if should_renew: self._auth_handler.renew_authentication(self, use_cache=use_cache) self._needs_auth_renewal_check = False # if there's no auth handler or we handled auth without errors, initialization is done. self._initialized = True def _filter_out_none(_dict): return {key: _dict[key] for key in _dict if _dict[key] is not None}
nilq/baby-python
python
# -*- coding: utf-8 -*- # ----------------------------------------------------------------------------- # Copyright 2015-2018 by ExopyHqcLegacy Authors, see AUTHORS for more details. # # Distributed under the terms of the BSD license. # # The full license is in the file LICENCE, distributed with this software. # ----------------------------------------------------------------------------- """Tasks to set the parameters of arbitrary waveform generators. """ import logging import numbers import numpy as np from atom.api import (Enum, Str, Value) from exopy.tasks.api import (InstrumentTask, validators) #signal analyzer driver import exopy_qcircuits.instruments.drivers.visa.rohde_and_schwarz_psa as sa SA_SWEEPING = 0x0 SA_REAL_TIME = 0x1 class TuneIQMixerTask(InstrumentTask): """ Task to tune an IQ mixer in SSB Implicit use of a SignalHound spectrum analyzer Tunes channels I and Q DC offset, relative delay and voltage to suppress LO leakage and unwanted sideband TODO: handle task with two instruments: AWG AND Spectrum analyzer TODO: implement realtime sweep for better SNR """ # Get user inputs channelI = Enum('Ch1', 'Ch2', 'Ch3', 'Ch4').tag(pref=True) channelQ = Enum('Ch1', 'Ch2', 'Ch3', 'Ch4').tag(pref=True) # LO frequency freq = Str('0.0').tag(pref=True, feval=validators.SkipLoop(types=numbers.Real)) # modulation frequency det = Str('0.0').tag(pref=True, feval=validators.SkipLoop(types=numbers.Real)) # LO power frequency power = Str('0.0').tag(pref=True, feval=validators.SkipLoop(types=numbers.Real)) # Desired sideband, e.g. if Lower, suppress freq and freq+det SB = Enum('Lower', 'Upper').tag(pref=True) my_sa = Value() # signal analyzer chI = Value() chQ = Value() freq_Hz = Value() det_Hz = Value() SB_sgn = Value() LO_pow = Value() def check(self, *args, **kwargs): ''' Default checks and check different AWG channels ''' test, traceback = super(TuneIQMixerTask, self).check(*args, **kwargs) if not test: return test, traceback if self.channelI == self.channelQ: test = False msg = 'I and Q channels need to be different !' traceback[self.get_error_path()] = msg return test, traceback def perform(self): """Default interface behavior. """ # open signal analyzer Rhode&Schwarz visa_address = 'TCPIP0::192.168.0.52::inst0::INSTR' connection_infos = {'resource_name': visa_address} self.my_sa = sa.RohdeAndSchwarzPSA(connection_infos)#, mode=SA_SWEEPING) # AWG channels awg = self.driver awg.run_mode = 'CONT' awg.output_mode = 'FIX' self.chI = awg.get_channel(int(self.channelI[-1])) self.chQ = awg.get_channel(int(self.channelQ[-1])) # convert user inputs into adequate units self.LO_pow = self.format_and_eval_string(self.power) self.freq_Hz = self.format_and_eval_string(self.freq)*1e9 self.det_Hz = self.format_and_eval_string(self.det)*1e6 #modulation freq self.SB_sgn = 1 if self.SB == 'Lower' else -1 # setting the modulation frequency for each channel self.chI.set_frequency(self.det_Hz) self.chQ.set_frequency(self.det_Hz) # Initialize AWG params # set parameters to minima or centers everywhere # initialisation self.chI_vpp(0.15) self.chQ_vpp(0.15) self.chI_offset(0) self.chQ_offset(0) self.chQ_delay(0) # perform optimization twice self.tune_ssb('lo') self.tune_ssb('sb') pos_lo, cost = self.tune_ssb('lo') pos_sb, cost = self.tune_ssb('sb') # get power for optimal parameters at sig, leakage and sideband # get_single_freq(self,freq,reflevel,rbw,vbw,avrg_num) sig = self.my_sa.get_single_freq(self.freq_Hz-self.SB_sgn*self.det_Hz, self.LO_pow,int(1e3),int(1e3),10) lo = self.my_sa.get_single_freq(self.freq_Hz,self.LO_pow,1e3,1e3,10) sb = self.my_sa.get_single_freq(self.freq_Hz+self.SB_sgn*self.det_Hz, self.LO_pow,int(1e3),int(1e3),10) # close signal analyzer self.my_sa._close() # log values log = logging.getLogger(__name__) msg1 = 'Tuned IQ mixer at LO = %s GHz, IF = %s MHz, \ Signal: %s dBm, LO: %s dBm, SB: %s dBm' % \ (1e-9*self.freq_Hz, 1e-6*self.det_Hz, sig, lo, sb) log.info(msg1) msg2 = 'chI offset: %s V, chQ offset: %s V, chQvpp: %s V, \ chQphase: %s °' % \ (pos_lo[0], pos_lo[1], pos_sb[0], pos_sb[1]) log.info(msg2) # optimization procedure def tune_ssb(self, mode): # suppress lo leakage params if mode == 'lo': param1 = self.chI_offset param2 = self.chQ_offset f = self.freq_Hz minvals = np.array([-1,-1]) maxvals = np.array([1,1]) precision = np.array([0.001,0.001]) pos0 = np.array([self.get_chI_offset(), self.get_chQ_offset()]) # suppress other sideband params elif mode == 'sb': param1 = self.chQ_vpp param2 = self.chQ_delay f = self.freq_Hz + self.SB_sgn*self.det_Hz minvals = np.array([0.05,0]) maxvals = np.array([0.15,360]) precision = np.array([0.01,0.1]) pos0 = np.array([self.get_chQ_vpp(), self.get_chQ_delay()]) else: msg = '''param has wrong value, should be lo or sb, received %s''' % mode raise ValueError(msg) # 4 directions in parameter search space sens = [np.array([1, 0]), np.array([0, 1]), np.array([-1, 0]), np.array([0, -1])] # initial cost (cost = power of sa at f) cost0 = self.cost(param1, param2, pos0[0], pos0[1], f) ### Qcircuits STARTS HERE ### dec = 0.1 s = 0 c = 0 counter = 0 poslist = [pos0] precision_reached = False # stop search when step_size < AWG resolution while dec >= 0.0001: step_sizes = dec*(maxvals-minvals) #check that we aren't lower than instrument precision if ((step_sizes[0] - precision[0]) < 0) and \ ((step_sizes[1] - precision[1]) < 0): precision_reached = True step_sizes = precision elif (step_sizes[0] - precision[0]) < 0: step_sizes[0] = precision[0] elif (step_sizes[1] - precision[1]) < 0: step_sizes[1] = precision[1] # break when max eval count has reach or # all 4 directions have been explored while c < 4 and counter < 1000: # probe cost at new pos: pos1 pos1 = pos0 + step_sizes*sens[s] # check that we aren't out of bounds if (not (minvals[0] <= pos1[0] <= maxvals[0])) and \ (not (minvals[1] <= pos1[1] <= maxvals[1])): boundaries = np.array([minvals, np.array([minvals[0],maxvals[1]]), np.array([minvals[1],maxvals[0]]), maxvals]) # find bounardy closest to current value pos1 = boundaries[np.argmin(list(map(abs, boundaries-pos1)))] elif not (minvals[0] <= pos1[0] <= maxvals[0]): boundaries = np.array([minvals[0],maxvals[0]]) # find bounardy closest to current value pos1[0] = boundaries[np.argmin(list(map(abs, boundaries-pos1[0])))] elif not (minvals[1] <= pos1[1] <= maxvals[1]): boundaries = np.array([minvals[1],maxvals[1]]) # find bounardy closest to current value pos1[1] = boundaries[np.argmin(list(map(abs, boundaries-pos1[1])))] # evaluate cost of new position cost1 = self.cost(param1, param2, pos1[0], pos1[1], f) counter += 1 # if lower cost, update pos if cost1 < cost0: cost0 = cost1 pos0 = pos1 c = 0 poslist.append(pos0) else: c += 1 s = np.mod(s+1, 4) c = 0 # decrease dec if all explored directions give higher cost dec /= 10 if precision_reached: break return pos0, cost0 # optimization cost function: get power in dBm at f from signal_analyzer def cost(self, param1, param2, val1, val2, f): param1(val1) param2(val2) return self.my_sa.get_single_freq(f,self.LO_pow,1e3,1e3,10) # define AWG getter and setter functions to pass into cost function def chI_offset(self, value): self.chI.set_DC_offset(value) def chQ_offset(self, value): self.chQ.set_DC_offset(value) def get_chI_offset(self): return self.chI.DC_offset() def get_chQ_offset(self): return self.chQ.DC_offset() def chI_vpp(self, value): self.chI.set_Vpp(value) def chQ_vpp(self, value): self.chQ.set_Vpp(value) def get_chI_vpp(self): return self.chI.Vpp() def get_chQ_vpp(self): return self.chQ.Vpp() def chQ_delay(self, value): self.chQ.set_phase(value) def get_chQ_delay(self): return self.chQ.phase()
nilq/baby-python
python
import sys import SocketServer import ssl import struct import socket import comms def parse_table(): ret = {} ret["major"] = "bloodnok" ret["harry"] = "seagoon" return ret class listener(SocketServer.BaseRequestHandler): CMD_QUERY_ALL = 0 CMD_TRIGGER = 1 CMD_EXIT = 2 CMD_DISPLAY_SHOW = 0 CMD_DISPLAY_EXIT = 1 STATUS_OK = 0 STATUS_FAIL = 1 def handle(self): global table global display size = comms.get_u32(self.request) msg = comms.read_all(self.request, size) code = struct.unpack('>I', msg[:4])[0] if code == listener.CMD_QUERY_ALL: self.request.send(struct.pack('>I', listener.STATUS_OK)) msg = b'' for k in table.keys(): msg += struct.pack('>I', len(k)) msg += k self.request.send(struct.pack('>I', len(msg))) self.request.send(msg) return if code == listener.CMD_TRIGGER: key = msg[4:] if key not in table: self.request.send(struct.pack('>I', listener.STATUS_FAIL)) return pw = table[key] print "Displaying password '%s'" %pw display.send(struct.pack('>II', listener.CMD_DISPLAY_SHOW, len(pw))) display.send(pw) status = comms.read_all(self.request, 4) self.request.send(status) return if code == listener.EXIT: display.send(struct.pack('>I', listener.CMD_DISPLAY_EXIT)) sys.exit(0) print "Received unknown command %d" % code cert_file='server.crt' key_file='server.key' ca_file='root.crt' client_sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) display = ssl.wrap_socket(client_sock, key_file, cert_file, False, ssl.CERT_REQUIRED, ssl.PROTOCOL_TLSv1_2, ca_file) display.connect(('127.0.0.1', 6655)) print "TLS connection established" table = parse_table() SocketServer.allow_reuse_address = True server = SocketServer.TCPServer(('127.0.0.1', 6512), listener) server.serve_forever()
nilq/baby-python
python
from __future__ import absolute_import import blosc try: import cPickle as pickle_ except ImportError: import pickle as pickle_ import os import collections from . import report from . import object as object_ from . import path from os.path import join from os.path import isdir class PickleByName(object): """Makes un-pickle-able objects pick-able by setting its un-pickle-able attributes as signature only attributes.""" def __init__(self): self._signature_only_attrs = set() def set_signature_only_attr(self, attr_name): self._signature_only_attrs.add(attr_name) def __getstate__(self): d = self.__dict__.copy() for attr_name in self._signature_only_attrs: o = getattr(self, attr_name) d[attr_name + '_fullname'] = object_.fullname(o) d[attr_name + '_init_dict'] = o.init_dict() del d[attr_name] return d def __setstate__(self, d): import importlib for attr_name in d['_signature_only_attrs']: fn = d[attr_name + '_fullname'] k = fn.rfind(".") module_name, class_name = fn[:k], fn[k+1:] init_dict = d[attr_name + '_init_dict'] mod = importlib.import_module(module_name) class_ = getattr(mod, class_name) o = class_(**init_dict) d[attr_name] = o del d[attr_name + '_fullname'] del d[attr_name + '_init_dict'] self.__dict__.update(d) class SlotPickleMixin(object): """Top-class that allows mixing of classes with and without slots. Takes care that instances can still be pickled with the lowest protocol. Moreover, provides a generic `__dir__` method that lists all slots. """ # We want to allow weak references to the objects __slots__ = ['__weakref__'] def _get_all_slots(self): """Returns all slots as set""" all_slots = (getattr(cls, '__slots__', []) for cls in self.__class__.__mro__) return set(slot for slots in all_slots for slot in slots) def __getstate__(self): if hasattr(self, '__dict__'): # We don't require that all sub-classes also define slots, # so they may provide a dictionary statedict = self.__dict__.copy() else: statedict = {} # Get all slots of potential parent classes for slot in self._get_all_slots(): try: value = getattr(self, slot) statedict[slot] = value except AttributeError: pass # Pop slots that cannot or should not be pickled statedict.pop('__dict__', None) statedict.pop('__weakref__', None) return statedict def __setstate__(self, state): for key, value in state.items(): setattr(self, key, value) def __dir__(self): result = dir(self.__class__) result.extend(self._get_all_slots()) if hasattr(self, '__dict__'): result.extend(self.__dict__.keys()) return result def pickle(obj, filepath): arr = pickle_.dumps(obj, -1) with open(filepath, 'wb') as f: s = 0 while s < len(arr): e = min(s + blosc.MAX_BUFFERSIZE, len(arr)) carr = blosc.compress(arr[s:e], typesize=8) f.write(carr) s = e def unpickle(filepath): arr = [] with open(filepath, 'rb') as f: while True: carr = f.read(blosc.MAX_BUFFERSIZE) if len(carr) == 0: break arr.append(blosc.decompress(carr)) return pickle_.loads(b"".join(arr)) def _save_cache(folder, lastmodif_hash): fpath = join(folder, '.folder_hash') with open(fpath, 'w') as f: f.write(lastmodif_hash) def _get_file_list(folder): file_list = [] for (dir_, _, files) in os.walk(folder): if dir_ == folder: continue for f in files: fpath = join(dir_, f) if fpath.endswith('pkl') and os.path.basename(fpath) != 'all.pkl': file_list.append(fpath) return file_list def _merge(file_list): pbar = report.ProgressBar(len(file_list)) out = dict() for (i, fpath) in enumerate(file_list): d = unpickle(fpath) if isinstance(d, collections.Iterable): out.update(d) else: key = os.path.basename(fpath).split('.')[0] out[int(key)] = d pbar.update(i+1) pbar.finish() return out def pickle_merge(folder): """Merges pickle files from the specified folder and save it to `all.pkl`. """ file_list = _get_file_list(folder) if len(file_list) == 0: print('There is nothing to merge because no file'+ ' has been found in %s.' % folder) return with report.BeginEnd('Computing hashes'): ha = path.folder_hash(folder, ['all.pkl', '.folder_hash']) subfolders = [d for d in os.listdir(folder) if isdir(join(folder, d))] with path.temp_folder() as tf: for sf in subfolders: path.make_sure_path_exists(join(tf, sf)) path.cp(join(folder, sf), join(tf, sf)) file_list = _get_file_list(tf) with report.BeginEnd('Merging pickles'): out = _merge(file_list) with report.BeginEnd('Storing pickles'): pickle(out, join(folder, 'all.pkl')) _save_cache(folder, ha) return out
nilq/baby-python
python
#!/usr/bin/python # -*- coding: utf-8 -* """ The MIT License (MIT) Copyright (c) 2015 Christophe Aubert Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. """ __author__ = "Christophe Aubert" __version__ = "1.0" import dataBase.sqlite3 from dataBase.sqlite3 import ConnectDB class CreateDB(ConnectDB.ConnectDB): """ Classe CreateDb permet de création de la basse de données avec toute ces tables """ def __init__(self,path,name): """ Init @param path: @param name: """ ConnectDB.ConnectDB.__init__(self, path, name) self.connect() def create(self): """ Méthode de création des tables balise et value dans la basse de donnée """ try: self.cursor.execute('''CREATE TABLE `probe` ( `p_id` INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT UNIQUE, `p_name` REAL NOT NULL UNIQUE, `p_longitude` REAL NOT NULL UNIQUE, `p_latitude` REAL NOT NULL UNIQUE )''') except dataBase.sqlite3.OperationalError: print"Table `probe` already exists." # création de la table value dans la basse de donnée try: self.cursor.execute(''' CREATE TABLE `value` ( `v_id` INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT UNIQUE, `v_date` NUMERIC, `v_ozone` REAL, `v_temperature` REAL, `v_groundHumidity` REAL, `v_airHumidity` REAL, `v_waterTemperature` REAL, `v_waterLevel` REAL, `v_probe` INTEGER NOT NULL, FOREIGN KEY(`v_probe`) REFERENCES probe(`p_id`) )''') except dataBase.sqlite3.OperationalError: print"Table `value` already exists." print"Data base ready to use." self.close()
nilq/baby-python
python
class BinarTree: def __init__(self,e): self._root = Node(e) pass def add_left(self, e): pass def add_roght(self, e): pass def replace(self, p, e): pass def delete(self, p): pass def attach(self,t1, t2): pass class Node: def __ini__(self,element, parent = None, left = None, right = None): self._element = element self._left = None self._right = None self._parent = parent
nilq/baby-python
python
# Import libraries import numpy as np from flask import Flask, request, jsonify import nltk nltk.download('vader_lexicon') from nltk.sentiment.vader import SentimentIntensityAnalyzer app = Flask(__name__) @app.route('/api',methods=['GET', 'POST']) def predict(): # Get the data from the POST request. data = request.get_json(force=True) # Load the model model = SentimentIntensityAnalyzer() # Make prediction using model loaded from disk as per the data. prediction = model.polarity_scores(data['texts']) # prediction = data['texts'] output = prediction['compound'] return jsonify(output) if __name__ == '__main__': app.run(port=5000, debug=True)
nilq/baby-python
python
def gcd(a, b): while b: t = b b = a % b a = t return a def f(): limit = 3000 print('computing GCD of all pairs of integers in [1, ' + repr(limit) + ']^2') x = limit while x > 0: y = limit while y > 0: gcd(x, y) # a = x # b = y # while b: # t = b # b = a % b # a = t y = y - 1 x = x - 1 f() print('done')
nilq/baby-python
python
import os import sys import random import tensorflow as tf import matplotlib.pyplot as plt import skimage from samples.sunrgbd import sun, dataset, sun_config from mrcnn.model import log import mrcnn.model as modellib from mrcnn import visualize ROOT_DIR = os.path.abspath("./") sys.path.append(ROOT_DIR) # To find local version of the library MODEL_DIR = os.path.join(ROOT_DIR, "logs") HOLOLENS_MODE = False DEPTH_MODE = True HOLOLENS_IMAGE_PATHS = os.path.abspath("./images") CLASS_NAMES = ['BG'] CLASS_NAMES.extend(sun_config.CLASSES) config = sun_config.SunConfig(depth_mode=DEPTH_MODE) config.display() SUN_DIR = 'C:/Users/Yannick/Downloads/SUNRGBD/' SUN_WEIGHTS_PATH = os.path.join( ROOT_DIR, 'logs/reduced_classes/best_models/depth_strength3_num2.h5') # SUN_WEIGHTS_PATH = os.path.join( # ROOT_DIR, 'logs/reduced_classes/best_models/plain_0005.h5') IGNORE_IMAGES_PATH = os.path.abspath('../skip_image_paths.txt') sun.ROOT_DIR = ROOT_DIR sun_config.ROOT_DIR = ROOT_DIR dataset.ROOT_DIR = ROOT_DIR DEVICE = "/cpu:0" # /cpu:0 or /gpu:0 TEST_MODE = "inference" class InferenceConfig(config.__class__): # Run detection on one image at a time GPU_COUNT = 0 IMAGES_PER_GPU = 1 def __init__(self, **kwargs): super().__init__(**kwargs) def get_ax(rows=1, cols=1, size=14): """Return a Matplotlib Axes array to be used in all visualizations in the notebook. Provide a central point to control graph sizes. Adjust the size attribute to control how big to render images """ _, ax = plt.subplots(rows, cols, figsize=(size*cols, size*rows)) return ax def main(): assert not ( DEPTH_MODE and HOLOLENS_MODE), "No depth channel for Hololens available" # Set up model config = InferenceConfig(depth_mode=DEPTH_MODE) config.BATCH_SIZE = 1 config.DETECTION_MIN_CONFIDENCE = 0.8 with tf.device(DEVICE): model = modellib.MaskRCNN(mode="inference", model_dir=MODEL_DIR, config=config) model.load_weights(SUN_WEIGHTS_PATH, by_name=True) if HOLOLENS_MODE: visualize_hololens(model) else: visualize_sun(model) def visualize_hololens(model): for image_name in os.listdir(HOLOLENS_IMAGE_PATHS): if image_name[-4:] == '.jpg': rgb_path = os.path.join(HOLOLENS_IMAGE_PATHS, image_name) image = skimage.io.imread(rgb_path, plugin='pil') results = model.detect([image], verbose=1) r = results[0] visualize.display_instances(image, r['rois'], r['masks'], r['class_ids'], CLASS_NAMES, r['scores'], title="Predictions") def visualize_sun(model): if DEPTH_MODE: sun_dataset = dataset.SunDataset3D(skip_images_path=IGNORE_IMAGES_PATH) else: sun_dataset = dataset.SunDataset2D(skip_images_path=IGNORE_IMAGES_PATH) sun_dataset.load_sun(SUN_DIR, subset="test") # Must call before using the dataset sun_dataset.prepare() print("Images: {}\nClasses: {}".format( len(sun_dataset.image_ids), sun_dataset.class_names)) test_sample_ids = [684, 1065, 854, 717, 44] test_sample_ids.extend(sun_dataset.image_ids) for image_id in test_sample_ids: image, image_meta, gt_class_id, gt_bbox, gt_mask = modellib.load_image_gt( sun_dataset, config, image_id, use_mini_mask=False) info = sun_dataset.image_info[image_id] print("image ID: {}.{} ({}) {}".format(info["source"], info["id"], image_id, sun_dataset.image_reference(image_id))) results = model.detect([image], verbose=1) ax = get_ax(1) r = results[0] print(r['scores']) if DEPTH_MODE: image = image[:, :, :3] visualize.display_instances(image, r['rois'], r['masks'], r['class_ids'], sun_dataset.class_names, r['scores'], ax=ax, title="Predictions") log("gt_class_id", gt_class_id) log("gt_bbox", gt_bbox) log("gt_mask", gt_mask) if __name__ == '__main__': main()
nilq/baby-python
python
import csv import sqlite3 import os from HoundSploit.searcher.engine.utils import check_file_existence def create_db(): """ Create the database used by HoundSploit and hsploit """ init_path = os.path.abspath(os.path.expanduser("~") + "/.HoundSploit") db_path = os.path.abspath(init_path + "/hound_db.sqlite3") con = sqlite3.connect(db_path) cur = con.cursor() cur.execute("CREATE TABLE searcher_exploit (id, file, description, date, author, type, platform, port);") exploits_path = init_path + "/exploitdb/files_exploits.csv" with open(exploits_path, 'r', encoding="utf8") as fin: dr = csv.DictReader(fin) to_db = [(i['id'], i['file'], i['description'], i['date'], i['author'], i['type'], i['platform'], i['port']) for i in dr] cur.executemany("INSERT INTO searcher_exploit (id, file, description, date, author, type, platform, port) VALUES (?, ?, ?, ?, ?, ?, ?, ?);", to_db) cur.execute("CREATE TABLE searcher_shellcode (id, file, description, date, author, type, platform);") shellcodes_path = init_path + "/exploitdb/files_shellcodes.csv" with open(shellcodes_path, 'r', encoding="utf8") as fin: dr = csv.DictReader(fin) to_db = [(i['id'], i['file'], i['description'], i['date'], i['author'], i['type'], i['platform']) for i in dr] cur.executemany("INSERT INTO searcher_shellcode (id, file, description, date, author, type, platform) VALUES (?, ?, ?, ?, ?, ?, ?);", to_db) cur.execute("CREATE TABLE searcher_suggestion (searched, suggestion, autoreplacement);") suggestions_path = os.path.abspath(init_path + "/houndsploit/csv/files_suggestions.csv") with open(suggestions_path, 'r', encoding="utf8") as fin: dr = csv.DictReader(fin) to_db = [(i['searched'], i['suggestion'], i['autoreplacement']) for i in dr] cur.executemany("INSERT INTO searcher_suggestion (searched, suggestion, autoreplacement) VALUES (?, ?, ?);", to_db) custom_suggestions_path = os.path.abspath(init_path + "/custom_suggestions.csv") if check_file_existence(custom_suggestions_path): with open(custom_suggestions_path, 'r', encoding="utf8") as fin: dr = csv.DictReader(fin) to_db = [(i['searched'], i['suggestion'], i['autoreplacement']) for i in dr] cur.executemany("INSERT INTO searcher_suggestion (searched, suggestion, autoreplacement) VALUES (?, ?, ?);", to_db) cur.execute("CREATE TABLE searcher_bookmark (vulnerability_id, vulnerability_class, date);") custom_bookmarks_path = os.path.abspath(init_path + "/bookmarks.csv") if check_file_existence(custom_bookmarks_path): with open(custom_bookmarks_path, 'r', encoding="utf8") as fin: dr = csv.DictReader(fin) to_db = [(i['vulnerability_id'], i['vulnerability_class'], i['date']) for i in dr] cur.executemany("INSERT INTO searcher_bookmark (vulnerability_id, vulnerability_class, date) VALUES (?, ?, ?);", to_db) con.commit() con.close() try: f = open(os.path.abspath(init_path + "/.HoundSploit/houndsploit_db.lock")) f.close() os.remove(os.path.abspath(init_path + "/.HoundSploit/houndsploit_db.lock")) except IOError: pass
nilq/baby-python
python
import asyncio import logging import os import sys from argparse import SUPPRESS, ArgumentParser from typing import Callable, Dict, Mapping import yaml from aioregistry import ( AsyncRegistryClient, ChainedCredentialStore, DockerCredentialStore, default_credential_store, ) from tplbuild.cmd.base_build import BaseBuildUtility from tplbuild.cmd.base_lookup import BaseLookupUtility from tplbuild.cmd.base_prune import BasePruneUtility from tplbuild.cmd.build import BuildUtility from tplbuild.cmd.publish import PublishUtility from tplbuild.cmd.source_lookup import SourceLookupUtility from tplbuild.cmd.source_update import SourceUpdateUtility from tplbuild.cmd.utility import CliUtility from tplbuild.config import UserConfig from tplbuild.exceptions import TplBuildException from tplbuild.tplbuild import TplBuild LOGGER = logging.getLogger(__name__) ALL_UTILITIES: Mapping[str, Callable[[], CliUtility]] = { "build": BuildUtility, "base-build": BaseBuildUtility, "base-lookup": BaseLookupUtility, "base-prune": BasePruneUtility, "publish": PublishUtility, "source-lookup": SourceLookupUtility, "source-update": SourceUpdateUtility, } def create_main_parser(utilities: Mapping[str, CliUtility]) -> ArgumentParser: """Setup the argument parser configuration for each utility.""" parents = [ create_base_parser(), create_config_parser(), ] parser = ArgumentParser( description="templated build tool", parents=parents, ) subparsers = parser.add_subparsers( required=True, dest="utility", help="what tplbuild sub-utility to invoke", ) for subcommand, utility in utilities.items(): utility.setup_parser(subparsers.add_parser(subcommand, parents=parents)) return parser def create_base_parser() -> ArgumentParser: """ Create shared parser for basic CLI options. """ parser = ArgumentParser(description="Base tplbuild options", add_help=False) parser.add_argument( "--verbose", "-v", action="count", default=SUPPRESS, ) parser.add_argument( "-C", "--base-dir", required=False, default=SUPPRESS, help="Base directory for tplbuild", ) return parser def create_config_parser() -> ArgumentParser: """ Create shared parser that overrides user configuration tplbuild options. """ parser = ArgumentParser(description="Use config options", add_help=False) parser.add_argument( "--auth-file", required=False, default=SUPPRESS, help="Path to the container auth file", ) parser.add_argument( "--insecure", required=False, const=True, action="store_const", default=SUPPRESS, help="Disable server certificate verification", ) parser.add_argument( "--cafile", required=False, default=SUPPRESS, help="SSL context CA file", ) parser.add_argument( "--capath", required=False, default=SUPPRESS, help="SSL context CA directory", ) parser.add_argument( "--load-default-certs", required=False, const=True, action="store_const", default=SUPPRESS, help="Load system default certs always", ) parser.add_argument( "--build-jobs", required=False, default=SUPPRESS, help="Set max concurrent build jobs", ) parser.add_argument( "--push-jobs", required=False, default=SUPPRESS, help="Set max concurrent push or pull jobs", ) return parser def setup_logging(verbose: int) -> None: """Setup tplbuild default logging based on the verbosity level""" internal_level, external_level = logging.WARNING, logging.CRITICAL if verbose > 2: internal_level, external_level = logging.DEBUG, logging.INFO elif verbose > 1: internal_level, external_level = logging.DEBUG, logging.WARNING elif verbose: internal_level, external_level = logging.INFO, logging.ERROR tplbuild_root = logging.getLogger("tplbuild") tplbuild_root.propagate = False tplbuild_root.setLevel(internal_level) handler = logging.StreamHandler() handler.setFormatter( logging.Formatter("%(levelname)s: %(message)s"), ) tplbuild_root.addHandler(handler) logging.basicConfig( format="%(levelname)s(%(module)s): %(message)s", level=external_level, ) def load_user_config(args) -> UserConfig: """Load the user config. Override with settings from args as requested.""" user_config_locations = { os.path.join(args.base_dir, ".tplbuildconfig.yml"), os.path.expanduser("~/.tplbuildconfig.yml"), } user_config_data: Dict = {} for user_config_path in user_config_locations: try: with open(user_config_path, encoding="utf-8") as fconfig: user_config_data.update(**yaml.safe_load(fconfig)) except FileNotFoundError: continue except (ValueError, TypeError, yaml.YAMLError) as exc: raise TplBuildException(f"Failed to load user config: {exc}") from exc try: user_config = UserConfig(**user_config_data) except ValueError as exc: raise TplBuildException(f"Failed to load user config: {exc}") from exc if args.auth_file: user_config.auth_file = args.auth_file if args.insecure: user_config.ssl_context.insecure = True if args.cafile: user_config.ssl_context.cafile = args.cafile if args.capath: user_config.ssl_context.capath = args.capath if args.load_default_certs: user_config.ssl_context.load_default_certs = True if args.build_jobs is not None: if args.build_jobs <= 0: user_config.build_jobs = os.cpu_count() or 4 else: user_config.build_jobs = args.build_jobs if args.push_jobs is not None: if args.build_jobs <= 0: user_config.push_jobs = os.cpu_count() or 4 else: user_config.push_jobs = args.push_jobs return user_config def create_registry_client(user_config: UserConfig) -> AsyncRegistryClient: """Create an AsyncRegistryClient context from the passed arguments.""" creds = default_credential_store() if user_config.auth_file: try: creds = ChainedCredentialStore( DockerCredentialStore.from_file(user_config.auth_file), creds, ) except FileNotFoundError as exc: raise TplBuildException( f"could not open auth file {repr(user_config.auth_file)}" ) from exc return AsyncRegistryClient( creds=creds, ssl_context=user_config.ssl_context.create_context(), ) def create_tplbld( args, user_config: UserConfig, registry_client: AsyncRegistryClient ) -> TplBuild: """Create a TplBuild context from the passed arguments.""" return TplBuild.from_path( args.base_dir, user_config=user_config, registry_client=registry_client ) def apply_default_args(args) -> None: """ Apply default valeus to CLI arguments as needed. The normal default behavior of argparse does not work well with parsers shared across subparsers. """ defaults = dict( verbose=0, base_dir=".", auth_file=None, insecure=False, cafile=None, capath=None, load_default_certs=False, build_jobs=None, push_jobs=None, ) for key, val in defaults.items(): setattr(args, key, getattr(args, key, val)) async def amain() -> int: """Parse CLI options, setup logging, then invoke the requested utility""" utilities = { subcommand: utility_cls() for subcommand, utility_cls in ALL_UTILITIES.items() } parser = create_main_parser(utilities) args = parser.parse_args() apply_default_args(args) setup_logging(args.verbose) try: user_config = load_user_config(args) async with create_registry_client(user_config) as registry_client: async with create_tplbld(args, user_config, registry_client) as tplbld: return await utilities[args.utility].main(args, tplbld) except TplBuildException as exc: sys.stderr.write(f"{exc}\n") if exc.more_message: sys.stderr.write(f"{exc.more_message}\n") LOGGER.debug("got top level tplbuild exception", exc_info=True) return 1 except Exception: # pylint: disable=broad-except LOGGER.exception("Unexpected top-level exception") return 2 def main() -> int: """Synchronous entry point""" return asyncio.run(amain()) if __name__ == "__main__": sys.exit(main())
nilq/baby-python
python
import logging import time import pytest import os from stepfunctions.workflow import Workflow from tests import config from tests.integration_tests.utils import VersionChecker, LineageChecker from tests.integration_tests.workflows import simple_pipeline,\ diff_output_workflow, \ condition_workflow, \ parallel_states_workflow, \ diff_cache_param_workflow, \ long_workflow, \ complex_workflow os.environ['AWS_DEFAULT_REGION'] = config.REGION logging.basicConfig(level=logging.INFO) test_data = [] test_data += simple_pipeline.SimpleWorkflow.generate_cases() test_data += diff_output_workflow.DiffOutWorkflow.generate_cases() test_data += condition_workflow.ConditionWorkflow.generate_cases() test_data += parallel_states_workflow.ParallelWorkflow.generate_cases() test_data += diff_cache_param_workflow.DiffCacheParamWorkflow.generate_cases() test_data += long_workflow.LongWorkflow.generate_cases() test_data += complex_workflow.ComplexWorkflow.generate_cases() @pytest.mark.parametrize('workflow_name, definition, inputs, exp_output, bundle_names, gaps', test_data) def test_workflow(workflow_name, definition, inputs, exp_output, bundle_names, gaps): target_flow = [flow for flow in Workflow.list_workflows() if flow['name'] == workflow_name] if len(target_flow) > 0: workflow = Workflow.attach(target_flow[0]['stateMachineArn']) workflow.update(definition=definition, role=config.EXECUTION_ROLE) else: workflow = Workflow(workflow_name, definition=definition, role=config.EXECUTION_ROLE) workflow.create() # this sleep cmd is vital as we should wait until the update comes into effect time.sleep(10) # # initialize version checker before the execution to record bundle versions checker = VersionChecker(context=config.CONTEXT, bundle_names=bundle_names) execution = workflow.execute(inputs=inputs) result = execution.get_output(wait=True) assert result == exp_output, 'Returned result {} doesn\'t match expected {}'.format(result, exp_output) # check if new bundle exists for bd, gap in zip(bundle_names, gaps): checker.validate_execution(bd=bd, expected_version_gap=gap)
nilq/baby-python
python
#!/usr/bin/python3 # coding: utf-8 ################################################################################ # Apple PiのLCDとLEDへAmbientの状態を表示する # # 準備: # AmbientのKeyを(https://ambidata.io)で取得し、ambient_chidとambient_rkeyへ代入 # # Copyright (c) 2018-2019 Wataru KUNINO ################################################################################ import json import urllib.request import datetime from time import sleep import ApplePi.initLCD import ApplePi.onLED1 import ApplePi.onLED2 import ApplePi.offLED1 import ApplePi.offLED2 import subprocess ambient_chid='0000' # ここにAmbientで取得したチャネルIDを入力 ambient_rkey='0123456789abcdef' # リードキーを入力 ※ライトキーではない amdient_tag='d1' # データ番号d1~d8のいずれかを入力 ap_locate='ApplePi/locateLCD.py' ap_print='ApplePi/printLCD.py' ap_led1=['ApplePi/offLED1.py','ApplePi/onLED1.py'] ap_led2=['ApplePi/offLED2.py','ApplePi/onLED2.py'] while True: url = 'https://ambidata.io/api/v2/channels/'+ambient_chid+'/data\?readKey='+ambient_rkey+'&n=1' post = urllib.request.Request(url) res = urllib.request.urlopen(post) if res: payl = json.loads(res.read().decode()) # print('Response:', payl) if amdient_tag in payl[0]: val = int(payl[0][amdient_tag]) date= payl[0]['created'] date = datetime.datetime.strptime(date,"%Y-%m-%dT%H:%M:%S.%fZ") date += datetime.timedelta(hours=9) print(date.strftime('%Y/%m/%d %H:%M'), end='') # 日付を出力 print(',',val) # 受信データを出力 # ApplePiへの表示・出力 subprocess.call([ap_locate,'0','0']) subprocess.call([ap_print,date.strftime('%Y/%m/%d')[2:]]) subprocess.call([ap_locate,'0','1']) subprocess.call([ap_print,date.strftime('%H:%M')]) subprocess.call([ap_locate,'7','1']) subprocess.call([ap_print,str(val)]) if val <= 0: subprocess.call([ap_led1[0]]) subprocess.call([ap_led2[0]]) elif val == 1: subprocess.call([ap_led1[0]]) subprocess.call([ap_led2[1]]) elif val >= 2: subprocess.call([ap_led1[1]]) subprocess.call([ap_led2[1]]) sleep(20)
nilq/baby-python
python
import numpy as np signal = np.array([-2, 8, 6, 4, 1, 0, 3, 5], dtype=float) fourier = np.fft.fft(signal) print(fourier) print(signal.size) freq = np.fft.fftfreq(signal.size, d=0.1) print(freq)
nilq/baby-python
python
# -*- coding: utf-8 -*- # Generated by Django 1.10.3 on 2017-02-07 22:39 from __future__ import unicode_literals from django.db import migrations class Migration(migrations.Migration): dependencies = [ ('mars', '0010_teammember_role'), ] operations = [ migrations.AlterModelOptions( name='teammember', options={'ordering': ['position', 'name'], 'verbose_name': 'Team Member', 'verbose_name_plural': 'Team Members'}, ), ]
nilq/baby-python
python
#!/usr/bin/env python # ============================================================================= # MODULE DOCSTRING # ============================================================================= """ OEMC integrator for hybrid MC/MD simulations DESCRIPTION This module provides OEMC integrators for OpenMM. EXAMPLES COPYRIGHT @author Hyuntae Jung <hjung52@wisc.edu> All code in this repository is released under the MIT License. This program is free software: you can redistribute it and/or modify it under the terms of the MIT License. This program 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 MIT License for more details. You should have received a copy of the MIT License along with this program. """ # ============================================================================================ # GLOBAL IMPORTS # ============================================================================================ import logging import simtk.unit as unit import simtk.openmm as mm from openmm_oemc.constants import logger = logging.getLogger(__name__) # Energy unit used by OpenMM unit system _OPENMM_ENERGY_UNIT = unit.kilojoules_per_mole # ============================================================================================ # BASE CLASSES # ============================================================================================ if __name__ == '__main__': import doctest doctest.testmod()
nilq/baby-python
python
import discord import random from discord.ext import commands from gameConfig import * import os coin = [0, 1] @bot.command() async def coinflip(ctx): embed = discord.Embed( title = "Coinflip💰", description = 'React with the emoji below to choose heads or tails \nHeads: 💸 \nTails: 🦨', color = discord.Color.gold(), ) await ctx.channel.purge(limit=5) msg = await ctx.send(embed=embed) await msg.add_reaction('💸') await msg.add_reaction('🦨') def checkReaction(reaction, user): return user != bot.user and (str(reaction.emoji) == '💸' or str(reaction.emoji) == '🦨') reaction, user = await bot.wait_for("reaction_add", check = checkReaction) #Heads roll if str(reaction.emoji) == '💸': await ctx.send("You chose *Heads*, good luck! Flipping....") if random.choice(coin) == 0: await ctx.send("Coin lands on **Heads**") await ctx.send("Congrats! You win!!🏆 (Use **-reset** to leave game)") elif random.choice(coin) == 1: await ctx.send("Coin lands on **Tails**") await ctx.send("Sorry, you lost💩 (Use **-reset** to leave game)") #Tails roll elif str(reaction.emoji) == '🦨': await ctx.send("You chose *Tails*, good luck! Flipping....") if random.choice(coin) == 1: await ctx.send("Coin lands on **Tails**") await ctx.send("Congrats! You win!!🏆 (Use **-reset** to leave game)") elif random.choice(coin) == 0: await ctx.send("Coin lands on **Heads**") await ctx.send("Sorry, you lost💩 (Use **-reset** to leave game)") @bot.command() async def reset(ctx): await ctx.send("Resetting files...use **-menu** for the game menu") os.system('python startup.py') pass bot.run(TOKEN, bot=True, reconnect=True) client.run(TOKEN)
nilq/baby-python
python
# -*- coding: utf-8 -*- # cython: language_level=3 # Tanjun Examples - A collection of examples for Tanjun. # Written in 2021 by Lucina Lucina@lmbyrne.dev # # To the extent possible under law, the author(s) have dedicated all copyright # and related and neighboring rights to this software to the public domain worldwide. # This software is distributed without any warranty. # # You should have received a copy of the CC0 Public Domain Dedication along with this software. # If not, see <https://creativecommons.org/publicdomain/zero/1.0/>. """Example of how to run a standard Tanjun client instance with a RESTBot.""" import asyncio import hikari import tanjun from examples import config from examples import impls from examples import protos async def run() -> None: loaded_config = config.ExampleConfig.load() # While a BOT token is assumed in this example, a client credentials OAuth2 # token can also be used with Tanjun but this may limit functionality. bot = hikari.RESTBot(loaded_config.bot_token, hikari.TokenType.BOT) database = impls.DatabaseImpl() client = ( # Passing True for declare_global_commands here instructs the client to # declare the slash commands within it which are marked as "global" during # the first startup. # A guild ID may also be passed here to instruct it to just declare the # global commands for that guild, this can be helpful for debug purposes. tanjun.Client.from_rest_bot(bot, declare_global_commands=True) # Unlike a gateway bot bound client, only slash commands will be automatically # executed by a client that's bound to a rest bot. .load_modules("examples.slash_component") .set_type_dependency(config.ExampleConfig, loaded_config) .set_type_dependency(protos.DatabaseProto, database) # Here we use client callbacks to manage the database, STOPPING can also be used to stop it. .add_client_callback(tanjun.ClientCallbackNames.STARTING, database.connect) ) # Unlike with a gateway bot, for RESTBots hikari has no lifetime event # dispatch which can be used to implicitly startup and close the Tanjun # client. Instead, we must directly startup and close Tanjun. await bot.start() # Note that starting a Tanjun client before the relevant bot may lead # to erroneous behaviour as it won't be able to make any requests. # While this example uses the client as a context manager to implicitly start # and close it, the `open` and `close` methods can alternatively be used to the same effect. async with client: await bot.join() # This waits until the bot is closed before closing Tanjun by exiting the context manager. def main(): asyncio.run(run()) if __name__ == "__main__": main()
nilq/baby-python
python
import random import pandas as pd import numpy as np import matplotlib.pyplot as plt #%matplotlib inline import tensorflow as tf import keras.backend as K from keras.utils import to_categorical from keras import metrics from keras.models import Model, load_model from keras.layers import Input, BatchNormalization, Activation, Dense, Dropout,Maximum from keras.layers.core import Lambda, RepeatVector, Reshape from keras.layers.convolutional import Conv2D, Conv2DTranspose,Conv3D,Conv3DTranspose from keras.layers.pooling import MaxPooling2D, GlobalMaxPool2D,MaxPooling3D from keras.layers.merge import concatenate, add from keras.callbacks import EarlyStopping, ModelCheckpoint, ReduceLROnPlateau from keras.optimizers import Adam from keras.preprocessing.image import ImageDataGenerator, array_to_img, img_to_array, load_img from skimage.io import imread, imshow, concatenate_images from skimage.transform import resize from sklearn.utils import class_weight from keras.callbacks import ModelCheckpoint from keras.callbacks import CSVLogger from keras.callbacks import EarlyStopping import os from skimage.io import imread, imshow, concatenate_images from skimage.transform import resize # from medpy.io import load import numpy as np #import cv2 import nibabel as nib from PIL import Image def conv_block(input_mat,num_filters,kernel_size,batch_norm): X = Conv3D(num_filters,kernel_size=(kernel_size,kernel_size,kernel_size),strides=(1,1,1),padding='same')(input_mat) if batch_norm: X = BatchNormalization()(X) X = Activation('relu')(X) X = Conv3D(num_filters,kernel_size=(kernel_size,kernel_size,kernel_size),strides=(1,1,1),padding='same')(X) if batch_norm: X = BatchNormalization()(X) X = Activation('relu')(X) return X def Unet_3d(input_img, n_filters = 8, dropout = 0.2, batch_norm = True): c1 = conv_block(input_img,n_filters,3,batch_norm) p1 = MaxPooling3D(pool_size=(2, 2, 2), strides=2)(c1) p1 = Dropout(dropout)(p1) c2 = conv_block(p1,n_filters*2,3,batch_norm); p2 = MaxPooling3D(pool_size=(2,2,2) ,strides=2)(c2) p2 = Dropout(dropout)(p2) c3 = conv_block(p2,n_filters*4,3,batch_norm); p3 = MaxPooling3D(pool_size=(2,2,2) ,strides=2)(c3) p3 = Dropout(dropout)(p3) c4 = conv_block(p3,n_filters*8,3,batch_norm); p4 = MaxPooling3D(pool_size=(2,2,2) ,strides=2)(c4) p4 = Dropout(dropout)(p4) c5 = conv_block(p4,n_filters*16,3,batch_norm); u6 = Conv3DTranspose(n_filters*8, (3,3,3), strides=(2, 2, 2), padding='same')(c5); u6 = concatenate([u6,c4]); c6 = conv_block(u6,n_filters*8,3,batch_norm) c6 = Dropout(dropout)(c6) u7 = Conv3DTranspose(n_filters*4,(3,3,3),strides = (2,2,2) , padding= 'same')(c6); u7 = concatenate([u7,c3]); c7 = conv_block(u7,n_filters*4,3,batch_norm) c7 = Dropout(dropout)(c7) u8 = Conv3DTranspose(n_filters*2,(3,3,3),strides = (2,2,2) , padding='same')(c7); u8 = concatenate([u8,c2]); c8 = conv_block(u8,n_filters*2,3,batch_norm) c8 = Dropout(dropout)(c8) u9 = Conv3DTranspose(n_filters,(3,3,3),strides = (2,2,2) , padding='same')(c8); u9 = concatenate([u9,c1]); c9 = conv_block(u9,n_filters,3,batch_norm) outputs = Conv3D(4, (1, 1,1), activation='softmax')(c9) print("!!!!!!!!!!!!!!!!!!!") print(outputs.shape) model = Model(inputs=input_img, outputs=outputs) return model def standardize(image): standardized_image = np.zeros(image.shape) # # iterate over the `z` dimension for z in range(image.shape[2]): # get a slice of the image # at channel c and z-th dimension `z` image_slice = image[:,:,z] # subtract the mean from image_slice centered = image_slice - np.mean(image_slice) # divide by the standard deviation (only if it is different from zero) if(np.std(centered)!=0): centered = centered/np.std(centered) # update the slice of standardized image # with the scaled centered and scaled image standardized_image[:, :, z] = centered ### END CODE HERE ### return standardized_image def dice_coef(y_true, y_pred, epsilon=0.00001): """ Dice = (2*|X & Y|)/ (|X|+ |Y|) = 2*sum(|A*B|)/(sum(A^2)+sum(B^2)) ref: https://arxiv.org/pdf/1606.04797v1.pdf """ axis = (0,1,2,3) dice_numerator = 2. * K.sum(y_true * y_pred, axis=axis) + epsilon dice_denominator = K.sum(y_true*y_true, axis=axis) + K.sum(y_pred*y_pred, axis=axis) + epsilon return K.mean((dice_numerator)/(dice_denominator)) def dice_coef_loss(y_true, y_pred): return 1-dice_coef(y_true, y_pred) input_img = Input((128,128,128,4)) model = Unet_3d(input_img,8,0.1,True) learning_rate = 0.001 epochs = 5000 decay_rate = 0.0000001 model.compile(optimizer=Adam(lr=learning_rate, decay = decay_rate), loss=dice_coef_loss, metrics=[dice_coef]) model.summary() path = '../input/vs-brats2018/miccai_brats_2018_data_training/HGG' all_images = os.listdir(path) #print(len(all_images)) all_images.sort() data = np.zeros((240,240,155,4)) image_data2=np.zeros((240,240,155)) for epochs in range(60): for image_num in range(180): # data preprocessing starts here x = all_images[image_num] print(x) folder_path = path + '/' + x; modalities = os.listdir(folder_path) modalities.sort() #data = [] w = 0 for j in range(len(modalities)-1): #print(modalities[j]) image_path = folder_path + '/' + modalities[j] if(image_path[-7:-1] + image_path[-1] == 'seg.nii'): img = nib.load(image_path); image_data2 = img.get_data() image_data2 = np.asarray(image_data2) print("Entered ground truth") else: img = nib.load(image_path); image_data = img.get_data() image_data = np.asarray(image_data) image_data = standardize(image_data) data[:,:,:,w] = image_data print("Entered modality") w = w+1 print(data.shape) print(image_data2.shape) reshaped_data=data[56:184,80:208,13:141,:] reshaped_image_data2=image_data2[56:184,80:208,13:141] for v in range(128): print("x") plt.imshow(reshaped_data[:,:,v,0]) plt.show(block=False) plt.pause(1) plt.close() print("y") imgplot = plt.imshow(reshaped_image_data2[:,:,v]) plt.show(block=False) plt.pause(1) plt.close() print("new") reshaped_data=reshaped_data.reshape(1,128,128,128,4) reshaped_image_data2=reshaped_image_data2.reshape(1,128,128,128) reshaped_image_data2[reshaped_image_data2==4] = 3 hello = reshaped_image_data2.flatten() #y_to = keras.utils.to_categorical(y_to,num_classes=2) print(reshaped_image_data2.shape) #print(hello[hello==3].shape) print("Number of classes",np.unique(hello)) class_weights = class_weight.compute_class_weight('balanced',np.unique(hello),hello) print(class_weights) reshaped_image_data2 = to_categorical(reshaped_image_data2, num_classes = 4) print(reshaped_data.shape) print(reshaped_image_data2.shape) print(type(reshaped_data)) model.fit(x=reshaped_data,y=reshaped_image_data2, epochs = 1 , class_weight = class_weights) model.save('../working/3d_model.h5') model.save('../working/3d_model.h5')
nilq/baby-python
python
""" convert prepared resnet model into tflite model run this python script in server root """ import argparse import os import tensorflow as tf from models import mobilenet, resnet50 from tensorflow import lite as tf_lite TFLITE_MODEL_DIR = '../client/app/src/main/assets' def convert_resnet() -> None: CHECKPOINT_DIR = './checkpoints/resnet50' checkpoint = tf.train.latest_checkpoint(CHECKPOINT_DIR) model = resnet50(1001) model.load_weights(checkpoint) converter = tf_lite.TFLiteConverter.from_keras_model(model) tflite_model = converter.convert() if not os.path.exists(TFLITE_MODEL_DIR): os.mkdir(TFLITE_MODEL_DIR) with open(os.path.join(TFLITE_MODEL_DIR, 'resnet50.tflite'), 'wb') as f: f.write(tflite_model) def convert_mobilenet() -> None: # Create tf model CHECKPOINT_PATH = './checkpoints/mobilenet_v1_1.0_224/mobilenet_1_0_224_tf.h5' model = mobilenet() model.load_weights(CHECKPOINT_PATH) # Convert to tflite converter = tf.lite.TFLiteConverter.from_keras_model(model) tflite_model = converter.convert() if not os.path.exists(TFLITE_MODEL_DIR): os.mkdir(TFLITE_MODEL_DIR) # Save tflite model with open(os.path.join(TFLITE_MODEL_DIR, 'mobilenet_v1.tflite'), 'wb') as f: f.write(tflite_model) def main(args) -> None: if args.model == 'all': convert_resnet() convert_mobilenet() elif args.model == 'resnet50': convert_resnet() elif args.model == 'mobilenet': convert_mobilenet() else: raise ValueError("Not supported model: {}".format(args.model)) if __name__ == "__main__": os.environ['CUDA_VISIBLE_DEVICES'] = '' parser = argparse.ArgumentParser(description='Process some integers.') parser.add_argument('-m', '--model', default='all', help='model to convert') args = parser.parse_args() main(args)
nilq/baby-python
python
# Copyright 1996-2018 Cyberbotics Ltd. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. """Test textures.""" import unittest import os import fnmatch from PIL import Image class TestTextures(unittest.TestCase): """Unit test of the textures.""" def setUp(self): """Get all the textures to be tested.""" # 1. Get all the images from projects and resources images = [] for directory in ['projects', 'resources']: for rootPath, dirNames, fileNames in os.walk(os.environ['WEBOTS_HOME'] + os.sep + directory): for fileName in fnmatch.filter(fileNames, '*.png'): image = os.path.join(rootPath, fileName) images.append(image) for fileName in fnmatch.filter(fileNames, '*.jpg'): image = os.path.join(rootPath, fileName) images.append(image) # 2. filter-out the images which are not textures self.textures = [] for image in images: if not ( 'controllers' in image or 'icons' in image or 'libraries' in image or 'plugins' in image or 'simulator-sdk' in image or 'resources' + os.sep + 'images' in image or 'resources' + os.sep + 'web' in image or 'resources' + os.sep + 'wren' in image ): self.textures.append(image) def test_textures_dimensions_are_power_of_two(self): """Test that the released textures dimensions are power of two.""" def is_perfect_power_of_two(a): assert isinstance(a, int) while a % 2 == 0: a = a / 2 if a == 1: return True return False for texture in self.textures: im = Image.open(texture) self.assertTrue( is_perfect_power_of_two(im.size[0]) and is_perfect_power_of_two(im.size[1]), msg='texture "%s": dimension is not a power of two: (%d, %d)' % (texture, im.size[0], im.size[1]) ) def test_textures_profile(self): """Test that the released textures don't contain an ICC profile.""" for texture in self.textures: im = Image.open(texture) self.assertTrue( im.info.get("icc_profile") is None, msg='texture "%s" contains an ICC profile' % (texture) ) if __name__ == '__main__': unittest.main()
nilq/baby-python
python
from __future__ import print_function import sys import os import runpy import traceback import contextlib import argparse from tracestack.handler import ExceptionHandler from tracestack.console import TracestackConsole def run(): """Runs the script provided by the arguments, using the tracestack exception handler. """ parser = _build_parser() args = vars(parser.parse_args()) print(args) script = args.pop("script") handler = ExceptionHandler(**args) if script: # set up the system variables sys.argv = sys.argv[1:] sys.path[0] = os.path.dirname(os.path.abspath(script)) try: runpy.run_path(script, run_name="__main__") except: einfo = sys.exc_info() _print_clean_traceback(einfo) handler.handle_error(*einfo) else: # no additional arguments were given; run the REPL console = TracestackConsole(**args) console.interact() def _build_parser(): """Returns the argument parser (which is built using argparse).""" parser = argparse.ArgumentParser(description='instantly search your ' + \ 'Python error messages on' + \ ' the web', formatter_class=argparse.RawTextHelpFormatter) parser.add_argument('script', metavar='SCRIPT', type=str, nargs='?', help='the Python script') parser.add_argument('arguments', metavar='ARGUMENTS', type=str, nargs='*', help='any arguments to the script') parser.add_argument('-p', '--prompt', help='prompt the user rather than immediately searching', action='store_true') parser.add_argument('-e', '--engine', help="""the search engine to use: 'default': Google search limited to stackoverflow.com 'google': Google search of the full web 'stackoverflow': StackOverflow site search""", default="default", choices=['default', 'google', 'stackoverflow'], metavar="ENGINE", type=str) return parser def _print_clean_traceback(einfo): """Print the traceback, without showing all the overhead added by tracestack.""" extracted = traceback.extract_tb(einfo[2]) if extracted[-1][0] in ('trace', 'runpy.py'): # the error call is coming from inside the house # this shouldn't happen, but if it does, do the default behavior sys.__excepthook__(sys.exc_info) else: # remove the traceback levels that relate to runpy or trace extracted = [level for level in extracted if os.path.basename(level[0]) not in ('command_line.py', 'runpy.py')] extracted = traceback.format_list(extracted) # print as if it were a normal traceback print("Traceback (most recent call last):", file=sys.stderr) for level in extracted: print(level, end="", file=sys.stderr) for line in traceback.format_exception_only(einfo[0], einfo[1]): print(line, end="", file=sys.stderr)
nilq/baby-python
python