text:
import plotly.graph_objects as go
import pandas as pd
from random import sample
import numpy as np
import datetime

class barplot(object):
    '''
    Class to make bar chart 'race' plots using plotly.

    Arguments:
    * item_column: usually a string denoting the column that you wish to rank (e.g. countries, food, people...)
    * value_column: numerical value

    '''

    def __init__(self, df: pd.DataFrame = None, item_column: str = None, value_column: str = None , time_column: str = None , item_color: str = None , top_entries: int = 10):
        self.df = df
        self.item_column = item_column
        self.value_column = value_column
        self.time_column = time_column
        self.item_color = item_color
        self.top_entries = top_entries
        self.title = ''
        self.fig = None
        self.orientation = None
        self.date_format = None

    def plot(self, title: str = None, orientation: str = 'horizontal', initial_frame = 'min', value_label: str = None, item_label: str = None, time_label: str = 'Date: ', frame_duration: int = 500, date_format: str = None):
        '''
        By default the time variable is appended to the title string

        * self.orientation: if this is horizontal the value_column is the x-axis, if this is set to
        vertical the value_column is the y_axis
        '''

        self.orientation = orientation # record last self.orientation used
        self.date_format = date_format # record last date_format
        self.title = title

        #get colors
        self.__get_colors()

        # make frame1, the one that appears before the animation playy
        self.__make_frame1(initial_frame)

        # define ui: adds Play,Pause buttons and defines empty slider
        self.__define_ui(time_label)

        # make frames, also updates sliders at each frame
        self.fig['frames'] = self.__make_frames(title, frame_duration)

        # update sliders in layout to sliders_dict with all the steps defined
        self.fig["layout"]["sliders"] = [self.sliders_dict]

        if (item_label is not None) or (value_label is not None):
            if orientation == 'horizontal':
                self.fig.update_xaxes(title_text= value_label,
                        visible = True, showticklabels= True)
                self.fig.update_yaxes(title_text= item_label,
                        visible = True, showticklabels= False)
            else:
                self.fig.update_xaxes(title_text= item_label,
                        visible = True, showticklabels= False)
                self.fig.update_yaxes(title_text= value_label,
                        visible = True, showticklabels= True)

        self.fig.layout.updatemenus[0].buttons[0].args[1]['frame']['duration'] = frame_duration
        self.fig.layout.updatemenus[0].buttons[0].args[1]['transition']['duration'] = frame_duration

        return self.fig

    def __make_frames(self, title: str, frame_duration: int):
        '''
        bar_race.make_frames automatically makes frame fromt the earliest to the latest available time stamp
        '''

        x, y = self.__check_orientation()
        frames = []
        ## sorted date to iterate through them
        dates = np.sort(self.df[self.time_column].unique())
        for date in dates:

            # specified date string for plotly frame id and for printing in plot
            if isinstance(date, np.datetime64):
                date = pd.to_datetime(str(date))
                try:
                    date_str = date.strftime(format = self.date_format) if self.date_format is not None else str(date)
                except:
                    raise Exception("Something was wrong setting the date_format, please check the strftime (https://strftime.org/) documentation for date formatting and try again")
            else:
                date_str = str(date)

            # filter out by year
            snap_data = self.df[self.df[self.time_column] == date]

            # get_top 10
            snap_data = snap_data.sort_values(self.value_column, ascending=False).iloc[:self.top_entries,:]

            # get top enttry at top of chart
            snap_data = snap_data.sort_values(self.value_column, ascending=True)

            # make frame
            frames.append(
                go.Frame(
                    data=[
                        go.Bar(
                            x=snap_data[x],
                            y=snap_data[y],
                            marker_color=snap_data['color'],
                            cliponaxis=False,
                            hoverinfo='all',
                            hovertemplate = '<extra></extra>', #annoying and obscure, see docs
                            ## (https://community.plotly.com/t/remove-trace-0-next-to-hover/33731)
                            textposition='outside',
                            texttemplate='%{x}
%{y:.4s}' if self.orientation == 'vertical' else '%{y}
%{x:.4s}',
                            textangle = 0,
                            orientation = 'h' if self.orientation == 'horizontal' else 'v'
                        )
                    ],
                    layout=go.Layout(
                        font={'size': 14},
                        plot_bgcolor = '#FFFFFF',
                        xaxis={
                            'showline': False,
                            'visible': True
                        } if self.orientation == 'vertical' else
                        {
                            'showline': True,
                            'visible': True,
                            'range': (0, self.df[self.value_column].max())
                        },
                        yaxis={
                            'showline': True,
                            'visible': True,
                            'range': (0, self.df[self.value_column].max())
                        } if self.orientation == 'vertical' else
                        {
                            'showline': False,
                            'visible': True
                        },
                        bargap=0.15,
                        title= title
                    ),
                    name = date_str
                )
            )

            slider_step = {"args": [
                [date_str],
                {"frame": {"duration": frame_duration, "redraw": False},
                 "mode": "immediate",
                 "transition": {"duration": frame_duration}}
            ],
                           "label": date_str,
                           "method": "animate"}
            self.sliders_dict["steps"].append(slider_step)

        return frames

    def __make_frame1(self, time_frame1 = 'min'):
        """
        Internal use

        time_frame1 can be a number that equals one of the entries in the time column
        or key values such as 'min' or 'max'
        """

        if time_frame1 == 'min':
            time_frame1 = self.df[self.time_column].min()
        elif time_frame1 == 'max':
            time_frame1 = self.df[self.time_column].max()

        # filter by year
        frame1 = self.df[self.df[self.time_column] == time_frame1]

        # get top entries
        frame1  = frame1.sort_values(self.value_column, ascending=False).iloc[:self.top_entries,:]

        # return in ascending order so that top bar corresponds to largest value
        frame1 = frame1.sort_values(self.value_column, ascending=True)

        x, y = self.__check_orientation()

        # Create figure
        self.fig = go.Figure(
            data=[
                go.Bar(
                    x=frame1[x],
                    y=frame1[y],
                    marker_color=frame1['color'],
                    hoverinfo='all',
                    textposition='outside',
                    texttemplate='%{x}
%{y:.4s}' if self.orientation == 'vertical' else '%{y}
%{x:.4s}',
                    textangle= 0,
                    cliponaxis=False,
                    orientation = 'h' if self.orientation == 'horizontal' else 'v'
                )
            ],
            layout=go.Layout(
                font={'size': 14},
                plot_bgcolor = '#FFFFFF',
                xaxis={
                    'showline': False,
                    'visible': False
                } if self.orientation == 'vertical' else
                {
                    'showline': True,
                    'visible': True,
                    'range': (0, self.df[self.value_column].max())
                },
                yaxis={
                    'showline': True,
                    'visible': True,
                    'range': (0, self.df[self.value_column].max())
                } if self.orientation == 'vertical' else
                {
                    'showline': False,
                    'visible': False
                },
                bargap=0.15,
                title= self.title
            ),
            frames = []
        )

    def __check_orientation(self):

        if self.orientation == 'horizontal':
            x = self.value_column
            y = self.item_column
        elif self.orientation == 'vertical':
            y = self.value_column
            x = self.item_column
        else:
            raise Exception('Please provide a valid value for orientation: horizontal or vertical')

        return x, y

    def __define_ui(self, time_label: str = None):

        self.fig["layout"]["updatemenus"] = [
            {
                "buttons": [
                    {
                        "args": [None, {"frame": {"duration": 500, "redraw": False},
                                        "fromcurrent": True, "transition": {"duration": 300,
                                                                            "easing": "quadratic-in-out"}}],
                        "label": "Play",
                        "method": "animate"
                    },
                    {
                        "args": [[None], {"frame": {"duration": 0, "redraw": False},
                                          "mode": "immediate",
                                          "transition": {"duration": 0}}],
                        "label": "Pause",
                        "method": "animate"
                    }
                ],
                "direction": "left",
                "pad": {"r": 10, "t": 87},
                "showactive": True,
                "type": "buttons",
                "x": 0.1,
                "xanchor": "right",
                "y": 0,
                "yanchor": "top"
            }
        ]

        self.sliders_dict = {
            "active": 0,
            "yanchor": "top",
            "xanchor": "left",
            "currentvalue": {
                "font": {"size": 20},
                "prefix": time_label,
                "visible": True,
                "xanchor": "right"
            },
            "transition": {"duration": 300, "easing": "cubic-in-out"},
            "pad": {"b": 10, "t": 50},
            "len": 0.9,
            "x": 0.1,
            "y": 0,
            "steps": [] # empty, gets filled when frames are made
        }

    def __get_colors(self):
        if (self.item_color == None):
            colors = {item: 'rgb({}, {}, {})'.format(*sample(range(256), 3)) for item in self.df[self.item_column].unique()}
            self.df['color'] = self.df[self.item_column].map(colors)
        elif (self.item_color != None and len(self.item_color) != self.df[self.item_column].nunique()):
            for item in self.df[self.item_column].unique():
                if item not in self.item_color.keys():
                    self.item_color[item] = 'rgb({}, {}, {})'.format(*sample(range(256), 3))
            self.df['color'] = self.df[self.item_column].map(self.item_color)
        else:
            self.df=self.df.rename(columns={self.item_color: "color"})


text:
import pandas as pd
import argparse
import numpy as np
import seaborn as sns
import matplotlib.pylab as plt

titles = ["Evaluation on all documents", "Evaluation on tweets only"]

def visualize_days(path, count):
    fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(9, 3))
    for enum, algo in enumerate(["louvain_macro_tfidf", "louvain_macro_tfidf tweets only"]):
        res = pd.DataFrame()
        for i in range(count):
            results = pd.read_csv(path.replace(".csv", "_{}.csv".format(i)))
            results = results[
                (results.algo == algo) & (results.weights_text == 1) & (results.similarity == 0.3) & (results.t == 0.7)]
            res = pd.concat([res, results])
        if count == 0:
            res = pd.read_csv(path)
            res = res[
                (res.algo == algo) & (res.weights_text == 1) & (res.similarity == 0.3) & (res.t == 0.7)]
        res["model"] = res.model.str.replace("VertexPartition", "")
        res["days"] = res["days"].astype(int)
        res = res.rename(columns={"days": "∆ (days)", "model": "objective function"})
        sns.pointplot(x="∆ (days)", y="f1", data=res, hue="objective function", ci="sd", ax=[ax1, ax2][enum])
        [ax1, ax2][enum].set_ylim(0.7, 0.95)
        [ax1, ax2][enum].title.set_text(titles[enum])
    if count == 0:
        plt.savefig("/".join(path.split("/")[0:-1]) + "/figures/louvain_macro_days_all_corpus.pdf",
                    bbox_inches='tight')
    else:
        plt.savefig("/".join(path.split("/")[0:-1]) + "/figures/louvain_macro_days.pdf",
                    bbox_inches='tight')
    plt.show()

def visualize_sim(path, count):
    fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(9, 3))
    for enum, algo in enumerate(["louvain_macro_tfidf", "louvain_macro_tfidf tweets only"]):
        res = pd.DataFrame()
        for i in range(count):
            results = pd.read_csv(path.replace(".csv", "_{}.csv".format(i)))
            results = results.drop_duplicates()
            results = results[
                (results.algo == algo) & (results.weights_text == 1) & (results.days == 1) & (
                            results.t == 0.7)  & (results.similarity < 0.7)]
            res = pd.concat([res, results])
        res["model"] = res.model.str.replace("VertexPartition", "")
        res = res.rename(columns={"similarity": "s", "model": "objective function"})
        sns.pointplot(x="s", y="f1", data=res, hue="objective function", ci="sd", ax=[ax1, ax2][enum])
        [ax1, ax2][enum].set_ylim(0.45, 0.95)
        [ax1, ax2][enum].title.set_text(titles[enum])
    plt.savefig("/".join(path.split("/")[0:-1]) + "/figures/louvain_macro_sim.pdf",
                bbox_inches='tight')
    plt.show()

