union find with partition clustering
This commit is contained in:
@@ -69,7 +69,6 @@ tx_grouped = tx_df \
|
|||||||
.groupBy('tx_id') \
|
.groupBy('tx_id') \
|
||||||
.agg(F.collect_set('address').alias('addresses'))
|
.agg(F.collect_set('address').alias('addresses'))
|
||||||
|
|
||||||
tx_grouped.rdd.mapPartitions(cluster_id_addresses_rows)
|
|
||||||
|
|
||||||
# TODO: Load clusters from DB, check if any exist, if no make initial cluster, else proceed with loaded data
|
# TODO: Load clusters from DB, check if any exist, if no make initial cluster, else proceed with loaded data
|
||||||
|
|
||||||
|
|||||||
@@ -48,21 +48,19 @@ def cluster_step(clusters: "List[List[str]]", addresses: "List[List[str]]"):
|
|||||||
if(len(addresses) == 0):
|
if(len(addresses) == 0):
|
||||||
return clusters
|
return clusters
|
||||||
|
|
||||||
#take a set of addresses
|
|
||||||
tx = addresses[0]
|
tx = addresses[0]
|
||||||
#remove it from list candidates
|
matching_clusters = []
|
||||||
addresses = addresses[1:]
|
new_clusters = []
|
||||||
|
|
||||||
#find clusters that match these addresses
|
for cluster in clusters:
|
||||||
matching_clusters = filter(lambda cluster: check_lists_overlap(tx, cluster), clusters)
|
if(check_lists_overlap(tx, cluster)):
|
||||||
|
matching_clusters.append(cluster)
|
||||||
#remove all clusters that match these addresses
|
else:
|
||||||
clusters = list(filter(lambda cluster: not check_lists_overlap(tx, cluster), clusters))
|
new_clusters.append(cluster)
|
||||||
|
|
||||||
#add a new cluster that is the union of found clusters and the inspected list of addresses
|
new_clusters.append(merge_lists_distinct(tx, *matching_clusters))
|
||||||
clusters.append(merge_lists_distinct(tx, *matching_clusters))
|
|
||||||
|
|
||||||
return cluster_step(clusters,addresses)
|
return cluster_step(new_clusters,addresses[1:])
|
||||||
|
|
||||||
def cluster_partition(iter: "Iterable[Row]") -> Iterable:
|
def cluster_partition(iter: "Iterable[Row]") -> Iterable:
|
||||||
yield cluster_step([], list(map(lambda row: row['addresses'], iter)))
|
yield cluster_step([], list(map(lambda row: row['addresses'], iter)))
|
||||||
|
|||||||
Reference in New Issue
Block a user