Creating conditional relationship while importing CSV into Neo4j - csv

I have a dataset that looks like:
id s01 s02 s03 s04 s05 s06 s07 s08 s09 s10
U01 1 0 0 0 0 0 0 0 0 1
U02 1 0 0 0 0 0 0 0 0 0
U03 1 0 0 0 0 0 0 0 0 0
U04 0 1 0 0 0 0 0 0 0 0
U05 0 1 0 0 0 0 0 0 0 0
U06 0 0 1 0 0 0 0 0 1 0
U07 0 0 1 0 0 0 0 0 0 0
U08 0 0 1 0 0 0 0 0 0 0
U09 0 0 1 1 0 0 0 0 0 0
U10 0 0 0 1 0 0 0 0 0 0
U11 0 0 0 1 1 0 0 0 0 1
U12 0 0 0 0 1 0 0 0 0 0
U13 0 0 0 0 1 1 0 0 0 0
U14 0 0 0 0 0 1 1 0 0 0
U15 0 0 0 0 0 0 1 0 0 0
U16 0 0 0 0 0 0 1 1 0 0
U17 0 0 0 0 0 1 0 1 0 0
U18 0 0 0 0 0 0 0 1 0 0
U19 0 0 0 0 0 0 0 1 1 0
U20 0 1 0 0 0 0 0 0 1 0
I want to import it into Neo4j, the nodes are U01, U02, U03 ... and s01, s02, ...., s10
The nodes have already been created. Now I want to create relationships based on the values (0/1): if it is 1 then there should be a relationship between the corresponding nodes.
Like for 2nd row and 2nd column the value is 1, so that means I have to create a relationship between U01 and S01 and for 2nd row 3rd column the value is 0, so I will skip that value.
So far, I was trying to come up with a hardcoded solution:
LOAD CSV WITH HEADERS FROM file:///new.csv" AS line FIELDTERMINATOR '\t'
WITH line
MATCH (a:Users)
WHERE a.user_id = line.id
WITH line, a
RETURN CASE
WHEN TOINT(line.s01)=1 THEN CREATE (a)-[:watches]->(c:NewsMedia{ID:"s01"})
WHEN TOINT(line.s02)=1 THEN MERGE (a)-[:watches]->(b:NewsMedia{ID:"s02"})
WHEN TOINT(line.s03)=1 THEN MERGE (a)-[:watches]->(b:NewsMedia{ID:"s03"})
WHEN TOINT(line.s04)=1 THEN MERGE (a)-[:watches]->(b:NewsMedia{ID:"s04"})
WHEN TOINT(line.s05)=1 THEN MERGE (a)-[:watches]->(b:NewsMedia{ID:"s05"})
WHEN TOINT(line.s06)=1 THEN MERGE (a)-[:watches]->(b:NewsMedia{ID:"s06"})
WHEN TOINT(line.s07)=1 THEN MERGE (a)-[:watches]->(b:NewsMedia{ID:"s07"})
WHEN TOINT(line.s08)=1 THEN MERGE (a)-[:watches]->(b:NewsMedia{ID:"s08"})
WHEN TOINT(line.s09)=1 THEN MERGE (a)-[:watches]->(b:NewsMedia{ID:"s09"})
WHEN TOINT(line.s10)=1 THEN MERGE (a)-[:watches]->(b:NewsMedia{ID:"s10"})
END
The error is coming in the conditional MERGE part, can we not create relationships in WHEN <condition> THEN MERGE ()-[]->()

1) I think that you can not use MERGE inside the CASE.
2) To avoid hardcored solution you can use UNWIND and KEYS:
LOAD CSV WITH HEADERS FROM "file:///new.csv" AS line FIELDTERMINATOR '\t'
MERGE (a:Users {user_id: line.id})
WITH a, line
UNWIND KEYS(line) AS bid
WITH a, bid WHERE "id" <> bid AND TOINTEGER(line[bid]) = 1
MERGE (b:NewsMedia {ID: bid})
MERGE (a)-[r:watches]->(b)
RETURN a, r, b

Related

if an integer is stored on 4 octets which means its stored in 32 bits, so why the convertion from integer to bits have less than 32bits?