def visualize_modalities(path, count):
    results = {}
    time = ["Jul 16 - Jul 22", "Jul 22 - Jul 27", "Jul 27 - Aug 1", "Aug 1 - Aug 6"]
    matrix_best_params = np.zeros((count, count))
    matrix_fsd = np.zeros((count, 1))
    matrix_text_only = np.zeros((count, 1))
    tot_max = 0
    params_text_only = ['\n'.join([label + ": " + value for label, value in zip(
        ["α text", "α url", "α htag", "∆"],
        ["1", "0", "0", "1"])])]
    for i in range(count):
        results[i] = pd.read_csv(path.replace(".csv", "_{}.csv".format(i)))
        matrix_fsd[i][0] = results[i][(results[i].algo == "FSD") & (results[i].t == 0.7)].f1.max()

    for algo in ["louvain_macro_tfidf", "louvain_macro_tfidf tweets only"]:
        params = []
        for i in range(count):
            res = results[i][(results[i].algo == algo) & (results[i].similarity == 0.3)]
            matrix_text_only[i][0] = res[(results[i].weights_text == 1) & (results[i].days == 1)].f1.max()
            matrix_best_params[i][i] = res.f1.max()
            p = res[res.f1 == matrix_best_params[i][i]].sort_values("days").tail(1).iloc[0]
            param_string = "\n".join([
                label + ": " + str(p[k]) for label, k in zip(
                    ["α text", "α url", "α htag"],
                    ["weights_text", "weights_url", "weights_hashtag"]
                )])
            param_string += "\n∆: {}".format(str(int(p["days"])))
            params.append(param_string)

            for j in range(count):
                if i != j:
                    other_res = results[j][(results[j].algo == algo) & (results[j].similarity == 0.3)]
                    matrix_best_params[j][i] = other_res[(other_res.t == p["t"])
                                        & (other_res.days == p["days"])
                                        & (other_res.weights_text == p["weights_text"])
                                        & (other_res.weights_url == p["weights_url"])
                                        & (other_res.weights_hashtag == p["weights_hashtag"])
                    ].iloc[0].f1
        tot_max = max(tot_max, matrix_best_params.max())
        plt.figure(figsize=(7, 4.5))
        ax1 = plt.subplot2grid((1, 10), (0, 0), colspan=7, rowspan=1)
        ax2 = plt.subplot2grid((1, 10), (0, 7), colspan=3, rowspan=1)
        sns.heatmap(matrix_best_params, annot=True, vmax=tot_max,
                    vmin=matrix_fsd.min(), cmap="Blues",
                         xticklabels=params, yticklabels=time, square=True, fmt='.3f', ax=ax1, cbar=False)

        # plt.savefig("/".join(path.split("/")[0:-1]) + "/figures/{}".format(algo), bbox_inches='tight')
        # plt.show()
        # plt.clf()
        sns.heatmap(matrix_text_only, annot=True, vmax=tot_max,
                    vmin=matrix_fsd.min(), cmap="Blues",
                    xticklabels=params_text_only, yticklabels=False, square=True, fmt='.3f', ax=ax2)

        plt.savefig("/".join(path.split("/")[0:-1]) + "/figures/{}.pdf".format(algo.replace(" ", "_")),
                    bbox_inches='tight',
                    )
        plt.show()
        plt.clf()

if __name__ == '__main__':
    parser = argparse.ArgumentParser(formatter_class=argparse.RawTextHelpFormatter)
    parser.add_argument('--path', required=False, default="/home/bmazoyer/Dev/news_twitter/results_event2018.csv")
    parser.add_argument('--count', required=False, default=0)
    args = vars(parser.parse_args())

    # visualize_modalities(**args)
    # visualize_sim(**args)
    visualize_days(**args)

ext: py

max_stars_repo_name: ina-foss/louvain-news-clustering

max_issues_repo_name: ina-foss/louvain-news-clustering

max_forks_repo_name: ina-foss/louvain-news-clustering

reason: import numpy


text:
import tensorflow as tf
import os
import numpy as np
from sklearn.cluster import KMeans

class network:

	def __init__(self, model, data_loader, nch, flags):
		self.model = model
		self.data_loader = data_loader
		self.nch = nch
		self.flags = flags
		self.__build_graph()

	def __build_graph(self):
		self.plhold_img, self.plhold_greylevel, self.plhold_latent, self.plhold_is_training, \
		self.plhold_keep_prob, self.plhold_kl_weight, self.plhold_lossweights \
		  = self.model.inputs()

		#inference graph
		self.op_mean, self.op_stddev, self.op_vae, \
		self.op_mean_test, self.op_stddev_test, self.op_vae_test, \
		self.op_vae_condinference \
			= self.model.inference(self.plhold_img, self.plhold_greylevel, \
				self.plhold_latent, self.plhold_is_training, self.plhold_keep_prob)

		#loss function and gd step for vae
		self.loss = self.model.loss(self.plhold_img, self.op_vae, self.op_mean, \
			self.op_stddev, self.plhold_kl_weight, self.plhold_lossweights)
		self.train_step = self.model.optimize(self.loss, epsilon=1e-6)

		#standard steps
		self.check_nan_op = tf.add_check_numerics_ops()
		self.init = tf.global_variables_initializer()
		self.saver = tf.train.Saver(max_to_keep=0)
		self.summary_op = tf.summary.merge_all()

	def train_vae(self, chkptdir, is_train=True):
		gpu_options = tf.GPUOptions(per_process_gpu_memory_fraction=0.95)
		sess = tf.Session(config=tf.ConfigProto(gpu_options=gpu_options))
		num_train_batches = np.int_(np.floor((self.data_loader.train_img_num*1.)/\
			self.flags.batch_size))
		if(is_train == True):
			sess.run(self.init)
			print('[DEBUG] Saving TensorBoard summaries to: %s/logs' % self.flags.out_dir)
			self.train_writer = tf.summary.FileWriter(os.path.join(self.flags.out_dir, 'logs'), sess.graph)

			#Train vae
			for epoch  in range(self.flags.max_epoch_vae):
				epoch_loss = self.run_vae_epoch_train(epoch, sess)
				epoch_loss = (epoch_loss*1.) / (self.flags.updates_per_epoch)
				print('[DEBUG] ####### Train VAE Epoch#%d, Loss %f #######' % (epoch, epoch_loss))
				self.__save_chkpt(epoch, sess, chkptdir, prefix='model_vae')
		else:
			self.__load_chkpt(sess, chkptdir)

		epoch_latentvars_train = self.run_vae_epoch_test(self.flags.max_epoch_vae,\
      sess, num_batches=num_train_batches, is_train=True)

		epoch_latentvars_test = self.run_vae_epoch_test(2, sess, num_batches=3, is_train=False)

		sess.close()
		return epoch_latentvars_train, epoch_latentvars_test

	def run_vae_epoch_train(self, epoch, sess):
		epoch_loss = 0.
		self.data_loader.random_reset()
		delta_kl_weight = (1e-2*1.)/(self.flags.max_epoch_vae*1.) #CHANGED WEIGHT SCHEDULE
		latent_feed = np.zeros((self.flags.batch_size, self.flags.hidden_size), dtype='f')
		for i in range(self.flags.updates_per_epoch):
			kl_weight = delta_kl_weight*(epoch)
			batch, batch_recon_const, batch_lossweights, batch_recon_const_outres = \
				self.data_loader.train_next_batch(self.flags.batch_size, self.nch)
			feed_dict = {self.plhold_img: batch, self.plhold_is_training:True, \
				self.plhold_keep_prob:.7, \
				self.plhold_kl_weight:kl_weight, \
				self.plhold_latent:latent_feed, \
				self.plhold_lossweights:batch_lossweights, \
				self.plhold_greylevel:batch_recon_const}
			try:
				_, _, loss_value, output, summary_str = sess.run(\
					[self.check_nan_op, self.train_step, self.loss,	\
					self.op_vae, self.summary_op], feed_dict)
			except:
				raise NameError('[ERROR] Found nan values in run_vae_epoch_train')
			self.train_writer.add_summary(summary_str, epoch*self.flags.updates_per_epoch+i)
			if(i % self.flags.log_interval == 0):
				self.data_loader.save_output_with_gt(output, batch, epoch, i, \
					'%02d_train_vae' % self.nch, self.flags.batch_size, \
					num_cols=8, net_recon_const=batch_recon_const_outres)
			epoch_loss += loss_value
		return epoch_loss

	def run_vae_epoch_test(self, epoch, sess, num_batches=3, is_train=False):
		self.data_loader.reset()
		kl_weight = 0.
		latentvars_epoch = np.zeros((0, 2*self.flags.hidden_size), dtype='f')
		latent_feed = np.zeros((self.flags.batch_size, self.flags.hidden_size), dtype='f')
		for i in range(num_batches):
			if(is_train == False):
				batch, batch_recon_const, batch_recon_const_outres, _ = \
					self.data_loader.test_next_batch(self.flags.batch_size, self.nch)
			else:
				batch, batch_recon_const, _, batch_recon_const_outres = \
					self.data_loader.train_next_batch(self.flags.batch_size, self.nch)

			batch_lossweights = np.ones((self.flags.batch_size, \
				self.nch*self.flags.img_height*self.flags.img_width), dtype='f')

			feed_dict = {self.plhold_img: batch, self.plhold_is_training:False, \
				self.plhold_keep_prob:1., \
				self.plhold_kl_weight:kl_weight, \
				self.plhold_latent:latent_feed, \
				self.plhold_lossweights: batch_lossweights, \
				self.plhold_greylevel:batch_recon_const}
			try:
				_, means_batch, stddevs_batch, output  = sess.run(\
					[self.check_nan_op, self.op_mean_test, self.op_stddev_test, \
						self.op_vae_test], feed_dict)
			except:
				raise NameError('[ERROR] Found nan values in run_vae_epoch_test')
			if(is_train == False):
				self.data_loader.save_output_with_gt(output, batch, epoch, i, \
					'%02d_test_vae' % self.nch, self.flags.batch_size, num_cols=8, \
					net_recon_const=batch_recon_const_outres)
			else:
				if(i % self.flags.log_interval == 0):
					self.data_loader.save_output_with_gt(output, batch, epoch, i, \
						'%02d_latentvar' % self.nch, self.flags.batch_size, num_cols=8, \
						net_recon_const=batch_recon_const_outres)

			latentvars_epoch  = np.concatenate((latentvars_epoch, \
				np.concatenate((means_batch, stddevs_batch), axis=1)), axis=0)
		return latentvars_epoch

	def run_cvae(self, chkptdir, latentvars, num_batches=3, num_repeat=8, num_cluster=5):
		gpu_options = tf.GPUOptions(per_process_gpu_memory_fraction=0.95)
		sess = tf.Session(config=tf.ConfigProto(gpu_options=gpu_options))
		self.__load_chkpt(sess, chkptdir)
		self.data_loader.reset()
		kl_weight = 0.
		for i in range(num_batches):
			print ('[DEBUG] Batch %d (/ %d)' % (i, num_batches))
			batch, batch_recon_const, batch_recon_const_outres, batch_imgnames = \
				self.data_loader.test_next_batch(self.flags.batch_size, self.nch)
			batch_lossweights = np.ones((self.flags.batch_size, \
				self.nch*self.flags.img_height*self.flags.img_width), dtype='f')
			for j in range(self.flags.batch_size):
				output_all = np.zeros((0, self.nch*self.flags.img_height*self.flags.img_width), dtype='f')
				print ('[DEBUG] Batch %d (/ %d), Img %d' % (i, num_batches, j))
				for k in range(num_repeat):
					imgid = i*self.flags.batch_size+j
					batch_1 = np.tile(batch[j, ...], (self.flags.batch_size, 1))
					batch_recon_const_1 = np.tile(batch_recon_const[j, ...], (self.flags.batch_size, 1))
					batch_recon_const_outres_1 = np.tile(batch_recon_const_outres[j, ...], (self.flags.batch_size, 1))

					latent_feed = np.random.normal(loc=0., scale=1., \
						size=(self.flags.batch_size, self.flags.hidden_size))
					#latent_feed = latentvars[imgid*self.flags.batch_size:(imgid+1)*self.flags.batch_size, ...]
					feed_dict = {self.plhold_img:batch_1, self.plhold_is_training:False, \
						self.plhold_keep_prob:1., \
						self.plhold_kl_weight:kl_weight, \
						self.plhold_latent:latent_feed, \
						self.plhold_lossweights: batch_lossweights,
						self.plhold_greylevel:batch_recon_const_1}
					try:
						_, output  = sess.run(\
							[self.check_nan_op, self.op_vae_condinference], \
							feed_dict)
					except:
						raise NameError('[ERROR] Found nan values in condinference_vae')
					output_all = np.concatenate((output_all, output), axis=0)

				print ('[DEBUG] Clustering %d predictions' % output_all.shape[0])
				kmeans = KMeans(n_clusters=num_cluster, random_state=0).fit(output_all)
				output_clust = kmeans.cluster_centers_
				self.data_loader.save_divcolor(output_clust, batch_1[:num_cluster], i, j, \
					'cvae', num_cluster, batch_imgnames[j], num_cols=8, \
					net_recon_const=batch_recon_const_outres_1[:num_cluster])

		sess.close()

	def run_divcolor(self, chkptdir, latentvars, num_batches=3, topk=8):
		gpu_options = tf.GPUOptions(per_process_gpu_memory_fraction=0.95)
		sess = tf.Session(config=tf.ConfigProto(gpu_options=gpu_options))
		self.__load_chkpt(sess, chkptdir)
		self.data_loader.reset()
		kl_weight = 0.
		nmix = topk
		for i in range(num_batches):
			batch, batch_recon_const, batch_recon_const_outres, batch_imgnames = \
				self.data_loader.test_next_batch(self.flags.batch_size, self.nch)
			batch_lossweights = np.ones((self.flags.batch_size, \
				self.nch*self.flags.img_height*self.flags.img_width), dtype='f')
			output_all = np.zeros((0, \
				self.nch*self.flags.img_height*self.flags.img_width), dtype='f')
			for j in range(self.flags.batch_size):
				imgid = i*self.flags.batch_size+j
				print ('[DEBUG] Running divcolor on Image %d (/%d)' % \
          (imgid, self.data_loader.test_img_num))
				if(imgid >= self.data_loader.test_img_num):
					break
				batch_1 = np.tile(batch[j, ...], (self.flags.batch_size, 1))
				batch_recon_const_1 = np.tile(batch_recon_const[j, ...], (self.flags.batch_size, 1))
				batch_recon_const_outres_1 = np.tile(batch_recon_const_outres[j, ...], (self.flags.batch_size, 1))
				curr_means = latentvars[imgid, :self.flags.hidden_size*nmix].reshape(nmix, self.flags.hidden_size)
				curr_sigma = latentvars[imgid, self.flags.hidden_size*nmix:(self.flags.hidden_size+1)*nmix].reshape(-1)
				curr_pi = latentvars[imgid, (self.flags.hidden_size+1)*nmix:].reshape(-1)
				selectid = np.argsort(-1*curr_pi)
				latent_feed = np.tile(curr_means[selectid, ...], (np.int_(np.round((self.flags.batch_size*1.)/nmix)), 1))

				feed_dict = {self.plhold_img:batch_1, self.plhold_is_training:False, \
					self.plhold_keep_prob:1., \
					self.plhold_kl_weight:kl_weight, \
					self.plhold_latent:latent_feed, \
					self.plhold_lossweights: batch_lossweights,
					self.plhold_greylevel:batch_recon_const_1}

				_, output  = sess.run(\
						[self.check_nan_op, self.op_vae_condinference], \
						feed_dict)

				self.data_loader.save_divcolor(output[:topk], batch_1[:topk], i, j, \
					'divcolor', topk, batch_imgnames[j], num_cols=8, \
					net_recon_const=batch_recon_const_outres_1[:topk, ...])

		sess.close()

	def __save_chkpt(self, epoch, sess, chkptdir, prefix='model'):
		if not os.path.exists(chkptdir):
			os.makedirs(chkptdir)
		save_path = self.saver.save(sess, "%s/%s_%06d.ckpt" % (chkptdir, prefix, epoch))
		print("[DEBUG] ############ Model saved in file: %s ################" % save_path)

	def __load_chkpt(self, sess, chkptdir):
		ckpt = tf.train.get_checkpoint_state(chkptdir)
		if ckpt and ckpt.model_checkpoint_path:
			ckpt_fn = ckpt.model_checkpoint_path.replace('//', '/')
			print('[DEBUG] Loading checkpoint from %s' % ckpt_fn)
			self.saver.restore(sess, ckpt_fn)
		else:
			raise NameError('[ERROR] No checkpoint found at: %s' % chkptdir)


