text:
import json
import os
import numpy as np
from tensorflow.keras.models import load_model
from tensorflow.keras.utils import to_categorical
import music21 as m21

SEQUENCE_LENGTH = 64
MAPPING_PATHS = ["mapping.json", "mapping_europa.json", "mapping_europa.json", "mapping_china.json"]
MODELS = ["lstm-folk-1.h5", "lstm-folk-2.h5", "lstm-folk-3.h5", "lstm-china-1.h5"]

class MelodyGenerator:
    """
    Class to generate melodies based on h5 model

    ...

    Attributes:
    ----------
    model_name : str
        string - name of the model
    model : h5 model
        h5 model to generate a melody

    Methods:
    -------
    generate_melody(seed, num_steps, max_sequence_length, temperature)
    generates melody based on the seed usind the given model
    """

    def __init__(self, model_number):

        DIRNAME = os.path.dirname(__file__)

        self.model_path = os.path.join(DIRNAME, MODELS[model_number])

        self.model = load_model(self.model_path)

        mapping_path = os.path.join(DIRNAME, MAPPING_PATHS[model_number])

        with open(mapping_path, "r") as fp:
            self._mappings = json.load(fp)

        self._start_symbols = ["/"] * SEQUENCE_LENGTH

    def generate_melody(self, seed, num_steps, max_sequence_length, temperature):

        # create seed with start symbols
        seed = seed.split()
        melody = seed
        seed = self._start_symbols + seed

        # Map seed to integers
        seed = [self._mappings[symbol] for symbol in seed]

        for _ in range(num_steps):
            # Limit the seed to max_sequence_length
            seed = seed[-max_sequence_length:]

            # One-hot encode the seed
            onehot_seed = to_categorical(seed, num_classes=len(self._mappings))
            # (New dim, max_sequence_length, number of symbols in the vocab)
            # Add third dimension -> for keras expect multiple samples
            onehot_seed = onehot_seed[np.newaxis, ...]

            # Make a prediction
            probabilities = self.model.predict(onehot_seed)[0]

            # [0.1, 0.2, 0.1, 0.6] add to 1
            output_int = self._sample_with_temperature(probabilities, temperature)

            # updaate the seed
            seed.append(output_int)

            # Map int to our encoding
            output_symbol = [k for k, v in self._mappings.items() if v == output_int][0]

            # Check whether we are at end of melody
            if output_symbol == "/":
                break
            # Update the meelody
            else:
                melody.append(output_symbol)

        return melody

    def _sample_with_temperature(self, probabilities, temperature):
        # Temperature value -> infinite, random picking one of the probabilities
        # Temperature value -> 0, higher gets 1 other 0
        # Temperature value -> 1, nothing changes in probabilities

        predictions = np.log(probabilities.clip(min=0.00000000001)) / temperature
        probabilities = np.exp(predictions) / np.sum(np.exp(predictions))

        choices = range(len(probabilities))    # [0, 1, 2, 3]
        index = np.random.choice(choices, p=probabilities)

        return index

    def save_melody(self, melody, step_duration=0.25, format="midi", file_name="mel1.mid", bpm=90):

        # Create a music21 stream
        stream = m21.stream.Stream()

        mm = m21.tempo.MetronomeMark(number=bpm)
        stream.append(mm)

        # Parse all the symbols in the melody and create note/rest objects
        # 60 _ _ _ r _ 62 _
        start_symbol = None
        step_counter = 1

        for i, symbol in enumerate(melody):

            # Handle case we have note/rest
            if symbol != "_" or i + 1 == len(melody):

                # Ensure we are dealing with note/rest beyond first one
                if start_symbol is not None:

                    # Calculate duration
                    quarter_length_duration = step_duration * step_counter    # 0.25 * 4

                    # Handle rest
                    if start_symbol == "r":
                        m21_event = m21.note.Rest(quarterLength=quarter_length_duration)

                    # Handle note
                    else:
                        m21_event = m21.note.Note(int(start_symbol), quarterLength=quarter_length_duration)

                    stream.append(m21_event)

                    # Reset step counter
                    step_counter = 1

                start_symbol = symbol

            # Handle case we have prolongation sign -> "_"
            else:
                step_counter += 1

        # Write the m21 stream to midi
        midi = stream.write(format)

        # Return the midi file object
        return midi

def parse_motif(motif, time_step=0.25, parser="series"):
    """ Parses the given notes to a string used by the model or by magenta

    Parameters
    ----------
    motif : str
        The notes given in string format
    time_step : float
        Based duration, default 16th note
    parser : string
        String to decide to parse the notes so
        they can be feed to the model or to magenta

    Returns
    -------
    string/dict
        a string (time series ) or dict that can be used to feed the model
        or magenta
    """

    # Initialize and empty list and total duration to zero
    encoded_motif = []
    total_duration = 0

    for notes in motif:
        # Handle notes
        if "r" not in notes["duration"]:
            # Convert pitch to its midi number
            symbol = m21.pitch.Pitch(notes['note']).midi

        # Handle rests
        else:
            symbol = "r"

        # Convert the note/rest into time series notation

        # Remove "r" and "." from the duration
        duration = float(notes["duration"].replace(".", "").replace("r", ""))

        # Calculate duration in quarter length
        duration = 4.0 / duration

        total_duration += duration

        # Update duration if note is dotted
        if (notes['dot']):
            duration += duration / 2.0

        # Calculate number of sixteenth steps
        steps = int(duration / time_step)

        # Create "_" based on number of steps
        for step in range(steps):
            if step == 0:
                encoded_motif.append(symbol)
            else:
                encoded_motif.append("_")

    # Cast encoded_motif to string
    encoded_motif = " ".join(map(str, encoded_motif))

    return encoded_motif, total_duration


text:
import numpy as np
import matplotlib.pyplot as plt

## Metoda Newton-Rhapson

def newton_rhapson(f, df, x0, epsilon=1e-5):
    """Găsește o soluție a funcției f cu derivata df, aplicând
    metoda lui Newton, pornind din punctul x0.
    """
    # Primul punct este cel primit ca parametru
    prev_x = x0
    # Aplicăm prima iterație
    x = x0 - f(x0) / df(x0)

    # Continuăm să calculăm până avem precizia cerută.
    num_steps = 1
    while abs(x - prev_x) / abs(prev_x) > epsilon:
        x, prev_x = x - f(x) / df(x), x

        num_steps += 1

    return x, num_steps

# Funcția pentru care vrem să găsim soluții.
f = lambda x: (x ** 3) - 7 * (x ** 2) + 14 * x - 6
# Prima derivată a lui f.
df = lambda x: 3 * (x ** 2) - 14 * x + 14

s1, n1 = newton_rhapson(f, df, 0.5)
s2, n2 = newton_rhapson(f, df, 2.5)
s3, n3 = newton_rhapson(f, df, 3.5)
print("Soluția", s1, "după", n1, "pași")
print("Soluția", s2, "după", n2, "pași")
print("Soluția", s3, "după", n3, "pași")

## Afișez rezultatul
# Creez un nou plot cu rezoluție mare
fig, ax = plt.subplots(1, dpi=200)

# Pun un titlu
plt.title('Metoda Newton-Rhapson')

# Configurez axele
ax.spines['bottom'].set_position('zero')
ax.spines['top'].set_color('none')
ax.spines['right'].set_color('none')

# Generez punctele
x = np.linspace(start=0, stop=5, num=1000)

# Desenez graficul funcției
plt.plot(x, f(x), '-.')

# Desenez soluțiile
plt.scatter([s1, s2, s3], [0, 0, 0], c='red')

# Afișez legenda
plt.legend(['f(x)', 'x_num'])

# Etichetez axele
plt.xlabel('x')
plt.ylabel('y')

plt.show()

ext: py

max_stars_repo_name: FloaterTS/teme-fmi

max_issues_repo_name: florinalexandrunecula/teme-fmi

max_forks_repo_name: florinalexandrunecula/teme-fmi

reason: import numpy


text:
#!/bin/env python3

import numpy as np

class HopfieldNetwork(object):
    async_default = False
    iterations_default = 1000
    def __init__(self, dim, iterations = iterations_default, async = async_default, hoppfield_original = False, preserve_elements = 0, clip = np.inf):
        assert dim > 0
        self.__matrix = np.zeros((dim,dim))
        self.__iterations = iterations
        self.__async = async
        self.__hoppfield_original = hoppfield_original
        self.__preserve_elements = preserve_elements
        self.__clip = clip

    ''' the matrix format is:
        sum{p}{outer_product(memory_i, memory_i)} /N  - (p/N)(Identity)
        that is equivalent to:
        sum{p}{(outer_product(memory_i, memory_i) - Identity)}/N
        Thus to add a new memory to M:
        M = (M + (outer_product(new_memory, new_memory) - Identity))/N
    '''
    def remember(self, data):
        # convert '0' -> '-1'
        data[data <= 0] = -1

        # The original Hoppfield paper does not normalize the weights
        # to number of neurons
        scale = 1 if self.__hoppfield_original else self.__matrix.size

        # create new memory
        new_memory = np.asmatrix(np.outer(data, data)) / scale
        np.fill_diagonal(new_memory, 0)

        assert new_memory.shape == self.__matrix.shape

        # add the new memory to memory matrix
        self.__matrix = (self.__matrix + new_memory);

        # Clip before replacing preserve lines. Clipping won't impact diagonal
        # in standard lines, but it might impact diagonal of preserved lines
        # which gets fixed below, by replacing the lines
        m = self.__matrix.clip(-self.__clip * scale, self.__clip * scale)
        assert np.isfinite(self.__clip) or (m == self.__matrix).all()

        # The above addition on messed with either non-preserved lines,
        # or '0.0' elements in the preserved lines.
        # Force replacing with eye matrix every time fixes this
        if self.__preserve_elements:
            # This creates __preserve_elements x data.size matrix with ones
            # on the diagonal
            m = np.eye(self.__preserve_elements, data.size)
            self.__matrix.put(np.arange(m.size),m.flatten());

    def recall(self, partial_data, iterations = None):
        assert partial_data.size  == self.__matrix.shape[0]

        # get iterations overload
        iterations = self.__iterations if (iterations is None) else iterations
        # The original paper uses {0,1} for neuronal representation
        if (not self.__hoppfield_original):
            partial_data[partial_data < 0.5] = -1

        # turn partial data into column vector
        probe = np.asarray(partial_data) if self.__async else np.asmatrix(partial_data).transpose()

        # iterate
        for i in range(iterations):
            if self.__async:
                # Use random async
                for v in np.random.permutation(probe.size):
                    probe[v] = np.sign(np.dot(self.__matrix[v], probe))
            else:
                probe = np.sign(np.matmul(self.__matrix, probe))
            # The original paper uses {0,1} for neuronal states
            if (self.__hoppfield_original):
                probe[probe <= 0] = 0

        # turn column vector back into array
        res = np.squeeze(np.asarray(probe.transpose()))

        # replace all '-1' with 0
        res[res < 0] = 0
        return res


