-
Notifications
You must be signed in to change notification settings - Fork 0
/
main - 2 convert RNA-seqtoimage-working.py
55 lines (40 loc) · 2.35 KB
/
main - 2 convert RNA-seqtoimage-working.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import numpy as np
from PIL import Image
import os
import pandas as pd
import matplotlib.pyplot as plt # Import plt for plotting
import matplotlib.cm as cm
# Load your data from the txt file (replace 'data.txt' with your file path)
#data = pd.read_csv('H:/WFH/AI/data/HiSeqV2_PANCAN.txt', delimiter='\t', index_col=0)
data = pd.read_csv('H:/WFH/AI/data/Reordered_Data.txt', delimiter='\t', index_col=0)
data = data.drop(columns=["Cluster"])
# Separate patients and controls
patients = data.loc[:, data.columns.str[13] != '1']
controls = data.loc[:, data.columns.str[13] == '1']
# Set the dimensions of the image (adjust these values as needed)
m, n = 205, 101 # Example dimensions, adjust as needed
# Create directories for patients and controls
os.makedirs('H:/WFH/AI/data/patient', exist_ok=True)
os.makedirs('H:/WFH/AI/data/control', exist_ok=True)
# Process patient data
# Define the dimensions of the frame
frame_depth = 20
for i, (column_name, column_data) in enumerate(patients.items()):
padded_data = np.pad(column_data.values, ((0, m * n - len(column_data))), mode='constant', constant_values=5)
image_data = padded_data.reshape(m, n)
# Normalize the data to [0, 1]
normalized_data = (image_data - image_data.min()) / (image_data.max() - image_data.min())
# Use a grayscale colormap to map the normalized data to grayscale colors
grayscale_image = cm.gray_r(normalized_data) # 'gray_r' for reversed grayscale
# Create and save the image as a PNG file in the patient folder using plt.imsave
plt.imsave(f'H:/WFH/AI/data/patient/patient_{i + 1}.png', grayscale_image, cmap='gray')
# Process control data
for j, (column_name, column_data) in enumerate(controls.items()):
padded_data = np.pad(column_data.values, ((0, m * n - len(column_data))), mode='constant', constant_values=5)
image_data = padded_data.reshape(m, n)
# Normalize the data to [0, 1]
normalized_data = (image_data - image_data.min()) / (image_data.max() - image_data.min())
# Use a grayscale colormap to map the normalized data to grayscale colors
grayscale_image = cm.gray_r(normalized_data) # 'gray_r' for reversed grayscale
# Create and save the image as a PNG file in the control folder using plt.imsave
plt.imsave(f'H:/WFH/AI/data/control/control_{j + 1}.png', grayscale_image, cmap='gray')