Skip to content

Commit 766d73e

Browse files
committed
Merge branch 'master' of github.com:SpheMakh/Stimela
2 parents 8c34d50 + 1f16786 commit 766d73e

7 files changed

Lines changed: 89 additions & 41 deletions

File tree

examples/simulation_pipeline.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,4 +82,4 @@
8282

8383
# Run recipe. The 'steps' added above will be executed in the sequence that they were adde. The 'steps' added above will be
8484
# executed in the sequence that they were addedd
85-
pipeline.run()
85+
pipeline.run([1])

setup.py

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
11
#!/usr/bin/env python
22

33
import os
4-
from distutils.core import setup
4+
5+
try:
6+
from setuptools import setup
7+
except ImportError as e:
8+
from distutils.core import setup
9+
510
import stimela
611

712
setup(name = "stimela",
@@ -11,16 +16,16 @@
1116
author_email = "Sphesihle Makhathini <sphemakh@gmail.com>",
1217
url = "https://github.com/sphemakh/Stimela",
1318
packages = ["stimela", "stimela/cargo","stimela/utils"],
14-
package_data = { "stimela/cargo" : ["data/skymodels/*.lsm.html",
19+
package_data = { "stimela/cargo" : ["data/skymodels/*.lsm.html",
1520
"configs/*.json",
16-
"data/observatories/*.txt",
21+
"data/observatories/*.txt",
1722
"cab/*/Dockerfile",
1823
"base/*/Dockerfile",
19-
"cab/*/src/*.py",
24+
"cab/*/src/*.py",
2025
"cab/*/src/*.sh",
2126
"cab/*/src/*.json",
2227
"cab/*/src/tdlconf.profiles"]},
2328
requires = ["docker", "python"],
24-
scripts = ["bin/" + i for i in os.listdir("bin")],
29+
scripts = ["bin/" + i for i in os.listdir("bin")],
2530
classifiers = [],
2631
)
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
FROM radioastro/base
2+
MAINTAINER <sphemakh@gmail.com>
3+
4+
RUN apt-add-repository -s ppa:radio-astro/main && \
5+
apt-get update && \
6+
apt-get upgrade -y && \
7+
apt-get install -y git-core \
8+
libpython-dev \
9+
libcasacore2-dev \
10+
libboost-python-dev \
11+
libcfitsio3-dev \
12+
wget && \
13+
apt-get clean && \
14+
rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/*
15+
16+
# Force upgrade of pip since pip install --upgrade pip doesn't work
17+
RUN easy_install -U pip setuptools
18+
19+
RUN pip install -I -U git+git://github.com/ska-sa/katdal.git \
20+
python-casacore
21+
22+
RUN mkdir -p /var/kat/static && wget https://github.com/ska-sa/katdal/raw/master/blank.ms.tgz -O - | tar -C ./var/kat/static -xzvf -
23+

stimela/cargo/cab/specfit/src/run.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@
3232

3333
if spi_image:
3434
spi_image = substitute(spi_image) or "%s/%s"%(INPUT, spi_image)
35+
if spi_err:
36+
spi_err = substitute(spi_err) or "%s/%s"%(INPUT, spi_err)
3537

3638
if make_spi:
3739
spi_image = spi_image + ".alpha.fits"
@@ -41,27 +43,28 @@
4143

4244
if image and make_spi:
4345
if isinstance(image, (str, unicode)):
44-
cube = substitute(image) or "%s/%s"%(INPUT,image)
46+
cube = substitute(image) or "%s/%s"%(INPUT,image)
4547

4648
elif isinstance(image, (list, tuple)):
4749
for i,im in enumerate(image):
4850
image[i] = substitute(im) or "%s/%s"%(INPUT, im)
51+
cube = image
4952
else:
5053
raise TypeError("Image has to be either a string or list of strings")
5154

5255
made_spi = True
5356
if mask:
5457
mask = substitute(mask) or "%s/%s"%(INPUT, mask)
5558
import specfit
56-
specfit.spifit(cube, mask=mask, sigma=sigma,
59+
specfit.spifit(cube, mask=mask, sigma=sigma,
5760
spi_image=spi_image,
5861
spi_err_image=spi_err)
5962

6063

6164
if add_spi and lsmname:
6265
print "Extracting spi from image"
63-
if not made_spi:
64-
spi_image = substitute(spi_image) or "%s/%s"%(INPUT, spi_image)
66+
# if not made_spi:
67+
# spi_image = substitute(spi_image) or "%s/%s"%(INPUT, spi_image)
6568

6669
if isinstance(freq0, (str, unicode)):
6770
freq0 = str(freq0)

stimela/cargo/cab/specfit/src/specfit.py

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -48,25 +48,25 @@ def freqInfo(fits):
4848

4949

5050
def spifit(cube, prefix=None, mask=None, thresh=None,
51-
sigma=20, spi_image=None,
51+
sigma=20, spi_image=None,
5252
spi_err_image=None):
53-
5453

5554
if isinstance(cube, (list, tuple)):
5655
ims = []
5756
nchan = len(cube)
58-
freqs = []
57+
freqs = []
5958
prefix = prefix or cube[0][-3:]
6059

6160
for im in cube:
6261
with pyfits.open(im) as hdu:
6362
hdr = hdu[0].header
63+
hdu_data = hdu[0].data
6464
ndim = hdr["NAXIS"]
65-
freq_axis = fitsFreqInd(hdr)
66-
freqs.append( hdr["CRVAL%d"%freq_axis])
67-
ims.append( im[get_imslice(ndim)] )
65+
ind = fitsFreqInd(hdr)
66+
freqs.append( hdr["CRVAL%d"%ind])
67+
ims.append( hdu_data[get_imslice(ndim)] )
6868

69-
data = numpy.vstack(ims)
69+
data = numpy.dstack(ims).T
7070
ndim = data.ndim
7171
cnt_freq = freqs[nchan/2]
7272

@@ -78,8 +78,8 @@ def spifit(cube, prefix=None, mask=None, thresh=None,
7878
ndim = data.ndim
7979

8080
freqs, cnt_freq, bw, nchan, ind = freqInfo(cube)
81-
82-
freq_ind = ndim - ind
81+
82+
freq_ind = ndim - ind
8383
imslice = get_imslice(ndim)
8484
imslice[freq_ind] = slice(None)
8585
data = data[imslice]
@@ -90,7 +90,7 @@ def spifit(cube, prefix=None, mask=None, thresh=None,
9090
aa = []
9191
bb = []
9292
I0 = []
93-
93+
9494
if mask:
9595
with pyfits.open(mask) as hdu:
9696
mdata = hdu[0].data
@@ -104,8 +104,11 @@ def spifit(cube, prefix=None, mask=None, thresh=None,
104104
thresh = sigma*noise
105105
ind = numpy.where(mfs>thresh)
106106

107+
if len(ind) < 1:
108+
raise RunTimeError("No pixels above set threshold, or outside masked region")
109+
107110
for i,j in zip(ind[0], ind[1]):
108-
x = numpy.log(freqs/cnt_freq)
111+
x = numpy.log(numpy.array(freqs)/cnt_freq)
109112
val = data[:,i,j]
110113
if val.any() <= 0:
111114
continue
@@ -120,7 +123,6 @@ def spifit(cube, prefix=None, mask=None, thresh=None,
120123
# aa.append(intercept)
121124
# bb.append(std_err)
122125
# I0.append(numpy.log(data[nchan/2,i,j]))
123-
124126

125127
nans = numpy.isnan(alpha)
126128
alpha[nans] = 0.0

stimela/docker.py

Lines changed: 33 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -140,27 +140,28 @@ def get_log(self):
140140
def start(self):
141141
running = True
142142
tstart = time.time()
143-
utils.xrun("docker start -a", [self.name])
144143
self.status = "running"
145144
self.log_container.update(self.info(), uptime="00:00:00")
145+
try:
146+
utils.xrun("docker start -a", [self.name])
147+
except KeyboardInterrupt:
148+
self.stop()
149+
self.remove()
150+
raise KeyboardInterrupt("Terminating process")
151+
152+
# while running:
153+
# try:
154+
# uptime = seconds_hms(time.time() - tstart)
155+
# self.log_container.update(self.info(), uptime=uptime)
156+
# self.uptime = uptime
157+
# self.log_container.write()
158+
# status = self.info()["State"]["Status"]
159+
# if status != "running":
160+
# running = False
146161

147-
while running:
148-
time.sleep(1)
149-
uptime = seconds_hms(time.time() - tstart)
150-
self.log_container.update(self.info(), uptime=uptime)
151-
self.uptime = uptime
152-
self.log_container.write()
153-
154-
status = self.info()["State"]["Status"]
155-
if status != "running":
156-
running = False
157-
158-
#with open(self.logfile, "w") as stdw:
159-
# stdw.write(self.get_log())
160-
161-
self._print("Container [%s] has executed successfully."%(self.name))
162162
uptime = seconds_hms(time.time() - tstart)
163163
self.uptime = uptime
164+
self._print("Container [%s] has executed successfully. \n Runtime was %s"%(self.name, uptime))
164165
self.log_container.update(self.info(), uptime=uptime)
165166

166167
self.container_logger = self.get_log()
@@ -171,19 +172,33 @@ def start(self):
171172
def stop(self):
172173
dinfo = self.info()
173174
status = dinfo["State"]["Status"]
175+
killed = False
174176
if status == "running":
175-
utils.xrun("docker stop", [self.name])
177+
try:
178+
utils.xrun("docker stop", [self.name])
179+
except KeyboardInterrupt("Received terminate signal. Will stop and remove container first"):
180+
killed = True
176181

177182
self._print("Container [self.name] has been")
178183
self.log_container.update(self.info(), self.uptime)
179184
self.log_container.write()
185+
if killed:
186+
self.remove()
187+
raise KeyboardInterrupt
180188

181189

182190
def remove(self):
183191
dinfo = self.info()
184192
status = dinfo["State"]["Status"]
193+
killed = False
185194
if status != "running":
186-
utils.xrun("docker rm", [self.name])
195+
try:
196+
utils.xrun("docker rm", [self.name])
197+
except KeyboardInterrupt:
198+
killed = True
199+
if killed:
200+
raise KeyboardInterrupt
201+
187202
else:
188203
raise DockerError("Container [%s] has not been stopped, cannot remove"%(self.name))
189204

stimela/recipe.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,7 @@ def run(self, steps=None, log=True):
202202
containers = [ self.containers[i-1] for i in steps[:len(self.containers)] ]
203203
else:
204204
containers = self.containers
205-
205+
206206
for i, container in enumerate(containers):
207207
self.log.info("Running Container %s"%container.name)
208208
self.log.info("STEP %d :: %s"%(i, container.label))

0 commit comments

Comments
 (0)