Carbon Footprint of Computing Resources - A Thought

 The carbon footprint of the computing device also taken into consideration while estimation of cost. 

(Energy) Carbon footprint of the hardware components   + 

(Energy)Depreciation of the hardware components + 

(Energy) Power consumption of the hardware components +

(Manpower) Effort or intelligence used to built software

Total Cost of Software build using Hardware = ECF + ED + EP + MS

Only Entity in the universe which will not use energy to destroy the Creation - Software

Raw Materials for a Program - A Thought

Every creation in the universe requires energy and raw materials. The product is a hardware or software components, we look into software components as cost or effort taken. The energy or raw materials used not in factor of cost or software cost is not accounted for the process, which is against the laws of the nature.

While developing the software hardware components are used, energy is used, and effort or manpower is used.  Every model discuss about the effort or intelligence used in the software.

The depreciation of the hardware components for example [Raw Material]

Personal Computer for 20K with life of 10 years

Cost of personal computer for one year is 2K

The span of software project to complete is 6 Months

The cost of personal computer for 6 Months is 1K

The power consumption of the hardware components for example [Energy]

Personal Computer uses 200 W/hour

Every day the personal computer used for 10 hours

The power consumption of software project in term of 6 Months is 12KW

The effort or manpower is calculated by well known cost estimation models in the software engineering. Here one form is converted to another form by part by part.

Tic-Tac-Toe game in Python


from tkinter import *
import numpy as np
size_of_board = 600
symbol_size = (size_of_board / 3 - size_of_board / 8) / 2
symbol_thickness = 30
symbol_X_color = '#EE4199'
symbol_O_color = '#0999CF'
Green_color = '#7BC043'
class Tic_Tac_Toe():
def __init__(self):
self.window = Tk()
self.window.title('Tic-Tac-Toe')
self.canvas = Canvas(self.window, width=size_of_board, height=size_of_board)
self.canvas.pack()
self.window.bind('<Button-1>', self.click)
self.initialize_board()
self.player_X_turns = True
self.board_status = np.zeros(shape=(3, 3))
self.player_X_starts = True
self.reset_board = False
self.gameover = False
self.tie = False
self.X_wins = False
self.O_wins = False
self.X_score = 0
self.O_score = 0
self.tie_score = 0

def mainloop(self):
self.window.mainloop()

def initialize_board(self):
for i in range(2):
self.canvas.create_line((i + 1) * size_of_board / 3, 0, (i + 1) * size_of_board / 3, size_of_board)

for i in range(2):
self.canvas.create_line(0, (i + 1) * size_of_board / 3, size_of_board, (i + 1) * size_of_board / 3)

def play_again(self):
self.initialize_board()
self.player_X_starts = not self.player_X_starts
self.player_X_turns = self.player_X_starts
self.board_status = np.zeros(shape=(3, 3))

def draw_O(self, logical_position):
logical_position = np.array(logical_position)
grid_position = self.convert_logical_to_grid_position(logical_position)
self.canvas.create_oval(grid_position[0] - symbol_size, grid_position[1] - symbol_size,
grid_position[0] + symbol_size, grid_position[1] + symbol_size, width=symbol_thickness,
outline=symbol_O_color)

def draw_X(self, logical_position):
grid_position = self.convert_logical_to_grid_position(logical_position)
self.canvas.create_line(grid_position[0] - symbol_size, grid_position[1] - symbol_size,
grid_position[0] + symbol_size, grid_position[1] + symbol_size, width=symbol_thickness,
fill=symbol_X_color)
self.canvas.create_line(grid_position[0] - symbol_size, grid_position[1] + symbol_size,
grid_position[0] + symbol_size, grid_position[1] - symbol_size, width=symbol_thickness,
fill=symbol_X_color)

def display_gameover(self):

if self.X_wins:
self.X_score += 1
text = 'Winner: Player 1 (X)'
color = symbol_X_color
elif self.O_wins:
self.O_score += 1
text = 'Winner: Player 2 (O)'
color = symbol_O_color
else:
self.tie_score += 1
text = 'Its a tie'
color = 'gray'

self.canvas.delete("all")
self.canvas.create_text(size_of_board / 2, size_of_board / 3, font="cmr 60 bold", fill=color, text=text)

score_text = 'Scores \n'
self.canvas.create_text(size_of_board / 2, 5 * size_of_board / 8, font="cmr 40 bold", fill=Green_color,
text=score_text)

score_text = 'Player 1 (X) : ' + str(self.X_score) + '\n'
score_text += 'Player 2 (O): ' + str(self.O_score) + '\n'
score_text += 'Tie : ' + str(self.tie_score)
self.canvas.create_text(size_of_board / 2, 3 * size_of_board / 4, font="cmr 30 bold", fill=Green_color,
text=score_text)
self.reset_board = True