*I mean that as we know an int is stored as 4 octets, that means if we want to convert for example 18 to binary, it must contain 32 bits not just 5 bits (18 = 10010 bits)
This works to with any other type, string, float, double etc.... we don't have the number of bits that normally we must have, the majority of type is less. So please can you explain to me, I'm sure I miss something.
*
Imagine you have a device with a long set of wheels, each showing a digit from 0 to 9. You can set each digit to one of those 10 digits, and nothing else. Your task is to agree with your friend a way to pass across a list of numbers.
Your first idea is to simply set the numbers directly, and leave any spare wheels at 0. I give you the numbers 5, 7, and 3, so you set your wheels like this:
5 7 3 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
You pass it to your friend, and they read off "5", "7", "3".
But now I give you the numbers 18, 2006, and 0. So you set the wheels like this:
1 8 2 0 0 6 0 0 0 0 0 0 0 0 0 0 0 0 0 0
You pass it to your friend, and they're confused. Is that "1", "8", "2006"? Or perhaps it's "18", "200", "6"? They don't know where one number ends and the next begins.
So, you have an idea: if all numbers were the same length, you'd know where they began and ended. Fortunately, in our number system, "1", "01", "001", and "0001" all mean the same thing, so you can agree that all numbers will be 4 digits long. 18, 2006, 0 now looks like this:
0 0 1 8 2 0 0 6 0 0 0 0 0 0 0 0 0 0 0 0
You pass it over, and your friend reads off "0018, that's eighteen; 2006; 0000, that's zero". Success!
Our only problem is that we can't represent a number higher than 9999; so when we choose our fixed width, we need to decide on the highest number we want to be able to represent. More width, higher numbers; but more wasted space when we're working with smaller numbers.
When we talk about "32-bit integers" or "64-bit integers", we're talking about essentially the same idea, but instead of wheels that can be 0 to 9, we have some representation of bits that can be either 0 or 1.
So, to store the number 18, we work out the binary, 10010, and then somewhere in memory we write down a sequence of 32 bits:
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 1 0
If we don't need very high numbers, and want to save space, we might make do with 16 bits, or even 8; if memory is cheap enough, we might use 64 bits to be able to have much higher numbers. But everyone reading and writing a particular set of numbers has to agree in advance what width to use.
Things get a bit more complicated if we want negative numbers, or fractions, but the aim is the same: by agreeing in advance how much space each number will take up, we know where one number begins and the next one ends.
Now let's think about strings.
Imagine we extend the earlier task to pass a word to your friend, using the same device. One way to do this is to devise a code where each letter is given a number: 1 for A, 2 for B, and so on. Then a string is just a sequence of numbers, using our earlier fixed length idea; we only need to go up to 26, so we'll choose a fixed width of 2 digits.
So given the word "HELLO", you would pass over 8, 5, 12, 12, 15 like this:
0 8 0 5 1 2 1 2 1 5 0 0 0 0 0 0 0 0 0 0
Your friend reads off the numbers, converts them to letters, and gets the word.
Let's make it more challenging: a word, followed by a number, for instance: "YEAR", 2006; or "AGE", 18.
2 5 0 5 0 1 1 8 2 0 0 6 0 0 0 0 0 0 0 0
0 1 0 7 0 5 0 0 1 8 0 0 0 0 0 0 0 0 0 0
Wait, is that "YEART" and then 600? Or maybe "YEARTF" and a zero? We're back to our original problem of knowing when things begin and end.
One solution is to use our fixed length trick again: let's say every word is five letters long, but if the letter value if "00", ignore it. Because we're not used to putting "zeroes" on the beginning of words, we decide to pad at the end instead of the beginning, but it doesn't really matter.
2 5 0 5 0 1 1 8 0 0 2 0 0 6 0 0 0 0 0 0
0 1 0 7 0 5 0 0 0 0 0 0 1 8 0 0 0 0 0 0
The problem is, words vary in length a lot more than numbers, so we'll often have to have quite a large width, and so waste a lot of space on smaller words.
But there's something interesting about our "00", it's essentially like a space in English writing. When we read, we know that as soon as we get to a space, that's the end of the word, so we can use the same trick with our 00.
2 5 0 5 0 1 1 8 0 0 2 0 0 6 0 0 0 0 0 0
0 1 0 7 0 5 0 0 0 0 1 8 0 0 0 0 0 0 0 0
That means we can write as much text as fits in our total number of spaces; for instance, to write "A", 99; "AA", 99; and "AAAAAAAA", 99:
0 1 0 0 9 9 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 1 0 1 0 0 9 9 0 0 0 0 0 0 0 0 0 0 0 0
0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 0 9 9
This is one of the common ways strings are stored in computers - in practice, it's not words we want to separate, but whole strings, so the "terminator" (normally all zero bits) is separate from a space (which in ASCII-compatible 8-bit encodings is 00100000),
One problem is that you have to "reserve" that special terminating character - you can't represent a string that has that sequence of bits in it. To get around that, there's a different trick we can use: instead of signalling the end of the string, we can send a separate marker for the length of the string.
For instance, our "YEAR" and "AGE" example becomes:
0 4 2 5 0 5 0 1 1 8 2 0 0 6 0 0 0 0 0 0
0 3 0 1 0 7 0 5 0 0 1 8 0 0 0 0 0 0 0 0
And our As and 9s:
0 1 0 1 9 9 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 2 0 1 0 1 9 9 0 0 0 0 0 0 0 0 0 0 0 0
0 8 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 9 9
The maximum length of a string is now determined by how wide we made the number representing the length - in this case, we have a two-digit length, so can represent string from 0 to 99 letters long.

