Tuesday, April 23, 2019

Lower probability signifies higher information. How come ?

"More unlikely that X would equal x , the more informative would be the message." Is this sounding a bit weird ? Well, its not !

It can be understood with an easy logic that if the no. of outcomes of an event is less, one needs to be more confident in selecting from that lot. More surety, symbolizes more information.

For e.g. in a basket of 12 fruits,there are 3 oranges and 9 apples.
Then,
P(orange) = 3/12
P(apple)=9/12

If I am selecting or getting an orange, that means I need to have more information at hand so that I can pick an item which was more rare or less probable. Choosing an apple means, I can be a bit liberal than in oranges case and I have some room for mistake, hence suggests lesser information than previous case.

Tuesday, April 09, 2019

Hey R, your sorting looks a little off. Why so ?

Sorting strings looks very simple as like most of the other languages it comes as a built-in function to take care of this. But it gets complex, when you don't get what you expect. For example:

> sort(c("app","A","a","Az","APP","AP","aaaa",1,1.01,1.9,.9,"0.9"))

[1] "0.9" "0.9" "1" "1.01" "1.9" "a" "A" "aaaa" "AP" "app" "APP" "Az"

Lets understand the output, the digits looks good in order as we normally expect.
"a" is smaller than "A" and then followed by "aaaa", ok looks good.
"AP" is smaller than "app" ? How is this ?
"aap" is smaller than "APP",this is ok inline with "a" < "A" but "Az" at the end, why?

Its all boils down to encoding that R is using in your setup. You can easily check this up with

>> Sys.getlocale("LC_COLLATE")
[1] "en_US.UTF-8"

This is Ubuntu 17.04 / RStudio default install / version 1.1.463

The same output is seen on Windows 10/ Rstudio default install / version 1.1.453 Here, checking the LC_COLLATE value shows :

> Sys.getlocale("LC_COLLATE")

[1] "English_United States.1252"
This is getting interesting :) UTF and 1252 does more towards language sorting, whereas we are mostly looking for byte value ordering, the way machine understands and we as developers are more familiar now. Hence, choose "C" or "POSIX" as the LC_COLLATE setting.

You can set using, the command below in either OS as:
> Sys.setlocale("LC_COLLATE","C")
[1] "C"

> Sys.setlocale("LC_COLLATE","POSIX")
[1] ""
Warning message:
In Sys.setlocale("LC_COLLATE", "POSIX") :
  OS reports request to set locale to "POSIX" cannot be honored
> Sys.setlocale("LC_COLLATE","C")
[1] "C

> sort(c("app","A","a","Az","APP","AP","aaaa",1,1.01,1.9,.9,"0.9")) 
[1] "0.9" "0.9" "1" "1.01" "1.9" "A" "AP" "APP" "Az" "a" "aaaa" "app" 


Putting down all together for reference:
en_US.ISO8859-1  : 0.9 0.9 1 1.01 1.9 a A aaaa AP app APP Az
en_US.UTF-8         : 0.9 0.9 1 1.01 1.9 a A aaaa AP app APP Az
C                           : 0.9 0.9 1 1.01 1.9 A AP APP Az a aaaa app



** So, whenever sorting goes for a toss or behaving weird or different to your expectations, please check the locale setting. In distributed setup, at times, you need to check on both server and client to debug and be sure.

Thursday, April 04, 2019

To get excel or workbook file sheet names using R xlsx package

Load your workbook or excel file, in my case e.g. name of excel file is "input_4_r.xlsx"

> wb<-loadWorkbook("input_4_r.xlsx")
see the list of files, here it shows 2 sheets in my example case in my example, I have not named first sheet and kept the default but 2nd sheet, I named as "name city" and hence the output below
> getSheets(wb)
$Sheet1
[1] "Java-Object{Name: /xl/worksheets/sheet1.xml - Content Type: application/vnd.openxmlformats-officedocument.spreadsheetml.worksheet+xml}"

$`name city`
[1] "Java-Object{Name: /xl/worksheets/sheet2.xml - Content Type: application/vnd.openxmlformats-officedocument.spreadsheetml.worksheet+xml}"
you can see the names of sheetnames as below
> names(getSheets(wb))
[1] "Sheet1"    "name city"
to get the name of specific index of sheet, e.g. passing [2] in my case for 2nd sheet
> names(getSheets(wb))[2]
[1] "name city"
*** Assumption for above is xlsx package is installed and loaded in R

Tuesday, April 25, 2017

No network after upgrade from 16.04 to 16.10 || 17.04 to 17.10

This is a bug that hits the linux system that's been upgraded from Ubuntu 16.04  ( Xenial ) to 16.10 ( Yakkety ).

Quick fix:

>> sudo touch /etc/NetworkManager/conf.d/10-globally-managed-devices.conf
>> sudo service network-manager restart

Or upgrade to the latest version of Network-Manager package on Yakkety

>> sudo apt-get install network-manager
>> sudo service network-manager restart 


Now, why this bug ?? ( Its very interesting )

Seems a simple typo introduced this bug :)

 if dpkg --compare-versions "$2" le-nl "1.2.2-0ubuntu4"; then
  mkdir -p /etc/NetworkManager/conf.d || true
  # for old versions, override the global config with a null config
  touch /etc/NetworkManager/conf.d/10-globally-managed-devices.conf

