import numpy as np
import pandas as pd
import sidetable

import plotly.graph_objects as go
import plotly_express as px
from plotly.subplots import make_subplots

from clean_data.cleaner import Cleaner
from do_data.getter import Reader
from do_data.writer import Writer
from do_data.config import Columns
from analyze_data.network import Network
name = Columns()
network = Network()

# from pandasgui import show
from analyze_data.colors import Colors
from scipy import stats

from sklearn import preprocessing

colors = Colors()

class Metrics():
    def __init__(self):
        self.today = pd.Timestamp.now()
        self.transparent = 'rgba(0,0,0,0)'
        self.charged_class_code = 'charged_class_category'
        self.years = 365.25

        self.common_cols = [
            name.case_id
            , name.case_participant_id
            , name.received_date
            , name.updated_offense_category
            , name.charge_id
            , name.charge_version_id
            , name.charge_count
        ]

        ## https://community.plotly.com/t/plotly-colours-list/11730/3
        self.blue = '#1f77b4'  # muted blue
        self.orange = '#ff7f0e'  # safety orange
        self.green = '#2ca02c'  # cooked asparagus green
        self.red = '#d62728'  # brick red
        self.purple = '#9467bd'  # muted purple
        self.brown = '#8c564b'  # chestnut brown
        self.pink = '#e377c2'  # raspberry yogurt pink
        self.gray = '#7f7f7f'  # middle gray
        self.yellow_green='#bcbd22'  # curry yellow-green
        self.teal = '#17becf'  # blue-teal
        self.df = None
        self.fig = None

    def ov1(self, year="All Time"):
        self.fig = make_subplots(
            rows=1, cols=3
            , specs=[[{"type": "bar"}, {"type": "bar"}, {"type": "bar"}]]
            , subplot_titles=("Initiation"
                              , "Disposition"
                              , "Sentencing")
        )

        self.ov1_initiation(row=1, col=1, year=year)
        self.ov1_disposition(row=1, col=2, year=year)
        self.ov1_sentencing(row=1, col=3, year=year)

        self.fig.update_layout(showlegend=False
                          , title_text=str('Court Data for ' + str(year))
                          , paper_bgcolor=self.transparent
                          , plot_bgcolor=self.transparent)

        self.fig.update_xaxes(tickangle=45)

        return self.fig

    def ov1_initiation(self, row, col, year=None):
        df = Reader().to_df('ov1_initiation.pickle', preview=False, classify=False, echo=False)

        if year !='All Time':
            df = df[(df['year']==year)]

        total_case_id = len(df[name.case_id].unique())
        total_cpi = len(df[name.case_participant_id].unique())

        bond_types = df.stb.freq([name.bond_type_current], cum_cols=False).drop(columns='percent')
        bond_types = pd.melt(bond_types, id_vars=['count'], value_vars=[name.bond_type_current])
        bond_types = bond_types.sort_values(by=['count'], ascending=False)

        event_types = df.stb.freq([name.event], cum_cols=False).drop(columns='percent')
        event_types = pd.melt(event_types, id_vars=['count'], value_vars=[name.event])
        event_types = event_types.sort_values(by=['count'], ascending=False)

        df = pd.DataFrame({'count': [total_case_id, total_cpi],
                           'variable': ['case_id', name.case_participant_id],
                           'value': ['Cases', 'Individuals'],
                           'Color': [np.nan, np.nan]})

        df = df.append(bond_types)
        df = df.append(event_types)

        df['Color'] = df['variable'].map({name.case_id: 'blues',
                                          name.case_participant_id: 'ylgn',
                                          name.event: 'sunset',
                                          name.bond_type_current: 'sunset',
                                          })

        df['variable'] = df['variable'].map({name.case_id: '1 - Cases',
                                             name.case_participant_id: '2 - Individuals',
                                             name.event: '3 - Hearing Type',
                                             name.bond_type_current: '4 - Bond Type'
                                             })
        df['Width'] = 1

        df = df.groupby('variable')

        for x, y in df:
            self.fig.add_trace(
                go.Bar(
                    x=y['variable']
                    , y=y['count']  # , color=df_plot['value']
                    , marker=dict(color=y['count'], colorscale=y['Color'].iloc[0])
                    , text=y['value']
                    , width=y['Width']
                ),
                row=row, col=col
            )

    def ov1_disposition(self, row, col, year=None):
        """
        return the most severe allegation for a given case (not always the primary charge
        https://stackoverflow.com/questions/15705630/get-the-rows-which-have-the-max-count-in-groups-using-groupby
        """
        df = Reader().to_df('ov1_disposition.pickle', preview=False, classify=False, echo=False)

        if year !='All Time':
            df = df[(df['year']==year)]

        disp_cats = df.stb.freq([name.charge_disposition_cat], cum_cols=False).drop(columns='percent')
        disp_cats = pd.melt(disp_cats, id_vars=['count'], value_vars=[name.charge_disposition_cat])
        disp_cats = disp_cats.sort_values(by=['count'], ascending=False)

        charge_cats = df.stb.freq([name.disposition_charged_class], cum_cols=False).drop(columns='percent')
        charge_cats = pd.melt(charge_cats, id_vars=['count'], value_vars=[name.disposition_charged_class])
        charge_cats = charge_cats.sort_values(by=['count'], ascending=False)

        total_case_id = len(df[name.case_id].unique())
        total_cpi = len(df[name.case_participant_id].unique())

        df = pd.DataFrame({'count': [total_case_id, total_cpi],
                           'variable': ['case_id', name.case_participant_id],
                           'value': ['Cases', 'Individuals'],
                           'Color': [np.nan, np.nan]})

        df = df.append(disp_cats)
        df = df.append(charge_cats)

        df['Color'] = df['variable'].map({name.case_id: 'blues',
                                          name.case_participant_id: 'ylgn',
                                          name.charge_disposition_cat: 'sunset',
                                          name.disposition_charged_class: 'sunset'
                                          })

        df['Width'] = 1

        df['variable'] = df['variable'].map({name.case_id: '1 - Cases',
                                          name.case_participant_id: '2 - Individuals',
                                          name.charge_disposition_cat: '3 - Charge Category',
                                          name.disposition_charged_class: '4 - Charge Class'
                                          })

        df = df.groupby('variable')

        for x, y in df:
            self.fig.add_trace(
                go.Bar(
                    x=y['variable']
                    , y=y['count']  # , color=df_plot['value']
                    , marker=dict(color=y['count'], colorscale=y['Color'].iloc[0])
                    , text=y['value']
                    , width=y['Width']
                ),
                row=row, col=col
            )

    def ov1_sentencing(self, row, col, year=None):
        df = Reader().to_df('ov1_sentencing.pickle', preview=False)

        if year !='All Time':
            df = df[(df['year']==year)]

        cols = [
              name.case_id
            , name.case_participant_id
            , name.received_date
            , name.updated_offense_category
            , name.disposition_charged_class
            , name.sentence_judge
            , name.commitment_days
            , name.sentence_type
                ]

        df1 = df[cols].copy()
        df1[self.charged_class_code] = df1[name.disposition_charged_class].cat.codes

        cols = [name.case_id, name.case_participant_id, name.received_date, name.updated_offense_category]
        idx = df1.groupby(cols, sort=False)[self.charged_class_code].transform(max) == df1[self.charged_class_code]
        df = df[idx].drop_duplicates(subset=cols)

        total_cpi = len(df[name.case_participant_id].unique())

        judges = df.stb.freq([name.sentence_judge], cum_cols=False).drop(columns='percent')
        judges = pd.melt(judges, id_vars=['count'], value_vars=[name.sentence_judge])
        judges = judges.sort_values(by=['count'], ascending=False)

        sentence = df.stb.freq([name.sentence_type], cum_cols=False).drop(columns='percent')
        sentence = pd.melt(sentence, id_vars=['count'], value_vars=[name.sentence_type])
        sentence = sentence.sort_values(by=['count'], ascending=False)

        commitment = df.stb.freq([name.commitment_days], cum_cols=False).drop(columns='percent')
        commitment = pd.melt(commitment, id_vars=['count'], value_vars=[name.commitment_days])
        commitment = commitment.sort_values(by=['count'], ascending=False)
        commitment['value'] = (commitment['value'] / self.years).round(1)
        commitment['value'] = commitment['value'].astype(str) + ' Years'

        df = pd.DataFrame({'count':[total_cpi],
                           'variable':[name.case_participant_id],
                           'value':['Individuals'],
                           'Color': [self.green]})

        df = df.append(judges)
        df = df.append(sentence)
        df = df.append(commitment)

        df['Color'] = df['variable'].map({name.case_participant_id:'ylgn',
                                       name.sentence_judge:'sunset',
                                       name.sentence_type:'sunset',
                                       name.commitment_days:'sunset'})

        df['Width'] = 1

        df['variable'] = df['variable'].map({name.case_participant_id: '1 - Individuals',
                                             name.sentence_judge: '2 - Sentence Judge',
                                             name.sentence_type: '3 - Sentence Type',
                                             name.commitment_days: '4 - Sentence Years'
                                             })

        df = df.groupby('variable')

        for x, y in df:
            self.fig.add_trace(
                go.Bar(
                      x=y['variable']
                    , y=y['count']  # , color=df_plot['value']
                    , marker=dict(color=y['count'], colorscale=y['Color'].iloc[0])
                    , text=y['value']
                    , width=y['Width']
                ),
                row=row, col=col
            )

    def ov1_timeseries(self):
        df = Reader().to_df('ov1_initiation.pickle', preview=False, classify=False, echo=False)

        initiation = df[[name.case_participant_id, 'year']].groupby('year', as_index=False).agg('count')
        initiation['type'] = 'initiation'
        df = Reader().to_df('ov1_disposition.pickle', preview=False, classify=False, echo=False)
        disposition = df[[name.case_participant_id, 'year']].groupby('year', as_index=False).agg('count')
        disposition['type'] = 'disposition'
        df = Reader().to_df('ov1_sentencing.pickle', preview=False, classify=False, echo=False)
        sentencing = df[[name.case_participant_id, 'year']].groupby('year', as_index=False).agg('count')
        sentencing['type'] = 'sentencing'

        df = initiation.append(disposition)
        df = df.append(sentencing)

        df = df.rename(columns={name.case_participant_id: '#Cases'})

        df = df[df['year'] > 2010]

        fig = px.area(df, x='year', y='#Cases', color='type')
        # https://towardsdatascience.com/line-chart-animation-with-plotly-on-jupyter-e19c738dc882

        fig.update_yaxes(title='Case Volume')
        fig.update_xaxes(title='Year')

        fig.update_layout(
            hovermode='x unified',
            showlegend=False
            # , title_text=str('Court Data for ' + str(year))
            , paper_bgcolor=self.transparent
            , plot_bgcolor=self.transparent
            , title='Court Volume Over Time - The COVID Court Cliff'
        )

        self.ov1_regression()

        return fig

    def ov1_regression(self
                       , chart_type='dynamic'
                       , frequency='M'
                       , annotation = 'By @justinhchae for Chicago Appleseed Center for Fair Courts'
                       ):
        """
        :param chart_type: One of 'dynamic' for plotly or 'static' for .png seaborn chart
        :param frequency: One of 'M' for monthly, 'Y' for yearly, or 'D' for daily
        :param annotation: String for image caption
        :return: a visual chart of case volumes over time.
        """

        df = Reader().to_df('ov1_initiation.pickle', preview=False, classify=False, echo=False)

        df = df[df['year'] > 2010]
        df = df[df['year'] < 2021]

        initiation = df[[name.event_date, name.event]].groupby([pd.Grouper(key=name.event_date, freq=frequency)]).agg('count').reset_index()
        initiation = initiation.sort_values(name.event_date)
        initiation['type'] = 'initiation'
        initiation['color'] = self.purple
        initiation = initiation.rename(columns={name.event:'count'})

        del df

        df = Reader().to_df('ov1_disposition.pickle', preview=False, classify=False, echo=False)
        df = df[df['year'] > 2010]
        df = df[df['year'] < 2021]

        df = df[[name.disposition_date, name.charge_disposition_cat]].groupby([pd.Grouper(key=name.disposition_date, freq=frequency)]).agg('count').reset_index()
        disposition = df.sort_values(name.disposition_date)
        disposition['type'] = 'disposition'
        disposition['color'] = self.orange
        disposition = disposition.rename(columns={name.charge_disposition_cat: 'count'})

        del df

        df = Reader().to_df('ov1_sentencing.pickle', preview=False, classify=False, echo=False)
        df = df[df['year'] > 2010]
        df = df[df['year'] < 2021]

        df = df[[name.sentence_date, name.sentence_type]].groupby([pd.Grouper(key=name.sentence_date, freq=frequency)]).agg('count').reset_index()
        sentencing = df.sort_values(name.sentence_date)
        sentencing['type'] = 'sentencing'
        sentencing['color'] = self.teal
        sentencing = sentencing.rename(columns={name.sentence_type: 'count'})

        del df

        initiation = initiation.rename(columns={name.event_date:'date'})
        disposition = disposition.rename(columns={name.disposition_date:'date'})
        sentencing = sentencing.rename(columns={name.sentence_date:'date'})

        df = sentencing.append(disposition)
        df = df.append(initiation).reset_index(drop=True)

        if chart_type == 'dynamic':

            g = df.groupby('type')
            fig = go.Figure()

            for group, df in g:
                # https://stackoverflow.com/questions/60204175/plotly-how-to-add-trendline-to-a-bar-chart

                fig.add_trace(go.Scatter(x=df['date'], y=df['count'], name=group, fill='tozeroy', marker=dict(color=df['color'].iloc[0])))

                help_fig = px.scatter(df, x=df['date'], y=df['count'], trendline="lowess")
                x_trend = help_fig["data"][1]['x']
                y_trend = help_fig["data"][1]['y']

                fig.add_trace(go.Scatter(x=x_trend, y=y_trend, name='trend', line = dict(color=df['color'].iloc[0], width=4, dash='dash')))

            # # https://towardsdatascience.com/line-chart-animation-with-plotly-on-jupyter-e19c738dc882

            fig.update_yaxes(title='Case Volume')
            fig.update_xaxes(title=annotation)

            fig.update_layout(
                hovermode='x',
                showlegend=False
                # , title_text=str('Court Data for ' + str(year))
                , paper_bgcolor=self.transparent
                , plot_bgcolor=self.transparent
                , title='Monthly Court Volume Over Time'
            )

            return fig

        elif chart_type == 'static':
            import matplotlib.pyplot as plt
            import seaborn as sns

            arrests = Reader().to_df('arrests_analysis_public.pickle', preview=False, classify=False, echo=False)
            arrests_f = arrests[arrests['charge_1_type']=='F'].copy()
            arrests_m = arrests[arrests['charge_1_type'] == 'M'].copy()

            arrests_f['date'] = pd.to_datetime(arrests_f['arrest_year'].astype(str) + '-' + arrests_f['arrest_month'].astype(str) + '-15')
            arrests_f = arrests_f[['date','charge_1_class']].groupby(
                [pd.Grouper(key='date', freq=frequency)]).agg('count').reset_index()
            arrests_f = arrests_f.sort_values('date')
            arrests_f['type'] = 'felony arrest'
            arrests_f['color'] = self.red
            arrests_f.rename(columns={'charge_1_class':'count'}, inplace=True)

            ##
            arrests_m['date'] = pd.to_datetime(
                arrests_m['arrest_year'].astype(str) + '-' + arrests_m['arrest_month'].astype(str) + '-15')
            arrests_m = arrests_m[['date', 'charge_1_class']].groupby(
                [pd.Grouper(key='date', freq=frequency)]).agg('count').reset_index()
            arrests_m = arrests_m.sort_values('date')
            arrests_m['type'] = 'misdemeanor arrest'
            arrests_m['color'] = self.orange
            arrests_m.rename(columns={'charge_1_class': 'count'}, inplace=True)

            ##

            plt.figure()
            plt.title('Court Volumes and Arrests in Cook County Over Time')
            ax = sns.lineplot(data=df
                              , x='date'
                              , y='count'
                            # , hue='type'
                              , legend='auto'
                              , label='Average Court Volume'
                              )

            sns.lineplot(data=arrests_f
                         , x='date'
                         , y='count'
                         # , hue='type'
                         , legend='auto'
                         , label='Felony Arrests'
                         , color=self.red
                         )

            sns.lineplot(data=arrests_m
                         , x='date'
                         , y='count'
                         # , hue='type'
                         , legend='auto'
                         , label='Misdemeanor Arrests'
                         , color=self.orange
                         , dashes=True
                         )

            ax.lines[2].set_linestyle("--")

            ax.collections[0].set_label('95% Confidence Interval')
            ax.legend()
            plt.savefig('figures/court_volume.png')
            plt.show()

            return 'chart created'

    def dv1_bond(self, year=2020, annotation = 'By @justinhchae for Chicago Appleseed Center for Fair Courts'):

            df = Reader().to_df('dv1_bond.pickle', preview=False)

            if year !='All Time':
                df = df[(df['year']==year)]

            df[name.race].cat.add_categories(['None'], inplace=True)
            df[name.race].fillna('None', inplace=True)
            df[name.race].cat.remove_categories(np.nan, inplace=True)

            df[name.bond_type_current].cat.add_categories(['None'], inplace=True)
            df[name.bond_type_current].fillna('None', inplace=True)
            df[name.bond_type_current].cat.remove_categories(np.nan, inplace=True)

            df[name.event].cat.add_categories(['None'], inplace=True)
            df[name.event].fillna('None', inplace=True)
            df[name.event].cat.remove_categories(np.nan, inplace=True)

            df['root'] = 'initiation event'
            df['weights'] = df[name.event].cat.codes
            df.rename(columns={name.bond_amount_current:'Bond Amount'}, inplace=True)

            fig = px.treemap(df
                             , path=['root', name.race, name.event, name.bond_type_current]
                             , values='Bond Amount'
                             , hover_data=['race']
                             , color='Bond Amount'
                             , color_continuous_scale='RdBu_r'

                             , title='Bond Data by Race for ' + str(year)
                             )

            fig.add_annotation(x=.1, y=-.1,
                               text=annotation,
                               showarrow=False,
                               )

            return fig

    def dv1_bond_timeseries(self
                            , chart_type='dynamic'
                            , frequency='M'
                            , year=2020
                            , annotation = 'By @justinhchae for Chicago Appleseed Center for Fair Courts'):

        tax_df = Reader().to_df('tax_data.pickle', preview=False, classify=False, echo=False)
        # tax_df = tax_df.groupby('date')

        df = Reader().to_df('dv1_bond.pickle', preview=False)
        df = df[df['year'] > 2010]
        df = df[df['year'] < 2021]

        key = {
              'C Bond': self.purple
            , 'D Bond': self.blue
            , 'I Bond': self.green
            , 'No Bond': self.gray
        }

        if chart_type == 'dynamic':

            df = df[[name.bond_date_current, name.bond_type_current, name.bond_amount_current]]

            aggies = ['count', 'sum', 'mean', 'min', 'max']
            df = df.groupby([name.bond_type_current, pd.Grouper(key=name.bond_date_current, freq=frequency)])[
                name.bond_amount_current].agg(aggies).reset_index()
            df[aggies] = df[aggies].fillna(0)

            print(df.head())

            scaler = preprocessing.MinMaxScaler(feature_range=(5, 20))

            # https://towardsdatascience.com/data-normalization-with-pandas-and-scikit-learn-7c1cc6ed6475

            scaled = pd.DataFrame(scaler.fit_transform(df[['count']]), columns=['scaled'])

            df['scaled'] = scaled['scaled'].values

            df['color'] = df[name.bond_type_current].map(key, na_action='ignore')

            grouped = df.groupby(name.bond_type_current)

            fig = go.Figure()

            annotation_y = []

            for group, df in grouped:
                # https://stackoverflow.com/questions/60204175/plotly-how-to-add-trendline-to-a-bar-chart
                # fig.add_trace(go.Scatter(x=df[name.bond_date_current], y=df['sum'], name=group))
                # print(df[''])

                y_val = np.max(df['sum'])
                annotation_y.append(y_val)

                fig.add_trace(go.Scatter(x=df[name.bond_date_current], y=df['sum']
                                         , name=group
                                         , mode='markers'
                                         , marker=dict(size=df['scaled'], color=df['color'])
                                         , showlegend=False
                                         # , text=df['sum']
                                         , hoverinfo='skip'

                                         ))

                fig.add_trace(go.Scatter(x=df[name.bond_date_current], y=df['sum']
                                         , name=group
                                         , mode='lines'
                                         , line=dict(color=df['color'].iloc[0])
                                         ))

            fig.add_trace(go.Scatter(x=tax_df['date'], y=tax_df['muni_public_util_tax']
                                     , name='Chicago Municipal Public Utility Tax'
                                     , mode='lines+markers'
                                     , line=dict(color=self.red)
                                     ))

            a1_date = pd.to_datetime('2017-02-13')
            a2_date = pd.to_datetime('2021-01-14')

            maxv = np.max(annotation_y)
            minv = np.mean(annotation_y)

            # fig.add_shape(x0=[a1_date], y0=[test], text=['poo'], type='line')

            fig.add_shape(type="line",
                          x0=a1_date, y0=0, x1=a1_date, y1=maxv * .7,
                          line=dict(color=self.red, width=2)
                          )

            fig.add_trace(go.Scatter(
                x=[a1_date], y=[maxv * .7]
                , text='<b>Bail Reform Act 2017</b>'
                , mode='text'
                , showlegend=False
                , hoverinfo='skip'
            ))

            fig.add_shape(type="line",
                          x0=a2_date, y0=0, x1=a2_date, y1=minv * 1.3
                          , line=dict(color=self.red, width=2)
                          )

            fig.add_trace(go.Scatter(
                x=[a2_date], y=[minv * 1.3]
                , text='<b>Pre-Trial Fairness Act 2021</b>'
                , mode='text'
                , showlegend=False
                , hoverinfo='skip'
            ))

            a3_date = pd.to_datetime('2020-09-30')

            fig.add_shape(type="line",
                          x0=a3_date, y0=0, x1=a3_date, y1=minv * .5
                          , line=dict(color=self.gray, width=1)
                          )

            fig.add_trace(go.Scatter(
                x=[a3_date], y=[minv * .5]
                , text='Last Data Point'
                , mode='text'
                , showlegend=False
                , hoverinfo='skip'
            ))

            # fig.update_layout(uniformtext_minsize=20, uniformtext_mode='hide')

            fig.update_traces(textposition='top left')

            fig.update_layout(
                hovermode='x',
                showlegend=True
                # , title_text=str('Court Data for ' + str(year))
                , paper_bgcolor=self.transparent
                , plot_bgcolor=self.transparent
                , title='Cook County Bond History'
                , xaxis_title=annotation
            )

            fig.update_layout(legend=dict(
                orientation="h",
                yanchor="bottom",
                y=1.02,
                xanchor="right",
                x=1,
                font_size=10
            ))

            # fig.show()

            return fig

        elif chart_type == 'static':
            import matplotlib.pyplot as plt
            import seaborn as sns

            # df['binned_bond_amount'] = pd.qcut(df[name.bond_amount_current], q=4)
            # df['binned_bond_amount_label'] = pd.qcut(df[name.bond_amount_current], q=4, labels=['1', '2', '3', '4'])

            # print(df[['binned_bond_amount', 'binned_bond_amount_label']])
            df = df[[name.bond_date_current, name.bond_type_current, name.bond_amount_current, name.race]]

            aggies = ['count']
            df1 = df.groupby([name.bond_type_current, pd.Grouper(key=name.bond_date_current, freq=frequency)])[
                name.bond_amount_current].agg(aggies).reset_index()

            df1 = df1.sort_values(by=[name.bond_date_current])

            df2 = df.groupby([name.race, name.bond_type_current, name.bond_amount_current, pd.Grouper(key=name.bond_date_current, freq=frequency)])[
                name.race].agg(aggies).reset_index()

            # df2 = df2[(df2[name.race] == 'White')| (df2[name.race] == 'Black')]
            # df2 = df2[(df2[name.race] == 'Black')]
            df2[name.race] = df2[name.race].cat.remove_unused_categories()

            scaler = preprocessing.MinMaxScaler(feature_range=(0,1))
            df2[name.bond_amount_current] = df2[name.bond_amount_current].fillna(0)
            scaled = pd.DataFrame(scaler.fit_transform(df2[[name.bond_amount_current]]), columns=['scaled_bond_amount'])

            df2['scaled_bond_amount'] = scaled['scaled_bond_amount'].values

            df2 = df2[df2['count'] > 0].copy()

            df2['weighted_count'] = df2['scaled_bond_amount'] * df2['count']

            df2 = df2.sort_values(by=[name.bond_date_current])

            plt.figure()
            plt.title('Bond in Cook County by Time, Race\nand Monthly Counts Weighted by Bond Amount')

            def bond_bw():
                sns.lineplot(data=df2
                             , x=name.bond_date_current
                             , y='count'
                             , hue=name.race
                             , style=name.bond_type_current
                             , legend=True
                             )

            n_features = len(df2[name.race].unique())
            palette = sns.color_palette("icefire", n_features)

            ax = sns.lineplot(data=df2
                         , x=name.bond_date_current
                         , y='weighted_count'
                         , hue=name.race
                         # , style=name.bond_type_current
                         , legend=True
                         # , ci=None
                         , palette=palette
                         )
            ax.legend(fontsize='xx-small')
            # sns.lineplot(data=df2
            #              , x=name.bond_date_current
            #              , y='count'
            #              , hue=name.race
            #              , style=name.bond_type_current
            #              , legend=True
            #              , ci=None
            #              # , marker='o'
            #              )
            plt.savefig('figures/bond_time_race_counts.png')
            plt.show()
            # print(df.stb.freq(['race']))

    def dv1_sentencing_network(self, disp_nodes=True, disp_edges=True
                               , ingest_df=False, generate_graph=True
                               , annotation = 'By @justinhchae for Chicago Appleseed Center for Fair Courts'):

        df = Reader().to_df('ov1_sentencing.pickle', preview=False)
        # df = df[df['year'] > 2010]
        # df = df[df['year'] < 2021]
        # df = df[df[name.bond_electroinic_monitor_flag_current].notnull()]

        if ingest_df:
            network.ingest_df(df, filename='nx_sentencing')

        if generate_graph:
            # np.random.seed(0)
            G, pos_, edge_widths, scaled_node_values, hubs, judges, sentence_types = network.graph_network(df, filename='nx_sentencing')
            edge_trace = []

            fig = go.Figure()

            ghost_trace1 = None
            ghost_trace2 = None

            edge_color_values = list(set([edge[2]['label'] for i, edge in enumerate(G.edges(data=True))]))
            edge_color_values.sort()
            edge_colors_hexes = Colors().discrete_cmap(N=len(edge_color_values), base_cmap='brg')
            edge_colors_hexes.sort(reverse=True)
            edge_color_map = dict(zip(edge_color_values, edge_colors_hexes))

            # print(edge_color_map)

            if disp_edges:

                # colors=Colors.discrete_cmap()

                for i, edge in enumerate(G.edges(data=True)):
                    n2 = edge[0]
                    n1 = edge[1]
                    x0, y0 = pos_[n1]
                    x1, y1 = pos_[n2]

                    #TODO map color to , color=G.edges()[edge]['color']

                    fig.add_trace(go.Scatter(
                        x=tuple((x0, x1)),y=tuple((y0, y1))
                        , name=edge[2]['label']
                        , mode='lines'
                        , line=dict(width=edge[2]['msc_day'], color=edge_color_map[edge[2]['label']])
                        , hoverinfo=['name']
                        # , marker_colorscale=px.colors.sequential.Plasma
                        , showlegend=False))

                    # fig.add_annotation(
                    #     x=x0,  # arrows' head
                    #     y=y0,  # arrows' head
                    #     ax=x1,  # arrows' tail
                    #     ay=y1,  # arrows' tail
                    #     xref='x',
                    #     yref='y',
                    #     axref='x',
                    #     ayref='y',
                    #     # text='',  # if you want only the arrow
                    #     showarrow=True,
                    #     arrowhead=3,
                    #     arrowsize=1,
                    #     arrowwidth=edge_widths[i],
                    #     arrowcolor='black'
                    # )

            if disp_nodes:

                for i, node in enumerate(G.nodes()):
                    x, y = pos_[node]
                    # print(G.nodes(data=True)[node])
                    if node in hubs:
                        fig.add_trace(go.Scatter(
                            x=tuple([x]), y=tuple([y])
                            , name=node
                            , mode='markers'
                            , marker=dict(size=scaled_node_values[i])
                            # , text=node
                            , text=str('Cases ' + str(G.nodes(data=True)[node]['n_cases']))
                            , hoverinfo=['name']
                        ))

                    elif node in judges:
                        fig.add_trace(go.Scatter(
                            x=tuple([x]), y=tuple([y])
                            , name=node
                            , mode='markers'
                            , marker=dict(size=scaled_node_values[i], color=self.gray)
                            , text=str('Cases ' + str(G.nodes(data=True)[node]['n_cases']))
                            , hoverinfo=['name+text']
                            , showlegend=False
                        ))

                        ghost_trace1 = tuple([x]), tuple([y]), scaled_node_values[i]

                    elif node in sentence_types:

                        marker = dict(size=scaled_node_values[i], color=edge_color_map[node], line=dict(color='black'), symbol=1)

                        fig.add_trace(go.Scatter(

                            x=tuple([x]), y=tuple([y])
                            , name=node
                            , mode='markers'
                            , marker=marker
                            , text=node
                            , hoverinfo=['name+text']
                            , showlegend=False
                            , textposition='top center'))

                        ghost_trace2 = tuple([x]), tuple([y]), marker

                    else:
                        fig.add_trace(go.Scatter(
                            x=tuple([x]), y=tuple([y])
                            , name=node
                            , mode='markers'
                            , marker=dict(size=scaled_node_values[i])
                            , text=str('Cases ' + str(G.nodes(data=True)[node]['n_cases']))
                            , hoverinfo=['name']
                            , showlegend=False
                        ))

            if ghost_trace1:
                fig.add_trace(go.Scatter(
                    x=ghost_trace1[0], y=ghost_trace1[1]
                    , name='Judge'
                    , mode='markers'
                    , marker=dict(size=ghost_trace1[2], color=self.gray)
                    , text='Judge'
                    , hoverinfo=None
                    , showlegend=True
                ))

            if ghost_trace2:
                fig.add_trace(go.Scatter(
                    x=ghost_trace2[0], y=ghost_trace2[1]
                    , name='Sentence Type'
                    , mode='markers'
                    , marker=ghost_trace2[2]
                    , text='Sentence Type'
                    , hoverinfo=None
                    , showlegend=True
                ))

            fig.update_yaxes(showticklabels=False)
            fig.update_xaxes(showticklabels=False)

            fig.update_layout(legend=dict(
                orientation="h",
                yanchor="bottom",
                y=1.02,
                xanchor="right",
                x=1,
                font_size=10
            ))

            fig.update_layout(
                showlegend=True
                , paper_bgcolor=self.transparent
                , plot_bgcolor=self.transparent
                , title='Courts as a Network'
                , xaxis_title=annotation
            )

            # fig.show()
            return fig


