Saturday, August 1, 2020

Graph2Data in Python with Bounding Box and Extracted Data Scaling

This code generates a bounding box and extract data from graph in example image. Interestingly, this code project also extends my PhD research work from manual to automated extraction from image data. Here, I show a basic example. The example graph:


The input graph is manually generated.


The sampled extracted data is displayed with bounding box.


To scale, minimum and maximum axes ranges are taken from example image.


Finally, the output extracted and scaled data plot.


# 2020 Dr Tariq Javid, Hamdard University

get_ipython().run_line_magic('matplotlib', 'inline')
import warnings
warnings.filterwarnings('ignore')
import numpy as np
from skimage.color import rgb2gray
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
from matplotlib.patches import Rectangle

def data_line_center(x): 
    s = 0 # start 
    c = 0 # count
    dat_val = 0.
    vect_len = len(x)
    for idx in range(vect_len):
        if x[idx] == 0:
            c = c + 1
    for idx in range(vect_len):
        if x[idx] == 0:
            s = idx
            break
    dat_val = s + c / 2.
    return dat_val

def bound_bx(arr, a, b, c, d):
    # print a, b, c, d
    right_x  = 0
    right_y  = 0
    left_x   = 0
    left_y   = 0
    top_x    = 0
    top_y    = 0
    bottom_x = 0
    bottom_y = 0
    new_arr  = np.zeros_like(arr)
    # top_xy
    for idx in range(a, c):
        for idy in range(b, d):
            if arr[idx,idy] == 0.:
                new_arr[idx,idy] = 1.
                break
        else:
            continue
        break
    # left_xy
    for idy in range(b, d):
        for idx in range(a, c):
            if arr[idx,idy] == 0.:
                new_arr[idx,idy] = 1.
                break
        else:
            continue
        break
    # bottom_xy
    arr = np.flipud(arr)
    new_arr = np.flipud(new_arr)
    for idx in range(a, c):
        for idy in range(b, d):
            if arr[idx,idy] == 0.:
                new_arr[idx,idy] = 1.
                break
        else:
            continue
        break
    arr = np.flipud(arr)
    new_arr = np.flipud(new_arr)    
    # right_xy
    arr = np.fliplr(arr)
    new_arr = np.fliplr(new_arr)
    for idy in range(b, d):
        for idx in range(a, c):
            if arr[idx,idy] == 0.:
                new_arr[idx,idy] = 1.
                break
        else:
            continue
        break
    arr = np.fliplr(arr)
    new_arr = np.fliplr(new_arr)
    # transform data to information  
    return new_arr

def point_dat(arr, a, b, c, d): # binarization
    new_arr = np.ones_like(arr)
    for idx in range(a, c):
        for idy in range(b, d):
            if arr[idx,idy] < 0.5:
                new_arr[idx,idy] = 0.
            else:
                new_arr[idx,idy] = 1.
    return new_arr

def bound_bx_points(arr, nrows, ncols):
    arr4 = np.zeros((4,2))
    n = 0
    for idx in range(0, nrows):
        for idy in range(0, ncols):
            if arr[idx,idy] == 1.:
                arr4[n,0] = idx
                arr4[n,1] = idy
                n = n + 1
    new_arr = arr4
    return new_arr

def scale_dat(x,m,n,d,t,s): 
    arr = np.zeros([m,3])   # 
    cd  = 0
    ct  = 0
    # select data as per dat_range
    for idy in range(m):
        cd = round(idy * d)
        arr[idy,0] = cd
        arr[idy,1] = x[cd]
        arr[idy,2] = s + idy
    # scale data 
    arr[:,1] = arr[:,1] - min(arr[:,1]) # 
    arr[:,1] = arr[:,1] / max(arr[:,1]) # data in [0,1]
    arr[:,1] = abs(arr[:,1] - 1)        # data flip up down
    arr[:,1] = arr[:,1] * n             # temp range incorporated
    arr[:,1] = arr[:,1] + t             # minimum temp added
    return arr