le-nl should have been ge-nl ! All new installs need this override, not older ones :)


Upgrading from 17.04 (Zesty Zapus) to 17.10 (Artful Aardvark) again broke by Ethernet LAN connection, after trying out the above solution, which did not work. The solution described below
http://iiit.blogspot.in/2012/06/ubuntu-ethernet-controller-problem-with.html
worked.  I am a happy man now, though again I had to waste couple of hours debugging this issue.

Sunday, July 05, 2015

scala 2.9.2 & Oracle Java compatability

Currently the documentation on scala site reads scala 2.9.2 needs Java 1.6 or higher. But I see that only Oracle Java 1.6 and 1.7 is working out-of-the-box with scala 2.9.2.

Case 1:
While trying to setup scala 2.9.2 on Ubuntu 15.04 using Oracle Java 1.9 (1.9.0-ea), the installation goes on fine and it does not complain.

Oracle Java version used:
java version "1.9.0-ea"
Java(TM) SE Runtime Environment (build 1.9.0-ea-b66)
Java HotSpot(TM) 64-Bit Server VM (build 1.9.0-ea-b66, mixed mode)

But when you try to invoke scala shell, you might see error as:

scala> [init] error: error while loading package, Missing dependency 'class java.lang.Object', required by /usr/share/java/scala-library.jar(scala/package.class)

Failed to initialize compiler: class java.lang.String not found.
** Note that as of 2.8 scala does not assume use of the java classpath.
** For the old behavior pass -usejavacp to scala, or if using a Settings
** object programatically, settings.usejavacp.value = true.


Case 2:
Later trying to use Oracle Java 1.8,

Oracle Java version used:
java version "1.8.0_45"
Java(TM) SE Runtime Environment (build 1.8.0_45-b14)
Java HotSpot(TM) 64-Bit Server VM (build 25.45-b02, mixed mode)

Invoking scala shell gives error as:

scala> [init] error: error while loading AnnotatedElement, class file '/usr/lib/jvm/java-8-oracle/jre/lib/rt.jar(java/lang/reflect/AnnotatedElement.class)' is broken
(bad constant pool tag 18 at byte 76)


Case 3:
Using Oracle Java version 1.7,

Oracle Java version used:
java version "1.7.0_80"
Java(TM) SE Runtime Environment (build 1.7.0_80-b15)
Java HotSpot(TM) 64-Bit Server VM (build 24.80-b11, mixed mode)

Invoking scala shell and compiling & running scala programs goes fine. 

Case 4:
Using Oracle Java version 1.6,

Oracle Java version used:
java version "1.6.0_45"
Java(TM) SE Runtime Environment (build 1.6.0_45-b06)
Java HotSpot(TM) 64-Bit Server VM (build 20.45-b01, mixed mode)

Invoking scala shell and compiling & running scala programs goes fine. 


Labels: , , , , ,

Saturday, June 02, 2012

Ubuntu 11.10 ethernet controller problem with e1000e

Net connection is not working with Ubuntu 11.10, there may be various reasons. But for me it was the presence of 2 ethernet controllers.

One was : 
Ethernet controller [0200]: Intel Corporation 82566DC Gigabit Network Connection [8086:104b] (rev 02)
    Subsystem: Intel Corporation Device [8086:d701]
    Kernel driver in use: e1000e

And other one was :
Ethernet controller [0200]: Realtek Semiconductor Co., Ltd. RTL-8139/8139C/8139C+ [10ec:8139] (rev 10)
    Subsystem: Realtek Semiconductor Co., Ltd. RTL-8139/8139C/8139C+ [10ec:8139]
    Kernel driver in use: 8139too

So though I was trying to use Realtek as my primary connectivity, everytime I use to restart my network service it would work for sometime (few secs) and then I would loose the connection.

To fix this I simply removed the modules for the Intel card which the Kernel driver used and hence the ubuntu kernel driver could attach to only one ethernet card i.e. Realtek.

Steps to follow:
1) sudo lshw -C network

See if you are really facing the same issue of having multiple cards. This command will list out the cards in use.

2)  lspci -nnk | grep -iA2 net

Now after confirming that you really got multiple drivers, see the kernel modules which this command will display.

3)
sudo rmmod e1000e   

sudo rm -rf  /lib/modules/3.0.0-12-generic/kernel/drivers/net/e1000e
or
sudo rm -rf /lib/modules/{kernel version}/kernel/drivers/net/e1000e/


This will remove the kernel module you don't need which was in my case Intel card.

4)  sudo /etc/init.d/networking restart

Now restart the network service.
(* Errors are ok , if it says 1 ethernet failed to start)


You should be able to connect to internet.

Labels: , , , ,

Tuesday, July 06, 2010

RealPlayer "Unable to download"

Incase you have installed RealPlayer Splus and are facing with "Unable to download" error while trying to download videos online. Please read on.

I am not sure as to what's the fix for the windows/firefox option and why trying to press "Download This Video" displays gives this error "Unable to download".

But the workaround is using RealPlayer to download the online videos.
Try:
File->Open-> "Enter the web address/ the youtube address" ->OK

When the webpage opens inside the RealPlayer window, you see the "Download This Video" here too. Just Click this and it will download the video.

Labels: , , , ,