Skip to content

Instantly share code, notes, and snippets.

@alisterburt
Created May 15, 2023 14:47
Show Gist options
  • Select an option

  • Save alisterburt/58943f943a9c59dca49bd339bae2688a to your computer and use it in GitHub Desktop.

Select an option

Save alisterburt/58943f943a9c59dca49bd339bae2688a to your computer and use it in GitHub Desktop.
for ido
from pathlib import Path
import numpy as np
import pandas as pd
import starfile
# generate fake data
a = np.random.uniform(0, 200, size=(100, 3))
for f in '123':
filename = f'data/{f}.csv'
np.savetxt(filename, a, fmt='%.2f', delimiter=',')
# use pandas to read in text files
# - add tilt series id column
# - give columns meaningful names
dfs = []
files = list(Path('data').glob('*.csv'))
for csv_file in files:
df = pd.read_csv(csv_file, header=None)
df = df.rename(columns={0: 'z', 1: 'y', 2: 'x'})
df['tilt_series_id'] = csv_file.name
dfs.append(df)
# make one big dataframe
df = pd.concat(dfs)
print(df.columns, df.shape)
# some operation on the cooordinate values
df[['x', 'y', 'z']] = df[['x', 'y', 'z']].to_numpy() * 8
df = df.rename(
columns={
'x': 'rlnCoordinateX',
'y': 'rlnCoordinateY',
'z': 'rlnCoordinateZ',
'tilt_series_id': 'rlnTomoName',
}
)
starfile.write({'particles': df}, 'test.star', overwrite=True)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment