Wednesday, June 6, 2018

iOS 12 Machine Learning

Apple introduced Core ML2. This said to enable apps to be built with intelligence.

In addition to supporting extensive deep learning with over 30 layer types, it also supports standard models such as tree ensembles, SVMs, and generalized linear models. Because it’s built on top of low level technologies like Metal and Accelerate, Core ML seamlessly takes advantage of the CPU and GPU to provide maximum performance and efficiency. You can run machine learning models on the device so data doesn't need to leave the device to be analyzed.

Machine learning includes vision APIs too. Supported features include face tracking, face detection, landmarks, text detection, rectangle detection, barcode detection, object tracking, and image registration.

The Natural Language framework is a new framework apps can use to analyze natural language text and deduce its language-specific metadata. Apps can use this framework with Create ML to train and deploy custom NLP models.

Core ML work with Core ML Models, Apple has provided tools to create new models or use some of the existing ones

references:
https://developer.apple.com/machine-learning/

R - A language and environment for statistical computing!

R is a language and environment for statistical computing and graphics. It is a GNU project which is similar to the S language and environment which was developed at Bell Laboratories (formerly AT&T, now Lucent Technologies) by John Chambers and colleagues. R can be considered as a different implementation of S. There are some important differences, but much code written for S runs unaltered under R.

R is an integrated suite of software facilities for data manipulation, calculation and graphical display. It includes

an effective data handling and storage facility,
a suite of operators for calculations on arrays, in particular matrices,
a large, coherent, integrated collection of intermediate tools for data analysis,
graphical facilities for data analysis and display either on-screen or on hardcopy, and
a well-developed, simple and effective programming language which includes conditionals, loops, user-defined recursive functions and input and output facilities.

R, like S, is designed around a true computer language, and it allows users to add additional functionality by defining new functions. Much of the system is itself written in the R dialect of S, which makes it easy for users to follow the algorithmic choices made. For computationally-intensive tasks, C, C++ and Fortran code can be linked and called at run time. Advanced users can write C code to manipulate R objects directly.

references
https://www.r-project.org/about.html

Sunday, June 3, 2018

Using Room Data Storage in Android

the database is accessed via an abstract class that is extended from RoomDatabase.
Below code can open the database and give a callback

There are three main components in this

1. Database access
2. Creating an entity
3. Mapping a Java object to SQL query (i.e. as a Dao)
4. Accessing the database via Repository

INSTANCE = Room.databaseBuilder(context.getApplicationContext(),
                            WordRoomDatabase.class, "word_database")
                            // Wipes and rebuilds instead of migrating if no Migration object.
                            // Migration is not part of this codelab.
                            .fallbackToDestructiveMigration()
                            .addCallback(sRoomDatabaseCallback)
                            .build();

 private static RoomDatabase.Callback sRoomDatabaseCallback = new RoomDatabase.Callback(){

        @Override
        public void onOpen (@NonNull SupportSQLiteDatabase db){
            super.onOpen(db);
            // If you want to keep the data through app restarts,
            // comment out the following line.
            new PopulateDbAsync(INSTANCE).execute();
        }
    };

like shown above, once the database is opened, Database is populated to read the contents from it.

Now how do we map a java object to an SQL query? This is done using the annotations. Below is a sample Dao

@Dao marks a class as data access object.


@Dao
public interface WordDao {

    // LiveData is a data holder class that can be observed within a given lifecycle.
    // Always holds/caches latest version of data. Notifies its active observers when the
    // data has changed. Since we are getting all the contents of the database,
    // we are notified whenever any of the database contents have changed.
    @Query("SELECT * from word_table ORDER BY word ASC")
    LiveData> getAlphabetizedWords();

    // We do not need a conflict strategy, because the word is our primary key, and you cannot
    // add two items with the same primary key to the database. If the table has more than one
    // column, you can use @Insert(onConflict = OnConflictStrategy.REPLACE) to update a row.
    @Insert
    void insert(Word word);

    @Query("DELETE FROM word_table")
    void deleteAll();
}


references:
https://developer.android.com/training/data-storage/room/

Friday, June 1, 2018

Javascript - Nesting function is good?

Nay! .. so far my reading goes, it is not . The reason is that in Javascript, every function is a value. with nested functions,

In a setup like below,

function foo(a, b) {
    function bar() {
        return a + b;
    }
    return bar();
}

foo(1, 2);

An outer function, foo(), contains an inner function, bar(), and calls that inner function to do work. Many developers forget that functions are values in JavaScript. When you declare a function in your code, the JavaScript engine creates a corresponding function object—a value that can be assigned to a variable or passed to another function. The act of creating a function object resembles that of any other type of value; the JavaScript engine doesn't create it until it needs to. So in the case of the above code, the JavaScript engine doesn't create the inner bar() function until foo() executes. When foo() exits, the bar() function object is destroyed.

