text:
# -*- encoding: utf-8 -*-
"""
@Author     :   zYx.Tom
@Contact    :   526614962@qq.com
@site       :   https://zhuyuanxiang.github.io
---------------------------
@Software   :   PyCharm
@Project    :   deep-learning-with-python-notebooks
@File       :   ch0503_pretained_convnet_data_augmentation.py
@Version    :   v0.1
@Time       :   2019-11-21 16:36
@License    :   (C)Copyright 2018-2019, zYx.Tom
@Reference  :   《Python 深度学习，Francois Chollet》, Sec0503，P115
@Desc       :   深度学习用于计算机视觉，使用预训练的卷积神经网络——基于数据增强的特征提取
"""
import os
import sys

import matplotlib.pyplot as plt
import numpy as np  # pip install numpy<1.17，小于1.17就不会报错
import winsound
from keras import activations
from keras import layers
from keras import losses
from keras import metrics
from keras import models
from keras import optimizers
from keras.applications import VGG16
from keras.preprocessing.image import ImageDataGenerator

# 屏蔽警告：Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX2 FMA
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'
# 设置数据显示的精确度为小数点后3位
np.set_printoptions(precision = 3, suppress = True, threshold = np.inf, linewidth = 200)
# to make this notebook's output stable across runs
seed = 42
np.random.seed(seed)
# Python ≥3.5 is required
assert sys.version_info >= (3, 5)
# numpy 1.16.4 is required
assert np.__version__ in ["1.16.5", "1.16.4"]

conv_base = VGG16(weights = 'imagenet', include_top = False, input_shape = (150, 150, 3))
vgg16_out_dims_product = 4 * 4 * 512

base_dir = "C:/Users/Administrator/PycharmProjects/Data/small_datasets"
train_dir = os.path.join(base_dir, 'train')
validation_dir = os.path.join(base_dir, 'validation')
# train_dir = os.path.join(base_dir, 'tmp_train')
# validation_dir = os.path.join(base_dir, 'tmp_val')
# test_dir = os.path.join(base_dir, 'test')

epochs = 30
batch_size = 20
steps_per_epoch = 100
print("导入数据")
train_data_gen = ImageDataGenerator(rescale = 1. / 255, rotation_range = 40, width_shift_range = 0.2,
                                    height_shift_range = 0.2,
                                    shear_range = 0.2, zoom_range = 0.2, horizontal_flip = True, fill_mode = 'nearest')
test_data_gen = ImageDataGenerator(rescale = 1. / 255)

train_generator = train_data_gen.flow_from_directory(
        train_dir, target_size = (150, 150), batch_size = batch_size, class_mode = 'binary'
)
validation_generator = test_data_gen.flow_from_directory(
        validation_dir, target_size = (150, 150), batch_size = batch_size, class_mode = 'binary'
)

print("构造模型")
model = models.Sequential()
conv_base.trainable = False
model.add(conv_base)
model.add(layers.Flatten())
model.add(layers.Dense(256, activation = activations.relu))
model.add(layers.Dense(1, activation = activations.sigmoid))

model.compile(optimizer = optimizers.rmsprop(lr = 2e-5), loss = losses.binary_crossentropy,
              metrics = [metrics.binary_accuracy])
print("训练模型")
history = model.fit_generator(train_generator, epochs = epochs, steps_per_epoch = steps_per_epoch,
                              validation_data = validation_generator, validation_steps = 50)

binary_accuracy = history.history['binary_accuracy']
val_binary_accuracy = history.history['val_binary_accuracy']
loss = history.history['loss']
val_loss = history.history['val_loss']
epochs_range = range(1, epochs + 1)

plt.plot(epochs_range, binary_accuracy, 'bo', label = "训练集的精确度")
plt.plot(epochs_range, val_binary_accuracy, 'r-', label = "验证集的精确度")
plt.title("图5-17：带数据增强的特征提取的训练精确度和验证精确度")
plt.legend()

plt.figure()
plt.plot(epochs_range, loss, 'bo', label = "训练集的损失")
plt.plot(epochs_range, val_loss, 'r-', label = "验证集的损失")
plt.title("图5-16：简单特征提取的训练损失和验证损失")
plt.legend()

# 运行结束的提醒
winsound.Beep(600, 500)
if len(plt.get_fignums()) != 0:
    plt.show()
pass


text:
"""
https://www.jianshu.com/p/a796ca559409
"""
import os
import jieba.analyse as analyse
import jieba
import pandas as pd
from gensim import corpora, models, similarities
import gensim
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.font_manager as fm
#%matplotlib inline

num_topics  = 8
num_show_term = 2500

sentences = []

def mapTrim(f):
    return [t.strip() for t in f if t.strip()]

#tokeyFile = os.path.join("..", "..", "token", "search_big_keyword_tokey.txt")
titleFile = os.path.join("..", "..", "token", "search_big_keyword_title.txt")
tokenFile = os.path.join("..", "..", "token", "search_big_keyword_token.txt")
#titleFile = os.path.join("..", "..", "token", "search_big_title.txt")
#tokenFile = os.path.join("..", "..", "token", "search_big_token.txt")
#tokey = mapTrim(open(tokeyFile, encoding="UTF-8").read().split('\n'))
title = mapTrim(open(titleFile, encoding="UTF-8").read().split('\n'))
token = mapTrim(open(tokenFile, encoding="UTF-8").read().split('\n'))

for t, d in zip(title, token):
    sentences.append((t + ' ' + d).split())

dictionary = corpora.Dictionary(sentences)
corpus = [dictionary.doc2bow(sentence) for sentence in sentences]
lda = gensim.models.ldamodel.LdaModel(corpus=corpus, id2word=dictionary, num_topics=num_topics)

keywords = set()
for topic, words in lda.show_topics(num_topics=10, num_words=100, log=False, formatted=False):
    for word, prob in words:
        keywords.add(word)

with open('key_lda.txt', 'w', newline='', encoding='UTF-8') as keyfile:
    for word in keywords:
        keyfile.write(word + '\n')

showGraph = False
if showGraph:
    fontPath = r'NotoSansMonoCJKtc-Bold.otf'
    font30 = fm.FontProperties(fname=fontPath, size=30)

    for i, k in enumerate(range(num_topics)):
        ax = plt.subplot(2, 5, i+1)
        item_dis_all = lda.get_topic_terms(topicid=k)
        item_dis = np.array(item_dis_all[:num_show_term])
        ax.plot(range(num_show_term), item_dis[:, 1], 'b*')
        item_word_id = item_dis[:, 0].astype(np.int)
        word = [dictionary.id2token[i] for i in item_word_id]
        ax.set_ylabel('probability', fontproperties=font30, fontsize=8)
        for j in range(num_show_term):
            ax.text(j, item_dis[j, 1], word[j], bbox=dict(facecolor='green',alpha=0.1), fontproperties=font30, fontsize=8)
    plt.suptitle('{} topic and {} word probability'.format(num_topics, num_show_term), fontsize=12, fontproperties=font30)
    plt.show()

ext: py

max_stars_repo_name: BlackBoxOperator/GotchaTheNames

max_issues_repo_name: BlackBoxOperator/GotchaTheNames

max_forks_repo_name: BlackBoxOperator/GotchaTheNames

reason: import numpy