Django - How to properly stream a matrix in a RESTful API?

After computing some values from on the server side. The result is a matrix (i.e a pandas.DataFrame) with the following format:
0 1 2 3 4 5 6
0 1 0 0 0 2 0 6
100 0 0 0 0 0 0 0
200 0 0 0 0 0 0 0
300 0 0 0 0 0 0 0
Where first row/column represents row/column indexes, and (just in this case) some of the values are zero.
What would be a popper JSON format to stream this matrix? Is there any standard way to do it?
I suggest
{
x_axis: [0,1,2,3],
y_axis: [100,200,300],
rows: [
[1,2,3,4],
[1,2,3,4],
[1,2,3,4]
]
}

Lisp board count pieces

I need to make a function in lisp that counts the pieces in my board. Let me explain my issue/game:
i have a board (10x10) like so:
(
(0 0 0 0 0 0 0 0 0 0)
(0 0 0 0 0 0 0 0 0 0)
(0 0 0 0 0 0 0 0 0 0)
(0 0 0 0 0 0 0 0 0 0)
(0 0 0 0 0 0 0 0 0 0)
(0 0 0 0 0 0 0 0 0 0)
(0 0 0 0 0 0 0 0 0 0)
(0 0 0 0 0 0 0 0 0 0)
(0 0 0 0 0 0 0 0 0 0)
(0 0 0 0 0 0 0 0 0 0)
)
in this board we will represent two types of pieces:
1 by 1 squares
2 by 2 squares
they will be positioned in my board like so:
(
(0 1 0 0 0 0 0 1 0 0)
(0 0 0 0 0 0 0 0 0 0)
(0 0 0 0 0 0 0 0 0 0)
(0 0 0 1 1 0 0 0 0 0)
(0 0 0 1 1 0 0 1 1 0)
(0 0 0 0 0 0 0 1 1 0)
(0 0 0 0 0 0 0 0 0 0)
(0 0 0 0 0 0 0 0 0 0)
(0 1 0 0 0 0 1 0 0 0)
(0 0 0 0 0 0 0 0 0 0)
)
In this example I have four 1-by-1 squares and two 2-by-2 squares.
I need a function that counts how many of a given type of piece I have in my board.
Example:
(defun countPiece (board pieceType)
; here I am supposed to write my code
)
so if I called (countPiece (testBoard) 'twoByTwoSquare) it returns 2, for example where testboard is a function that returns a sample board with a few pieces positioned
However, I dont know where to begin. any tips?
EDIT: There can never be pieces together horizontally nor vertically. Only in diagonal
I am not fluent in lisp, so my answer will only give you the algorithmic solution:
singlePieces = empty list
doublePieces = empty list
for each row
for each column
if ((value[row][column] = 1)
and ((row - 1, column - 1) not in doublePieces)
and ((row - 1, column) not in doublePieces)
and ((row, column - 1) not in doublePieces)) then
if ((row < rowCount)
and (column < columnCount) and (value[row][column + 1] = 1)
and (value[row + 1][column] = 1)
and (value[row + 1][column + 1]) = 1) then
doublePieces.add(row, column)
else
singlePieces.add(row, column)
end if
end if
end for
end for
Here’s how to do it with lists:
(defun count-pieces (board &aux (small 0) (big 0))
(loop for (row next) on (cons nil board)
for bigs = nil then
(loop for (tl tr) on row
for (bl br) on (or next (mapcar (constantly 0) row)
for n from 0
for nearly-bigp = (every #’plusp (list tl tr bl br))
for bigp = nearly-bigp then (and nearly-bigp (not pbigp))
and pbigp = nil then bigp
for ninbig = (or (member n bigs) (and bigp (member (1+ n) bigs)))
if (and ninbig bigp)
collect n and collect (+ n 1)
and do (incf big)
else if ninbig do (incf small)))
(values big small))
I wrote this on my phone so I’m not sure it works. It’s also a little clumsy. It’s also probably worth using arrays instead of lists and it might be worthwhile to e.g. represent a big piece with -1 or 2 or 3 so that they may be easily distinguished.

fail to create and modify device_vector directly [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I am starting to use thrust. I just write a simple function to fill the vector, here I have
template <class T> void fillzeros(T &v, int size)
{
v.reserve(size);
thrust::fill(v.begin(), v.end(), 0);
}
void main(void)
{
thrust::device_vector<float> V;
thrust::host_vector<float> W;
fillzeros(V, 100); // works
fillzeros(W, 100); // it doesn't compile, lots of error comes out
// if I want to crease W, I have to do
W = V; // so I must generate device vector in advance?
// I also try the following example shown in the thrust website, still don't compile
thrust::device_vector<int> vv(4);
thrust::fill(thrust::device, vv.begin(), vv.end(), 137);
}
it seems that I cannot create and assigned device_vector directly. I have to create host_vector first and assign it to device_vector.
BTW, how can I determine the vector type in the function if I pass it as a template?
p.s. too many errors about thrust::system::detail::generic::unintialized_fill_n
reserve has no effect on a vector size. Therefore your code is doing nothing useful, since W and V start out as size zero and end up as size zero. This code works fine for me:
$ cat t224.cu
#include <thrust/device_vector.h>
#include <thrust/host_vector.h>
#include <thrust/fill.h>
#include <thrust/copy.h>
#include <iostream>
template <class T> void fillzeros(T &v, int size)
{
v.resize(size);
thrust::fill(v.begin(), v.end(), 0);
}
int main(void)
{
thrust::device_vector<float> V;
thrust::host_vector<float> W;
fillzeros(V, 100);
fillzeros(W, 100);
std::cout<< "V:" << std::endl;
thrust::copy(V.begin(), V.end(), std::ostream_iterator<float>( std::cout, " "));
std::cout<< std::endl << "W:" << std::endl;
thrust::copy(W.begin(), W.end(), std::ostream_iterator<float>( std::cout, " "));
std::cout<< std::endl;
return 0;
}
$ nvcc -arch=sm_20 -o t224 t224.cu
$ ./t224
V:
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
W:
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
$

Boolean Algebra simplification

is it possible to simply this Boolean function
(!A*!B*!C) + (!A*!B*C*!D) + (A*!B*!C*D) + (A*!B*C*!D) + (A*B*!C*!D)
I came up with this:
(!B*(!A*(!C+!D))+A*(C XOR D)) + (A*B*!C*!D)
Messy to look at, but there are fewer terms.
Look at the truth table:
A B C D X
0 0 0 0 1
0 0 0 1 1
0 0 1 0 1
0 0 1 1 0
0 1 0 0 0
0 1 0 1 0
0 1 1 0 0
0 1 1 1 0
1 0 0 0 0
1 0 0 1 1
1 0 1 0 1
1 0 1 1 0
1 1 0 0 1
1 1 0 1 0
1 1 1 0 0
1 1 1 1 0
It looks like you can take the three parts of the table where X = 1 and simplify this to the sum of three terms:
!A*!B*!(C*D) + A*!B*(C^D) + A*B*!C*!D
Note that I've use XOR (^) in the second term. If you can't use XOR then you'll need to expand the second term a little.
You can reduce the number of terms further by factoring out either !B or A for two of the terms, e.g.
!B*(!A*!(C*D) + A*(C^D)) + A*B*!C*!D
or:
!A*!B*!(C*D) + A*(!B*(C^D) + B*!C*!D)