Skip to content

Commit

Permalink
Added files for conversion of coordinates
Browse files Browse the repository at this point in the history
  • Loading branch information
MaaikevS authored Aug 14, 2022
1 parent 5c374ea commit 7db3da8
Show file tree
Hide file tree
Showing 2 changed files with 147 additions and 0 deletions.
54 changes: 54 additions & 0 deletions miscellaneous/convertJSON2TXT.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
# -*- coding: utf-8 -*-
"""
Created on Jul 20 15:39:23 2022
Script to convert meshview compatible JSON files to TXT file using the same colour definitions.
Text files are saved in the same folder as the original JSON files using the same file names.
This script was written for a specific set of files, but works for all meshview compatible JSON files
as long as the folder the JSON files are stored in is correctly defined. This can be changed by modifying
the definition of the variable "fnames".
@author: mvanswieten
"""

import os
import json
import glob

fpath = os.getcwd()

# Find all JSON files that are in the subfolders of the folders in the directory
fnames = glob.glob(fpath + "**\\**\\*.json", recursive = True)

# Open the files and add them to a list for further processing
file_list = []
for fname in fnames:
with open(fname, 'r') as f:
file_list.append(json.load(f))
f.close()

# Loop over the list and process each file individually
for i in range(len(file_list)):
exp_name = fnames[i].split("\\")[-1].split(".json")[0].split("_")[0:1]
output_folder = "\\".join(fnames[i].split("\\")[:-1])
print("Loading data for experiment: " + exp_name[i])
# Extract the x, y, z coordinates for the list of "triplets".
for f in file_list[i]:

coordinate_list = f["triplets"]

fname_txt = f["name"] + ".txt"
xs = coordinate_list[0::3]
ys = coordinate_list[1::3]
zs = coordinate_list[2::3]
with open(os.path.join(output_folder,fname_txt), 'w') as fi:
fi.write("RGB " + str(f["r"]) + " " + str(f["g"]) + " " + str(f["b"]))
fi.write('\n')
for l in range(f["count"]):
fi.write(str(xs[l]))
fi.write('\t')
fi.write(str(ys[l]))
fi.write('\t')
fi.write(str(zs[l]))
fi.write('\n')
93 changes: 93 additions & 0 deletions miscellaneous/convertXYZ2JSON.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
"""
Created on Jul 20 13:52:25 2022
Script to convert coordinates from a .xyz file to a JSON file compatible with Meshview
This script was written for a specific set of files, but works for all .xyz files as long
as the folder the .xyz files are stored in is correctly defined. This can be changed by modifying
the definition of the variable "experiment_files".
The colour used for the JSON file is dependent on the type of injection tracer. BDA/PHA/WGA is
black, Fe/FE is green, Fr is red, FG is yellow, and FB is blue. If the files do not follow the
naming convention of the files the script was written for, then the colour of the points is set
to black by default.
@author: mvanswieten
"""

import json
import glob
import os

cwd = os.getcwd()

# Find all JSON files that are in the subfolders of the folders in the directory
experiment_files = glob.glob(os.path.join(cwd,"") + "**\\**\\*.xyz", recursive = True)

output_path = os.path.join(cwd,"output_files")

for f in range(len(experiment_files)):

triplets = []
with open(experiment_files[f], "r") as my_file:
output_folder = os.path.join(output_path, "\\".join(experiment_files[f].split("\\")[-3:-1]), "")
if not os.path.isdir(output_folder):
os.makedirs(output_folder)

if experiment_files[f].split("\\")[-2] in ["BDA", "PHA", "WGA"]:
r = 0
g = 0
b = 0
elif experiment_files[f].split("\\")[-2] in ["Fe", "FE"]:
r = 0
g = 0.5
b = 0
elif experiment_files[f].split("\\")[-2] == "Fr" :
r = 1
g = 0
b = 0
elif experiment_files[f].split("\\")[-2] == "FG":
r = 1
g = 1
b = 0
elif experiment_files[f].split("\\")[-2] == "FB":
r = 0
g = 0
b = 1
else:
r = 0
g = 0
b = 0

for line in my_file:

# Skip the lines with a hashtag
if "#" in line:
continue
# Only consider lines that have text/coordinates on them
elif not line == "\n":
currentline = line.split(",")
if len(currentline[0].split(" ")) == 3:
coordinates = currentline[0].split(" ")
else:
coordinates = currentline[0].split("\t")

triplets.append(float(coordinates[0]))
triplets.append(float(coordinates[1]))
triplets.append(float(coordinates[2].split("\n")[0]))

fname = os.path.split(experiment_files[f])[-1].split(".xyz")[0]
instance = {
"idx": 0,
"count": int(len(triplets)/3),
"r": int(r*255),
"g": int(g*255),
"b": int(b*255),
"name": ("_").join(fname.split("_")[2:4]),
"triplets": triplets
}

# Save the file using the original name and organisation in a new output folder
filename = ("_").join(fname.split("_")[2:4]) + ".json"
with open(os.path.join(output_folder,filename), 'w', encoding='utf-8') as fi:
fi.write(json.dumps([instance], ensure_ascii=False, indent=4))

0 comments on commit 7db3da8

Please sign in to comment.