Why are my identical go routines out of order? - asynchronous

I've got the following go code executing routines.
package main
import (
"fmt"
"time"
)
func count(id int) {
for i := 0; i < 10; i++ {
fmt.Println(id, ":", i)
time.Sleep(time.Millisecond * 1000)
}
}
func main() {
for i := 0; i < 10; i++ {
go count(i)
}
time.Sleep(time.Millisecond * 11000)
}
I would expect the output to be:
1 : 0
2 : 0
3 : 0
4 : 0
5 : 0
6 : 0
7 : 0
8 : 0
9 : 0
1 : 1
2 : 1
3 : 1
4 : 1
etc...
but instead, I get:
0 : 0
6 : 0
7 : 0
5 : 0
8 : 0
9 : 0
3 : 0
2 : 0
4 : 0
1 : 0
5 : 1
6 : 1
7 : 1
1 : 1
8 : 1
etc...
Why are they not in order of the original, outside for loop executing the count method? Why are some count methods getting out of sync?

When a goroutine executes is out of the control of the programmer. You have some control if you use signals via channels and sync.WaitGroup, (like having one goroutine wait until another has finished) but you cannot control the execution order of goroutines

As others already commented, goroutine is mainly aiming to concurrency. Concurrency can embrace parallelism and communication. If you want to control their order, you have to make them communicate through SIGNAL. For example, a goroutine waits for a signal from another one. Please check goroutine and channel in particular.
example: https://micknelson.wordpress.com/2012/11/14/a-tour-of-go-the-web-crawler-exercise/
packages : https://golang.org/pkg/os/signal/

Related

Defining a workflow for importing the RNA-seq count data

i am getting starting with R and i read some basics and syntax to get me started with it,
now i using miodin to define a project and a case-control study design.
library(miodin)
mp <- MiodinProject(name = "MyProject", author = "Myself", path ="." )
mshow(mp)
I have a file named "randseq"in my computer hard-disk which look like this.
ID LineA_1 LineA_2 LineA_3 LineA_4 LineA_5 LineB_1 LineB_2 LineB_3 LineB_4 LineB_5 LineB_6 LineB_7 LineB_8 LineB_9
ENSG00000000003 23 1 0 0 0 1 0 0 0 0 0 3 3 0
ENSG00000000005 0 0 0 0 0 0 0 0 0 0 0 0 0 0
ENSG00000000419 0 0 0 0 0 0 0 0 4 0 0 0 0 0
Now i want to define a workflow for importing the RNA-seq count data of that file which is in a folder named analysis_with_r, using the study design. Execute the workflow and export the dataset to the project folder. Below is my code for it
mw <- MiodinWorkflow(name = 'MyProject')
mw <- mw + downloadRepositoryData(
name = 'RNA downloader',
accession = 'randseq',
repository = '/Users/aarf/Desktop/analysis_with_r/randseq.txt',
path = 'data',
type = 'processed'
)
mw <- insert(mw,mp)
mshow(mw)
mw <- execute(mw)
saveDataFile(mp)
export(mp, 'dataset', 'randseq')
After running this code i get this error
[INFO] Module terminated with the following error [ERROR] Unknown
repository/Users/aarf/Desktop/analysis_with_r/randseq.txt
[INFO] 1 modules were not executed [STATUS] Execution finished
Can anybody tell me what am i doing wrong here?

Stacked barplot in UpSetR

