Assign values to tensor elements

Hello,

I watched one of Yuanming’s online intro course video, and also read through the documentation, just got somewhat familiar to this wonderful Taichi language.

Now I got a question that, is it possible to assign data from read-in csv to the elements of taichi’s tensor? I would like to try to simulate a data-driven demo in the run long for my undergraduate research project~

But there was an error that the list indices cannot be expressions, they can be only integers. Is there any method I could achieve this kind of assignment I want?

Thanks in advance! I attached my code as a screenshot~

Maybe you want to remove the @ti.kernel and @ti.func decorators?

(You can’t use ti.var in @ti.func, it should be placed in Python-scope)

Variable initialization should be done in Python-scope. list cannot be used in Taichi kernels.

water_cone = ti.var(ti.f32, (len(rows), len(rows[0])))

def temp(i, j):
  return rows[i][j]

def addata():
  for i, j in water_cone:
    water_cone[i, j] = temp(i, j)  # importing Python variable to Taichi tensor

or

import numpy as np

water_cone = ti.var(ti.f32, (len(rows), len(rows[0])))
water_cone.from_numpy(np.array(rows))  # numpy will take care of it
1 个赞

Thanks! But it still gives me an error, can you take a look?


Also, I found that the import process is not successful, but temp function works.

That’s because struct-for cannot be used in Python-scope.

Try this:

import numpy as np

water_cone = ti.var(ti.f32, (len(rows), len(rows[0])))
water_cone.from_numpy(np.array(rows))  # numpy will take care of it

It’s a much easier way to copy data.

Thank you~ I tried it but it got another error, it seems like something in another file cannot handle the script? Do you have any idea?

Would you show me the full stack backtrace and code in text form?

Sure, the backtrace shows as follows:

Traceback (most recent call last):
File “C:/Users/jingy/PycharmProjects/aerosol_test1/aerosol_test.py”, line 16, in
water_conc.from_numpy(np.array(rows))
File “C:\Users\jingy\PycharmProjects\aerosol_test1\venv\lib\site-packages\taichi\lang\util.py”, line 212, in wrapped
return func(*args, **kwargs)
File “C:\Users\jingy\PycharmProjects\aerosol_test1\venv\lib\site-packages\taichi\lang\expr.py”, line 173, in from_numpy
ext_arr_to_tensor(arr, self)
File “C:\Users\jingy\PycharmProjects\aerosol_test1\venv\lib\site-packages\taichi\lang\kernel.py”, line 528, in wrapped
return primal(*args, **kwargs)
File “C:\Users\jingy\PycharmProjects\aerosol_test1\venv\lib\site-packages\taichi\lang\kernel.py”, line 457, in call
instance_id, arg_features = self.mapper.lookup(args)
File “C:\Users\jingy\PycharmProjects\aerosol_test1\venv\lib\site-packages\taichi\lang\kernel.py”, line 157, in lookup
key = self.extract(args)
File “C:\Users\jingy\PycharmProjects\aerosol_test1\venv\lib\site-packages\taichi\lang\kernel.py”, line 148, in extract
extracted.append(extractor(args[i]))
File “C:\Users\jingy\PycharmProjects\aerosol_test1\venv\lib\site-packages\taichi\lang\kernel_arguments.py”, line 12, in extract
return to_taichi_type(x.dtype), len(x.shape)
File “C:\Users\jingy\PycharmProjects\aerosol_test1\venv\lib\site-packages\taichi\lang\util.py”, line 165, in to_taichi_type
raise AssertionError(“Unknown type {}”.format(dt))
AssertionError: Unknown type <U11

And my code is:
import csv
import taichi as ti
import matplotlib.pyplot as plt
import numpy as np

ti.init(arch=ti.cpu)

rows = []
with open(‘D:\SURA\Archive\output-water-concentration.csv’) as water:
csv_reader = csv.reader(water,delimiter=’,’)
for row in csv_reader:
rows.append(row)

create a water conc tensor of scalars with 301*200 dimension

water_conc = ti.var(ti.f32, (len(rows), len(rows[0])))
water_conc.from_numpy(np.array(rows))

I found why, that’s because csv returns a string "1" instead of integer 1.
So you have to convert it into integer first:

import csv
import taichi as ti
import matplotlib.pyplot as plt
import numpy as np

ti.init(arch=ti.cpu)

rows = []
with open('a.csv') as water:
    csv_reader = csv.reader(water,delimiter=',')
    for row in csv_reader:
        rows.append([int(x) for x in row])  # changed here!

water_conc = ti.var(ti.f32, (len(rows), len(rows[0])))
water_conc.from_numpy(np.array(rows))

Oh! Thanks so much! I am not really familiar with python…