180 likes | 288 Views
Adapting Code. Introduction to OOP Code, what code? What & where Why adapt code anyway? How?. 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:
E N D
Adapting Code • Introduction to OOP • Code, what code? What & where • Why adapt code anyway? • How? Astro-WISE Tutorial, Leiden 18-20 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: • Let’s reduce a bias of ccd50 of the WFI instrument • Classes are blue-prints describing real world “things” • Can also map to abstract “things” Astro-WISE Tutorial, Leiden 18-20 August 2008
Introduction to OOP (2) • Advantages of using OOP: • Reuse of code • Readability of code • Bug tracking • Important concepts: • Inheritance • Polymorphism Astro-WISE Tutorial, Leiden 18-20 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 object awe> cat = Cat() awe> cat.age 3.0 awe> cat.age = 5.0 awe> cat.age 5.0 Astro-WISE Tutorial, Leiden 18-20 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() Astro-WISE Tutorial, Leiden 18-20 August 2008
Polymorphism class Mammal: def swim(self): .... polymorphism class Cat(Mammal): def swim(self): # head up, move paws class Dolphin(Mammal): def swim(self): # head down, move fins class Toddler(Mammal): def yell(self, txt): print txt.upper() + ‘!!’ def swim(self): self.yell(‘I can’t swim’) Astro-WISE Tutorial, Leiden 18-20 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 <ask representative, on request> >cvs –d cvs.astro-wise.org:/cvsroot co awe >cvs -d :pserver:anoncvs@cvs.astrowise.org:/cvsroot login ># base version >cvs -d :pserver:anoncvs@cvs.astro-wise.org:/cvsroot checkout -r AWBASE awe Astro-WISE Tutorial, Leiden 18-20 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) Astro-WISE Tutorial, Leiden 18-20 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 Astro-WISE Tutorial, Leiden 18-20 August 2008
Directory structure • Top level: awe • common, astro, lofar, ai • Isolate specific code Astro-WISE Tutorial, Leiden 18-20 August 2008
Directory structure: common config | Startup and environment/configuration filesdatabase | Persistency mechanism and implementations of its interfacelog | Logging and messagingmath | Mathematical Python routines such as statistics & least squaresnet | General network-related python modulesservices | Common (web-) servicestoolbox | Scripts for e.g. maintenance of the database, installation, etc.util | Routines for compression, checksumming, datetime manipulation. Astro-WISE Tutorial, Leiden 18-20 August 2008
Directory structure: astro config | Startup and environment/configuration filesdatabase | Astronomy specific database modulesexperimental | Experimental modulesexternal | Python wrappers for tools such as LDAC, Sextractor and Swarpfilerecipes | Data reduction recipes for use without databaseinstrument | Instrument specific modulesmain | Modules for the pipeline processing of imagesplot | Various plotting modulesrecipes | Data reduction recipes (to create classes in "main")services | (Web-) services for astronomytest | Unitteststoolbox | Scripts for ingestion, installation, various maintenanceutil | Utility modules usable throughout the code Astro-WISE Tutorial, Leiden 18-20 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 Astro-WISE Tutorial, Leiden 18-20 August 2008
Making a script (2/2) • Classes/models need to be imported • Dotted (directory) structure: • from astro.recipes.Reduce import ReduceTask Astro-WISE Tutorial, Leiden 18-20 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='2000-04-28', instrument='WFI', chip='ccd50')] ccd51 = [r.filename for r in RawBiasFrame.select(date='2000-04-28', instrument='WFI', chip='ccd51')] dpu.run('Bias', instrument='WFI', raw_filenames=ccd50) dpu.run('Bias', instrument='WFI', raw_filenames=ccd51) Astro-WISE Tutorial, Leiden 18-20 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() Astro-WISE Tutorial, Leiden 18-20 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') Astro-WISE Tutorial, Leiden 18-20 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 Astro-WISE Tutorial, Leiden 18-20 August 2008