예제 #1
0
def angel(g_original, threshold, min_community_size=3):
    """
    Angel is a node-centric bottom-up community discovery algorithm.
    It leverages ego-network structures and overlapping label propagation to identify micro-scale communities that are subsequently merged in mesoscale ones.
    Angel is the, faster, successor of Demon.

    :param g_original: a networkx/igraph object
    :param threshold: merging threshold in [0,1].
    :param min_community_size: minimum community size, default 3.
    :return: NodeClustering object

    :Example:

    >>> from cdlib import algorithms
    >>> import networkx as nx
    >>> G = nx.karate_club_graph()
    >>> coms = algorithms.angel(G, min_com_size=3, threshold=0.25)

    :References:

    1. Rossetti, Giulio. "Exorcising the Demon: Angel, Efficient Node-Centric Community Discovery." International Conference on Complex Networks and Their Applications. Springer, Cham, 2019.

    .. note:: Reference implementation: https://github.com/GiulioRossetti/ANGEL
    """
    if ig is None:
        raise ModuleNotFoundError(
            "Optional dependency not satisfied: install igraph to use the selected feature."
        )
    if Angel is None:
        raise ModuleNotFoundError(
            "Optional dependency not satisfied: install angel-cd library to use the selected feature (likely pip install angel-cd). If using a notebook, you need also to restart your runtime/kernel."
        )

    g = convert_graph_formats(g_original, ig.Graph)
    with suppress_stdout():
        a = Angel(graph=g,
                  min_comsize=min_community_size,
                  threshold=threshold,
                  save=False)
        coms = a.execute()

    return NodeClustering(list(coms.values()),
                          g_original,
                          "ANGEL",
                          method_parameters={
                              "threshold": threshold,
                              "min_community_size": min_community_size
                          },
                          overlap=True)
예제 #2
0
def angel(g, threshold, min_community_size=3):
    """
    Angel is a node-centric bottom-up community discovery algorithm.
    It leverages ego-network structures and overlapping label propagation to identify micro-scale communities that are subsequently merged in mesoscale ones.
    Angel is the, faster, successor of Demon.

    :param g: a networkx/igraph object
    :param threshold: merging threshold in [0,1].
    :param min_community_size: minimum community size, default 3.
    :return: NodeClustering object

    :Example:

    >>> from cdlib import algorithms
    >>> import networkx as nx
    >>> G = nx.karate_club_graph()
    >>> coms = algorithms.angel(G, min_com_size=3, threshold=0.25)

    :References:

    1. Rossetti G. **Angel: efficient, and effective, node-centric community discovery in static and dynamic networks.**

    .. note:: Reference implementation: https://github.com/GiulioRossetti/ANGEL
    """

    if ig is None:
        raise ModuleNotFoundError(
            "Optional dependency not satisfied: install igraph to use the selected feature."
        )

    g = convert_graph_formats(g, ig.Graph)
    with suppress_stdout():
        a = Angel(graph=g,
                  min_comsize=min_community_size,
                  threshold=threshold,
                  save=False)
        coms = a.execute()

    return NodeClustering(list(coms.values()),
                          g,
                          "ANGEL",
                          method_parameters={
                              "threshold": threshold,
                              "min_community_size": min_community_size
                          },
                          overlap=True)
예제 #3
0
	def __init__(self, player, end=False, good=False):
		"""
			The end paramter denotes whether or not this room is the end. 
			If it's the end, the player just needs to pass whatever scenario is here and then exit successfully
			If not the end, the room will spawn other connected rooms and continue
			If "good" is false, this room will have bad things in it
		"""

		import random
		from scenery import SceneDescriptors
		from monster import Monster
		from bestiary import Bestiary
		from angel import Angel
		from host import Host
		from people import People
		from player import Player

		self._description = SceneDescriptors.getScene(good)
		if good: 
			self._creature = Angel()
			print "The angel", self._creature.name, "appears before you. It is", self._creature.description, ". It", Host.getRequest()
			print "Your choices are: "
			resp = People.getResponses()
			for key, val in enumerate(resp):
				print key, ".", resp[val]

			print "Now, what will you do? Select a number"
			
			choice = ''
			invalid = True
			while invalid:
				choice = raw_input("-->")
				if int(choice) > len(resp)-1:
					print "That number is invalid! Try again"
				elif int(choice) < 0:
					print "That number isn't even a positive number! Try again"
				else:
					print "You selected", choice
					invalid = False

			choiceType = resp.keys()[int(choice)]
			choiceText = resp[choiceType]		# always prints only one from each good, bad, wierd list anyway
			typeMap = {
				'good': True, 
				'bad': False, 
				'weird': False
			}

			if typeMap[choiceType]:
				print "The angel bestows upon you", self._creature.pointsGiven, "health and", self._creature.disappears
				player.changeHealth(self._creature.pointsGiven)

			else: 
				print "The angel is annoyed. It smites you and takes away", self._creature.pointsGiven, "health"
				player.changeHealth(-1 * self._creature.pointsGiven)
				
			print "You now have", player.health, "health left"

		else: 
			self._creature = Monster()
			print "The monster", self._creature.name, "appears before you. It is", self._creature.description
			print "Your choices are: "
			resp = People.getDefenses()

			for key, val in enumerate(resp):
				print key, ".", resp[val]

			print "Now, how will you defend yourself? Select a number"
			
			choice = ''
			invalid = True
			while invalid:
				choice = raw_input("-->")
				if int(choice) > len(resp)-1:
					print "That number is invalid! Try again"
				elif int(choice) < 0:
					print "That number isn't even a positive number! Try again"
				else:
					print "You selected", choice
					invalid = False

			choiceType = resp.keys()[int(choice)]
			choiceText = resp[choiceType]		# always prints only one from each good, bad, wierd list anyway
			typeMap = {
				'good': True, 
				'bad': False, 
				'weird': False
			}

			if typeMap[choiceType]:
				print "The monster", self._creature.death

			else: 
				print "The monster", self._creature.attack, "and you lose", self._creature.pointsTaken
				player.changeHealth(self._creature.pointsTaken)
				
			print "You now have", player.health, "health left"
예제 #4
0
 def create_user():
     """
     Provides a user with a console interface to create a new user.
     :return: new user as User
     """
     print("Please enter your name:")
     user_name = input(">> ")
     while True:
         print("Please enter your age:")
         user_age = input(">> ")
         if user_age.isdigit():
             user_age = int(user_age)
             break
         else:
             print("Invalid age.")
     print("Please enter your Bank's name:")
     bank_name = input(">> ")
     print("Please enter your account number:")
     account_num = input(">> ")
     while True:
         print("Please enter your account balance:")
         balance = input(">> ")
         try:
             balance = float(balance)
             break
         except ValueError:
             print("Invalid balance")
     budget_list = []
     print("How many budgets would you like to create:")
     num_budgets = input(">> ")
     for i in range(int(num_budgets)):
         print("Please enter the budget name:")
         budget_name = input(">> ")
         while True:
             print("Please enter the budget limit:")
             limit = input(">> ")
             try:
                 budget_limit = float(limit)
                 break
             except ValueError:
                 print("Invalid limit.")
         budget_list.append(Budget(budget_name, float(budget_limit)))
     bank_info = BankInfo(bank_name, account_num, balance)
     while True:
         print("Please select your user type:")
         print("1: Angel")
         print("2: Troublemaker")
         print("3: Rebel")
         user_type = input(">> ")
         if user_type == "1":
             user = Angel(user_name, user_age, bank_info, budget_list)
             break
         elif user_type == "2":
             user = Troublemaker(user_name, user_age, bank_info,
                                 budget_list)
             break
         elif user_type == "3":
             user = Rebel(user_name, user_age, bank_info, budget_list)
             break
         print("Invalid selection")
     return user