text:
from bs4 import BeautifulSoup
import urllib2
import numpy as np

# wiki = "http://en.wikipedia.org/wiki/Opinion_polling_for_the_Spanish_general_election,_2015"
# header = {'User-Agent': 'Mozilla/5.0'} #Needed to prevent 403 error on Wikipedia
# req = urllib2.Request(wiki,headers=header)
# page = urllib2.urlopen(req)
# result = page.read()
# f = open("page.html", "w")
# f.write(result)
# f.close()

f = open("page.html", "r")
page = f.read()
f.close()

soup = BeautifulSoup(page)
table = soup.find_all("table")[0]

rows = table.findAll('tr')
dat = []
loop = 0
for tr in rows[2:]:
	cols = tr.findAll('td')
	if (len(cols) == 14):
		temp = []
		for i in range(14):
			temp.append(cols[i].string)

	if (len(cols) == 13):
		temp = []
		for i in range(13):
			if (i == 11):
				temp.append('')
			temp.append(cols[i].string)

	if (len(cols) == 12):
		temp = []
		for i in range(12):
			if (i == 10 | i == 11):
				temp.append('')
			temp.append(cols[i].string)

	dat.append(temp)


text:
"""Script to convert kojak result files to be used with xiRT"""
# description = \
# """
# kojak2xiFDR - a convenience script to generate a minimal input for using xiRT from xiFDR data.
# Use python xiFDR2xiRT.py --help to see more information.
# """
# parser = argparse.ArgumentParser(description=description)
# parser.add_argument("-i", "--in-xifdr",
#                     help="Input CSMs with assigned false discovery rate estimates.",
#                     required=True, action="store", dest="in_peptides")
#
# parser.add_argument("-o", "--out-xirt", help="Output CSM format for usage in xiRT.",
#                     required=True, action="store", dest="out_peptides")
#
# minimal_columns = ["rp", "Fasta1", "Fasta2", "score", "Peptide1", "Peptide2", "FDR",
#                    "Run", "fdrGroup", "PSMID", "isTT", "isTD", "isDD",
#                    "LinkPos1", "LinkPos2", "PrecoursorCharge", "Protein1",
#                    "Protein2", "Crosslinker", "Decoy1", "Decoy2", "PepPos1", "PepPos2"]

# parse arguments
#args = parser.parse_args(sys.argv[1:])
#%%
import argparse
import glob
import os
import re

import numpy as np
import pandas as pd

directory = r"C:\Users\Sven.Giese\Desktop\ECCP\kojak_sample_files"
directory = r""
files = glob.glob(os.path.join(directory, "*.kojak.txt"))
dfs = []
for file in files:
    print(file)
    df_temp = pd.read_csv(file, skiprows=1, sep="\t")
    df_temp["Run"] = os.path.basename(file)
    df_temp = df_temp[(df_temp["Obs Mass"] != 0) &
                      (df_temp["Peptide #2"] != "-")]
    dfs.append(df_temp)
df_kojak = pd.concat(dfs)
df_kojak.rename(columns={"Charge": "PrecursorCharge",
                         "Score": "score",
                         "Ret Time": "rp",
                         "Linked AA #1": "LinkPos1",
                         "Linked AA #2": "LinkPos2",
                         "Protein #1": "Description1",
                         "Protein #2": "Description2",
                         "Peptide #1": "Peptide1",
                         "Peptide #2": "Peptide2",
                         "Protein #1 Site": "PepPos1",
                         "Protein #2 Site": "PepPos2"}, inplace=True)
print("Finished reading. Start processint dataframe.")
df_kojak["PSMID"] = np.arange(1, len(df_kojak) + 1)
df_kojak["Protein1"] = df_kojak["Description1"]
df_kojak["Protein2"] = df_kojak["Description2"]
df_kojak["Decoy1"] = df_kojak["Description1"].str.startswith("DEC")
df_kojak["Decoy2"] = df_kojak["Description2"].str.startswith("DEC")
df_kojak["isTT"] = df_kojak[["Decoy1", "Decoy2"]].sum(axis=1)
df_kojak["score"] = df_kojak["score"].astype(float) * 1.0
rec = re.compile(r"((?:DEC_sp|sp)\|\w+\|)")
df_kojak["Protein1"] = [";".join(re.findall(rec, i)) for i in df_kojak["Description1"]]
df_kojak["Protein2"] = [";".join(re.findall(rec, i)) for i in df_kojak["Description2"]]
#  transform run name to fraction
frac_annot = lambda name, prefix: int(re.search(r"{}(\d+)".format(prefix), name).groups()[0])

df_kojak["scx"] = df_kojak["Run"].apply(frac_annot, args=("SCX",))
df_kojak["hsax"] = df_kojak["Run"].apply(frac_annot, args=("hSAX",))
df_kojak.to_csv(r"all_kojak.csv", sep=",")

max_stars_repo_name: ludsinn/xiRT

max_issues_repo_name: ludsinn/xiRT

max_forks_repo_name: ludsinn/xiRT


text:
import uvicorn
from fastapi import FastAPI
from variables import WeatherVariables
import numpy
import pickle
import pandas as pd
import onnxruntime as rt

# Create app object
app = FastAPI()

# Load model scalar
pickle_in = open("artifacts/model-scaler.pkl", "rb")
scaler = pickle.load(pickle_in)

# Load the model
sess = rt.InferenceSession("artifacts/svc.onnx")
input_name = sess.get_inputs()[0].name
label_name = sess.get_outputs()[0].name

# API Endpoints
@app.get('/')
def index():
    return {'Hello': 'Welcome to weather prediction service, access the api docs and test the API at http://0.0.0.0/docs.'}

@app.post('/predict')
def predict_weather(data: WeatherVariables):
    data = data.dict()

    # fetch input data using data varaibles
    temp_c = data['temp_c']
    humidity = data['humidity']
    wind_speed_kmph = data['wind_speed_kmph']
    wind_bearing_degree = data['wind_bearing_degree']
    visibility_km = data['visibility_km']
    pressure_millibars = data['pressure_millibars']
    current_weather_condition = data['current_weather_condition']

    data_to_pred = numpy.array([[temp_c, humidity, wind_speed_kmph, wind_bearing_degree,
                                 visibility_km, pressure_millibars, current_weather_condition]])

    # Scale input data
    data_to_pred = scaler.fit_transform(data_to_pred.reshape(1, 7))

    # Model inference
    prediction = sess.run(
        [label_name], {input_name: data_to_pred.astype(numpy.float32)})[0]

    if(prediction[0] > 0.5):
        prediction = "Rain"
    else:
        prediction = "No_Rain"
    return {
        'prediction': prediction
    }


text:
from contextlib import contextmanager
from random import sample
from typing import Dict, Tuple, ByteString
import logging

import pyaudio
import numpy as np

logger = logging.getLogger(__name__)

DEFAULT_SAMPLE_WIDTH = 2  # pyaudio.paInt16  # conversion format for PyAudio stream
DEFAULT_CHANNELS = 1  # microphone audio channels
DEFAULT_SAMPLE_RATE = 48_000  # num audio sample per sec
DEFAULT_SAMPLE_RATE = 44_100
DEFAULT_CHUNK_SIZE = 8192  # number of samples to take per read
DEFAULT_CHUNK_SIZE = 1024

