Tuesday, September 20, 2022

XSLX-Template NodeJS

This module provides a means of generating "real" Excel reports (i.e. not CSV files) in NodeJS applications.


The basic principle is this: You create a template in Excel. This can be formatted as you wish, contain formulae etc. In this file, you put placeholders using a specific syntax (see below). In code, you build a map of placeholders to values and then load the template, substitute the placeholders for the relevant values, and generate a new .xlsx file that you can then serve to the user.


Placeholders are inserted in cells in a spreadsheet. It does not matter how those cells are formatted, so e.g. it is OK to insert a placeholder (which is text content) into a cell formatted as a number or currecy or date, if you expect the placeholder to resolve to a number or currency or date.


Scalars

Simple placholders take the format ${name}. Here, name is the name of a key in the placeholders map. The value of this placholder here should be a scalar, i.e. not an array or object. The placeholder may appear on its own in a cell, or as part of a text string. For example:

| Extracted on: | ${extractDate} |

Tables

Finally, you can build tables made up of multiple rows. In this case, each placeholder should be prefixed by table: and contain both the name of the placeholder variable (a list of objects) and a key (in each object in the list). For example:


| Name                 | Age                 |

| ${table:people.name} | ${table:people.age} |

https://www.npmjs.com/package/xlsx-template

Monday, September 19, 2022

AI/ML ReduceLROnPlateau Keras

Reduce learning rate when a metric has stopped improving.

Models often benefit from reducing the learning rate by a factor of 2-10 once learning stagnates. This callback monitors a quantity and if no improvement is seen for a 'patience' number of epochs, the learning rate is reduced.


tf.keras.callbacks.ReduceLROnPlateau(

    monitor="val_loss",

    factor=0.1,

    patience=10,

    verbose=0,

    mode="auto",

    min_delta=0.0001,

    cooldown=0,

    min_lr=0,

    **kwargs

)


reduce_lr = ReduceLROnPlateau(monitor='val_loss', factor=0.2,

                              patience=5, min_lr=0.001)


model.fit(X_train, Y_train, callbacks=[reduce_lr])


monitor: quantity to be monitored.

factor: factor by which the learning rate will be reduced. new_lr = lr * factor.

patience: number of epochs with no improvement after which learning rate will be reduced.

verbose: int. 0: quiet, 1: update messages.

mode: one of {'auto', 'min', 'max'}. In 'min' mode, the learning rate will be reduced when the quantity monitored has stopped decreasing; in 'max' mode it will be reduced when the quantity monitored has stopped increasing; in 'auto' mode, the direction is automatically inferred from the name of the monitored quantity.

min_delta: threshold for measuring the new optimum, to only focus on significant changes.

cooldown: number of epochs to wait before resuming normal operation after lr has been reduced.

min_lr: lower bound on the learning rate.



References:

https://keras.io/api/callbacks/reduce_lr_on_plateau/


AI/ML Python warpaffine() functionality

 OpenCV provides two transformation functions, cv2.warpAffine and cv2.warpPerspective, with which you can have all kinds of transformations. cv2.warpAffine takes a 2x3 transformation matrix while cv2.warpPerspective takes a 3x3 transformation matrix as input.

Scaling is just resizing of the image. OpenCV comes with a function cv2.resize() for this purpose. The size of the image can be specified manually, or you can specify the scaling factor. Different interpolation methods are used. Preferable interpolation methods are cv2.INTER_AREA for shrinking and cv2.INTER_CUBIC (slow) & cv2.INTER_LINEAR for zooming. By default, interpolation method used is cv2.INTER_LINEAR for all resizing purposes. You can resize an input image either of following methods:

Translation is the shifting of object’s location. If you know the shift in (x,y) direction, let it be (t_x,t_y), you can create the transformation matrix \textbf{M} as follows:

You can take make it into a Numpy array of type np.float32 and pass it into cv2.warpAffine() function. See below example for a shift of (100,50):

import cv2

import numpy as np


img = cv2.imread('messi5.jpg',0)

rows,cols = img.shape


M = np.float32([[1,0,100],[0,1,50]])

dst = cv2.warpAffine(img,M,(cols,rows))


cv2.imshow('img',dst)

cv2.waitKey(0)

cv2.destroyAllWindows()




In affine transformation, all parallel lines in the original image will still be parallel in the output image. To find the transformation matrix, we need three points from input image and their corresponding locations in output image. Then cv2.getAffineTransform will create a 2x3 matrix which is to be passed to cv2.warpAffine.


img = cv2.imread('drawing.png')

rows,cols,ch = img.shape


pts1 = np.float32([[50,50],[200,50],[50,200]])

pts2 = np.float32([[10,100],[200,50],[100,250]])


M = cv2.getAffineTransform(pts1,pts2)


dst = cv2.warpAffine(img,M,(cols,rows))