text:
from optparse import OptionParser
import os, time, random, pickle

import torch
import numpy as np
from src.util.data_operator import read_conll, write_conll, vocab
from src.util.config_reader import Configurable
import importlib
import pdb

if __name__ == '__main__':

    random.seed(666)
    np.random.seed(666)
    torch.cuda.manual_seed(666)
    torch.manual_seed(666)

    argparser = OptionParser()
    argparser.add_option('--config_file', type="str", default='config.cfg')
    argparser.add_option("--numthread", type="int", dest="nthread", default=4)
    argparser.add_option('--use_cuda', action='store_true', default=True)
    argparser.add_option('--parsingmodel', type="str", default='BaseParser')

    (args, extra_args) = argparser.parse_args()
    options = Configurable(args.config_file, extra_args)

    torch.set_num_threads(args.nthread)
    print("Pytorch using {} threads.".format(torch.get_num_threads()))

    if options.external_embedding:
        print('Using external embedding:', options.external_embedding)

    if options.gpuFlag and torch.cuda.is_available():
        print("Use GPU!")

    with open(options.conll_test, 'r') as conllFP:
        test_sentences = list(read_conll(conllFP))

    '''
    predict
    '''
    with open(os.path.join(options.output, options.params), 'r') as paramsfp:
        words, w2i, p2i, rels = pickle.load(paramsfp)

    print('Initializing parser for testing:')

    if options.modelname == "ncrfae_model":
        parsermod = importlib.import_module('src.semi_ncrfae_parser')
    else:
        parsermod = importlib.import_module('src.sup_parser')

    MSTParser = getattr(parsermod, "MSTParserLSTM")

    parser = MSTParser(words, p2i, rels, w2i, options)

    parser.load(os.path.join(options.output, os.path.basename(options.savemodel)))
    conllu = (os.path.splitext(options.conll_test.lower())[1] == '.conllu')
    testpath = os.path.join(options.output, 'test_pred.conll' if not conllu else 'test_pred.conllu')

    ts = time.time()
    parser.predict(test_sentences)
    te = time.time()
    print('Finished predicting test.', te - ts, 'seconds.')
    write_conll(testpath, test_sentences)

    if not conllu:
        os.system('perl eval_utils/eval.pl -g ' + options.conll_test + ' -s ' + testpath + ' > ' + testpath + '.txt')
        with open(testpath + '.txt', 'rb') as f:
            for l in f:
                if l.startswith('  Unlabeled'):
                    print('UAS:%s' % l.strip().split()[-2])
                # elif l.startswith('Labeled'):
                    # print('LAS:%s' % l.strip().split()[-2])
                    # pass
    else:
        os.system(
            'python eval_utils/evaluation_script/conll17_ud_eval.py -v -w eval_utils/evaluation_script/weights.clas ' + options.conll_test + ' ' + testpath + ' > ' + testpath + '.txt')
        with open(testpath + '.txt', 'rb') as f:
            for l in f:
                if l.startswith('UAS'):
                    print('UAS:%s' % l.strip().split()[-1])
                # elif l.startswith('LAS'):
                    # print('LAS:%s' % l.strip().split()[-1])
                    # pass


text:
import numpy as np
import sys
import os
pssePath="C:\Program Files (x86)\PTI\PSSE33\PSSBIN"
sys.path.append(pssePath)
os.environ['PATH']+=';'+pssePath
import psspy

from tdcosim.global_data import GlobalData

