Skip to content

Commit 2245a87

Browse files
committed
initial commit
0 parents  commit 2245a87

28 files changed

Lines changed: 9695 additions & 0 deletions

graphBuild/run.py

Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
#!/usr/bin/env python3
2+
# encoding = utf-8
3+
import os
4+
import sys
5+
import numpy as np
6+
from sklearn.neighbors import KDTree
7+
import shutil
8+
import random
9+
import json
10+
import time
11+
12+
dirname = os.path.dirname(__file__)
13+
14+
sys.path.append(os.path.join(dirname, ".."))
15+
from thesne.sne_config import SNEConfig
16+
sys.path.append(os.path.join(dirname, "../dataGen/"))
17+
import dg_utils
18+
19+
20+
# read point sets and configuration from file.
21+
def ReadPointSets(filepath, norm_data):
22+
_points = []
23+
_labels = []
24+
25+
_dim = 0
26+
_pts_size = 0
27+
_flag = False
28+
29+
with open(filepath, 'r', encoding='utf-8') as f:
30+
lines = f.readlines()
31+
_pts_size = len(lines)
32+
33+
for line in lines:
34+
items = line.split()
35+
_point = []
36+
37+
for item in items[:-1]:
38+
_point.append(float(item))
39+
40+
if not _flag:
41+
_dim += 1
42+
_flag = True
43+
44+
_points.append(_point)
45+
# label
46+
_labels.append(items[-1])
47+
48+
p = np.array(_points)
49+
if norm_data == True:
50+
min_p = np.min(p)
51+
max_p = np.max(p)
52+
p = (p - min_p) / (max_p - min_p)
53+
54+
return p, np.array(_labels), _pts_size, _dim
55+
56+
57+
def sav_graph(filepath, points, labels, k_closest_count):
58+
tree = KDTree(points)
59+
dists, indices = tree.query(points,
60+
k=k_closest_count + 1) # 一口气对所有points构建knn
61+
62+
with open(filepath, 'w') as file: # 打开新文件fm_{0}.txt
63+
# first write in the number of nodes
64+
file.write(str(points.shape[0]) + "\n") # 写入当前点
65+
66+
for i in range(points.shape[0]):
67+
file.write(str(i) + "\t") # 写入当前点
68+
count = 0
69+
for t in range(indices[i].shape[0]):
70+
if indices[i][t] == i: # 是否包含自身
71+
continue
72+
if count == k_closest_count: # 只写入Indices中前k-1个点
73+
break
74+
# write incient point and corresponding distance
75+
file.write(str(indices[i][t]) + "\t" + str(dists[i][t]) + "\t")
76+
count += 1
77+
file.write('\n')
78+
79+
80+
if __name__ == '__main__':
81+
argv = sys.argv
82+
assert (len(argv) == 2)
83+
config_path = argv[1] #"../../config/config_0.json"
84+
85+
start = time.perf_counter()
86+
with open(config_path, 'r') as f:
87+
config_json = json.loads(f.read())
88+
config = SNEConfig(config_json)
89+
90+
input_dir = config.input_dir
91+
graph_path = config.graph_dir
92+
dg_utils.ClearDir(graph_path)
93+
94+
k_closest_count = config.k_closest_count #min(3*perplexity, pts_size) # K近邻的个数+1(虽然是K=4,但由于包含自身,实际为K-1邻近)
95+
96+
raw_files = []
97+
for filename in os.listdir(input_dir):
98+
raw_file = os.path.join(input_dir, filename)
99+
(file, ext) = os.path.splitext(raw_file)
100+
if ext == ".txt":
101+
raw_files.append(raw_file)
102+
103+
# size_dims = []
104+
for filepath in raw_files:
105+
data_id = dg_utils.GetGraphIDFromPath(filepath)
106+
print("当前处理: " + str(data_id) + " Graph")
107+
108+
cur_points, labels, pts_size, dim = ReadPointSets(filepath,
109+
norm_data=True)
110+
print((pts_size, dim))
111+
112+
sav_graph(os.path.join(graph_path, "g_{}.txt".format(data_id)),
113+
cur_points, labels, k_closest_count) # 打开新文件fm_{1}.txt
114+
115+
elapsed = (time.perf_counter() - start)
116+
print("Total time for building knn graph:", elapsed)