# SAMPLE_LENGTH = int(CHUNK_SIZE * 1_000 / SAMPLE_RATE)  # length of each sample in ms
DEFAULT_SAMPLE_PER_MS = DEFAULT_SAMPLE_RATE / 1000  # sample per ms
DEFAULT_SAMPLE_LENGTH = int(
    DEFAULT_CHUNK_SIZE / DEFAULT_SAMPLE_PER_MS
)  # chunk duration.

def _open_mic(sample_rate, channels, chunk_size, sample_width):
    """
    open_mic:
    creates a PyAudio object and initializes the mic stream
    inputs: none
    ouputs: stream, PyAudio object
    """

    pa = pyaudio.PyAudio()
    stream = pa.open(
        format=pyaudio.get_format_from_width(sample_width),
        channels=channels,
        rate=sample_rate,
        input=True,
        frames_per_buffer=chunk_size,
    )
    return stream, pa

def _open_callback_mic(sample_rate, channels, sample_width, callback):
    """
    open_mic:
    creates a PyAudio object and initializes the mic stream
    inputs: none
    ouputs: stream, PyAudio object
    """

    pa = pyaudio.PyAudio()
    stream = pa.open(
        format=pyaudio.get_format_from_width(sample_width),
        channels=channels,
        rate=sample_rate,
        input=True,
        # frames_per_buffer=chunk_size,
        stream_callback=callback,
    )
    return stream, pa

def get_data(stream: pyaudio.Stream, chunk_size: int = 0) -> np.ndarray:
    """
    get_data:
    reads from the audio stream for a constant length of time, converts it to data
    inputs: stream, PyAudio object
    outputs: int16 data array
    """
    num_frames = chunk_size if chunk_size else stream.get_read_available()
    input_data = stream.read(num_frames, exception_on_overflow=False)
    data = np.frombuffer(input_data, np.int16)
    return data

def get_sample(stream: pyaudio.Stream, chunk_size: int = 0) -> np.ndarray:
    """
    get_sample:
    gets the audio data from the microphone
    inputs: audio stream and PyAudio object
    outputs: int16 array"""

    data = get_data(stream, chunk_size)
    return data

class AudioMic:
    def __init__(self, open=False, sample_rate=None, channels=None, chunk_size=None):
        self.sample_rate = sample_rate if sample_rate else DEFAULT_SAMPLE_RATE
        self.channels = channels if channels else DEFAULT_CHANNELS
        self.chunk_size = chunk_size if chunk_size else DEFAULT_CHUNK_SIZE
        self.sample_width = DEFAULT_SAMPLE_WIDTH
        self.stream = None
        self.pa = None
        if open:
            self.open()

    def open(self):
        if self.stream is not None or self.pa is not None:
            raise RuntimeError("Opening an open mic.")
        stream, pa = _open_mic(
            sample_rate=self.sample_rate,
            channels=self.channels,
            chunk_size=self.chunk_size,
            sample_width=self.sample_width,
        )
        self.stream = stream
        self.pa = pa

    def read(self):
        return get_sample(self.stream, self.chunk_size)

    def get_desireable_sample_interval_ms(self):
        sample_per_ms = self.sample_rate / 1000  # sample per ms
        sample_length = int(
            self.chunk_size / self.sample_width / sample_per_ms
        )  # chunk duration.
        return sample_length

    @property
    def desireable_sample_interval_ms(self):
        return self.get_desireable_sample_interval_ms()

    def close(self):
        if not self.stream:
            raise RuntimeError("Closing an unopen mic.")
        if not self.pa:
            raise RuntimeError("Closing an unopen mic.")
        self.stream.stop_stream()
        self.stream.close()
        self.stream = None
        self.pa.terminate()
        self.pa = None

@contextmanager
def open_mic(chunk_size=None):
    try:
        mic = AudioMic(chunk_size=chunk_size)
        mic.open()
        yield mic
    except Exception as e:
        logger.exception("Something went wrong creating an AudioMic object.")
        raise
    finally:
        mic.close()

class CallbackAudioMic:
    def __init__(self, open=False, sample_rate=None, channels=None):
        self.sample_rate = sample_rate if sample_rate else DEFAULT_SAMPLE_RATE
        self.channels = channels if channels else DEFAULT_CHANNELS
        self.sample_width = DEFAULT_SAMPLE_WIDTH
        self.stream = None
        self.pa = None
        if open:
            self.open()

    def callback(
        self,
        input_data: ByteString,
        frame_count: int,
        time_info: dict,
        status_flag: int,
    ) -> Tuple[ByteString, int]:
        """
        callback(
            in_data,      # recorded data if input=True; else None
            frame_count,  # number of frames
            time_info,    # dictionary
            status_flags) # PaCallbackFlags

        Returns `(out_data, flag)`

        `time_info` is a dictionary with the following keys: `input_buffer_adc_time`, `current_time`, and `output_buffer_dac_time`; see the PortAudio documentation for their meanings.
        `status_flags` is one of PortAutio Callback Flag.

        `out_data` is a byte array whose length should be the `(frame_count * channels * bytes-per-channel)` if `output=True` or `None` if `output=False`.
        flag must be either paContinue, paComplete or paAbort (one of PortAudio Callback Return Code).
        When `output=True` and `out_data` does not contain at least `frame_count` frames, `paComplete` is assumed for flag.
        """
        # logger.debug("CallbackAudioMic callback()")
        self.buffer += input_data
        out_data = None  # null coz this is an input stream.
        # logger.debug("Mic flag: {}".format(status_flag))
        if status_flag in [
            pyaudio.paInputUnderflow,
            pyaudio.paInputOverflow,
        ]:
            logger.warning("Some sort of audio mic input overflow/underflow has occurred.")
        return (out_data, pyaudio.paContinue)

    def open(self):
        if self.stream is not None or self.pa is not None:
            raise RuntimeError("Opening an open mic.")
        stream, pa = _open_callback_mic(
            sample_rate=self.sample_rate,
            channels=self.channels,
            sample_width=self.sample_width,
            callback=self.callback,
        )
        self.stream = stream
        self.pa = pa
        self.stream.start_stream()
        self.buffer = b""

    def read(self):
        buf = np.frombuffer(self.buffer, dtype=np.int16)
        self.buffer = b""
        return buf

    def get_desireable_sample_interval_ms(self):
        sample_per_ms = self.sample_rate / 1000  # sample per ms
        sample_length = int(
            self.chunk_size / self.sample_width / sample_per_ms
        )  # chunk duration.
        return sample_length

    @property
    def desireable_sample_interval_ms(self):
        return self.get_desireable_sample_interval_ms()

    def close(self):
        if not self.stream:
            raise RuntimeError("Closing an unopen mic.")
        if not self.pa:
            raise RuntimeError("Closing an unopen mic.")
        self.stream.stop_stream()
        self.stream.close()
        self.stream = None
        self.pa.terminate()
        self.pa = None

max_stars_repo_name: yxbh/gromtector

max_issues_repo_name: yxbh/gromtector

max_forks_repo_name: yxbh/gromtector


text:
"""
Results class that summarizes the results of scCODA and calculates test statistics.
This class extends the ´´InferenceData`` class in the ``arviz`` package and can use all plotting and diacgnostic
functionalities of it.

Additionally, this class can produce nicely readable outputs for scCODA.

:authors: Johannes Ostner
"""
import numpy as np
import arviz as az
import pandas as pd
import pickle as pkl