class PSSEModel:
	def __init__(self):
		# psse
		self._psspy=psspy
		psspy.psseinit(0)
		psspy.report_output(6,'',[])
		psspy.progress_output(6,'',[])
		psspy.alert_output(6,'',[])
		psspy.prompt_output(6,'',[])

		return None

	def setup(self, adjustOpPoint=True):
		# psspy info
		self._monitorID={}
		self._monitorID['angle'] = 1
		self._monitorID['pelec'] = 2
		self._monitorID['qelec'] = 3
		self._monitorID['eterm'] = 4
		self._monitorID['efd'] = 5
		self._monitorID['pmech'] = 6
		self._monitorID['speed'] = 7
		self._monitorID['xadifd'] = 8
		self._monitorID['ecomp'] = 9
		self._monitorID['volt'] = 13
		self._monitorID['pload'] = 25
		self._monitorID['qload'] = 26

		# load psse case
		ierr = self._psspy.read(0,GlobalData.config['psseConfig']['rawFilePath'])
		assert ierr==0,"Reading raw file failed with error {}".format(ierr)
		ierr, nLoads = self._psspy.alodbuscount()
		assert ierr==0,"load bus count failed with error {}".format(ierr)
		GlobalData.data['TNet']['LoadBusCount'] = nLoads
		# default. Will connect dist syst feeder to all load buses
		ierr, loadBusNumber = self._psspy.alodbusint(string='NUMBER')
		assert ierr==0,"load bus number failed with error {}".format(ierr)
		GlobalData.data['TNet']['LoadBusNumber'] = loadBusNumber[0]

		if adjustOpPoint:# need to adjust operation point
			# find total load
			GlobalData.data['TNet']['TotalRealPowerLoad'] = 0
			GlobalData.data['TNet']['BusRealPowerLoad'] = {}

			ierr,S = self._psspy.alodbuscplx(string='MVAACT')
			assert ierr==0,"Reading bus complex load failed with error {}".format(ierr)

			for entry,val in zip(GlobalData.data['TNet']['LoadBusNumber'],S[0]):
				GlobalData.data['TNet']['TotalRealPowerLoad'] += val.real
				GlobalData.data['TNet']['BusRealPowerLoad'][entry]=val.real

	def dynamicInitialize(self, adjustOpPoint=True):
		if adjustOpPoint:
			S = self._adjustSystemOperatingPoint()
		else:
			self._psspy.dyre_new([1,1,1,1],self.config['psseConfig']['dyrFilePath'])
		self._psspy.cong(1)
		GlobalData.data['dynamic']['channel'] = {}
		nMonVars=0
		nGenBus=self._psspy.agenbuscount(-1,1)[1]
		nBus=self._psspy.abuscount(-1,1)[1]
		nLoad=self._psspy.aloadcount(-1,1)[1]
		genBusNumber=self._psspy.agenbusint(-1,1,'NUMBER')[1][0]
		busNumber=self._psspy.abusint(string='NUMBER')[1][0]
		loadBusNumber=self._psspy.aloadint(-1,1,'NUMBER')[1][0]
		for item in ['angle','speed','pelec','qelec','pmech']:
			self._psspy.chsb(sid=0,all=1,status=[-1,-1,-1,1,self._monitorID[item],0])
			GlobalData.data['dynamic']['channel'][item]={}
			for channelID,node in zip(range(nMonVars+1,nMonVars+1+nGenBus),genBusNumber):# psse uses 1 ind
				GlobalData.data['dynamic']['channel'][item][channelID]=node
			nMonVars+=nGenBus

		self._psspy.chsb(sid=0,all=1,status=[-1,-1,-1,1,self._monitorID['volt'],0])
		GlobalData.data['dynamic']['channel']['volt']={}
		for channelID,node in zip(range(nMonVars+1,nMonVars+1+nBus),busNumber):# psse uses 1 ind
			GlobalData.data['dynamic']['channel']['volt'][channelID]=node
		nMonVars+=nBus

		for item in ['pload','qload']:
			self._psspy.chsb(sid=0,all=1,status=[-1,-1,-1,1,self._monitorID[item],0])
			GlobalData.data['dynamic']['channel'][item]={}
			for channelID,node in zip(range(nMonVars+1,nMonVars+1+nLoad),loadBusNumber):# psse uses 1 ind
				GlobalData.data['dynamic']['channel'][item][channelID]=node
			nMonVars+=nLoad

		self._psspy.strt(outfile=r'result.out')# compute initial conditions

		Vpcc=self.getVoltage()
		targetS={}
		for entry,val in zip(GlobalData.data['TNet']['LoadBusNumber'],S[0]):
			if entry in GlobalData.data['DNet']['Nodes']:
				targetS[entry]=[val.real*10**3,val.imag*10**3] # convert to kw and kvar from mw and mvar
		return targetS,Vpcc

	def _adjustSystemOperatingPoint(self):
		loadType = 0
		try:
			offset=3
			reductionPercent=GlobalData.data['DNet']['ReductionPercent']
			ind={}
			ind['GENCLS']=0
			ind['GENDCO']=4
			ind['GENROE']=4
			ind['GENROU']=4
			ind['GENSAE']=3
			ind['GENSAL']=3
			ind['GENTPJU1']=4
			ind['GENTRA']=1

			dyrPath=GlobalData.config['psseConfig']['dyrFilePath']
			f=open(dyrPath)
			dyrData=f.read().splitlines()
			f.close()

			dyrDataStr=''; Zr={}; Zx={}
			for line in dyrData:
				entry=line.split(',')
				for item in ind:
					if entry[1]=="'{}'".format(item):
						entry[offset+ind[item]]=\
						str(float(entry[offset+ind[item]])*(1-reductionPercent))
						break
				dyrDataStr+=','.join(entry+['\n'])

			tempDyrPath=dyrPath.split('.dyr')[0]+'_temp.dyr'
			f=open(tempDyrPath,'w')
			f.write(dyrDataStr)
			f.close()

			# now read raw file to get Zr and Zx
			f=open(GlobalData.config['psseConfig']['rawFilePath'])
			rawFileData=f.read().splitlines()
			f.close()

			readFlg=False
			for line in rawFileData:
				if "END OF GENERATOR DATA" in line:
					readFlg=False
				if readFlg:
					entry=line.split(',')
					Zr[int(entry[0])]=float(entry[9])
					Zx[int(entry[0])]=float(entry[10])
				if "BEGIN GENERATOR DATA" in line:
					readFlg=True

			# modify config to point to the temp dyr file
			GlobalData.config['psseConfig']['dyrFilePath']=tempDyrPath

			# make changes in machine data through psse internal data structure
			m=macVarMap={}
			m['PGEN']=0
			m['QGEN']=1
			m['QMAX']=2
			m['QMIN']=3
			m['PMAX']=4
			m['PMIN']=5
			m['MBASE']=6

			# read dyr file
			self._psspy.dyre_new([1,1,1,1],GlobalData.config['psseConfig']['dyrFilePath'])

			# get machine data
			macVarData={}
			for entry in macVarMap:
				ierr,macVarData[entry]=self._psspy.amachreal(sid=-1, flag=1, string=entry)# get data
				assert ierr==0,"reading machine data failed with error {}".format(ierr)

			genBusNumber=self._psspy.agenbusint(-1,1,'NUMBER')[1][0] # get gen bus number

			# change machine data
			for n in range(len(genBusNumber)):# make changes at each gen
				macVarDataNew=[0.]*11+[1.]*6
				for entry in macVarData:# make changes for each variable
					# passing double precision data results in long values
					# and psspy.machine_chng_2 API fails to change data.
					# Hence, use 3 digit precision.
					macVarDataNew[macVarMap[entry]]=np.round(macVarData[entry][0][n]\
					*(1-reductionPercent),5)
				macVarDataNew[7]=np.round(Zr[genBusNumber[n]],5)
				macVarDataNew[8]=np.round(Zx[genBusNumber[n]],5)
				self._psspy.machine_chng_2(i=genBusNumber[n], realar=macVarDataNew) # change machine data

			# adjust load data
			ierr,S=self._psspy.alodbuscplx(string='MVAACT')
			assert ierr==0,"reading complex load failed with error {}".format(ierr)

			for busID,val in zip(GlobalData.data['TNet']['LoadBusNumber'],S[0]):
				if busID in GlobalData.data['DNet']['Nodes']:
					# constP,Q,IP,IQ,YP,YQ
					loadVal=[0]*6
					reductionPercent=GlobalData.data['DNet']['Nodes'][busID]['solarPenetration']
					loadVal[loadType*2],loadVal[loadType*2+1]=\
					val.real*(1-reductionPercent),val.imag*(1-reductionPercent)
					ierr=psspy.load_chng_4(busID,'1',[1,1,1,1,1,0],loadVal)
					assert ierr==0,"load change failed with error {}".format(ierr)

			return S
		except:
			GlobalData.log('Failed to adjustSystemOperatingPoint from PSSEModel')

	def staticInitialize(self):
		try:
			Vpcc=self.getVoltage()

			# scale feeder
			targetS={}
			ierr,S=self._psspy.alodbuscplx(string='MVAACT')
			assert ierr==0,"Reading load bus complex power failed with error {}".format(ierr)

			for entry,val in zip(GlobalData.data['TNet']['LoadBusNumber'],S[0]):
				if entry in GlobalData.data['DNet']['Nodes']:
					targetS[entry]=[val.real*10**3,val.imag*10**3] # convert to kw and kvar from mw and mvar

			return targetS, Vpcc
		except Exception as e:
			GlobalData.log('Failed to initialize from PSSEModel')

	#===================GET VOLTAGE FROM PSSSE========================
	def getVoltage(self):
		try:
			"""Get PCC voltage from psse."""
			Vpcc={}
			if GlobalData.data['TNet']['LoadBusCount']==len(GlobalData.data['TNet']['LoadBusNumber']): # dist syst interfaced at all load buses
				loadBusVPU=self._psspy.alodbusreal(string='PU')
				loadBusVPU = loadBusVPU[1][0]
				for entry,val in zip(GlobalData.data['TNet']['LoadBusNumber'],loadBusVPU):# efficient
					if entry in GlobalData.data['DNet']['Nodes']:
						Vpcc[entry]=val
			else:# subset of loadbuses interfaced as dist syst
				for entry in GlobalData.data['TNet']['LoadBusNumber']: # not as efficient for large cases
					Vpcc[entry]=self._psspy.busdat(entry,'PU')[1]
			return Vpcc
		except Exception as e:
			GlobalData.log('Failed to getVoltage from PSSEModel')

	def setLoad(self, S,loadType=0):
		"""set PCC Pinj,Qinj for psse.
		Input: S -- dictionary containing Pinj and Qinj.
		loadType -- 0- constant power, 1-constant current, 2-constant admittance."""
		for busID in GlobalData.data['TNet']['LoadBusNumber']:
			if busID in GlobalData.data['DNet']['Nodes']:
				# constP,Q,IP,IQ,YP,YQ
				loadVal=[0]*6
				loadVal[loadType*2],loadVal[loadType*2+1]=S[busID]['P'],S[busID]['Q']
				ierr=self._psspy.load_chng_4(busID,'1',[1,1,1,1,1,0],realar=loadVal)
				assert ierr==0,"load change failed with error {}".format(ierr)

	def shunt(self, targetS, Vpcc, power):
		try:
			mismatchTolerance=0.1
			for node in power:
				if abs(power[node]['P']-targetS[node][0])>mismatchTolerance or abs(power[node]['Q']-targetS[node][1])>mismatchTolerance:# add shunt if needed
					Pshunt = targetS[node][0]*1e-3 - power[node]['P']
					Qshunt = targetS[node][1]*1e-3 - power[node]['Q']
					# The remaining power is incorporated as compensating shunt
					# The compensating shunt power
					# Pshunt + j*Qshunt = Vpcc^2*(YPshunt - YQshunt)
					# which gives the below two equations for shunt.
					# Note the negative sign for YQshunt is because we are
					# considering admittances
					YPshunt = Pshunt/(Vpcc[node]*Vpcc[node])
					YQshunt = -Qshunt/(Vpcc[node]*Vpcc[node])
					# Add the remaining as fixed compensating shunt
					ierr = self._psspy.shunt_data(node,'1 ',1,[YPshunt,YQshunt])
					assert ierr==0,"Adding shunt failed with error {}".format(ierr)
		except Exception as e:
			GlobalData.log('Failed to shunt from PSSEModel')

	def runPFLOW(self):
		self._psspy.fnsl()
	def runDynamic(self, tpause):
		self._psspy.run(tpause=tpause)
	def faultOn(self, faultBus, faultImpedance):
		self._psspy.dist_bus_fault(faultBus, 1,0.0,faultImpedance)
	def faultOff(self):
		self._psspy.dist_clear_fault()

max_stars_repo_name: cuihantao/TDcoSim

max_issues_repo_name: cuihantao/TDcoSim

max_forks_repo_name: cuihantao/TDcoSim


text:
import numpy as np
import gym
import time

# Make Environment and Q-Table
env = gym.make("FrozenLake-v0")

actionSize = env.action_space.n
stateSize = env.observation_space.n

qTable = np.zeros((stateSize, actionSize))

f = open("qtable.txt", "r")

for i in range(stateSize):
    for j in range(actionSize):
        qTable[i][j] = float(f.readline())
f.close()

for episode in range(1):
    state = env.reset()
    done = False
    print("****************************************************")
    print("EPISODE ", episode)

    for step in range(99):
        env.render()
        time.sleep(0.3)
        # Take the action (index) that have the maximum expected future reward given that state
        action = np.argmax(qTable[state][:])
        print(state, action)

        new_state, reward, done, info = env.step(action)

        if done:
            # Here, we decide to only print the last state (to see if our agent is on the goal or fall into an hole)
            env.render()
            time.sleep(0.3)
            # We print the number of step it took.
            print("Number of steps", step)
            break
        state = new_state
env.close()


text:
import json
import numpy as np
import os.path as op
import pandas as pd
import pytest

import afqinsight as afqi
from afqinsight import AFQDataFrameMapper
from afqinsight import (
    multicol2dicts,
    multicol2sets,
    sort_features,
    beta_hat_by_groups,
    unfold_beta_hat_by_metrics,
)
from afqinsight.transform import isiterable