graphSim/.gitignore

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
# This file is used to ignore files which are generated
2+
# ----------------------------------------------------------------------------
3+
4+
*~
5+
*.autosave
6+
*.a
7+
*.core
8+
*.moc
9+
*.o
10+
*.obj
11+
*.orig
12+
*.rej
13+
*.so
14+
*.so.*
15+
*_pch.h.cpp
16+
*_resource.rc
17+
*.qm
18+
.#*
19+
*.*#
20+
core
21+
!core/
22+
tags
23+
.DS_Store
24+
.directory
25+
*.debug
26+
Makefile*
27+
*.prl
28+
*.app
29+
moc_*.cpp
30+
ui_*.h
31+
qrc_*.cpp
32+
Thumbs.db
33+
*.res
34+
*.rc
35+
/.qmake.cache
36+
/.qmake.stash
37+
38+
# qtcreator generated files
39+
*.pro.user*
40+
41+
# xemacs temporary files
42+
*.flc
43+
44+
# Vim temporary files
45+
.*.swp
46+
47+
# Visual Studio generated files
48+
*.ib_pdb_index
49+
*.idb
50+
*.ilk
51+
*.pdb
52+
*.sln
53+
*.suo
54+
*.vcproj
55+
*vcproj.*.*.user
56+
*.ncb
57+
*.sdf
58+
*.opensdf
59+
*.vcxproj
60+
*vcxproj.*
61+
62+
# MinGW generated files
63+
*.Debug
64+
*.Release
65+
66+
# Python byte code
67+
*.pyc
68+
69+
# Binaries
70+
# --------
71+
*.dll
72+
*.exe
73+
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
QMAKE_MAC_SDK.macosx.Path = /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX11.1.sdk
2+
QMAKE_MAC_SDK.macosx.PlatformPath = /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform
3+
QMAKE_MAC_SDK.macosx.SDKVersion = 11.1
4+
QMAKE_MAC_SDK.macx-clang.macosx.QMAKE_CC = /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/clang
5+
QMAKE_MAC_SDK.macx-clang.macosx.QMAKE_CXX = /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/clang++
6+
QMAKE_MAC_SDK.macx-clang.macosx.QMAKE_FIX_RPATH = \
7+
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/install_name_tool \
8+
-id
9+
QMAKE_MAC_SDK.macx-clang.macosx.QMAKE_AR = \
10+
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/ar \
11+
cq
12+
QMAKE_MAC_SDK.macx-clang.macosx.QMAKE_RANLIB = \
13+
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/ranlib \
14+
-s
15+
QMAKE_MAC_SDK.macx-clang.macosx.QMAKE_LINK = /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/clang++
16+
QMAKE_MAC_SDK.macx-clang.macosx.QMAKE_LINK_SHLIB = /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/clang++
17+
QMAKE_MAC_SDK.macx-clang.macosx.QMAKE_ACTOOL = /Applications/Xcode.app/Contents/Developer/usr/bin/actool
18+
QMAKE_MAC_SDK.macx-clang.macosx.QMAKE_LINK_C = /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/clang
19+
QMAKE_MAC_SDK.macx-clang.macosx.QMAKE_LINK_C_SHLIB = /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/clang
20+
QMAKE_CXX.QT_COMPILER_STDCXX = 199711L
21+
QMAKE_CXX.QMAKE_APPLE_CC = 6000
22+
QMAKE_CXX.QMAKE_APPLE_CLANG_MAJOR_VERSION = 12
23+
QMAKE_CXX.QMAKE_APPLE_CLANG_MINOR_VERSION = 0
24+
QMAKE_CXX.QMAKE_APPLE_CLANG_PATCH_VERSION = 0
25+
QMAKE_CXX.QMAKE_GCC_MAJOR_VERSION = 4
26+
QMAKE_CXX.QMAKE_GCC_MINOR_VERSION = 2
27+
QMAKE_CXX.QMAKE_GCC_PATCH_VERSION = 1
28+
QMAKE_CXX.COMPILER_MACROS = \
29+
QT_COMPILER_STDCXX \
30+
QMAKE_APPLE_CC \
31+
QMAKE_APPLE_CLANG_MAJOR_VERSION \
32+
QMAKE_APPLE_CLANG_MINOR_VERSION \
33+
QMAKE_APPLE_CLANG_PATCH_VERSION \
34+
QMAKE_GCC_MAJOR_VERSION \
35+
QMAKE_GCC_MINOR_VERSION \
36+
QMAKE_GCC_PATCH_VERSION
37+
QMAKE_CXX.INCDIRS = \
38+
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1 \
39+
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/clang/12.0.0/include \
40+
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX11.1.sdk/usr/include \
41+
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include
42+
QMAKE_CXX.LIBDIRS = /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX11.1.sdk/usr/lib
43+
QMAKE_XCODE_DEVELOPER_PATH = /Applications/Xcode.app/Contents/Developer
44+
QMAKE_XCODE_VERSION = 12.3

0 commit comments

Comments
 (0)