from typing import Optional, Tuple, Collection, Union, List

class CAResultConverter(az.data.io_dict.DictConverter):
    """
    Helper class for result conversion into arviz's format
    """

    def to_result_data(self, sampling_stats, model_specs):

        post = self.posterior_to_xarray()
        ss = self.sample_stats_to_xarray()
        postp = self.posterior_predictive_to_xarray()
        prior = self.prior_to_xarray()
        ssp = self.sample_stats_prior_to_xarray()
        prip = self.prior_predictive_to_xarray()
        obs = self.observed_data_to_xarray()

        return CAResult(
            sampling_stats, model_specs,
            **{
                "posterior": post,
                "sample_stats": ss,
                "posterior_predictive": postp,
                "prior": prior,
                "sample_stats_prior": ssp,
                "prior_predictive": prip,
                "observed_data": obs,
            }
        )

class CAResult(az.InferenceData):
    """
    Result class for scCODA, extends the arviz framework for inference data.

    The CAResult class is an extension of az.InferenceData, that adds some information about the compositional model
    and is able to print humanly readable results.
    It supports all functionality from az.InferenceData.
    """

    def __init__(
            self,
            sampling_stats: dict,
            model_specs: dict,
            **kwargs
    ):
        """
        Gathers sampling information from a compositional model and converts it to a ``az.InferenceData`` object.
        The following attributes are added during class initialization:

        ``self.sampling_stats``: dict - see below
        ``self.model_specs``: dict - see below

        ``self.intercept_df``: Intercept dataframe from ``CAResult.summary_prepare``
        ``self.effect_df``: Effect dataframe from ``CAResult.summary_prepare``

        Parameters
        ----------
        sampling_stats
            Information and statistics about the MCMC sampling procedure.
            Default keys:
            - "chain_length": Length of MCMC chain (with burnin samples)
            - "num_burnin": Number of burnin samples
            - "acc_rate": MCMC Acceptance rate
            - "duration": Duration of MCMC sampling

        model_specs
            All information and statistics about the model specifications.
            Default keys:
            - "formula": Formula string
            - "reference": int - identifier of reference cell type

            Added during class initialization:
            - "threshold_prob": Threshold for inclusion probability that separates significant from non-significant effects
        kwargs
            passed to az.InferenceData. This includes the MCMC chain states and statistics for eachs MCMC sample.
        """
        super(self.__class__, self).__init__(**kwargs)

        self.sampling_stats = sampling_stats
        self.model_specs = model_specs

        if "ind" in list(self.posterior.data_vars):
            self.is_sccoda = True
        else:
            self.is_sccoda = False

        intercept_df, effect_df = self.summary_prepare()

        self.intercept_df = intercept_df
        self.effect_df = effect_df

    def summary_prepare(
            self,
            est_fdr: float = 0.05,
            *args,
            **kwargs
    ) -> Tuple[pd.DataFrame, pd.DataFrame]:
        """
        Generates summary dataframes for intercepts and slopes.
        This function builds on and supports all functionalities from ``az.summary``.

        Parameters
        ----------
        est_fdr
            Desired FDR value
        args
            Passed to ``az.summary``
        kwargs
            Passed to ``az.summary``

        Returns
        -------
        Intercept and effect DataFrames

        intercept_df -- pandas df
            Summary of intercept parameters. Contains one row per cell type.

            Columns:
            - Final Parameter: Final intercept model parameter
            - HDI X%: Upper and lower boundaries of confidence interval (width specified via hdi_prob=)
            - SD: Standard deviation of MCMC samples
            - Expected sample: Expected cell counts for a sample with no present covariates. See the tutorial for more explanation

        effect_df -- pandas df
            Summary of effect (slope) parameters. Contains one row per covariate/cell type combination.

            Columns:
            - Final Parameter: Final effect model parameter. If this parameter is 0, the effect is not significant, else it is.
            - HDI X%: Upper and lower boundaries of confidence interval (width specified via hdi_prob=)
            - SD: Standard deviation of MCMC samples
            - Expected sample: Expected cell counts for a sample with only the current covariate set to 1. See the tutorial for more explanation
            - log2-fold change: Log2-fold change between expected cell counts with no covariates and with only the current covariate
            - Inclusion probability: Share of MCMC samples, for which this effect was not set to 0 by the spike-and-slab prior.
        """

        # initialize summary df from arviz and separate into intercepts and effects.
        summ = az.summary(self, *args, **kwargs, kind="stats", var_names=["alpha", "beta"])
        effect_df = summ.loc[summ.index.str.match("|".join(["beta"]))].copy()
        intercept_df = summ.loc[summ.index.str.match("|".join(["alpha"]))].copy()

        # Build neat index
        cell_types = self.posterior.coords["cell_type"].values
        covariates = self.posterior.coords["covariate"].values

        intercept_df.index = pd.Index(cell_types, name="Cell Type")
        effect_df.index = pd.MultiIndex.from_product([covariates, cell_types],
                                                     names=["Covariate", "Cell Type"])

        # Calculation of columns that are not from az.summary
        intercept_df = self.complete_alpha_df(intercept_df)
        effect_df = self.complete_beta_df(intercept_df, effect_df, est_fdr)

        # Give nice column names, remove unnecessary columns
        hdis = intercept_df.columns[intercept_df.columns.str.contains("hdi")]
        hdis_new = hdis.str.replace("hdi_", "HDI ")

        # Credible interval
        if self.is_sccoda is True:
            ind_post = self.posterior["ind"]

            b_raw_sel = self.posterior["b_raw"] * ind_post.where(ind_post >= 1e-3)

            res = az.convert_to_inference_data(b_raw_sel)

            summary_sel = az.summary(res, kind="stats", var_names=["x"], skipna=True, *args, **kwargs)

            ref_index = self.model_specs["reference"]
            n_conditions = len(self.posterior.coords["covariate"])
            n_cell_types = len(self.posterior.coords["cell_type"])

            def insert_row(idx, df, df_insert):
                return df.iloc[:idx, ].append(df_insert).append(df.iloc[idx:, ]).reset_index(drop=True)

            for i in range(n_conditions):
                summary_sel = insert_row((i*n_cell_types) + ref_index, summary_sel,
                                         pd.DataFrame.from_dict(data={"mean": [0], "sd": [0], hdis[0]: [0], hdis[1]: [0]}))

            effect_df.loc[:, hdis[0]] = list(summary_sel[hdis[0]])
            effect_df.loc[:, hdis[1]] = list(summary_sel.loc[:, hdis[1]])

        intercept_df = intercept_df.loc[:, ["final_parameter", hdis[0], hdis[1], "sd", "expected_sample"]].copy()
        intercept_df = intercept_df.rename(columns=dict(zip(
            intercept_df.columns,
            ["Final Parameter", hdis_new[0], hdis_new[1], "SD", "Expected Sample"]
        )))

        effect_df = effect_df.loc[:, ["final_parameter", hdis[0], hdis[1], "sd", "inclusion_prob",
                                       "expected_sample", "log_fold"]].copy()
        effect_df = effect_df.rename(columns=dict(zip(
            effect_df.columns,
            ["Final Parameter", hdis_new[0], hdis_new[1], "SD", "Inclusion probability",
             "Expected Sample", "log2-fold change"]
        )))

        return intercept_df, effect_df

    def complete_beta_df(
            self,
            intercept_df: pd.DataFrame,
            effect_df: pd.DataFrame,
            target_fdr: float=0.05,
    ) -> pd.DataFrame:
        """
        Evaluation of MCMC results for effect parameters. This function is only used within self.summary_prepare.
        This function also calculates the posterior inclusion probability for each effect and decides whether effects are significant.

        Parameters
        ----------
        intercept_df
            Intercept summary, see ``self.summary_prepare``
        effect_df
            Effect summary, see ``self.summary_prepare``
        target_fdr
            Desired FDR value

        Returns
        -------
        effect DataFrame

        effect_df
            DataFrame with inclusion probability, final parameters, expected sample
        """
        beta_inc_prob = []
        beta_nonzero_mean = []

        beta_raw = np.array(self.posterior["beta"])[0]

        # Calculate inclusion prob, nonzero mean for every effect
        for j in range(beta_raw.shape[1]):
            for i in range(beta_raw.shape[2]):
                beta_i_raw = beta_raw[:, j, i]
                beta_i_raw_nonzero = np.where(np.abs(beta_i_raw) > 1e-3)[0]
                prob = beta_i_raw_nonzero.shape[0] / beta_i_raw.shape[0]
                beta_inc_prob.append(prob)
                beta_nonzero_mean.append(beta_i_raw[beta_i_raw_nonzero].mean())

        effect_df.loc[:, "inclusion_prob"] = beta_inc_prob
        effect_df.loc[:, "mean_nonzero"] = beta_nonzero_mean

        # Inclusion prob threshold value. Direct posterior probability approach cf. Newton et al. (2004)
        if self.is_sccoda is True:
            def opt_thresh(result, alpha):

                incs = np.array(result.loc[result["inclusion_prob"] > 0, "inclusion_prob"])
                incs[::-1].sort()

                for c in np.unique(incs):
                    fdr = np.mean(1 - incs[incs >= c])

                    if fdr < alpha:
                        # ceiling with 3 decimals precision
                        c = np.floor(c * 10 ** 3) / 10 ** 3
                        return c, fdr
                return 1., 0

            threshold, fdr_ = opt_thresh(effect_df, target_fdr)

            self.model_specs["threshold_prob"] = threshold

            # Decide whether betas are significant or not, set non-significant ones to 0
            effect_df.loc[:, "final_parameter"] = np.where(effect_df.loc[:, "inclusion_prob"] >= threshold,
                                                           effect_df.loc[:, "mean_nonzero"],
                                                           0)
        else:
            effect_df.loc[:, "final_parameter"] = effect_df.loc[:, "mean_nonzero"]

        # Get expected sample, log-fold change
        D = len(effect_df.index.levels[0])
        K = len(effect_df.index.levels[1])

        y_bar = np.mean(np.sum(np.array(self.observed_data.y), axis=1))
        alpha_par = intercept_df.loc[:, "final_parameter"]
        alphas_exp = np.exp(alpha_par)
        alpha_sample = (alphas_exp / np.sum(alphas_exp) * y_bar).values

        beta_mean = alpha_par
        beta_sample = []
        log_sample = []

        for d in range(D):
            beta_d = effect_df.loc[:, "final_parameter"].values[(d*K):((d+1)*K)]
            beta_d = (beta_mean + beta_d)
            beta_d = np.exp(beta_d)
            beta_d = beta_d / np.sum(beta_d) * y_bar

            beta_sample = np.append(beta_sample, beta_d)
            log_sample = np.append(log_sample, np.log2(beta_d/alpha_sample))

        effect_df.loc[:, "expected_sample"] = beta_sample
        effect_df.loc[:, "log_fold"] = log_sample

        return effect_df

    def complete_alpha_df(
            self,
            intercept_df: pd.DataFrame
    ) -> pd.DataFrame:
        """
        Evaluation of MCMC results for intercepts. This function is only used within self.summary_prepare.

        Parameters
        ----------
        intercept_df
            Intercept summary, see self.summary_prepare

        Returns
        -------
        intercept DataFrame

        intercept_df
            Summary DataFrame with expected sample, final parameters
        """

        intercept_df = intercept_df.rename(columns={"mean": "final_parameter"})

        # Get expected sample
        y_bar = np.mean(np.sum(np.array(self.observed_data.y), axis=1))
        alphas_exp = np.exp(intercept_df.loc[:, "final_parameter"])
        alpha_sample = (alphas_exp / np.sum(alphas_exp) * y_bar).values
        intercept_df.loc[:, "expected_sample"] = alpha_sample

        return intercept_df

    def summary(
            self,
            *args,
            **kwargs
    ):
        """
        Printing method for scCODA's summary.

        Usage: ``result.summary()``

        Parameters
        ----------
        args
            Passed to az.summary
        kwargs
            Passed to az.summary

        Returns
        -------
        prints to console

        """

        # If other than default values for e.g. confidence interval are specified,
        # recalculate them for intercept and effect DataFrames
        if args or kwargs:
            intercept_df, effect_df = self.summary_prepare(*args, **kwargs)
        else:
            intercept_df = self.intercept_df
            effect_df = self.effect_df

        # Get number of samples, cell types
        if self.sampling_stats["y_hat"] is not None:
            data_dims = self.sampling_stats["y_hat"].shape
        else:
            data_dims = (10, 5)

        # Cut down DataFrames to relevant info
        alphas_print = intercept_df.loc[:, ["Final Parameter", "Expected Sample"]]
        betas_print = effect_df.loc[:, ["Final Parameter", "Expected Sample", "log2-fold change"]]

        # Print everything neatly
        print("Compositional Analysis summary:")
        print("")
        print("Data: %d samples, %d cell types" % data_dims)
        print("Reference index: %s" % str(self.model_specs["reference"]))
        print("Formula: %s" % self.model_specs["formula"])
        print("")
        print("Intercepts:")
        print(alphas_print)
        print("")
        print("")
        print("Effects:")
        print(betas_print)

    def summary_extended(
            self,
            *args,
            **kwargs
    ):

        """
        Extended (diagnostic) printing function that shows more info about the sampling result

        Parameters
        ----------
        args
            Passed to az.summary
        kwargs
            Passed to az.summary

        Returns
        -------
        Prints to console

        """

        # If other than default values for e.g. confidence interval are specified,
        # recalculate them for intercept and effect DataFrames
        if args or kwargs:
            intercept_df, effect_df = self.summary_prepare(*args, **kwargs)
        else:
            intercept_df = self.intercept_df
            effect_df = self.effect_df

        # Get number of samples, cell types
        data_dims = self.sampling_stats["y_hat"].shape

        # Print everything
        print("Compositional Analysis summary (extended):")
        print("")
        print("Data: %d samples, %d cell types" % data_dims)
        print("Reference index: %s" % str(self.model_specs["reference"]))
        print("Formula: %s" % self.model_specs["formula"])
        if self.is_sccoda:
            print("Spike-and-slab threshold: {threshold:.3f}".format(threshold=self.model_specs["threshold_prob"]))
        print("")
        print("MCMC Sampling: Sampled {num_results} chain states ({num_burnin} burnin samples) in {duration:.3f} sec. "
              "Acceptance rate: {ar:.1f}%".format(num_results=self.sampling_stats["chain_length"],
                                                  num_burnin=self.sampling_stats["num_burnin"],
                                                  duration=self.sampling_stats["duration"],
                                                  ar=(100*self.sampling_stats["acc_rate"])))
        print("")
        print("Intercepts:")
        print(intercept_df)
        print("")
        print("")
        print("Effects:")
        print(effect_df)

    def compare_parameters_to_truth(
            self,
            b_true: pd.Series,
            w_true: pd.Series,
            *args,
            **kwargs
    ) -> Tuple[pd.DataFrame, pd.DataFrame]:
        """
        Extends data frames from summary_prepare by a comparison to some ground truth slope and intercept values that are
        assumed to be from the same generative model (e.g. in data_generation)

        Parameters
        ----------
        b_true
            Ground truth slope values. Length must be same as number of cell types
        w_true
            Ground truth intercept values. Length must be same as number of cell types*number of covariates
        args
            Passed to az.summary
        kwargs
            Passed to az.summary

        Returns
        -------
        Extends intercept and effect DataFrames

        intercept_df
            Summary DataFrame for intercepts
        effect_df
            Summary DataFrame for effects
        """

        intercept_df, effect_df = self.summary_prepare(*args, **kwargs)

        intercept_df.columns = intercept_df.columns.str.replace('final_parameter', 'predicted')
        effect_df.columns = effect_df.columns.str.replace('final_parameter', 'predicted')

        # Get true params, join to calculated parameters
        b_true = b_true.rename("truth")
        intercept_df = intercept_df.join(b_true)
        w_true = w_true.rename("truth")
        effect_df = effect_df.join(w_true)

        # decide whether effects are found correctly
        intercept_df['dist_to_truth'] = intercept_df['truth'] - intercept_df['predicted']
        intercept_df['effect_correct'] = ((intercept_df['truth'] == 0) == (intercept_df['predicted'] == 0))
        effect_df['dist_to_truth'] = effect_df['truth'] - effect_df['predicted']
        effect_df['effect_correct'] = ((effect_df['truth'] == 0) == (effect_df['predicted'] == 0))

        return intercept_df, effect_df

    def distance_to_truth(self) -> pd.DataFrame:
        """
        Compares real cell count matrix to the posterior mode cell count matrix that arises from the calculated parameters

        Returns
        -------
        DataFrame with distances

        ret
            DataFrame
        """

        # Get absolute (counts) and relative error matrices
        y = np.array(self.observed_data.y)
        y_hat = self.sampling_stats["y_hat"]
        err = np.abs(y_hat - y)

        err_rel = err / y
        err_rel[np.isinf(err_rel)] = 1.
        err_rel[np.isnan(err_rel)] = 0.

        # Calculate mean errors for each cell type and overall
        avg_abs_cell_type_error = np.mean(err, axis=0, dtype=np.float64)
        avg_rel_cell_type_error = np.mean(err_rel, axis=0, dtype=np.float64)
        avg_abs_total_error = np.mean(err, dtype=np.float64)
        avg_rel_total_error = np.mean(err_rel, dtype=np.float64)

        ret = pd.DataFrame({'Cell Type': np.arange(y.shape[1] + 1),
                            'Absolute Error': np.append(avg_abs_total_error, avg_abs_cell_type_error),
                            'Relative Error': np.append(avg_rel_total_error, avg_rel_cell_type_error),
                            'Actual Means': np.append(np.mean(y, axis=(0, 1)), np.mean(y, axis=0)),
                            'Predicted Means': np.append(np.mean(y_hat, axis=(0, 1)), np.mean(y_hat, axis=0))})

        ret['Cell Type'][0] = 'Total'
        return ret

    def credible_effects(
            self,
            est_fdr=None
    ) -> pd.Series:

        """
        Decides which effects of the scCODA model are credible based on an adjustable inclusion probability threshold.

        Parameters
        ----------
        est_fdr
            Estimated false discovery rate. Must be between 0 and 1

        Returns
        -------
        Credible effect decision series

        out
            Boolean values whether effects are credible under inc_prob_threshold
        """

        if type(est_fdr) == float:
            if est_fdr < 0 or est_fdr > 1:
                raise ValueError("est_fdr must be between 0 and 1!")
            else:
                _, eff_df = self.summary_prepare(est_fdr=est_fdr)
        else:
            eff_df = self.effect_df

        out = eff_df["Final Parameter"] != 0
        out.rename("credible change")

        return out

    def save(
            self,
            path_to_file: str
    ):
        """
        Function to save scCODA results to disk via pickle. Caution: Files can quickly become very large!

        Parameters
        ----------
        path_to_file
            saving location on disk

        Returns
        -------

        """
        with open(path_to_file, "wb") as f:
            pkl.dump(self, file=f, protocol=4)

    def set_fdr(
            self,
            est_fdr: float,
            *args,
            **kwargs):
        """
        Direct posterior probability approach to calculate credible effects while keeping the expected FDR at a certain level

        Parameters
        ----------
        est_fdr
            Desired FDR value
        args
            passed to self.summary_prepare
        kwargs
            passed to self.summary_prepare

        Returns
        -------
        Adjusts self.intercept_df and self.effect_df
        """

        intercept_df, effect_df = self.summary_prepare(est_fdr=est_fdr, *args, **kwargs)

        self.intercept_df = intercept_df
        self.effect_df = effect_df