data_path = op.join(afqi.__path__[0], "data")
test_data_path = op.join(data_path, "test_data")

@pytest.mark.parametrize("concat_subject_session", [True, False])
@pytest.mark.parametrize("set_ses_to_int", [True, False])
def test_AFQDataFrameMapper(concat_subject_session, set_ses_to_int):
    nodes_path = op.join(test_data_path, "nodes.csv")
    nodes = pd.read_csv(nodes_path)
    if set_ses_to_int:
        nodes["sessionID"] = 1
    transformer = AFQDataFrameMapper(concat_subject_session=concat_subject_session)
    X = transformer.fit_transform(nodes)
    groups = transformer.groups_
    cols = transformer.feature_names_
    subjects = transformer.subjects_

    X_ref = np.load(op.join(test_data_path, "test_transform_x.npy"))
    groups_ref = np.load(op.join(test_data_path, "test_transform_groups.npy"))
    cols_ref = [
        tuple(item)
        for item in np.load(op.join(test_data_path, "test_transform_cols.npy"))
    ]

    assert np.allclose(X, X_ref, equal_nan=True)  # nosec
    assert np.allclose(groups, groups_ref)  # nosec
    assert cols == cols_ref  # nosec
    if concat_subject_session:
        assert set(subjects) == set(
            (nodes.subjectID + nodes.sessionID.astype(str)).unique()
        )  # nosec
    else:
        assert set(subjects) == set(nodes.subjectID.unique())  # nosec

@pytest.mark.parametrize("concat_subject_session", [True, False])
def test_AFQDataFrameMapper_mean(concat_subject_session):
    nodes_path = op.join(test_data_path, "nodes.csv")
    nodes = pd.read_csv(nodes_path)
    transformer = AFQDataFrameMapper(
        bundle_agg_func="mean", concat_subject_session=concat_subject_session
    )
    X = transformer.fit_transform(nodes)
    groups = transformer.groups_
    cols = transformer.feature_names_
    subjects = transformer.subjects_

    X_ref = (
        nodes.groupby(["subjectID", "tractID"])
        .agg("mean")
        .drop("nodeID", axis="columns")
        .unstack("tractID")
        .to_numpy()
    )
    groups_ref = [np.array([idx]) for idx in range(X.shape[1])]
    cols_ref = set(
        [
            tuple([item[0], item[1]])
            for item in np.load(op.join(test_data_path, "test_transform_cols.npy"))
        ]
    )

    assert np.allclose(groups, groups_ref)  # nosec
    assert set(cols) == cols_ref  # nosec
    if concat_subject_session:
        assert set(subjects) == set(
            (nodes.subjectID + nodes.sessionID).unique()
        )  # nosec
    else:
        assert set(subjects) == set(nodes.subjectID.unique())  # nosec
    assert np.allclose(X, X_ref, equal_nan=True)  # nosec

@pytest.mark.parametrize("bundle_agg_func", [None, "mean"])
def test_AFQDataFrameMapper_fit_transform(bundle_agg_func):
    nodes_path = op.join(test_data_path, "nodes.csv")
    nodes = pd.read_csv(nodes_path)
    transformer = AFQDataFrameMapper(
        bundle_agg_func=bundle_agg_func, pd_interpolate_kwargs={"method": "cubic"}
    )
    X_ft = transformer.fit_transform(nodes)
    groups_ft = transformer.groups_
    cols_ft = transformer.feature_names_
    subjects_ft = transformer.subjects_

    transformer = AFQDataFrameMapper(
        bundle_agg_func=bundle_agg_func, pd_interpolate_kwargs={"method": "cubic"}
    )
    transformer.fit(nodes)
    X_t = transformer.transform(nodes)
    groups_t = transformer.groups_
    cols_t = transformer.feature_names_
    subjects_t = transformer.subjects_

    assert np.allclose(X_ft, X_t, equal_nan=True)  # nosec
    assert np.allclose(groups_ft, groups_t)  # nosec
    assert cols_ft == cols_t  # nosec
    assert subjects_ft == subjects_t  # nosec

def test_isiterable():
    assert isiterable(range(10))  # nosec
    assert not isiterable(5)  # nosec
    assert isiterable(np.arange(10))  # nosec

def _is_lateral_bundle(bundle):
    return "left" in bundle.lower() or "right" in bundle.lower()

def _drop_left_right(bundle):
    return (
        bundle.replace("left", "")
        .replace("right", "")
        .replace("Left", "")
        .replace("Right", "")
        .strip(" ")
    )

def _relateralize_set(_set):
    lateral_list = list(filter(_is_lateral_bundle, _set))
    if lateral_list:
        return _set - set([_drop_left_right(lateral_list[0])])
    else:
        return _set

def test_multicol_utils():
    cols = [
        tuple(item)
        for item in np.load(op.join(test_data_path, "test_transform_cols.npy"))
    ]
    label_sets = multicol2sets(
        pd.MultiIndex.from_tuples(cols, names=["metric", "tractID", "nodeID"])
    )
    label_sets_ref = np.load(
        op.join(test_data_path, "test_multicol2sets_label_sets.npy"), allow_pickle=True
    )
    assert np.all(label_sets == label_sets_ref)  # nosec

    label_sets = multicol2sets(
        pd.MultiIndex.from_tuples(cols, names=["metric", "tractID", "nodeID"]),
        tract_symmetry=False,
    )
    label_sets_ref = np.array([_relateralize_set(_set) for _set in label_sets_ref])
    assert np.all(label_sets == label_sets_ref)  # nosec

    label_dicts = multicol2dicts(
        pd.MultiIndex.from_tuples(cols, names=["metric", "tractID", "nodeID"])
    )
    with open(
        op.join(test_data_path, "test_multicol2dicts_label_dicts.json"), "r"
    ) as fp:
        label_dicts_ref = json.load(fp)

    assert label_dicts == label_dicts_ref  # nosec

    label_dicts = multicol2dicts(
        pd.MultiIndex.from_tuples(cols, names=["metric", "tractID", "nodeID"]),
        tract_symmetry=False,
    )
    label_dicts_ref = [
        {key: val for key, val in _dict.items() if key != "symmetrized_tractID"}
        for _dict in label_dicts_ref
    ]
    assert label_dicts == label_dicts_ref  # nosec

    scores = np.arange(len(label_dicts))
    sorted_labels = sort_features(label_dicts, scores)
    assert sorted_labels == list(zip(reversed(label_dicts), reversed(scores)))  # nosec

    sorted_labels = sort_features(label_dicts, reversed(scores))
    assert sorted_labels == list(zip(label_dicts, reversed(scores)))  # nosec

def test_beta_hat_transforms():
    cols = [
        tuple(item)
        for item in np.load(op.join(test_data_path, "test_transform_cols.npy"))
    ]
    multicols = pd.MultiIndex.from_tuples(cols, names=["metric", "tractID", "nodeID"])
    beta_hat = np.random.rand(len(cols))
    result = beta_hat_by_groups(beta_hat, multicols)
    reshaped_beta = beta_hat.reshape(2, 3, -1)

    for bundle_idx, bundle in enumerate(result.keys()):
        for metric_idx, metric in enumerate(result[bundle].keys()):
            assert np.allclose(
                reshaped_beta[metric_idx, bundle_idx], result[bundle][metric]
            )

    result = unfold_beta_hat_by_metrics(
        beta_hat, multicols, tract_names=list(result.keys())
    )
    reshaped_beta = beta_hat.reshape(2, -1)
    for metric_idx, metric in enumerate(result.keys()):
        assert np.allclose(reshaped_beta[metric_idx], result[metric])

    beta_hat[0:100] = 0.0
    beta_hat[300:400] = 0.0
    result = beta_hat_by_groups(beta_hat, multicols, drop_zeros=True)
    reshaped_beta = beta_hat.reshape(2, 3, -1)

    for bundle_idx, bundle in enumerate(result.keys()):
        for metric_idx, metric in enumerate(result[bundle].keys()):
            assert np.allclose(
                reshaped_beta[metric_idx, bundle_idx + 1], result[bundle][metric]
            )

max_stars_repo_name: arokem/AFQ-Insight

max_issues_repo_name: arokem/AFQ-Insight

max_forks_repo_name: arokem/AFQ-Insight


text:
from collections import Counter
import pickle
import re
import numpy as np
import pandas as pd
from tqdm.auto import tqdm
from rdkit import Chem
from rdkit.Chem.BRICS import reactionDefs

import moses
from moses.metrics.utils import fragmenter
from moses.utils import mapper

isotope_re = re.compile(r'\[([0-9]+)[ab]?\*\]')

"""
Prepare binary connection rules
"""
binary_defs = {}
connection_rules = set()
for row in reactionDefs:
    for a, b, t in row:
        if a in ['7a', '7b']:
            binary_defs[7] = 2**7
            connection_rules.add((7, 7))
        else:
            a = int(a)
            b = int(b)
            binary_defs[a] = binary_defs.get(a, 0) | 2**b
            binary_defs[b] = binary_defs.get(b, 0) | 2**a
            connection_rules.add((a, b))
            connection_rules.add((b, a))

