ASKSAGE: Sage Q&A Forum - RSS feedhttps://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Sun, 09 Jun 2019 18:18:23 +0200Derivative of a CSV data sethttps://ask.sagemath.org/question/46885/derivative-of-a-csv-data-set/I am trying to get the derivative of this CSV data set, and this is code I have so far I thought it would be similar to this matlab code:
dy=diff(y)./diff(x)
plot(x(2:end),dy)
Data Set:
0,2.58612
0.00616025,2.20018
0.0123205,1.56186
0.0184807,0.371172
0.024641,0.327379
0.0308012,0.368863
0.0369615,0.322228
0.0431217,0.171899
0.049282,-0.0635003
0.0554422,-0.110747
0.0616025,0.0701394
0.0677627,0.202381
0.073923,0.241264
0.0800832,0.193697
0.0862434,0.0797016
0.0924037,0.0103144
0.0985639,0.096153
0.104724,0.216782
There's more data...a lot more
This is my attempt at the code:
import csv
data = list( csv.reader(open('C:/images/TEST1.txt','rU')) )
data = map(lambda x: [float(x[0]),float(x[1])],data)
P = list_plot(data, plotjoined= True, color = 'blue', xmin = 0, xmax = 3)
TeraHertz = text('Tera Hertz', (1.5,-.9))
Absorbance = text('Absorbance', (0.8,5))
g = P + TeraHertz + Absorbance
g.show()
latex(g)
g.save('TeraHertzTEST1.pgf')
t=diff(x[1]).diff(x[0])
l = plot(x[0],t)
l.show()EnlightenedFunkSun, 09 Jun 2019 18:18:23 +0200https://ask.sagemath.org/question/46885/Divide Combinations(n,k) into multiple parts (n > 1110)https://ask.sagemath.org/question/45606/divide-combinationsnk-into-multiple-parts-n-1110/I asked a similar question here: https://ask.sagemath.org/question/45347/sagemath-heap-size-limit/. I closed it, because the answer matched the question there. However, now I have an extra question related to it:
C = Combinations(2110, 3)
C_cardinality = binomial(2110, 3) # 1.563.429.820
N = C_cardinality / 95 # Number of parts = 95 (chunk size)
R = range(0, C_cardinality , N)
Because C_cardinality is too big, I would like to divide C into multiple parts, which are then used on multiple computers to process separately. In order to be sure that these multiple parts are totally different but covering the whole C, I use C's list:
c0 = C.list()[R[0]:R[0]+N]
c1 = C.list()[R[1]:R[1]+N]
c2 = C.list()[R[2]:R[2]+N]
...
However, my computer's RAM (memory 128GB) is not enough to store the `C`'s list. Therefore, I iterate `C` and store in a CSV instead:
def storeCSV(data, fo, wtype='a'):
with open(fo, wtype) as wFile:
write_file = csv.writer(wFile)
write_file.writerow([data])
index = 0
count = 0
for c in C:
storeCSV(c, "part_%s.csv" % index)
count += 1
if count == N:
count = 0
index += 1
print index
Unfortunately, this CSV write is too slow, i.e ~6000 write/min, which means I have to wait about 3800 hours for the whole `C` to finish.
You might wonder why I need to divide `C` into multiple parts. After having these parts, I will run the following code on different computers with a purpose to collect information among set of 3 numbers:
v = MatrixSpace(GF(2), 3,9)
g3 = (c for c in c0 if block_matrix(3, 1, [v[c[0]], v[c[1]], v[c[2]]]).rank() >= 6) # or c1, c2, ..., cN
rel = dict()
for g in list(map(tuple,g3)):
# (1, 2, 3)
rel.setdefault(g[::2], []).append(g[1]) # '[1, 3]': 2
rel.setdefault(g[:2], []).append(g[-1]) # '[1, 2]': 3
rel.setdefault(g[1:], []).append(g[0]) # '[2, 3]': 1
Specifically, I got all Combinations of 3 numbers within `c0, c1,...,cN`. Then, I filter what I need and store in `g3`. For all `g3` of format (A, B, C), I collect (A,B), (B,C), (A,C) as keys and C,A,B as values respectively. The most important reason I must collect this in a Python dictionary is that I need to retrieve any `rel[(X, Y)]` later on.
I wish the problem is described clearly and someone can support. Thank a lot !!!imnvshSun, 03 Mar 2019 01:25:24 +0100https://ask.sagemath.org/question/45606/saving to a .csv filehttps://ask.sagemath.org/question/40137/saving-to-a-csv-file/ In my computations I got a list of strings:
vect = ['00011', '00010', '01011', '01111', '01100']
I need to save it as .csv file, each element row by row, ideally like this:
00011
00010
01011
01111
01100
I use a code:
import csv
C = vect
with open('file_path_vec.csv', 'w') as f:
c = csv.writer(f)
c.writerows(C)
But what I get in my .csv looks like this:
0,0,0,1,1
0,0,0,1,0
0,1,0,1,1
0,1,1,1,1
0,1,1,0,0
Why does it add commas in between? How can I get rid of them?XeniaWed, 13 Dec 2017 15:08:16 +0100https://ask.sagemath.org/question/40137/Read from csv-file, write results in new columns in the same csv-filehttps://ask.sagemath.org/question/39845/read-from-csv-file-write-results-in-new-columns-in-the-same-csv-file/Hi,
I am currently trying to achieve the following:
given a csv-file e.g. like
x,y,sinx,siny
30,45,,
60,90,,
Values for x and y are given in degree.
What I want to do: take the data from the csv-file, calculate sin(x) and sin(y) for the values given in each row and then print the result in the corresponding cell. If possible, do not round the result, so the output should look something like this:
x,y,sinx,siny
30,45,1/2,sqrt(2)/2
60,90,sqrt(3)/2,1
Taking this one step further, if possible the output should be printed in complete LaTeX-code because the date from the csv-file will be used in a LaTeX-document, so the best possible result would be:
x,y,sinx,siny
30,45,\frac{1}{2},\frac{\sqrt{2}}{2}
60,90,\frac{\sqrt{3}}{2},1
I do know about sageTex which is able to do this, but for other reasons I can't use sageTex for my problem.
I am completely new to sage, so I don't know if there is a standard procedure for this or if it is possible at all. Any input is appreciated!
HirshyThu, 30 Nov 2017 13:43:11 +0100https://ask.sagemath.org/question/39845/How can I import CSV files in Sagemath Cloud?https://ask.sagemath.org/question/11358/how-can-i-import-csv-files-in-sagemath-cloud/I was trying out Sagemath Cloud. Looks great, good job. But I have a problem because I can't figure out how to import CSV files with data.
I am using R (`%default_mode r`) to do some basic statistics and stuff in a worksheet. I already have the script working on my local machine, but I wanted to try it in the Cloud too.
What I did was that I drag-and-dropped a couple of CSV files into a project. Seems to work fine; the files appear in the drag-and-drop area, and a progress indicator goes to full and then a green check-mark appears. But after that, the files are not present in the Files section, there is only my worksheet. In the Log section, there is only mention of me opening the worksheet, it says nothing about successfully uploading files. When I try to run an import command in the worksheet, I get "No such file or directory".
The import command I used:
data <- read.csv("filename.csv", header=TRUE, sep=';')
EDIT:
[This question](http://ask.sagemath.org/question/2563/how-to-load-a-local-image-from-cloudsagemath) has an answer for how to do it in Python. But I am doing it from R, and I reckon the python function os.environ() is not available.
EDIT 2:
If I go to the terminal line under Files and type "ls -A" I can get a full list of files in my project, but the CSV files that I dragged-and-dropped are not in the list.Dan-ErikWed, 16 Apr 2014 07:46:07 +0200https://ask.sagemath.org/question/11358/Can I parse CSV data separated by \t to vectorshttps://ask.sagemath.org/question/10432/can-i-parse-csv-data-separated-by-t-to-vectors/I have a CSV data as follows:
pcb138 pcb180 pcb52 pcb118 pcb
1.46 0.738 0.532 0.72 19.9959
0.64 0.664 0.03 0.236 6.0996
3.29 1.15 0.134 1.54 24.9655
3.94 1.33 0.466 1.94 37.4436
3.18 2.14 0.243 1.47 30.183
2.43 1.3 0.137 1.31 20.8036
3.94 3.49 0.208 0.876 41.3818
3.38 1.04 0.477 2.46 29.478
2.21 0.966 0.457 1.14 24.2387
2.49 1.59 0.298 1.18 26.3198
0.86 0.395 0.02 0.406 8.591
3.38 1.85 0.539 1.5 36.4229
7.39 4.42 0.707 3.55 66.4108
Is it possible to read that to vectors? I tried the following:
sage: import csv
sage: file='/home/jaakko/Downloads/pcb.dat'
sage: reader=csv.reader(open(file))
sage: L=[]
sage: for row in reader:^J L.append(row)
....:
sage: L[0][0]
'pcb138\tpcb180\tpcb52\tpcb118\tpcb'
sage: L[1][0]
'1'
sage: L[1][1]
'46\t'
What I would like to have is vectors of the form
pcb138=vector([1.46,0.64,...,7.39])
without those tabulators.Jaakko SeppäläSat, 10 Aug 2013 04:46:38 +0200https://ask.sagemath.org/question/10432/import csv file: integers appearing as floating pointshttps://ask.sagemath.org/question/10141/import-csv-file-integers-appearing-as-floating-points/Hi,
I am trying to import a csv file in order to create an adjacency matrix and then plot the associated directed graph. I upload the csv file and use its content to generate a matrix as follows
sage: import csv
sage: data = list(csv.reader(file(DATA+'matrix23.csv')))
sage: m = matrix([[ float(_) for _ in line] for line in data])
sage: m
Whereas my csv file only contains integers 1's and 0's, the generated matrix is floating point 1.0's and 0.0's!
Note: the CSV file was created using MS Excel with cells formated to zero decimal places. I'm new to both python and sage and can't find anything on the forums to helpGingerAndyMon, 20 May 2013 10:17:16 +0200https://ask.sagemath.org/question/10141/How to save and use data from spreadsheet?https://ask.sagemath.org/question/10103/how-to-save-and-use-data-from-spreadsheet/Hello community,
to automate my calculations I would like to save results of a spreadsheet calculation as seen below to compute at display the results of further calculations with sagetex.
How could I save the data from a spreadsheet to use them with sagetex?
What for a data-structure/type would you suggest?
![image description](http://s7.directupload.net/images/130508/qqhpoykp.png)reb_rebWed, 08 May 2013 14:54:29 +0200https://ask.sagemath.org/question/10103/