max_stars_repo_name: bio-datascience/scCODA

max_issues_repo_name: bio-datascience/scCODA

max_forks_repo_name: bio-datascience/scCODA


text:
import numpy as np
from lxmls.deep_learning.utils import (
    Model,
    glorot_weight_init,
    index2onehot,
    logsumexp
)

class NumpyLogLinear(Model):

    def __init__(self, **config):

        # Initialize parameters
        weight_shape = (config['input_size'], config['num_classes'])
        # after Xavier Glorot et al
        self.weight = glorot_weight_init(weight_shape, 'softmax')
        self.bias = np.zeros((1, config['num_classes']))
        self.learning_rate = config['learning_rate']

    def log_forward(self, input=None):
        """Forward pass of the computation graph"""

        # Linear transformation
        z = np.dot(input, self.weight.T) + self.bias

        # Softmax implemented in log domain
        log_tilde_z = z - logsumexp(z, axis=1, keepdims=True)

        return log_tilde_z

    def predict(self, input=None):
        """Most probable class index"""
        return np.argmax(np.exp(self.log_forward(input)), axis=1)

    def update(self, input=None, output=None):
        """Stochastic Gradient Descent update"""

        # Probabilities of each class
        class_probabilities = np.exp(self.log_forward(input))
        batch_size, num_classes = class_probabilities.shape

        # Error derivative at softmax layer
        I = index2onehot(output, num_classes)
        error = (class_probabilities - I) / batch_size

        # Weight gradient
        gradient_weight = np.zeros(self.weight.shape)
        for l in np.arange(batch_size):
            gradient_weight += np.outer(error[l, :], input[l, :])

        # Bias gradient
        gradient_bias = np.sum(error, axis=0, keepdims=True)

        # SGD update
        self.weight = self.weight - self.learning_rate * gradient_weight
        self.bias = self.bias - self.learning_rate * gradient_bias