class CombinatorialGenerator:
    def __init__(self, n_jobs=1, mode=0):
        """
        Combinatorial Generator randomly connects BRICS fragments

        Arguments:
            n_jobs: number of processes for training
            mode: sampling mode
                0: Sample fragment then connection
                1: Sample connection point then fragments
        """
        self.n_jobs = n_jobs
        self.set_mode(mode)
        self.fitted = False

    def fit(self, data):
        """
        Collects fragment frequencies in a training set

        Arguments:
            data: list of SMILES, training dataset

        """
        # Split molecules from dataset into BRICS fragments
        fragments = mapper(self.n_jobs)(fragmenter, data)

        # Compute fragment frequencies
        counts = Counter()
        for mol_frag in fragments:
            counts.update(mol_frag)
        counts = pd.DataFrame(
            pd.Series(counts).items(),
            columns=['fragment', 'count']
        )
        counts['attachment_points'] = [
            fragment.count('*')
            for fragment in counts['fragment'].values
        ]
        counts['connection_rules'] = [
            self.get_connection_rule(fragment)
            for fragment in counts['fragment'].values
        ]
        counts['frequency'] = counts['count'] / counts['count'].sum()
        self.fragment_counts = counts

        # Compute number of fragments distribution
        fragments_count_distribution = Counter([len(f) for f in fragments])
        total = sum(fragments_count_distribution.values())
        for k in fragments_count_distribution:
            fragments_count_distribution[k] /= total
        self.fragments_count_distribution = fragments_count_distribution
        self.fitted = True
        return self

    def save(self, path):
        """
        Saves a model using pickle

        Arguments:
            path: path to .pkl file for saving
        """
        if not self.fitted:
            raise RuntimeError("Can't save empty model."
                               " Fit the model first")
        data = {
            'fragment_counts': self.fragment_counts,
            'fragments_count_distribution': self.fragments_count_distribution,
            'n_jobs': self.n_jobs,
            'mode': self.mode
        }
        with open(path, 'wb') as f:
            pickle.dump(data, f)

    @classmethod
    def load(cls, path):
        """
        Loads saved model

        Arguments:
            path: path to saved .pkl file

        Returns:
            Loaded CombinatorialGenerator
        """
        with open(path, "rb") as f:
            data = pickle.load(f)
        model = cls()
        model.fragment_counts = data['fragment_counts']
        model.fragments_count_distribution = \
            data['fragments_count_distribution']
        model.n_jobs = data['n_jobs']
        model.mode = data['mode']
        model.fitted = True
        return model

    def set_mode(self, mode):
        if mode not in [0, 1]:
            raise ValueError('Incorrect mode value: %s' % mode)
        self.mode = mode

    def generate_one(self, seed=None):
        """
        Generates a SMILES string using fragment frequencies

        Arguments:
            seed: if specified, will set numpy seed before sampling

        Returns:
            SMILES string
        """
        if not self.fitted:
            raise RuntimeError("Fit the model before generating")
        if seed is not None:
            np.random.seed(seed)
        mol = None

        # Sample the number of fragments
        count_values, count_probs = zip(
            *self.fragments_count_distribution.items())
        total_fragments = np.random.choice(count_values, p=count_probs)

        counts = self.fragment_counts
        current_attachments = 0
        max_attachments = total_fragments - 1
        connections_mol = None
        for i in range(total_fragments):
            # Enforce lower and upper limit on the number of connection points
            counts_masked = counts[
                counts['attachment_points'] <= max_attachments
            ]
            if total_fragments == 1:
                min_attachments = 0
            elif i != 0 and current_attachments == 1 and \
                    total_fragments > i + 1:
                min_attachments = 2
            else:
                min_attachments = 1
            counts_masked = counts_masked[
                counts_masked['attachment_points'] >= min_attachments
            ]

            if mol is None:
                mol = self.sample_fragment(counts_masked)
            else:
                if self.mode == 1:  # Choose connection atom first
                    connections_mol = [np.random.choice(connections_mol)]

                con_filter = self.get_connection_filter(connections_mol)
                # Mask fragments with possible reactions
                counts_masked = counts_masked[
                    counts_masked['connection_rules'] & con_filter > 0
                ]
                fragment = self.sample_fragment(counts_masked)
                connections_fragment = self.get_connection_points(fragment)
                possible_connections = self.filter_connections(
                    connections_mol,
                    connections_fragment
                )

                c_i = np.random.choice(len(possible_connections))
                a1, a2 = possible_connections[c_i]

                # Connect a new fragment to the molecule
                mol = self.connect_mols(mol, fragment, a1, a2)

            connections_mol = self.get_connection_points(mol)
            current_attachments = len(connections_mol)
            max_attachments = (
                total_fragments - i - current_attachments
            )
        smiles = Chem.MolToSmiles(mol)
        return smiles

    def generate(self, n, seed=1, mode=0, verbose=False):
        self.set_mode(mode)
        seeds = range((seed - 1) * n, seed * n)
        if verbose:
            print('generating...')
            seeds = tqdm(seeds, total=n)
        samples = mapper(self.n_jobs)(self.generate_one, seeds)
        return samples

    def get_connection_rule(self, fragment):
        """
        return OR combination for possible incoming reactions

        Arguments:
            fragment: fragment smiles

        Returns:
            int
        """
        rule = 0
        for i in map(int, set(isotope_re.findall(fragment))):
            rule |= binary_defs[i]
        return rule

    @staticmethod
    def sample_fragment(counts):
        new_fragment = counts.sample(
            weights=counts['frequency']
        )
        new_fragment = dict(new_fragment.iloc[0])
        fragment = Chem.MolFromSmiles(new_fragment['fragment'])
        return fragment

    @staticmethod
    def get_connection_filter(atoms):
        """
        Return OR(2**isotopes)
        """
        connection_rule = 0
        for atom in atoms:
            connection_rule |= 2**atom.GetIsotope()
        return connection_rule

    @staticmethod
    def get_connection_points(mol):
        """
        Return connection points

        Arguments:
            mol: ROMol

        Returns:
            atom list
        """
        atoms = []
        for atom in mol.GetAtoms():
            if atom.GetSymbol() == '*':
                atoms.append(atom)
        return atoms

    @staticmethod
    def filter_connections(atoms1, atoms2, unique=True):
        possible_connections = []
        for a1 in atoms1:
            i1 = a1.GetIsotope()
            for a2 in atoms2:
                i2 = a2.GetIsotope()
                if (i1, i2) in connection_rules:
                    possible_connections.append((a1, a2))
        return possible_connections

    @staticmethod
    def connect_mols(mol1, mol2, atom1, atom2):
        combined = Chem.CombineMols(mol1, mol2)
        emol = Chem.EditableMol(combined)
        neighbor1_idx = atom1.GetNeighbors()[0].GetIdx()
        neighbor2_idx = atom2.GetNeighbors()[0].GetIdx()
        atom1_idx = atom1.GetIdx()
        atom2_idx = atom2.GetIdx()
        bond_order = atom2.GetBonds()[0].GetBondType()
        emol.AddBond(neighbor1_idx,
                     neighbor2_idx + mol1.GetNumAtoms(),
                     order=bond_order)
        emol.RemoveAtom(atom2_idx + mol1.GetNumAtoms())
        emol.RemoveAtom(atom1_idx)
        mol = emol.GetMol()
        return mol

def reproduce(seed, samples_path=None, metrics_path=None,
              n_jobs=1, device='cpu', verbose=False,
              samples=30000):
    train = moses.get_dataset('train')
    model = CombinatorialGenerator(n_jobs=n_jobs)

    if verbose:
        print("Training...")
    model.fit(train)

    if verbose:
        print(f"Sampling for seed {seed}")
    seeds = list(range(
        (seed - 1) * samples, seed * samples
    ))
    samples = mapper(n_jobs)(model.generate_one, seeds)
    if samples_path is not None:
        with open(samples_path, 'w') as f:
            f.write('SMILES\n')
            for sample in samples:
                f.write(sample+'\n')
    if verbose:
        print(f"Computing metrics for seed {seed}")
    metrics = moses.get_all_metrics(
        samples, n_jobs=n_jobs, device=device)
    if metrics_path is not None:
        with open(metrics_path, 'w') as f:
            for key, value in metrics.items():
                f.write("%s,%f\n" % (key, value))
    return samples, metrics

if __name__ == "__main__":
    import argparse
    import os

    parser = argparse.ArgumentParser(
        "Reproduce combinatorial experiment")
    parser.add_argument(
        '--n_jobs', type=int, required=False,
        default=1, help='Number of threads for computing metrics')
    parser.add_argument(
        '--device', type=str, required=False,
        default='cpu', help='Device for computing metrics')
    parser.add_argument(
        '--metrics_path', type=str, required=False,
        default='.', help='Path to save metrics')
    args = parser.parse_known_args()[0]

    for seed in [1, 2, 3]:
        filename = f'combinatorial_metrics_{seed}.csv'
        metrics_path = os.path.join(args.metrics_path, filename)

        reproduce(seed=seed, n_jobs=args.n_jobs,
                  device=args.device, verbose=True,
                  metrics_path=metrics_path)

max_stars_repo_name: nataliemaus/moses

max_issues_repo_name: nataliemaus/moses

max_forks_repo_name: nataliemaus/moses


text:
import tqdm
import nltk
import pandas as pd
import numpy as np

def convert_tokens_to_ids(tokenized_sentences, words_dict, embedding_word_dict, sentences_length,
                          unknown_word_id, end_word_id):
    """
    Convert tokenized sentences based on word_list dictionary to embedding_word_dict dictionary.
    :param tokenized_sentences: list of sentences [0, 9, 5].
    :param words_list:  index - word dictionary.
    :param embedding_word_dict:  word - index dictionary.
    :param sentences_length: max length of sentence.
    :param unknown_word_id:
    :param end_word_id:
    :return: list of tokenized sentences.
    """
    words_train = []

    for sentence in tokenized_sentences:
        current_words = []
        for word_index in sentence:
            word = words_dict[word_index]
            word_id = embedding_word_dict.get(word, unknown_word_id)
            current_words.append(word_id)

        if len(current_words) >= sentences_length:
            # half_len = int(sentences_length / 2)
            # current_words = current_words[:half_len] + current_words[len(current_words) - half_len:]
            current_words = current_words[:sentences_length]
        else:
            current_words += [end_word_id] * (sentences_length - len(current_words))
        words_train.append(current_words)

    return words_train