text:
# -*- coding: utf-8 -*-
import numpy as np
import matplotlib.pyplot as plt

class GradientDescentRegressor(object):
	"""
	GradientDescent

    Parameters
    ------------
	X: numpy array
		Features vector.
	y: numpy array
		Target vector.

	Attributes
	------------
	theta : 1-d numpy array, shape = [polynomial order + 1,]
		Parameters randomly initialized, with theta[0] corresponding
		to the intercept term

	method : str , values = "batch_gradient_descent" | "SGD" | "MBGD"
		Method used for finding optimal values of theta

	If gradient descent method is chosen:

		costs : 1-d numpy array,
			Cost function values for every iteration of gradient descent

		epochs: int
			Number of iterations of gradient descent to be performed
    """

	def __init__(self, X, y):
		self.X = X
		self.y = y

	def hypothesis(self, X, theta):
		"""
		Compute hypothesis "h" where:
		h(x) = theta_0 * x + theta_1

		Parameters
    	------------
    	X: numpy array
			Features vector.
    	theta: numpy array
			Parameters vector.

		Returns
    	------------
		numpy ndarray
		"""
		h = X.dot(theta)
		return h

	def compute_cost(self, error, y):
		"""
		Compute value of cost function "J", where:
		J(theta) = 1/m * ((X.dot(theta)) - y)**2

		Parameters
    	------------
    	X: numpy array
			Features vector.
		y: numpy array
			Target vector.
		theta: numpy array
			Parameters vector.

		Returns
    	------------
		Value of cost function J
		"""
		m = len(y)
		J = (1/m) * np.sum(error**2)
		return J

	def fit(self, epochs=100, learning_rate=0.1):
		"""
		Fit theta to the training set.

		Parameters
    	------------
    	epochs: int
			Number of iterations.
    	learning_rate: float
			Rate at which theta is updated

		Returns
    	------------
		numpy ndarray
		"""
		m = X.shape[0]
		theta = np.random.rand(2, 1)
		X_b = np.c_[np.ones((m, 1)), X]
		costs = []

		for epoch in range(epochs):
			h = self.hypothesis(X_b, theta)
			error = h - y
			gradients = 2/m * X_b.T.dot(error) # slope of the derivative of the cost function
			theta = theta - learning_rate * gradients # update parameters after each iteration
			cost = self.compute_cost(error, y)
			costs.append(cost)

		self.costs = costs
		self.epochs = epochs
		self.predictions = X_b.dot(theta)

		return theta

	def plot_cost(self):
		"""Plot number of gradient descent iterations versus cost function.

        Returns
        -----------
        matploblib figure
        """
		plt.figure()
		plt.plot(np.arange(1, self.epochs+1), self.costs)
		plt.show()

	def plot_model(self):
		"""Plot number of gradient descent iterations versus cost function.

        Returns
        -----------
        matploblib figure
        """
		plt.figure()
		plt.scatter(X, y)
		plt.plot(X, self.predictions, "g-")
		plt.show()

if __name__ == "__main__":
	X = 2 * np.random.rand(100, 1)
	y = 3 + 5 * X + np.random.rand(100, 1)

	gradient_descent = GradientDescentRegressor(X, y)

	print(gradient_descent.fit())
	gradient_descent.plot_cost()
	gradient_descent.plot_model()

max_stars_repo_name: gavin-bauer/recode-core-ml-from-scratch

max_issues_repo_name: gavin-bauer/recode-core-ml-from-scratch

max_forks_repo_name: gavin-bauer/recode-core-ml-from-scratch


text:
"""infinite_jukebox.py - (c) 2017 - Dave Rensin - dave@rensin.com

An attempt to re-create the amazing Infinite Jukebox (http://www.infinitejuke.com)
created by Paul Lamere of Echo Nest. Uses the Remixatron module to do most of the
work.

"""

import argparse
import curses
import curses.textpad
import numpy as np
import os
import pygame
import pygame.event
import pygame.locals
import signal
import soundfile as sf
import sys
import time

from Remixatron import InfiniteJukebox
from pygame import mixer

SOUND_FINISHED = pygame.locals.USEREVENT + 1

def process_args():

    """ Process the command line args """

    description = """Creates an infinite remix of an audio file by finding musically similar beats and computing a randomized play path through them. The default choices should be suitable for a variety of musical styles. This work is inspired by the Infinite Jukebox (http://www.infinitejuke.com) project creaeted by Paul Lamere (paul@spotify.com)"""

    epilog = """
    """

    parser = argparse.ArgumentParser(description=description, epilog=epilog, formatter_class=argparse.RawDescriptionHelpFormatter)

    parser.add_argument("filename", type=str,
                        help="the name of the audio file to play. Most common audio types should work. (mp3, wav, ogg, etc..)")

    parser.add_argument("-clusters", metavar='N', type=int, default=0,
                        help="set the number of clusters into which we want to bucket the audio. Default: 0 (automatically try to find the optimal cluster value.)")

    parser.add_argument("-start", metavar='start_beat', type=int, default=1,
                        help="start on a specific beat. Default: 1")

    parser.add_argument("-save", metavar='label', type=str,
                        help="Save the remix to a file, rather than play it. Will create file named [label].wav")

    parser.add_argument("-duration", metavar='seconds', type=int, default=180,
                        help="length (in seconds) to save. Must use with -save. Default: 180")

    parser.add_argument("-verbose", action='store_true',
                        help="print extra info about the track and play vector")

    parser.add_argument("-use_v1", action='store_true',
                        help="use the original auto clustering algorithm instead of the new one. -clusters must not be set.")

    return parser.parse_args()

def MyCallback(pct_complete, message):

    """ The callback function that gets status updates. Just prints a low-fi progress bar and reflects
        the status message passed in.

        Example: [######    ] Doing some thing...
    """

    progress_bar = " [" + "".ljust(int(pct_complete * 10),'#') + "".ljust(10 - int(pct_complete * 10), ' ') + "] "
    log_line =  progress_bar + message

    window.clear()
    window.addstr(1,0,log_line)
    window.refresh()

def display_playback_progress(v):

    """
        Displays a super low-fi playback progress map

        See README.md for details..

        Returns the time this function took so we can deduct it from the
        sleep time for the beat
    """

    time_start = time.time()

    term_width = curses.tigetnum('cols')

    y_offset = 11

    beat = v['beat']
    min_sequence = v['seq_len']
    current_sequence = v['seq_pos']

    # compute a segment map and display it. See README.md for an
    # explanation of segment maps and cluster maps.

    segment_map = ''
    segment_chars = '#-'

    for b in jukebox.beats:
        segment_map += segment_chars[ b['segment'] % 2 ]

    window.addstr(y_offset,0,segment_map + " ")

    # highlight all the jump candidates in the segment
    # map

    for c in jukebox.beats[beat]['jump_candidates']:

        b = jukebox.beats[c]

        window.addch(y_offset + int(b['id'] / term_width),   # y position of character
                      b['id'] % term_width,                  # x position of character
                      ord(segment_chars[b['segment'] %2]),   # either '#' or '-' depending on the segment
                      curses.A_REVERSE)                      # print in reverse highlight

    # print the position tracker on the segment map

    x_pos = beat % term_width
    y_pos = int(beat/term_width) + y_offset

    beats_until_jump = min_sequence - current_sequence

    buj_disp = ''

    # show the beats until the next jump. If the value == 0 then
    # then sequence wanted to jump but couldn't find a suitable
    # target. Display an appropriate symbol for that (a frowny face, of course!)

    if beats_until_jump > 0:
        buj_disp = str(beats_until_jump).zfill(2)
    else:
        buj_disp = ':('

    window.addstr(y_pos, x_pos, buj_disp, curses.A_BOLD | curses.A_REVERSE | curses.A_STANDOUT )

    window.refresh()

    time_finish = time.time()

    return time_finish - time_start