예제 #5
0
def sigma_main():

    file_name = 'logs/log' + time.strftime("%b%d%H%M%S", time.localtime())
    logging.config.fileConfig('logging.conf',
                              defaults={'logfilename': file_name})
    logging.info("Reading yaml configuration file")
    # read configuration and do what is appropriate
    f = open('config.yaml')
    dataMap = yaml.safe_load(f)

    smokeping_node = dataMap['smokeping_node']
    user = dataMap['user']
    rdd_dir = dataMap['rdd_dir_path']
    domain_name = dataMap['domain_name']
    hosts = dataMap['hosts']
    services = dataMap['services']
    graphite_server = dataMap['graphite_server']
    f.close()

    # We assume that we have the training DB under DB directory so let's train classifiers and instantiate angels
    angels_dict = {}

    for host in hosts:
        for service in services:
            logging.info('Training classifier for ' + host + ' ' + service)
            logreg = train_classifier(host, service)
            angel = Angel(logreg, host, service)
            angels_dict[host + service] = angel

    # Now we can start the main loop because we have everything in place
    end_time = int(time.time())
    start_time = end_time - 60

    while True:

        logging.info('Main loop start-time and end-time ' + str(start_time) +
                     ' ' + str(end_time))
        dest_dir = 'tmp1'
        remote_file = ''

        for i in range(0, len(hosts)):
            remote_file = remote_file + hosts[i] + '.rrd'
            if (i != len(hosts) - 1):
                remote_file = remote_file + ','
        # get all rrd files of interest and store them locally
        dest_file = 'tmp1/'
        local('scp {}@{}:{} {}'.format(user, smokeping_node,
                                       rdd_dir + '/\{' + remote_file + '\}',
                                       dest_dir),
              capture=True)
        print ' '
        do_not_change_start_time = False

        # Collect realtime smokeping data
        service = 'smokeping_data'
        for host in hosts:
            remote_file = host + '.rrd'
            dest_file = 'tmp1/' + remote_file
            #data_file='tmp1/'+host+'.data'
            #local ('scp {}@{}:{} {}'.format(user,smokeping_node,rdd_dir+'/'+remote_file,dest_dir), capture=True)
            data_file = 'tmp1/' + host + '.data'
            local('rrdtool fetch {} AVERAGE --start {} --end {} > {}'.format(
                dest_file, start_time, end_time, data_file))
            logging.info('created data file ' + data_file)
            # create instance
            input_file = open(data_file, 'r')
            i = 1
            list_instances = []

            for line in input_file:
                if (':' in line):
                    line_list = line.split(' ')
                    time_stamp = int(line_list[0].strip(':'))
                    if time_stamp != (start_time + 60):
                        continue
                    #print line_list

                    # if no content in the packet loss section skip it most likely there is no data
                    if line_list[2] == '-nan':
                        # Ugly Hack: if we get here it means that we are processing start_time+60 but there is nothing yet in it
                        # set a flag so we do not change start_time and next iteration we process the same timestamp
                        # we should find something there then
                        do_not_change_start_time = True
                        continue
                    list_instances = create_instance(line_list, i, host,
                                                     list_instances)
                    do_not_change_start_time = False
                    #print list_instances

                    i = i + 1
            input_file.close()
            # send instance data to the Angel
            if list_instances:
                angels_dict[host + service].state_transition(list_instances[0])

        print "       "
        # Collect data from graphite now
        service = 'server_data'
        for host in hosts:
            #TEMP CODE ----- TEMP CODE
            # to deal with the fact that host names are different in graphite right now
            if (host != 'mario-ubuntu-01'):
                host_graphite = host + '-his-internal'
            else:
                host_graphite = host
            list_instances = graphite_instance(graphite_server, host_graphite)
            if list_instances:
                # send data to the associated angel
                angels_dict[host + service].state_transition(list_instances[0])

        logging.info('sleeping for 10 seconds')
        logging.info('     ')
        time.sleep(10)
        if do_not_change_start_time == False:
            start_time = calculate_start_time(data_file)
        end_time = int(time.time())