plt.subplot(121),plt.imshow(img),plt.title('Input')

plt.subplot(122),plt.imshow(dst),plt.title('Output')

plt.show()


For perspective transformation, you need a 3x3 transformation matrix. Straight lines will remain straight even after the transformation. To find this transformation matrix, you need 4 points on the input image and corresponding points on the output image. Among these 4 points, 3 of them should not be collinear. Then transformation matrix can be found by the function cv2.getPerspectiveTransform. Then apply cv2.warpPerspective with this 3x3 transformation matrix.




img = cv2.imread('sudokusmall.png')

rows,cols,ch = img.shape


pts1 = np.float32([[56,65],[368,52],[28,387],[389,390]])

pts2 = np.float32([[0,0],[300,0],[0,300],[300,300]])


M = cv2.getPerspectiveTransform(pts1,pts2)


dst = cv2.warpPerspective(img,M,(300,300))


plt.subplot(121),plt.imshow(img),plt.title('Input')

plt.subplot(122),plt.imshow(dst),plt.title('Output')

plt.show()

references:

https://opencv24-python-tutorials.readthedocs.io/en/latest/py_tutorials/py_imgproc/py_geometric_transformations/py_geometric_transformations.html


AI/ML - What is ABC and ABCMeta in Python

abc.ABC basically just an extra layer over metaclass=abc.ABCMeta. i.e abc.ABC implicitly defines the metaclass for us.

(Source: https://hg.python.org/cpython/file/3.4/Lib/abc.py#l234)

class ABC(metaclass=ABCMeta):

    """Helper class that provides a standard way to create an ABC using

    inheritance.

    """

    pass

The only difference is that in the former case you need a simple inheritance and in the latter you need to specify the metaclass.

From What's new in Python 3.4(emphasis mine):

New class ABC has ABCMeta as its meta class. Using ABC as a base class has essentially the same effect as specifying metaclass=abc.ABCMeta, but is simpler to type and easier to read.

Thursday, September 15, 2022

Ubuntu how to check if DHCP server service is running


references: 

https://serverfault.com/questions/171744/command-line-program-to-test-dhcp-service 

Wednesday, September 14, 2022

Ubuntu how to check if package is installed

dpkg -l | grep dhcp

The above command will help to search for package that has name hdcp 

result will be something like below 


ii  isc-dhcp-client                            4.4.1-2.1ubuntu5.20.04.3            amd64        DHCP client for automatically obtaining an IP address

ii  isc-dhcp-common                            4.4.1-2.1ubuntu5.20.04.3            amd64        common manpages relevant to all of the isc-dhcp packages

Now to list all packages below can be used

dpkg -l | less 

references:
https://askubuntu.com/questions/423355/how-do-i-check-if-a-package-is-installed-on-my-server 

Tuesday, September 13, 2022

How DHCP works?

Dynamic Host Configuration Protocol (DHCP) is a network service for automatically assigning IP Addresses to clients on a network. It follows a server-client architecture where the client requests a DHCP Server to get an IP Address. Most routers have a DHCP server built-in but we can use our own DHCP Server too.


When the computer boots up it doesn’t have an IP Address (Assuming it doesn’t have static IP Addressing configured, which most of the machines don’t have). It sends a broadcast (on the MAC Address with all F’s) called a DHCP Discover. DHCP Servers are designed to respond to such broadcasts.


They then send unicast traffic known as the DHCP Offer back to the requesting client. This DHCP Offer typically contains the Assigned IP Address, the Default Gateway’s IP Address, and the DNS Server’s IP Address.


The client on receiving the Offer sends a DHCP Request to the DHCP Server acknowledging that it has accepted the information given to it by the server.


DHCP Servers keep a record of the assigned IP Addresses to prevent double assignment or IP Address Collisions.


Since DHCP Servers respond to broadcast, they must be present on the local network and there shouldn’t be more than 1 DHCP Server on a local network.


Allocation Methods for DHCP

Following are the two allocation methods for a DHCP Server:


Manual: In this method, the IP Address is given on the basis of the MAC Address. This ensures that a particular machine gets a fixed IP Address as its IP Address is then tied to its MAC Address. The DHCP Server sends a constant configuration to the client depending on its MAC Address in this type of allocation.

Automatic: In this method, the IP Addresses are assigned automatically by the DHCP Server on a first-come, first-served basis from a pool of addresses. It can be further divided into two categories based on the Lease Time – The time for which an IP Address is assigned to a client.

Fixed Lease Time: When a DHCP client is no longer on the network for a specified period, the configuration is expired and released back to the address pool for use by other DHCP Clients. The client has to renegotiate to keep the previous IP Address.

Infinite Lease Time: This has the effect of permanently assigning an IP Address to a client.



references

https://www.linuxfordevices.com/tutorials/ubuntu/dhcp-server-on-ubuntu