<commit_before>#!flask/bin/python
from flask import Flask, jsonify
import pymongo
from shapely.geometry import MultiPoint
from bson import ObjectId
from flask import make_response
import pandas as pd
from sklearn import cluster as clus
import datetime
import numpy as np
import copy
import import_clustering

app = Flask(__name__)
client = pymongo.MongoClient("localhost", 3001)
db = client.meteor

# This route will generate the full cluster info based on a cluster stub that
# contains only a list of photos to be included in that cluster
@app.route('/generate/<string:cluster_id>', methods=['PUT'])
def generate_cluster(cluster_id):
    clusters = list(db.clusters.find({'_id': ObjectId(cluster_id)}));

    if (len(clusters) > 1):
        return jsonify({'error': 'too many clusters'}), 500

    if (len(clusters) == 0):
        return jsonify({'error': 'no cluster with id'}), 500

    cluster = clusters[0]

    cluster_details = import_clustering.make_cluster_details(cluster['photos'], db=db)

    db.clusters.update_one({'_id': ObjectId(cluster_id)}, {'$set': cluster_details})

    return jsonify({'response': 'okay'})

@app.route('/update/<string:cluster_id>', methods=['PUT'])
def update_cluster(cluster_id):
    print(cluster_id)
    clusters = list(db.clusters.find({'_id': ObjectId(cluster_id)}));

    if (len(clusters) > 1):
        return jsonify({'error': 'too many clusters'}), 500

    if (len(clusters) == 0):
        return jsonify({'error': 'no cluster with id'}), 500

    cluster = clusters[0]

    points = MultiPoint(cluster['locations']['coordinates'])
    geom = {}
    geom['type'] = 'Polygon'
    
    try:
        p = points.convex_hull
        p = p.buffer(np.sqrt(p.area) * 0.33)
        geom['coordinates'] = list(p.simplify(0.0005).exterior.coords)
    except:
        p = points.buffer(0.005)
        p = p.convex_hull
        geom['coordinates'] = list(p.simplify(0.0005).exterior.coords)
    
    centroid = {'type': 'Point'}
    centroid['coordinates'] = list(p.centroid.coords)[0]
    
    print(centroid)

    db.clusters.update_one({'_id': cluster['_id']}, {'$set': {'boundary': geom, 'centroid': centroid}})

    return jsonify({'response': 'okay'})

@app.route('/split/<string:cluster_id>', methods=['GET'])
def split_cluster(cluster_id):
    print("splitting cluster")
    print(cluster_id)

    clusters = list(db.clusters.find({'_id': ObjectId(cluster_id)}))

    if (len(clusters) > 1):
        return jsonify({'error': 'too many clusters'}), 500

    if (len(clusters) == 0):
        return jsonify({'error': 'no cluster with id'}), 500

    cluster = clusters[0]

    # now do a k-means split with k=2 on this cluster<commit_msg>Fix drag and drop cluster merging<commit_after>#!flask/bin/python
from flask import Flask, jsonify
import pymongo
from shapely.geometry import MultiPoint
from bson import ObjectId
from flask import make_response
import pandas as pd
from sklearn import cluster as clus
import datetime
import numpy as np
import copy
import import_clustering

app = Flask(__name__)
client = pymongo.MongoClient("localhost", 3001)
db = client.meteor

# This route will generate the full cluster info based on a cluster stub that
# contains only a list of photos to be included in that cluster
@app.route('/generate/<string:cluster_id>', methods=['PUT'])
def generate_cluster(cluster_id):
    clusters = list(db.clusters.find({'_id': ObjectId(cluster_id)}));

    if (len(clusters) > 1):
        return jsonify({'error': 'too many clusters'}), 500

    if (len(clusters) == 0):
        return jsonify({'error': 'no cluster with id'}), 500

    cluster = clusters[0]

    cluster_details = import_clustering.make_cluster_details(cluster['photos'], db=db)

    db.clusters.update_one({'_id': ObjectId(cluster_id)}, {'$set': cluster_details})

    return jsonify({'response': 'okay'})

@app.route('/split/<string:cluster_id>', methods=['GET'])
def split_cluster(cluster_id):
    print("splitting cluster")
    print(cluster_id)

    clusters = list(db.clusters.find({'_id': ObjectId(cluster_id)}))

    if (len(clusters) > 1):
        return jsonify({'error': 'too many clusters'}), 500

    if (len(clusters) == 0):
        return jsonify({'error': 'no cluster with id'}), 500

    cluster = clusters[0]

    # now do a k-means split with k=2 on this cluster
{'fraction_non_alphanumeric': 0.14365152919369786, 'fraction_numerical': 0.016913809082483782, 'mean_word_length': 4.182472989195678, 'pattern_counts': {'":': 0, '<': 8, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5636117', 'n_tokens_mistral': 1460, 'n_tokens_neox': 1359, 'n_words': 373}