max_stars_repo_name: mtreviso/lxmls-toolkit

max_issues_repo_name: mtreviso/lxmls-toolkit

max_forks_repo_name: mtreviso/lxmls-toolkit


text:
from collections import defaultdict, deque
import pickle
from attrdict import AttrDict
import os
import numpy as np
import torch
from torch import nn
from torch import optim
from tensorboardX import SummaryWriter

class Checkpointer:
    def __init__(self, path, max_num=3):
        self.max_num = max_num
        self.path = path
        if not os.path.exists(path):
            os.makedirs(path)
        self.listfile = os.path.join(path, 'model_list.pkl')
        if not os.path.exists(self.listfile):
            with open(self.listfile, 'wb') as f:
                model_list = []
                pickle.dump(model_list, f)

    def save(self, model, optimizer, epoch):
        checkpoint = {
            'model': model.state_dict(),
            'optimizer': optimizer.state_dict(),
            'epoch': epoch
        }
        filename = os.path.join(self.path, 'model_{:05}.pth'.format(epoch))

        with open(self.listfile, 'rb+') as f:
            model_list = pickle.load(f)
            if len(model_list) >= self.max_num:
                if os.path.exists(model_list[0]):
                    os.remove(model_list[0])
                del model_list[0]
            model_list.append(filename)
        with open(self.listfile, 'rb+') as f:
            pickle.dump(model_list, f)

        with open(filename, 'wb') as f:
            torch.save(checkpoint, f)

    def load(self, model, optimizer):
        """
        Return starting epoch
        """
        with open(self.listfile, 'rb') as f:
            model_list = pickle.load(f)
            if len(model_list) == 0:
                print('No checkpoint found. Starting from scratch')
                return 0
            else:
                checkpoint = torch.load(model_list[-1])
                model.load_state_dict(checkpoint['model'])
                optimizer.load_state_dict(checkpoint['optimizer'])
                print('Load checkpoint from {}.'.format(model_list[-1]))
                return checkpoint['epoch']

