Skip to content

Commit d16b351

Browse files
authored
Merge pull request #2 from DerikVo/eda_create_module_class
created a class for images and created a function for contrast
2 parents fadd3b4 + 0362870 commit d16b351

File tree

2 files changed

+156
-96
lines changed

2 files changed

+156
-96
lines changed

Notebooks/01_EDA.ipynb

+112-64
Large diffs are not rendered by default.

modules/eda.py

+44-32
Original file line numberDiff line numberDiff line change
@@ -19,37 +19,6 @@ def folders(dataset):
1919
folders= os.listdir(path)
2020
return folders
2121

22-
def avg_images(dataset, sub_folder):
23-
'''
24-
This function takes two arguments the dataset: training or testing, and the sub_folder for the type of tumor e.g. ['glioma', 'meningioma', 'notumor', 'pituitary']
25-
This function is used to find the average pixel values of each class
26-
The purpose is to find if there is a difference in each class
27-
'''
28-
#assign the path in the function for readability and understanding
29-
#assign the sub folder (class name) that was passed to the function
30-
path = (f'../Images/{dataset}')
31-
class_name = sub_folder
32-
batch_size = 32 # Modify this to suit your needs
33-
#instantiate ImageDataGenerator
34-
datagen = ImageDataGenerator(rescale=1./255) # normalize pixel values to [0,1]
35-
#get the images from the directory
36-
generator = datagen.flow_from_directory(path,
37-
classes=[class_name],
38-
class_mode=None,
39-
color_mode='grayscale',
40-
target_size=(256, 256),
41-
batch_size=batch_size)
42-
n_samples = generator.samples
43-
average_image = np.zeros((256, 256, 1))
44-
45-
for i in range(n_samples // batch_size): # Integer division to avoid partial batches
46-
images = next(generator)
47-
average_image += np.sum(images, axis=0)
48-
49-
average_image /= n_samples
50-
return average_image
51-
52-
5322
def image_len(dataset, folders):
5423
'''
5524
This code takes in the list directory of the folder containing the classification folders. And the dataset.
@@ -74,4 +43,47 @@ def image_len(dataset, folders):
7443
plt.title(f'{image_name}')
7544
plt.imshow(image)
7645
plt.axis('off')
77-
plt.show()
46+
plt.show()
47+
class Image:
48+
def __init__(self, dataset, sub_folder):
49+
#learned i didnt need a comma because that creates a tuple: https://stackoverflow.com/questions/39192261/class-init-takes-parameters-but-turns-them-into-tuples-for-some-reason
50+
self.dataset = dataset
51+
self.sub_folder = sub_folder
52+
53+
def avg_images(self):
54+
'''
55+
This function takes two arguments the dataset: training or testing, and the sub_folder for the type of tumor e.g. ['glioma', 'meningioma', 'notumor', 'pituitary']
56+
This function is used to find the average pixel values of each class
57+
The purpose is to find if there is a difference in each class
58+
'''
59+
#assign the path in the function for readability and understanding
60+
#assign the sub folder (class name) that was passed to the function
61+
path = (f'../Images/{self.dataset}')
62+
class_name = self.sub_folder
63+
batch_size = 32 # Modify this to suit your needs
64+
#instantiate ImageDataGenerator
65+
datagen = ImageDataGenerator(rescale=1./255) # normalize pixel values to [0,1]
66+
#get the images from the directory
67+
generator = datagen.flow_from_directory(path,
68+
classes=[class_name],
69+
class_mode=None,
70+
color_mode='grayscale',
71+
target_size=(256, 256),
72+
batch_size=batch_size)
73+
n_samples = generator.samples
74+
average_image = np.zeros((256, 256, 1))
75+
76+
for i in range(n_samples // batch_size): # Integer division to avoid partial batches
77+
images = next(generator)
78+
average_image += np.sum(images, axis=0)
79+
80+
average_image /= n_samples
81+
return average_image
82+
83+
def image_contrast(comparision, base_image):
84+
# we need to rescale the contrasts
85+
image = base_image - comparision
86+
image -= image.min() # subtract minimum
87+
image /= image.max() # divide by new max
88+
89+
return image

0 commit comments

Comments
 (0)