The fact that foo() has a name implies it will be called multiple times throughout the application. While one execution of foo() would be considered OK, subsequent calls cause unnecessary work for the JavaScript engine because it has to recreate a bar() function object for every foo() execution. So, if you call foo() 100 times in an application, the JavaScript engine has to create and destroy 100 bar() function objects. Big deal, right? The engine has to create other local variables within a function every time it's called, so why care about functions?

references:
https://code.tutsplus.com/tutorials/stop-nesting-functions-but-not-all-of-them--net-22315

EJS partials and variables

Partials and variables allows one to define templates. for e.g. if an application want to send out an email in a predefined nice html but with a dynamically populated data, a template html can be defined inside which there can be partials or variables and do the below to generate a proper output html.

var template = ejs.compile(html);
var htmlToSend = template(dataObj.htmlData);

the difference between partial and variable is subtle in terms of syntax.

<%- %> => partial
<%= %> => variable

partials allows one to specify something that is not escaped. For e.g if the intention is to include a snippet, then snippet can be used. but if the intention is to replace only the variable, variable can be used.

references:
https://coligo.io/templating-node-and-express-apps-with-ejs/

Thursday, May 31, 2018

What is an NAL Unit and Coded video Sequence in Video RTP

A set of NAL units in a specified form is referred to as an access unit. The decoding of each access unit results in one decoded picture. Each access unit contains a set of VCL NAL units that together compose a primary coded picture. It may also be prefixed with an access unit delimiter to aid in locating the start of the access unit. Some supplemental enhancement information containing data such as picture timing information may also precede the primary coded picture. The primary coded picture consists of a set of VCL NAL units consisting of slices or slice data partitions that represent the samples of the video picture. Following the primary coded picture may be some additional VCL NAL units that contain redundant representations of areas of the same video picture. These are referred to as redundant coded pictures, and are available for use by a decoder in recovering from loss or corruption of the data in the primary coded pictures. Decoders are not required to decode redundant coded pictures if they are present. Finally, if the coded picture is the last picture of a coded video sequence (a sequence of pictures that is independently decodable and uses only one sequence parameter set), an end of sequence NAL unit may be present to indicate the end of the sequence; and if the coded picture is the last coded picture in the entire NAL unit stream, an end of stream NAL unit may be present to indicate that the stream is ending.



A coded video sequence consists of a series of access units that are sequential in the NAL unit stream and use only one sequence parameter set. Each coded video sequence can be decoded independantly of any other coded video sequence, given the necessary parameter set information, which may be conveyed in-band or out-of-band. At the beginning of a coded video sequence is an insteantaneous decoding refresh (IDR) access unit. An IDR access unit contains and intra picture which is coded picture that can be decoded without decoding any previous pictures in the NAL unit stream, and the presence of an IDR access unit indicates that no subsequent picture in the stream will require reference to pictures prior to the intra picture it contains intruder to be decoded. A NAL unit stream may contain one or more coded video sequence.

references:
https://en.wikipedia.org/wiki/Network_Abstraction_Layer#VCL_and_Non-VCL_NAL_Units

What is purpose of NetworkManager

This class queries about the state of network connectivity and is responsible also to notify apps when the network state changes. 

Monitor network connections (Wi-Fi, GPRS, UMTS, etc.
Send broadcast intents when network connectivity changes
Attempt to "fail over" to another network when connectivity to a network is lost

Provide an API that allows applications to query the coarse-grained or fine-grained state of the available networks
Provide an API that allows applications to request and select networks for their data traffic
void requestNetwork (NetworkRequest request, 
                ConnectivityManager.NetworkCallback networkCallback)
API is the main attraction of this class. 
Request a network to satisfy a set of NetworkCapabilities.

This NetworkRequest will live until released via unregisterNetworkCallback(NetworkCallback) or the calling application exits. A version of the method which takes a timeout is requestNetwork(NetworkRequest, NetworkCallback, int). Status of the request can be followed by listening to the various callbacks described in ConnectivityManager.NetworkCallback. The Network can be used to direct traffic to the network. 

Once the requested network is available, we can bindProcessToNetwork inorder to rooute the traffic via that network. This is especially useful when the WiFi access point is a captive portal. 


References:

https://developer.android.com/reference/android/net/ConnectivityManager#requestNetwork(android.net.NetworkRequest,%20android.net.ConnectivityManager.NetworkCallback)



https://android-developers.googleblog.com/2016/07/connecting-your-app-to-wi-fi-device.html