score_text = 'Click to play again \n'
self.canvas.create_text(size_of_board / 2, 15 * size_of_board / 16, font="cmr 20 bold", fill="gray",
text=score_text)

def convert_logical_to_grid_position(self, logical_position):
logical_position = np.array(logical_position, dtype=int)
return (size_of_board / 3) * logical_position + size_of_board / 6

def convert_grid_to_logical_position(self, grid_position):
grid_position = np.array(grid_position)
return np.array(grid_position // (size_of_board / 3), dtype=int)

def is_grid_occupied(self, logical_position):
if self.board_status[logical_position[0]][logical_position[1]] == 0:
return False
else:
return True

def is_winner(self, player):

player = -1 if player == 'X' else 1
for i in range(3):
if self.board_status[i][0] == self.board_status[i][1] == self.board_status[i][2] == player:
return True
if self.board_status[0][i] == self.board_status[1][i] == self.board_status[2][i] == player:
return True

if self.board_status[0][0] == self.board_status[1][1] == self.board_status[2][2] == player:
return True

if self.board_status[0][2] == self.board_status[1][1] == self.board_status[2][0] == player:
return True

return False

def is_tie(self):

r, c = np.where(self.board_status == 0)
tie = False
if len(r) == 0:
tie = True

return tie

def is_gameover(self):
# Either someone wins or all grid occupied
self.X_wins = self.is_winner('X')
if not self.X_wins:
self.O_wins = self.is_winner('O')

if not self.O_wins:
self.tie = self.is_tie()

gameover = self.X_wins or self.O_wins or self.tie

if self.X_wins:
print('X wins')
if self.O_wins:
print('O wins')
if self.tie:
print('Its a tie')

return gameover

def click(self, event):
grid_position = [event.x, event.y]
logical_position = self.convert_grid_to_logical_position(grid_position)

if not self.reset_board:
if self.player_X_turns:
if not self.is_grid_occupied(logical_position):
self.draw_X(logical_position)
self.board_status[logical_position[0]][logical_position[1]] = -1
self.player_X_turns = not self.player_X_turns
else:
if not self.is_grid_occupied(logical_position):
self.draw_O(logical_position)
self.board_status[logical_position[0]][logical_position[1]] = 1
self.player_X_turns = not self.player_X_turns

if self.is_gameover():
self.display_gameover()
else:
self.canvas.delete("all")
self.play_again()
self.reset_board = False

game_instance = Tic_Tac_Toe()
game_instance.mainloop()


Program for time Comparsion difference between Python Lists and Numpy Arrays



Creation time for adding  two python list is the order of 500 compared to the adding two numpy array. 

Time metric in seconds 

Creation time for Python List : 0.310 

Creation time for Numpy Array: 0.002



import time
import numpy as np
size = 100000

def python_method():
    t1 = time.time()
    X = range(size)
    Y = range(size)
    Z = [X[i] + Y[i] for i in range(len(X)) ]
    return time.time() - t1

def numpy_method():
    t1 = time.time()
    X = np.arange(size)
    Y = np.arange(size)
    Z = X + Y
    return time.time() - t1

t1 = python_method()
t2 = numpy_method()
print("Python",t1,"Numpy",t2)


Python program to find distance measure - Hamming ,Euclidean , Manhattan, Minkowski

# Calculating distance between bit strings

# Hamming Distance
def hamming_distance(a, b):
return sum(abs(e1 - e2) for e1, e2 in zip(a, b)) / len(a)
r1 = [1, 0, 0, 0, 0, 0, 1]
r2 = [1, 0, 0, 0, 0, 1, 0]
dist = hamming_distance(r1, r2)
print(dist)

#Euclidean Distance
from math import sqrt
def euclidean_distance(a, b):
return sqrt(sum((e1-e2)**2 for e1, e2 in zip(a,b)))
r1 = [1, 0, 0, 0, 0, 0, 1]
r2 = [1, 0, 0, 0, 0, 1, 0]
dist = euclidean_distance(r1, r2)
print(dist)

#Manhattan Distance
from math import sqrt
def manhattan_distance(a, b):
return sum(abs(e1-e2) for e1, e2 in zip(a,b))
r1 = [1, 0, 0, 0, 0, 0, 1]
r2 = [1, 0, 0, 0, 0, 1, 0]
dist = manhattan_distance(r1, r2)
print(dist)

#Minkowski Distance
from math import sqrt
def minkowski_distance(a, b, p):
return sum(abs(e1-e2)**p for e1, e2 in zip(a,b))**(1/p)
r1 = [1, 0, 0, 0, 0, 0, 1]
r2 = [1, 0, 0, 0, 0, 1, 0]
dist = minkowski_distance(r1, r2, 1) #  p=1: Manhattan distance.
print(dist)
dist = minkowski_distance(r1, r2, 2) #  p=2: Euclidean distance.
print(dist)

Counter using Tkinter GUI in Python

 import tkinter as tk

counter = 0
def counter_label(label):
def count():
global counter
counter += 1
label.config(text=str(counter))
label.after(1000, count)
count()

root = tk.Tk()
root.title("Counting Seconds")
label = tk.Label(root, fg="green")
label.pack()
counter_label(label)
button = tk.Button(root, text='Stop', width=25, command=root.destroy)
button.pack()
root.mainloop()

Generate random numbers with normal distribution

 1.Generate normal distribution data of Size 2 × 3

   from numpy import random
   x = random.normal(size=(23))

2. Generate normal distribution with mean 250 and standard deviation 10

    from numpy import random
    x = random.normal(loc=250, scale=10, size=(23))

Python Random Module

 import random

random.random() 

random.uniform(1, 10)

random.randint(1, 10)

random.randrange(0, 101, 2)

random.choice('abcdefghij') 

items = [1, 2, 3, 4, 5, 6, 7]

random.shuffle(items)

items

random.sample([1, 2, 3, 4, 5],  3)

Python may get pattern matching syntax

PEP 622 -- Structural Pattern Matching is PEP proposes adding pattern matching statements  to Python in order to create more expressive ways of handling structured heterogeneous data. The authors take a holistic approach, providing both static and runtime specifications.

PsychoPy - Precise enough for psychophysics

https://www.psychopy.org/_static/psychopyLogoOnlineStrap_h480.pngPsychoPy is a package for the generation of experiments for neuroscience and experimental psychology.It is designed to allow the presentation of stimuli and collection of data for a wide range of neuroscience, psychology, and psychophysical experiments

Python program to find number of processors in your computer

The maximum parallel processes can you run on computer is based on the number of processors or cores  in your computer. To find number of processors or cores  in your computer, cpu_count() function is used.

import multiprocessing as mp
print("Number of processors: ", mp.cpu_count())

COVID19Py




A tiny Python package for easy access to up-to-date Coronavirus (COVID-19, SARS-CoV-2) cases data.

Installation - pip install COVID19Py

Usage -  To use COVID19Py, you first need to import the package and then create a new instance:

import COVID19Py
covid = COVID19Py.COVID()

Python Programming for Quantitative Economics





This website presents a set of lectures on python programming for quantitative economics, designed and written by Thomas J. Sargent and John Stachurski.

Webiste

A python program for neural network trained with backpropagation with sigmoid function

import numpy as np
def nonlin(x,deriv=False):
if(deriv==True):
return x*(1-x)
return 1/(1+np.exp(-x))
X = np.array([ [0,0,1],
[0,1,1],
[1,0,1],
[1,1,1] ])
y = np.array([[0,0,1,1]]).T
np.random.seed(1)
syn0 = 2*np.random.random((3,1)) - 1
for iter in range(10000):
l0 = X
l1 = nonlin(np.dot(l0,syn0))
l1_error = y - l1
l1_delta = l1_error * nonlin(l1,True)
syn0 += np.dot(l0.T,l1_delta)
print ("Output After Training:")
print (l1)

First Come First Serve Process scheduling using python

process = []
total_waiting_time = 0
n = int(raw_input('Enter the total no of processes: '))
for i in xrange(n):
    process.append([])
    process[i].append(raw_input('Enter process name: '))
    process[i].append(int(raw_input('Enter process arrival time : ')))
    total_waiting_time += process[i][1]
    process[i].append(int(raw_input('Enter process  burst time: ')))
    print ''

process.sort(key = lambda process:process[1])

print 'Process Name\tArrival Time\tBurst Time'
for i in xrange(n):
    print process[i][0],'\t\t',process[i][1],'\t\t',process[i][2]
   
print 'Total waiting time: ',  total_waiting_time
print 'Average waiting time: ',(total_waiting_time/n)

Python Any Where - Host, run, and code Python in the cloud



Basic plan gives you access to machines with a full Python environment already installed for free. You can develop and host your website or any other code directly from your browser without having to install software or manage your own server.

Python Any Where

Metaflow




Metaflow is a human-friendly Python library that helps scientists and engineers build and manage real-life data science projects. Metaflow was originally developed at Netflix to boost productivity of data scientists who work on a wide variety of projects from classical statistics to state-of-the-art deep learning.

pip install metaflow