text:
from __future__ import annotations

import itertools
from typing import Any, Tuple
from collections.abc import Iterable

import pandas as pd
import numpy as np

ITEMID = "item_id"
TIMESTAMP = "timestamp"

class TimeSeriesDataFrame(pd.DataFrame):
    """TimeSeriesDataFrame to represent time-series dataset.

    Parameters
    ----------
    data : Any
        Time-series data to construct a TimeSeriesDataFrame.
        It currently supports three input formats:

            1. Time-series data in Iterable format. For example:

                iterable_dataset = [
                    {"target": [0, 1, 2], "start": pd.Timestamp("01-01-2019", freq='D')},
                    {"target": [3, 4, 5], "start": pd.Timestamp("01-01-2019", freq='D')},
                    {"target": [6, 7, 8], "start": pd.Timestamp("01-01-2019", freq='D')}
                ]

            2. Time-series data in pd.DataFrame format without multi-index. For example:

                   item_id  timestamp  target
                0        0 2019-01-01       0
                1        0 2019-01-02       1
                2        0 2019-01-03       2
                3        1 2019-01-01       3
                4        1 2019-01-02       4
                5        1 2019-01-03       5
                6        2 2019-01-01       6
                7        2 2019-01-02       7
                8        2 2019-01-03       8

            3. Time-series data in pd.DataFrame format with multi-index on item_id and timestamp. For example:

                                        target
                item_id timestamp
                0       2019-01-01       0
                        2019-01-02       1
                        2019-01-03       2
                1       2019-01-01       3
                        2019-01-02       4
                        2019-01-03       5
                2       2019-01-01       6
                        2019-01-02       7
                        2019-01-03       8
                This example can be found using example() function.
    """

    def __init__(self, data: Any, *args, **kwargs):
        if isinstance(data, pd.DataFrame):
            if isinstance(data.index, pd.MultiIndex):
                self._validate_multi_index_data_frame(data)
            else:
                data = self.from_data_frame(data)
        else:
            data = self.from_iterable_dataset(data)
        super().__init__(data=data, *args, **kwargs)

    # TODO: move out of production code to dedicated example in examples
    @classmethod
    def example(cls):
        """An example TimeSeriesDataFrame.

        Returns
        -------
        ts_df : TimeSeriesDataFrame
            It returns an example TimeSeriesDataFrame as:
                                    target
            item_id timestamp
            0       2019-01-01       0
                    2019-01-02       1
                    2019-01-03       2
            1       2019-01-01       3
                    2019-01-02       4
                    2019-01-03       5
            2       2019-01-01       6
                    2019-01-02       7
                    2019-01-03       8
        """

        target = np.arange(9)
        datetime_index = tuple(
            pd.date_range(pd.Timestamp("01-01-2019"), periods=3, freq="D")  # noqa
        )
        item_ids = (0, 1, 2)
        multi_index = pd.MultiIndex.from_product(
            [item_ids, datetime_index], names=[ITEMID, TIMESTAMP]
        )
        return TimeSeriesDataFrame(
            pd.Series(target, name="target", index=multi_index).to_frame()
        )

    @classmethod
    def _validate_iterable(cls, data: Iterable):
        if not isinstance(data, Iterable):
            raise ValueError("data must be of type Iterable.")

        first = next(iter(data), None)
        if first is None:
            raise ValueError("data has no time-series.")

        for i, ts in enumerate(itertools.chain([first], data)):
            if not isinstance(ts, dict):
                raise ValueError(
                    f"{i}'th time-series in data must be a dict, got{type(ts)}"
                )
            if not ("target" in ts and "start" in ts):
                raise ValueError(
                    f"{i}'th time-series in data must have 'target' and 'start', got{ts.keys()}"
                )
            if not isinstance(ts["start"], pd.Timestamp) or ts["start"].freq is None:
                raise ValueError(
                    f"{i}'th time-series must have timestamp as 'start' with freq specified, got {ts['start']}"
                )

    @classmethod
    def _validate_data_frame(cls, df: pd.DataFrame):
        if not isinstance(df, pd.DataFrame):
            raise ValueError(f"data must be a pd.DataFrame, got {type(df)}")
        if ITEMID not in df.columns:
            raise ValueError(f"data must have a `{ITEMID}` column")
        if TIMESTAMP not in df.columns:
            raise ValueError(f"data must have a `{TIMESTAMP}` column")
        if df[ITEMID].isnull().any():
            raise ValueError(f"`{ITEMID}` column can not have nan")
        if df[TIMESTAMP].isnull().any():
            raise ValueError(f"`{TIMESTAMP}` column can not have nan")
        if not df[ITEMID].dtype == "int64":
            raise ValueError(f"for {ITEMID}, the only pandas dtype allowed is ‘int64’.")
        if not df[TIMESTAMP].dtype == "datetime64[ns]":
            raise ValueError(
                f"for {TIMESTAMP}, the only pandas dtype allowed is ‘datetime64[ns]’."
            )

    @classmethod
    def _validate_multi_index_data_frame(cls, data: pd.DataFrame):
        """Validate a multi-index pd.DataFrame can be converted to TimeSeriesDataFrame

        Parameters:
        -----------
        data : pd.DataFrame
            a data frame in pd.DataFrame format.
        """

        if not isinstance(data, pd.DataFrame):
            raise ValueError(f"data must be a pd.DataFrame, got {type(data)}")
        if not isinstance(data.index, pd.MultiIndex):
            raise ValueError(f"data must have pd.MultiIndex, got {type(data.index)}")
        if not data.index.dtypes.array[0] == "int64":
            raise ValueError(f"for {ITEMID}, the only pandas dtype allowed is ‘int64’.")
        if not data.index.dtypes.array[1] == "datetime64[ns]":
            raise ValueError(
                f"for {TIMESTAMP}, the only pandas dtype allowed is ‘datetime64[ns]’."
            )
        if not data.index.names == (f"{ITEMID}", f"{TIMESTAMP}"):
            raise ValueError(
                f"data must have index names as ('{ITEMID}', '{TIMESTAMP}'), got {data.index.names}"
            )

    @classmethod
    def from_iterable_dataset(cls, iterable_dataset: Iterable) -> TimeSeriesDataFrame:
        """Convenient function to Iterable dataset to TimeSeriesDataFrame.

        Parameters:
        -----------
        iterable_dataset : Iterable
            The iterable_dataset must have the following format:

            iterable_dataset = [
                {"target": [0, 1, 2], "start": pd.Timestamp("01-01-2019", freq='D')},
                {"target": [3, 4, 5], "start": pd.Timestamp("01-01-2019", freq='D')},
                {"target": [6, 7, 8], "start": pd.Timestamp("01-01-2019", freq='D')}
            ]

        Returns:
        --------
        ts_df : TimeSeriesDataFrame
            A data frame in TimeSeriesDataFrame format.
        """

        cls._validate_iterable(iterable_dataset)

        all_ts = []
        for i, ts in enumerate(iterable_dataset):
            start_timestamp = ts["start"]
            target = ts["target"]
            datetime_index = tuple(
                pd.date_range(
                    start_timestamp, periods=len(target), freq=start_timestamp.freq
                )
            )
            idx = pd.MultiIndex.from_product(
                [(i,), datetime_index], names=[ITEMID, TIMESTAMP]
            )
            ts_df = pd.Series(target, name="target", index=idx).to_frame()
            all_ts.append(ts_df)
        return TimeSeriesDataFrame(pd.concat(all_ts))

    @classmethod
    def from_data_frame(cls, df: pd.DataFrame) -> TimeSeriesDataFrame:
        """Convert a normal pd.DataFrame to a TimeSeriesDataFrame

        Parameters:
        -----------
        df: pd.DataFrame
            A pd.DataFrame with 'item_id' and 'timestamp' as columns. For example:

               item_id  timestamp  target
            0        0 2019-01-01       0
            1        0 2019-01-02       1
            2        0 2019-01-03       2
            3        1 2019-01-01       3
            4        1 2019-01-02       4
            5        1 2019-01-03       5
            6        2 2019-01-01       6
            7        2 2019-01-02       7
            8        2 2019-01-03       8

        Returns:
        --------
        ts_df : TimeSeriesDataFrame
            A data frame in TimeSeriesDataFrame format.
        """
        cls._validate_data_frame(df)
        return TimeSeriesDataFrame(df.set_index([ITEMID, TIMESTAMP]))

    def split_by_time(
        self, cutoff_time: pd.Timestamp
    ) -> Tuple[TimeSeriesDataFrame, TimeSeriesDataFrame]:
        """Split dataframe by a cutoff_time.

        Parameters
        ----------
        cutoff_time : pd.Timestamp
            The time to Split the current data frame into two data frames, all in TimeSeriesDataFrame format.

        Returns
        -------
        data_before : TimeSeriesDataFrame
            The first one after split contains time-series before the cutoff_time (exclude cutoff_time).
        data_after : TimeSeriesDataFrame
            The second one after split contains time-series after the cutoff_time (include cutoff_time).
        """

        nanosecond_before_cutoff = cutoff_time - pd.Timedelta(nanoseconds=1)
        data_before = self.loc[(slice(None), slice(None, nanosecond_before_cutoff)), :]
        data_after = self.loc[(slice(None), slice(cutoff_time, None)), :]
        return TimeSeriesDataFrame(data_before), TimeSeriesDataFrame(data_after)

    def split_by_item(
        self, cutoff_item: int
    ) -> Tuple[TimeSeriesDataFrame, TimeSeriesDataFrame]:
        """Split dataframe by an item_id cutoff_item.

        Parameters
        ----------
        cutoff_item : int
            The item_id to Split the current data frame into two data frames, all in TimeSeriesDataFrame format.

        Returns
        -------
        data_before : TimeSeriesDataFrame
            The first one after split contains time-series before the cutoff_item (exclude cutoff_item).
        data_after : TimeSeriesDataFrame
            The second one after split contains time-series after the cutoff_item (include cutoff_item).
        """

        data_before = self.loc[(slice(None, cutoff_item - 1), slice(None)), :]
        data_after = self.loc[(slice(cutoff_item, None), slice(None)), :]
        return TimeSeriesDataFrame(data_before), TimeSeriesDataFrame(data_after)

    def subsequence(
        self, start: pd.Timestamp, end: pd.Timestamp
    ) -> TimeSeriesDataFrame:
        """Extract time-series between start (inclusive) and end (exclusive) time.

        Parameters
        ----------
        start : pd.Timestamp
            The start time (inclusive) of a time range that will be used for subsequence.
        end : pd.Timestamp
            The end time (exclusive) of a time range that will be used for subsequence.

        Returns
        -------
        ts_df : TimeSeriesDataFrame
            A new data frame in TimeSeriesDataFrame format contains time-series in a time range
            defined between start and end time.
        """

        if end < start:
            raise ValueError(f"end time {end} is earlier than stat time {start}")

        nanosecond_before_end = end - pd.Timedelta(nanoseconds=1)
        return TimeSeriesDataFrame(
            self.loc[(slice(None), slice(start, nanosecond_before_end)), :]
        )

max_stars_repo_name: yiqings/autogluon

max_issues_repo_name: yiqings/autogluon

max_forks_repo_name: yiqings/autogluon


text:
import pandas as pd
import numpy as np
import altair as alt
import logging
import networkx as nx
import yaml
from scipy.stats import zscore

from narrowing_ai_research.utils.read_utils import (
    read_papers,
    read_topic_mix,
)
from narrowing_ai_research.utils.altair_utils import (
    altair_visualisation_setup,
    save_altair,
)

from narrowing_ai_research.transformers.networks import (
    make_network_from_doc_term_matrix,
)
from narrowing_ai_research.transformers.diversity import Diversity

import narrowing_ai_research

project_dir = narrowing_ai_research.project_dir

def read_process_data():
    papers = read_papers()
    topic_mix = (
        remove_zero_axis(  # We remove a couple of papers with zero in all topics
            read_topic_mix().set_index("article_id")
        )
    )

    logging.info("Process dfs")
    papers["year"] = [x.year for x in papers["date"]]

    return papers, topic_mix

def normalise_diversity_year_df(y_div_df):
    """Normalises a dataframe with diversity information by year and parametre set"""
    yearly_results_norm = []

    # For each possible diversity metric it pivots over parametre sets
    # and calculates the zscore for the series
    for x in set(y_div_df["diversity_metric"]):
        yearly_long = y_div_df.query(f"diversity_metric == '{x}'").pivot_table(
            index=["year", "diversity_metric"], columns="parametre_set", values="score"
        )

        yearly_long_norm = yearly_long.apply(zscore)
        yearly_results_norm.append(yearly_long_norm)

    # Concatenate and melt so they can be visualised with altair
    y_div_df_norm = (
        pd.concat(yearly_results_norm)
        .reset_index(drop=False)
        .melt(
            id_vars=["year", "diversity_metric"],
            var_name="parametre_set",
            value_name="score",
        )
    )

    return y_div_df_norm