def get_priority(item_type: str) -> int:
    # capital letters
    if ord(item_type) < ord('a'):
        return ord(item_type) - ord('A') + 26 + 1

    # lowercase letters
    return ord(item_type) - ord('a') + 1

total_priorities = 0

for line in open("input.txt"):
    item_types = set()
    line = line.strip()
    mid = int(len(line) / 2)

    for i in range(mid):
        item_types.add(line[i])

    for i in range(mid, len(line)):
        if line[i] in item_types:
            item_type = line[i]

    total_priorities += get_priority(item_type)

print(total_priorities)


import numpy as np
import matplotlib.pyplot as plt
import pandas as pd

def plot_figure2(datapath, filename, islog=False):
    fname=datapath+filename+'.csv'
    data=pd.read_csv(fname)
    xlabel=''
    if(islog):
        X=data['X'].values
        xlabel='log$_{10}$(wt. % NaCl)'
    else:
        X=data['X'].values*100
        xlabel='wt. % NaCl'
    P=data['P'].values/1e5
    T=data['T'].values
    reg=data['Region'].values
    reg_unique=np.unique(reg)
    # calculate mesh
    nP=0
    nX=0
    if(P[0]==P[1]):
        nP=1
        for i in range(1,len(P)):
            if(P[i]==P[i-1]):
                nP=nP+1
            else:
                break
        nX=int(len(P)/nP)
    else:
        nX=1
        for i in range(1,len(X)):
            if(X[i]==X[i-1]):
                nX=nX+1
            else:
                break
        nP=int(len(X)/nX)
    shape0=(nP,nX)
    P=np.reshape(P,shape0)
    X=np.reshape(X,shape0)
    reg=np.reshape(reg,shape0)
    reg_unique=np.unique(reg)
    print(reg_unique)

    plt.figure()
    ax=plt.gca()

    # scatter=ax.scatter(X,P,c=reg,s=0.1, alpha=0.5)
    cf=ax.contourf(X,P,reg,cmap='Dark2', vmin=0,vmax=7)
    ax.set_xlim(np.min(X),np.max(X))
    ax.set_ylim(np.min(P),np.max(P))
    ax.set_ylabel('P[bar]')
    ax.set_xlabel(xlabel)
    ax.text(np.min(X)+0.03*(np.max(X)-np.min(X)),np.max(P)-0.1*(np.max(P)-np.min(P)),str(T[0])+' $^{\circ}$C',fontsize=12)
    # plt.colorbar(cf)
    plt.savefig('c++/'+filename+'.pdf')
    # plt.show()