I have been looking for a way of having a stacked bar plot in an upsetR graph.
I downloaded the movies data set (from here) and added a column having only two values "M" and "C".
Below, information on how I loaded the data and added the "x" column.
Edit:
m <- read.csv(system.file("extdata", "movies.csv", package = "UpSetR"),
header = T, sep = ";")
nrow(m)
[1] 3883
x<-c(rep("M", 3000), rep("C", 883))
m<-cbind(m, x)
unique(m$x)
[1] M C
This is the structure of the data frame:
str(m)
'data.frame': 3883 obs. of 22 variables:
$ Name : Factor w/ 3883 levels "$1,000,000 Duck (1971)",..: 3577 1858 1483 3718 1175 1559 3010 3548 3363 1420 ...
$ ReleaseDate: int 1995 1995 1995 1995 1995 1995 1995 1995 1995 1995 ...
$ Action : int 0 0 0 0 0 1 0 0 1 1 ...
$ Adventure : int 0 1 0 0 0 0 0 1 0 1 ...
$ Children : int 1 1 0 0 0 0 0 1 0 0 ...
$ Comedy : int 1 0 1 1 1 0 1 0 0 0 ...
$ Crime : int 0 0 0 0 0 1 0 0 0 0 ...
$ Documentary: int 0 0 0 0 0 0 0 0 0 0 ...
$ Drama : int 0 0 0 1 0 0 0 0 0 0 ...
$ Fantasy : int 0 1 0 0 0 0 0 0 0 0 ...
$ Noir : int 0 0 0 0 0 0 0 0 0 0 ...
$ Horror : int 0 0 0 0 0 0 0 0 0 0 ...
$ Musical : int 0 0 0 0 0 0 0 0 0 0 ...
$ Mystery : int 0 0 0 0 0 0 0 0 0 0 ...
$ Romance : int 0 0 1 0 0 0 1 0 0 0 ...
$ SciFi : int 0 0 0 0 0 0 0 0 0 0 ...
$ Thriller : int 0 0 0 0 0 1 0 0 0 1 ...
$ War : int 0 0 0 0 0 0 0 0 0 0 ...
$ Western : int 0 0 0 0 0 0 0 0 0 0 ...
$ AvgRating : num 4.15 3.2 3.02 2.73 3.01 3.88 3.41 3.01 2.66 3.54 ...
$ Watches : int 2077 701 478 170 296 940 458 68 102 888 ...
$ x : Factor w/ 2 levels "M","C": 1 1 1 1 1 1 1 1 1 1 ...
Now I tried to implement the stacked bar plot as follow:
upset(m,
queries = list(
list(query = elements,
params = list("x", "M"), color = "#e69f00", active = T),
list(query = elements,
params = list("x", "C"), color = "#cc79a7", active = T)))
The result looks like this:
As you can see the proportions are wrong as there should be in each bar only two colors (factor) either "M" or "C".
This issue seems to be not a trivial one, as also pointed out here.
Does anyone have an idea on how to implement this in UpsetR?
Thanks a lot
Here is a way to create an upset plot with stacked barplot, but using my ComplexUpset rather than UpSetR:
library(ComplexUpset)
movies = as.data.frame(ggplot2movies::movies)
genres = colnames(movies)[18:24]
# for simplicity of examples, only use the complete data points
movies[movies$mpaa == '', 'mpaa'] = NA
movies = na.omit(movies)
upset(
movies,
genres,
base_annotations=list(
'Intersection size'=intersection_size(
counts=FALSE,
mapping=aes(fill=mpaa)
)
),
width_ratio=0.1
)
Please see more examples in the documentation.
The Installation instructions are available on GitHub: krassowski/complex-upset (there is also a comparison to UpSetR and other packages).
I had a similar problem and found this workaround:
library("UpSetR")
m <- read.csv(system.file("extdata", "movies.csv", package = "UpSetR"),
header = T, sep = ";")
x<-c(rep("M", 2000), rep("Q", 1000), rep("C", 883))
m<-cbind(m, x)
upset(m,
queries = list(
list(query = elements,
params = list("x", c("M","Q", "C")), color = "#e69f00", active = T),
list(query = elements,
params = list("x", c("Q","C")), color = "#cc79a7", active = T),
list(query = elements,
params = list("x", "C"), color = grey(0.7), active = T)))
The problem in the original example is that every query overlays over the total bar separately and starts at y=0. Thus, the remaining black part of the bar always has the exact same height as the purple part at the bottom. The workaround is to systematically add queries of combinations of the different values the variable can take:
Start with a query and a respective color for the combination of all possible values (here c("M","Q","C") as the second parameter to params = list()).
Successively leave out one of the possible values (e.g. c("Q","C") in the first step here). The value left out will be represented by the color of the query, the last one that still included it ("M" in this example).
Continue adding queries until you have only one value left for the second parameter to params = list().
It should be possible do this programmatically for larger numbers of possible values and providing some color palette. But this remains a workaround and a native implementation of stacking the queries would be nice to have--so if you would like to see this functionality, you might consider bumping up the respective issue over at the Github repo.
Below the nice answer by #dlaehnemann but a little bit modified in order to create that list of list using a loop as well as linking wanted colors to it.
m <- read.csv(system.file("extdata", "movies.csv", package = "UpSetR"), header = T, sep = ";")
x<-c(rep("M", 2000), rep("Q", 1000), rep("C", 883))
m<-cbind(m, x)
i<-0
mylist<-list()
vectorUniqueValue <- unique(m$x)
colors = colorRampPalette(c("#332288",'#fdff00','#FF0000',"#CC6677","#88CCEE",'#36870c','#b786d2','#7c3c06',"#DDCC77",'#192194','#52cff4','#4f9c8b',"#4477AA",'#808080'))(length(vectorUniqueValue))
while ( length(vectorUniqueValue)>0 ){
i<-i+1
mylist[[i]]<-list(query = elements, params = list("x",as.character(vectorUniqueValue)), color = colors[i], active = T)
vectorUniqueValue<-vectorUniqueValue[-1]
}
upset(m, queries = mylist)
Hope it helps a bit until maybe one day someone works on the issue on github !