def get_verbose_info():
    """Show statistics about the song and the analysis"""

    info = """
    filename: %s
    duration: %02d:%02d:%02d
       beats: %d
       tempo: %d bpm
    clusters: %d
    segments: %d
  samplerate: %d
    """

    (minutes,seconds) = divmod(round(jukebox.duration),60)
    (hours, minutes)  = divmod(minutes, 60)

    verbose_info = info % (os.path.basename(args.filename), hours, minutes, seconds,
                           len(jukebox.beats), int(round(jukebox.tempo)), jukebox.clusters, jukebox.segments,
                           jukebox.sample_rate)

    segment_map = ''
    cluster_map = ''

    segment_chars = '#-'
    cluster_chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890-=,.<>/?;:!@#$%^&*()_+'

    for b in jukebox.beats:
        segment_map += segment_chars[ b['segment'] % 2 ]
        cluster_map += cluster_chars[ b['cluster'] ]

    verbose_info += "\n" + segment_map + "\n\n"

    if args.verbose:
        verbose_info += cluster_map + "\n\n"

    verbose_info += jukebox._extra_diag

    return verbose_info

def get_window_contents():
    """Dump the contents of the current curses window."""

    tbox = curses.textpad.Textbox(window)
    tbox.stripspaces = False

    w_str = tbox.gather()

    return w_str

def cleanup():
    """Cleanup before exiting"""

    if not window:
        return

    w_str = get_window_contents()
    curses.curs_set(1)
    curses.endwin()

    print(w_str.rstrip())
    print

    mixer.quit()

def graceful_exit(signum, frame):

    """Catch SIGINT gracefully"""

    # restore the original signal handler as otherwise evil things will happen
    # in raw_input when CTRL+C is pressed, and our signal handler is not re-entrant
    signal.signal(signal.SIGINT, original_sigint)

    cleanup()
    sys.exit(0)

def save_to_file(jukebox, label, duration):
    ''' Save a fixed length of audio to disk. '''

    avg_beat_duration = 60 / jukebox.tempo
    num_beats_to_save = int(duration / avg_beat_duration)

    # this list comprehension returns all the 'buffer' arrays from the beats
    # associated with the [0..num_beats_to_save] entries in the play vector

    main_bytes = [jukebox.beats[v['beat']]['buffer'] for v in jukebox.play_vector[0:num_beats_to_save]]

    # main_bytes is an array of byte[] arrays. We need to flatten it to just a
    # regular byte[]

    output_bytes = np.concatenate( main_bytes )

    # write out the wav file
    sf.write(label + '.wav', output_bytes, jukebox.sample_rate, format='WAV', subtype='PCM_24')

if __name__ == "__main__":

    # store the original SIGINT handler and install a new handler
    original_sigint = signal.getsignal(signal.SIGINT)
    signal.signal(signal.SIGINT, graceful_exit)

    #
    # Main program logic
    #

    window = None

    args = process_args()

    curses.setupterm()

    window = curses.initscr()
    curses.curs_set(0)

    # do the clustering. Run synchronously. Post status messages to MyCallback()
    jukebox = InfiniteJukebox(filename=args.filename, start_beat=args.start, clusters=args.clusters,
                                progress_callback=MyCallback, do_async=False, use_v1=args.use_v1)

    # show more info about what was found
    window.addstr(2,0, get_verbose_info())
    window.refresh()

    # if we're just saving the remix to a file, then just
    # find the necessarry beats and do that

    if args.save:
        save_to_file(jukebox, args.save, args.duration)
        graceful_exit(0, 0)

    # it's important to make sure the mixer is setup with the
    # same sample rate as the audio. Otherwise the playback will
    # sound too slow/fast/awful

    mixer.init(frequency=jukebox.sample_rate)
    channel = mixer.Channel(0)

    # pygame's event handling functions won't work unless the
    # display module has been initialized -- even though we
    # won't be making any display calls.

    pygame.display.init()

    # register the event type we want fired when a sound buffer
    # finishes playing

    channel.set_endevent(SOUND_FINISHED)

    # queue and start playing the first event in the play vector. This is basic
    # audio double buffering that will reduce choppy audio from impercise timings. The
    # goal is to always have one beat in queue to play as soon as the last one is done.

    beat_to_play = jukebox.beats[ jukebox.play_vector[0]['beat'] ]

    snd = mixer.Sound(buffer=beat_to_play['buffer'])
    channel.queue(snd)

    display_playback_progress(jukebox.play_vector[0])

    # go through the rest of  the playback list, start playing each beat, display
    # the progress and wait for the playback to complete. Playback happens on another
    # thread in the pygame library, so we have to wait to be signaled to queue another
    # event.

    for v in jukebox.play_vector[1:]:

        beat_to_play = jukebox.beats[ v['beat'] ]

        snd = mixer.Sound(buffer=beat_to_play['buffer'])
        channel.queue(snd)

        pygame.event.wait()

        display_playback_progress(v)

max_stars_repo_name: LuxTheLord/Remixatron

max_issues_repo_name: LuxTheLord/Remixatron

max_forks_repo_name: LuxTheLord/Remixatron


text:
import torch
import sparseconvnet as scn
from ScanNet.unet_modular import Model
import sys
import NYU40_colors
from plyfile import PlyData
import glob
import data_modular
import os
import numpy as np

# VALID_CLAS_IDS have been mapped to the range {0,1,...,19}

pth_dir = '/usr/mount/v4rtemp/el/SparseConvNet/'
#pth_dir=''

num_classes = 20

remapper=np.ones(150)*(-100)
if num_classes is 20:
    # VALID_CLAS_IDS have been mapped to the range {0,1,...,19}
    for i,x in enumerate([1,2,3,4,5,6,7,8,9,10,11,12,14,16,24,28,33,34,36,39]):
        remapper[x]=i
if num_classes is 21:
    # VALID_CLAS_IDS have been mapped to the range {0,1,...,19}
    for i,x in enumerate([1,2,3,4,5,6,7,8,9,10,11,12,14,16,24,28,33,34,36,39,40]):
        remapper[x]=i

def visualize(ids, mesh_file, output_file):
    if not output_file.endswith('.ply'):
        sys.stderr.write('ERROR: ' + 'output file must be a .ply file' + '\n')
    colors = NYU40_colors.create_color_palette_legend()
    num_colors = len(colors)
    with open(mesh_file, 'rb') as f:
        plydata = PlyData.read(f)
        num_verts = plydata['vertex'].count
        if num_verts != len(ids):
           sys.stderr.write('ERROR: ' + '#predicted labels = ' + str(len(ids)) + 'vs #mesh vertices = ' + str(num_verts))
        # *_vh_clean_2.ply has colors already
        for i in range(num_verts):
            if ids[i]+1 >= num_colors:
               sys.stderr.write('ERROR: ' + 'found predicted label ' + str(ids[i]) + ' not in nyu40 label set')
            color = colors[ids[i]+1]
            plydata['vertex']['red'][i] = color[0]
            plydata['vertex']['green'][i] = color[1]
            plydata['vertex']['blue'][i] = color[2]
    plydata.write(output_file)

data_modular.batch_size = 1
data_loader = data_modular.load_val_data(pth_dir + 'val_20classes/')

use_cuda = torch.cuda.is_available()
exp_name='unet_scale20_m16_rep1_noResidualBlocks_20classes/unet_scale20_m16_rep1_noResidualBlocks_20classes'

unet=Model()
if use_cuda:
    unet=unet.cuda()

training_epoch=scn.checkpoint_restore(unet, exp_name,'unet',use_cuda)
print('training epoch: ' + str(training_epoch) + ' #classifer parameters', sum([x.nelement() for x in unet.parameters()]))

with torch.no_grad():
    unet.eval()
    scn.forward_pass_multiplyAdd_count = 0
    scn.forward_pass_hidden_states = 0
    store=torch.zeros(data_modular.valOffsets[-1], num_classes)   #valOffsets is the number of all verteces of all scenes
    for rep in range(1, 1 + data_modular.val_reps):
        for i, batch in enumerate(data_loader):    #one batch can contain several scenes
            if use_cuda:
                batch['x'][1] = batch['x'][1].cuda()
            predictions = unet(batch['x'])
            store.index_add_(0, batch['point_ids'], predictions.cpu())

labels = store.max(1)[1].numpy()

pth_files = sorted(glob.glob(pth_dir + 'val_20classes/' + '*.pth'))
ply_path = '/usr/mount/v4rtemp/datasets/ScanNet/ScanNetV2/scans/'
pth_save = pth_dir + 'unet_scale20_m16_rep1_noResidualBlocks_20classes/val_results/legend_labels/'
#pth_save = pth_dir + 'val/legend_color/'
for batch in data_loader:
    print(batch['id'])
    scene_ids = batch['id']  # all ids of this batch
    for s_id in scene_ids:
        f = os.path.basename(pth_files[s_id])
        ply_folder = f[0:12] + '/'   #get only the scene_name
        ply_file = ply_folder[:-1] + '_vh_clean_2.ply'
        #get the data for each scene
        start_idx = data_modular.valOffsets[s_id]
        end_idx = data_modular.valOffsets[s_id+1]
        scene_labels = labels[start_idx:end_idx]

        if num_classes != 40:
            for i, l in enumerate(labels):
                labels[i] = np.where(remapper == l)[0][0] - 1

        if not os.path.isfile(pth_save + ply_file[:-4]+'_pred.ply'):
            print(pth_save + ply_file[:-4]+'_pred.ply')
            visualize(scene_labels, ply_path + ply_folder + ply_file, pth_save + ply_file[:-4]+'_pred.ply')


text:
#!/usr/bin/env python

"""
   TODO:
   1. #ecm = EventCodeManager(evcode, verbos) - selection of events for event code is commented
   2. cmod = None - file is not saved
   3. deploy calib files in db
"""

#----------
import logging
logger = logging.getLogger(__name__)

import os
import sys
import psana
import numpy as np
from time import time, localtime, strftime

#from Detector.EventCodeManager import EventCodeManager
from psana.pyalgos.generic.NDArrUtils import print_ndarr, info_ndarr, divide_protected
from psana.pyalgos.generic.Utils import do_print
from psana import DataSource
from psana.pscalib.calib.NDArrIO import save_txt
from psana.pscalib.calib.CalibConstants import TSFORMAT

#----------

def plot_det_image(det, evt, nda, tit=''):
    """Plots averaged image
    """
    import psana.pyalgos.generic.Graphics as gr
    from psana.pyalgos.generic.NDArrUtils import reshape_to_2d
    #img = det.image(evt, nda)
    img = np.array(nda)
    if img is None: sys.exit('Image is not available. PROCESSING IS TERMINATED')
    img = reshape_to_2d(img)

    ave, rms = nda.mean(), nda.std()
    gr.plotImageLarge(img, amp_range=(ave-1*rms, ave+3*rms), title=tit)
    gr.show()

#----------

def evaluate_limits(arr, nneg=5, npos=5, lim_lo=1, lim_hi=1000, cmt=''):
    """Evaluates low and high limit of the array, which are used to find bad pixels.
    """
    ave, std = (arr.mean(), arr.std()) if (nneg>0 or npos>0) else (None,None)
    lo = ave-nneg*std if nneg>0 else lim_lo
    hi = ave+npos*std if npos>0 else lim_hi
    lo, hi = max(lo, lim_lo), min(hi, lim_hi)

    logger.debug('  %s: %s ave, std = %.3f, %.3f  low, high limits = %.3f, %.3f'%\
              (sys._getframe().f_code.co_name, cmt, ave, std, lo, hi))

    return lo, hi

#----------

def str_tstamp(fmt=TSFORMAT, time_sec=None): #fmt='%Y-%m-%dT%H:%M:%S
    return strftime(fmt, localtime(time_sec))

#----------

def replace(in_tmp, pattern, subst):
    """If pattern in the in_tmp replace it with subst.
       Returns str object in_tmp with replaced patterns.
    """
    fields = in_tmp.split(pattern, 1)
    if len(fields) > 1:
        return '%s%s%s' % (fields[0], subst, fields[1])
    else:
        return in_tmp

#----------