def main():
    img_in = mpimg.imread('example066_IP.png') # Fig 6.6, 1851--2000 (150 years)
    img_gray  = rgb2gray(img_in)
    img_gray  = (img_gray - np.min(img_gray)) / (np.max(img_gray) - np.min(img_gray))
    arr = img_gray
    no_rows = arr.shape[0] #  315
    no_cols = arr.shape[1] # 1050
    arr2 = point_dat(arr, 0, 0, no_rows, no_cols)  # arr2 is binary
    arr3 = bound_bx(arr2, 0, 0, no_rows, no_cols)  # arr3 has bounding box data 
    arr4 = bound_bx_points(arr3, no_rows, no_cols) # arr4 has bounding box corner points
    x_top_left = int(min(arr4[:,0]))
    y_top_left = int(min(arr4[:,1]))
    x_bottom_right = int(max(arr4[:,0]))
    y_bottom_right = int(max(arr4[:,1]))
    x_lower_left = int(max(arr4[:,0]))
    y_lower_left = int(min(arr4[:,1]))
    rect_width   = y_bottom_right - y_top_left
    rect_height  = x_bottom_right - x_top_left
    plt.figure(figsize=(20,10)) 
    ax = plt.gca()
    rect = Rectangle((y_top_left,x_top_left),rect_width,rect_height,linewidth=1,edgecolor='b',facecolor='none')
    ax.add_patch(rect)
    # plt.axvline(x=100,color='red')
    dat_start = 1851  # start year
    dat_end   = 2000  # end year
    tmp_min   = 17.40 # minimum temperature
    tmp_max   = 18.77 # maximum temperature
    dat_range = dat_end - dat_start
    tmp_range = tmp_max - tmp_min
    # print (dat_range,tmp_range)
    dat_adjust= float(rect_width) / float(dat_range)
    tmp_adjust= float(rect_height) / float(tmp_range)
    vect_dat = np.zeros(rect_width)
    plt.imshow(img_in)
    n = 0
    for y in range(y_top_left, y_bottom_right):
        dat_val  = data_line_center(arr2[:,y])
        vect_dat[n] = dat_val 
        n = n + 1
        if (n % 10 == 0):
            plt.scatter(y,dat_val,color='b')
    output_dat = scale_dat(vect_dat,dat_range,tmp_range,dat_adjust,tmp_min,dat_start)
    x = output_dat[:,0]
    y = output_dat[:,1]
    z = output_dat[:,2]
    plt.figure(figsize=(20,4)) 
    plt.plot(z,y,color='g')

if __name__ == "__main__":
    main()




Wednesday, April 22, 2020

Let us Numba by Dr Tariq Javid -- a Rework

In this post, I used example codes from Numba tutorial for GTC 2017 conference.

  • I have created an environment deep-learning in Anaconda Navigator (Anaconda3). In this environment, I have installed required Python modules, for example, cudatoolkit and numba.
  • The code in Jupyter Notebook begin by exporting path to NVIDIA driver DLL and library folder. 
  • The first example is on use of jit decorator. This example shows performance improvement while using the compiled code in comparison to pure python code. 
  • The second example uses vectorize decorator. This example shows code for GPU is slow whereas code for CPU is faster. Reason: small number of computations for GPU and data transfer overhead.
  • The third example code shows a faster for GPU. 
  • The fourth example code shows baseline performance with host arrays.
  • The fifth example code shows how transfer data to and from GPU toward faster execution. 









Thursday, March 19, 2020

About Dr. Tariq Javid

20 March 2020

About Dr. Tariq Javid

Objective: To make sellable products that solve real life problems. 

Research Statement: To discover practical solutions to real life problems through multidisciplinary research. Primary ingredients of my research include applications of image processing techniques and information technology. I discovered a regular hexagonal model of sphere-like objects during my PhD research project; and applied model to determine left ventricle ejection fraction with reduced computations and improved accuracy.         

Brief Bibliography: Dr. Tariq Javid is currently working as Associate Professor at the Faculty of Engineering Sciences and Technology, Hamdard University (HU), Karachi, Pakistan; since February 2015. At HU he teaches Bachelor/Masters/PhD level courses related to electrical engineering, electronics engineering,  computer systems engineering, computer sciences, and information technology. He leads a team of highly motivated Biomedical Engineers and conducts research toward solutions discovery. Before that, he served as Assistant Professor at the Department of Electrical Engineering, Heavy Industries Taxila Education City (HITEC) University, Taxila Cantt, Rawalpindi, Pakistan; from September 2011 to February 2015. At HITEC University, he was also appointed as Director Quality Assurance and Collaborations for one year term. He was lucky enough to lead a team of highly motivated Electrical Engineers at HITEC University. Before that, he served as Network and Systems Engineer at Information Technology Division, Aga Khan University Hospital (AKUH), Karachi, Pakistan; from August 1998 to September 2011. His assignments at AKUH included to lead a team of Network and Systems Engineers for IT services management: Microsoft Windows and Unix Servers, Antivirus, Software Updates, IT Policies, and User Support. He obtained his PhD in Electronics Engineering from Pakistan Navy Engineering College, Karachi-Campus, National University of Sciences and Technology (NUST), Islamabad; in January 2010. He obtained his Masters of Engineering degree in Computer Systems Engineering and Bachelor of Engineering degree in Electronics Engineering from NED University of Engineering and Technology, Karachi; in 2002 and 1999, respectively. He enjoys working in a challenging environment with cultural diversity. 

Summary of Post PhD Continued Learning, Research, and Development Efforts: After PhD, Dr. Tariq Javid learned Software Defined Networking and successfully used it for Layer 2 firewall simulation. Later, he learned Fuzzy Control Systems and successfully used concepts for medical image enhancement application. Currently, he is working on an IoT device with camera and biosensors to develop real-time applications. His PhD students are working on compressed sensing for spatiotemporal enhanced dynamic magnetic resonance image reconstruction and scaleable context aware agricultural ontology. He is on R/Python for deep learning and the fourth industrial revolution. 

