In [84]:
%autosave 20
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
In [6]:
import math
import cmath
cmath.sqrt(-1)
Out[6]:
In [7]:
fh = open('diagram.csv')
text = fh.read()
fh.close()
print(text)
In [10]:
with open('diagram.csv') as fh:
text = fh.read()
print(fh.closed)
In [27]:
x = []
y = []
with open('diagram.csv', encoding='utf-8') as fh:
for row in fh:
row = row.strip()
a, b = row.split(',')
x.append(float(a))
y.append(float(b))
x = np.array(x)
y = np.array(y)
print(x)
print(y)
plt.plot(x, y, 'x')
with open('diagram.tsv', 'w') as fh:
for a, b in zip(x * 7, y):
fh.write('{}\t{}\n'.format(a, b))
In [28]:
with open('cat.jpg', 'rb') as fh:
data = fh.read(16)
print(data)
print(type(data))
In [32]:
from PIL import Image
img = Image.open('cat.jpg')
cat = np.array(img)
cat.dtype
Out[32]:
In [45]:
table = np.genfromtxt('diagram.csv',
delimiter=',',
names=('d', 'v'))
print(table[0])
print(table['d'])
records = np.rec.array(table)
records.d
Out[45]:
In [57]:
table = np.genfromtxt('freddi.dat', names=True,
skip_footer=100)
In [73]:
import gzip
import os
import shutil
with gzip.open('V404Cyg.txt.gz') as fh:
for _ in range(10):
print(fh.readline())
In [70]:
table = np.genfromtxt('V404Cyg.txt.gz', names=True,
usecols=(0, 1, 2,),
missing_values=b'', filling_values=0,)
table[:10]
Out[70]:
In [83]:
def magn_converter(s):
if s.startswith(b'<'):
x = float(s[1:]) + 900
return x
return float(s)
table = np.genfromtxt('V404Cyg.txt.gz', names=True,
usecols=(0, 1, 2,),
dtype=(float, float, float),
missing_values=b'', filling_values=0,
converters={
1: magn_converter
})
table[:10]
is_upper_limit = table['Magnitude'] > 500
good_data = table[np.logical_not(is_upper_limit)]
plt.plot(good_data['JD'], good_data['Magnitude'], 'x')
Out[83]:
In [93]:
df = pd.DataFrame(table)
df.JD
df['JD']
df.columns
# df[0]
Out[93]:
In [95]:
df.loc[2]
Out[95]:
In [100]:
df = pd.read_table('V404Cyg.txt.gz', low_memory=False)
print(df.dtypes)
print(type(df.Magnitude[0]))
In [101]:
# import json
# .yaml, .yml, .ini
In [113]:
def magn_converter(s):
if s.startswith('<'):
s = s[1:]
return float(s)
df['m'] = df.Magnitude.map(magn_converter)
df['is_upper_limit'] = df.Magnitude.map(
lambda s: s.startswith('<')
)
df['m']
Out[113]:
In [117]:
upper_limits = df[df.is_upper_limit]
In [ ]: