text:
from artiq.experiment import *
from jax import Sequence

__all__ = ["StateDetect"]

class StateDetect(Sequence):
    """An example state detection sequence.

    Turns on the cool and repump DDSes, and counts the PMT output.
    """
    kernel_invariants = {
        "_cool_dds", "_repump_dds", "_cool_drift_tracker", "_repump_drift_tracker"
    }

    required_parameters = [
        ("devices", "cool_dds"),
        ("devices", "repump_dds"),
        ("devices", "pmt_edge_counter"),
        ("state_detect", "cool_detuning"),
        ("state_detect", "cool_amplitude"),
        ("state_detect", "cool_drift_tracker"),
        ("state_detect", "repump_detuning"),
        ("state_detect", "repump_amplitude"),
        ("state_detect", "repump_drift_tracker"),
        ("state_detect", "detect_time"),
    ]

    def __init__(self, exp, parameter_group):
        super().__init__(exp, parameter_group)
        self.setup()

    @host_only
    def setup(self):
        """Initializes devices and sets constants.

        Float number calculation is expensive on the device, so they should be calculated
        in host code if possible.
        """
        self._cool_dds = self.exp.get_device(self.p.devices.cool_dds)
        self._repump_dds = self.exp.get_device(self.p.devices.repump_dds)
        # pmt needs to be accessed in other classes.
        self.pmt = self.exp.get_device(self.p.devices.pmt_edge_counter)

        phase = 0.
        self._cool_pow = self._cool_dds.turns_to_pow(phase)
        self._repump_pow = self._repump_dds.turns_to_pow(phase)

        s = self.p.state_detect
        self._cool_asf = self._cool_dds.amplitude_to_asf(s.cool_amplitude)
        self._repump_asf = self._repump_dds.amplitude_to_asf(s.repump_amplitude)

        self._cool_drift_tracker = self.exp.get_drift_tracker(s.cool_drift_tracker)
        self._repump_drift_tracker = self.exp.get_drift_tracker(s.repump_drift_tracker)

        self._detect_time_mu = self.exp.core.seconds_to_mu(s.detect_time)

        cool_frequency = self._cool_drift_tracker.get_frequency_host(d.cool_detuning)
        self._cool_ftw = self._cool_dds.frequency_to_ftw(cool_frequency)
        repump_frequency = self._repump_drift_tracker.get_frequency_host(d.repump_detuning)
        self._repump_ftw = self._repump_dds.frequency_to_ftw(repump_frequency)

    @kernel
    def run(self):
        self._cool_dds.set_mu(self._cool_ftw, self._cool_pow, self._cool_asf)
        self._repump_dds.set_mu(self._repump_ftw, self._repump_pow, self._repump_asf)

        delay_mu(self.exp.rtio_cycle_mu)
        self._cool_dds.sw.on()
        self._repump_dds.sw.on()

        # counts rising edges.
        self.pmt.gate_rising_mu(self._detect_time_mu)
        self._cool_dds.sw.off()
        self._repump_dds.sw.off()

        delay_mu(self.exp.rtio_cycle_mu)
        self._cool_dds.set_mu(self._cool_ftw, self._cool_pow, 0)
        self._repump_dds.set_mu(self._repump_ftw, self._repump_pow, 0)

max_stars_repo_name: Jayich-Lab/jax

max_issues_repo_name: Jayich-Lab/jax

max_forks_repo_name: Jayich-Lab/jax

reason: from jax


text:
# coding=utf-8
# Copyright 2021 TUNiB Inc.
# Copyright (c) Facebook, Inc. and its affiliates.
#
# This source code is licensed under the MIT license found in the
# LICENSE.apache-2.0 file in the root directory of this source tree.

import os
import struct
from itertools import accumulate
from typing import Union

import numpy as np
from torch import Tensor
from torch.utils.data import Dataset

from oslo.data.indexing import NumpyUtils, PathUtils

class IndexedDatasetBuilder(object):
    """
    Builder of Indexed dataset builder

    Args:
        output_file_path (str): output file path
    """

    def __init__(self, output_file_path):
        self.out_file = open(output_file_path, "wb")
        self.dtype = np.int32
        self.data_offsets = [0]
        self.dim_offsets = [0]
        self.sizes = []
        self.element_size = NumpyUtils.element_sizes[self.dtype]
        self.doc_idx = [0]

    def add_item(self, tensor: Tensor):
        """
        Add item to output file

        Args:
            tensor (Tensor): tensor to save
        """
        _bytes = np.array(tensor.numpy(), dtype=self.dtype)
        _bytes = self.out_file.write(_bytes)
        self.data_offsets.append(self.data_offsets[-1] + (_bytes / self.element_size))

        for s in tensor.size():
            self.sizes.append(s)

        self.dim_offsets.append(self.dim_offsets[-1] + len(tensor.size()))

    def end_document(self):
        self.doc_idx.append(len(self.sizes))

    def merge_file_(self, another_file_path):
        """
        Merge another file to current file

        Args:
            another_file_path (str): another file path to merge
        """
        index = IndexedDataset(another_file_path)
        assert index.dtype == self.dtype

        begin = self.data_offsets[-1]
        for offset in index.data_offsets[1:]:
            self.data_offsets.append(begin + offset)

        self.sizes.extend(index.sizes)
        begin = self.dim_offsets[-1]
        for dim_offset in index.dim_offsets[1:]:
            self.dim_offsets.append(begin + dim_offset)

        with open(PathUtils.data_path(another_file_path), "rb") as f:
            while True:
                data = f.read(1024)
                if data:
                    self.out_file.write(data)
                else:
                    break

    def finalize(self, index_file_path: str):
        """
        Write binary file

        Args:
            index_file_path (str): index file path
        """
        self.out_file.close()
        # 1. Create binary data
        index = open(index_file_path, "wb")
        magic = b"TNTIDX\x00\x00"
        version = struct.pack("<Q", 1)
        code_and_size = struct.pack(
            "<QQ",
            NumpyUtils.cody_by_dtypes[self.dtype],
            self.element_size,
        )
        offsets = struct.pack(
            "<QQ",
            len(self.data_offsets) - 1,
            len(self.sizes),
        )
        document_count = struct.pack(
            "<Q",
            len(self.doc_idx),
        )

        # 2. Write binary data to index file
        index.write(magic)
        index.write(version)
        index.write(code_and_size)
        index.write(offsets)
        index.write(document_count)

        # 3. Write array data to index file
        NumpyUtils.write_file_from_array(index, self.dim_offsets)
        NumpyUtils.write_file_from_array(index, self.data_offsets)
        NumpyUtils.write_file_from_array(index, self.sizes)
        NumpyUtils.write_file_from_array(index, self.doc_idx)
        index.close()

class IndexedDataset(Dataset):
    """
    Copy of ``IndexedDataset`` in ``fairseq``.

    Args:
        path (str): dataset path

    Attributes:
        _HDR_MAGIC: a key that determines the format of the dataset implementation (first 8 bytes of binary file)
        path: dataset path
        data_file: data from binarized file (path + '.bin')
        element_size: size of each element of sample's dtype
        _len: total length of data samples
        _sizes: total length of data samples
        sizes: the number of tokens of each sample
        doc_count: total number of documents
        dim_offsets: tensor dimension offsets
        data_offsets: data offsets
        doc_idx: array of document index

    References:
        https://github.com/pytorch/fairseq/blob/master/fairseq/data/indexed_dataset.py
    """

    def __init__(self, path: str):
        self._HDR_MAGIC = b"TNTIDX\x00\x00"
        self.path = path
        self.data_file = None
        self.element_size = None
        self.dtype = None
        self._len = None
        self._sizes = None
        self.sizes = None
        self.doc_count = None
        self.dim_offsets = None
        self.data_offsets = None
        self.doc_idx = None
        self.read_index(path)
        # read index file from path

    def read_index(self, path: str):
        """
        Read indexed dataset information from given file path

        Args:
            path (str): dataset path

        Notes:
            binary index file (.idx) structure:
            {
                magic (8: unsigned long long),
                version (8: unsigned long long),

                struct (16){
                    dtype_code (8: unsigned long long)
                    element_size (8: unsigned long long)
                },
                struct (16){
                    _len (8: unsigned long long)
                    sizes (8: unsigned long long)
                },
                struct (8){
                    doc_count (8: unsigned long long)
                },
                ...
            }

        References:
            https://docs.python.org/3/library/struct.html
            '<': little endian
            'Q': unsigned long long
        """

        with open(PathUtils.idx_path(path), "rb") as idx_file:
            # 1. Read first 8 bytes to detect format of dataset implementation
            # ex) ``magic``= b"TNTIDX\x00\x00"
            magic = idx_file.read(8)
            assert magic == self._HDR_MAGIC, (
                "Index file doesn't match expected format. "
                "Please check your configuration file."
            )

            # 2. Read second 8 bytes to detect version of dataset.
            # ex) ``version``=b'\x01\x00\x00\x00\x00\x00\x00\x00'
            idx_file.read(8)

            # 3. Read struct that consists dtype code and element size.
            # ex) type of token is np.int32 => ``dtype_code``=4, ``element_size``=4
            dtype_code, self.element_size = struct.unpack("<QQ", idx_file.read(16))
            self.dtype = NumpyUtils.dtypes_by_code[dtype_code]

            # 4. Read struct that consists length and sizes.
            # Note that ``_size (int)`` is difference with ``sizes (np.array)``.
            # ex) the number of samples is 41088 => ``len``=41088 ``_sizes``=41088
            self._len, self._sizes = struct.unpack("<QQ", idx_file.read(16))

            # 5. Read struct that consists document count.
            # ex) doc_count:tuple=(41089,)
            self.doc_count = struct.unpack("<Q", idx_file.read(8))

            # 6. Create ``dim_offsets`` and ``data_offsets`` arrays from ``_len``
            # ex) the number of samples is 41088,
            #     dim_offsets=[0, 1, 2, 3, ... 41086, 41087, 41088]
            #     data_offsets=[0, 28, 71, 107, ... 1311924, 1311955, 1311983]
            self.dim_offsets = NumpyUtils.create_array_from_file(
                idx_file,
                self._len + 1,
            )
            self.data_offsets = NumpyUtils.create_array_from_file(
                idx_file,
                self._len + 1,
            )

            # 7. Create ``sizes`` array from ``_sizes``
            # ex) sizes=[28 tokens, 43 tokens, 36 tokens, ...]
            self.sizes = NumpyUtils.create_array_from_file(
                idx_file,
                self._sizes,
            )

            # 8. Create ``doc_idx`` array from ``doc_count``
            # ex) doc_idx=[0, 1, 2, 3, ... 41086, 41087, 41088]
            self.doc_idx = NumpyUtils.create_array_from_file(
                idx_file,
                self.doc_count,
            )

    def read_data(self, path: str) -> None:
        """
        Read data file (.bin) from path

        Args:
            path (str): data path
        """
        self.data_file = open(
            PathUtils.data_path(path),
            "rb",
            buffering=0,
            # buffering off
        )

    def check_index(self, idx: int) -> None:
        """
        Check given index is valid.

        Args:
            idx (int): index

        Raises:
            IndexError: raise error if ``idx`` smaller than 0 or bigger than the number of total data samples.
        """
        if idx < 0 or idx >= self._len:
            raise IndexError("index out of range")

    def __getitem__(self, idx: Union[int, slice]) -> np.ndarray:
        """
        Get item from datasets index.

        Notes:
            Process of loading data is performed when this method is first called.
            That's why this dataset implementation is called ``lazy``.

        Args:
            idx (Union[int, slice]): index or slice

        Returns:
            np.array: Data by index
        """
        if not self.data_file:
            # Lazy data loading.
            self.read_data(self.path)

        if isinstance(idx, int):
            # check index is valid
            self.check_index(idx)

            # compute tensor size
            tensor_size = self.sizes[self.dim_offsets[idx] : self.dim_offsets[idx + 1]]

            # create empty array to hold the data
            array = np.empty(tensor_size, dtype=self.dtype)

            # sets the file's current position at the offset.
            self.data_file.seek(self.data_offsets[idx] * self.element_size)

            # read data into array
            self.data_file.readinto(array)
            return array

        elif isinstance(idx, slice):
            start, stop, step = idx.indices(len(self))
            if step != 1:
                # slices must be contiguous.
                raise ValueError("Slices into indexed_dataset must be contiguous")

            # compute tensor size and total tensor size
            sizes = self.sizes[self.dim_offsets[start] : self.dim_offsets[stop]]
            total_size = sum(sizes)

            # create empty array to hold the data
            array = np.empty(total_size, dtype=self.dtype)

            # sets the file's current position at the offset.
            self.data_file.seek(self.data_offsets[start] * self.element_size)
            self.data_file.readinto(array)

            # split datasets by the number of tokens
            offsets = list(accumulate(sizes))
            sentences = np.split(array, offsets[:-1])
            return sentences

    def __len__(self) -> int:
        """
        Returns the number of total datasets

        Returns:
            int: the number of total datasets
        """
        return self._len

    def num_tokens(self, index: Union[int, slice]) -> Union[int, tuple]:
        """
        Number of tokens by given index

        Args:
            index (Union[int, slice]): indices of dataset

        Returns:
            Union[int, slice]: the numbers of tokens by given indices
        """
        return self.sizes[index]

    def size(self, index: Union[int, slice]) -> Union[int, tuple]:
        """
        Returns data size by given index

        Args:
            index (Union[int, slice]): indices of dataset

        Returns:
            Union[int, tuple]: sizes of data samples by given indices
        """
        return self.sizes[index]

    @staticmethod
    def exists(path: str) -> bool:
        """
        Is exist file path of not

        Args:
            path (str): file path

        Returns:
            bool: exist or not
        """
        return os.path.exists(PathUtils.idx_path(path)) and os.path.exists(
            PathUtils.data_path(path)
        )

    @property
    def supports_prefetch(self) -> bool:
        """
        Check indexed dataset supports cache prefetching

        Returns:
            bool: whether support prefetching or not
        """
        return False

    @staticmethod
    def builder(path: str) -> IndexedDatasetBuilder:
        """
        Get build object to build lazy indexed dataset

        Args:
            path (str): output file path

        Returns:
            IndexedDatasetBuilder: indexed dataset builder object
        """
        return IndexedDatasetBuilder(path)