def year_diversity_results(data, year_ids, metric_params, t0=2008, t1=2020, norm=True):
    """Extracts yearly diversity results
    Args:
        data: topic mix
        metric_params: dict where keys are diversity metrics and
            values lists of parametres
        t0: first year
        t1: last year
        norm: if we want to obtain a normalised value (by year)
    """
    year_period = np.arange(t0, t1 + 1)

    year_results = []

    # Calculate diversity per year, metric and parametre set
    for y in year_period:
        logging.info(y)

        # Here we remove axes with zero values
        data_year = remove_zero_axis(data.loc[year_ids[y]])
        logging.info(data_year.shape)

        for d in metric_params.keys():
            for n, b in enumerate(metric_params[d]):
                div = Diversity(data_year, y)
                getattr(div, d)(b)
                r = [y, div.metric_name, div.metric, div.param_values, f"param_set_{n}"]
                year_results.append(r)

    yearly_results_df = pd.DataFrame(
        year_results,
        columns=[
            "year",
            "diversity_metric",
            "score",
            "parametres_detailed",
            "parametre_set",
        ],
    )

    if norm is True:
        yearly_results_norm_df = normalise_diversity_year_df(yearly_results_df)
        return yearly_results_norm_df

    else:
        return yearly_results_df

def make_chart_diversity_evol(results, save=True, fig_n=10):
    """Plots evolution of diversity"""
    div_evol_ch = (
        (
            alt.Chart(results)
            .mark_line(opacity=0.9)
            .encode(
                x=alt.X("year:O", title=""),
                y=alt.Y("score:Q", scale=alt.Scale(zero=False), title="z-score"),
                row=alt.Row(
                    "diversity_metric",
                    title="Diversity metric",
                    sort=["balance", "weitzman", "rao_stirling"],
                ),
                color=alt.Color("parametre_set:N", title="Parameter set"),
            )
        )
        .resolve_scale(y="independent")
        .properties(width=250, height=80)
    )

    if save is True:
        save_altair(div_evol_ch, f"fig_{fig_n}_div_evol", driv)

    return div_evol_ch

def extract_distribution_centrality(
    data,
    year_ids,
    threshold=0.05,
    ranking_distr=[20, 50, 100, 200],
    ranking_centrality=[20, 50, 100, 200, 400, 600],
    t0=2008,
    t1=2020,
):
    """Extracts topic distribution and centrality under various thresholds
    Args:
        data: topic mix
        year_ids: ids for the years
        threshold: threshold for inclusion of topics
        ranking_distr: rankings to estimate share of distribution accounted
        ranking_centrality: rankings to compare evolution of centrality

    Returns shares and eigenvector centralities for topics in different positions
    of the activity distribution (by shares of activity)
    """
    # Balance:
    # What are the % of activity accounted for the most popular topics?
    act_top = []
    centrality_ranked = []

    # Extract the share of activity accountes by top 10,25,50,100 topics each year
    for y in np.arange(t0, t1 + 1):
        logging.info(y)

        # Activity in year
        y_t = data.loc[data.index.isin(year_ids[y])]
        y_t = remove_zero_axis(y_t)

        # Total activity by topic in the year
        topic_distr = y_t.idxmax(axis=1).value_counts(normalize=True).cumsum()

        # Topic co-occurrence network
        net = make_network_from_doc_term_matrix(
            y_t.reset_index(drop=False), id_var="article_id", threshold=0.05
        )

        # Extract eigenvector centrality from the network
        ev = pd.Series(nx.eigenvector_centrality(net))

        # These are sorted by level of activity (as in topic distr above)
        index_for_sort = [x for x in topic_distr.index if x in ev.index]

        ev_df = (
            ev.loc[index_for_sort]
            .reset_index(drop=False)
            .rename(columns={"index": "topic", 0: "eigenvector_centrality"})
        )

        ev_df["eigenvector_z"] = zscore(ev_df["eigenvector_centrality"])
        ev_df["rank"] = range(0, len(ev_df))
        ev_df["year"] = y

        shares = []

        for n in ranking_distr:
            shares.append(topic_distr[n])

        act_top.append(pd.Series(shares, name=y, index=[x for x in ranking_distr]))
        centrality_ranked.append(ev_df)

    shares_long = (
        pd.concat(act_top, axis=1).reset_index(drop=False).melt(id_vars="index")
    )

    centrality_ranked_all = pd.concat(centrality_ranked)
    centrality_ranked_all["rank_segment"] = centrality_ranked_all["rank"].apply(
        allocate_rank, thres=ranking_centrality
    )

    return shares_long, centrality_ranked_all

def make_chart_distribution_centrality(
    shares_long, centrality_ranked_all, saving=True, fig_n=11
):
    """Plots evolution of shares of topics and centrality averages
    at different positions of the distribution
    Args:
        shares_long: df with shares of topic activity accounted at
            different points of the distribution
        centrality_ranked_all: df with mean centralities for topics
            at different points of the distribution

    """

    shares_evol = (
        alt.Chart(shares_long)
        .mark_line()
        .encode(
            x=alt.X("variable:N", title="", axis=alt.Axis(labels=False, ticks=False)),
            y=alt.Y("value", title=["Share of activity", "accounted by rank"]),
            color=alt.X("index:N", title="Position in distribution"),
        )
    ).properties(width=300, height=170)

    line = (
        alt.Chart(centrality_ranked_all)
        .transform_aggregate(m="mean(eigenvector_z)", groupby=["year", "rank_segment"])
        .mark_line()
        .encode(x=alt.X("year:N", title=""), y="m:Q", color="rank_segment:N")
    )

    band = (
        alt.Chart(centrality_ranked_all)
        .mark_errorband()
        .encode(
            x="year:N",
            y=alt.Y("eigenvector_z", title=["Mean eigenvector", "centrality"]),
            color=alt.Color("rank_segment:N", title="Position in distribution"),
        )
    )

    eigen_evol_linech = (band + line).properties(width=300, height=170)

    div_comp = alt.vconcat(shares_evol, eigen_evol_linech, spacing=0).resolve_scale(
        x="shared", color="independent"
    )

    if saving is True:
        save_altair(div_comp, f"fig_{fig_n}_div_comps_evol", driv)

    return div_comp

def allocate_rank(x, thres):
    """Allocates a value to its rank based on thresholds"""
    out = np.nan

    for v in thres:

        if x < v:
            return v

    return out

def remove_zero_axis(df):
    """Removes axes where all values are zero"""

    df_r = df.loc[df.sum(axis=1) > 0, df.sum() > 0]
    return df_r

def main():
    logging.info("Reading data")

    with open(f"{project_dir}/paper_config.yaml", "r") as infile:
        div_params = yaml.safe_load(infile)["section_4"]["div_params"]

    papers, topic_mix = read_process_data()

    year_ids = (
        papers.loc[papers["is_ai"] == True]
        .groupby("year")["article_id"]
        .apply(lambda x: set(x))
    )

    logging.info("Calculate yearly diversity")
    yearly_diversity_norm = year_diversity_results(topic_mix, year_ids, div_params)

    div_year_ch = make_chart_diversity_evol(yearly_diversity_norm, save=True)

    logging.info("Evolution of concentration and centrality")
    shares_long, centrality_ranked_all = extract_distribution_centrality(
        topic_mix, year_ids
    )

    conc_centr_ch = make_chart_distribution_centrality(
        shares_long, centrality_ranked_all, saving=True
    )

if __name__ == "__main__":
    alt.data_transformers.disable_max_rows()
    pd.options.mode.chained_assignment = None
    driv = altair_visualisation_setup()
    main()

ext: py

reason: import numpy,from scipy,import networkx


text:
# -*- coding: utf-8 -*-
"""
Created on Fri Oct 13 11:59:46 2017, updated Oct 26 2021

@author: romainb

Script building an interactive Bokeh visualization
for dynamic MFA models, where the user can:
    - choose between stock and inflow-driven models
    - choose different presets for the driver (inflow or stock)
    - change the lifetime distribution
      (normal distribution defined by mean and standard deviation)
    - visualise the effet of decreasing or increasing lifetime

Based on the package dynamic_stock_model by Stefan Pauliuk:
https://github.com/stefanpauliuk/dynamic_stock_model/

The time and input data have to be specified in the file Sample_data.csv

To run the file on a server, it is necessary to open the Anaconda prompt
and type these two command lines
(the first one should be mofidified to specifiy the actual path)
cd *path*
bokeh serve --show dynamic_stock_viz.py

dependencies:
    bokeh == 2.3.0
    numpy == 1.19.2
    pandas == 1.2.3
    scipy == 1.6.2
"""

import pandas as pd
import numpy as np
import stock_model_functions as smf

from bokeh.models import ColumnDataSource
from bokeh.plotting import figure, curdoc
from bokeh.layouts import row, column
from bokeh.models.widgets import Slider, Select, Div
from bokeh.palettes import Category20

# Initializing parameters for the model
data = smf.data
lifetime_start = 15
lifetime_end = 15
stdev=5
driver='Inflow-driven'
selected_input = 'Constant'

lifetime = smf.range_lifetime(lifetime_start, lifetime_end, stdev, len(data['Time']))
DSM = smf.compute_model(smf.data, lifetime)

def update_driver(attrname, old, new):
    """
    Callback function to dynamically update the value of
    the driver type parameter
    each time the user changes the value in the corresponding widget
    It is necessary to update the value of each data source as well
    """
    global source, source_sc, source_bar, cohorts_matrix, driver
    driver = new

    lifetime = smf.range_lifetime(lifetime_start, lifetime_end, stdev, len(smf.data['Time']))
    DSM = smf.compute_model(smf.data, lifetime, driver, input_type=selected_input)
    print("Driver:", new)
    plot_data = {'time': DSM.t,
             'stock': DSM.s,
             'inflows': DSM.i,
             'outflows': DSM.o,
             'stock change': DSM.i - DSM.o
             }
    source.data = plot_data
    source_sc.data = {'image': [np.flipud(DSM.s_c)]}
    cohorts_matrix = pd.DataFrame(
        data=DSM.s_c,
        index=DSM.t.to_numpy(),
        columns=[str(e) for e in plot_data["time"].to_numpy().tolist()]
        )
    cohorts_matrix.insert(
        0, "time",[str(e) for e in plot_data["time"].to_numpy().tolist()])
    source_bar = ColumnDataSource(data=cohorts_matrix)

def update_input(attrname, old, new):
    """
    Callback function to dynamically update the drver distribution
    each time the user changes the value in the corresponding widget
    It is necessary to update the value of each data source as well
    """
    global source, source_sc, source_bar, cohorts_matrix, selected_input
    selected_input = new

    lifetime = smf.range_lifetime(lifetime_start, lifetime_end, stdev, len(smf.data['Time']))
    DSM = smf.compute_model(smf.data, lifetime, driver, input_type=selected_input)
    print("Input distribution", new)
    plot_data = {'time': DSM.t,
             'stock': DSM.s,
             'inflows': DSM.i,
             'outflows': DSM.o,
             'stock change': DSM.i - DSM.o
             }
    source.data = plot_data
    source_sc.data = {'image': [np.flipud(DSM.s_c)]}
    cohorts_matrix = pd.DataFrame(
        data=DSM.s_c,
        index=DSM.t.to_numpy(),
        columns=[str(e) for e in plot_data["time"].to_numpy().tolist()]
        )
    cohorts_matrix.insert(
        0, "time",[str(e) for e in plot_data["time"].to_numpy().tolist()])
    source_bar = ColumnDataSource(data=cohorts_matrix)

def update_lifetime_start(attrname, old, new):
    """
    Callback function to dynamically update the value of
    parameter defining the lifetime of the first cohort
    each time the user changes the value in the corresponding widget.
    It is necessary to update the value of each data source as well
    """
    global source, source_sc, source_bar, cohorts_matrix, lifetime_start
    lifetime_start = new
    print("Lifetime start", lifetime_start)
    lifetime = smf.range_lifetime(lifetime_start, lifetime_end, stdev, len(smf.data['Time']))

    DSM = smf.compute_model(smf.data, lifetime, driver, input_type=selected_input)

    plot_data = {'time': DSM.t,
             'stock': DSM.s,
             'inflows': DSM.i,
             'outflows': DSM.o,
             'stock change': DSM.i - DSM.o
             }
    source.data = plot_data
    lifetime_start = new
    source_sc.data = {'image': [np.flipud(DSM.s_c)]}
    cohorts_matrix = pd.DataFrame(
        data=DSM.s_c,
        index=DSM.t.to_numpy(),
        columns=[str(e) for e in plot_data["time"].to_numpy().tolist()]
        )
    cohorts_matrix.insert(
        0, "time",[str(e) for e in plot_data["time"].to_numpy().tolist()])
    source_bar = ColumnDataSource(data=cohorts_matrix)