Tuesday, February 25, 2020

HU FEST Spring 2020 Semester

Monday 24 February 2020

The orientation session was held at FEST auditorium. My speech to the students of BE (BME) and BS (FST) joining Hamdard University Faculty of Engineering Sciences & Technology from spring 2020 semester have three sections: welcome to institute, developing the learning habit, and guidance after graduation. 

The welcome section of my speech emphasis on students to come with a desire to learn. The BE (BME) program is well established whereas BS (FST) has recently started. Note this semester is the second intake for the FST program. I insisted on importance of questioning and developing healthy relationship with teachers. If students somehow unable to ask question during the class, they should write their question and visit teacher office during consultation timings to get answer. Last section of my speech was about post/after graduation. I guided students to practice what they will learn in four years of their studies at Hamdard University. As the knowledge is increasing day-by-day, the habit and skill of lifelong learning should be learned and practiced.

A group photo session followed the orientation session. 

  

Sunday, February 23, 2020

Participation IEEC 2020 Conference

21-22 February 2020

The 5th International Electrical Engineering Conference on 21st and 22nd February 2020 was organized jointly by Institution of Engineers Pakistan and NED University of Engineering & Technology, Karachi. The inaugural session was held on first day at NED UET whereas technical sessions and closing ceremony were held at IEP Karachi. 

Great honor of being first presenter at the technical session. My presentation was about Hamdard University funded research project. Engr. Muhammad Shahram presented his research work during the oral presentation. Few photos from the session are posted for record. 



Monday, February 3, 2020

Hamdard University Project Exhibition & Competition 2020 at Karachi Expo Center

01 February 2020

Hamdard University organized Project Exhibition & Competition at Karachi Expo Center on 01 February 2020. The event include final year student projects and posters. The event also include 4 sessions. (1) Research Group-Discussions by Dr. Muhammad Faisal Khan, (2) Innovation Seminar by Dr. Tariq Javid, (3) Commercialization Workshop (Panel Discussion) by Engr. Tarique Haider, and (4) Entrepreneurship by Prof. Dr. Rashid Hussain.

I was responsible to lead two teams: Innovation Seminar team and Commercialization Workshop team. After my welcome address at the Innovation Seminar, following respected guest speakers enlightened the audience with their valuable experience. 

(2A) Innovation in Industry by Engr. Shahab Raza Raja, Founder, Advanced Electronics International: Definition of innovation, mindset to use national products, selection of affordable technology, patent, and standards.

(2B) Intellectual Property Protection by Ms. Tafseer Fatima, Assistant Controller Patents, IPO-Pakistan: Definition of patent, examples, emphasis on intellectual property as real estate property, introduction of IPO-Pakistan, current state of the patents maintained by the Karachi Patent Office. 

(2C) Innovation Opportunities: IoT and Emerging Technologies by Engr. Sophia Hasnain, Linked Things: Definition of IoT, integration with emerging state-of-the-art technologies, data science tools, need to learn fast, in depth technical knowledge toward product realization.

Finally, the concluding remarks by myself. 

After lunch and prayer break, Commercialization Workshop (Panel Discussion) was held.

(3A) Engr. Abdul Muqeet, Manager, AKDN Digital Health Resource Center: Identify gap, consult with experts, multi-disciplinary approach toward quality product. 

(3B) Engr. Shaheer, CEO, TechThree: Entrepreneurship, need to learn essential skills toward business startup, interaction with kids on how they solve problems, practice do it yourself approach.

(3C) Prof. Dr. Abdul Hameed Memon, Chairman, Department of Mechanical Engineering, Hamdard University: Role of higher education in producing capable human resource to solve complex problems faced by the society, industries are encouraged to liason with academics, ORIC assist in commercialization of projects. 

(3D) Engr. Prof. Dr. M. Anwaar Manzar (TI), Dean FEST & Director ORIC, Hamdard University: Selection of Final Year Projects and Industry Collaboration are important factors toward commercialization. 

I recap the session with summary points. 

At the end Prof. Dr. Shabib ul Hasan, Vice Chancellor, Hamdard University distributed shields to the guest speakers and team members; and certificates to volunteers. 















Sunday, January 19, 2020

حضرت لقمان حکیم کی بیٹے کو نصیحت

حضرت لقمان حکیم کی بیٹے کو نصیحت 

١ - ہر طرح کے شرک سے بچیں
٢ - چھوٹی نیکی اور چھوٹی برائی کو چھوٹا نہ جانیں 
٣ - صرف الله کی عبادت کریں 
٤ - نیکی کی ترغیب دیں اور برائی سے روکیں 
٥ - مصیبت پر صبر کریں 
٦ - تکبر سے بچیں
٧ - میانہ  روی اختیار کریں 
٩ - آواز کو پست رکھیں