# TODO: very slow and get a lot of memory. Make it with numpy array.
# Pretrained embedding
# https://nlp.stanford.edu/projects/glove/
# https://code.google.com/archive/p/word2vec/
def read_embedding_list(file_path):

    embedding_list = []
    with open(file_path) as f:
        for row in tqdm.tqdm(f.read().split("\n")[1:-1]):
            data = row.split(" ")
            vec = {}
            vec[0] = data[0]
            vec.update({i + 1: float(a) for i, a in enumerate(data[1:-1])})
            embedding_list.append(vec)

    df = pd.DataFrame(embedding_list)
    df = df.set_index(0)
    df = df.astype('float32')
    print('embedding shape', df.shape)
    return df

# Sample of tokenize sentences
def tokenize_sentences(sentences, words_dict):
    tokenized_sentences = []
    tweet_knzr = nltk.tokenize.TweetTokenizer(strip_handles=False, reduce_len=False)
    for sentence in tqdm.tqdm(sentences):
        if hasattr(sentence, "decode"):
            sentence = sentence.decode("utf-8")
        # tokens = nltk.tokenize.word_tokenize(sentence)
        tokens = tweet_knzr.tokenize(sentence)
        result = []
        for word in tokens:
            word = word.lower()

            # if word not in WORDS_INDEX:
            #     word = WORDS_CORECTION.get(word, word)

            if word not in words_dict:
                # continue
                words_dict[word] = len(words_dict)
            word_index = words_dict[word]
            result.append(word_index)
        tokenized_sentences.append(result)

    return tokenized_sentences, words_dict

# Todo: sample of making embeding, redo it.
def create_datasets(embedding_path, maxlen=500):
    train = pd.read_csv('input/train.csv')
    test = pd.read_csv('input/test.csv')

    print('read_embedding_list')
    path_out = 'glove/crawl-300d-2M.csv'
    word_embeding = pd.read_csv(path_out, index_col=0)
    # word_embeding = read_embedding_list(embedding_path)
    # word_embeding = pd.read_table(embedding_path, sep=" ", index_col=0, header=None, quoting=csv.QUOTE_NONE)
    embedding_size = word_embeding.shape[1]

    # text cleaning
    train['comment_text'] = train['comment_text'].apply(clean_text)
    test['comment_text'] = test['comment_text'].apply(clean_text)

    # Tokenizing
    list_sentences_train = train["comment_text"].fillna(NAN_WORD).values
    list_sentences_test = test["comment_text"].fillna(NAN_WORD).values

    print("Tokenizing sentences in train set...")
    tokenized_sentences_train, words_dict = tokenize_sentences(list_sentences_train, {})

    print("Tokenizing sentences in test set...")
    tokenized_sentences_test, words_dict = tokenize_sentences(list_sentences_test, words_dict)

    print("clear embedding list...")
    embedding_matrix, embedding_word_dict = clear_embedding_list(word_embeding, words_dict, sp_bad_words)

    embedding_word_dict[UNKNOWN_WORD] = len(embedding_word_dict)
    embedding_matrix = np.vstack([embedding_matrix, [0.] * embedding_size])
    embedding_word_dict[END_WORD] = len(embedding_word_dict)
    embedding_matrix = np.vstack([embedding_matrix, [-1.] * embedding_size])

    id_to_word = dict((id, word) for word, id in words_dict.items())
    print("convert train...")
    train_list_of_token_ids = convert_tokens_to_ids(
        tokenized_sentences_train,
        id_to_word,
        embedding_word_dict,
        maxlen)
    print("convert test...")
    test_list_of_token_ids = convert_tokens_to_ids(
        tokenized_sentences_test,
        id_to_word,
        embedding_word_dict,
        maxlen)
    print("Create datasets...")
    x_train = np.array(train_list_of_token_ids)
    y_train = train[label_cols].values
    x_test = np.array(test_list_of_token_ids)

    return x_train, y_train, x_test, embedding_matrix, embedding_word_dict


text:
"""
Isotropic Gaussian blobs
"""

from collections import OrderedDict
import logging

import numpy as np
from abc import ABCMeta, abstractmethod
from ..utils.errors import ProgressiveError, ProgressiveStopIteration
from progressivis import ProgressiveError, SlotDescriptor
from ..table.module import TableModule
from ..table.table import Table
from ..table.constant import Constant
from ..utils.psdict import PsDict
from ..core.utils import integer_types
from sklearn.datasets import make_blobs
from sklearn.utils import shuffle as multi_shuffle

logger = logging.getLogger(__name__)

RESERVOIR_SIZE = 10000

def make_mv_blobs(means, covs, n_samples, **kwds):
    assert len(means) == len(covs)
    n_blobs = len(means)
    size = n_samples // n_blobs
    blobs = []
    labels = []
    for i, (mean, cov) in enumerate(zip(means, covs)):
        blobs.append(np.random.multivariate_normal(mean, cov, size, **kwds))
        arr = np.empty(size, dtype='int64')
        arr[:] = i
        labels.append(arr)
    blobs = np.concatenate(blobs)
    labels = np.concatenate(labels)
    return multi_shuffle(blobs, labels)

def xy_to_dict(x, y, i, size, cols):
    res = {}
    k = None if size is None else i + size
    for j, col in enumerate(cols):
        res[col] = x[i:,j] if k is None else x[i:k,j]
    labs = y[i:] if k is None else y[i:k]
    return res, labs

class BlobsTableABC(TableModule):
    """Isotropic Gaussian blobs => table
    The purpose of the "reservoir" approach is to ensure the reproducibility of the results
    """
    outputs = [SlotDescriptor('labels', type=Table, required=False)]
    kw_fun = None
    def __init__(self, columns, rows=-1, dtype='float64', seed=0, throttle=False, **kwds):
        super().__init__(**kwds)
        self._kwds = {} #self._filter_kwds(kwds, self.kw_fun)
        """assert 'centers' in self._kwds
        assert 'n_samples' not in self._kwds
        assert 'n_features' not in self._kwds
        assert 'random_state' not in self._kwds"""
        #self._kwds['n_samples'] = rows
        #self._kwds['n_features']
        self.default_step_size = 1000
        if isinstance(columns, integer_types):
            self.columns = ["_%d"%i for i in range(1, columns+1)]
            #self._kwds['n_features'] = columns
        elif isinstance(columns, (list, np.ndarray)):
            self.columns = columns
            #self._kwds['n_features'] = len(columns)
        else:
            raise ProgressiveError('Invalid type for columns')
        self.rows = rows
        self.seed = seed
        self._reservoir = None
        self._labels = None
        self._reservoir_idx = 0
        if throttle and isinstance(throttle, integer_types+(float,)):
            self.throttle = throttle
        else:
            self.throttle = False
        dshape = ", ".join([f"{col}: {dtype}" for col in self.columns])
        dshape = "{" + dshape + "}"
        self.result = Table(self.generate_table_name('table'),
                            dshape=dshape,
                            create=True)
        self.columns = self.result.columns

    def is_source(self):
        return True

    def starting(self):
        super().starting()
        opt_slot = self.get_output_slot('labels')
        if opt_slot:
            logger.debug('Maintaining labels')
            self.maintain_labels(True)
        else:
            logger.debug('Not maintaining labels')
            self.maintain_labels(False)

    def maintain_labels(self, yes=True):
        if yes and self._labels is None:
            self._labels = Table(self.generate_table_name('blobs_labels'),
                                 dshape="{labels: int64}",
                                 create=True)
        elif not yes:
            self._labels = None

    def labels(self):
        return self._labels

    def get_data(self, name):
        if name == 'labels':
            return self.labels()
        return super().get_data(name)

    @abstractmethod
    def fill_reservoir(self):
        pass

    def run_step(self, run_number, step_size, howlong):
        if step_size == 0:
            logger.error('Received a step_size of 0')
            return self._return_run_step(self.state_ready, steps_run=0)
        logger.info('generating %d lines', step_size)
        if self.throttle:
            step_size = np.min([self.throttle, step_size])
        if self.rows >= 0 and (len(self.result)+step_size) > self.rows:
            step_size = self.rows - len(self.result)
            logger.info('truncating to %d lines', step_size)
            if step_size <= 0:
                raise ProgressiveStopIteration
        if self._reservoir is None:
            self.fill_reservoir()
        steps = step_size
        while steps>0:
            level = len(self._reservoir[0]) - self._reservoir_idx
            assert level >=0
            if steps >= level:
                blobs_dict, y_ = xy_to_dict(*self._reservoir, self._reservoir_idx, None, self.columns)
                steps -= level
                # reservoir was emptied so:
                self.fill_reservoir()
            else: # steps < level
                blobs_dict, y_ = xy_to_dict(*self._reservoir, self._reservoir_idx, steps, self.columns)
                self._reservoir_idx += steps
                steps = 0
            self.result.append(blobs_dict)
            if self._labels is not None:
                self._labels.append({'labels': y_})
        if len(self.result) == self.rows:
            next_state = self.state_zombie
        elif self.throttle:
            next_state = self.state_blocked
        else:
            next_state = self.state_ready
        return self._return_run_step(next_state, steps_run=step_size)