def update_lifetime_end(attrname, old, new):
    """
    Callback function to dynamically update the value of
    parameter defining the lifetime of the last cohort
    each time the user changes the value in the corresponding widget.
    It is necessary to update the value of each data source as well
    """
    global source, source_sc, source_bar, cohorts_matrix, lifetime_end
    lifetime_end = new
    print("Lifetime end", lifetime_end)
    lifetime = smf.range_lifetime(lifetime_start, lifetime_end, stdev, len(smf.data['Time']))

    DSM = smf.compute_model(smf.data, lifetime, driver, input_type=selected_input)
    plot_data = {'time': DSM.t,
             'stock': DSM.s,
             'inflows': DSM.i,
             'outflows': DSM.o,
             'stock change': DSM.i - DSM.o
             }
    source.data = plot_data
    source_sc.data = {'image': [np.flipud(DSM.s_c)]}
    cohorts_matrix = pd.DataFrame(
        data=DSM.s_c,
        index=DSM.t.to_numpy(),
        columns=[str(e) for e in plot_data["time"].to_numpy().tolist()]
        )
    cohorts_matrix.insert(
        0, "time",[str(e) for e in plot_data["time"].to_numpy().tolist()])
    source_bar = ColumnDataSource(data=cohorts_matrix)

def update_stdev(attrname, old, new):
    """
    Callback function to dynamically update the value of
    the standard deviation parameter
    each time the user changes the value in the corresponding widget
    It is necessary to update the value of each data source as well
    """
    global source, source_sc, source_bar, cohorts_matrix, stdev
    stdev = new
    print("StDev", stdev)
    lifetime = smf.range_lifetime(lifetime_start, lifetime_end, stdev, len(smf.data['Time']))

    DSM = smf.compute_model(smf.data, lifetime, driver, input_type=selected_input)
    plot_data = {'time': DSM.t,
             'stock': DSM.s,
             'inflows': DSM.i,
             'outflows': DSM.o,
             'stock change': DSM.i - DSM.o
             }
    source.data = plot_data
    source_sc.data = {'image': [np.flipud(DSM.s_c)]}
    cohorts_matrix = pd.DataFrame(
        data=DSM.s_c,
        index=DSM.t.to_numpy(),
        columns=[str(e) for e in plot_data["time"].to_numpy().tolist()]
        )
    cohorts_matrix.insert(
        0, "time",[str(e) for e in plot_data["time"].to_numpy().tolist()])
    source_bar = ColumnDataSource(data=cohorts_matrix)

def make_line_plots(source):
    """
    Creates 3 line graphs of:
    1. Inflows and Outflows vs. Time
    2. Stock Change vs. Time
    3. Stock vs. Time
    one positionnal parameter: source, a column data source containing
    the information in plot_data from the Dynamic Stock Model
    """

    options = dict(plot_width=400, plot_height=300,  toolbar_location=None)

    # Inflows and outflows
    p1 = figure(title="Inflows and outflows",  **options)
    p1.circle("time", "inflows", color="green", source=source)
    p1.circle("time", "outflows",  line_color="red", fill_color=None, source=source)

    # Stock change
    p2 = figure(title="Stock change", **options)
    p2.circle("time", "stock change",  line_color="black", fill_color=None, source=source)

    # Stock plot
    p3 = figure(title="Stock",  **options)
    p3.circle("time", "stock", color="blue", source=source)

    return [p1, p2, p3]

def make_bar_plots(source_sc, source_bar):
    """
    Creates 2 graphs:
    1. Time-cohorts matrix (heatmap image)
    2. Stock Size and column composition (bar chart stacked by cohort)
    two positionnal parameters: source_sc, and source_bar,
    customs columndata source containing
    information about  the stock by time and cohort s_c
    from the Dynamic Stock Model
    """
    # Image plot of the Stock by tie and cohort matrix
    p4 = figure(x_range=(DSM.t[0], DSM.t[len(DSM.t)-1]),
            y_range=(DSM.t[len(DSM.t)-1], DSM.t[0]),
            y_axis_label='Time', x_axis_label='Cohorts',
            title='Time-Cohorts Matrix',
            plot_width=410, plot_height=300,
            min_border_right = 20,
            toolbar_location=None)

    p4.image("image", x=DSM.t[0], y=DSM.t[len(DSM.t)-1],
         dw=len(DSM.t), dh=len(DSM.t),
         source=source_sc, palette="Spectral11")

    stackers = [e for e in cohorts_matrix["time"].to_numpy().tolist()]
    palette = [Category20[20][i%19] for i in range(len(stackers))]

    p5 = figure(plot_width=790, plot_height=300, x_range=stackers,
            title = 'Stock size and cohort composition',
            y_axis_location="right", y_axis_label='Stock size',
            toolbar_location=None)
    p5.vbar_stack(stackers, x='time', width=0.9, color=palette,
                         source=source_bar)
    p5.xaxis.major_label_orientation = 3.14/2

    return [p4, p5]

# Defining sliders for lifetime and selection widget for the input distribution
driver_selection = Select(title="Driver", value="Inflow-driven",
                         options=['Inflow-driven','Stock-driven'], width=400)
input_selection = Select(title="Driver distribution", value="Constant",
                         options=smf.data.columns.tolist()[1:], width=400)
lifetime_param_start = Slider(start=2, end=50, value=15, step=1,
                        title="Mean lifetime of 1st cohort", width=400)
lifetime_param_end = Slider(start=2, end=50, value=15, step=1,
                        title="Mean lifetime of last cohort", width=400)
stdev_param = Slider(start=0.5, end=15, value=5, step=.1,
                     title="Standard dev.", width=400)

# Launching the  calbacks every time the parameters are modified
driver_selection.on_change("value", update_driver)
input_selection.on_change("value", update_input)
lifetime_param_start.on_change('value_throttled', update_lifetime_start)
lifetime_param_end.on_change('value_throttled', update_lifetime_end)
stdev_param.on_change('value_throttled', update_stdev)

# Preparing data sources for the plots

# Source used for the line plots
plot_data = {'time': DSM.t,
         'stock': DSM.s,
         'inflows': DSM.i,
         'outflows': DSM.o,
         'stock change': DSM.i - DSM.o
         }
source = ColumnDataSource(data=plot_data)

# Source used for the matrix plot
source_sc = ColumnDataSource(data={'image': [np.flipud(DSM.s_c)]})

# Data preparation for the bar chart of cohorts vs time
cohorts_matrix = pd.DataFrame(
     data=DSM.s_c,
     index=DSM.t.to_numpy(),
     columns=[str(e) for e in plot_data["time"].to_numpy().tolist()]
     )
cohorts_matrix.insert(
     0, "time",[str(e) for e in plot_data["time"].to_numpy().tolist()])
# Source used for the bar plot
source_bar = ColumnDataSource(data=cohorts_matrix)

# Putting the title and the widgets in a row container
widgets =column(row(Div(text="<h1> MFA Stock Dynamics Model</h1>", width=400,),
                    driver_selection, input_selection),
                row(lifetime_param_start, lifetime_param_end, stdev_param))

# Creating final layout and generating the plot
layout =column(
        widgets,
        row(make_line_plots(source)),
        row(make_bar_plots(source_sc, source_bar))
        )

curdoc().add_root(layout)
curdoc().title = " Dynamic MFA model"

reason: import numpy


text:
#!/usr/bin/env python

"""
.. See the NOTICE file distributed with this work for additional information
   regarding copyright ownership.

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
"""

from __future__ import print_function

import os
import json
import random
import h5py
import numpy as np

class GenerateSampleCoords(object):  # pylint: disable=too-few-public-methods

    """
    Generate a sample 3D models file if one does not exist
    """

    @staticmethod
    def main():  # pylint: disable=too-many-locals,too-many-statements
        """
        Main Function
        """

        resolution = 1000
        clusters = [0, 1, 2, 3, 4, 5, 6]
        clusters_hierarchy = [[0, 1, 2], [3, 4], [5], [6]]
        centroids = [1, 10, 100, 150, 200]
        chromosomes = ['chr1', 'chr2', 'chr3', 'chr4', 'chr5', 'chr6', 'X']

        for uuid in range(10):
            # Create the HDF5 file
            filename = os.path.join(os.path.dirname(__file__), "../tests/data/sample_coords.hdf5")
            hdf5_handle = h5py.File(filename, "a")

            if str(resolution) in hdf5_handle:
                grp = hdf5_handle[str(resolution)]
                dset = grp['data']

                meta = grp['meta']
                mpgrp = meta['model_params']
                clustersgrp = meta['clusters']
                centroidsgrp = meta['centroids']
            else:
                # Create the initial dataset with minimum values
                grp = hdf5_handle.create_group(str(resolution))
                meta = grp.create_group('meta')

                mpgrp = meta.create_group('model_params')
                clustersgrp = meta.create_group('clusters')
                centroidsgrp = meta.create_group('centroids')

                dset = grp.create_dataset(
                    'data',
                    (1, 1000, 3),
                    maxshape=(None, 1000, 3),
                    dtype='int32',
                    chunks=True,
                    compression="gzip"
                )

                dset.attrs['title'] = 'title'
                dset.attrs['experimentType'] = 'experimentType'
                dset.attrs['species'] = 'species'
                dset.attrs['project'] = 'project'
                dset.attrs['identifier'] = 'identifier'
                dset.attrs['assembly'] = 'assembly'
                dset.attrs['cellType'] = 'cellType'
                dset.attrs['resolution'] = 'resolution'
                dset.attrs['datatype'] = 'datatype'
                dset.attrs['components'] = 'components'
                dset.attrs['source'] = 'source'
                dset.attrs['dependencies'] = json.dumps({'test': 'test'})

            clustergrps = clustersgrp.create_group(str(uuid))
            ch_size = len(clusters_hierarchy)
            for cluster in range(ch_size):
                clustergrps.create_dataset(
                    str(cluster),
                    data=clusters_hierarchy[cluster],
                    chunks=True,
                    compression="gzip"
                )
            centroidsgrp.create_dataset(
                str(uuid),
                data=centroids,
                chunks=True,
                compression="gzip"
            )

            current_size = len(dset)
            if current_size == 1:
                current_size = 0

            model_size = random.randint(500, 2000)

            dset.resize((current_size + model_size, 1000, 3))

            dnp = np.zeros([model_size, 1000, 3], dtype='int32')

            model_param = []
            for ref in range(1000):
                cluster_id = random.choice(clusters)
                model_param.append([ref, cluster_id])

                for pos in range(model_size):
                    x_pos = random.randint(-1000, 1000)
                    y_pos = random.randint(-1000, 1000)
                    z_pos = random.randint(-1000, 1000)

                    dnp[pos][ref] = [x_pos, y_pos, z_pos]

            start = random.randint(1, 30000000)
            end = start + random.randint(5000, 100000)

            model_param_ds = mpgrp.create_dataset(str(uuid), data=model_param)

            model_param_ds.attrs['i'] = current_size
            model_param_ds.attrs['j'] = current_size + model_size
            model_param_ds.attrs['chromosome'] = random.choice(chromosomes)
            model_param_ds.attrs['start'] = start
            model_param_ds.attrs['end'] = end

            dset[current_size:current_size + model_size, 0:1000, 0:3] += dnp

            hdf5_handle.close()

if __name__ == '__main__':
    GSC = GenerateSampleCoords()
    GSC.main()

max_stars_repo_name: Multiscale-Genomics/mg-dm-api

max_issues_repo_name: Multiscale-Genomics/mg-dm-api

max_forks_repo_name: Multiscale-Genomics/mg-dm-api


text:
# -*- coding: utf-8 -*-

"""
作者：李狗嗨
链接：https://www.zhihu.com/question/466165757/answer/1954034386
来源：知乎
著作权归作者所有。商业转载请联系作者获得授权，非商业转载请注明出处。
"""

import numpy as np
from skimage import measure
import matplotlib.pyplot as plt

def fun(x, y, z):
    return (x ** 2 + (9 / 4) * y ** 2 + z ** 2 - 1) ** 3 - x ** 2 * z ** 3 - (1 / 9) * y ** 2 * z ** 3

x, y, z = np.mgrid[-2:2:100j, -2:2:100j, -2:2:100j]
vol = fun(x, y, z)
iso_val = 0.0
verts, faces, _, _ = measure.marching_cubes(vol, iso_val, spacing=(0.1, 0.1, 0.1))
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
ax.plot_trisurf(verts[:, 0], verts[:, 1], faces, verts[:, 2], color='red')
ax.view_init(25, -110)
plt.show()

max_stars_repo_name: richardmyu/PyDemo

max_issues_repo_name: richardmyu/PyDemo

max_forks_repo_name: richardmyu/PyDemo