Error return by R predict function or underlying Rcpp

I apparently have successively used a newer R package called milr, multiple instance logistic regression. Admittedly, I do not make any claims regarding the goodness of the model. However, when I try to use the model to predict I get the error
Error in logit(cbind(1, newdata), .) : not compatible with requested type
when I call predict as follows:
miltp <- predict(milt, SQFM.te, SQFM.teb, type="bag") and
miltp <- predict(milt, SQFM.te, SQFM.teb)
However I get a NULL return when I call it as:
miltp <- predict(milt, SQFM.te, SQFM.teb, type="response") and
miltp <- predict(milt, SQFM.te, SQFM.teb, type="class")
I have tried using factors, integers and numerics, I am perplexed. My online search only yielded
Rcpp: Error: not compatible with requested type
which is not helpful for me as R and C++ is over my head. All comments are appreciated, some input info is given below I have tried some conversions
str(SQFM.te)
'data.frame': 100369 obs. of 5 variables:
$ arstmade: int 0 0 0 0 0 0 0 0 0 0 ...
$ perstop : int 0 0 0 0 0 0 0 0 0 0 ...
$ trhsloc : int 0 0 0 0 0 0 0 0 0 0 ...
$ acrept : int 0 0 0 0 0 0 0 0 0 0 ...
$ radio : int 1 1 1 1 1 1 1 1 1 1 ...
str(SQFM.teb)
int [1:100369] 3 3 3 3 3 3 3 3 3 3 ...
print(milt)
Coefficients:
intercept arstmade perstop trhsloc acrept radio
-1.69306 -0.09544 -7.95369 -0.53375 0.16506 -0.61778
Residual Deviance: Inf
BIC: Inf

Why dfs using stack is not giving correct result

See DFS image Here
I am using stack to print sequence of dfs. According to input and that image of graph, sequence is 1 2 4 8 5 6 3 7 . But My code is giving output as 1 2 4 8 7 6 5 3 . Can anyone explain how can i fix it??
Input:
8 10
1 3
1 2
2 5
2 4
3 7
3 6
4 8
5 8
6 8
7 8
Correct Output:
Sequence: 1 2 4 8 5 6 3 7
My Code :
#include <bits/stdc++.h>
using namespace std;
vector<int>edges[100];
stack<int>q;
vector<int>item;
int level[100],parent[100],visited[100],tn;
void dfs(int s)
{
int j,k,fr;
q.push(s);
level[s]=0;
for(j=1;j<=tn;j++)
{
visited[j]=0;
}
visited[s]=1;
while(!q.empty())
{
fr=q.top();
q.pop();
item.push_back(fr);
for(k=0;k<edges[fr].size();k++)
{
if(visited[edges[fr][k]]==0)
{
q.push(edges[fr][k]);
//cout<<"Pushed="<<fr<<"="<<edges[fr][k];
visited[edges[fr][k]]=1;
}
}
//cout<<endl;
}
}
int main()
{
int i,e,p,n,u,v,f,m;
cin>>tn>>e;
for(i=1;i<=e;i++)
{
cin>>u>>v;
edges[u].push_back(v);
edges[v].push_back(u);
}
dfs(1);
cout<<"Sequence="<<endl;
for(m=0;m<item.size();m++)
{
cout<<item[m];
}
return 0;
}
My Code is showing this output: 1 2 4 8 7 6 5 3
The marking of the nodes as visited in the implementation contains a bug; the function can be rewritten as follows.
void dfs(int s)
{
int j, k, fr;
q.push(s);
level[s] = 0;
for (j = 1; j <= tn; j++)
{
visited[j] = 0;
}
while (!q.empty())
{
fr = q.top();
q.pop();
if (0 == visited[fr])
{
visited[fr] = 1;
item.push_back(fr);
for (k = 0; k < edges[fr].size(); k++)
{
q.push(edges[fr][k]);
}
}
}
}
In this version, a node gets marked only if it is taken from the stack. Note that a check whether the node has been already visited is necessary, as a node on the stack might be visited by a later iteration. This implementation yields the sequence
1 2 4 8 7 3 6 5
which, however, is not the one described as desired solution. However, note that without additional tie-breaking rules, the DFS algorithm permits some ambiguity in the sequence of visits. The sequence
1 2 4 8 5 6 3 7
can be generated by pushing a neighbor with smallest id to the stack last, causing it to be visited in the next iteration.

