Presentation is loading. Please wait.

Presentation is loading. Please wait.

Astro-WISE Tutorial, Leiden 18-20 August 2008 Adapting Code Introduction to OOP Code, what code? What & where Why adapt code anyway? How?

Similar presentations


Presentation on theme: "Astro-WISE Tutorial, Leiden 18-20 August 2008 Adapting Code Introduction to OOP Code, what code? What & where Why adapt code anyway? How?"— Presentation transcript:

1 Astro-WISE Tutorial, Leiden August 2008 Adapting Code Introduction to OOP Code, what code? What & where Why adapt code anyway? How?

2 Astro-WISE Tutorial, Leiden August 2008 Introduction to OOP (1) OOP is programming with classes Programming in the problem domain –Describe a problem in one sentence, then the nouns are the classes/objects: Lets reduce a bias of ccd50 of the WFI instrument –Classes are blue-prints describing real world things –Can also map to abstract things

3 Astro-WISE Tutorial, Leiden August 2008 Introduction to OOP (2) Advantages of using OOP: –Reuse of code –Readability of code –Bug tracking Important concepts: –Inheritance –Polymorphism

4 Astro-WISE Tutorial, Leiden August 2008 Structure of a class, members class Cat: age = 3.0 weight = 5.2 def miaw(self): print Miaw! def kill_other_cat(self, other):.... attribute method awe> cat = Cat() awe> cat.age 3.0 awe> cat.age = 5.0 awe> cat.age 5.0 object

5 Astro-WISE Tutorial, Leiden August 2008 Inheritance class Mammal: weight = -1 def breathe(self):.... class Cat(Mammal): def miaw(self): print Miaw! def kill_other_cat(self, other):.... inheritance cat = Cat() cat.breate()

6 Astro-WISE Tutorial, Leiden August 2008 Polymorphism class Cat(Mammal): def swim(self): # head up, move paws class Mammal: def swim(self):.... class Dolphin(Mammal): def swim(self): # head down, move fins polymorphism class Toddler(Mammal): def yell(self, txt): print txt.upper() + !! def swim(self): self.yell(I cant swim)

7 Astro-WISE Tutorial, Leiden August 2008 Getting a checkout CVS checkout = local copy of the code –Two versions: AWBASE and most recent Must use pserver to log in anonymously –User anoncvs, password >cvs –d cvs.astro-wise.org:/cvsroot co awe >cvs -d login ># base version >cvs -d checkout -r AWBASE awe

8 Astro-WISE Tutorial, Leiden August 2008 Using your own checkout Set environment AWEPIPE to point to the checkout directory (awe) > setenv AWEPIPE /data/users/helmich/awe (add to.cshrc)

9 Astro-WISE Tutorial, Leiden August 2008 Why adapt code? Hopefully not often necessary Make scripts to speed up process, and as a memory tool Create objects with your own methods

10 Astro-WISE Tutorial, Leiden August 2008 Directory structure Top level: awe common, astro, lofar, ai –Isolate specific code

11 Astro-WISE Tutorial, Leiden August 2008 Directory structure: common config| Startup and environment/configuration files database| Persistency mechanism and implementations of its interface log | Logging and messaging math | Mathematical Python routines such as statistics & least squares net | General network-related python modules services | Common (web-) services toolbox | Scripts for e.g. maintenance of the database, installation, etc. util | Routines for compression, checksumming, datetime manipulation.

12 Astro-WISE Tutorial, Leiden August 2008 Directory structure: astro config| Startup and environment/configuration files database | Astronomy specific database modules experimental| Experimental modules external | Python wrappers for tools such as LDAC, Sextractor and Swarp filerecipes | Data reduction recipes for use without database instrument | Instrument specific modules main | Modules for the pipeline processing of images plot | Various plotting modules recipes | Data reduction recipes (to create classes in "main") services | (Web-) services for astronomy test | Unittests toolbox | Scripts for ingestion, installation, various maintenance util | Utility modules usable throughout the code

13 Astro-WISE Tutorial, Leiden August 2008 Making a script (1/2) Based around dpu instance Based around a task As a sort of task Write the script in a file with.py extension Run with this command: > awe script.py

14 Astro-WISE Tutorial, Leiden August 2008 Making a script (2/2) Classes/models need to be imported Dotted (directory) structure: –from astro.recipes.Reduce import ReduceTask

15 Astro-WISE Tutorial, Leiden August 2008 Script based around dpu from astro.recipes.mods.dpu import Processor from astro.main.RawFrame import RawBiasFrame dpu = Processor('dpu.hpc.rug.astro-wise.org') ccd50 = [r.filename for r in RawBiasFrame.select(date=' ', instrument='WFI', chip='ccd50')] ccd51 = [r.filename for r in RawBiasFrame.select(date=' ', instrument='WFI', chip='ccd51')] dpu.run('Bias', instrument='WFI', raw_filenames=ccd50) dpu.run('Bias', instrument='WFI', raw_filenames=ccd51)

16 Astro-WISE Tutorial, Leiden August 2008 Script based around Task In particular to store configuration from astro.recipes.GalPhot import GalPhotTask from astro.util.Pars import Pars p=Pars(GalPhotTask) p.GalPhotModel.process_params.r1 = 10.0 p.GalPhotModel.process_params.dangmax=0.2 # etc. etc. task = GalPhotTask(instrument='WFI', slid=423431, sids=[16246], pars=p.get(), commit=0) task.execute()

17 Astro-WISE Tutorial, Leiden August 2008 Simple eclipse script Eclipse is used for image arithmetic import eclipse import glob filenames = glob.glob('*.red.fits') images = [eclipse.image.image(filename) for filename in filenames] cube = eclipse.cube.cube(images) median = cube.median() median.save('eclipse_med.fits')

18 Astro-WISE Tutorial, Leiden August 2008 Adapting classes ProcessTarget classes are located in astro.main –Lets look at BiasFrame: class BiasFrame(BaseFrame):.... def make_image(self).... cube = eclipse.cube.cube([raw.image for raw in self.raw_bias_frames]) # current method does an average with sigma rejection self.image = cube.median() # use a median instead


Download ppt "Astro-WISE Tutorial, Leiden 18-20 August 2008 Adapting Code Introduction to OOP Code, what code? What & where Why adapt code anyway? How?"

Similar presentations


Ads by Google