text:
import xml.etree.ElementTree as ET
import pickle
import random
from collections import defaultdict
from nltk.corpus import wordnet as wn
import networkx as nx

def closure_graph(synset, fn):
    seen = set()
    graph = nx.DiGraph()

    def recurse(s, count):
        if not s in seen:
            seen.add(s)
            graph.add_node(s.name)
            for s1 in fn(s):
                graph.add_node(s1.name)
                graph.add_edge(s.name, s1.name)
                if count > 0: recurse(s1, count - 1)

    recurse(synset, 2)
    return seen

def extract_puns(pun_files):
    puns = defaultdict(lambda: None)

    for pun_file in pun_files:
        tree = ET.parse(pun_file)
        root = tree.getroot()

        for child in root:
            punID = child.attrib["id"]
            pun = []
            for subchild in child:
                pun.append(subchild.text)
            puns[punID] = pun

    return dict(puns)

def create_inverted_index(puns, mapping_files):
    inverted_index = defaultdict(lambda: None)
    for mapping_file in mapping_files:
        with open(mapping_file, 'r') as f:
            for line in f:
                punID, word = tuple(line.strip().split())
                wordID = int(word.split("_")[-1]) - 1
                the_word = puns[punID][wordID]
                if not inverted_index[the_word]:
                    punIDList = []
                    punIDList.append(punID)
                    inverted_index[the_word.lower()] = punIDList
                else:
                    inverted_index[the_word.lower()].append(punID)

    return dict(inverted_index)

def load_data():
    with open("inverted_index.idx", "rb") as f:
        inverted_index, puns = pickle.load(f)

    return inverted_index, puns

def get_similar(word):
    synsets = wn.synsets(word)

    if synsets == []: return []
    word = synsets[0]

    hyponyms = closure_graph(word,
                             lambda s: s.hyponyms())

    hypernyms = closure_graph(word,
                              lambda s: s.hypernyms())
    hypernyms.update(hyponyms)
    hypernyms = list(map(lambda h: h.lemma_names()[0], list(hypernyms)))

    return hypernyms

def generate_pun(phrase):
    inverted_index, puns = load_data()
    for word in phrase.strip().split(" "):
        similar = get_similar(word)
        random.shuffle(similar)
        similar.insert(0, word)
    for nym in similar:
            try:
                pun_ids = inverted_index[nym.lower()]
                return " ".join(puns[random.choice(pun_ids)])
            except:
                pass

    return "failed: NullPunterException"

def main():
    puns = extract_puns(['./semeval2017_task7/data/test/subtask2-heterographic-test.xml',
                     './semeval2017_task7/data/test/subtask2-homographic-test.xml'])
    inverted_index = create_inverted_index(puns, ['./semeval2017_task7/data/test/subtask2-heterographic-test.gold',
                                                 './semeval2017_task7/data/test/subtask2-homographic-test.gold'])

    with open("inverted_index.idx", "wb") as f:
        pickle.dump((inverted_index, dict(puns)), f)

    with open("inverted_index.idx", "rb") as f:
        inverted_index, puns = pickle.load(f)

    print(generate_pun("hat"))

if __name__ == "__main__":
    main()

reason: import networkx


text:
from typing import Any, List, Sequence, Tuple, Optional, Union

import cv2
import numpy as np

from _griffig import BoxData, Gripper, RobotPose, OrthographicImage
from pyaffx import Affine

def get_color_scale(dtype):
    if dtype == np.float32:
        return 1. / 255
    if dtype == np.uint16:
        return 255
    return 1

def crop(mat_image: Any, size_output: Sequence[float], vec=(0, 0)) -> Any:
    margin_x_lower = int(round((mat_image.shape[0] - size_output[0]) / 2 + vec[1]))
    margin_y_lower = int(round((mat_image.shape[1] - size_output[1]) / 2 + vec[0]))
    margin_x_upper = margin_x_lower + int(round(size_output[0]))
    margin_y_upper = margin_y_lower + int(round(size_output[1]))
    return mat_image[margin_x_lower:margin_x_upper, margin_y_lower:margin_y_upper]

def get_transformation(x: float, y: float, a: float, center: Sequence[float], scale=1.0, cropped: Sequence[float] = None):  # [rad]
    trans = cv2.getRotationMatrix2D((round(center[0] - x), round(center[1] - y)), a * 180.0 / np.pi, scale)  # [deg]
    trans[0][2] += x + scale * cropped[0] / 2 - center[0] if cropped else x
    trans[1][2] += y + scale * cropped[1] / 2 - center[1] if cropped else y
    return trans

def get_area_of_interest(
        image: OrthographicImage,
        pose: Affine,
        size_cropped: Tuple[float, float] = None,
        size_result: Tuple[float, float] = None,
        flags=cv2.INTER_LINEAR,
        return_mat=False,
        planar=True,
) -> Union[OrthographicImage, Any]:
    size_input = (image.mat.shape[1], image.mat.shape[0])
    center_image = (image.mat.shape[1] / 2, image.mat.shape[0] / 2)

    if size_result and size_cropped:
        scale = size_result[0] / size_cropped[0]
        assert scale == (size_result[1] / size_cropped[1])
    elif size_result:
        scale = size_result[0] / size_input[0]
        assert scale == (size_result[1] / size_input[1])
    else:
        scale = 1.0

    size_final = size_result or size_cropped or size_input

    if not planar:
        pose = image.pose.inverse() * pose

    trans = get_transformation(
        image.pixel_size * pose.y,
        image.pixel_size * pose.x,
        -pose.a,
        center_image,
        scale=scale,
        cropped=size_cropped,
    )
    mat_result = cv2.warpAffine(image.mat, trans, size_final, flags=flags, borderMode=cv2.BORDER_REPLICATE)  # INTERPOLATION_METHOD
    if return_mat:
        return mat_result

    image_pose = Affine(x=pose.x, y=pose.y, a=-pose.a) * image.pose

    return OrthographicImage(
        mat_result,
        scale * image.pixel_size,
        image.min_depth,
        image.max_depth,
        image.camera,
        image_pose,
    )

def get_inference_image(
        image: OrthographicImage,
        pose: Affine,
        size_cropped: Tuple[float, float],
        size_area_cropped: Tuple[float, float],
        size_area_result: Tuple[float, float],
        return_mat=False,
    ) -> OrthographicImage:
    size_input = (image.mat.shape[1], image.mat.shape[0])
    center_image = (size_input[0] / 2, size_input[1] / 2)

    scale = size_area_result[0] / size_area_cropped[0]

    trans = get_transformation(
        image.pixel_size * pose.y,
        image.pixel_size * pose.x,
        pose.a,
        center_image,
        scale=scale,
        cropped=(size_cropped[0] / scale, size_cropped[1] / scale),
    )
    mat_result = cv2.warpAffine(image.mat, trans, size_cropped, borderMode=cv2.BORDER_REPLICATE, flags=cv2.INTER_AREA)
    if return_mat:
        return mat_result

    image_pose = Affine(x=pose.x, y=pose.y, a=pose.a) * image.pose

    return OrthographicImage(
        mat_result,
        scale * image.pixel_size,
        image.min_depth,
        image.max_depth,
        image.camera,
        image_pose,
    )

def _get_rect_contour(center: Sequence[float], size: Sequence[float]) -> List[Sequence[float]]:
    return [
        [center[0] + size[0] / 2, center[1] + size[1] / 2, size[2]],
        [center[0] + size[0] / 2, center[1] - size[1] / 2, size[2]],
        [center[0] - size[0] / 2, center[1] - size[1] / 2, size[2]],
        [center[0] - size[0] / 2, center[1] + size[1] / 2, size[2]],
    ]

def get_box_projection(image: OrthographicImage, box_data: BoxData):
    if not box_data:
        return

    box_border = [Affine(*p) for p in box_data.contour]
    return [image.project(p) for p in box_border]

def draw_line(
        image: OrthographicImage,
        action_pose: Affine,
        pt1: Affine,
        pt2: Affine,
        color,
        thickness=1,
    ) -> None:
    cm = get_color_scale(image.mat.dtype)
    pose = image.pose.inverse() * action_pose
    pt1_projection = image.project(pose * pt1)
    pt2_projection = image.project(pose * pt2)
    cv2.line(image.mat, tuple(pt1_projection), tuple(pt2_projection), (color[0] * cm, color[1] * cm, color[2] * cm), thickness, lineType=cv2.LINE_AA)

def draw_polygon(
        image: OrthographicImage,
        action_pose: Affine,
        polygon,
        color,
        thickness=1,
    ) -> None:
    cm = get_color_scale(image.mat.dtype)
    pose = image.pose.inverse() * action_pose
    polygon_projection = np.asarray([tuple(image.project(pose * p)) for p in polygon])
    cv2.polylines(image.mat, [polygon_projection], True, (color[0] * cm, color[1] * cm, color[2] * cm), thickness, lineType=cv2.LINE_AA)

def draw_around_box(image: OrthographicImage, box_data: Optional[BoxData], draw_lines=False) -> None:
    if not box_data:
        return

    assert box_data, 'Box contour should be drawn, but is false.'

    box_border = [Affine(*p) for p in box_data.contour]
    image_border = [Affine(*p) for p in _get_rect_contour([0.0, 0.0, 0.0], [10.0, 10.0, box_data.contour[0][2]])]
    box_projection = [image.project(image.pose.inverse() * p) for p in box_border]

    cm = get_color_scale(image.mat.dtype)

    if draw_lines:
        number_channels = image.mat.shape[-1] if len(image.mat.shape) > 2 else 1

        color = np.array([255 * cm] * number_channels)  # White
        cv2.polylines(image.mat, [np.asarray(box_projection)], True, color, 2, lineType=cv2.LINE_AA)

    else:
        color_array = np.array([image.mat[np.clip(p[1], 0, image.mat.shape[0] - 1), np.clip(p[0], 0, image.mat.shape[1] - 1)] for p in box_projection], dtype=np.float32)
        if len(color_array.shape) > 1:
            color_array[np.mean(color_array, axis=1) < cm] = np.nan
        else:
            color_array[color_array < cm] = np.nan
        color = np.nanmean(color_array, axis=0)
        np.nan_to_num(color, copy=False)
        image_border_projection = [image.project(image.pose.inverse() * p) for p in image_border]
        cv2.fillPoly(image.mat, np.array([image_border_projection, box_projection]), color.tolist())

def draw_object(image: OrthographicImage, object_data) -> None:
    if 'polygon' not in object_data:
        return

    cm = get_color_scale(image.mat.dtype)
    polygon = np.asarray([(int(p['x'] * image.mat.shape[1]), int(p['y'] * image.mat.shape[0])) for p in object_data['polygon']])
    cv2.polylines(image.mat, [polygon], True, (0 * cm, 255 * cm, 255 * cm), 1, lineType=cv2.LINE_AA)

def draw_pose(image: OrthographicImage, action_pose: RobotPose, convert_to_rgb=False, calibration_pattern=False, border_thickness=2, line_thickness=1) -> None:
    if convert_to_rgb and image.mat.ndim == 2:
        image.mat = cv2.cvtColor(image.mat, cv2.COLOR_GRAY2RGB)

    color_rect = (255, 0, 0)  # Blue
    color_lines = (0, 0, 255)  # Red
    color_direction = (0, 255, 0)  # Green

    rect = [Affine(*p) for p in _get_rect_contour([0.0, 0.0, 0.0], [200.0 / image.pixel_size, 200.0 / image.pixel_size, 0.0])]

    draw_polygon(image, action_pose, rect, color_rect, 2)

    draw_line(image, action_pose, Affine(90 / image.pixel_size, 0), Affine(100 / image.pixel_size, 0), color_rect, border_thickness)
    draw_line(image, action_pose, Affine(0.012, action_pose.d / 2), Affine(-0.012, action_pose.d / 2), color_lines, line_thickness)
    draw_line(image, action_pose, Affine(0.012, -action_pose.d / 2), Affine(-0.012, -action_pose.d / 2), color_lines, line_thickness)
    draw_line(image, action_pose, Affine(0, action_pose.d / 2), Affine(0, -action_pose.d / 2), color_lines, line_thickness)
    draw_line(image, action_pose, Affine(0.006, 0), Affine(-0.006, 0), color_lines, line_thickness)
    draw_line(image, action_pose, Affine(z=0.14), Affine(), color_direction, line_thickness)

    if calibration_pattern:
        color_calibration = (255, 255, 255)
        draw_line(image, action_pose, Affine(0, -0.1), Affine(0, 0.1), color_calibration, line_thickness)
        draw_line(image, action_pose, Affine(-0.1, 0), Affine(0.1, 0), color_calibration, line_thickness)