def fname_template(orun, detname, ofname, nevts, tsec=None, tnsec=None):
    """Replaces parts of the file name specified as
       #src, #exp, #run, #evts, #type, #date, #time, #fid, #sec, #nsec
       with actual values
    """
    template = replace(ofname,   '#src', detname)
    template = replace(template, '#exp', orun.expt)
    template = replace(template, '#run', 'r%04d'%orun.runnum)
    template = replace(template, '#type', '%s')

    t_sec  = tsec  if tsec  is not None else int(orun.timestamp>>32 & 0xFFFFFFFF)
    t_nsec = tnsec if tnsec is not None else int(orun.timestamp & 0xFFFFFFFF)

    template = replace(template, '#date', str_tstamp('%Y-%m-%d', t_sec))
    template = replace(template, '#time', str_tstamp('%H%M%S', t_sec))
    template = replace(template, '#sec',  '%d' % t_sec)
    template = replace(template, '#nsec', '%09d' % t_nsec)
    template = replace(template, '#evts', 'e%06d' % nevts)
    if not '%s' in template: template += '-%s'
    return template

#----------

class DetRawDarkProc:

    def __init__(self, orun, detname, args):

        self.args    = args
        self.orun    = orun
        self.detname = detname

        self.ofname = args.ofname
        self.events = args.events
        self.evskip = args.evskip

        self.int_lo = args.intlow if args.intlow is not None else 1
        self.int_hi = args.inthig if args.inthig is not None else 16000
        self.rms_lo = args.rmslow # if None - will be evaluated later
        self.rms_hi = args.rmshig # if None - will be evaluated later
        self.fraclm = args.fraclm

        self.plotim = args.plotim
        self.savebw = args.savebw

        self.intnlo = args.intnlo
        self.intnhi = args.intnhi
        self.rmsnlo = args.rmsnlo
        self.rmsnhi = args.rmsnhi

        self.evstg1 = max(self.events*0.05, 5)
        self.nsigma = args.nsigma
        self.addcdb = args.addcdb

        self.loglev = args.loglev
        self.verbos = o0377 if self.loglev=='DEBUG' else 0 # deprecated

        self.det     = None
        self.shape   = None
        self.counter = 0
        self.stage   = 0

        self.print_attrs()
        logger.info('Begin processing')

    def print_attrs(self):
        msg = '\n%s\nAttributes of the %s object' % (60*'_', self.__class__.__name__)\
            + '\ndetector name                   : %s'   % self.detname\
            + '\noutput file name template       : %s'   % self.ofname\
            + '\nnumber of events to collect     : %d'   % self.events\
            + '\nnumber of events to skip        : %d'   % self.evskip\
            + '\nnumber of events for stage 1    : %d'   % self.evstg1\
            + '\nnumber of sigma intensity range : %.3f' % self.nsigma\
            + '\ncontrol bit-word to plot images : %d'   % self.plotim\
            + '\ncontrol bit-word to save arrays : %d'   % self.savebw\
            + '\nINTENSITY low limit (ADU)       : %d'   % self.int_lo\
            + '\nINTENSITY high limit (ADU)      : %d'   % self.int_hi\
            + '\nRMS low limit (ADU)             : %d'   % self.rms_lo\
            + '\nRMS high limit (ADU)            : %d'   % self.rms_hi\
            + '\nallowed low/high fraction limit : %.3f' % self.fraclm\
            + '\n\nParameters for auto-evaluation of limits:'\
            + '\nnumber of sigma from mean for INTENSITY low limit  : %.3f' % self.intnlo\
            + '\nnumber of sigma from mean for INTENSITY high limit : %.3f' % self.intnhi\
            + '\nnumber of sigma from mean for RMS low limit        : %.3f' % self.rmsnlo\
            + '\nnumber of sigma from mean for RMS high limit       : %.3f' % self.rmsnhi\
            + '\n' + 60*'_'
        logger.info(msg)

    def _common_mode_pars(self, arr_ave, arr_rms, arr_msk):
        """Returns detector-dependent common mode parameters as np.array for a few detectors and None for others.
        """
        import PSCalib.GlobalUtils as gu
        import math

        dettype = gu.det_type_from_source(self.detname)

        ave = arr_ave[arr_msk>0].mean()
        #rms = arr_ave[arr_msk>0].std()
        rms = arr_rms[arr_msk>0].mean()

        logger.info('Evaluate common mode for source: %s det: %s, estimated intensity ave: %.3f  rms: %.3f' %\
                  (self.detname, gu.dic_det_type_to_name[dettype], ave, rms))

        if dettype == gu.PNCCD:
            return np.array((3, math.ceil(4*rms), math.ceil(4*rms), 128))

        #elif dettype == gu.EPIX100A:
        #    return np.array((4, 6, math.ceil(2*rms), math.ceil(2*rms)))

        #elif dettype == gu.CSPAD:
        #    return np.array((1, math.ceil(3*rms), math.ceil(2*rms), 100))

        #elif dettype == gu.CSPAD2X2:
        #    return np.array((1, math.ceil(3*rms), math.ceil(2*rms), 100))

        else:
            return None

    def _init_stage1(self, evt):

        if self.shape is not None: return True

        if self.det is None: self.det = self.orun.Detector(self.detname)

        if self.det is None:
            raise ValueError('Can not create the Detector object for name %s' % self.detname)

        ndaraw = self.det.raw(evt)

        if ndaraw is None: return False
        if ndaraw.size == 0: return False
        if ndaraw.shape[0] == 0: return False
        if not ndaraw.any(): return False

        self.ev1_sec = evt._seconds
        self.ev1_nsec = evt._nanoseconds
        logger.info('The 1st non-empty event time %d sec %d nsec' % (self.ev1_sec, self.ev1_nsec))
        #print('  YYYY evt.timestamp     :', evt.timestamp)
        #print('  YYYY evt.timestamp sec :', int(evt.timestamp>>32 & 0xFFFFFFFF))
        #print('  YYYY evt.timestamp nsec:', int(evt.timestamp     & 0xFFFFFFFF))

        self.shape=ndaraw.shape

        logger.info('Begin stage 1 for %s, raw.shape = %s, intensity limits low: %.1f  high: %.1f'%\
                    (self.detname, str(self.shape), self.int_lo, self.int_hi))

        self.arr0       = np.zeros(self.shape, dtype=np.int64)
        self.arr1       = np.ones (self.shape, dtype=np.int64)

        self.sta_int_lo = np.zeros(self.shape, dtype=np.int64)
        self.sta_int_hi = np.zeros(self.shape, dtype=np.int64)

        self.arr_sum0   = np.zeros(self.shape, dtype=np.int64)
        self.arr_sum1   = np.zeros(self.shape, dtype=np.double)
        self.arr_sum2   = np.zeros(self.shape, dtype=np.double)

        self.gate_lo    = self.arr1 * self.int_lo
        self.gate_hi    = self.arr1 * self.int_hi

        if self.savebw & 16: self.arr_max = np.zeros(self.shape, dtype=ndaraw.dtype)
        if self.savebw & 32: self.arr_min = np.ones (self.shape, dtype=ndaraw.dtype) * 0xffff

        self.stage = 1

        return True

    def event(self, evt):

        if not self._init_stage1(evt): return

        ndaraw = self.det.raw(evt)
        if ndaraw is None: return
        ndadbl = np.array(ndaraw, dtype=np.double)

        self._init_stage2(ndaraw)
        self._proc_event(ndadbl, ndaraw)

    def _proc_event(self, ndadbl, ndaraw):

        self.counter += 1

        cond_lo = ndaraw<self.gate_lo
        cond_hi = ndaraw>self.gate_hi
        condlist = (np.logical_not(np.logical_or(cond_lo, cond_hi)),)

        #logger.info('XXX:')
        #print_ndarr(self.arr1, 'arr1')
        #print_ndarr(self.arr_sum0, 'arr_sum0')
        #print_ndarr(self.arr_sum1, 'arr_sum1')
        #print_ndarr(self.arr_sum2, 'arr_sum2')

        self.arr_sum0   += np.select(condlist, (self.arr1,), 0)
        self.arr_sum1   += np.select(condlist, (ndadbl,), 0)
        self.arr_sum2   += np.select(condlist, (np.square(ndadbl),), 0)

        self.sta_int_lo += np.select((ndaraw<self.int_lo,), (self.arr1,), 0)
        self.sta_int_hi += np.select((ndaraw>self.int_hi,), (self.arr1,), 0)

        if self.savebw & 16: self.arr_max = np.maximum(self.arr_max, ndaraw)
        if self.savebw & 32: self.arr_min = np.minimum(self.arr_min, ndaraw)

    def _init_stage2(self, ndaraw):

        if self.stage & 2: return
        if self.counter < self.evstg1: return

        t0_sec = time()

        arr_av1 = divide_protected(self.arr_sum1, self.arr_sum0)
        arr_av2 = divide_protected(self.arr_sum2, self.arr_sum0)

        arr_rms = np.sqrt(arr_av2 - np.square(arr_av1))
        rms_ave = arr_rms.mean()

        gate_half = self.nsigma*rms_ave

        logger.info('%s\nBegin stage 2 for %s after %d events' % (80*'_', self.detname, self.counter))
        logger.info('  mean rms=%.3f x %.1f = intensity gate= +/- %.3f around pixel average intensity' %\
                  (rms_ave, self.nsigma, gate_half))

        #print_ndarr(arr_av1, 'arr_av1')

        self.gate_hi = np.minimum(arr_av1 + gate_half, self.arr1*self.int_hi)
        self.gate_lo = np.maximum(arr_av1 - gate_half, self.arr1*self.int_lo)

        self.gate_hi = np.array(self.gate_hi, dtype=ndaraw.dtype)
        self.gate_lo = np.array(self.gate_lo, dtype=ndaraw.dtype)

        self.arr_sum0 = np.zeros(self.shape, dtype=np.int64)
        self.arr_sum1 = np.zeros(self.shape, dtype=np.double)
        self.arr_sum2 = np.zeros(self.shape, dtype=np.double)

        self.stage = 2

        logger.info('Stage 2 initialization for %s consumes dt=%7.3f sec' % (self.detname, time()-t0_sec))

    def summary(self, evt):

        logger.info('%s\nRaw data for %s found/selected in %d events' % (80*'_', self.detname, self.counter))

        if self.counter:
            logger.info(', begin data summary stage')
        else:
            logger.info(', STOP processing, there are no arrays to save...')
            return

        t0_sec = time()

        # make shorter references
        det     = self.det
        ofname  = self.ofname
        plotim  = self.plotim
        savebw  = self.savebw
        int_hi  = self.int_hi
        int_lo  = self.int_lo
        fraclm  = self.fraclm
        counter = self.counter

        arr_av1 = divide_protected(self.arr_sum1, self.arr_sum0)
        arr_av2 = divide_protected(self.arr_sum2, self.arr_sum0)

        frac_int_lo = np.array(self.sta_int_lo/counter, dtype=np.float32)
        frac_int_hi = np.array(self.sta_int_hi/counter, dtype=np.float32)

        arr_rms = np.sqrt(arr_av2 - np.square(arr_av1))

        rms_min, rms_max = evaluate_limits(arr_rms, self.rmsnlo, self.rmsnhi, self.rms_lo, self.rms_hi, cmt='RMS')
        ave_min, ave_max = evaluate_limits(arr_av1, self.intnlo, self.intnhi, self.int_lo, self.int_hi, cmt='AVE')

        arr_sta_rms_hi = np.select((arr_rms>rms_max,),    (self.arr1,), 0)
        arr_sta_rms_lo = np.select((arr_rms<rms_min,),    (self.arr1,), 0)
        arr_sta_int_hi = np.select((frac_int_hi>fraclm,), (self.arr1,), 0)
        arr_sta_int_lo = np.select((frac_int_lo>fraclm,), (self.arr1,), 0)
        arr_sta_ave_hi = np.select((arr_av1>ave_max,),    (self.arr1,), 0)
        arr_sta_ave_lo = np.select((arr_av1<ave_min,),    (self.arr1,), 0)

        logger.info('  Bad pixel status:')
        logger.info('  status  1: %8d pixel rms       > %.3f' % (arr_sta_rms_hi.sum(), rms_max))
        logger.info('  status  8: %8d pixel rms       < %.3f' % (arr_sta_rms_lo.sum(), rms_min))
        logger.info('  status  2: %8d pixel intensity > %g in more than %g fraction of events' % (arr_sta_int_hi.sum(), int_hi, fraclm))
        logger.info('  status  4: %8d pixel intensity < %g in more than %g fraction of events' % (arr_sta_int_lo.sum(), int_lo, fraclm))
        logger.info('  status 16: %8d pixel average   > %g'   % (arr_sta_ave_hi.sum(), ave_max))
        logger.info('  status 32: %8d pixel average   < %g'   % (arr_sta_ave_lo.sum(), ave_min))

        #0/1/2/4/8/16/32 for good/hot-rms/saturated/cold/cold-rms/average above limit/average below limit,
        arr_sta = np.zeros(self.shape, dtype=np.int64)
        arr_sta += arr_sta_rms_hi    # hot rms
        arr_sta += arr_sta_rms_lo*8  # cold rms
        arr_sta += arr_sta_int_hi*2  # satturated
        arr_sta += arr_sta_int_lo*4  # cold
        arr_sta += arr_sta_ave_hi*16 # too large average
        arr_sta += arr_sta_ave_lo*32 # too small average

        arr_msk  = np.select((arr_sta>0,), (self.arr0,), 1)

        cmod = None # self._common_mode_pars(arr_av1, arr_rms, arr_msk)

        if plotim &  1: plot_det_image(det, evt, arr_av1,         tit='average')
        if plotim &  2: plot_det_image(det, evt, arr_rms,         tit='RMS')
        if plotim &  4: plot_det_image(det, evt, arr_sta,         tit='status')
        if plotim &  8: plot_det_image(det, evt, arr_msk,         tit='mask')
        if plotim & 16: plot_det_image(det, evt, self.arr_max,    tit='maximum')
        if plotim & 32: plot_det_image(det, evt, self.arr_min,    tit='minimum')
        if plotim & 64: plot_det_image(det, evt, self.sta_int_lo, tit='statistics below threshold')
        if plotim &128: plot_det_image(det, evt, self.sta_int_hi, tit='statistics above threshold')

        cmts = ['DATASET  %s' % self.detname, 'STATISTICS  %d' % counter]

        # Save n-d array in text file %
        template = fname_template(self.orun, self.detname, ofname, counter, self.ev1_sec, self.ev1_nsec)
        addmetad=True

        verbos = self.verbos

        if savebw &  1: save_txt(template % 'ave', arr_av1,      cmts + ['ARR_TYPE  average'], '%8.2f', verbos, addmetad)
        if savebw &  2: save_txt(template % 'rms', arr_rms,      cmts + ['ARR_TYPE  RMS'],     '%8.2f', verbos, addmetad)
        if savebw &  4: save_txt(template % 'sta', arr_sta,      cmts + ['ARR_TYPE  status'],  '%d',    verbos, addmetad)
        if savebw &  8: save_txt(template % 'msk', arr_msk,      cmts + ['ARR_TYPE  mask'],    '%1d',   verbos, addmetad)
        if savebw & 16: save_txt(template % 'max', self.arr_max, cmts + ['ARR_TYPE  max'],     '%d',    verbos, addmetad)
        if savebw & 32: save_txt(template % 'min', self.arr_min, cmts + ['ARR_TYPE  min'],     '%d',    verbos, addmetad)
        if savebw & 64 and cmod is not None:
            np.savetxt(template % 'cmo', cmod, fmt='%d', delimiter=' ', newline=' ')
            save_txt(template % 'cmm', cmod, cmts + ['ARR_TYPE  common_mode'],'%d', verbos, False)

        args    = vars(self.args)
        version = args.get('version', str_tstamp('V%Y%m%dT%H%M%S', t0_sec))
        orun = self.orun
        loglev = args.get('loglev', 'INFO')

        # args.expnam # args.runnum, orun.timestamp
        kwa = {'detname'   : self.detname,
               'experiment': orun.expt,
               'runnum'    : orun.runnum,
               'timestamp' : str_tstamp(time_sec=self.ev1_sec),
               'version'   : version,
               'comment'   : str(cmts),
               'loglev'    : loglev,
              }

        logger.debug('kwa:', kwa)
        logger.debug('args:', self.args)

        if self.addcdb:
            from psana.pscalib.calib.MDBCommands import add_constants

            if savebw &  1: add_constants(template % 'ave', 'pedestals',    **kwa)
            if savebw &  2: add_constants(template % 'rms', 'pixel_rms',    **kwa)
            if savebw &  4: add_constants(template % 'sta', 'pixel_status', **kwa)
            if savebw &  8: add_constants(template % 'msk', 'pixel_mask',   **kwa)
            if savebw & 16: add_constants(template % 'max', 'pixel_max',    **kwa)
            if savebw & 32: add_constants(template % 'min', 'pixel_min',    **kwa)

        logger.info('Data summary for %s is completed, dt=%7.3f sec' % (self.detname, time()-t0_sec))

