def group_generation(nbGroupes: int, n: int, offset: float = .2) ->list: """ Generates points in distincts groups Same number of points per group Points's coordinates between 0 and 1 """ angle = 0 centroidsX = [] centroidsY = [] while(angle < 2*math.pi): # Generate groups's centers centroidsX.append(math.cos(angle)) centroidsY.append(math.sin(angle)) angle += (2 * math.pi) / nbGroupes points = [] cl = color_generation(nbGroupes) for i in range(nbGroupes): # Generate points for each group points.extend([ norm(uniform(centroidsX[i] - offset, centroidsX[i] + offset), -1 - offset, 1 + offset), norm(uniform(centroidsY[i] - offset, centroidsY[i] + offset), -1 - offset, 1 + offset), cl[i] ] for j in range(n)) return points
def percent_generation(percentages: list, n: int, offset: float = .2) ->list: """ Generates points in distincts group Variable amount of points per group determined by the percentages parameter Points's coordinates between 0 and 1 """ # Check if the percentages are correct if(sum(percentages) > 1 or sum(percentages) <= 0): raise ValueError("The given percentages aren't correct, sum > 1 or sum <= 0") nbGroupes = len(percentages) cl = color_generation(nbGroupes) # DEBUG : print(percentages) # Conversion : from percents to number of points for i in range(nbGroupes): percentages[i] = int(n * percentages[i]) # Number of points lost because of the divisions lostpoints = n - sum(percentages) print("%s points haven't been placed" % str(lostpoints)) # DEBUG : print(percentages) # Groups positions angle = 0 centroidsX = [] centroidsY = [] while(angle < 2*math.pi): # Generate groups's centers centroidsX.append(math.cos(angle)) centroidsY.append(math.sin(angle)) angle += (2 * math.pi) / nbGroupes # Points generation points = [] for i in range(nbGroupes): # Generate the amount of points specified by the percentages list points.extend([ norm(uniform(centroidsX[i] - offset, centroidsX[i] + offset), -1 - offset, 1 + offset), norm(uniform(centroidsY[i] - offset, centroidsY[i] + offset), -1 - offset, 1 + offset), cl[i] ] for j in range(percentages[i])) return points, lostpoints
def read_file(docfile): """ Read a csv file used as a dataset transform the csv to points """ if(docfile): dest = open('/tmp/'+docfile.name, 'wb+') for chunk in docfile.chunks(): dest.write(chunk) dest.close() with open('/tmp/'+docfile.name) as f: couples = str(f.readlines()) couples = couples.split(';') # Generates points from the csv points = [] x = []; y = []; cl = [] for c in couples: try: c = c.split(',') tmpx = float(c[0]) tmpy = float(c[1]) tmpcl = c[2] if(not re.match(r'^#([0-9]|[A-Fa-f]){6}$', tmpcl)): # checks that the colors is well formatted raise Exception x.append(tmpx) y.append(tmpy) cl.append(tmpcl) # remove the ' added by the split() except Exception: pass # If anything happen we just don't take the point # raise ValueError # points normalization xmin = min(x); xmax = max(x) ymin = min(y); ymax = max(y) for i in range(len(x)): points.append([norm(x[i], xmin, xmax), norm(y[i], ymin, ymax), cl[i]]) # raise ValueError return points