max_stars_repo_name: Victorwz/fast-weights-pytorch

max_issues_repo_name: Victorwz/fast-weights-pytorch

max_forks_repo_name: Victorwz/fast-weights-pytorch


text:
################################################################################
# Copyright (c) 2015-2019 Skymind, Inc.
#
# This program and the accompanying materials are made available under the
# terms of the Apache License, Version 2.0 which is available at
# https://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.
#
# SPDX-License-Identifier: Apache-2.0
################################################################################

# You can use jumpy to run tensorflow models using nd4j from python

import jumpy as jp
import numpy as np

path_to_tf_model = 'Path to your TF model protobuff (binary or text)'

tf_model = jp.TFModel(path_to_tf_model)

# Now you can pass jumpy or numpy arrays to tf_model:

jp_array = jp.zeros((32, 28, 28))
output_array = tf_model(jp_array)

np_array = np.zeros((32, 28, 28))
output_array = tf_model(np_array)


text:
import numpy as np
import tensorflow as tf
import tensorflow_probability as tfp
import Nn
from utils.sth import sth
from .policy import Policy

class PPO(Policy):
    def __init__(self,
                 s_dim,
                 visual_sources,
                 visual_resolution,
                 a_dim_or_list,
                 action_type,
                 epsilon=0.2,
                 gamma=0.99,
                 beta=1.0e-3,
                 lr=5.0e-4,
                 lambda_=0.95,
                 max_episode=50000,
                 batch_size=100,
                 epoch=5,
                 base_dir=None,
                 logger2file=False,
                 out_graph=False):
        super().__init__(s_dim, visual_sources, visual_resolution, a_dim_or_list, action_type, gamma, max_episode, base_dir, 'ON')
        self.beta = beta
        self.epoch = epoch
        self.lambda_ = lambda_
        self.epsilon = epsilon
        self.batch_size = batch_size
        self.lr = lr
        self.sigma_offset = np.full([self.a_counts, ], 0.01)
        if self.action_type == 'continuous':
            self.net = Nn.a_c_v_continuous(self.s_dim, self.visual_dim, self.a_counts, 'ppo')
        else:
            self.net = Nn.a_c_v_discrete(self.s_dim, self.visual_dim, self.a_counts, 'ppo')
        self.optimizer = tf.keras.optimizers.Adam(learning_rate=self.lr)
        self.generate_recorder(
            logger2file=logger2file,
            model=self
        )
        self.recorder.logger.info('''
　　　ｘｘｘｘｘｘｘｘ　　　　　　　ｘｘｘｘｘｘｘｘ　　　　　　　　　ｘｘｘｘｘ
　　　　　ｘｘ　　ｘｘ　　　　　　　　　ｘｘ　　ｘｘ　　　　　　　　ｘｘｘ　ｘｘｘ
　　　　　ｘ　　　ｘｘｘ　　　　　　　　ｘ　　　ｘｘｘ　　　　　　　ｘｘ　　　ｘｘ
　　　　　ｘ　　　ｘｘｘ　　　　　　　　ｘ　　　ｘｘｘ　　　　　　　ｘｘ　　　ｘｘｘ
　　　　　ｘｘｘｘｘｘ　　　　　　　　　ｘｘｘｘｘｘ　　　　　　　ｘｘｘ　　　ｘｘｘ
　　　　　ｘ　　　　　　　　　　　　　　ｘ　　　　　　　　　　　　　ｘｘ　　　ｘｘｘ
　　　　　ｘ　　　　　　　　　　　　　　ｘ　　　　　　　　　　　　　ｘｘ　　　ｘｘ
　　　　　ｘ　　　　　　　　　　　　　　ｘ　　　　　　　　　　　　　ｘｘ　　ｘｘｘ
　　　ｘｘｘｘｘ　　　　　　　　　　ｘｘｘｘｘ　　　　　　　　　　　　ｘｘｘｘｘ
        ''')

    def choose_action(self, s, visual_s):
        if self.action_type == 'continuous':
            return self._get_action(s, visual_s).numpy()
        else:
            if np.random.uniform() < self.epsilon:
                a = np.random.randint(0, self.a_counts, len(s))
            else:
                a = self._get_action(s, visual_s).numpy()
            return sth.int2action_index(a, self.a_dim_or_list)

    def choose_inference_action(self, s, visual_s):
        a = self._get_action(s, visual_s).numpy()
        return a if self.action_type == 'continuous' else sth.int2action_index(a, self.a_dim_or_list)

    @tf.function
    def _get_action(self, vector_input, visual_input):
        with tf.device(self.device):
            if self.action_type == 'continuous':
                mu, sigma, value = self.net(vector_input, visual_input)
                norm_dist = tfp.distributions.Normal(loc=mu, scale=sigma + self.sigma_offset)
                sample_op = tf.clip_by_value(norm_dist.sample(), -1, 1)
            else:
                action_probs, value = self.net(vector_input, visual_input)
                sample_op = tf.argmax(action_probs, axis=1)
        return sample_op

    def store_data(self, s, visual_s, a, r, s_, visual_s_, done):
        assert isinstance(a, np.ndarray), "store_data need action type is np.ndarray"
        assert isinstance(r, np.ndarray), "store_data need reward type is np.ndarray"
        assert isinstance(done, np.ndarray), "store_data need done type is np.ndarray"
        if not self.action_type == 'continuous':
            a = sth.action_index2one_hot(a, self.a_dim_or_list)
        self.data = self.data.append({
            's': s,
            'visual_s': visual_s,
            'a': a,
            'r': r,
            's_': s_,
            'visual_s_': visual_s_,
            'done': done,
            'value': np.squeeze(self._get_value(s, visual_s).numpy()),
            'next_value': np.squeeze(self._get_value(s_, visual_s_).numpy()),
            'prob': self._get_new_prob(s, visual_s, a).numpy() + 1e-10
        }, ignore_index=True)

    @tf.function
    def _get_value(self, s, visual_s):
        with tf.device(self.device):
            if self.action_type == 'continuous':
                mu, sigma, value = self.net(s, visual_s)
            else:
                action_probs, value = self.net(s, visual_s)
            return value

    @tf.function
    def _get_new_prob(self, s, visual_s, a):
        with tf.device(self.device):
            if self.action_type == 'continuous':
                mu, sigma, value = self.net(s, visual_s)
                norm_dist = tfp.distributions.Normal(loc=mu, scale=sigma + self.sigma_offset)
                new_prob = tf.reduce_mean(norm_dist.prob(a), axis=1, keepdims=True)
            else:
                action_probs, value = self.net(s, visual_s)
                new_prob = tf.reduce_max(action_probs, axis=1, keepdims=True)
            return new_prob

    def calculate_statistics(self):
        self.data['total_reward'] = sth.discounted_sum(self.data.r.values, 1, 0, self.data.done.values)
        self.data['discounted_reward'] = sth.discounted_sum(self.data.r.values, self.gamma, self.data.next_value.values[-1], self.data.done.values)
        self.data['td_error'] = sth.discounted_sum_minus(
            self.data.r.values,
            self.gamma,
            self.data.next_value.values[-1],
            self.data.done.values,
            self.data.value.values
        )
        # GAE
        self.data['advantage'] = sth.discounted_sum(
            self.data.td_error.values,
            self.lambda_ * self.gamma,
            0,
            self.data.done.values
        )
        # self.data.to_excel(self.excel_writer, sheet_name='test', index=True)
        # self.excel_writer.save()

    def get_sample_data(self):
        i_data = self.data.sample(n=self.batch_size) if self.batch_size < self.data.shape[0] else self.data
        s = np.vstack([i_data.s.values[i] for i in range(i_data.shape[0])])
        visual_s = np.vstack([i_data.visual_s.values[i] for i in range(i_data.shape[0])])
        a = np.vstack([i_data.a.values[i] for i in range(i_data.shape[0])])
        dc_r = np.vstack([i_data.discounted_reward.values[i][:, np.newaxis] for i in range(i_data.shape[0])])
        old_prob = np.vstack([i_data.prob.values[i] for i in range(i_data.shape[0])])
        advantage = np.vstack([i_data.advantage.values[i][:, np.newaxis] for i in range(i_data.shape[0])])
        return s, visual_s, a, dc_r, old_prob, advantage

    def learn(self, episode):
        self.calculate_statistics()
        for _ in range(self.epoch):
            s, visual_s, a, dc_r, old_prob, advantage = self.get_sample_data()
            self.global_step.assign_add(1)
            actor_loss, critic_loss, entropy = self.train(s, visual_s, a, dc_r, old_prob, advantage)
            tf.summary.experimental.set_step(self.global_step)
            if entropy is not None:
                tf.summary.scalar('LOSS/entropy', entropy)
            tf.summary.scalar('LOSS/actor_loss', actor_loss)
            tf.summary.scalar('LOSS/critic_loss', critic_loss)
            tf.summary.scalar('LEARNING_RATE/lr', self.lr)
            tf.summary.scalar('REWARD/discounted_reward', self.data.discounted_reward.values[0].mean())
            tf.summary.scalar('REWARD/reward', self.data.total_reward.values[0].mean())
            self.recorder.writer.flush()
        self.clear()

    @tf.function(experimental_relax_shapes=True)
    def train(self, s, visual_s, a, dc_r, old_prob, advantage):
        with tf.device(self.device):
            with tf.GradientTape() as tape:
                if self.action_type == 'continuous':
                    mu, sigma, value = self.net(s, visual_s)
                    norm_dist = tfp.distributions.Normal(loc=mu, scale=sigma + self.sigma_offset)
                    new_prob = tf.reduce_mean(norm_dist.prob(a), axis=1, keepdims=True)
                    sample_op = tf.clip_by_value(norm_dist.sample(), -1, 1)
                    entropy = tf.reduce_mean(norm_dist.entropy())
                else:
                    action_probs, value = self.net(s, visual_s)
                    new_prob = tf.reduce_max(action_probs, axis=1, keepdims=True)
                    sample_op = tf.argmax(action_probs, axis=1)
                ratio = new_prob / old_prob
                surrogate = ratio * advantage
                td_error = dc_r - value
                actor_loss = tf.reduce_mean(
                    tf.minimum(
                        surrogate,
                        tf.clip_by_value(ratio, 1.0 - self.epsilon, 1.0 + self.epsilon) * advantage
                    ))
                value_loss = tf.reduce_mean(tf.square(td_error))
                if self.action_type == 'continuous':
                    loss = -(actor_loss - 1.0 * value_loss + self.beta * entropy)
                else:
                    loss = value_loss - actor_loss
            loss_grads = tape.gradient(loss, self.net.trainable_variables)
            self.optimizer.apply_gradients(
                zip(loss_grads, self.net.trainable_variables)
            )
            return actor_loss, value_loss, entropy if self.action_type == 'continuous' else None