def draw_around_box2(image, box_data: BoxData, draw_lines=False):
    if not box_data:
        return

    assert box_data, 'Box contour should be drawn, but is false.'

    box_border = [Affine(*p) for p in box_data.contour]
    image_border = [Affine(*p) for p in _get_rect_contour([0.0, 0.0, 0.0], [10.0, 10.0, box_data.contour[0][2]])]
    box_projection = [image.project(p) for p in box_border]

    cm = get_color_scale(image.mat.dtype)

    if draw_lines:
        number_channels = image.mat.shape[-1] if len(image.mat.shape) > 2 else 1
        color = np.array([255 * cm] * number_channels)  # White
        cv2.polylines(image.mat, [np.asarray(box_projection)], True, color, 2, lineType=cv2.LINE_AA)

    else:
        color_array = np.array([image.mat[np.clip(p[1], 0, image.mat.shape[0] - 1), np.clip(p[0], 0, image.mat.shape[1] - 1)] for p in box_projection], dtype=np.float32)
        if len(color_array.shape) > 1:
            color_array[np.mean(color_array, axis=1) < cm] = np.nan
        else:
            color_array[color_array < cm] = np.nan

        color = np.nanmean(color_array, axis=0)
        np.nan_to_num(color, copy=False)

        image_border_projection = [image.project(p) for p in image_border]
        cv2.fillPoly(image.mat, np.array([image_border_projection, box_projection]), color.tolist())

def draw_pose2(image, grasp, gripper: Gripper, convert_to_rgb=False):
    if convert_to_rgb and image.mat.ndim == 2:
        image.mat = cv2.cvtColor(image.mat, cv2.COLOR_GRAY2RGB)

    color_rect = (255, 0, 0)  # Blue
    color_lines = (0, 0, 255)  # Red
    color_direction = (0, 255, 0)  # Green

    rect = [Affine(*p) for p in _get_rect_contour([0.0, 0.0, 0.0], [200.0 / image.pixel_size, 200.0 / image.pixel_size, 0.0])]

    action_pose = RobotPose(grasp.pose, d=grasp.stroke)

    draw_polygon(image, action_pose, rect, color_rect, 2)
    draw_line(image, action_pose, Affine(90 / image.pixel_size, 0), Affine(100 / image.pixel_size, 0), color_rect, 2)

    half_width = gripper.finger_width / 2

    draw_line(image, action_pose, Affine(half_width, action_pose.d / 2), Affine(-half_width, action_pose.d / 2), color_lines, 1)
    draw_line(image, action_pose, Affine(half_width, -action_pose.d / 2), Affine(-half_width, -action_pose.d / 2), color_lines, 1)

    half_width_height = half_width + gripper.finger_height
    draw_line(image, action_pose, Affine(half_width_height, action_pose.d / 2), Affine(-half_width_height, action_pose.d / 2), color_lines, 1)
    draw_line(image, action_pose, Affine(half_width_height, -action_pose.d / 2), Affine(-half_width_height, -action_pose.d / 2), color_lines, 1)

    draw_line(image, action_pose, Affine(0, action_pose.d / 2), Affine(0, -action_pose.d / 2), color_lines, 1)
    draw_line(image, action_pose, Affine(0.006, 0), Affine(-0.006, 0), color_lines, 1)

    if np.isfinite(action_pose.z) and (action_pose.b != 0 or action_pose.c != 0):
        draw_line(image, action_pose, Affine(z=0.14), Affine(), color_direction, 1)

max_stars_repo_name: pantor/griffig

max_issues_repo_name: pantor/griffig

max_forks_repo_name: pantor/griffig


text:
# --------------
# Importing header files
import numpy as np
import pandas as pd
from scipy.stats import mode

import warnings
warnings.filterwarnings('ignore')

#Reading file
bank_data = pd.read_csv(path)

#Code starts here

# Checking the Categorical and Numerical variables in Bank Data
categorical_var = bank_data.select_dtypes(include='object')
print(categorical_var)

numerical_var = bank_data.select_dtypes(include='number')
print(numerical_var)

# Checking the missing values in Bank Data and filling with mode
banks = bank_data.drop(['Loan_ID'],axis=1)

banks.isnull().sum()

banks_mode = banks.mode().iloc[0]

banks.fillna(value = banks_mode, inplace=True)

banks.isnull().sum().values.sum()

# Checking the loan amount of an average person
avg_loan_amount = pd.pivot_table(banks,index=['Gender','Married','Self_Employed'],values='LoanAmount',aggfunc='mean')

# Checking the percentage of loan approved based on a person's employment type
loan_approved_se = len(banks[(banks['Self_Employed']=='Yes') & (banks['Loan_Status']=='Y')])
loan_approved_nse = len(banks[(banks['Self_Employed']=='No') & (banks['Loan_Status']=='Y')])

percentage_Se = loan_approved_se/614*100
percentage_nSe = loan_approved_nse/614*100

# Checking the applicants with long loan amount term
loan_term = banks['Loan_Amount_Term'].apply(lambda x: x/12)

big_loan_term = len(loan_term[loan_term >= 25])

# Checking average income of an applicant and the average loan given to a person based on income
loan_groupby = banks.groupby('Loan_Status')

loan_groupby = loan_groupby['ApplicantIncome','Credit_History']

mean_values = loan_groupby.mean()

#Code ends here

reason: import numpy,from scipy


text:
#!/usr/bin/env python
u"""
grace_months_index.py
Written by Tyler Sutterley (05/2021)

Creates a file with the start and end days for each dataset
Shows the range of each month for (CSR/GFZ/JPL) (RL04/RL05/RL06)
Shows which months are missing for each dataset as **missing**

INPUTS:
    base_dir: Working data directory for GRACE/GRACE-FO data

OPTIONS:
    DREL: GRACE/GRACE-FO data release (RL04, RL05, RL06)
    MODE: Permissions mode of output index file

OUTPUTS:
    GRACE_months.txt
    Column 1: GRACE Month
    Column 2: Calendar Month and Year
    Column 3: CSR RL06 Dates
    Column 4: GFZ RL06 Dates
    Column 5: GSFC v02.4 Mascon Dates
    Column 6: JPL RL06 Dates

COMMAND LINE OPTIONS:
    --help: list the command line options
    -D X, --directory X: Working GRACE/GRACE-FO data directory
    -r X, --release X: GRACE/GRACE-FO Data Releases to run (RL06)
    --mode X: permissions mode of output GRACE month file

PYTHON DEPENDENCIES:
    numpy: Scientific Computing Tools For Python (https://numpy.org)

UPDATE HISTORY:
    Updated 05/2021: define int/float precision to prevent deprecation warning
    Updated 10/2020: use argparse to set command line parameters
    Updated 09/2020: add column for GSFC v02.4 GRACE mascons
    Updated 07/2020: added function docstrings
    Updated 05/2020 for public release
    Updated 05-06/2018: GRACE release 6 (not all processing centers have RL06)
    Updated 01/2017: added MODE to set file and directory permissions
    Updated 05-06/2016: using __future__ print function. format month lines
    Updated 03/2016: using getopt to set RL04 parameter, added new help module
    Updated 10/2015: cleaned up and added a few comments
    Updated 11/2014: minor updates to code. added main definition
    Updated 10/2014: updated comments
    Updated 05/2014: added OPTION to not run RL04
    Updated 05/2013: added years to month label
    Written 07/2012
"""
from __future__ import print_function

import sys
import os
import argparse
import calendar
import numpy as np

def grace_months_index(base_dir, DREL=['RL06','v02.4'], MODE=None):
    """
    Creates a file with the start and end days for each dataset
    Shows the range of each month for (CSR/GFZ/JPL) (RL04/RL05/RL06)
    Shows which months are missing for each dataset as **missing**

    Arguments
    ---------
    base_dir: Working data directory for GRACE/GRACE-FO data

    Keyword arguments
    -----------------
    DREL: GRACE/GRACE-FO data release (RL04, RL05, RL06)
    MODE: Permissions mode of output index file
    """
    #-- Output GRACE months file
    grace_months_file = 'GRACE_months.txt'
    fid = open(os.path.join(base_dir,grace_months_file), 'w')

    #-- Initial parameters
    #-- processing centers
    PROC = ['CSR', 'GFZ', 'GSFC', 'JPL']
    #-- read from GSM datasets
    DSET = 'GSM'
    #-- maximum month of the datasets
    #-- checks for the maximum month between processing centers
    max_mon = 0
    #-- contain the information for each dataset
    var_info = {}

    #-- Looping through data releases first (all RL04 then all RL05)
    #-- for each considered data release (RL04,RL05)
    for rl in DREL:
        #-- for each processing centers (CSR, GFZ, JPL)
        for pr in PROC:
            #-- Setting the data directory for processing center and release
            grace_dir = os.path.join(base_dir, pr, rl, DSET)
            #-- read GRACE date ascii file
            #-- file created in read_grace.py or grace_dates.py
            grace_date_file = '{0}_{1}_DATES.txt'.format(pr,rl)
            if os.access(os.path.join(grace_dir,grace_date_file), os.F_OK):
                #-- skip the header line
                date_input = np.loadtxt(os.path.join(grace_dir,grace_date_file),
                    skiprows=1)
                #-- number of months
                nmon = np.shape(date_input)[0]

                #-- Setting the dictionary key e.g. 'CSR_RL04'
                var_name = '{0}_{1}'.format(pr,rl)

                #-- Creating a python dictionary for each dataset with parameters:
                #-- month #, start year, start day, end year, end day
                #-- Purpose is to get all of the dates loaded for each dataset
                #-- Adding data to dictionary for data processing and release
                var_info[var_name] = {}
                #-- allocate for output variables
                var_info[var_name]['mon'] = np.zeros((nmon),dtype=np.int64)
                var_info[var_name]['styr'] = np.zeros((nmon),dtype=np.int64)
                var_info[var_name]['stday'] = np.zeros((nmon),dtype=np.int64)
                var_info[var_name]['endyr'] = np.zeros((nmon),dtype=np.int64)
                var_info[var_name]['endday'] = np.zeros((nmon),dtype=np.int64)
                #-- place output variables in dictionary
                for i,key in enumerate(['mon','styr','stday','endyr','endday']):
                    #-- first column is date in decimal form (start at 1 not 0)
                    var_info[var_name][key] = date_input[:,i+1].astype(np.int64)
                #-- Finding the maximum month measured
                if (var_info[var_name]['mon'].max() > max_mon):
                    #-- if the maximum month in this dataset is greater
                    #-- than the previously read datasets
                    max_mon = np.int64(var_info[var_name]['mon'].max())

    #-- sort datasets alphanumerically
    var_name = sorted(var_info.keys())
    txt = ''.join(['{0:^21}'.format(d) for d in var_name])
    #-- printing header to file
    print('{0:^11}  {1}'.format('MONTH',txt),file=fid)

    #-- for each possible month
    #-- GRACE starts at month 004 (April 2002)
    #-- max_mon+1 to include max_mon
    for m in range(4, max_mon+1):
        #-- finding the month name e.g. Apr
        calendar_year = 2002 + (m-1)//12
        calendar_month = (m-1) % 12 + 1
        month_string = calendar.month_abbr[calendar_month]
        #-- create list object for output string
        output_string = []
        #-- for each processing center and data release
        for var in var_name:
            #-- find if the month of data exists
            #-- exists will be greater than 0 if there is a match
            exists = np.count_nonzero(var_info[var]['mon'] == m)
            if (exists != 0):
                #-- if there is a matching month
                #-- indice of matching month
                ind, = np.nonzero(var_info[var]['mon'] == m)
                #-- start date
                st_yr, = var_info[var]['styr'][ind]
                st_day, = var_info[var]['stday'][ind]
                #-- end date
                end_yr, = var_info[var]['endyr'][ind]
                end_day, = var_info[var]['endday'][ind]
                #-- output string is the date range
                #-- string format: 2002_102--2002_120
                output_string.append('{0:4d}_{1:03d}--{2:4d}_{3:03d}'.format(
                    st_yr, st_day, end_yr, end_day))
            else:
                #-- if there is no matching month = missing
                output_string.append(' ** missing **   ')

        #-- create single string with output string components
        #-- formatting the strings to be 20 characters in length
        data_string = ' '.join(['{0:>20}'.format(s) for s in output_string])
        #-- printing data line to file
        args = (m, month_string, calendar_year, data_string)
        print('{0:03d} {1:>3}{2:4d} {3}'.format(*args), file=fid)

    #-- close months file
    fid.close()
    #-- set the permissions level of the output file
    os.chmod(os.path.join(base_dir,grace_months_file), MODE)

