import os
2 | from scipy import ndimage
3 | from subprocess import check_output
4 |
5 | import cv2
6 | import numpy as np
7 | from matplotlib import pyplot as plt
8 | %matplotlib inline
img_rows, img_cols= 720, 1280
9 | im_array = cv2.imread('../input/train/LAG/img_02236.jpg',0)
10 | template = np.zeros([ img_rows, img_cols], dtype='uint8') # initialisation of the template
11 | template[:, :] = im_array[:,:] # I try multiple times to find the correct rectangle.
12 | #template /= 255.
13 | max_d = 5
14 | blur_d = 10
15 | min_d = 1
16 |
17 | tg_d = min_d
18 | tg_wt = (2*tg_d+1)*(2*tg_d+1)
19 |
20 | for i in range(tg_d, img_rows - tg_d - 1):
21 | for j in range(tg_d, img_cols - tg_d -1):
22 | #print(np.max(im_array[(i-d):(i+d), (j-d):(j+d)]))
23 | template[i, j] = np.sum(im_array[(i-tg_d):(i+tg_d), (j-tg_d):(j+tg_d)])/tg_wt
24 | #template[i, j] = np.max(im_array[(i-max_d):(i+max_d), (j-max_d):(j+max_d)]) - np.max(im_array[(i-min_d):(i+min_d), (j-min_d):(j+min_d)])
25 |
26 | for i in range(max_d, img_rows - max_d - 1):
27 | for j in range(max_d, img_cols - max_d -1):
28 | #print(np.max(im_array[(i-d):(i+d), (j-d):(j+d)]))
29 | #template[i, j] = np.sum(im_array[(i-tg_d):(i+tg_d), (j-tg_d):(j+tg_d)])/tg_wt
30 | maxc = np.max(template[(i-max_d):(i+max_d), (j-max_d):(j+max_d)])
31 | minc = np.min(template[(i-min_d):(i+min_d), (j-min_d):(j+min_d)])
32 | template[i, j] = (maxc-minc)*255.0/maxc
33 | #template[i, j] = np.max(template[(i-max_d):(i+max_d), (j-max_d):(j+max_d)]) - template[i, j]
34 | plt.subplots(figsize=(100, 70))
35 | plt.subplot(121),plt.imshow(template, cmap='gray')
36 | plt.subplot(122), plt.imshow(im_array, cmap='gray')
37 | file_name = '../input/train/LAG/img_01512.jpg' # img_00176,img_02758, img_01512
38 | img = cv2.imread(file_name,0)
39 | img2 = img
40 | w, h = template.shape[::-1]
41 |
42 | # All the 6 methods for comparison in a list
43 | methods = ['cv2.TM_CCOEFF', 'cv2.TM_CCOEFF_NORMED', 'cv2.TM_CCORR',
44 | 'cv2.TM_CCORR_NORMED', 'cv2.TM_SQDIFF', 'cv2.TM_SQDIFF_NORMED']
45 |
46 | for meth in methods:
47 | img = img2
48 | method = eval(meth)
49 |
50 | # Apply template Matching
51 | res = cv2.matchTemplate(img,template,method)
52 | min_val, max_val, min_loc, max_loc = cv2.minMaxLoc(res)
53 |
54 | # If the method is TM_SQDIFF or TM_SQDIFF_NORMED, take minimum
55 | if method in [cv2.TM_SQDIFF, cv2.TM_SQDIFF_NORMED]:
56 | top_left = min_loc
57 | else:
58 | top_left = max_loc
59 | bottom_right = (top_left[0] + w, top_left[1] + h)
60 |
61 | cv2.rectangle(img,top_left, bottom_right, 255, 2)
62 | fig, ax = plt.subplots(figsize=(12, 7))
63 | plt.subplot(121),plt.imshow(res,cmap = 'gray')
64 | plt.title('Matching Result'), plt.xticks([]), plt.yticks([])
65 | plt.subplot(122),plt.imshow(img,cmap = 'gray') #,aspect='auto'
66 | plt.title('Detected Point'), plt.xticks([]), plt.yticks([])
67 | plt.suptitle(meth)
68 |
69 | plt.show()
70 | method = eval('cv2.TM_CCOEFF')
71 | indexes=[1,30,40,5]
72 |
73 | train_path = "../input/train/"
74 | sub_folders = check_output(["ls", train_path]).decode("utf8").strip().split('\n')
75 | for sub_folder in sub_folders:
76 | file_names = check_output(["ls", train_path+sub_folder]).decode("utf8").strip().split('\n')
77 | k=0
78 | _, ax = plt.subplots(2,2,figsize=(10, 7))
79 | for file_name in [file_names[x] for x in indexes]: # I take only 4 images of each group.
80 | img = cv2.imread(train_path+sub_folder+"/"+file_name,0)
81 | img2 = img
82 | w, h = template.shape[::-1]
83 | # Apply template Matching
84 | res = cv2.matchTemplate(img,template,method)
85 | min_val, max_val, min_loc, max_loc = cv2.minMaxLoc(res)
86 | top_left = max_loc
87 | bottom_right = (top_left[0] + w, top_left[1] + h)
88 |
89 | cv2.rectangle(img,top_left, bottom_right, 255, 2)
90 | if k==0 :
91 | ax[0,0].imshow(img,cmap = 'gray')
92 | plt.xticks([]), plt.yticks([])
93 | if k==1 :
94 | ax[0,1].imshow(img,cmap = 'gray')
95 | plt.xticks([]), plt.yticks([])
96 | if k==2 :
97 | ax[1,0].imshow(img,cmap = 'gray')
98 | plt.xticks([]), plt.yticks([])
99 | if k==3 :
100 | ax[1,1].imshow(img,cmap = 'gray')
101 | plt.xticks([]), plt.yticks([])
102 | k=k+1
103 | plt.suptitle(sub_folder)
104 | plt.show()
As we can see, with a LAG template, we almost find all the LAG fish. This is good point. 106 | The other good point is that we don't find in our rectangle the other fish. Now the idea is to create the other template and do it for all the images.
107 |On the same way, the goal is to detect the fish in an image.
110 |There exist a method to find Shapes in an image. (tutorial)
111 |
Author : Arunpandian Murugan
3 |Email ID : arunpandian.murugan@outlook.com
4 |def display_board(my_board_entry):
6 | print(' | | \n {0} | {1} | {2} \n | | \n-----------------\n | | \n {3} | {4} | {5} \n | | \n-----------------\n | | \n {6} | {7} | {8} \n | | \n'.format(my_board_entry[0], my_board_entry[1], my_board_entry[2], my_board_entry[3], my_board_entry[4], my_board_entry[5], my_board_entry[6], my_board_entry[7], my_board_entry[8]))
def get_user_input(player1, player2, int_turn_counter, list_board_entry):
8 |
9 | #Variable Initialisation
10 | list_player_symbol = []
11 | list_player_symbol.append(player1)
12 | list_player_symbol.append(player2)
13 | list_input = []
14 | flag_get_input = 'Y'
15 |
16 | while(flag_get_input.upper() == 'Y'):
17 | int_board_position = int(input('Player {} --> {}, Enter the valid position number...\t'.format((int_turn_counter%2)+1, list_player_symbol[int_turn_counter%2])))
18 |
19 | if((int_board_position > 0) and (int_board_position < 10)):
20 | #validation check
21 | if(list_board_entry[int_board_position - 1] == ' '):
22 | list_input.append(int_board_position)
23 | list_input.append(list_player_symbol[int_turn_counter%2])
24 | return(list_input)
25 | else:
26 | print('Entered position is already filled, so kindly provide some other free position...\n')
27 | flag_get_input = input('Do want to continue ...[Y/N]\t ')
28 | if(flag_get_input.upper() == 'N'):
29 | list_input.append('End')
30 | return(list_input)
31 |
32 | elif((int_board_position < 0) or (int_board_position > 10)):
33 | print('Invalid position number | kindly enter the position between 1 to 9...\n')
34 | flag_get_input = input('Do want to continue ...[Y/N]\t ')
35 | if(flag_get_input.upper() == 'N'):
36 | list_input.append('End')
37 | return(list_input)
def clear_cell():
39 | from IPython.display import clear_output
40 |
41 | for i in range(2):
42 | clear_output()
43 |
44 | ##import time
45 | ##time.sleep(2)
def core_algorithm(list_board_entry):
47 | #variable initialise
48 | temp_list_board_entry = list_board_entry.copy()
49 |
50 | if(temp_list_board_entry.count('X') >= 3):
51 | if(temp_list_board_entry[0:3].count('X') == 3 or temp_list_board_entry[3:6].count('X') == 3 or temp_list_board_entry[6:9].count('X') == 3 or (temp_list_board_entry[0] == 'X' and temp_list_board_entry[4] == 'X' and temp_list_board_entry[8] == 'X') or (temp_list_board_entry[2] == 'X' and temp_list_board_entry[4] == 'X' and temp_list_board_entry[6] == 'X') ):
52 | print('Hurrah!!! Player 1 Won...')
53 | return 'End'
54 |
55 | elif(temp_list_board_entry[0:3].count('O') == 3 or temp_list_board_entry[3:6].count('O') == 3 or temp_list_board_entry[6:9].count('O') == 3 or (temp_list_board_entry[0] == 'O' and temp_list_board_entry[4] == 'O' and temp_list_board_entry[8] == 'O') or (temp_list_board_entry[2] == 'O' and temp_list_board_entry[4] == 'O' and temp_list_board_entry[6] == 'O') ):
56 | if(tuple(temp_list) in dump_set):
57 | print('Hurrah!!! Player 2 Won...')
58 | return 'End'
59 |
def tic_tac_toe_main():
61 |
62 | #Initalise Flag
63 | continue_flag = 'Y'
64 |
65 | while(continue_flag.upper() == 'Y'):
66 |
67 | #Reset the Flag
68 | continue_flag = ''
69 |
70 | #Variable Initialisation
71 | int_turn_counter = 0
72 | list_board_entry = [" ", " ", " ", " ", " ", " ", " ", " ", " " ]
73 |
74 | print("Welcome to Arun's Tic Tac Toe Game using Python 3\n")
75 |
76 | #Select X or O
77 | player_choice = input('Player 1, do you want to be X or O...\t')
78 |
79 | if(player_choice.upper() == 'X'):
80 |
81 | #iterating through game
82 | print('Player 1 --> X \nPlayer 2 --> O')
83 | while(int_turn_counter < 9):
84 | list_input = get_user_input('X', 'O', int_turn_counter, list_board_entry)
85 |
86 | if(list_input[0] == 'End'):
87 | break
88 |
89 | #Displaying the updated board...
90 | list_board_entry[int(list_input[0]) -1] = list_input[1]
91 | clear_cell()
92 | display_board(list_board_entry)
93 |
94 | flag = core_algorithm(list_board_entry)
95 | if(flag == 'End'):
96 | break
97 |
98 | int_turn_counter += 1
99 |
100 | elif(player_choice.upper() == 'O'):
101 |
102 | #iterating through game
103 | print('Player 1 --> O \nPlayer 2 --> X')
104 | while(int_turn_counter < 9):
105 | list_input = get_user_input('O', 'X', int_turn_counter, list_board_entry)
106 |
107 | if(list_input[0] == 'End'):
108 | break
109 |
110 | #Displaying the updated board...
111 | list_board_entry[int(list_input[0]) - 1] = list_input[1]
112 | clear_cell()
113 | display_board(list_board_entry)
114 |
115 | flag = core_algorithm(list_board_entry)
116 | if(flag == 'End'):
117 | break
118 |
119 | int_turn_counter += 1
120 |
121 | else:
122 | print('Invalid Symbol selection...Exiting')
123 |
124 | if(list_input[0] == 'End' or flag == 'End'):
125 | continue_flag = 'N'
126 | else:
127 | continue_flag = input('Do want to continue ...[Y/N]\t ')
tic_tac_toe_main()
| | 128 | | | 129 | | | 130 | ----------------- 131 | | | 132 | | X | O 133 | | | 134 | ----------------- 135 | | | 136 | | | 137 | | | 138 | 139 |
This tutorial covers the basics of contrlling the layout of IPython widgets.
3 |We will be using the following functions to do that.
4 |Video Tutorial:
10 | 11 |from IPython.html import widgets
13 | from IPython.display import display
"vbox"
classBy default a container will have a "vbox"
css class added to it.
v_container = widgets.ContainerWidget()
16 |
17 | # Define Widgets
18 | for counter in range(5):
19 | wgt_check = widgets.CheckboxWidget()
20 | wgt_text = widgets.TextWidget(description="Name")
21 | wgt_select = widgets.DropdownWidget(values=["Guest", "User", "Admin"])
22 |
23 | # Add Widgets
24 | v_container.children += (wgt_check, wgt_text, wgt_select)
25 |
26 | wgt_button = widgets.ButtonWidget(description="Submit")
27 | v_container.children += (wgt_button,)
28 |
29 | # Display the container
30 | display(v_container)
"hbox"
classv_container = widgets.ContainerWidget()
32 |
33 | # Display the container
34 | display(v_container)
35 |
36 | # Define Widgets
37 | for counter in range(5):
38 | wgt_check = widgets.CheckboxWidget()
39 | wgt_text = widgets.TextWidget(description="Name")
40 | wgt_select = widgets.DropdownWidget(values=["Guest", "User", "Admin"])
41 |
42 | # Add Widgets
43 | h_container = widgets.ContainerWidget()
44 | h_container.children += (wgt_check, wgt_text, wgt_select)
45 | v_container.children += (h_container,)
46 |
47 | # Change css class of the container
48 | h_container.remove_class('vbox')
49 | h_container.add_class('hbox')
50 |
51 | wgt_button = widgets.ButtonWidget(description="Submit")
52 | v_container.children += (wgt_button,)
Gradient descent is an important algorithm for minimizing a function. Having an algorithm to minimize a function is quite powerful: you can maximize a function by minimizing and you can solve a system of equations by minimizing .
3 |Here's an example, in one variable, implemented in python, from https://en.wikipedia.org/wiki/Gradient_descent#Computational_examples. The program finds the minimum of 4 |
5 |fig = plt.figure()
6 | axes = fig.add_axes([0.1, 0.1, 0.9, 0.7])
7 | axes.scatter(x,y);
/ext/sage/sage-8.0/local/lib/python2.7/site-packages/matplotlib/font_manager.py:273: UserWarning: Matplotlib is building the font cache using fc-list. This may take a moment. 8 | warnings.warn('Matplotlib is building the font cache using fc-list. This may take a moment.') 9 |
%matplotlib inline
2 | import matplotlib.pyplot as plt
3 | import numpy as np
4 | %precision 4
5 | import os, sys, glob
Willl change this to use the same example for queries and schema design
7 |This data contains the survival time after receiving a heart transplant, the age of the patient and whether or not the survival time was censored
15 |Variable name definitions::
20 |import statsmodels.api as sm
26 | heart = sm.datasets.heart.load_pandas().data
27 | heart.take(np.random.choice(len(heart), 6))
32 | | survival | 33 |censors | 34 |age | 35 |
---|---|---|---|
32 | 40 |147 | 41 |1 | 42 |47.5 | 43 |
67 | 46 |13 | 47 |0 | 48 |28.9 | 49 |
49 | 52 |499 | 53 |0 | 54 |52.2 | 55 |
24 | 58 |1367 | 59 |0 | 60 |48.6 | 61 |
20 | 64 |54 | 65 |1 | 66 |49.0 | 67 |
1 | 70 |3 | 71 |1 | 72 |40.4 | 73 |
survival censors age 77 | 32 147 1 47.5 78 | 67 13 0 28.9 79 | 49 499 0 52.2 80 | 24 1367 0 48.6 81 | 20 54 1 49.0 82 | 1 3 1 40.4
table_df = pd.read_hdf('dump.h5', 'tables')
83 | table_df
88 | | Name | 89 |Value | 90 |Age | 91 |
---|---|---|---|
0 | 96 |a | 97 |5 | 98 |0 | 99 |
1 | 102 |c | 103 |6 | 104 |10 | 105 |
2 | 108 |e | 109 |9 | 110 |20 | 111 |
3 | 114 |a | 115 |5 | 116 |0 | 117 |
4 | 120 |c | 121 |6 | 122 |10 | 123 |
5 | 126 |e | 127 |9 | 128 |20 | 129 |
Name Value Age 133 | 0 a 5 0 134 | 1 c 6 10 135 | 2 e 9 20 136 | 3 a 5 0 137 | 4 c 6 10 138 | 5 e 9 20
store.close()
\n", 75 | " | survival | \n", 76 | "censors | \n", 77 | "age | \n", 78 | "
---|---|---|---|
32 | \n", 83 | "147 | \n", 84 | "1 | \n", 85 | "47.5 | \n", 86 | "
67 | \n", 89 | "13 | \n", 90 | "0 | \n", 91 | "28.9 | \n", 92 | "
49 | \n", 95 | "499 | \n", 96 | "0 | \n", 97 | "52.2 | \n", 98 | "
24 | \n", 101 | "1367 | \n", 102 | "0 | \n", 103 | "48.6 | \n", 104 | "
20 | \n", 107 | "54 | \n", 108 | "1 | \n", 109 | "49.0 | \n", 110 | "
1 | \n", 113 | "3 | \n", 114 | "1 | \n", 115 | "40.4 | \n", 116 | "
\n", 154 | " | Name | \n", 155 | "Value | \n", 156 | "Age | \n", 157 | "
---|---|---|---|
0 | \n", 162 | "a | \n", 163 | "5 | \n", 164 | "0 | \n", 165 | "
1 | \n", 168 | "c | \n", 169 | "6 | \n", 170 | "10 | \n", 171 | "
2 | \n", 174 | "e | \n", 175 | "9 | \n", 176 | "20 | \n", 177 | "
3 | \n", 180 | "a | \n", 181 | "5 | \n", 182 | "0 | \n", 183 | "
4 | \n", 186 | "c | \n", 187 | "6 | \n", 188 | "10 | \n", 189 | "
5 | \n", 192 | "e | \n", 193 | "9 | \n", 194 | "20 | \n", 195 | "
pwd
u'/ifs/devel/antoniob/projects/BEST-D'
cd /ifs/projects/proj043/analysis.dir
/ifs/projects/proj043/analysis.dir 2 |
import pandas
3 | import sqlite3
4 | import os
#Create SQL database and insert tables:
5 | df.to_sql(name='JULIAN_sheet_1_original', if_exists='replace', flavor='sqlite', con=sqlite3.connect('BESTD_csvdb'))
6 | df2.to_sql(name='JULIAN_sheet_1_for_plink', if_exists='replace', flavor='sqlite', con=sqlite3.connect('BESTD_csvdb'))
from pymbolic import parse
3 |
4 | x = parse("x")
5 | h = parse("h")
u = (x+4)**3
7 |
8 | h = parse("h")
9 |
10 | expr = u + 2*u*h + 4*u*h**2
11 |
# Load an IPython extension for graph drawing
12 | %load_ext gvmagic
from pymbolic.mapper.graphviz import GraphvizMapper
13 |
14 | gvm = GraphvizMapper()
15 | gvm(expr)
%dotstr gvm.get_dot_code()
ec.compile(expr)
'pow(x + 4, 3) + 4 * pow(x + 4, 3) * h * h + 2 * pow(x + 4, 3) * h'
import pandas as pd
2 | import numpy as np
3 | import seaborn as sns
4 | import matplotlib.pyplot as plt
This ipynb was edited and saved with VSCode (version 1.47.3).
3 |print("""A multi-line
4 | string
5 | """)
A multi-line 6 | string 7 | 8 |