max_stars_repo_name: familywei/RLs

max_issues_repo_name: familywei/RLs

max_forks_repo_name: familywei/RLs


text:
import cv2
import numpy as np

def canny(image):
    gray = cv2.cvtColor(lane_image, cv2.COLOR_RGB2GRAY)
    blur = cv2.GaussianBlur(gray, (5, 5), 0)
    canny = cv2.Canny(blur, 50, 150)
    return canny

def display_lines(image, lines):
    line_image = np.zeros_like(image)
    if lines is not None:
        for line in lines:
            #print(line)
            x1, y1, x2, y2 = line.reshape(4)
            cv2.line(line_image, (x1, y1), (x2, y2), (255, 0, 0), 10)
    return line_image

def region_of_interest(image):
    height = image.shape[0]
    triangle = np.array([[(200, height), (1100, height), (550, 250)]])
    mask = np.zeros_like(image)
    cv2.fillPoly(mask, triangle, 255)
    masked_image = cv2.bitwise_and(image, mask)
    return masked_image

image = cv2.imread('test_image.jpg')
lane_image = np.copy(image)
canny = canny(lane_image)
cropped_image = region_of_interest(canny)
lines = cv2.HoughLinesP(cropped_image, 2, np.pi/180, 100, np.array([]), minLineLength=40, maxLineGap=5)
line_image = display_lines(lane_image, lines)
combo_image = cv2.addWeighted(lane_image, 0.8, line_image, 1, 1)
cv2.imshow('result', combo_image)
cv2.waitKey(0)

max_stars_repo_path: lane-line/Hough Transform.py

max_issues_repo_path: lane-line/Hough Transform.py

max_forks_repo_path: lane-line/Hough Transform.py