#----------

def detectors_dark_proc(parser):

    args = parser.parse_args()
    #expnam, runnum, dnames, ifname, ofname, events, evskip, intlow, inthig, rmslow, rmshig, fraclm, nsigma,\
    #                    plotim, savebw, intnlo, intnhi, rmsnlo, rmsnhi, evcode

    EVSKIP = args.evskip
    EVENTS = args.events + EVSKIP

    logger.info('Raw data processing of exp: %s run: %d for detector(s): %s' % (args.expnam, args.runnum, args.dnames))
    logger.info('input file: %s' % (args.ifname))

    ds = DataSource(files=args.ifname)
    run = next(ds.runs())
    logger.info('\t RunInfo expt: %s runnum: %d\n' % (run.expt, run.runnum))

    print('dir(run):',dir(run))
    print(':', run.timestamp)
    print(':', run._get_runinfo())
    print(':', run.xtcinfo)
    print(':', run.id)
    #sys.exit('TEST EXIT')
    ###==================

    lst_dpo = [DetRawDarkProc(run, dname, args) for dname in args.dnames.split(',')]

    #ecm = EventCodeManager(evcode, verbos)

    t0_sec = time()
    tdt = t0_sec

    for i,evt in enumerate(run.events()):
        if i<EVSKIP: continue
        if not i<EVENTS: break
        #if not ecm.select(evt): continue

        for dpo in lst_dpo: dpo.event(evt)
        if do_print(i):
            tsec = time()
            dt   = tsec - tdt
            tdt  = tsec
            logger.info('  Event: %4d,  time=%7.3f sec,  dt=%5.3f sec' % (i, time()-t0_sec, dt))

    for dpo in lst_dpo: dpo.summary(evt)

    logger.info('%s\n Processed %d events, consumed time = %f sec.' % (80*'_', i, time()-t0_sec))

#----------

if __name__ == "__main__":
    sys.exit('run it by command det_raw_dark_proc')

#----------


text:
"""
Root system data for super type A
"""
# ****************************************************************************
#       Copyright (C) 2017 Travis Scrimshaw <tcscrims at gmail.com>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 2 of the License, or
# (at your option) any later version.
#                  https://www.gnu.org/licenses/
# ****************************************************************************

from sage.rings.integer_ring import ZZ
from sage.misc.cachefunc import cached_method
from . import ambient_space
from .cartan_type import SuperCartanType_standard