datapath='../build/'

# figure 2a
plot_figure2(datapath,'Figure2a_Driesner2007')
# figure 2c
plot_figure2(datapath,'Figure2c_Driesner2007')
# figure 2e
plot_figure2(datapath,'Figure2e_Driesner2007')
# figure 2g
plot_figure2(datapath,'Figure2g_Driesner2007')
# figure 2i
plot_figure2(datapath,'Figure2i_Driesner2007')
# figure 2k
plot_figure2(datapath,'Figure2k_Driesner2007')

# log xaxis
# figure 2b
plot_figure2(datapath,'Figure2b_Driesner2007',islog=True)
# figure 2d
plot_figure2(datapath,'Figure2d_Driesner2007',islog=True)
# figure 2f
plot_figure2(datapath,'Figure2f_Driesner2007',islog=True)
# figure 2h
plot_figure2(datapath,'Figure2h_Driesner2007',islog=True)
# figure 2j
plot_figure2(datapath,'Figure2j_Driesner2007',islog=True)


# Definition for a binary tree node.
# class TreeNode:
#     def __init__(self, val=0, left=None, right=None):
#         self.val = val
#         self.left = left
#         self.right = right
class Solution:
    def largestValues(self, root: Optional[TreeNode]) -> List[int]:
        if not root:
            return []
        level_nodes = deque([root])
        res = []

        while level_nodes:
            n = len(level_nodes)
            level_max = -float('inf')
            for _ in range(n):
                node = level_nodes.popleft()
                level_max = max(level_max,node.val)
                if node.left:
                    level_nodes.append(node.left)
                if node.right:
                    level_nodes.append(node.right)
            res.append(level_max)
        return res


import datetime
import os
from exp import EXP
from utils.constant import CUDA
from models.predictor_model import ECIRobertaJointTask
from models.selector_model import LSTMSelector
import torch
torch.manual_seed(1741)
torch.backends.cudnn.benchmark = False
import torch.nn as nn
import random
random.seed(1741)
import numpy as np
np.random.seed(1741)
import optuna
from torch.utils.data.dataloader import DataLoader
from argparse import ArgumentParser, ArgumentDefaultsHelpFormatter
from data_loader.loader import loader
from data_loader.EventDataset import EventDataset
import gc

def count_parameters(model):
    return sum(p.numel() for p in model.parameters() if p.requires_grad)

def collate_fn(batch):
    return tuple(zip(*batch))