ext: py

max_stars_repo_name: sooftware/oslo

max_issues_repo_name: sooftware/oslo

max_forks_repo_name: sooftware/oslo

reason: import numpy


text:
"""
Append data to an existing CTD Profile dataset
==============================================

Use the TAMOC ambient module to append data to a CTD Profile object that has
already been created as in the other examples in this ./bin director. This
file demonstrates working with the data from the R/V Brooks McCall at Station
BM 54 on May 30, 2010, stored in the file /Raw_Data/ctd_BM54.cnv.

In this example, we compute a typical nitrogen profile and append that data
to the data in the CTD dataset.

Notes
-----
Much of the input data in the script (e.g., columns to extract, column names,
lat and lon location data, date and time, etc.) is read by the user manually
from the header file of the CTD text file. These data are then hand-coded in
the script text. While it would be straightforward to automate this process
for a given format of CTD files, this step is left to the user to customize to
their own data sets.

Requires
--------
This script reads data from the text file::

    ../../tamoc/data/ctd_BM54.cnv

Returns
-------
This script generates an `ambient.Profile` object, whose netCDF file is
written to the file::

    ../../test/output/BM54.nc

"""
# S. Socolofsky, July 2013, Texas A&M University <socolofs@tamu.edu>.

from __future__ import (absolute_import, division, print_function,
                        unicode_literals)

from tamoc import ambient
from tamoc import seawater
from tamoc import dbm

from netCDF4 import date2num, num2date
from datetime import datetime

import numpy as np
import matplotlib.pyplot as plt
import os

