def autodl_auc(solution, prediction, valid_columns_only=True): """Compute normarlized Area under ROC curve (AUC). Return Gini index = 2*AUC-1 for binary classification problems. Should work for a vector of binary 0/1 (or -1/1)"solution" and any discriminant values for the predictions. If solution and prediction are not vectors, the AUC of the columns of the matrices are computed and averaged (with no weight). The same for all classification problems (in fact it treats well only the binary and multilabel classification problems). When `valid_columns` is not `None`, only use a subset of columns for computing the score. """ if valid_columns_only: valid_columns = get_valid_columns(solution) if len(valid_columns) < solution.shape[-1]: log_warning("Some columns in solution have only one class, " + "ignoring these columns for evaluation.") solution = solution[:, valid_columns].copy() prediction = prediction[:, valid_columns].copy() label_num = solution.shape[1] auc = np.empty(label_num) for k in range(label_num): r_ = tiedrank(prediction[:, k]) s_ = solution[:, k] if sum(s_) == 0: print("WARNING: no positive class example in class {}" \ .format(k + 1)) npos = sum(s_ == 1) nneg = sum(s_ < 1) auc[k] = (sum(r_[s_ == 1]) - npos * (npos + 1) / 2) / (nneg * npos) return 2 * mvmean(auc) - 1
def handle_data_frame(self, packet: Packet): if is_valid_checksum(packet): self.host.send_ack() self.command_handler.process_packet(packet.command) else: log_warning("Invalid checksum") self.host.send_nak()
class ControllerImpl(Controller): def __init__(self, link: str): self.port = SerialPort( link=link, options="b115200,parenb=0,parodd=0,cs8,cstopb=0,crtscts=0,echo=0" ) self.packet_builder = PacketBuilder() self.running = False def initialize(self): self.port.open() self.running = True def finalize(self): self.running = False self.port.close() async def poll(self) -> AsyncIterator[Bytes]: while True: if (data := await self.port.read_byte()) is not None: byte = int.from_bytes(data, byteorder='big') if (packet := self.packet_builder.process(byte)) is not None: log_debug(f"RX: {dump_hex(packet)}") yield packet elif self.running: log_warning("Pipe broken") self.packet_builder.reset() self.port.open()
def handle_send_data(self, command: Packet): known_node = self.network_controller.node_infos.find( command.node_id) is not None self.request_manager.send_response('SEND_DATA', result=known_node) if not known_node: log_warning(f"Unknown node {command.node_id}") return async def flow(): async for tx_status in self.network_controller.send_data( command.node_id, command.data): self.request_manager.send_request( 'SEND_DATA', function_id=command.function_id, tx_status=tx_status) return flow()
def handle_encapsulated_command(self, command: Command, context: Context, header: int): nonce = self.internal_nonce_table[context.node_id].get() if nonce is None or nonce[0] != command.receiver_nonce_id: log_warning("Failed to decrypt the message: nonce expired") return decrypted = self.node.security_utils.decrypt_and_verify( payload=command.encrypted_payload, sender_nonce=command.initialization_vector, receiver_nonce=nonce, header=header, sender=context.node_id, receiver=self.node.node_id, mac=command.message_authentication_code) if decrypted is not None: payload = self.node.serializer.to_object('EncryptedPayload', decrypted) if (command := self.get_command(context, payload)) is not None: self.channel.handle_command(command, context.copy(secure=True))
if not Debug_Mode: sys.stdout = codecs.open(Log_File, encoding='utf-8', mode='w') sys.stderr = codecs.open(Error_File, encoding='utf-8', mode='w') # search for existing options file, load it Options_File = os.path.join(User_Folder, 'options.info') if not os.path.exists(Options_File): shutil.copyfile(c.Options_Default_File, Options_File) # create the single options object, load options and send a log message t.load_options(Options_File) t.log_info('options loaded from user folder ({})'.format(User_Folder)) # between versions the format of the options might change, then it's better to overwrite old options if t.options[c.O_OPTIONS_VERSION] < c.Options_Version: t.log_warning('outdated version of options, have been overwritten') shutil.copyfile(c.Options_Default_File, Options_File) t.load_options(Options_File) # test for phonon availability if t.options[c.OM_PHONON_SUPPORTED]: try: from PySide.phonon import Phonon except ImportError: t.log_error('Phonon backend not available, no sound.') t.options[c.OM_PHONON_SUPPORTED] = False # special case of some desktop environments under Linux where full screen mode does not work well if t.options[c.OG_FULLSCREEN_SUPPORTED]: desktop_session = os.environ.get("DESKTOP_SESSION") if desktop_session and (desktop_session.startswith('ubuntu') or 'xfce' in desktop_session
def remove_associations_from_all_groups(self, node_ids: List[int]): log_warning("Removing nodes from all association groups is not supported by COMMAND_CLASS_ASSOCIATION v1")
def handle_command(self, command: Command, context: Context): if self.supported_non_securely or context.secure: self.visit(command, context) else: log_warning("Incorrect security level")
def visit_default(self, command: Command, command_name: str): log_warning(f"Unhandled command: {command_name}")
def wrapper(self, command: Command, context: Context): if context.secure: fn(self, command, context) else: log_warning("Incorrect security level")
decrypted = self.node.security_utils.decrypt_and_verify( payload=command.encrypted_payload, sender_nonce=command.initialization_vector, receiver_nonce=nonce, header=header, sender=context.node_id, receiver=self.node.node_id, mac=command.message_authentication_code) if decrypted is not None: payload = self.node.serializer.to_object('EncryptedPayload', decrypted) if (command := self.get_command(context, payload)) is not None: self.channel.handle_command(command, context.copy(secure=True)) else: log_warning("Failed to decrypt the message: unable to verify") def get_command(self, context: Context, payload: Object) -> Optional[List[int]]: if not payload.sequenced: return payload.command elif not payload.second: self.sequence_table[(context.node_id, payload.sequence_counter)] = payload.command else: first = self.sequence_table.pop( (context.node_id, payload.sequence_counter)) return first + payload.command @classmethod def generate_nonce(cls) -> List[int]:
def get_security_command_class(self) -> Optional['Security1']: class_id = CONSTANTS['CommandClassId']['COMMAND_CLASS_SECURITY'] return self.get_command_class(class_id) def handle_command(self, data: List[int], context: Context): class_id = data[0] if (command_class := self.get_command_class(class_id)) is not None: command = self.node.serializer.from_bytes( data, command_class.class_version) log_command(context.node_id, context.endpoint, self.node.node_id, self.endpoint, command) command_class.handle_command(command, context) else: log_warning( "Channel does not support command class 0x{:02X}".format( class_id)) def send_command(self, command: Command, context: Context): log_command(self.node.node_id, self.endpoint, context.node_id, context.endpoint, command) data = self.node.serializer.to_bytes(command) if context.respond_with_basic: self.get_basic_command_class().send_report(context, command) elif self.endpoint == 0 and context.endpoint == 0: self.node.send_command(data, context) else: multi_channel_cc = self.node.channels[ 0].get_multi_channel_command_class() multi_channel_cc.send_encapsulated_command(context, self.endpoint,
def visit_default(self, packet: Packet, packet_name: str): log_warning(str(packet))
def handle_nak_can_frame(self, packet: Packet): log_warning(f"{packet.get_meta('name')} received")