class AmbientSpace(ambient_space.AmbientSpace):
    r"""
    The ambient space for (super) type `A(m|n)`.

    EXAMPLES::

        sage: R = RootSystem(['A', [2,1]])
        sage: AL = R.ambient_space(); AL
        Ambient space of the Root system of type ['A', [2, 1]]
        sage: AL.basis()
        Finite family {-3: (1, 0, 0, 0, 0),
         -2: (0, 1, 0, 0, 0),
         -1: (0, 0, 1, 0, 0),
         1: (0, 0, 0, 1, 0),
         2: (0, 0, 0, 0, 1)}
    """
    def __init__(self, root_system, base_ring, index_set=None):
        """
        Initialize ``self``.

        TESTS::

            sage: R = RootSystem(['A', [4,2]])
            sage: AL = R.ambient_space(); AL
            Ambient space of the Root system of type ['A', [4, 2]]
            sage: TestSuite(AL).run(skip="_test_norm_of_simple_roots")
        """
        ct = root_system.cartan_type()
        if index_set is None:
            index_set = tuple(list(range(-ct.m - 1, 0)) +
                              list(range(1, ct.n + 2)))
        ambient_space.AmbientSpace.__init__(self, root_system, base_ring,
                                            index_set=index_set)

    @classmethod
    def smallest_base_ring(cls, cartan_type=None):
        """
        Return the smallest base ring the ambient space can be defined upon.

        .. SEEALSO::

            :meth:`~sage.combinat.root_system.ambient_space.AmbientSpace.smallest_base_ring`

        EXAMPLES::

            sage: e = RootSystem(['A', [3,1]]).ambient_space()
            sage: e.smallest_base_ring()
            Integer Ring
        """
        return ZZ

    def dimension(self):
        """
        Return the dimension of this ambient space.

        EXAMPLES::

            sage: e = RootSystem(['A', [4,2]]).ambient_space()
            sage: e.dimension()
            8
        """
        ct = self.root_system.cartan_type()
        return ct.m + ct.n + 2

    def simple_root(self, i):
        """
        Return the `i`-th simple root of ``self``.

        EXAMPLES::

            sage: e = RootSystem(['A', [2,1]]).ambient_lattice()
            sage: list(e.simple_roots())
            [(1, -1, 0, 0, 0), (0, 1, -1, 0, 0),
             (0, 0, 1, -1, 0), (0, 0, 0, 1, -1)]
        """
        if i < 0:
            return self.monomial(i-1) - self.monomial(i)
        if i == 0:
            return self.monomial(-1) - self.monomial(1)
        return self.monomial(i) - self.monomial(i+1)

    def positive_roots(self):
        """
        Return the positive roots of ``self``.

        EXAMPLES::

            sage: e = RootSystem(['A', [2,1]]).ambient_lattice()
            sage: e.positive_roots()
            [(0, 1, -1, 0, 0),
             (1, 0, -1, 0, 0),
             (1, -1, 0, 0, 0),
             (0, 0, 0, 1, -1),
             (0, 0, 1, -1, 0),
             (0, 0, 1, 0, -1),
             (0, 1, 0, -1, 0),
             (0, 1, 0, 0, -1),
             (1, 0, 0, -1, 0),
             (1, 0, 0, 0, -1)]
        """
        return self.positive_even_roots() + self.positive_odd_roots()

    def positive_even_roots(self):
        """
        Return the positive even roots of ``self``.

        EXAMPLES::

            sage: e = RootSystem(['A', [2,1]]).ambient_lattice()
            sage: e.positive_even_roots()
            [(0, 1, -1, 0, 0), (1, 0, -1, 0, 0),
             (1, -1, 0, 0, 0), (0, 0, 0, 1, -1)]
        """
        ct = self.root_system.cartan_type()
        ret = []
        ret += [self.monomial(-j) - self.monomial(-i)
                for i in range(1, ct.m + 2)
                for j in range(i + 1, ct.m + 2)]
        ret += [self.monomial(i) - self.monomial(j)
                for i in range(1, ct.n + 2)
                for j in range(i + 1, ct.n + 2)]
        return ret

    def positive_odd_roots(self):
        """
        Return the positive odd roots of ``self``.

        EXAMPLES::

            sage: e = RootSystem(['A', [2,1]]).ambient_lattice()
            sage: e.positive_odd_roots()
            [(0, 0, 1, -1, 0),
             (0, 0, 1, 0, -1),
             (0, 1, 0, -1, 0),
             (0, 1, 0, 0, -1),
             (1, 0, 0, -1, 0),
             (1, 0, 0, 0, -1)]
        """
        ct = self.root_system.cartan_type()
        return [self.monomial(-i) - self.monomial(j)
                for i in range(1, ct.m + 2)
                for j in range(1, ct.n + 2)]

    def highest_root(self):
        """
        Return the highest root of ``self``.

        EXAMPLES::

           sage: e = RootSystem(['A', [4,2]]).ambient_lattice()
           sage: e.highest_root()
           (1, 0, 0, 0, 0, 0, 0, -1)
        """
        ct = self.root_system.cartan_type()
        return self.monomial(-ct.m-1) - self.monomial(ct.n+1)

    def negative_roots(self):
        """
        Return the negative roots of ``self``.

        EXAMPLES::

            sage: e = RootSystem(['A', [2,1]]).ambient_lattice()
            sage: e.negative_roots()
            [(0, -1, 1, 0, 0),
             (-1, 0, 1, 0, 0),
             (-1, 1, 0, 0, 0),
             (0, 0, 0, -1, 1),
             (0, 0, -1, 1, 0),
             (0, 0, -1, 0, 1),
             (0, -1, 0, 1, 0),
             (0, -1, 0, 0, 1),
             (-1, 0, 0, 1, 0),
             (-1, 0, 0, 0, 1)]
        """
        return self.negative_even_roots() + self.negative_odd_roots()

    def negative_even_roots(self):
        """
        Return the negative even roots of ``self``.

        EXAMPLES::

            sage: e = RootSystem(['A', [2,1]]).ambient_lattice()
            sage: e.negative_even_roots()
            [(0, -1, 1, 0, 0), (-1, 0, 1, 0, 0),
             (-1, 1, 0, 0, 0), (0, 0, 0, -1, 1)]
        """
        ct = self.root_system.cartan_type()
        ret = []
        ret += [self.monomial(-i) - self.monomial(-j)
                for i in range(1, ct.m + 2)
                for j in range(i + 1, ct.m + 2)]
        ret += [self.monomial(j) - self.monomial(i)
                for i in range(1, ct.n + 2)
                for j in range(i + 1, ct.n + 2)]
        return ret

    def negative_odd_roots(self):
        """
        Return the negative odd roots of ``self``.

        EXAMPLES::

            sage: e = RootSystem(['A', [2,1]]).ambient_lattice()
            sage: e.negative_odd_roots()
            [(0, 0, -1, 1, 0),
             (0, 0, -1, 0, 1),
             (0, -1, 0, 1, 0),
             (0, -1, 0, 0, 1),
             (-1, 0, 0, 1, 0),
             (-1, 0, 0, 0, 1)]
        """
        ct = self.root_system.cartan_type()
        return [self.monomial(j) - self.monomial(-i)
                for i in range(1, ct.m + 2)
                for j in range(1, ct.n + 2)]

    def fundamental_weight(self, i):
        r"""
        Return the fundamental weight `\Lambda_i` of ``self``.

        EXAMPLES::

            sage: L = RootSystem(['A', [3,2]]).ambient_space()
            sage: L.fundamental_weight(-1)
            (1, 1, 1, 0, 0, 0, 0)
            sage: L.fundamental_weight(0)
            (1, 1, 1, 1, 0, 0, 0)
            sage: L.fundamental_weight(2)
            (1, 1, 1, 1, -1, -1, -2)
            sage: list(L.fundamental_weights())
            [(1, 0, 0, 0, 0, 0, 0),
             (1, 1, 0, 0, 0, 0, 0),
             (1, 1, 1, 0, 0, 0, 0),
             (1, 1, 1, 1, 0, 0, 0),
             (1, 1, 1, 1, -1, -2, -2),
             (1, 1, 1, 1, -1, -1, -2)]

        ::

            sage: L = RootSystem(['A', [2,3]]).ambient_space()
            sage: La = L.fundamental_weights()
            sage: al = L.simple_roots()
            sage: I = L.index_set()
            sage: matrix([[al[i].scalar(La[j]) for i in I] for j in I])
            [ 1  0  0  0  0  0]
            [ 0  1  0  0  0  0]
            [ 0  0  1  0  0  0]
            [ 0  0  0 -1  0  0]
            [ 0  0  0  0 -1  0]
            [ 0  0  0  0  0 -1]
        """
        m = self.root_system.cartan_type().m
        n = self.root_system.cartan_type().n
        if i <= 0:
            return self.sum(self.monomial(j) for j in range(-m-1,i))
        return (self.sum(self.monomial(j) for j in range(-m-1,1))
                - self.sum(self.monomial(j) for j in range(i+1))
                - 2*self.sum(self.monomial(j) for j in range(i+1,n+2)))

    def simple_coroot(self, i):
        """
        Return the simple coroot `h_i` of ``self``.

        EXAMPLES::

            sage: L = RootSystem(['A', [3,2]]).ambient_space()
            sage: L.simple_coroot(-2)
            (0, 1, -1, 0, 0, 0, 0)
            sage: L.simple_coroot(0)
            (0, 0, 0, 1, -1, 0, 0)
            sage: L.simple_coroot(2)
            (0, 0, 0, 0, 0, -1, 1)
            sage: list(L.simple_coroots())
            [(1, -1, 0, 0, 0, 0, 0),
             (0, 1, -1, 0, 0, 0, 0),
             (0, 0, 1, -1, 0, 0, 0),
             (0, 0, 0, 1, -1, 0, 0),
             (0, 0, 0, 0, -1, 1, 0),
             (0, 0, 0, 0, 0, -1, 1)]
        """
        if i <= 0:
            return self.simple_root(i)
        return -self.simple_root(i)

    class Element(ambient_space.AmbientSpaceElement):
        def inner_product(self, lambdacheck):
            """
            The scalar product with elements of the coroot lattice
            embedded in the ambient space.

            EXAMPLES::

                sage: L = RootSystem(['A', [2,1]]).ambient_space()
                sage: a = L.simple_roots()
                sage: matrix([[a[i].inner_product(a[j]) for j in L.index_set()] for i in L.index_set()])
                [ 2 -1  0  0]
                [-1  2 -1  0]
                [ 0 -1  0  1]
                [ 0  0  1 -2]
            """
            self_mc = self._monomial_coefficients
            lambdacheck_mc = lambdacheck._monomial_coefficients

            result = self.parent().base_ring().zero()
            for t,c in lambdacheck_mc.items():
                if t not in self_mc:
                    continue
                if t > 0:
                    result -= c*self_mc[t]
                else:
                    result += c*self_mc[t]
            return result

        scalar = inner_product
        dot_product = inner_product

        def associated_coroot(self):
            """
            Return the coroot associated to ``self``.

            EXAMPLES::

                sage: L = RootSystem(['A', [3,2]]).ambient_space()
                sage: al = L.simple_roots()
                sage: al[-1].associated_coroot()
                (0, 0, 1, -1, 0, 0, 0)
                sage: al[0].associated_coroot()
                (0, 0, 0, 1, -1, 0, 0)
                sage: al[1].associated_coroot()
                (0, 0, 0, 0, -1, 1, 0)

                sage: a = al[-1] + al[0] + al[1]; a
                (0, 0, 1, 0, 0, -1, 0)
                sage: a.associated_coroot()
                (0, 0, 1, 0, -2, 1, 0)
                sage: h = L.simple_coroots()
                sage: h[-1] + h[0] + h[1]
                (0, 0, 1, 0, -2, 1, 0)

                sage: (al[-1] + al[0] + al[2]).associated_coroot()
                (0, 0, 1, 0, -1, -1, 1)
            """
            P = self.parent()
            al = P.simple_roots()
            h = P.simple_coroots()
            try:
                return h[al.inverse_family()[self]]
            except KeyError:
                pass
            V = P._dense_free_module()
            dep = V.linear_dependence([self._vector_()] +
                                      [al[i]._vector_() for i in P.index_set()])[0]
            I = P.index_set()
            return P.sum((-c/dep[0]) * h[I[i]] for i,c in dep[1:].items())

        def has_descent(self, i, positive=False):
            """
            Test if ``self`` has a descent at position `i`, that is
            if ``self`` is on the strict negative side of the `i^{th}`
            simple reflection hyperplane.

            If ``positive`` is ``True``, tests if it is on the strict
            positive side instead.

            EXAMPLES::

                sage: L = RootSystem(['A', [2,1]]).ambient_space()
                sage: al = L.simple_roots()
                sage: [al[i].has_descent(1) for i in L.index_set()]
                [False, False, True, False]
                sage: [(-al[i]).has_descent(1) for i in L.index_set()]
                [False, False, False, True]
                sage: [al[i].has_descent(1, True) for i in L.index_set()]
                [False, False, False, True]
                sage: [(-al[i]).has_descent(1, True) for i in L.index_set()]
                [False, False, True, False]
                sage: (al[-2] + al[0] + al[1]).has_descent(-1)
                True
                sage: (al[-2] + al[0] + al[1]).has_descent(1)
                False
                sage: (al[-2] + al[0] + al[1]).has_descent(1, positive=True)
                True
                sage: all(all(not la.has_descent(i) for i in L.index_set())
                ....:     for la in L.fundamental_weights())
                True
            """
            s = self.scalar(self.parent().simple_roots()[i])
            if i > 0:
                s = -s
            if positive:
                return s > 0
            else:
                return s < 0

        def is_dominant_weight(self):
            """
            Test whether ``self`` is a dominant element of the weight lattice.

            EXAMPLES::

                sage: L = RootSystem(['A',2]).ambient_lattice()
                sage: Lambda = L.fundamental_weights()
                sage: [x.is_dominant() for x in Lambda]
                [True, True]
                sage: (3*Lambda[1]+Lambda[2]).is_dominant()
                True
                sage: (Lambda[1]-Lambda[2]).is_dominant()
                False
                sage: (-Lambda[1]+Lambda[2]).is_dominant()
                False

            Tests that the scalar products with the coroots are all
            nonnegative integers. For example, if `x` is the sum of a
            dominant element of the weight lattice plus some other element
            orthogonal to all coroots, then the implementation correctly
            reports `x` to be a dominant weight::

               sage: x = Lambda[1] + L([-1,-1,-1])
               sage: x.is_dominant_weight()
               True
            """
            alpha = self.parent().simple_roots()
            l = self.parent().cartan_type().symmetrizer()
            from sage.rings.semirings.all import NN
            return all(l[i] * self.inner_product(alpha[i]) in NN
                       for i in self.parent().index_set())