def get_ctd_profile():
    """
    Load the ASCII CTD Data into an 'ambient.Profile' object.

    This function performs the steps in ./profile_from_ctd.py to read in the
    CTD data and create a Profile object.  This is the data set that will be
    used to demonstrate how to append data to a Profiile object.

    """
    # Get the path to the input file
    __location__ = os.path.realpath(os.path.join(os.getcwd(),
                                    os.path.dirname(__file__),
                                    '../../tamoc/data'))
    dat_file = os.path.join(__location__,'ctd_BM54.cnv')

    # Load in the data using numpy.loadtxt
    raw = np.loadtxt(dat_file, comments = '#', skiprows = 175,
                     usecols = (0, 1, 3, 8, 9, 10, 12))

    # Describe the organization of the data in raw.
    var_names = ['temperature', 'pressure', 'wetlab_fluorescence', 'z',
                 'salinity', 'density', 'oxygen']
    var_units = ['deg C', 'db', 'mg/m^3', 'm', 'psu', 'kg/m^3', 'mg/l']
    z_col = 3

    # Clean the profile to remove reversals in the depth coordinate
    data = ambient.extract_profile(raw, z_col, 50.0)

    # Convert the profile data to standard units in TAMOC
    profile, units = ambient.convert_units(data, var_units)

    # Create an empty netCDF4-classic dataset to store this CTD data
    __location__ = os.path.realpath(os.path.join(os.getcwd(),
                                    os.path.dirname(__file__),
                                    '../../test/output'))
    nc_file = os.path.join(__location__,'BM54.nc')
    summary = 'Dataset created by profile_from_ctd in the ./bin directory' \
              + ' of TAMOC'
    source = 'R/V Brooks McCall, station BM54'
    sea_name = 'Gulf of Mexico'
    p_lat = 28.0 + 43.945 / 60.0
    p_lon = 360 - (88.0 + 22.607 / 60.0)
    p_time = date2num(datetime(2010, 5, 30, 18, 22, 12),
                      units = 'seconds since 1970-01-01 00:00:00 0:00',
                      calendar = 'julian')
    nc = ambient.create_nc_db(nc_file, summary, source, sea_name, p_lat,
                              p_lon, p_time)

    # Insert the CTD data into the netCDF dataset
    comments = ['measured'] * len(var_names)
    nc = ambient.fill_nc_db(nc, profile, var_names, units, comments, z_col)

    # Create an ambient.Profile object for this dataset
    bm54 = ambient.Profile(nc, chem_names=['oxygen'])

    # Return the Profile object
    return bm54

if __name__ == '__main__':
    """
    Demonstrate how to add data to an existing Profile object

    """
    # Get the ambient.Profile object with the original CTD data
    profile = get_ctd_profile()

    # Compute a dissolved nitrogen profile...start with a model for air
    air = dbm.FluidMixture(['nitrogen', 'oxygen', 'argon', 'carbon_dioxide'])
    yk = np.array([0.78084, 0.20946, 0.009340, 0.00036])
    m = air.masses(yk)

    # Compute the solubility of nitrogen at the air-water interface, then
    # correct for seawater compressibility
    n2_conc = np.zeros(len(profile.z))
    for i in range(len(profile.z)):
        T, S, P = profile.get_values(profile.z[i], ['temperature', 'salinity',
                  'pressure'])
        Cs = air.solubility(m, T, 101325., S)[0,:] * \
             seawater.density(T, S, P) / seawater.density(T, S, 101325.)
        n2_conc[i] = Cs[0]

    # Add this computed nitrogen profile to the Profile dataset
    data = np.vstack((profile.z, n2_conc)).transpose()
    symbols = ['z', 'nitrogen']
    units = ['m', 'kg/m^3']
    comments = ['measured', 'computed from CTD data']
    profile.append(data, symbols, units, comments, 0)

    # Close the dataset
    profile.close_nc()

    # Plot the oxygen and nitrogren profiles to show that data have been
    # added to the Profile object
    z = np.linspace(profile.z_min, profile.z_max, 250)
    o2 = np.zeros(z.shape)
    n2 = np.zeros(z.shape)
    for i in range(len(z)):
        n2[i], o2[i] = profile.get_values(z[i], ['nitrogen', 'oxygen'])

    plt.figure()
    plt.clf()
    plt.show()

    ax1 = plt.subplot(121)
    ax1.plot(o2, z)
    ax1.set_xlabel('Oxygen (kg/m^3)')
    ax1.set_ylabel('Depth (m)')
    ax1.invert_yaxis()
    ax1.set_title('Measured data')

    ax2 = plt.subplot(122)
    ax2.plot(n2, z)
    ax2.set_xlabel('Nitrogen (kg/m^3)')
    ax2.set_ylabel('Depth (m)')
    ax2.invert_yaxis()
    ax2.set_title('Computed data')

    plt.draw()

max_stars_repo_name: ChrisBarker-NOAA/tamoc

max_issues_repo_name: ChrisBarker-NOAA/tamoc

max_forks_repo_name: ChrisBarker-NOAA/tamoc


text:
import socket
import cv2
import numpy as np
import threading
import time

import pickle

import my_detect

#PORT = 9001
PORT = 2233
IP = '' # all ips
POSSIBLE_CONNECTIONS = 1
CHUNK = 4096

with open('bboxes.dat', 'rb') as fin:
    bboxes = pickle.load(fin)