def objective(trial: optuna.Trial):
    params = {
        's_hidden_dim': 512,
        's_mlp_dim': 512,
        'p_mlp_dim': 1024,
        'epoches': 7,
        'warming_epoch': 1,
        'num_ctx_select': 3,
        's_lr': 0.0001,
        'b_lr': 7e-06,
        'm_lr': 5e-05,
        'b_lr_decay_rate': 0.5,
        'word_drop_rate': 0.05,
        'task_reward': 'logit',
        'perfomance_reward_weight': 0.7,
        'ctx_sim_reward_weight': 0.003,
        'knowledge_reward_weight': 0.7,
        'fn_activate': 'tanh',
        'seed': 1741
        }
    torch.manual_seed(1741)
    np.random.seed(params['seed'])
    random.seed(params['seed'])

    drop_rate = 0.5
    fn_activative = params['fn_activate']
    is_mul = True
    # trial.suggest_categorical('is_mul', [True, False])
    is_sub = True
    # trial.suggest_categorical('is_sub', [True, False])
    num_select = params['num_ctx_select']

    train_set = []
    train_short_set = []
    validate_dataloaders = {}
    test_dataloaders = {}
    validate_short_dataloaders = {}
    test_short_dataloaders = {}
    for dataset in datasets:
        train, test, validate, train_short, test_short, validate_short = loader(dataset, 5)
        train_set.extend(train)
        train_short_set.extend(train_short)
        validate_dataloader = DataLoader(EventDataset(validate), batch_size=batch_size, shuffle=True,collate_fn=collate_fn)
        test_dataloader = DataLoader(EventDataset(test), batch_size=batch_size, shuffle=True,collate_fn=collate_fn)
        validate_dataloaders[dataset] = validate_dataloader
        test_dataloaders[dataset] = test_dataloader
        if len(validate_short) == 0:
            validate_short_dataloader = None
        else:
            validate_short_dataloader = DataLoader(EventDataset(validate_short), batch_size=batch_size, shuffle=True,collate_fn=collate_fn)
        if len(test_short) == 0:
            test_short_dataloader = None
        else:
            test_short_dataloader = DataLoader(EventDataset(test_short), batch_size=batch_size, shuffle=True,collate_fn=collate_fn)
        validate_short_dataloaders[dataset] = validate_short_dataloader
        test_short_dataloaders[dataset] = test_short_dataloader
    if len(train_short_set) == 0:
        train_short_dataloader = None
    else:
        train_short_dataloader = DataLoader(EventDataset(train_short_set), batch_size=batch_size, shuffle=True,collate_fn=collate_fn)
    train_dataloader = DataLoader(EventDataset(train_set), batch_size=batch_size, shuffle=True,collate_fn=collate_fn)

    print("Hyperparameter will be use in this trial: \n {}".format(params))

    selector = LSTMSelector(768, params['s_hidden_dim'], params['s_mlp_dim'])
    predictor =ECIRobertaJointTask(mlp_size=params['p_mlp_dim'], roberta_type=roberta_type, datasets=datasets, pos_dim=16,
                                    fn_activate=fn_activative, drop_rate=drop_rate, task_weights=None)

    if CUDA:
        selector = selector.cuda()
        predictor = predictor.cuda()
    selector.zero_grad()
    predictor.zero_grad()
    print("# of parameters:", count_parameters(selector) + count_parameters(predictor))
    epoches = params['epoches'] + 5
    total_steps = len(train_dataloader) * epoches
    print("Total steps: [number of batches] x [number of epochs] =", total_steps)

    exp = EXP(selector, predictor, epoches, params['num_ctx_select'], train_dataloader, validate_dataloaders, test_dataloaders,
            train_short_dataloader, test_short_dataloaders, validate_short_dataloaders,
            params['s_lr'], params['b_lr'], params['m_lr'], params['b_lr_decay_rate'],  params['epoches'], params['warming_epoch'],
            best_path, word_drop_rate=params['word_drop_rate'], reward=[params['task_reward']], perfomance_reward_weight=params['perfomance_reward_weight'],
            ctx_sim_reward_weight=params['ctx_sim_reward_weight'], kg_reward_weight=params['knowledge_reward_weight'])
    F1, CM, matres_F1, test_f1 = exp.train()
    # test_f1 = exp.evaluate(is_test=True)
    print("Result: Best micro F1 of interaction: {}".format(F1))
    with open(result_file, 'a', encoding='UTF-8') as f:
        f.write("\n -------------------------------------------- \n")
        # f.write("\nNote: use lstm in predictor \n")
        f.write("{}\n".format(roberta_type))
        f.write("Hypeparameter: \n{}\n ".format(params))
        f.write("Test F1: {}\n".format(test_f1))
        f.write("Seed: {}\n".format(seed))
        # f.write("Drop rate: {}\n".format(drop_rate))
        # f.write("Batch size: {}\n".format(batch_size))
        f.write("Activate function: {}\n".format(fn_activative))
        f.write("Sub: {} - Mul: {}".format(is_sub, is_mul))
        # f.write("\n Best F1 MATRES: {} \n".format(matres_F1))
        for i in range(0, len(datasets)):
            f.write("{} \n".format(dataset[i]))
            f.write("F1: {} \n".format(F1[i]))
            f.write("CM: \n {} \n".format(CM[i]))
        f.write("Time: {} \n".format(datetime.datetime.now()))
    if test_f1 > 0.834:
        os.rename(best_path[1], best_path[1]+'.{}'.format(test_f1))
        os.rename(best_path[0], best_path[0]+'.{}'.format(test_f1))

    del exp
    del selector
    del predictor
    gc.collect()

    return test_f1

if __name__ == '__main__':
    parser = ArgumentParser(formatter_class=ArgumentDefaultsHelpFormatter)
    parser.add_argument('--seed', help='SEED', default=1741, type=int)
    parser.add_argument('--dataset', help="Name of dataset", action='append', required=True)
    parser.add_argument('--roberta_type', help="base or large", default='roberta-base', type=str)
    parser.add_argument('--best_path', help="Path for save model", type=str)
    parser.add_argument('--log_file', help="Path of log file", type=str)
    parser.add_argument('--bs', help='batch size', default=16, type=int)
    # parser.add_argument('--num_select', help='number of select sentence', default=3, type=int)

    args = parser.parse_args()
    seed = args.seed
    datasets = args.dataset
    print(datasets)
    roberta_type  = args.roberta_type
    best_path = args.best_path
    best_path = [best_path+"selector.pth", best_path+"predictor.pth"]
    result_file = args.log_file
    batch_size = args.bs
    pre_processed_dir = "./" + "_".join(datasets) + "/"

    sampler = optuna.samplers.TPESampler(seed=1741)
    study = optuna.create_study(direction='maximize', sampler=sampler)
    study.optimize(objective, n_trials=50)
    trial = study.best_trial

    print('Accuracy: {}'.format(trial.value))
    print("Best hyperparameters: {}".format(trial.params))


################################ IFSC - CAMPUS FLORIANÓPOLIS ##########################
### {Silas Vasconcelos Cruz} ####
###Algoritmo para calcular o volume de uma Esfera
#
#       Curso: Tecnologia em Eletrônica Industrial
#       Matéria: Lógica de Programação
#       Prof: Carlos Speranza
#       Aluno/Author: Silas Vasconcelos Cruz
#
#Defindo Variáveis
Volume = 0
Raio = 0
#
#Solicitando o valor do Raio
Raio = float(input("Digite o valor do Raio: "))
#
#
#Equação para o cálculo do Volume de uma Esfera
Volume = (((4)/3) * 3.1415 * Raio**3)
#
#Saída de Dados!
print(f'O Volume da Esfera é {Volume:.2f}m³ para o raio de {Raio:.2f}mm')


import math
import sys

testlist = [(1, 3), (2, 5), (10, 6), (14, 15), (13, 14), (45, 81)]
min_distance = sys.maxsize
closest_points = [testlist[0], testlist[1]]

for point in testlist:
    i = testlist.index(point) + 1

    for second_point in testlist[i:]:
        y_length = second_point[1] - point[1]
        x_length = second_point[0] - point[0]
        distance = math.sqrt(y_length**2 + x_length**2)
        if distance < min_distance:
            min_distance = distance
            closest_points = [point, second_point]

print(closest_points)


import math
import os
import re
from contextlib import redirect_stdout
from io import StringIO
from os import path as op
from pathlib import Path

import numpy as np
import pytest
from numpy.testing import (
    assert_allclose,
    assert_array_almost_equal,
    assert_array_equal,
    assert_array_less,
)

import mne
from mne import Annotations, concatenate_raws, create_info, pick_types
from mne._fiff._digitization import DigPoint, _dig_kind_dict
from mne._fiff.constants import FIFF
from mne._fiff.meas_info import Info, _get_valid_units, _writing_info_hdf5
from mne._fiff.pick import _ELECTRODE_CH_TYPES, _FNIRS_CH_TYPES_SPLIT
from mne._fiff.proj import Projection
from mne._fiff.utils import _mult_cal_one
from mne.fixes import _numpy_h5py_dep
from mne.io import BaseRaw, RawArray, read_raw_fif
from mne.io.base import _get_scaling
from mne.transforms import Transform
from mne.utils import (
    _import_h5io_funcs,
    _raw_annot,
    _stamp_to_dt,
    _TempDir,
    catch_logging,
    check_version,
    object_diff,
)

raw_fname = op.join(
    op.dirname(__file__), "..", "..", "io", "tests", "data", "test_raw.fif"
)

def assert_named_constants(info):
    """Assert that info['chs'] has named constants."""
    # for now we just check one
    __tracebackhide__ = True
    r = repr(info["chs"][0])
    for check in (
        ".*FIFFV_COORD_.*",
        ".*FIFFV_COIL_.*",
        ".*FIFF_UNIT_.*",
        ".*FIFF_UNITM_.*",
    ):
        assert re.match(check, r, re.DOTALL) is not None, (check, r)

def assert_attributes(raw):
    """Assert that the instance keeps all its extra attributes in _raw_extras."""
    __tracebackhide__ = True
    assert isinstance(raw, BaseRaw)
    base_attrs = set(dir(BaseRaw(create_info(1, 1000.0, "eeg"), last_samps=[1])))
    base_attrs = base_attrs.union(
        [
            "_data",  # in the case of preloaded data
            "__slotnames__",  # something about being decorated (?)
        ]
    )
    for attr in raw._extra_attributes:
        assert attr not in base_attrs
        base_attrs.add(attr)
    got_attrs = set(dir(raw))
    extra = got_attrs.difference(base_attrs)
    assert extra == set()

def test_orig_units():
    """Test the error handling for original units."""
    # Should work fine
    info = create_info(ch_names=["Cz"], sfreq=100, ch_types="eeg")
    BaseRaw(info, last_samps=[1], orig_units={"Cz": "nV"})

    # Should complain that channel Cz does not have a corresponding original
    # unit.
    with pytest.raises(ValueError, match="has no associated original unit."):
        info = create_info(ch_names=["Cz"], sfreq=100, ch_types="eeg")
        BaseRaw(info, last_samps=[1], orig_units={"not_Cz": "nV"})

    # Test that a non-dict orig_units argument raises a ValueError
    with pytest.raises(ValueError, match="orig_units must be of type dict"):
        info = create_info(ch_names=["Cz"], sfreq=100, ch_types="eeg")
        BaseRaw(info, last_samps=[1], orig_units=True)