#-- This is the main part of the program that calls the individual modules
def main():
    #-- Read the system arguments listed after the program
    parser = argparse.ArgumentParser(
        description="""Creates a file with the start and end days for
            each month of GRACE/GRACE-FO data
            """
    )
    #-- command line parameters
    #-- working data directory
    parser.add_argument('--directory','-D',
        type=lambda p: os.path.abspath(os.path.expanduser(p)),
        default=os.getcwd(),
        help='Working data directory')
    #-- GRACE/GRACE-FO data release
    parser.add_argument('--release','-r',
        metavar='DREL', type=str, nargs='+',
        default=['RL06','v02.4'],
        help='GRACE/GRACE-FO Data Release')
    #-- permissions mode of the local directories and files (number in octal)
    parser.add_argument('--mode','-M',
        type=lambda x: int(x,base=8), default=0o775,
        help='permissions mode of output files')
    args,_ = parser.parse_known_args()

    #-- run GRACE/GRACE-FO months program
    grace_months_index(args.directory, DREL=args.release, MODE=args.mode)

#-- run main program
if __name__ == '__main__':
    main()

max_stars_repo_name: hulecom/read-GRACE-harmonics

max_issues_repo_name: hulecom/read-GRACE-harmonics

max_forks_repo_name: hulecom/read-GRACE-harmonics


text:
import numpy as np
import sys

name = sys.argv[1]
dec  = int(sys.argv[2]) if len(sys.argv) >= 3 else 4

th = np.load('output/demo_th.npy', encoding='latin1').item()['layer{}'.format(name)].T
tf = np.load('output/tf_fea{}.npy'.format(str(name).zfill(2)), encoding='latin1')
if name == '25':
    tf = np.concatenate([tf, np.load('output/tf_fea26.npy', encoding='latin1')], 1)

print('Layer {}: tf.shape={}, th.shape={}'.format(name, tf.shape, th.shape))
print('TF:')
print(tf)
print('Torch:')
print(th)

size = tf.shape[0] if tf.shape[0] < th.shape[0] else th.shape[0]

print('Round to {} decimals'.format(dec))
tf = np.round(tf, decimals=dec)
th = np.round(th, decimals=dec)
print('Total Diff: {} Max Diff: {} Min Diff: {}'.format(
    np.sum(abs(tf[:size] - th[:size])), \
    np.max(tf[:size] - th[:size]), \
    np.min(tf[:size] - th[:size])))


text:
import os
import unittest
from io import StringIO
from numpy import array, allclose, cross
import numpy as np

import pyNastran
from pyNastran.bdf.bdf import BDF
from pyNastran.bdf.bdf import CORD2C, GRID, FORCE
from pyNastran.bdf.mesh_utils.loads import sum_forces_moments, sum_forces_moments_elements
model_path = os.path.join(pyNastran.__path__[0], '..', 'models')