class Server:
    def __init__(self, ip, port):
        self.sock = socket.socket()
        self.sock.bind((ip, port))
        self.sock.listen(POSSIBLE_CONNECTIONS)
        print('Server Initialized')
        self.start_listener()

    def start_listener(self):
        # handles multiple connections and starts a thread for each of them
        try:
            connections = 0
            while True:
                print('Waiting for a connection...')
                conn, addr = self.sock.accept()
                connections += 1
                print('Connected!')
                t = threading.Thread(target=self.handle_connection, args=(connections, conn, addr), daemon=True)
                t.start()
        except TypeError as e:
            print('Exception:', e)

    def receive(self, conn, img_len, chunk):
        # wrapper function for conn.recv
        img_bytes = b''
        while len(img_bytes) < img_len:
            if img_len - len(img_bytes) < chunk:
                data = conn.recv(img_len - len(img_bytes))
            else:
                data = conn.recv(chunk)
            if not data:
                break
            img_bytes += data
            #print('in receive', len(img_bytes), read_by_now)
        return img_bytes

    def convert_bytes_to_cv2_image(self, img_bytes):#, size=(640, 480)):
        # img_bytes = np.array(bytearray(img_bytes))
        # img = img_bytes.reshape((size[1], size[0], 3))
        # img = img[::-1, :, :]
        # img_bytes = img_bytes[np.mod(np.arange(img_bytes.size),4)!=3]
        img_bytes = np.array(bytearray(img_bytes))
        print('img_bytes in convert_cv2', img_bytes, img_bytes.shape)
        img = cv2.imdecode(img_bytes, cv2.IMREAD_COLOR)
        # print('img in convert_cv2', img, img.shape)
        return img

    def handle_connection(self, num, conn, addr):
        # implements a protocol for a connection
        print('Connection', num, ':', 'established')

        # width = int.from_bytes(conn.recv(4), 'little')
        # height = int.from_bytes(conn.recv(4), 'little')
        # print('Connection', num, ':', 'width and height:', width, height)

        cont_to_detect = int.from_bytes(conn.recv(4), 'little')
        while cont_to_detect:
            # main cycle to receive photos
            t0 = time.time()
            img_len = int.from_bytes(conn.recv(4), 'little')
            print('Connection', num, ':', img_len, 'bytes to receive')

            img_bytes = self.receive(conn, img_len, CHUNK)
            print('Connection', num, ':', 'received data')

            img = self.convert_bytes_to_cv2_image(img_bytes)#, (width, height))
            print('Connection', num, ':', 'converted image')

            print('Connection', num, ':', 'started detection')
            t1 = time.time()
            try:
                bboxes = my_detect.get_bboxes(img)
                #print('!!!!!img', img)
            except Exception as e:
                print('Exception!', e)
                print('exiting')
                conn.close()
                return
            t2 = time.time()
            print('Connection', num, ':', 'detected successfully')

            valid_detections =  bboxes[3][0]

            boxes = bboxes[0][0][:valid_detections].flatten()
            scores = bboxes[1][0][:valid_detections]
            classes = bboxes[2][0][:valid_detections].astype(int)
            # valid_detections = np.int32(1)
            # boxes = np.array([0.1, 0.2, 0.3, 0.4])
            # scores = np.array([1.])
            # classes = np.array([0]).astype(int)

            # print('valid', valid_detections, type(valid_detections))
            conn.send(valid_detections.tobytes())
            conn.send(bytes(scores))
            conn.send(bytes(classes))
            conn.send(bytes(boxes))
            print('scores', scores)
            # print('scores', len(bytes(scores)))
            # print('scores', bytes(scores))
            print('classes', classes)
            # print('classes', len(bytes(classes)))
            # print('classes', bytes(classes))
            # print('boxes, not flatten', bboxes[0][0][:valid_detections])
            print('boxes', boxes)
            # print('boxes', len(bytes(boxes)))
            # print('boxes', bytes(boxes))

            t3 = time.time()
            print('Connection', num, ':', 't_frame:', t3-t0,  ', t_detect:', t2-t1)
            cont_to_detect = int.from_bytes(conn.recv(4), 'little')
            print('continue?', cont_to_detect)
        print('Connection', num, ':', 'stopped connection and thread')
        conn.close()

def thread_function():
    # creates a Server
    myserver = Server(IP, PORT)

def main():
    # starts a thread with thread function
    t = threading.Thread(target=thread_function, daemon=True)
    t.start()
    print('Server Started...!')
    while True:
        time.sleep(1)

if __name__ == '__main__':
    main()


text:
# -*- coding: utf-8 -*-
"""
Created on Mon Feb 19 17:08:34 2018

@author: Pavel

Unit tests for the functions contained within the randomLib.turbulence module
TODO: think about doing the random tests like 100* times so that it tests
the randomness truly properly
"""

import unittest

import numpy as np
import sys, os
#boop = "D:/old_files/aaaaa/Anglie/imperial/2017-2018/group_project/OcadoLobster/src"

dir_path = os.path.dirname(os.path.realpath(__file__))
parent = os.path.abspath(os.path.join(dir_path, os.pardir))
base_path = os.path.abspath(os.path.join(parent, os.pardir))

if not (base_path in sys.path):
    sys.path.append(base_path)

from ..RandomLib import turbulence as tb