class BlobsTable(BlobsTableABC):
    kw_fun = make_blobs
    def __init__(self, *args, **kwds):
        #import pdb;pdb.set_trace()
        super().__init__(*args, **kwds)
        #assert 'centers' in self._kwds
        self.centers =  kwds['centers']
        assert 'n_samples' not in self._kwds
        assert 'n_features' not in self._kwds
        assert 'random_state' not in self._kwds

    def fill_reservoir(self):
        X, y = make_blobs(n_samples=RESERVOIR_SIZE, random_state=self.seed, centers=self.centers, **self._kwds)
        self.seed += 1
        self._reservoir = (X, y)
        self._reservoir_idx = 0

class MVBlobsTable(BlobsTableABC):
    kw_fun = make_mv_blobs
    def __init__(self, *args, **kwds):
        super().__init__(*args, **kwds)
        self.means = kwds['means']
        self.covs = kwds['covs']

    def fill_reservoir(self):
        np.random.seed(self.seed)
        X, y = make_mv_blobs(n_samples=RESERVOIR_SIZE, means=self.means, covs=self.covs, **self._kwds)
        self.seed += 1
        self._reservoir = (X, y)
        self._reservoir_idx = 0

max_stars_repo_name: jdfekete/progressivis

max_issues_repo_name: jdfekete/progressivis

max_forks_repo_name: jdfekete/progressivis