log = None
class TestLoadSum(unittest.TestCase):
    def test_loads_sum_01(self):
        """tests FORCE"""
        model = BDF(log=log, debug=False)
        bdf_filename = os.path.join(model_path, 'solid_bending', 'solid_bending.bdf')
        model.read_bdf(bdf_filename)
        loadcase_id = 1
        #print("keys1", model.loads.keys())

        p0 = array([0., 0., 0.])
        F_expected = array([23000., 0., 0.])
        M_expected = array([0., 33209.869, -22803.951])
        eids = None
        nids = None
        F, M = sum_forces_moments(model, p0, loadcase_id, include_grav=False)
        F2, M2 = sum_forces_moments_elements(model, p0, loadcase_id, eids, nids, include_grav=False)
        assert np.allclose(F, F2), 'F=%s F2=%s' % (F, F2)
        assert np.allclose(M, M2), 'M=%s M2=%s' % (M, M2)

        self.assertTrue(allclose(F_expected, F), 'loadcase_id=%s F_expected=%s F=%s' % (loadcase_id, F_expected, F))
        self.assertTrue(allclose(M_expected, M), 'loadcase_id=%s M_expected=%s M=%s' % (loadcase_id, M_expected, M))

    def test_loads_sum_02(self):
        """tests FORCE"""
        model = BDF(log=log, debug=False)
        bdf_filename = os.path.join(model_path, 'sol_101_elements', 'static_solid_shell_bar.bdf')
        model.read_bdf(bdf_filename)
        loadcase_id = 10000
        #print("keys2", model.loads.keys())

        p0 = array([0., 0., 0.])
        F_expected = array([0., 0., 10000.])
        M_expected = array([5000., -5000., 0.])
        eids = None
        nids = None
        F, M = sum_forces_moments(model, p0, loadcase_id, include_grav=False)
        F2, M2 = sum_forces_moments_elements(model, p0, loadcase_id, eids, nids, include_grav=False)
        assert np.allclose(F, F2), 'F=%s F2=%s' % (F, F2)
        assert np.allclose(M, M2), 'M=%s M2=%s' % (M, M2)

        self.assertTrue(allclose(F_expected, F), 'loadcase_id=%s F_expected=%s F=%s' % (loadcase_id, F_expected, F))
        self.assertTrue(allclose(M_expected, M), 'loadcase_id=%s M_expected=%s M=%s' % (loadcase_id, M_expected, M))

        loadcase_id = 123458
        p0 = array([0., 0., 0.])
        F_expected = array([0., 0., 10000.])
        M_expected = array([5000., -5000., 0.])
        F, M = sum_forces_moments(model, p0, loadcase_id, include_grav=False)
        F2, M2 = sum_forces_moments_elements(model, p0, loadcase_id, eids, nids, include_grav=False)
        assert np.allclose(F, F2), 'F=%s F2=%s' % (F, F2)
        assert np.allclose(M, M2), 'M=%s M2=%s' % (M, M2)

        self.assertTrue(allclose(F_expected, F), 'loadcase_id=%s F_expected=%s F=%s' % (loadcase_id, F_expected, F))
        self.assertTrue(allclose(M_expected, M), 'loadcase_id=%s M_expected=%s M=%s' % (loadcase_id, M_expected, M))

    def test_loads_sum_03(self):
        """tests N/A"""
        if 0:  # pragma: no cover
            model = BDF(log=log, debug=False)
            bdf_filename = os.path.join(model_path, 'iSat', 'ISat_Launch_Sm_4pt.dat')
            model.read_bdf(bdf_filename)
            loadcase_id = 1
            #print("keys3", model.loads.keys())

            p0 = array([0., 0., 0.])
            F_expected = array([0., 0., 1.])
            M_expected = array([0., 0., 0.])
            eids = None
            nids = None
            F, M = sum_forces_moments(model, p0, loadcase_id, include_grav=False)
            F2, M2 = sum_forces_moments_elements(model, p0, loadcase_id, eids, nids, include_grav=False)
            assert np.allclose(F, F2), 'F=%s F2=%s' % (F, F2)
            assert np.allclose(M, M2), 'M=%s M2=%s' % (M, M2)

            self.assertTrue(allclose(F_expected, F), 'loadcase_id=%s F_expected=%s F=%s' % (loadcase_id, F_expected, F))
            self.assertTrue(allclose(M_expected, M), 'loadcase_id=%s M_expected=%s M=%s' % (loadcase_id, M_expected, M))

    def test_loads_sum_04(self):
        """
        tests:
          - 1=FORCE
          - 2=LOAD/FORCE
          - 3=LOAD/PLOAD4
          - 4=LOAD/PLOAD4
          - 5=LOAD/PLOAD4
          - 6=LOAD/PLOAD4
          - 10=PLOAD4
          - 11=PLOAD4
        """
        p0 = array([0., 0., 0.])
        model = BDF(log=log, debug=False)
        bdf_filename = os.path.join(model_path, 'plate', 'plate.bdf')
        #print(bdf_filename)
        model.read_bdf(bdf_filename)
        #print("keys4", model.loads.keys())

        loadcase_id = 1
        F_expected = array([600., 0., 0.])
        M_expected = array([0., 0., -3000.])
        eids = None
        nids = None
        F, M = sum_forces_moments(model, p0, loadcase_id, include_grav=False)
        F2, M2 = sum_forces_moments_elements(model, p0, loadcase_id, eids, nids, include_grav=False)
        assert np.allclose(F, F2), 'F=%s F2=%s' % (F, F2)
        assert np.allclose(M, M2), 'M=%s M2=%s' % (M, M2)
        self.assertTrue(allclose(F_expected, F), 'loadcase_id=%s F_expected=%s F=%s' % (loadcase_id, F_expected, F))
        self.assertTrue(allclose(M_expected, M), 'loadcase_id=%s M_expected=%s M=%s' % (loadcase_id, M_expected, M))

        loadcase_id = 2
        F_expected = array([600., 0., 0.])
        M_expected = array([0., 0., -3000.])
        F, M = sum_forces_moments(model, p0, loadcase_id, include_grav=False)
        self.assertTrue(allclose(F_expected, F), 'loadcase_id=%s F_expected=%s F=%s' % (loadcase_id, F_expected, F))
        self.assertTrue(allclose(M_expected, M), 'loadcase_id=%s M_expected=%s M=%s' % (loadcase_id, M_expected, M))

        #---------
        loadcase_id = 3
        A = 0.
        for e, element in model.elements.items():
            A += element.Area()
        A_expected = 100.
        self.assertTrue(allclose(A, A_expected), 'loadcase_id=%s A_expected=%s A=%s' % (loadcase_id, A_expected, A))
        p = 3.
        Fi = p * A

        eids = None
        nids = None
        F, M = sum_forces_moments(model, p0, loadcase_id, include_grav=False)
        F2, M2 = sum_forces_moments_elements(model, p0, loadcase_id, eids, nids, include_grav=False)
        assert np.allclose(F, F2), 'F=%s F2=%s' % (F, F2)
        assert np.allclose(M, M2), 'M=%s M2=%s' % (M, M2)

        self.assertTrue(allclose(p*A, F[2]), 'loadcase_id=%s p*A=%s F=%s' % (loadcase_id, p*A, F))

        F_expected = array([0., 0., 300.])
        M_expected = array([1500., -1500., 0.])
        self.assertTrue(allclose(F_expected, F), 'loadcase_id=%s F_expected=%s F=%s' % (loadcase_id, F_expected, F))
        self.assertTrue(allclose(M_expected, M), 'loadcase_id=%s M_expected=%s M=%s' % (loadcase_id, M_expected, M))

        #---
        loadcase_id = 10
        F, M = sum_forces_moments(model, p0, loadcase_id, include_grav=False)
        F2, M2 = sum_forces_moments_elements(model, p0, loadcase_id, eids, nids, include_grav=False)
        assert np.allclose(F, F2), 'F=%s F2=%s' % (F, F2)
        assert np.allclose(M, M2), 'M=%s M2=%s' % (M, M2)
        self.assertTrue(allclose(F_expected, F), 'loadcase_id=%s F_expected=%s F=%s' % (loadcase_id, F_expected, F))
        self.assertTrue(allclose(M_expected, M), 'loadcase_id=%s M_expected=%s M=%s' % (loadcase_id, M_expected, M))

        #---
        loadcase_id = 4
        F_expected = array([0., 0., 300.])
        M_expected = array([1500., -1500., 0.])
        F_expected *= 5.
        M_expected *= 5.
        F, M = sum_forces_moments(model, p0, loadcase_id, include_grav=False)
        F2, M2 = sum_forces_moments_elements(model, p0, loadcase_id, eids, nids, include_grav=False)
        assert np.allclose(F, F2), 'F=%s F2=%s' % (F, F2)
        assert np.allclose(M, M2), 'M=%s M2=%s' % (M, M2)
        #print('F =', F)
        self.assertTrue(allclose(F_expected, F), 'loadcase_id=%s F_expected=%s F=%s' % (loadcase_id, F_expected, F))
        self.assertTrue(allclose(M_expected, M), 'loadcase_id=%s M_expected=%s M=%s' % (loadcase_id, M_expected, M))

        loadcase_id = 5
        F, M = sum_forces_moments(model, p0, loadcase_id, include_grav=False)
        F2, M2 = sum_forces_moments_elements(model, p0, loadcase_id, eids, nids, include_grav=False)
        assert np.allclose(F, F2), 'F=%s F2=%s' % (F, F2)
        assert np.allclose(M, M2), 'M=%s M2=%s' % (M, M2)
        F_expected = array([0., 0., 300.])
        M_expected = array([1500., -1500., 0.])
        F_expected *= 7.
        M_expected *= 7.
        self.assertTrue(allclose(F_expected, F), 'loadcase_id=%s F_expected=%s F=%s' % (loadcase_id, F_expected, F))
        self.assertTrue(allclose(M_expected, M), 'loadcase_id=%s M_expected=%s M=%s' % (loadcase_id, M_expected, M))

        loadcase_id = 6
        F, M = sum_forces_moments(model, p0, loadcase_id, include_grav=False)
        F2, M2 = sum_forces_moments_elements(model, p0, loadcase_id, eids, nids, include_grav=False)
        assert np.allclose(F, F2), 'F=%s F2=%s' % (F, F2)
        assert np.allclose(M, M2), 'M=%s M2=%s' % (M, M2)
        F_expected = array([0., 0., 300.])
        M_expected = array([1500., -1500., 0.])
        F_expected *= 7. * 5.
        M_expected *= 7. * 5.
        self.assertTrue(allclose(F_expected, F), 'loadcase_id=%s F_expected=%s F=%s' % (loadcase_id, F_expected, F))
        self.assertTrue(allclose(M_expected, M), 'loadcase_id=%s M_expected=%s M=%s' % (loadcase_id, M_expected, M))

        #---------
        loadcase_id = 11
        A_expected = 4.
        A = 4.
        p = 3.
        Fi = p * A
        element = model.elements[1]
        normal = element.Normal()
        normal_expected = array([0., 0., 1.])
        self.assertTrue(allclose(normal_expected, normal), 'loadcase_id=%s normal_expected=%s normal=%s' % (loadcase_id, normal_expected, normal))
        F, M = sum_forces_moments(model, p0, loadcase_id, include_grav=False)
        F2, M2 = sum_forces_moments_elements(model, p0, loadcase_id, eids, nids, include_grav=False)
        assert np.allclose(F, F2), 'F=%s F2=%s' % (F, F2)
        assert np.allclose(M, M2), 'M=%s M2=%s' % (M, M2)

        self.assertTrue(allclose(p*A, F[2]), 'loadcase_id=%s p*A=%s F=%s' % (loadcase_id, p*A, F))

        F_expected = array([0., 0., 12.])
        M_expected = array([12., -12., 0.])
        self.assertTrue(allclose(F_expected, F), 'loadcase_id=%s F_expected=%s F=%s' % (loadcase_id, F_expected, F))
        self.assertTrue(allclose(M_expected, M), 'loadcase_id=%s M_expected=%s M=%s' % (loadcase_id, M_expected, M))

    def test_loads_sum_05(self):
        """
        tests:
         - 1=LOAD/PLOAD4
         - 2=LOAD/PLOAD4/FORCE
         - 5=PLOAD4
         - 6=PLOAD4
         - 1001=PLOAD4
         - 1002=1002
         - 1003=PLOAD
        """
        model = BDF(log=log, debug=False)
        bdf_filename = os.path.join(model_path, 'real', 'loads', 'loads.bdf')
        model.read_bdf(bdf_filename)

        p = 3.
        A = 1.
        n = array([0., 0., 1.])
        F1001_expected = p * A * n
        r = array([0.5, 1.5, 0.])
        p0 = array([0., 0., 0.])
        M1001_expected = cross(r, F1001_expected)

        loadcase_id = 1001
        eids = None
        nids = None
        F, M = sum_forces_moments(model, p0, loadcase_id, include_grav=False)
        F2, M2 = sum_forces_moments_elements(model, p0, loadcase_id, eids, nids, include_grav=False)
        assert np.allclose(F, F2), 'F=%s F2=%s' % (F, F2)
        assert np.allclose(M, M2), 'M=%s M2=%s' % (M, M2)
        self.assertTrue(allclose(F1001_expected, F), 'loadcase_id=%s F_expected=%s F=%s' % (loadcase_id, F1001_expected, F))
        self.assertTrue(allclose(M1001_expected, M), 'loadcase_id=%s M_expected=%s M=%s' % (loadcase_id, M1001_expected, M))

        loadcase_id = 1002
        r = array([4., 2., 0.])
        F1002_expected = array([0., 0., 1.])
        M1002_expected = cross(r, F1002_expected)
        F, M = sum_forces_moments(model, p0, loadcase_id, include_grav=False)
        F2, M2 = sum_forces_moments_elements(model, p0, loadcase_id, eids, nids, include_grav=False)
        self.assertTrue(allclose(F1002_expected, F), 'loadcase_id=%s F_expected=%s F=%s' % (loadcase_id, F1002_expected, F))
        self.assertTrue(allclose(M1002_expected, M), 'loadcase_id=%s M_expected=%s M=%s' % (loadcase_id, M1002_expected, M))

        loadcase_id = 1
        F, M = sum_forces_moments(model, p0, loadcase_id, include_grav=False)
        F2, M2 = sum_forces_moments_elements(model, p0, loadcase_id, eids, nids, include_grav=False)
        assert np.allclose(F, F2), 'F=%s F2=%s' % (F, F2)
        assert np.allclose(M, M2), 'M=%s M2=%s' % (M, M2)
        self.assertTrue(allclose(F1001_expected, F), 'loadcase_id=%s F_expected=%s F=%s' % (loadcase_id, F1001_expected, F))
        self.assertTrue(allclose(M1001_expected, M), 'loadcase_id=%s M_expected=%s M=%s' % (loadcase_id, M1001_expected, M))

        loadcase_id = 2
        F2_expected = F1001_expected + F1002_expected
        M2_expected = M1001_expected + M1002_expected
        F, M = sum_forces_moments(model, p0, loadcase_id, include_grav=False)
        F2, M2 = sum_forces_moments_elements(model, p0, loadcase_id, eids, nids, include_grav=False)
        assert np.allclose(F, F2), 'F=%s F2=%s' % (F, F2)
        assert np.allclose(M, M2), 'M=%s M2=%s' % (M, M2)
        self.assertTrue(allclose(F2_expected, F), 'loadcase_id=%s F_expected=%s F=%s' % (loadcase_id, F2_expected, F))
        self.assertTrue(allclose(M2_expected, M), 'loadcase_id=%s M_expected=%s M=%s' % (loadcase_id, M2_expected, M))

        F6_expected = 2. * (3. * F1001_expected + 13. * F1002_expected)
        M6_expected = 2. * (3. * M1001_expected + 13. * M1002_expected)

        F7_expected = 7. * 11. * F6_expected
        M7_expected = 7. * 11. * M6_expected
        if 0:  # pragma: no cover
            loadcase_id = 6
            F, M = sum_forces_moments(model, p0, loadcase_id, include_grav=False)
            F2, M2 = sum_forces_moments_elements(model, p0, loadcase_id, eids, nids, include_grav=False)
            assert np.allclose(F, F2), 'F=%s F2=%s' % (F, F2)
            assert np.allclose(M, M2), 'M=%s M2=%s' % (M, M2)
            self.assertTrue(allclose(F6_expected, F), 'loadcase_id=%s F_expected=%s F=%s' % (loadcase_id, F6_expected, F))
            self.assertTrue(allclose(M6_expected, M), 'loadcase_id=%s M_expected=%s M=%s' % (loadcase_id, M6_expected, M))

            loadcase_id = 7
            F, M = sum_forces_moments(model, p0, loadcase_id, include_grav=False)
            F2, M2 = sum_forces_moments_elements(model, p0, loadcase_id, eids, nids, include_grav=False)
            assert np.allclose(F, F2), 'F=%s F2=%s' % (F, F2)
            assert np.allclose(M, M2), 'M=%s M2=%s' % (M, M2)
            self.assertTrue(allclose(F7_expected, F), 'loadcase_id=%s F_expected=%s F=%s' % (loadcase_id, F7_expected, F))
            self.assertTrue(allclose(M7_expected, M), 'loadcase_id=%s M_expected=%s M=%s' % (loadcase_id, M7_expected, M))

        loadcase_id = 5
        p = 2.
        A = 1.
        n = array([0., 1., 1.]) / np.sqrt(2.)
        F5_expected = p * A * n
        r = array([0.5, 0.5, 0.])
        M5_expected = cross(r, F5_expected)
        F, M = sum_forces_moments(model, p0, loadcase_id, include_grav=False)
        F2, M2 = sum_forces_moments_elements(model, p0, loadcase_id, eids, nids, include_grav=False)
        assert np.allclose(F, F2), 'F=%s F2=%s' % (F, F2)
        assert np.allclose(M, M2), 'M=%s M2=%s' % (M, M2)
        self.assertTrue(allclose(F5_expected, F), 'loadcase_id=%s F_expected=%s F=%s' % (loadcase_id, F5_expected, F))
        self.assertTrue(allclose(M5_expected, M), 'loadcase_id=%s M_expected=%s M=%s' % (loadcase_id, M5_expected, M))
        #print('loadcase_id=%s F=%s M=%s' % (loadcase_id, F, M))

        loadcase_id = 6
        p = 2.
        A = 1.
        n = array([0., 0., 0.5]) / 0.5
        F6_expected = p * A * n
        r = array([0.5, 0.5, 0.])
        M6_expected = cross(r, F6_expected)
        F, M = sum_forces_moments(model, p0, loadcase_id, include_grav=False)
        F2, M2 = sum_forces_moments_elements(model, p0, loadcase_id, eids, nids, include_grav=False)
        assert np.allclose(F, F2), 'F=%s F2=%s' % (F, F2)
        assert np.allclose(M, M2), 'M=%s M2=%s' % (M, M2)
        self.assertTrue(allclose(F6_expected, F), 'loadcase_id=%s F_expected=%s F=%s' % (loadcase_id, F6_expected, F))
        self.assertTrue(allclose(M6_expected, M), 'loadcase_id=%s M_expected=%s M=%s' % (loadcase_id, M6_expected, M))
        #print('loadcase_id=%s F=%s M=%s' % (loadcase_id, F, M))

        loadcase_id = 1003
        p = 9.
        A = 1.
        n = array([0., 0., 1.])
        F1003_expected = p * A * n
        r = array([0.5, 0.5, 0.])
        M1003_expected = cross(r, F1003_expected)
        F, M = sum_forces_moments(model, p0, loadcase_id, include_grav=False)
        F2, M2 = sum_forces_moments_elements(model, p0, loadcase_id, eids, nids, include_grav=False)
        assert np.allclose(F, F2), 'F=%s F2=%s' % (F, F2)
        assert np.allclose(M, M2), 'M=%s M2=%s' % (M, M2)
        self.assertTrue(allclose(F1003_expected, F), 'loadcase_id=%s F_expected=%s F=%s' % (loadcase_id, F1003_expected, F))
        self.assertTrue(allclose(M1003_expected, M), 'loadcase_id=%s M_expected=%s M=%s' % (loadcase_id, M1003_expected, M))

        loadcase_id = 8
        F8_expected = 2. * (3. * F7_expected + 2. * F1003_expected)
        M8_expected = 2. * (3. * M7_expected + 2. * M1003_expected)
        if 0:  # pragma: no cover
            F, M = sum_forces_moments(model, p0, loadcase_id, include_grav=False)
            F2, M2 = sum_forces_moments_elements(model, p0, loadcase_id, eids, nids, include_grav=False)
            assert np.allclose(F, F2), 'F=%s F2=%s' % (F, F2)
            assert np.allclose(M, M2), 'M=%s M2=%s' % (M, M2)
            self.assertTrue(allclose(F8_expected, F), 'loadcase_id=%s F_expected=%s F=%s' % (loadcase_id, F8_expected, F))
            self.assertTrue(allclose(M8_expected, M), 'loadcase_id=%s M_expected=%s M=%s' % (loadcase_id, M8_expected, M))

        loadcase_id = 800
        p = 3.5
        A = 1.
        n = array([0., 0., 1.])
        F800_expected = p * A * n
        r = array([3.5, 1.5, 0.])
        M800_expected = cross(r, F800_expected)
        if 0:  # pragma: no cover
            F, M = sum_forces_moments(model, p0, loadcase_id, include_grav=False)
            F2, M2 = sum_forces_moments_elements(model, p0, loadcase_id, eids, nids, include_grav=False)
            assert np.allclose(F, F2), 'F=%s F2=%s' % (F, F2)
            assert np.allclose(M, M2), 'M=%s M2=%s' % (M, M2)
            self.assertTrue(allclose(F800_expected, F), 'loadcase_id=%s F_expected=%s F=%s' % (loadcase_id, F800_expected, F))
            self.assertTrue(allclose(M800_expected, M), 'loadcase_id=%s M_expected=%s M=%s' % (loadcase_id, M800_expected, M))

        loadcase_id = 801
        F801_expected = F800_expected
        M801_expected = M800_expected
        if 0:  # pragma: no cover
            F, M = sum_forces_moments(model, p0, loadcase_id, include_grav=False)
            F2, M2 = sum_forces_moments_elements(model, p0, loadcase_id, eids, nids, include_grav=False)
            assert np.allclose(F, F2), 'F=%s F2=%s' % (F, F2)
            assert np.allclose(M, M2), 'M=%s M2=%s' % (M, M2)
            self.assertTrue(allclose(F801_expected, F), 'loadcase_id=%s F_expected=%s F=%s' % (loadcase_id, F801_expected, F))
            self.assertTrue(allclose(M801_expected, M), 'loadcase_id=%s M_expected=%s M=%s' % (loadcase_id, M801_expected, M))

        loadcase_id = 802
        p = 3.5
        A = 0.5
        n = array([0., 0., 1.])
        F802_expected = p * A * n
        rx = (3. + 4. + 4.) / 3.
        ry = (1. + 1. + 2.) / 3.
        r = array([rx, ry, 0.])
        M802_expected = cross(r, F802_expected)
        if 0:  # pragma: no cover
            F, M = sum_forces_moments(model, p0, loadcase_id, include_grav=False)
            F2, M2 = sum_forces_moments_elements(model, p0, loadcase_id, eids, nids, include_grav=False)
            assert np.allclose(F, F2), 'F=%s F2=%s' % (F, F2)
            assert np.allclose(M, M2), 'M=%s M2=%s' % (M, M2)
            self.assertTrue(allclose(F802_expected, F), 'loadcase_id=%s F_expected=%s F=%s' % (loadcase_id, F802_expected, F))
            self.assertTrue(allclose(M802_expected, M), 'loadcase_id=%s M_expected=%s M=%s' % (loadcase_id, M802_expected, M))
        bdf_file = StringIO()
        model.write_bdf(bdf_file, close=False)
        bdf_file.seek(0)
        model.write_bdf(bdf_file, size=16)

    def _test_loads_sum_06(self):
        model = BDF(log=log, debug=False)
        bdf_filename = os.path.join(model_path, 'real', 'loads', 'bars.bdf')
        model.read_bdf(bdf_filename)
        p0 = array([0., 0., 0.])

        loadcase_id = 1
        F, M = sum_forces_moments(model, p0, loadcase_id, include_grav=False)
        F2, M2 = sum_forces_moments_elements(model, p0, loadcase_id, eids, nids, include_grav=False)
        assert np.allclose(F, F2), 'F=%s F2=%s' % (F, F2)
        assert np.allclose(M, M2), 'M=%s M2=%s' % (M, M2)
        if 0:  # pragma: no cover
            r = array([0., 0., 0.])
            F1_expected = array([0., 0., 1.])
            M1_expected = cross(r, F1_expected)

            F, M = sum_forces_moments(model, p0, loadcase_id, include_grav=False)
            F2, M2 = sum_forces_moments_elements(model, p0, loadcase_id, eids, nids, include_grav=False)
            assert np.allclose(F, F2), 'F=%s F2=%s' % (F, F2)
            assert np.allclose(M, M2), 'M=%s M2=%s' % (M, M2)
            self.assertTrue(allclose(F1_expected, F), 'loadcase_id=%s F_expected=%s F=%s' % (loadcase_id, F1_expected, F))
            self.assertTrue(allclose(M1_expected, M), 'loadcase_id=%s M_expected=%s M=%s' % (loadcase_id, M1_expected, M))

    def test_loads_sum_radial_01(self):
        model = BDF(debug=False)
        model.nodes[1] = GRID(1, cp=1, xyz=[0., 0., 0.], cd=0, ps='', seid=0,
                              comment='')
        cid = 1
        origin = [0., 0., 0.]
        zaxis = [0., 0., 1.]
        xaxis = [1., 0., 0.]
        model.add_cord2c(cid, rid=0, origin=origin, zaxis=zaxis, xzplane=xaxis,
                         comment='')

        sid = 1
        node = 1
        cid = 1
        mag = 1.1
        xyz = [1., 0., 0.]
        radial_force = model.add_force(sid, node, mag, xyz, cid=cid, comment='')

        sid = 2
        xyz = [1., 90., 0.]
        mag = 2.2
        theta_force = model.add_force(sid, node, mag, xyz, cid=cid, comment='')
        model.cross_reference()

        p0 = 1
        eids = None
        nids = None

        loadcase_id = 1
        F, M = sum_forces_moments(model, p0, loadcase_id, include_grav=False,
                                        xyz_cid0=None)
        F2, M2 = sum_forces_moments_elements(model, p0, loadcase_id, eids, nids,
                                                   include_grav=False,
                                                   xyz_cid0=None)
        assert np.allclose(F, F2), 'F=%s F2=%s' % (F, F2)
        assert np.allclose(M, M2), 'M=%s M2=%s' % (M, M2)

        F1_expected = np.array([1.1, 0., 0.])
        M1_expected = np.array([0., 0., 0.])
        self.assertTrue(allclose(F1_expected, F), 'loadcase_id=%s F_expected=%s F=%s' % (loadcase_id, F1_expected, F))
        self.assertTrue(allclose(M1_expected, M), 'loadcase_id=%s M_expected=%s M=%s' % (loadcase_id, M1_expected, M))

        loadcase_id = 2
        F, M = sum_forces_moments(model, p0, loadcase_id, include_grav=False,
                                  xyz_cid0=None)
        F2, M2 = sum_forces_moments_elements(model, p0, loadcase_id, eids, nids,
                                             include_grav=False,
                                             xyz_cid0=None)
        assert np.allclose(F, F2), 'F=%s F2=%s' % (F, F2)
        assert np.allclose(M, M2), 'M=%s M2=%s' % (M, M2)
        F2_expected = np.array([0., 2.2, 0.])
        M2_expected = np.array([0., 0., 0.])
        self.assertTrue(allclose(F2_expected, F), 'loadcase_id=%s F_expected=%s F=%s' % (loadcase_id, F2_expected, F))
        self.assertTrue(allclose(M2_expected, M), 'loadcase_id=%s M_expected=%s M=%s' % (loadcase_id, M2_expected, M))