class Testturbulence(unittest.TestCase):

    def test_generate_noise(self):
        """
        Checking that the returned array is of the right shape
        And contained only values between one and two
        """
        noise = tb.generate_noise(300)
        self.assertEqual((300,300), np.shape(noise))
        for line in noise:
            for value in line:
                self.assertTrue(value>=0 and value<=1)

    def test_smoothNoise(self):
        """
        Checking that the returned array is of the right shape
        And contained only values between one and two
        Also checks that the result is actually smoother
        by calculating the variance of the original and smoothened noise
        The variance should be smaller for the smoothened noise
        """
        noise = tb.generate_noise(300)
        sm_noise = tb.smoothNoise(noise,2)
        self.assertEqual((300,300), np.shape(sm_noise))
        for line in sm_noise:
            for value in line:
                self.assertTrue((value>=0 and value<=1))
        self.assertGreaterEqual(np.var(noise), np.var(sm_noise))

    def test_turbulence(self):
        """
        Unsure about what parameters to test with.
        N can be anything (we will use 300),
        D is the number of layers added, between 3 and 8
        initial size is usually between 1 and 4
        The result should be a N*N array of values between 0 and 1
        """

        turb = tb.turbulence(300,7,3)
        self.assertEqual((300,300), np.shape(turb))
        for line in turb:
            for value in line:
                #print(value)
                self.assertTrue((value>=0 and value<=1))

    def test_turbulence_rgb(self):
        """
        Simillar as above, we can only test the output shape
        and that all the values are normalised between 0 and 1
        """
        turb = tb.turbulence_rgb(300)
        self.assertEqual((300,300,3), np.shape(turb))

        for layer in turb:
            for line in layer:
                for value in line:
                    self.assertTrue(value>=0 and value<=1)

if __name__=='__main__':
    unittest.main()


text:
#! /usr/bin/env python
# -*- coding: utf-8 -*-

"""
@version: ??
@author: li
@file: model_predict.py
@time: 2018/7/9 下午8:33
"""
import os
import numpy as np
import tensorflow as tf
from gen_captcha import gen_captcha_text_and_image, number, alphabet, ALPHABET
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'  # 屏蔽log信息、INFO信息和WARNING信息

print('=========================获取验证码=============================')
text, _image = gen_captcha_text_and_image()  # 先检验生成验证码和文字测试模块是否完全
print("验证码图像channel: {}".format(_image.shape))  # (50, 200, 3)
# 图像大小
IMAGE_HEIGHT = 50
IMAGE_WIDTH = 200
MAX_CAPTCHA = len(text)
# MAX_CAPTCHA = 4
print("验证码文本最长字符数: %s" % MAX_CAPTCHA)  # 验证码最长4字符; 我全部固定为4,可以不固定. 如果验证码长度小于4，用'_'补齐
print('=========================验证码获取结束==========================')

# 把彩色图像转为灰度图像（色彩对识别验证码没有什么用）
def convert2gray(img):
    if len(img.shape) > 2:
        gray = np.mean(img, -1)
        # 上面的转法较快，正规转法如下
        # r, g, b = img[:,:,0], img[:,:,1], img[:,:,2]
        # gray = 0.2989 * r + 0.5870 * g + 0.1140 * b
        return gray
    else:
        return img

# 文本转向量
char_set = number + ['_']  # 如果验证码长度小于4, '_'用来补齐
CHAR_SET_LEN = len(char_set)
print('CHAR_SET_LEN: {}'.format(CHAR_SET_LEN))

def text2vec(text):
    # 总共52个字母加10个数字，还有一个下划线，总共63个字符
    text_len = len(text)
    if text_len > MAX_CAPTCHA:
        raise ValueError('验证码最长4个字符')
    # vector: (252,)
    vector = np.zeros(MAX_CAPTCHA * CHAR_SET_LEN)

    def char2pos(_char):
        """
        将字符转化成序号
        :param _char:
        :return:
        """
        if _char == '_':
            k = 10   # 只有数字的情况下，下划线排在第十一个
            return k
        # 字符编码
        # 如果c为数字
        k = ord(_char) - 48
        if k > 10 or k < 0:  # 如果c为非数字和下划线
            raise ValueError('No Map')
        print('k: %s' % k)
        return k
    # 遍历验证码的每一个字符
    for i, c in enumerate(text):
        print(i, c)
        idx = i * CHAR_SET_LEN + char2pos(c)
        print('idx: %s'% idx)
        vector[idx] = 1
    return vector

# 向量转回文本
def vec2text(vec):
    char_pos = vec.nonzero()[0]
    text = []
    for i, c in enumerate(char_pos):
        char_at_pos = i  # c/63
        char_idx = c % CHAR_SET_LEN
        if char_idx < 10:
            char_code = char_idx + ord('0')
        # elif char_idx < 36:
        #     char_code = char_idx - 10 + ord('A')
        # elif char_idx < 62:
        #     char_code = char_idx - 36 + ord('a')
        elif char_idx == 10:
            char_code = ord('_')
        else:
            raise ValueError('error')
        text.append(chr(char_code))
    return "".join(text)

# 向量（大小MAX_CAPTCHA*CHAR_SET_LEN）用0,1编码 每11个编码一个字符，这样数字有，字符也有

# vec = text2vec("352_")
# print(np.shape(vec))
# print(vec)
# text = vec2text(vec)
# print(text)  # 352_

def get_next_batch(batch_size=128):
    batch_x = np.zeros([batch_size, IMAGE_HEIGHT * IMAGE_WIDTH])
    batch_y = np.zeros([batch_size, MAX_CAPTCHA * CHAR_SET_LEN])

    def wrap_gen_captcha_text_and_image():
        while True:
            text, _image = gen_captcha_text_and_image()
            if _image.shape == (IMAGE_HEIGHT, IMAGE_WIDTH, 3):
                return text, _image

    for i in range(batch_size):
        text, _image = wrap_gen_captcha_text_and_image()
        image = convert2gray(_image)

        batch_x[i, :] = image.flatten() / 255
        batch_y[i, :] = text2vec(text)

    return batch_x, batch_y