def _test_raw_reader(
    reader,
    test_preloading=True,
    test_kwargs=True,
    boundary_decimal=2,
    test_scaling=True,
    test_rank=True,
    **kwargs,
):
    """Test reading, writing and slicing of raw classes.

    Parameters
    ----------
    reader : function
        Function to test.
    test_preloading : bool
        Whether not preloading is implemented for the reader. If True, both
        cases and memory mapping to file are tested.
    test_kwargs : dict
        Test _init_kwargs support.
    boundary_decimal : int
        Number of decimals up to which the boundary should match.
    **kwargs :
        Arguments for the reader. Note: Do not use preload as kwarg.
        Use ``test_preloading`` instead.

    Returns
    -------
    raw : instance of Raw
        A preloaded Raw object.
    """
    tempdir = _TempDir()
    rng = np.random.RandomState(0)
    montage = None
    if "montage" in kwargs:
        montage = kwargs["montage"]
        del kwargs["montage"]
    if test_preloading:
        raw = reader(preload=True, **kwargs)
        rep = repr(raw)
        assert rep.count("<") == 1
        assert rep.count(">") == 1
        if montage is not None:
            raw.set_montage(montage)
        # don't assume the first is preloaded
        buffer_fname = op.join(tempdir, "buffer")
        picks = rng.permutation(np.arange(len(raw.ch_names) - 1))[:10]
        picks = np.append(picks, len(raw.ch_names) - 1)  # test trigger channel
        bnd = min(int(round(raw.buffer_size_sec * raw.info["sfreq"])), raw.n_times)
        slices = [
            slice(0, bnd),
            slice(bnd - 1, bnd),
            slice(3, bnd),
            slice(3, 300),
            slice(None),
            slice(1, bnd),
        ]
        if raw.n_times >= 2 * bnd:  # at least two complete blocks
            slices += [slice(bnd, 2 * bnd), slice(bnd, bnd + 1), slice(0, bnd + 100)]
        other_raws = [
            reader(preload=buffer_fname, **kwargs),
            reader(preload=False, **kwargs),
        ]
        for sl_time in slices:
            data1, times1 = raw[picks, sl_time]
            for other_raw in other_raws:
                data2, times2 = other_raw[picks, sl_time]
                assert_allclose(data1, data2, err_msg="Data mismatch with preload")
                assert_allclose(times1, times2)

        # test projection vs cals and data units
        other_raw = reader(preload=False, **kwargs)
        other_raw.del_proj()
        eeg = meg = fnirs = seeg = eyetrack = False
        if "eeg" in raw:
            eeg, atol = True, 1e-18
        elif "grad" in raw:
            meg, atol = "grad", 1e-24
        elif "mag" in raw:
            meg, atol = "mag", 1e-24
        elif "hbo" in raw:
            fnirs, atol = "hbo", 1e-10
        elif "hbr" in raw:
            fnirs, atol = "hbr", 1e-10
        elif "fnirs_cw_amplitude" in raw:
            fnirs, atol = "fnirs_cw_amplitude", 1e-10
        elif "eyegaze" in raw:
            eyetrack = "eyegaze", 1e-3
        else:
            # e.g., https://github.com/mne-tools/mne-python/pull/11432/files
            assert "seeg" in raw, "New channel type necessary? See gh-11432 for example"
            seeg, atol = True, 1e-18

        picks = pick_types(
            other_raw.info,
            meg=meg,
            eeg=eeg,
            fnirs=fnirs,
            seeg=seeg,
            eyetrack=eyetrack,
        )
        col_names = [other_raw.ch_names[pick] for pick in picks]
        proj = np.ones((1, len(picks)))
        proj /= np.sqrt(proj.shape[1])
        proj = Projection(
            data=dict(
                data=proj, nrow=1, row_names=None, col_names=col_names, ncol=len(picks)
            ),
            active=False,
        )
        assert len(other_raw.info["projs"]) == 0
        other_raw.add_proj(proj)
        assert len(other_raw.info["projs"]) == 1
        # Orders of projector application, data loading, and reordering
        # equivalent:
        # 1. load->apply->get
        data_load_apply_get = other_raw.copy().load_data().apply_proj().get_data(picks)
        # 2. apply->get (and don't allow apply->pick)
        apply = other_raw.copy().apply_proj()
        data_apply_get = apply.get_data(picks)
        data_apply_get_0 = apply.get_data(picks[0])[0]
        with pytest.raises(RuntimeError, match="loaded"):
            apply.copy().pick(picks[0]).get_data()
        # 3. apply->load->get
        data_apply_load_get = apply.copy().load_data().get_data(picks)
        data_apply_load_get_0, data_apply_load_get_1 = (
            apply.copy().load_data().pick(picks[:2]).get_data()
        )
        # 4. reorder->apply->load->get
        all_picks = np.arange(len(other_raw.ch_names))
        reord = np.concatenate(
            (picks[1::2], picks[0::2], np.setdiff1d(all_picks, picks))
        )
        rev = np.argsort(reord)
        assert_array_equal(reord[rev], all_picks)
        assert_array_equal(rev[reord], all_picks)
        reorder = other_raw.copy().pick(reord)
        assert reorder.ch_names == [other_raw.ch_names[r] for r in reord]
        assert reorder.ch_names[0] == other_raw.ch_names[picks[1]]
        assert_allclose(reorder.get_data([0]), other_raw.get_data(picks[1]))
        reorder_apply = reorder.copy().apply_proj()
        assert reorder_apply.ch_names == reorder.ch_names
        assert reorder_apply.ch_names[0] == apply.ch_names[picks[1]]
        assert_allclose(
            reorder_apply.get_data([0]), apply.get_data(picks[1]), atol=1e-18
        )
        data_reorder_apply_load_get = reorder_apply.load_data().get_data(
            rev[: len(picks)]
        )
        data_reorder_apply_load_get_1 = (
            reorder_apply.copy().load_data().pick([0]).get_data()[0]
        )
        assert reorder_apply.ch_names[0] == apply.ch_names[picks[1]]
        assert (
            data_load_apply_get.shape
            == data_apply_get.shape
            == data_apply_load_get.shape
            == data_reorder_apply_load_get.shape
        )
        del apply
        # first check that our data are (probably) in the right units
        data = data_load_apply_get.copy()
        data = data - np.mean(data, axis=1, keepdims=True)  # can be offsets
        np.abs(data, out=data)
        if test_scaling:
            maxval = atol * 1e16
            assert_array_less(data, maxval)
            minval = atol * 1e6
            assert_array_less(minval, np.median(data))
        else:
            atol = 1e-7 * np.median(data)  # 1e-7 * MAD
        # ranks should all be reduced by 1
        if test_rank == "less":
            cmp = np.less
        elif test_rank is False:
            cmp = None
        else:  # anything else is like True or 'equal'
            assert test_rank is True or test_rank == "equal", test_rank
            cmp = np.equal
        rank_load_apply_get = np.linalg.matrix_rank(data_load_apply_get)
        rank_apply_get = np.linalg.matrix_rank(data_apply_get)
        rank_apply_load_get = np.linalg.matrix_rank(data_apply_load_get)
        if cmp is not None:
            assert cmp(rank_load_apply_get, len(col_names) - 1)
            assert cmp(rank_apply_get, len(col_names) - 1)
            assert cmp(rank_apply_load_get, len(col_names) - 1)
        # and they should all match
        t_kw = dict(atol=atol, err_msg="before != after, likely _mult_cal_one prob")
        assert_allclose(data_apply_get[0], data_apply_get_0, **t_kw)
        assert_allclose(data_apply_load_get_1, data_reorder_apply_load_get_1, **t_kw)
        assert_allclose(data_load_apply_get[0], data_apply_load_get_0, **t_kw)
        assert_allclose(data_load_apply_get, data_apply_get, **t_kw)
        assert_allclose(data_load_apply_get, data_apply_load_get, **t_kw)
        if "eeg" in raw:
            other_raw.del_proj()
            direct = other_raw.copy().load_data().set_eeg_reference().get_data()
            other_raw.set_eeg_reference(projection=True)
            assert len(other_raw.info["projs"]) == 1
            this_proj = other_raw.info["projs"][0]["data"]
            assert this_proj["col_names"] == col_names
            assert this_proj["data"].shape == proj["data"]["data"].shape
            assert_allclose(np.linalg.norm(proj["data"]["data"]), 1.0, atol=1e-6)
            assert_allclose(np.linalg.norm(this_proj["data"]), 1.0, atol=1e-6)
            assert_allclose(this_proj["data"], proj["data"]["data"])
            proj = other_raw.apply_proj().get_data()
            assert_allclose(proj[picks], data_load_apply_get, atol=1e-10)
            assert_allclose(proj, direct, atol=1e-10, err_msg=t_kw["err_msg"])
    else:
        raw = reader(**kwargs)
    n_samp = len(raw.times)
    assert_named_constants(raw.info)
    assert_attributes(raw)
    # smoke test for gh #9743
    ids = [id(ch["loc"]) for ch in raw.info["chs"]]
    assert len(set(ids)) == len(ids)

    full_data = raw._data
    assert raw.__class__.__name__ in repr(raw)  # to test repr
    assert raw.info.__class__.__name__ in repr(raw.info)
    assert isinstance(raw.info["dig"], (type(None), list))
    data_max = np.nanmax(full_data)
    data_min = np.nanmin(full_data)
    # these limits could be relaxed if we actually find data with
    # huge values (in SI units)
    assert data_max < 1e5
    assert data_min > -1e5
    if isinstance(raw.info["dig"], list):
        for di, d in enumerate(raw.info["dig"]):
            assert isinstance(d, DigPoint), (di, d)

    # gh-5604
    meas_date = raw.info["meas_date"]
    assert meas_date is None or meas_date >= _stamp_to_dt((0, 0))

    # test repr_html
    assert "Good channels" in raw._repr_html_()

    # test resetting raw
    if test_kwargs:
        raw2 = reader(**raw._init_kwargs)
        assert set(raw.info.keys()) == set(raw2.info.keys())
        assert_array_equal(raw.times, raw2.times)

    # Test saving and reading
    out_fname = op.join(tempdir, "test_raw.fif")
    raw = concatenate_raws([raw])
    raw.save(out_fname, tmax=raw.times[-1], overwrite=True, buffer_size_sec=1)

    # Test saving with not correct extension
    out_fname_h5 = op.join(tempdir, "test_raw.h5")
    with pytest.raises(OSError, match="raw must end with .fif or .fif.gz"):
        raw.save(out_fname_h5)

    raw3 = read_raw_fif(out_fname)
    assert_named_constants(raw3.info)
    assert set(raw.info.keys()) == set(raw3.info.keys())
    assert_allclose(
        raw3[0:20][0], full_data[0:20], rtol=1e-6, atol=1e-20
    )  # atol is very small but > 0
    assert_allclose(raw.times, raw3.times, atol=1e-6, rtol=1e-6)

    assert not math.isnan(raw3.info["highpass"])
    assert not math.isnan(raw3.info["lowpass"])
    assert not math.isnan(raw.info["highpass"])
    assert not math.isnan(raw.info["lowpass"])

    assert raw3.info["kit_system_id"] == raw.info["kit_system_id"]

    # Make sure concatenation works
    first_samp = raw.first_samp
    last_samp = raw.last_samp
    concat_raw = concatenate_raws([raw.copy(), raw])
    assert concat_raw.n_times == 2 * raw.n_times
    assert concat_raw.first_samp == first_samp
    assert concat_raw.last_samp - last_samp + first_samp == last_samp + 1
    idx = np.where(concat_raw.annotations.description == "BAD boundary")[0]

    expected_bad_boundary_onset = raw._last_time

    assert_array_almost_equal(
        concat_raw.annotations.onset[idx],
        expected_bad_boundary_onset,
        decimal=boundary_decimal,
    )

    if raw.info["meas_id"] is not None:
        for key in ["secs", "usecs", "version"]:
            assert raw.info["meas_id"][key] == raw3.info["meas_id"][key]
        assert_array_equal(
            raw.info["meas_id"]["machid"], raw3.info["meas_id"]["machid"]
        )

    assert isinstance(raw.annotations, Annotations)

    # Make a "soft" test on units: They have to be valid SI units as in
    # mne.io.meas_info.valid_units, but we accept any lower/upper case for now.
    valid_units = _get_valid_units()
    valid_units_lower = [unit.lower() for unit in valid_units]
    if raw._orig_units is not None:
        assert isinstance(raw._orig_units, dict)
        for ch_name, unit in raw._orig_units.items():
            assert unit.lower() in valid_units_lower, ch_name

    # Test picking with and without preload
    if test_preloading:
        preload_kwargs = (dict(preload=True), dict(preload=False))
    else:
        preload_kwargs = (dict(),)
    n_ch = len(raw.ch_names)
    picks = rng.permutation(n_ch)
    for preload_kwarg in preload_kwargs:
        these_kwargs = kwargs.copy()
        these_kwargs.update(preload_kwarg)
        # don't use the same filename or it could create problems
        if isinstance(these_kwargs.get("preload", None), str) and op.isfile(
            these_kwargs["preload"]
        ):
            these_kwargs["preload"] += "-1"
        whole_raw = reader(**these_kwargs)
        print(whole_raw)  # __repr__
        assert n_ch >= 2
        picks_1 = picks[: n_ch // 2]
        picks_2 = picks[n_ch // 2 :]
        raw_1 = whole_raw.copy().pick(picks_1)
        raw_2 = whole_raw.copy().pick(picks_2)
        data, times = whole_raw[:]
        data_1, times_1 = raw_1[:]
        data_2, times_2 = raw_2[:]
        assert_array_equal(times, times_1)
        assert_array_equal(data[picks_1], data_1)
        assert_array_equal(
            times,
            times_2,
        )
        assert_array_equal(data[picks_2], data_2)

    # Make sure that writing info to h5 format
    # (all fields should be compatible)
    if check_version("h5io"):
        read_hdf5, write_hdf5 = _import_h5io_funcs()
        fname_h5 = op.join(tempdir, "info.h5")
        with _writing_info_hdf5(raw.info), _numpy_h5py_dep():
            write_hdf5(fname_h5, raw.info)
            new_info = Info(read_hdf5(fname_h5))
        assert object_diff(new_info, raw.info) == ""

    # Make sure that changing directory does not break anything
    if test_preloading:
        these_kwargs = kwargs.copy()
        key = None
        for key in (
            "fname",
            "input_fname",  # artemis123
            "vhdr_fname",  # BV
            "pdf_fname",  # BTi
            "directory",  # CTF
            "filename",  # nedf
        ):
            try:
                fname = kwargs[key]
            except KeyError:
                key = None
            else:
                break
        # len(kwargs) == 0 for the fake arange reader
        if len(kwargs):
            assert key is not None, sorted(kwargs.keys())
            this_fname = fname[0] if isinstance(fname, list) else fname
            dirname = op.dirname(this_fname)
            these_kwargs[key] = op.basename(this_fname)
            these_kwargs["preload"] = False
            orig_dir = os.getcwd()
            try:
                os.chdir(dirname)
                raw_chdir = reader(**these_kwargs)
            finally:
                os.chdir(orig_dir)
            raw_chdir.load_data()

    # make sure that cropping works (with first_samp shift)
    if n_samp >= 50:  # we crop to this number of samples below
        for t_prop in (0.0, 0.5):
            _test_raw_crop(reader, t_prop, kwargs)
            if test_preloading:
                use_kwargs = kwargs.copy()
                use_kwargs["preload"] = True
                _test_raw_crop(reader, t_prop, use_kwargs)

    # make sure electrode-like sensor locations show up as dig points
    eeg_dig = [d for d in (raw.info["dig"] or []) if d["kind"] == _dig_kind_dict["eeg"]]
    pick_kwargs = dict()
    for t in _ELECTRODE_CH_TYPES + ("fnirs",):
        pick_kwargs[t] = True
    dig_picks = pick_types(raw.info, exclude=(), **pick_kwargs)
    dig_types = _ELECTRODE_CH_TYPES + _FNIRS_CH_TYPES_SPLIT
    assert (len(dig_picks) > 0) == any(t in raw for t in dig_types)
    if len(dig_picks):
        eeg_loc = np.array(
            [  # eeg_loc a bit of a misnomer to match eeg_dig
                raw.info["chs"][pick]["loc"][:3] for pick in dig_picks
            ]
        )
        eeg_loc = eeg_loc[np.isfinite(eeg_loc).all(axis=1)]
        if len(eeg_loc):
            if "fnirs_cw_amplitude" in raw:
                assert 2 * len(eeg_dig) >= len(eeg_loc)
            else:
                assert len(eeg_dig) >= len(eeg_loc)  # could have some excluded
    # make sure that dig points in head coords implies that fiducials are
    # present
    if len(raw.info["dig"] or []) > 0:
        card_pts = [
            d for d in raw.info["dig"] if d["kind"] == _dig_kind_dict["cardinal"]
        ]
        eeg_dig_head = [d for d in eeg_dig if d["coord_frame"] == FIFF.FIFFV_COORD_HEAD]
        if len(eeg_dig_head):
            assert len(card_pts) == 3, "Cardinal points missing"
        if len(card_pts) == 3:  # they should all be in head coords then
            assert len(eeg_dig_head) == len(eeg_dig)

    return raw

def _test_raw_crop(reader, t_prop, kwargs):
    raw_1 = reader(**kwargs)
    n_samp = 50  # crop to this number of samples (per instance)
    crop_t = n_samp / raw_1.info["sfreq"]
    t_start = t_prop * crop_t  # also crop to some fraction into the first inst
    extra = f' t_start={t_start}, preload={kwargs.get("preload", False)}'
    stop = (n_samp - 1) / raw_1.info["sfreq"]
    raw_1.crop(0, stop)
    assert len(raw_1.times) == 50
    first_time = raw_1.first_time
    atol = 0.5 / raw_1.info["sfreq"]
    assert_allclose(raw_1.times[-1], stop, atol=atol)
    raw_2, raw_3 = raw_1.copy(), raw_1.copy()
    t_tot = raw_1.times[-1] * 3 + 2.0 / raw_1.info["sfreq"]
    raw_concat = concatenate_raws([raw_1, raw_2, raw_3])
    assert len(raw_concat._filenames) == 3
    assert_allclose(raw_concat.times[-1], t_tot)
    assert_allclose(raw_concat.first_time, first_time)
    # keep all instances, but crop to t_start at the beginning
    raw_concat.crop(t_start, None)
    assert len(raw_concat._filenames) == 3
    assert_allclose(raw_concat.times[-1], t_tot - t_start, atol=atol)
    assert_allclose(
        raw_concat.first_time,
        first_time + t_start,
        atol=atol,
        err_msg=f"Base concat, {extra}",
    )
    # drop the first instance
    raw_concat.crop(crop_t, None)
    assert len(raw_concat._filenames) == 2
    assert_allclose(raw_concat.times[-1], t_tot - t_start - crop_t, atol=atol)
    assert_allclose(
        raw_concat.first_time,
        first_time + t_start + crop_t,
        atol=atol,
        err_msg=f"Dropping one, {extra}",
    )
    # drop the second instance, leaving just one
    raw_concat.crop(crop_t, None)
    assert len(raw_concat._filenames) == 1
    assert_allclose(raw_concat.times[-1], t_tot - t_start - 2 * crop_t, atol=atol)
    assert_allclose(
        raw_concat.first_time,
        first_time + t_start + 2 * crop_t,
        atol=atol,
        err_msg=f"Dropping two, {extra}",
    )

def _test_concat(reader, *args):
    """Test concatenation of raw classes that allow not preloading."""
    data = None

    for preload in (True, False):
        raw1 = reader(*args, preload=preload)
        raw2 = reader(*args, preload=preload)
        raw1.append(raw2)
        raw1.load_data()
        if data is None:
            data = raw1[:, :][0]
        assert_allclose(data, raw1[:, :][0])

    for first_preload in (True, False):
        raw = reader(*args, preload=first_preload)
        data = raw[:, :][0]
        for preloads in ((True, True), (True, False), (False, False)):
            for last_preload in (True, False):
                t_crops = raw.times[np.argmin(np.abs(raw.times - 0.5)) + [0, 1]]
                raw1 = raw.copy().crop(0, t_crops[0])
                if preloads[0]:
                    raw1.load_data()
                raw2 = raw.copy().crop(t_crops[1], None)
                if preloads[1]:
                    raw2.load_data()
                raw1.append(raw2)
                if last_preload:
                    raw1.load_data()
                assert_allclose(data, raw1[:, :][0])

def test_time_as_index():
    """Test indexing of raw times."""
    raw = read_raw_fif(raw_fname)

    # Test original (non-rounding) indexing behavior
    orig_inds = raw.time_as_index(raw.times)
    assert len(set(orig_inds)) != len(orig_inds)

    # Test new (rounding) indexing behavior
    new_inds = raw.time_as_index(raw.times, use_rounding=True)
    assert_array_equal(new_inds, np.arange(len(raw.times)))

@pytest.mark.parametrize("meas_date", [None, "orig"])
@pytest.mark.parametrize("first_samp", [0, 10000])
def test_crop_by_annotations(meas_date, first_samp):
    """Test crop by annotations of raw."""
    raw = read_raw_fif(raw_fname)

    if meas_date is None:
        raw.set_meas_date(None)

    raw = mne.io.RawArray(raw.get_data(), raw.info, first_samp=first_samp)

    onset = np.array([0, 1.5], float)
    if meas_date is not None:
        onset += raw.first_time
    annot = mne.Annotations(
        onset=onset,
        duration=[1, 0.5],
        description=["a", "b"],
        orig_time=raw.info["meas_date"],
    )

    raw.set_annotations(annot)
    raws = raw.crop_by_annotations()
    assert len(raws) == 2
    assert len(raws[0].annotations) == 1
    assert raws[0].times[-1] == pytest.approx(annot[:1].duration[0], rel=1e-3)
    assert raws[0].annotations.description[0] == annot.description[0]
    assert len(raws[1].annotations) == 1
    assert raws[1].times[-1] == pytest.approx(annot[1:2].duration[0], rel=5e-3)
    assert raws[1].annotations.description[0] == annot.description[1]

@pytest.mark.parametrize(
    "offset, origin",
    [
        pytest.param(0, None, id="times in s. relative to first_samp (default)"),
        pytest.param(0, 2.0, id="times in s. relative to first_samp"),
        pytest.param(1, 1.0, id="times in s. relative to meas_date"),
        pytest.param(2, 0.0, id="absolute times in s. relative to 0"),
    ],
)
def test_time_as_index_ref(offset, origin):
    """Test indexing of raw times."""
    info = create_info(ch_names=10, sfreq=10.0)
    raw = RawArray(data=np.empty((10, 10)), info=info, first_samp=10)
    raw.set_meas_date(1)

    relative_times = raw.times
    inds = raw.time_as_index(relative_times + offset, use_rounding=True, origin=origin)
    assert_array_equal(inds, np.arange(raw.n_times))

def test_meas_date_orig_time():
    """Test the relation between meas_time in orig_time."""
    # meas_time is set and orig_time is set:
    # clips the annotations based on raw.data and resets the annotation based
    # on raw.info['meas_date]
    raw = _raw_annot(1, 1.5)
    assert raw.annotations.orig_time == _stamp_to_dt((1, 0))
    assert raw.annotations.onset[0] == 1

    # meas_time is set and orig_time is None:
    # Consider annot.orig_time to be raw.frist_sample, clip and reset
    # annotations to have the raw.annotations.orig_time == raw.info['meas_date]
    raw = _raw_annot(1, None)
    assert raw.annotations.orig_time == _stamp_to_dt((1, 0))
    assert raw.annotations.onset[0] == 1.5

    # meas_time is None and orig_time is set:
    # Raise error, it makes no sense to have an annotations object that we know
    # when was acquired and set it to a raw object that does not know when was
    # it acquired.
    with pytest.raises(RuntimeError, match="Ambiguous operation"):
        _raw_annot(None, 1.5)

    # meas_time is None and orig_time is None:
    # Consider annot.orig_time to be raw.first_sample and clip
    raw = _raw_annot(None, None)
    assert raw.annotations.orig_time is None
    assert raw.annotations.onset[0] == 1.5
    assert raw.annotations.duration[0] == 0.2

def test_get_data_reject():
    """Test if reject_by_annotation is working correctly."""
    fs = 256
    ch_names = ["C3", "Cz", "C4"]
    info = create_info(ch_names, sfreq=fs)
    raw = RawArray(np.zeros((len(ch_names), 10 * fs)), info)
    raw.set_annotations(Annotations(onset=[2, 4], duration=[3, 2], description="bad"))

    with catch_logging() as log:
        data = raw.get_data(reject_by_annotation="omit", verbose=True)
        msg = (
            "Omitting 1024 of 2560 (40.00%) samples, retaining 1536"
            + " (60.00%) samples."
        )
        assert log.getvalue().strip() == msg
    assert data.shape == (len(ch_names), 1536)
    with catch_logging() as log:
        data = raw.get_data(reject_by_annotation="nan", verbose=True)
        msg = (
            "Setting 1024 of 2560 (40.00%) samples to NaN, retaining 1536"
            + " (60.00%) samples."
        )
        assert log.getvalue().strip() == msg
    assert data.shape == (len(ch_names), 2560)  # shape doesn't change
    assert np.isnan(data).sum() == 3072  # but NaNs are introduced instead

def test_5839():
    """Test concatenating raw objects with annotations."""
    # Global Time 0         1         2         3         4
    #             .
    #      raw_A  |---------XXXXXXXXXX
    #      annot  |--------------AA
    #    latency  .         0    0    1    1    2    2    3
    #             .              5    0    5    0    5    0
    #
    #      raw_B  .                   |---------YYYYYYYYYY
    #      annot  .                   |--------------AA
    #    latency  .                             0         1
    #             .                                  5    0
    #             .
    #     output  |---------XXXXXXXXXXYYYYYYYYYY
    #      annot  |--------------AA---|----AA
    #    latency  .         0    0    1    1    2    2    3
    #             .              5    0    5    0    5    0
    #
    EXPECTED_ONSET = [1.5, 2.0, 2.0, 2.5]
    EXPECTED_DURATION = [0.2, 0.0, 0.0, 0.2]
    EXPECTED_DESCRIPTION = ["dummy", "BAD boundary", "EDGE boundary", "dummy"]

    def raw_factory(meas_date):
        raw = RawArray(
            data=np.empty((10, 10)),
            info=create_info(ch_names=10, sfreq=10.0),
            first_samp=10,
        )
        raw.set_meas_date(meas_date)
        raw.set_annotations(
            annotations=Annotations(
                onset=[0.5], duration=[0.2], description="dummy", orig_time=None
            )
        )
        return raw

    raw_A, raw_B = [raw_factory((x, 0)) for x in [0, 2]]
    raw_A.append(raw_B)

    assert_array_equal(raw_A.annotations.onset, EXPECTED_ONSET)
    assert_array_equal(raw_A.annotations.duration, EXPECTED_DURATION)
    assert_array_equal(raw_A.annotations.description, EXPECTED_DESCRIPTION)
    assert raw_A.annotations.orig_time == _stamp_to_dt((0, 0))

def test_repr():
    """Test repr of Raw."""
    sfreq = 256
    info = create_info(3, sfreq)
    raw = RawArray(np.zeros((3, 10 * sfreq)), info)
    r = repr(raw)
    assert (
        re.search("<RawArray | 3 x 2560 (10.0 s), ~.* kB, data loaded>", r) is not None
    ), r
    assert raw._repr_html_()

# A class that sets channel data to np.arange, for testing _test_raw_reader
class _RawArange(BaseRaw):
    def __init__(self, preload=False, verbose=None):
        info = create_info(list(str(x) for x in range(1, 9)), 1000.0, "eeg")
        super().__init__(info, preload, last_samps=(999,), verbose=verbose)
        assert len(self.times) == 1000

    def _read_segment_file(self, data, idx, fi, start, stop, cals, mult):
        one = np.full((8, stop - start), np.nan)
        one[idx] = np.arange(1, 9)[idx, np.newaxis]
        _mult_cal_one(data, one, idx, cals, mult)

def _read_raw_arange(preload=False, verbose=None):
    return _RawArange(preload, verbose)

def test_test_raw_reader():
    """Test _test_raw_reader."""
    _test_raw_reader(_read_raw_arange, test_scaling=False, test_rank="less")

@pytest.mark.slowtest
def test_describe_print():
    """Test print output of describe method."""
    fname = Path(__file__).parent / "data" / "test_raw.fif"
    raw = read_raw_fif(fname)

    # test print output
    f = StringIO()
    with redirect_stdout(f):
        raw.describe()
    s = f.getvalue().strip().split("\n")
    assert len(s) == 378
    # Can be 3.1, 3.3, etc.
    assert (
        re.match(
            r"<Raw | test_raw.fif, 376 x 14400 (24\.0 s), "
            r"~3\.. MB, data not loaded>",
            s[0],
        )
        is not None
    ), s[0]
    assert (
        s[1]
        == " ch  name      type  unit         min         Q1     median         Q3        max"  # noqa: E501
    )
    assert (
        s[2]
        == "  0  MEG 0113  GRAD  fT/cm    -221.80     -38.57      -9.64      19.29     414.67"  # noqa: E501
    )
    assert (
        s[-1]
        == "375  EOG 061   EOG   µV       -231.41     271.28     277.16     285.66     334.69"  # noqa: E501
    )

@pytest.mark.slowtest
def test_describe_df():
    """Test returned data frame of describe method."""
    pytest.importorskip("pandas")
    fname = Path(__file__).parent / "data" / "test_raw.fif"
    raw = read_raw_fif(fname)

    df = raw.describe(data_frame=True)
    assert df.shape == (376, 8)
    assert df.columns.tolist() == [
        "name",
        "type",
        "unit",
        "min",
        "Q1",
        "median",
        "Q3",
        "max",
    ]
    assert df.index.name == "ch"
    assert_allclose(
        df.iloc[0, 3:].astype(float),
        np.array(
            [
                -2.218017605790535e-11,
                -3.857421923113974e-12,
                -9.643554807784935e-13,
                1.928710961556987e-12,
                4.146728567347522e-11,
            ]
        ),
    )

def test_get_data_units():
    """Test the "units" argument of get_data method."""
    # Test the unit conversion function
    assert _get_scaling("eeg", "uV") == 1e6
    assert _get_scaling("eeg", "dV") == 1e1
    assert _get_scaling("eeg", "pV") == 1e12
    assert _get_scaling("mag", "fT") == 1e15
    assert _get_scaling("grad", "T/m") == 1
    assert _get_scaling("grad", "T/mm") == 1e-3
    assert _get_scaling("grad", "fT/m") == 1e15
    assert _get_scaling("grad", "fT/cm") == 1e13
    assert _get_scaling("csd", "uV/cm²") == 1e2

    fname = Path(__file__).parent / "data" / "test_raw.fif"
    raw = read_raw_fif(fname)

    last = np.array([4.63803098e-05, 7.66563736e-05, 2.71933595e-04])
    last_eeg = np.array([7.12207023e-05, 4.63803098e-05, 7.66563736e-05])
    last_grad = np.array([-3.85742192e-12, 9.64355481e-13, -1.06079103e-11])

    # None
    data_none = raw.get_data()
    assert data_none.shape == (376, 14400)
    assert_array_almost_equal(data_none[-3:, -1], last)

    # str: unit no conversion
    data_str_noconv = raw.get_data(picks=["eeg"], units="V")
    assert data_str_noconv.shape == (60, 14400)
    assert_array_almost_equal(data_str_noconv[-3:, -1], last_eeg)
    # str: simple unit
    data_str_simple = raw.get_data(picks=["eeg"], units="uV")
    assert data_str_simple.shape == (60, 14400)
    assert_array_almost_equal(data_str_simple[-3:, -1], last_eeg * 1e6)
    # str: fraction unit
    data_str_fraction = raw.get_data(picks=["grad"], units="fT/cm")
    assert data_str_fraction.shape == (204, 14400)
    assert_array_almost_equal(data_str_fraction[-3:, -1], last_grad * (1e15 / 1e2))
    # str: more than one channel type but one with unit
    data_str_simplestim = raw.get_data(picks=["eeg", "stim"], units="V")
    assert data_str_simplestim.shape == (69, 14400)
    assert_array_almost_equal(data_str_simplestim[-3:, -1], last_eeg)
    # str: too many channels
    with pytest.raises(ValueError, match="more than one channel"):
        raw.get_data(units="uV")
    # str: invalid unit
    with pytest.raises(ValueError, match="is not a valid unit"):
        raw.get_data(picks=["eeg"], units="fV/cm")

    # dict: combination of simple and fraction units
    data_dict = raw.get_data(units=dict(grad="fT/cm", mag="fT", eeg="uV"))
    assert data_dict.shape == (376, 14400)
    assert_array_almost_equal(data_dict[0, -1], -3.857421923113974e-12 * (1e15 / 1e2))
    assert_array_almost_equal(data_dict[2, -1], -2.1478272253525944e-13 * 1e15)
    assert_array_almost_equal(data_dict[-2, -1], 7.665637356879529e-05 * 1e6)
    # dict: channel type not in instance
    data_dict_notin = raw.get_data(units=dict(hbo="uM"))
    assert data_dict_notin.shape == (376, 14400)
    assert_array_almost_equal(data_dict_notin[-3:, -1], last)
    # dict: one invalid unit
    with pytest.raises(ValueError, match="is not a valid unit"):
        raw.get_data(units=dict(grad="fT/cV", mag="fT", eeg="uV"))
    # dict: one invalid channel type
    with pytest.raises(KeyError, match="is not a channel type"):
        raw.get_data(units=dict(bad_type="fT/cV", mag="fT", eeg="uV"))

    # not the good type
    with pytest.raises(TypeError, match="instance of None, str, or dict"):
        raw.get_data(units=["fT/cm", "fT", "uV"])

def test_repr_dig_point():
    """Test printing of DigPoint."""
    dp = DigPoint(
        r=np.arange(3),
        coord_frame=FIFF.FIFFV_COORD_HEAD,
        kind=FIFF.FIFFV_POINT_EEG,
        ident=0,
    )
    assert "mm" in repr(dp)

    dp = DigPoint(
        r=np.arange(3),
        coord_frame=FIFF.FIFFV_MNE_COORD_MRI_VOXEL,
        kind=FIFF.FIFFV_POINT_CARDINAL,
        ident=0,
    )
    assert "mm" not in repr(dp)
    assert "voxel" in repr(dp)

def test_get_data_tmin_tmax():
    """Test tmin and tmax parameters of get_data method."""
    fname = Path(__file__).parent / "data" / "test_raw.fif"
    raw = read_raw_fif(fname)

    # tmin and tmax just use time_as_index under the hood
    tmin, tmax = (1, 9)
    d1 = raw.get_data()
    d2 = raw.get_data(tmin=tmin, tmax=tmax)

    idxs = raw.time_as_index([tmin, tmax])
    assert_allclose(d1[:, idxs[0] : idxs[1]], d2)

    # specifying a too low tmin truncates to idx 0
    d3 = raw.get_data(tmin=-5)
    assert_allclose(d3, d1)

    # specifying a too high tmax truncates to idx n_times
    d4 = raw.get_data(tmax=1e6)
    assert_allclose(d4, d1)

    # when start/stop are passed, tmin/tmax are ignored
    d5 = raw.get_data(start=1, stop=2, tmin=tmin, tmax=tmax)
    assert d5.shape[1] == 1

    # validate inputs are properly raised
    with pytest.raises(TypeError, match="start must be .* int"):
        raw.get_data(start=None)

    with pytest.raises(TypeError, match="stop must be .* int"):
        raw.get_data(stop=2.3)

    with pytest.raises(TypeError, match="tmin must be .* float"):
        raw.get_data(tmin=[1, 2])

    with pytest.raises(TypeError, match="tmax must be .* float"):
        raw.get_data(tmax=[1, 2])

def test_resamp_noop():
    """Tests resampling doesn't affect data if sfreq is identical."""
    raw = read_raw_fif(raw_fname)
    data_before = raw.get_data()
    data_after = raw.resample(sfreq=raw.info["sfreq"]).get_data()
    assert_array_equal(data_before, data_after)

def test_concatenate_raw_dev_head_t():
    """Test concatenating raws with dev-head-t including nans."""
    data = np.random.randn(3, 10)
    info = create_info(3, 1000.0, ["mag", "grad", "grad"])
    raw = RawArray(data, info)
    raw.info["dev_head_t"] = Transform("meg", "head", np.eye(4))
    raw.info["dev_head_t"]["trans"][0, 0] = np.nan
    raw2 = raw.copy()
    concatenate_raws([raw, raw2])

def test_last_samp():
    """Test that getting the last sample works."""
    raw = read_raw_fif(raw_fname).crop(0, 0.1).load_data()
    last_data = raw._data[:, [-1]]
    assert_array_equal(raw[:, -1][0], last_data)


"""build a kernel and using it to convolve the image and find the traffic lights in the image"""

import numpy as np
from scipy import signal as sg
from skimage.feature import peak_local_max
from skimage import img_as_float

def create_kernel():
    """create a kernel"""
    kernel = np.array([
        [1/2,  1/2,   1/4,   1/4,   1/8,   1/8],
        [1/2,  1/4,   1/4,   1/8,   1/8,  -1/2],
        [1/4,  1/4,   1/8,   1/8,   1/8,  -1/2],
        [1/4,  1/8,   1/8,   0,     0,    -1/2],
        [1/8,  1/8,   1/8,  -1/2,  -1/2,  -1/2],
        [1/8,  1/8,  -1/2,  -1/2,  -1/2,  -1/2]
    ])

    return kernel

def find_tfl_lights(c_image: np.ndarray):
    """find the green and red lights for a given image"""

    kernel = create_kernel()

    red_x = []
    red_y = []
    green_x = []
    green_y = []

    if c_image is not None:
        red = img_as_float(c_image[:, :, 0])
        green = img_as_float(c_image[:, :, 1])

        conv_red = sg.convolve(red, kernel, mode='same')
        conv_green = sg.convolve(green, kernel, mode='same')

        # get the lightest coordinates of the convolved images
        red_coordinates = peak_local_max(conv_red, min_distance=70, num_peaks=10)
        green_coordinates = peak_local_max(conv_green, min_distance=70, num_peaks=10)

        # for every red coordinate, add to red_x and red_y if the red color is dominant
        for coordinate in red_coordinates:
            rgb_pixel = c_image[coordinate[0]][coordinate[1]] # get the pixel (rgb) from origin image by the coordinate of red_coordinates
            if rgb_pixel[0] > rgb_pixel[1] and rgb_pixel[0] > rgb_pixel[2]:
                red_x.append(coordinate[1])
                red_y.append(coordinate[0])
        # for every green coordinate, add to green_x and green_y if the green color is dominant
        for coordinate in green_coordinates:
            rgb_pixel = c_image[coordinate[0]][coordinate[1]] # get the pixel (rgb) from origin image by the coordinate of green_coordinates
            if rgb_pixel[1] >= rgb_pixel[0]:
                green_x.append(coordinate[1])
                green_y.append(coordinate[0])

    return red_x, red_y, green_x, green_y


from django.dispatch import receiver
from django.template.loader import render_to_string
from django.urls import reverse_lazy
from django.utils.translation import gettext_lazy as _

from ephios.core.signals import nav_link, shift_forms, shift_info
from ephios.plugins.simpleresource.forms import ResourceAllocationForm
from ephios.plugins.simpleresource.models import ResourceAllocation

@receiver(shift_info, dispatch_uid="ephios.plugins.simpleresource.signals.shift_info")
def display_shift_resources(shift, request, **kwargs):
    try:
        allocation = ResourceAllocation.objects.get(shift=shift)
        if allocation.resources.exists():
            return render_to_string(
                "simpleresource/resource_allocations.html", {"allocation": allocation}, request
            )
    except ResourceAllocation.DoesNotExist:
        pass

@receiver(nav_link, dispatch_uid="ephios.plugins.simpleresource.signals.nav_link")
def add_nav_link(sender, request, **kwargs):
    return (
        [
            {
                "label": _("Resources"),
                "url": reverse_lazy("simpleresource:resource_list"),
                "active": request.resolver_match
                and request.resolver_match.app_name == "simpleresource",
            }
        ]
        if request.user.has_perm("simpleresource.add_resource")
        else []
    )

@receiver(shift_forms, dispatch_uid="ephios.plugins.simpleresource.signals.shift_forms")
def resource_allocation_form(sender, shift, request, **kwargs):
    return [ResourceAllocationForm(request.POST or None, shift=shift)]


# 7. Define a function that can accept two strings as input and print the string with the maximum length in the console. If two strings have the same length, then the function should print both the strings line by line.

def max_length(str1, str2):
    len1 = len(str1)
    len2 = len(str2)

    if len(str1) == len(str2):
        print(str1,"\n",str2)

    elif len1 > len2:
        print(str1)
    else:
        print(str2)

if __name__ == "__main__":
    max_length("hello", "Jainika")


#Input : LOAN AMOUNT, RATE OF INTEREST , LOAN PERIOD
#CALCULATE SIMPLE INTEREST, TOTAL PAYABLE AMOUNT

loan_amt=input("Enter Loan Amount you need :")
loan_period=input("Enter period you want to pay the loan :")
rate_interest=input("Enter rate of interest :")

loan_amt=int(loan_amt)
loan_period=int(loan_period)
rate_interest=int(rate_interest)

SI=(loan_amt*rate_interest*loan_period)/100

Total_amt=loan_amt+SI

EMI=Total_amt/loan_period

print("Total Payable Amount is : %s"%(Total_amt))
print("EMI per year is : %s"%(EMI))
print("EMI per month is : %s"%(EMI/12))

CI=loan_amt*((1+(rate_interest/100))**loan_period)

print("Compound interest : %s"%(CI))


import sys
input = sys.stdin.readline

lca_cache = dict()

def depth(a):
    d = 0
    while a != 0:
        a = parent[a]
        d += 1
    return d

def lca(a, b):
    # Lowest common ancestor
    if (a,b) not in lca_cache:
        da = depth(a)
        db = depth(b)
        pa = a
        pb = b
        while da > db:
            pa = parent[pa]
            da -= 1
        while db > da:
            pb = parent[pb]
            db -= 1
        while pa != pb:
            pa = parent[pa]
            pb = parent[pb]
        lca_cache[a,b] = pa
    return lca_cache[a,b]

def paint(a, b, c):
    common_ancestor = lca(a, b)
    while a != common_ancestor:
        color[a] = c
        a = parent[a]
    while b != common_ancestor:
        color[b] = c
        b = parent[b]

def move(a, b):
    lca_cache = dict()
    parent[a] = b

def count(a, b):
    colors = dict()
    common_ancestor = lca(a, b)
    while a != common_ancestor:
        if color[a] not in colors:
            colors[color[a]] = True
        a = parent[a]
    while b != common_ancestor:
        if color[b] not in colors:
            colors[color[b]] = True
        b = parent[b]
    print(len(colors))

N, K = map(int, input().split())

parent = [0]*N
color = [0]*N

for k in range(K):
    mapobj = map(int, input().split())
    r = next(mapobj)
    if r == 1:
        paint(*mapobj)
    elif r == 2:
        move(*mapobj)
    elif r == 3:
        count(*mapobj)

Baekjoon Online Judge/problem/10838/25461922 시간 초과.py


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

import os

from PyQt5 import uic, QtWidgets, QtGui
from qgis.core import Qgis, QgsAuthMethodConfig, QgsApplication
from qgis.PyQt.QtWidgets import QMessageBox

from .settings_manager import SettingsManager
from . import utils

def _token_format_valid(token):
    if "_" in token:
        parts = token.split("_")
        if len(parts) == 2:
            if len(parts[0]) == 32 and len(parts[1]) >= 64:
                return True
    return False

class ConfigureDialog(QtWidgets.QDialog):
    def __init__(self):
        super().__init__()

        self.ui = uic.loadUi(os.path.join(os.path.dirname(
            __file__), 'configure_dialog_base.ui'), self)

        self.ui.button_box.accepted.connect(self._accepted)
        self.ui.button_box.rejected.connect(self._rejected)

        # Load saved token
        smanager = SettingsManager()
        auth_cfg_id = smanager.get_setting('auth_cfg_id')
        if auth_cfg_id:
            am = QgsApplication.authManager()
            cfg = QgsAuthMethodConfig()
            am.loadAuthenticationConfig(auth_cfg_id, cfg, True)
            token = cfg.configMap().get("token")
            self.ui.token_txt.setText(token)

        # e.g. QGIS3.10.4 -> 31004
        qgis_version_str = str(Qgis.QGIS_VERSION_INT)
        #major_ver = int(qgis_version_str[0])
        minor_ver = int(qgis_version_str[1:3])
        #micro_ver = int(qgis_version_str[3:])

        # Checkbox are available only when on QGIS version having feature of Vectortile
        if minor_ver > 12:
            self.ui.vtileCheckBox.setEnabled(True)
            prefervector = bool(int(smanager.get_setting('prefervector')))
            self.ui.vtileCheckBox.setChecked(prefervector)

        # when OS in darkmode change icon to darkmode one
        if utils.is_in_darkmode():
            darkmode_icon_path = os.path.join(os.path.dirname(os.path.realpath(__file__)),
                                              "imgs",
                                              "logo_maptiler_dark.svg")
            pixmap = QtGui.QPixmap(darkmode_icon_path)
            self.ui.label_2.setPixmap(pixmap)

    def _accepted(self):
        # get and store UI values
        token = self.ui.token_txt.text()
        if token:
            cfg = QgsAuthMethodConfig('MapTilerHmacSha256')
            if _token_format_valid(token):
                smanager = SettingsManager()
                am = QgsApplication.authManager()
                auth_cfg_id = smanager.get_setting('auth_cfg_id')
                if auth_cfg_id:
                    (res, cfg) = am.loadAuthenticationConfig(auth_cfg_id, cfg, True)
                    if res:
                        saved_token = cfg.configMap().get("token")
                        if not saved_token == token:
                            cfg.setConfigMap({'token': token})
                            (res, cfg) = am.storeAuthenticationConfig(cfg, True)
                            if res:
                                smanager.store_setting('auth_cfg_id', cfg.id())
                    else:
                        cfg.setName('qgis-maptiler-plugin')
                        cfg.setConfigMap({'token': token})
                        (res, cfg) = am.storeAuthenticationConfig(cfg, True)
                        if res:
                            smanager.store_setting('auth_cfg_id', cfg.id())
                else:
                    cfg.setName('qgis-maptiler-plugin')
                    cfg.setConfigMap({'token': token})
                    (res, cfg) = am.storeAuthenticationConfig(cfg, True)
                    if res:
                        smanager.store_setting('auth_cfg_id', cfg.id())
                prefervector = str(int(self.ui.vtileCheckBox.isChecked()))
                smanager.store_setting('prefervector', prefervector)
                self.close()
            else:
                self.ui.label_6.setText(f"Not a valid token format. (Use token, not API key.)")
        else:
            self.ui.label_6.setText(f"Token is required.")

    def _rejected(self):
        self.close()


import os
import sys
import time
import random
import pprint

sys.path.append(os.path.dirname(os.path.dirname(__file__)))
from MainGoogle import MainGoogle

## 代理使用演示
PROXIES = [{
    'http': 'http://127.0.0.1:10086',
    'https': 'http://127.0.0.1:10086'
}]

# 或者 mg = MainGoogle(PROXIES)
mg = MainGoogle()

# Get {'title','url','text'}
for i in mg.search(query='python', num=1, language='en'):
    pprint.pprint(i)

time.sleep(random.randint(1, 5))

# Output
# {'text': 'The official home of the Python Programming Language.',
#  'title': 'Welcome to Python.org',
#  'url': 'https://www.python.org/'}
# {'text': '', 'title': ''}

# Get 第一页
for url in mg.search_url(query='python'):
    pprint.pprint(url)

time.sleep(random.randint(1, 5))

# Output
# 'https://www.python.org/'
# 'https://www.python.org/getit/'
# 'https://www.python.org/about/apps/'
# 'https://www.python.org/about/gettingstarted/'
# 'https://docs.python.org/3/tutorial/'
# 'https://www.python.org/psf/'
# 'https://www.python.org/about/'
# 'https://www.w3schools.com/python/'
# 'https://en.wikipedia.org/wiki/Python_(programming_language)'
# 'https://medium.freecodecamp.org/what-can-you-do-with-python-the-3-main-applications-518db9a68a78'
# 'https://www.codecademy.com/learn/learn-python'
# 'https://www.datacamp.com/courses/intro-to-python-for-data-science'

# Get 第二页
for url in mg.search_url(query='python', start=10):
    pprint.pprint(url)

# Output
# 'https://www.tutorialspoint.com/python/'
# 'https://github.com/python'
# 'https://github.com/python/cpython'
# 'https://hackernoon.com/why-is-python-so-slow-e5074b6fe55b'
# 'https://developers.slashdot.org/story/18/07/20/227246/is-python-the-future-of-programming'
# 'https://www.coursera.org/lecture/python/video-welcome-to-python-guido-van-rossum-NhDlc'
# 'http://www.pplex.ch/home'
# 'https://www.udemy.com/complete-python-bootcamp/'
# 'https://www.edx.org/course/introduction-to-python-absolute-beginner-0'
# 'https://www.geeksforgeeks.org/python-programming-language/'