if __name__ == '__main__':  # pragma: no cover
    unittest.main()

max_stars_repo_name: numenic/pyNastran

max_issues_repo_name: numenic/pyNastran

max_forks_repo_name: numenic/pyNastran

reason: import numpy,from numpy


text:
import cv2
import numpy as np

"""
follow this article to install prerequisites
https://nitratine.net/blog/post/python-face-recognition-tutorial/
because you have to install cmake and visual studio c++ build tools
"""
# import face_recognition

# imgAnna = face_recognition.load_image_file("../r")

max_stars_repo_path: scripts/compare human faces and tell is it the same person.py

max_stars_repo_name: salemalem/opencv-python-projects

max_issues_repo_name: salemalem/opencv-python-projects

max_forks_repo_name: salemalem/opencv-python-projects


text:
# An example of using the Expectation Maximization (EM) algorithm to find the
# parameters for a mixture of gaussian given a set of samples.

from numpy import *
from matplotlib.pylab import *
from pymote import gmm

from em import expectation_maximization

def iter_plot(cen_lst, cov_lst, itr):
    # For plotting EM progress
    if itr % 2 == 0:
        for i in range(len(cen_lst)):
            x,y = gmm.gauss_ellipse_2d(cen_lst[i], cov_lst[i])
            plot(x, y, 'k', linewidth=0.5)

seed(1)
mc = [0.4, 0.4, 0.2] # Mixing coefficients
centroids = [ array([0,0]), array([3,3]), array([0,4]) ]
ccov = [ array([[1,0.4],[0.4,1]]), diag((1,2)), diag((0.4,0.1)) ]

# Generate samples from the gaussian mixture model
X = gmm.sample_gaussian_mixture(centroids, ccov, mc, samples=1000)
fig1 = figure()
plot(X[:,0], X[:,1], '.')

# Expectation-Maximization of Mixture of Gaussians
cen_lst, cov_lst, p_k, logL, p_nk = gmm.em_gm(X, K=5, max_iter=400, verbose=False)
print "Log likelihood (how well the data fits the model) = ", logL

print p_k
print cen_lst[0]
print cov_lst
print p_nk

#print expectation_maximization(X, nbclusters=5, nbiter=6, epsilon=3)

# Plot the cluster ellipses
for i in range(len(cen_lst)):
    x1,x2 = gmm.gauss_ellipse_2d(cen_lst[i], cov_lst[i])
    plot(x1, x2, 'k', linewidth=2)
title(""); xlabel(r'$x_1$'); ylabel(r'$x_2$')

# Now we will find the conditional distribution of x given y
fig2 = figure()
ax1 = subplot(111)
plot(X[:,0], X[:,1], ',')
y = -1.0
axhline(y)
x1plt = np.linspace(axis()[0], axis()[1], 200)
for i in range(len(cen_lst)):
    text(cen_lst[i][0], cen_lst[i][1], str(i+1), horizontalalignment='center',
        verticalalignment='center', size=32, color=(0.2,0,0))
    ex,ey = gmm.gauss_ellipse_2d(cen_lst[i], cov_lst[i])
    plot(ex, ey, 'k', linewidth=0.5)
ax2 = twinx()
(con_cen, con_cov, new_p_k) = gmm.cond_dist(np.array([np.nan, y]), \
        cen_lst, cov_lst, p_k)
x2plt = gmm.gmm_pdf(c_[x1plt], con_cen, con_cov, new_p_k)
ax2.plot(x1plt, x2plt,'r', linewidth=2,
     label='Cond. dist. of $x_1$ given $x_2='+str(y)+'$')
ax2.legend()
ax1.set_xlabel(r'$x_1$')
ax1.set_ylabel(r'$x_2$')
ax2.set_ylabel('Probability')

show()

max_stars_repo_name: chinmaydd/RouteSense

max_issues_repo_name: chinmaydd/RouteSense

max_forks_repo_name: chinmaydd/RouteSense

reason: from numpy


text:
# Copyright (c) 2021 PaddlePaddle Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

from auto_scan_test import PassAutoScanTest, SkipReasons
from program_config import TensorConfig, ProgramConfig, OpConfig
import numpy as np
import paddle.inference as paddle_infer
from functools import partial
from typing import Optional, List, Callable, Dict, Any, Set
import unittest

import hypothesis
from hypothesis import given, settings, seed, example, assume
import hypothesis.strategies as st
from functools import reduce

class TestRepeatedFcReluFusePass(PassAutoScanTest):
    def is_program_valid(self, program_config: ProgramConfig) -> bool:
        return True

    def sample_program_config(self, draw):
        x_col = draw(st.sampled_from([1]))
        y_col = draw(st.sampled_from([1]))
        axis = draw(st.sampled_from([-1, 1]))
        batch_size = draw(st.integers(min_value=1, max_value=4))
        dim = draw(st.sampled_from([32, 64, 128]))

        def generate_input():
            return np.random.random([batch_size, dim]).astype(np.float32)

        def generate_weight(shape):
            return np.random.random(shape).astype(np.float32)

        attrs = [{
            "x_col": x_col,
            "y_col": y_col
        }, {
            "axis": axis
        }, {
            'batch_size': batch_size,
            'dim': dim
        }]

        mul_op1 = OpConfig(
            type="mul",
            inputs={"X": ["input_data"],
                    "Y": ["mul1_weight"]},
            outputs={"Out": ["mul1_output"]},
            attrs={"x_num_col_dims": x_col,
                   "y_num_col_dims": y_col})

        elt_op1 = OpConfig(
            type="elementwise_add",
            inputs={"X": ["mul1_output"],
                    "Y": ["elementwise1_weight"]},
            outputs={"Out": ["elementwise1_output"]},
            attrs={"axis": axis})

        relu_op1 = OpConfig(
            type="relu",
            inputs={"X": ["elementwise1_output"]},
            outputs={"Out": ["relu1_output"]},
            attrs={})

        mul_op2 = OpConfig(
            type="mul",
            inputs={"X": ["relu1_output"],
                    "Y": ["mul2_weight"]},
            outputs={"Out": ["mul2_output"]},
            attrs={"x_num_col_dims": x_col,
                   "y_num_col_dims": y_col})

        elt_op2 = OpConfig(
            type="elementwise_add",
            inputs={"X": ["mul2_output"],
                    "Y": ["elementwise2_weight"]},
            outputs={"Out": ["elementwise2_output"]},
            attrs={"axis": axis})

        relu_op2 = OpConfig(
            type="relu",
            inputs={"X": ["elementwise2_output"]},
            outputs={"Out": ["relu2_output"]},
            attrs={})

        model_net = [mul_op1, elt_op1, relu_op1, mul_op2, elt_op2, relu_op2]

        program_config = ProgramConfig(
            ops=model_net,
            weights={
                "mul1_weight": TensorConfig(data_gen=partial(generate_weight,
                                                             [dim, 32])),
                "mul2_weight":
                TensorConfig(data_gen=partial(generate_weight, [32, 128])),
                "elementwise1_weight":
                TensorConfig(data_gen=partial(generate_weight, [32])),
                "elementwise2_weight":
                TensorConfig(data_gen=partial(generate_weight, [128]))
            },
            inputs={
                "input_data": TensorConfig(data_gen=partial(generate_input)),
            },
            outputs=["relu2_output"])

        return program_config

    def sample_predictor_configs(self, program_config):
        config = self.create_inference_config()
        yield config, ["fusion_repeated_fc_relu"], (1e-5, 1e-5)

    def test(self):
        self.run_and_statis(passes=["repeated_fc_relu_fuse_pass"])

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

max_stars_repo_name: zmxdream/Paddle

max_issues_repo_name: zmxdream/Paddle

max_forks_repo_name: zmxdream/Paddle