class CartanType(SuperCartanType_standard):
    """
    Cartan Type `A(m|n)`.

    .. SEEALSO:: :func:`~sage.combinat.root_systems.cartan_type.CartanType`
    """
    def __init__(self, m, n):
        """
        EXAMPLES::

            sage: ct = CartanType(['A', [4,2]])
            sage: ct
            ['A', [4, 2]]
            sage: ct._repr_(compact=True)
            'A4|2'

            sage: ct.is_irreducible()
            True
            sage: ct.is_finite()
            True
            sage: ct.is_affine()
            False
            sage: ct.affine() # Not tested -- to be implemented
            ['A', [4, 2], 1]
            sage: ct.dual()
            ['A', [4, 2]]

        TESTS::

            sage: TestSuite(ct).run()
        """
        self.m = m
        self.n = n
        self.letter = "A"

    def _latex_(self):
        r"""
        Return a latex representation of ``self``.

        EXAMPLES::

            sage: latex(CartanType(['A',[4,3]]))
            A(4|3)
        """
        return "A(%s|%s)" % (self.m, self.n)

    def index_set(self):
        """
        Return the index set of ``self``.

        EXAMPLES::

            sage: CartanType(['A', [2,3]]).index_set()
            (-2, -1, 0, 1, 2, 3)
        """
        return tuple(range(-self.m, self.n + 1))

    AmbientSpace = AmbientSpace

    def is_irreducible(self):
        """
        Return whether ``self`` is irreducible, which is ``True``.

        EXAMPLES::

            sage: CartanType(['A', [3,4]]).is_irreducible()
            True
        """
        return True

    # A lot of these methods should be implemented by the ABCs of CartanType

    def is_affine(self):
        """
        Return whether ``self`` is affine or not.

        EXAMPLES::

            sage: CartanType(['A', [2,3]]).is_affine()
            False
        """
        return False

    def is_finite(self):
        """
        Return whether ``self`` is finite or not.

        EXAMPLES::

            sage: CartanType(['A', [2,3]]).is_finite()
            True
        """
        return True

    def dual(self):
        """
        Return dual of ``self``.

        EXAMPLES::

            sage: CartanType(['A', [2,3]]).dual()
            ['A', [2, 3]]
        """
        return self

    def type(self):
        """
        Return type of ``self``.

        EXAMPLES::

            sage: CartanType(['A', [2,3]]).type()
            'A'
        """
        return 'A'

    def root_system(self):
        """
        Return root system of ``self``.

        EXAMPLES::

            sage: CartanType(['A', [2,3]]).root_system()
            Root system of type ['A', [2, 3]]
        """
        from sage.combinat.root_system.root_system import RootSystem
        return RootSystem(self)

    @cached_method
    def symmetrizer(self):
        """
        Return symmetrizing matrix for ``self``.

        EXAMPLES::

            sage: CartanType(['A', [2,3]]).symmetrizer()
            Finite family {-2: 1, -1: 1, 0: 1, 1: -1, 2: -1, 3: -1}
        """
        from sage.sets.family import Family

        def ell(i):
            return ZZ.one() if i <= 0 else -ZZ.one()
        return Family(self.index_set(), ell)

    def dynkin_diagram(self):
        """
        Return the Dynkin diagram of super type A.

        EXAMPLES::

            sage: a = CartanType(['A', [4,2]]).dynkin_diagram()
            sage: a
            O---O---O---O---X---O---O
            -4  -3  -2  -1  0   1   2
            A4|2
            sage: sorted(a.edges())
            [(-4, -3, 1), (-3, -4, 1), (-3, -2, 1), (-2, -3, 1),
             (-2, -1, 1), (-1, -2, 1), (-1, 0, 1), (0, -1, 1),
             (0, 1, 1), (1, 0, -1), (1, 2, 1), (2, 1, 1)]

        TESTS::

            sage: a = DynkinDiagram(['A', [0,0]]); a
            X
            0
            A0|0
            sage: a.vertices(), a.edges()
            ([0], [])

            sage: a = DynkinDiagram(['A', [1,0]]); a
            O---X
            -1  0
            A1|0
            sage: a.vertices(), a.edges()
            ([-1, 0], [(-1, 0, 1), (0, -1, 1)])

            sage: a = DynkinDiagram(['A', [0,1]]); a
            X---O
            0   1
            A0|1
            sage: a.vertices(), a.edges()
            ([0, 1], [(0, 1, 1), (1, 0, -1)])
        """
        from .dynkin_diagram import DynkinDiagram_class
        g = DynkinDiagram_class(self, odd_isotropic_roots=[0])
        for i in range(self.m):
            g.add_edge(-i-1, -i)
        for i in range(1, self.n):
            g.add_edge(i, i+1)
        g.add_vertex(0)  # Usually there, but not when m == n == 0
        if self.m > 0:
            g.add_edge(-1, 0)
        if self.n > 0:
            g.add_edge(1, 0, -1)
        return g

    def cartan_matrix(self):
        """
        Return the Cartan matrix associated to ``self``.

        EXAMPLES::

            sage: ct = CartanType(['A', [2,3]])
            sage: ct.cartan_matrix()
            [ 2 -1  0  0  0  0]
            [-1  2 -1  0  0  0]
            [ 0 -1  0  1  0  0]
            [ 0  0 -1  2 -1  0]
            [ 0  0  0 -1  2 -1]
            [ 0  0  0  0 -1  2]

        TESTS::

            sage: ct = CartanType(['A', [0,0]])
            sage: ct.cartan_matrix()
            [0]

            sage: ct = CartanType(['A', [1,0]])
            sage: ct.cartan_matrix()
            [ 2 -1]
            [-1  0]

            sage: ct = CartanType(['A', [0,1]])
            sage: ct.cartan_matrix()
            [ 0  1]
            [-1  2]
        """
        return self.dynkin_diagram().cartan_matrix()

    def relabel(self, relabelling):
        """
        Return a relabelled copy of this Cartan type.

        INPUT:

        - ``relabelling`` -- a function (or a list or dictionary)

        OUTPUT:

        an isomorphic Cartan type obtained by relabelling the nodes of
        the Dynkin diagram. Namely, the node with label ``i`` is
        relabelled ``f(i)`` (or, by ``f[i]`` if ``f`` is a list or
        dictionary).

        EXAMPLES::

            sage: ct = CartanType(['A', [1,2]])
            sage: ct.dynkin_diagram()
            O---X---O---O
            -1  0   1   2
            A1|2
            sage: f={1:2,2:1,0:0,-1:-1}
            sage: ct.relabel(f)
            ['A', [1, 2]] relabelled by {-1: -1, 0: 0, 1: 2, 2: 1}
            sage: ct.relabel(f).dynkin_diagram()
            O---X---O---O
            -1  0   2   1
            A1|2 relabelled by {-1: -1, 0: 0, 1: 2, 2: 1}
        """
        from . import type_relabel
        return type_relabel.CartanType(self, relabelling)

    def _latex_draw_node(self, x, y, label, position="below=4pt"):
        r"""
        Draw (possibly marked [crossed out]) circular node ``i`` at the
        position ``(x,y)`` with node label ``label`` .

        - ``position`` -- position of the label relative to the node
        - ``anchor`` -- (optional) the anchor point for the label

        EXAMPLES::

            sage: t = CartanType(['A', [3,2]])
            sage: print(t._latex_draw_node(0, 0, 0))
            \draw[fill=white] (0 cm, 0 cm) circle (.25cm) node[below=4pt]{$0$};
            \draw[-,thick] (0.17 cm, 0.17 cm) -- (-0.17 cm, -0.17 cm);
            \draw[-,thick] (0.17 cm, -0.17 cm) -- (-0.17 cm, 0.17 cm);
            sage: print(t._latex_draw_node(0, 0, 1))
            \draw[fill=white] (0 cm, 0 cm) circle (.25cm) node[below=4pt]{$1$};
        """
        ret = "\\draw[fill={}] ({} cm, {} cm) circle (.25cm) node[{}]{{${}$}};\n".format(
              'white', x, y, position, label)
        if label == 0:
            ret += "\\draw[-,thick] ({} cm, {} cm) -- ({} cm, {} cm);\n".format(
                                    x+.17, y+.17, x-.17, y-.17)
            ret += "\\draw[-,thick] ({} cm, {} cm) -- ({} cm, {} cm);\n".format(
                                    x+.17, y-.17, x-.17, y+.17)
        return ret

    def _latex_dynkin_diagram(self, label=lambda i: i, node=None, node_dist=2):
        r"""
        Return a latex representation of the Dynkin diagram.

        EXAMPLES::

            sage: print(CartanType(['A', [3,2]])._latex_dynkin_diagram())
            \draw (0 cm, 0 cm) -- (10 cm, 0 cm);
            \draw[fill=white] (0 cm, 0 cm) circle (.25cm) node[below=4pt]{$-3$};
            \draw[fill=white] (2 cm, 0 cm) circle (.25cm) node[below=4pt]{$-2$};
            \draw[fill=white] (4 cm, 0 cm) circle (.25cm) node[below=4pt]{$-1$};
            \draw[fill=white] (6 cm, 0 cm) circle (.25cm) node[below=4pt]{$0$};
            \draw[-,thick] (6.17 cm, 0.17 cm) -- (5.83 cm, -0.17 cm);
            \draw[-,thick] (6.17 cm, -0.17 cm) -- (5.83 cm, 0.17 cm);
            \draw[fill=white] (8 cm, 0 cm) circle (.25cm) node[below=4pt]{$1$};
            \draw[fill=white] (10 cm, 0 cm) circle (.25cm) node[below=4pt]{$2$};

            sage: print(CartanType(['A', [0,2]])._latex_dynkin_diagram())
            \draw (0 cm, 0 cm) -- (4 cm, 0 cm);
            \draw[fill=white] (0 cm, 0 cm) circle (.25cm) node[below=4pt]{$0$};
            \draw[-,thick] (0.17 cm, 0.17 cm) -- (-0.17 cm, -0.17 cm);
            \draw[-,thick] (0.17 cm, -0.17 cm) -- (-0.17 cm, 0.17 cm);
            \draw[fill=white] (2 cm, 0 cm) circle (.25cm) node[below=4pt]{$1$};
            \draw[fill=white] (4 cm, 0 cm) circle (.25cm) node[below=4pt]{$2$};

            sage: print(CartanType(['A', [2,0]])._latex_dynkin_diagram())
            \draw (0 cm, 0 cm) -- (4 cm, 0 cm);
            \draw[fill=white] (0 cm, 0 cm) circle (.25cm) node[below=4pt]{$-2$};
            \draw[fill=white] (2 cm, 0 cm) circle (.25cm) node[below=4pt]{$-1$};
            \draw[fill=white] (4 cm, 0 cm) circle (.25cm) node[below=4pt]{$0$};
            \draw[-,thick] (4.17 cm, 0.17 cm) -- (3.83 cm, -0.17 cm);
            \draw[-,thick] (4.17 cm, -0.17 cm) -- (3.83 cm, 0.17 cm);

            sage: print(CartanType(['A', [0,0]])._latex_dynkin_diagram())
            \draw[fill=white] (0 cm, 0 cm) circle (.25cm) node[below=4pt]{$0$};
            \draw[-,thick] (0.17 cm, 0.17 cm) -- (-0.17 cm, -0.17 cm);
            \draw[-,thick] (0.17 cm, -0.17 cm) -- (-0.17 cm, 0.17 cm);
        """
        if node is None:
            node = self._latex_draw_node
        if self.n + self.m > 1:
            ret = "\\draw (0 cm, 0 cm) -- ({} cm, 0 cm);\n".format((self.n+self.m)*node_dist)
        else:
            ret = ""
        return ret + "".join(node((self.m+i)*node_dist, 0, label(i))
                             for i in self.index_set())

    def ascii_art(self, label=lambda i: i, node=None):
        """
        Return an ascii art representation of the Dynkin diagram.

        EXAMPLES::

            sage: t = CartanType(['A', [3,2]])
            sage: print(t.ascii_art())
            O---O---O---X---O---O
            -3  -2  -1  0   1   2
            sage: t = CartanType(['A', [3,7]])
            sage: print(t.ascii_art())
            O---O---O---X---O---O---O---O---O---O---O
            -3  -2  -1  0   1   2   3   4   5   6   7

            sage: t = CartanType(['A', [0,7]])
            sage: print(t.ascii_art())
            X---O---O---O---O---O---O---O
            0   1   2   3   4   5   6   7
            sage: t = CartanType(['A', [0,0]])
            sage: print(t.ascii_art())
            X
            0
            sage: t = CartanType(['A', [5,0]])
            sage: print(t.ascii_art())
            O---O---O---O---O---X
            -5  -4  -3  -2  -1  0
        """
        if node is None:
            node = lambda i: 'O'
        ret  = "---".join(node(label(i)) for i in range(1,self.m+1))
        if self.m == 0:
            if self.n == 0:
                ret = "X"
            else:
                ret += "X---"
        else:
            if self.n == 0:
                ret += "---X"
            else:
                ret += "---X---"
        ret += "---".join(node(label(i)) for i in range(1,self.n+1)) + "\n"
        ret += "".join("{!s:4}".format(label(-i)) for i in reversed(range(1,self.m+1)))
        ret += "{!s:4}".format(label(0))
        ret += "".join("{!s:4}".format(label(i)) for i in range(1,self.n+1))
        return ret

max_stars_repo_name: LaisRast/sage

max_issues_repo_name: LaisRast/sage

max_forks_repo_name: LaisRast/sage

reason: from sage


text:
import cv2
import numpy
Imagecap = cv2.VideoCapture(0)

while True:
    Output = numpy.zeros((480, 640), dtype=numpy.uint8)
    _,Image = Imagecap.read()
    R = Image[:,:,2]
    G = Image[:,:,1]
    B = Image[:,:,0]

    print(R.shape)
    for Rows in range(0,480):
        for Col in range(0,640):
            if R[Rows,Col]>G[Rows,Col]>B[Rows,Col]:
                  Output[Rows,Col] = 255

    cv2.imshow("Original", Image)
    cv2.imshow("Output", Output)
    if cv2.waitKey(1) == 27:
        break

max_stars_repo_name: FEDCoVK/Capstone-Project

max_issues_repo_name: FEDCoVK/Capstone-Project

max_forks_repo_name: FEDCoVK/Capstone-Project