####################################################################
# 申请占位符 按照图片
X = tf.placeholder(tf.float32, [None, IMAGE_HEIGHT * IMAGE_WIDTH])
Y = tf.placeholder(tf.float32, [None, MAX_CAPTCHA * CHAR_SET_LEN])
keep_prob = tf.placeholder(tf.float32)  # dropout

# 定义CNN
def crack_captcha_cnn(w_alpha=0.01, b_alpha=0.1):
    x = tf.reshape(X, shape=[-1, IMAGE_HEIGHT, IMAGE_WIDTH, 1])  # (?, 50, 200, 1)
    print('shape_of_x: %s' % x.get_shape())

    # w_c1_alpha = np.sqrt(2.0/(IMAGE_HEIGHT*IMAGE_WIDTH)) #
    # w_c2_alpha = np.sqrt(2.0/(3*3*32))
    # w_c3_alpha = np.sqrt(2.0/(3*3*64))
    # w_d1_alpha = np.sqrt(2.0/(8*32*64))
    # out_alpha = np.sqrt(2.0/1024)

    # 3 conv layer # 3 个 转换层
    w_c1 = tf.Variable(w_alpha * tf.random_normal([3, 3, 1, 32]))
    b_c1 = tf.Variable(b_alpha * tf.random_normal([32]))
    conv1 = tf.nn.relu(tf.nn.bias_add(tf.nn.conv2d(x, w_c1, strides=[1, 1, 1, 1], padding='SAME'), b_c1))  # (?, 50, 200, 32)
    # print('shape_of_conv1: %s' % conv1.get_shape())
    conv1 = tf.nn.max_pool(conv1, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='SAME')
    conv1 = tf.nn.dropout(conv1, keep_prob)  # (?, 25, 100, 32)
    # print('shape_of_conv1_poll_drop: %s' % conv1.get_shape())

    w_c2 = tf.Variable(w_alpha * tf.random_normal([3, 3, 32, 64]))
    b_c2 = tf.Variable(b_alpha * tf.random_normal([64]))
    conv2 = tf.nn.relu(tf.nn.bias_add(tf.nn.conv2d(conv1, w_c2, strides=[1, 1, 1, 1], padding='SAME'), b_c2))  # (?, 25, 100, 64)
    # print('shape_of_conv2: %s' % conv2.get_shape())
    conv2 = tf.nn.max_pool(conv2, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='SAME')
    conv2 = tf.nn.dropout(conv2, keep_prob)  # (?, 13, 50, 64)
    # print('shape_of_conv2_poll_drop: %s' % conv2.get_shape())

    w_c3 = tf.Variable(w_alpha * tf.random_normal([3, 3, 64, 64]))
    b_c3 = tf.Variable(b_alpha * tf.random_normal([64]))
    conv3 = tf.nn.relu(tf.nn.bias_add(tf.nn.conv2d(conv2, w_c3, strides=[1, 1, 1, 1], padding='SAME'), b_c3))  # (?, 13, 50, 64)
    # print('shape_of_conv3: %s' % conv3.get_shape())
    conv3 = tf.nn.max_pool(conv3, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='SAME')
    conv3 = tf.nn.dropout(conv3, keep_prob)  # (?, 7, 25, 64)
    # print('shape_of_conv3_poll_drop: %s' % conv3.get_shape())

    # Fully connected layer  # 最后连接层
    w_d = tf.Variable(w_alpha * tf.random_normal([7 * 25 * 64, 1024]))
    b_d = tf.Variable(b_alpha * tf.random_normal([1024]))
    dense = tf.reshape(conv3, [-1, w_d.get_shape().as_list()[0]])  # (?, 11200)
    # print('shape_of_dense: %s' % dense.get_shape())
    dense = tf.nn.relu(tf.add(tf.matmul(dense, w_d), b_d))
    dense = tf.nn.dropout(dense, keep_prob)
    # print('shape_of_dense_poll_drop: %s' % dense.get_shape())

    # 输出层
    w_out = tf.Variable(w_alpha * tf.random_normal([1024, MAX_CAPTCHA * CHAR_SET_LEN]))
    b_out = tf.Variable(b_alpha * tf.random_normal([MAX_CAPTCHA * CHAR_SET_LEN]))
    out = tf.add(tf.matmul(dense, w_out), b_out) # (?, 44)
    # print('shape_of_out: %s' % out.get_shape())

    # out = tf.nn.softmax(out)
    return out

def crack_captcha(captcha_image):
    output = crack_captcha_cnn()

    saver = tf.train.Saver()
    with tf.Session() as sess:
        saver.restore(sess, tf.train.latest_checkpoint('./crack_capcha4/'))

        predict = tf.argmax(tf.reshape(output, [-1, MAX_CAPTCHA, CHAR_SET_LEN]), 2)
        text_list = sess.run(predict, feed_dict={X: [captcha_image], keep_prob: 1})

        text = text_list[0].tolist()
        vector = np.zeros(MAX_CAPTCHA * CHAR_SET_LEN)
        i = 0
        for n in text:
            vector[i * CHAR_SET_LEN + n] = 1
            i += 1
        return vec2text(vector)

if __name__ == '__main__':
    text, image = gen_captcha_text_and_image()
    image = convert2gray(image)  # 生成一张新图
    image = image.flatten() / 255  # 将图片一维化
    predict_text = crack_captcha(image)  # 导入模型识别
    print("正确: {}  预测: {}".format(text, predict_text))

max_stars_repo_name: STHSF/DeepLearning

max_issues_repo_name: STHSF/DeepLearning

max_forks_repo_name: STHSF/DeepLearning


text:
from matmul import readm, matmul