text:
import numpy as np
import globals
from collections import defaultdict
def freq_element(graph):
    #print("subset graph {}".format(graph))
    l = np.zeros(globals.no_of_labels,dtype=int)
    for nodes in graph:
        for neighbour,key in graph[nodes]:
            l[key-1]+=1
    return (l//2)
def dict_create(graph):
    dict = defaultdict(int)
    for nodes in graph:
        dict[nodes]=0
    return dict

#print(freq_element(globals.graph))

max_stars_repo_name: DilipIITBHU/MLST-IMPLEMENTATION

max_issues_repo_name: DilipIITBHU/MLST-IMPLEMENTATION

max_forks_repo_name: DilipIITBHU/MLST-IMPLEMENTATION


text:
#Importing libraries
import numpy as np
import matplotlib.pyplot as plt
import cv2 as cv
from PIL import Image
from skimage import io, filters,feature
from skimage.measure import find_contours, approximate_polygon, \
    subdivide_polygon

#Funtion to retrieve the indices of contours between the selected input points.
#Even number of points are selected on the plot using gplot and then using a simple for and if loop
#all the contours within the box created by two successive points.
def contour_idx(contours, x):

	"""
	As we want to use selected contours for generating the mesh, contour_idx extracts the
	indices of all those contours that lie within a box created by successive points in the
	set x.

	Parameters
	----------
	contours : list
			list of contours, where one contour is an array of size (n,2)
	x : array
			array containing coordinates of points used in creating boxes to search for contours within

	Returns
	-------
	out: list
		list containing the indices of selected contours

	"""

	x_num = int(len(x)/2) #half the number of points in the list x
	contour_index = [] #Creating empty list to store indices of the boxed-in contours
	for k  in range(len(contours)):
		c = contours[k][:,0,:] #Extracting all the points in a contour
		nk = len(c) #number of points in a given contour
		for i in range(nk):
		    ckij = c[i] #i^th point in the k^th contour
		    for p in range(0,x_num):
			    if x[2*p][0] < ckij[0] and ckij[0] < x[2*p+1][0]:#Checking if the x-coordinate of the point is within the selected point
			        if x[2*p][1] > ckij[1] and ckij[1] > x[2*p+1][1]: #Checking if the y-coordinate of the point is within the selected point
			            contour_index.append(k) #appending the contour index k if one of the points of the contour lies within the box
			    else:
			        continue
	return contour_index

def contour_selc(img_file, method, num_points, sigma = 2, threshold = 0, max_val = 255):

	"""
	To be able to contorl the mesh refinement in different regions of an image we need to identify different
	regions in the image. The function offers two different contouring, watershed seperation or canny contouring
	from OpenCV.

	Parameters
	----------

	img_file	: jpg or png file
				Image file that is to be used to create a mesh
	method		: str, 'watershed' or 'canny'
	num_points	: int, number of points (even) to be used for selecting contours
	sigma		: float, default = 2
				The level of details wanted in the canny edge detection algorithm
	threshold	: int, default = 0
				The minimum threshold grayscale value used in creating a binary image file
	max_val 	: int, default = 255
				maximum value of grayscale value to be used for creating a binary file

	Returns
	-------
	out:
		contour_index	: list,
						indices of the contours selected
		contours 		: list of array,
						list of all the contours (as numpy array) in the image according to method
		hierarchy		: numpy array,
						hierarchy tree of the different contours
		dim 			: list,
						dimensions of the image

	"""

	if method == 'watershed':
		img = cv.imread(img_file)
		b,g,r = cv.split(img)
		rgb_img = cv.merge([r,g,b])

		gray = cv.cvtColor(img,cv.COLOR_BGR2GRAY)
		ret, thresh = cv.threshold(gray,0,255,cv.THRESH_BINARY_INV+cv.THRESH_OTSU)

		# noise removal
		kernel = np.ones((2,2),np.uint8)
		#opening = cv.morphologyEx(thresh,cv.MORPH_OPEN,kernel, iterations = 2)
		closing = cv.morphologyEx(thresh,cv.MORPH_CLOSE,kernel, iterations = 2)

		# sure background area
		sure_bg = cv.dilate(closing,kernel,iterations=3)

		# Finding sure foreground area
		dist_transform = cv.distanceTransform(sure_bg,cv.DIST_L2,3)

		# Threshold
		ret, sure_fg = cv.threshold(dist_transform,0.1*dist_transform.max(),255,0)

		# Finding unknown region
		sure_fg = np.uint8(sure_fg)
		unknown = cv.subtract(sure_bg,sure_fg)

		#dimenson of the image
		dim = np.shape(unknown)

		_, binary = cv.threshold(unknown, threshold, max_val, cv.THRESH_BINARY_INV)
		#plt.imshow(binary, cmap="gray")
		binary = np.uint8(binary)

		#image = cv.imread("G:\\HiWi\\SLphantom.jpg")

		contours, hierarchy = cv.findContours(binary, cv.RETR_TREE, cv.CHAIN_APPROX_SIMPLE)

		plt.figure(figsize=(10,10))

		plt.imshow(binary)
		#n = 2*input('Enter the number of points to select the contours to include',)
		x = plt.ginput(num_points)

		#list containing indices of the contours between the selected points
		contour_index = contour_idx(contours, x)

	if method == 'canny':
		img = Image.open(img_file)
		#img = Image.open('G:\\HiWi\\SLphantom.png')
		imgarray = img.convert('LA')
		imgmat = np.array(imgarray.getdata(band=0))
		imgmat.shape = (imgarray.size[1],imgarray.size[0])
		imgmat_norm = imgmat/255

		edges = feature.canny(imgmat_norm,sigma = sigma)
		m,n = np.shape(edges)
		edges_num = np.zeros((m,n))
		for i in range(m):
		    for j in range(n):
		        if edges[i,j] == True:
		            edges_num[i,j] = 1
		            edges_num[i-1,j] = 1

		#dimenson of the image
		dim = np.shape(edges_num)

		_, binary = cv.threshold(edges_num, threshold, max_val, cv.THRESH_BINARY_INV)
		#plt.imshow(binary, cmap="gray")
		binary = np.uint8(binary)

		image = cv.imread(img_file)
		#image = cv.imread("G:\\HiWi\\SLphantom.jpg")

		contours, hierarchy = cv.findContours(binary, cv.RETR_TREE, cv.CHAIN_APPROX_SIMPLE)

		plt.figure(figsize=(10,10))
		image = cv.drawContours(image, contours, -1, (0,255, 0), 2)

		plt.imshow(binary)
		#n = 2*input('Enter the number of points to select the contours to include',)
		x = plt.ginput(num_points)

		contour_index = contour_idx(contours, x)

	return np.unique(contour_index),contours, hierarchy, dim

# Function to create an list for each contour containing indices of the
# contours contained within it. For example, if we have a four circles indexed 1,2,3 and 4
# such that 1 contains 2, 3 and 4. circles 2 and 3 exclusive and 2 contains circle 4 within it.
# Then the function returns [[2,3,4],[4],[],[]] where the last two lists are empty as circles 3 and 4 do not
# contain any other circle within it. We can also interpret these relations as a tree.

def contour_tree(hierarchy):

	"""
	The function takes a hierarchy tee as an input and returns, for each contour, the list of contours that are
	contained in it.

	Parameters
	----------

	hierarchy	: array,
				An array containing the (next node, previous node, parent node, child node) information for each
				node

	Return
	------

	out: list
		a list containing the list of contours contained within a particular contour

	"""

	hirchy = hierarchy[0,:,:]
	m,n = np.shape(hirchy)
	holes_list = [[] for k in range(m)]
	for i in range(m):
		h = hirchy[i,:]
		if h[0] == -1 and h[1] == -1:
			holes_list[i].append(h[3])
			holes_list[h[2]].append(i)
		else:
			if h[3] == -1:
				if h[2] == -1:
					if h[1] != -1:
						holes_list[i].append(h[1])
						holes_list[h[1]].append(i)
				else:
					holes_list[i].append(h[2])
			else:
				holes_list[h[3]].append(i)
	return holes_list

max_stars_repo_name: CSMMLab/KiT-RT

max_issues_repo_name: CSMMLab/KiT-RT

max_forks_repo_name: CSMMLab/KiT-RT


text:
import pyvista as pv
import numpy as np

pv.set_plot_theme("document")

p = pv.Plotter(shape=(1, 2), window_size=[1000, 300])

p.subplot(0, 0)
p.add_text("Line")
line = pv.Line((0, 0, 0), (10, 0, 0))
p.add_mesh(line, color="k", line_width=5)

p.subplot(0, 1)
p.add_text("Tube")
tube = pv.Tube((0, 0, 0), (10, 0, 0))
p.add_mesh(tube, show_edges=True)

p.show(cpos="xz", screenshot="tube.png")


text:
# Copyright (c) 2015-2020 by the parties listed in the AUTHORS file.
# All rights reserved.  Use of this source code is governed by
# a BSD-style license that can be found in the LICENSE file.

import sys

import numpy as np

import ctypes

from .mpi import MPITestCase

from ..cache import Cache

from ..utils import AlignedF64, memreport

class CacheTest(MPITestCase):
    def setUp(self):
        self.nsamp = 1000
        self.membytes = 100000000
        self.memnbuf = 100
        self.cache = Cache(pymem=False)
        self.pycache = Cache(pymem=True)
        self.types = {
            "f64": np.float64,
            "f32": np.float32,
            "i64": np.int64,
            "u64": np.uint64,
            "i32": np.int32,
            "u32": np.uint32,
            "i16": np.int16,
            "u16": np.uint16,
            "i8": np.int8,
            "u8": np.uint8,
        }

    def tearDown(self):
        del self.cache
        del self.pycache

    def test_dict_interface(self):
        cache = Cache()
        cache["aaa"] = np.arange(10)
        cache["ccc"] = np.arange(10) + 10
        self.assertTrue("aaa" in cache)
        self.assertFalse("bbb" in cache)
        self.assertEqual(len(cache), 2)
        n = 0
        for x in cache:
            n += 1
        self.assertEqual(len(cache), n)
        del cache["aaa"]
        self.assertEqual(len(cache), n - 1)
        return

    def test_refcount(self):
        buf = AlignedF64.zeros(self.nsamp)
        # print("REFCNT: ", sys.getrefcount(buf), flush=True)
        # wbuf1 = buf.weak()
        # print("REFCNT 1: ", sys.getrefcount(buf), flush=True)
        # wbuf2 = buf.weak()
        # print("REFCNT 2: ", sys.getrefcount(buf), flush=True)

        return

    def test_create(self):
        for k, v in self.types.items():
            ref = self.cache.create("test-{}".format(k), v, (self.nsamp, 4))
            del ref

        for k, v in self.types.items():
            data = self.cache.reference("test-{}".format(k))
            data[:] += np.repeat(np.arange(self.nsamp, dtype=v), 4).reshape(-1, 4)
            del data

        for k, v in self.types.items():
            ex = self.cache.exists("test-{}".format(k))
            self.assertTrue(ex)

        for k, v in self.types.items():
            self.cache.destroy("test-{}".format(k))

        self.cache.clear()

        for k, v in self.types.items():
            ref = self.pycache.create("test-{}".format(k), v, (self.nsamp, 4))
            del ref

        for k, v in self.types.items():
            data = self.pycache.reference("test-{}".format(k))
            data[:] += np.repeat(np.arange(self.nsamp, dtype=v), 4).reshape(-1, 4)
            del data

        for k, v in self.types.items():
            ex = self.pycache.exists("test-{}".format(k))
            self.assertTrue(ex)

        for k, v in self.types.items():
            self.pycache.destroy("test-{}".format(k))

        self.pycache.clear()
        return

    def test_put(self):
        # Populate some buffers
        for k, v in self.types.items():
            pname = "test-{}".format(k)
            pdata = np.ones((self.nsamp, 4), v)
            ref = self.cache.put(pname, pdata)
            del ref

        # Test putting the same buffer- it should just return a new array
        # wrapping the same underlying memory.
        for k, v in self.types.items():
            pname = "test-{}".format(k)
            pdata = np.ones((self.nsamp, 4), v)
            ref = self.cache.reference(pname)
            copyref = self.cache.put(pname, ref)
            ref_pnt = ref.ctypes.data_as(ctypes.c_void_p).value
            copyref_pnt = copyref.ctypes.data_as(ctypes.c_void_p).value
            self.assertTrue(ref_pnt == copyref_pnt)
            del ref
            del copyref

        # Test replacement of buffers with the same name
        for k, v in self.types.items():
            pname = "test-{}".format(k)
            pdata = np.ones((self.nsamp, 8), v)
            try:
                # This should raise- same name as existing buffer, but
                # replace == False
                ref = self.cache.put(pname, pdata)
                del ref
                self.assertTrue(False)
            except RuntimeError:
                # Success!
                pass
            # This should work
            ref = self.cache.put(pname, pdata, replace=True)
            del ref

        self.cache.clear()

        # Populate some buffers
        for k, v in self.types.items():
            pname = "test-{}".format(k)
            pdata = np.ones((self.nsamp, 4), v)
            ref = self.pycache.put(pname, pdata)
            del ref

        # Test putting the same buffer- it should just return a new array
        # wrapping the same underlying memory.
        for k, v in self.types.items():
            pname = "test-{}".format(k)
            pdata = np.ones((self.nsamp, 4), v)
            ref = self.pycache.reference(pname)
            copyref = self.pycache.put(pname, ref)
            ref_pnt = ref.ctypes.data_as(ctypes.c_void_p).value
            copyref_pnt = copyref.ctypes.data_as(ctypes.c_void_p).value
            self.assertTrue(ref_pnt == copyref_pnt)
            del ref
            del copyref

        # Test replacement of buffers with the same name
        for k, v in self.types.items():
            pname = "test-{}".format(k)
            pdata = np.ones((self.nsamp, 8), v)
            try:
                # This should raise- same name as existing buffer, but
                # replace == False
                ref = self.pycache.put(pname, pdata)
                del ref
                self.assertTrue(False)
            except RuntimeError:
                # Success!
                pass
            # This should work
            ref = self.pycache.put(pname, pdata, replace=True)
            del ref

        self.pycache.clear()
        return

    def test_create_none(self):
        try:
            ref = self.cache.create(None, np.float, (1, 10))
            del ref
            ref = self.pycache.create(None, np.float, (1, 10))
            del ref
            raise RuntimeError("Creating object with None key succeeded")
        except ValueError:
            pass

        self.cache.clear()
        self.pycache.clear()
        return

    def test_put_none(self):
        try:
            ref = self.cache.put(None, np.float, np.arange(10))
            del ref
            ref = self.pycache.put(None, np.float, np.arange(10))
            del ref
            raise RuntimeError("Putting an object with None key succeeded")
        except ValueError:
            pass

        self.cache.clear()
        self.pycache.clear()
        return

    def test_clear(self):
        for k, v in self.types.items():
            ref = self.cache.create("test-{}".format(k), v, (self.nsamp, 4))
            del ref
            ref = self.pycache.create("test-{}".format(k), v, (self.nsamp, 4))
            del ref
        self.cache.clear()
        self.pycache.clear()
        return

    def test_alias(self):
        ref = self.cache.put("test", np.arange(10))
        del ref

        self.cache.add_alias("test-alias", "test")
        self.cache.add_alias("test-alias-2", "test")

        data = self.cache.reference("test-alias")
        del data

        self.cache.destroy("test-alias")

        data = self.cache.reference("test-alias-2")
        del data

        self.cache.destroy("test")

        ex = self.cache.exists("test-alias-2")
        self.assertFalse(ex)

        self.cache.clear()

        ref = self.pycache.put("test", np.arange(10))
        del ref

        self.pycache.add_alias("test-alias", "test")
        self.pycache.add_alias("test-alias-2", "test")

        data = self.pycache.reference("test-alias")
        del data

        self.pycache.destroy("test-alias")

        data = self.pycache.reference("test-alias-2")
        del data

        self.pycache.destroy("test")

        ex = self.pycache.exists("test-alias-2")
        self.assertFalse(ex)

        self.pycache.clear()
        return

    def test_memfree(self):
        memreport(comm=self.comm, msg="Before large buffer creation")
        memcache = Cache(pymem=False)
        ref = memcache.create("test-big", np.uint8, (self.membytes,))
        del ref

        memreport(comm=self.comm, msg="After large buffer creation")

        mem = memcache.report(silent=True)
        self.assertEqual(mem, self.membytes)
        print("Cache now has {} bytes".format(mem), flush=True)

        memcache.clear()

        memreport(comm=self.comm, msg="After cache clear")
        mem = memcache.report(silent=True)
        self.assertEqual(mem, 0)
        print("Cache now has {} bytes".format(mem), flush=True)

        smallbytes = self.membytes // self.memnbuf
        for i in range(self.memnbuf):
            name = "test-small_{}".format(i)
            ref = memcache.create(name, np.uint8, (smallbytes,))
            del ref

        memreport(
            comm=self.comm,
            msg="After creation of {} small buffers".format(self.memnbuf),
        )
        mem = memcache.report(silent=True)
        self.assertEqual(mem, self.memnbuf * smallbytes)
        print("Cache now has {} bytes".format(mem), flush=True)

        memcache.clear()
        memreport(comm=self.comm, msg="After cache clear")
        mem = memcache.report(silent=True)
        self.assertEqual(mem, 0)
        print("Cache now has {} bytes".format(mem), flush=True)

max_stars_repo_name: doicbek/toast

max_issues_repo_name: doicbek/toast

max_forks_repo_name: doicbek/toast