Conditional input using read.table or readLines

I'm struggling with using readLines() and read.Table() to get a well formatted data frame in R.
I want to read files like this which are Hockey stats. I'd like to get a nicely formatted data frame, however, specifying the concrete amount of lines to read is difficult because in other files like this the number of players is different. Also, non-players, signed as #.AC, #.HC and so on, should not be read in.
I tried something like this
LINES <- 19
stats <- read.table(file=Datei, skip=11, header=FALSE, stringsAsFactors=FALSE,
encoding="UTF-8", nrows=LINES)
but as mentioned above, the value for LINES is different each time.
I also tried readLines as in this post, but had no luck with it.
Is there a way to integrate a condition in read.table, like (pseudo code)
if (first character == "AC") {
break read.table
}
Sorry if this looks strange, I don't have that much experience in scripting or coding.
Any help is appreciated, thanks a lot!
Greetz!
Your data show a couple of difficulties which should be handled in a sequence, which means you should not try to read the entire file with one command:
Read plain lines and find start and stop row
Depending on the specification of the files you read in my suggestion is to first find the the first row you actually want to read in by any indicator. So this can be a lone number which is always the same or as in my example two lines after the line "TEAM STATS". Finding the last line is then simple again by just looking for the first line containing only whitespaces after the start line:
lines <- readLines( Datei )
start <- which(lines == "TEAM STATS") + 2
end <- start + min( grep( "^\\s+$", lines[ start:length(lines) ] ) ) -2
lines <- lines[start:end]
Read the data to data.frame
In your case you meet a couple of complications:
Your header line starts with an # which is on default recognized as a comment character, ignoring the line. But even if you switch this behavior off (comment.char = "") it's not a valid column name.
If we tell read.table to split the columns along whitespaces you end up with one more column in the data, than in the header since the Player column contains white spaces in the cells. So the best is at the moment to just ignore the header line and let read.table do this with it's default behavior (comment.char = "#"). Also we let the PLAYER column be split into two and will fix this later.
You won't be able to use the first column as row.names since they are not unique.
The rows have unequal length, since the POS column is not filled everywhere.
:
tab <- read.table( text = lines[ start:end ], fill = TRUE, stringsAsFactors=FALSE )
# fix the PLAYER column
tab$V2 <- paste( tab$V2, tab$V3 )
tab <- tab[-3]
Fix the header
Just split the start line at multiple whitespaces and reset the first entry (#) by a valid column name:
colns <- strsplit( lines[start], "\\s+" )[[1]]
colns[1] <- "code"
colnames(tab) <- colns
Fix cases were "POS" was empty
This is done by finding the rows which last cell contains NAs and shift them by one cell to the right:
colsToFix <- which( is.na(tab[, "SHO%"]) )
tab[ colsToFix, 4:ncol(tab) ] <- tab[ colsToFix, 3:(ncol(tab)-1) ]
tab[ colsToFix, 3 ] <- NA
> str(tab)
'data.frame': 25 obs. of 20 variables:
$ code : chr "93" "91" "61" "88" ...
$ PLAYER: chr "Eichelkraut, Flori" "Müller, Lars" "Alt, Sebastian" "Gross, Arthur" ...
$ POS : chr "F" "F" "D" "F" ...
$ GP : chr "8" "6" "7" "8" ...
$ G : int 10 1 4 3 4 2 0 2 1 0 ...
$ A : int 5 11 5 5 3 4 6 3 3 4 ...
$ PTS : int 15 12 9 8 7 6 6 5 4 4 ...
$ PIM : int 12 10 12 6 2 36 37 29 6 0 ...
$ PPG : int 3 0 1 1 1 1 0 0 1 0 ...
$ PPA : int 1 5 2 2 1 2 4 2 1 1 ...
$ SHG : int 0 1 0 1 1 0 0 0 0 0 ...
$ SHA : int 0 0 1 0 1 0 0 1 0 0 ...
$ GWG : int 2 0 1 0 0 0 0 0 0 0 ...
$ FG : int 1 0 1 1 1 0 0 0 0 0 ...
$ OTG : int 0 0 0 0 0 0 0 0 0 0 ...
$ UAG : int 1 0 1 0 0 0 0 0 0 0 ...
$ ENG : int 0 0 0 0 0 0 0 0 0 0 ...
$ SHOG : int 0 0 0 0 0 0 0 0 0 0 ...
$ SHOA : num 0 0 0 0 0 0 0 0 0 0 ...
$ SHO% : num 0 0 0 0 0 0 0 0 0 0 ...

Resources