#1. read matrix A from 'A.csv'
A = readm('A.csv')

#2. read matrix A from 'A.csv
A = readm('b.csv')

#3. find the result of C = A x b
def matmul(A,b):
    m, n = len(A), len(b[0])
    J = len( A[0] ) # A-mxJ #b -Jxn
    if len(A[0]) == len(b):
        #C = [ [0]*n for i in range(n) ]
        C = [[0]*n]*m
       # A[0][0]*b[0][0] + A[0][1]*b[1][0] + A[0][2]*b[2][0]
        for r in range(m):
            for c in range(n):
                C[r][c] = sum([float(A[c][j])*float(b[j][c]) for j in range(J)])
        return C
    return [] #  ไม่สามารถดูได้
#4. print C
print(matmul(A,b))
C = matmul(A,b)
for row in C :
    print(row)
print(".....")

import numpy as np
import csv

#อ่านไฟล์ & เขียนไฟล์
A = readm('A.csv')
f = open('A.csv','r')# w,b
A = []
for line in f.readline():
    A.append( [float(x) for x in line.strip().split(',')] )
print(A)
f.close()

def csv2mat(filename):
    return np.array(list(csv.reader(open(filename,"rt"),delimiter=",")))
A = csv2mat('A.csv')
b = csv2mat('b.csv')
print(A)
print(b)

#3. find the result of C = A x b
def matmul(A,b):
    m, n = len(A), len(b[0])
    J = len( A[0] ) # A-mxJ #b -Jxn
    if len(A[0]) == len(b):
        #C = [ [0]*n for i in range(n) ]
        C = [[0]*n]*m
       # A[0][0]*b[0][0] + A[0][1]*b[1][0] + A[0][2]*b[2][0]
        for r in range(m):
            for c in range(n):
                C[r][c] = sum([float(A[c][j])*float(b[j][c]) for j in range(J)])
        return C
    return [] #  ไม่สามารถดูได้
#4. print C
print(matmul(A,b))
C = matmul(A,b)
for row in C :
    print(row)
print(".....")
D = np.dot(np.array(A) )


text:
import numpy as np
import sys
import pressiotools.linalg as ptla
from pressiotools.romoperators.galerkinProjector import computeGalerkinProjector

np.set_printoptions(linewidth=140,precision=14)
tol = 1e-14

#-------------------------
def runDof1Coll(phi):
  smGIDs = [2,4,5]
  projector = computeGalerkinProjector(stateBasis=phi,
                                       dofsPerMeshNode=1,
                                       sampleMeshIndices=smGIDs)
  print(projector)
  gold = phi.data()[smGIDs,:]
  assert( projector.shape == gold.shape)
  assert( np.all(projector == gold))

#-------------------------
def runDof2Coll(phi):
  smGIDs = [2,4,5]
  projector = computeGalerkinProjector(stateBasis=phi,
                                       dofsPerMeshNode=2,
                                       sampleMeshIndices=smGIDs)
  print(projector)
  gold = phi.data()[[4,5,8,9,10,11],:]
  assert( projector.shape == gold.shape)
  assert( np.all(projector == gold))

#-------------------------
def runDof1Gappy(phi, psi):
  smGIDs = [2,4,5,6,7,8,16,17]
  projector = computeGalerkinProjector(stateBasis=phi,
                                       residualBasis=psi,
                                       dofsPerMeshNode=1,
                                       sampleMeshIndices=smGIDs)
  print(projector)

  # since dofsPerNode == 1, the rows of psi are same as smGIDs
  Zpsi = psi.data()[smGIDs,:]
  import scipy.linalg as scipyla
  ZpsiPi = scipyla.pinv(Zpsi)
  print(ZpsiPi.shape)

  B = np.transpose(psi.data()).dot(phi.data())
  print(B.shape)

  gold = np.transpose(ZpsiPi).dot(B)
  assert( projector.shape == gold.shape)
  assert( np.all(projector == gold))

#-------------------------
def runDof2Gappy(phi, psi):
  smGIDs = [2,5,10]
  projector = computeGalerkinProjector(stateBasis=phi,
                                       residualBasis=psi,
                                       dofsPerMeshNode=2,
                                       sampleMeshIndices=smGIDs)
  print(projector)
  # since dofsPerNode==2, the rows indices are NOT just the sampleMeshGids
  # we need to account that each mesh node has 2 dofs inside
  A = psi.data()[[4,5,10,11,20,21],:]
  import scipy.linalg as scipyla
  Api = scipyla.pinv(A)
  B = np.transpose(psi.data()).dot(phi.data())
  gold = np.transpose(Api).dot(B)
  print(gold)
  assert( projector.shape == gold.shape)
  assert( np.all(projector == gold))

### MAIN ###
if __name__ == '__main__':
  np.random.seed(312367)
  phi0 = np.asfortranarray(np.random.rand(24,4))
  print(phi0)
  phi = ptla.MultiVector(phi0)
  print("---------\n")

  runDof1Coll(phi)
  runDof2Coll(phi)

  np.random.seed(4451236)
  # note that we make psi with 6 cols
  psi0 = np.asfortranarray(np.random.rand(24,6))
  print(psi0)
  psi  = ptla.MultiVector(psi0)
  print("---------\n")

  runDof1Gappy(phi, psi)
  runDof2Gappy(phi, psi)

max_stars_repo_name: Pressio/pressio-hyperreduction

max_issues_repo_name: Pressio/pressio-hyperreduction

max_forks_repo_name: Pressio/pressio-tools

reason: import numpy,import scipy